blob: 10a69679a43101472e9047ba323f4049b7e5155b [file] [log] [blame]
Tobias Boschcfa8c242019-07-19 03:29:40 -07001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tobias Boschef8f9692019-06-10 15:50:33 -07005package main
6
Tobias Boschaa311162019-06-20 17:47:19 -07007import (
Tobias Boschaa311162019-06-20 17:47:19 -07008 "strconv"
9)
10
Tobias Boschef8f9692019-06-10 15:50:33 -070011type config struct {
Tobias Bosch31dec2c2019-07-18 07:34:03 -070012 // TODO: Refactor this flag into more generic configuration properties.
Tobias Boschbbb3c812019-09-30 10:11:25 -070013 isHostWrapper bool
14 isAndroidWrapper bool
Tobias Boschaa311162019-06-20 17:47:19 -070015 // Whether to use ccache.
16 useCCache bool
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -080017 // Whether llvmNext wrapper.
18 useLlvmNext bool
Tobias Boschef8f9692019-06-10 15:50:33 -070019 // Flags to add to gcc and clang.
20 commonFlags []string
21 // Flags to add to gcc only.
22 gccFlags []string
23 // Flags to add to clang only.
24 clangFlags []string
Caroline Tice8ac33e02019-10-28 09:48:18 -070025 // Flags to add to clang only, AFTER user flags (cannot be overridden
26 // by the user).
27 clangPostFlags []string
Tobias Boschef8f9692019-06-10 15:50:33 -070028 // Toolchain root path relative to the wrapper binary.
29 rootRelPath string
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070030 // Directory to store errors that were prevented with -Wno-error.
31 newWarningsDir string
George Burgess IV163efaa2020-06-08 10:58:36 -070032 // Directory to store nits in when using `WITH_TIDY=tricium`.
33 triciumNitsDir string
George Burgess IV0a377f42020-08-05 15:03:36 -070034 // Directory to store crash artifacts in.
35 crashArtifactsDir string
Tobias Boschd8aa0d02019-08-19 09:55:30 -070036 // Version. Only used for printing via -print-cmd.
37 version string
Tobias Boschef8f9692019-06-10 15:50:33 -070038}
39
Tobias Boschd8aa0d02019-08-19 09:55:30 -070040// Version can be set via a linker flag.
41// Values fills config.version.
42var Version = ""
43
Tobias Boschaa311162019-06-20 17:47:19 -070044// UseCCache can be set via a linker flag.
45// Value will be passed to strconv.ParseBool.
46// E.g. go build -ldflags '-X config.UseCCache=true'.
47var UseCCache = "unknown"
48
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070049// UseLlvmNext can be set via a linker flag.
50// Value will be passed to strconv.ParseBool.
51// E.g. go build -ldflags '-X config.UseLlvmNext=true'.
52var UseLlvmNext = "unknown"
53
Tobias Boschaa311162019-06-20 17:47:19 -070054// ConfigName can be set via a linker flag.
55// Value has to be one of:
56// - "cros.hardened"
57// - "cros.nonhardened"
58var ConfigName = "unknown"
59
60// Returns the configuration matching the UseCCache and ConfigName.
61func getRealConfig() (*config, error) {
62 useCCache, err := strconv.ParseBool(UseCCache)
63 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070064 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070065 }
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070066 useLlvmNext, err := strconv.ParseBool(UseLlvmNext)
67 if err != nil {
68 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext")
69 }
Tobias Bosch8dd67e12019-10-28 14:26:51 -070070 config, err := getConfig(ConfigName, useCCache, useLlvmNext, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070071 if err != nil {
72 return nil, err
73 }
74 return config, nil
75}
76
George Burgess IV2efe72e2020-06-18 20:37:28 -070077func isAndroidConfig() bool {
78 return ConfigName == "android"
79}
80
Tobias Bosch8dd67e12019-10-28 14:26:51 -070081func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) {
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070082 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070083 switch configName {
84 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070085 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070086 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070087 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070088 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070089 cfg = *crosHostConfig
Tobias Boschbbb3c812019-09-30 10:11:25 -070090 case "android":
91 cfg = *androidConfig
Tobias Boschaa311162019-06-20 17:47:19 -070092 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070093 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070094 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070095 cfg.useCCache = useCCache
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -080096 cfg.useLlvmNext = useLlvmNext
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070097 if useLlvmNext {
98 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
99 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700100 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -0700101 return &cfg, nil
102}
103
Tobias Boschef8f9692019-06-10 15:50:33 -0700104// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700105// Temporarily disable function splitting because of chromium:434751.
106var crosHardenedConfig = &config{
107 rootRelPath: "../../../../..",
108 commonFlags: []string{
109 "-fstack-protector-strong",
110 "-fPIE",
111 "-pie",
112 "-D_FORTIFY_SOURCE=2",
113 "-fno-omit-frame-pointer",
114 },
115 gccFlags: []string{
116 "-fno-reorder-blocks-and-partition",
117 "-Wno-unused-local-typedefs",
118 "-Wno-maybe-uninitialized",
119 },
120 // Temporarily disable tautological-*-compare chromium:778316.
121 // Temporarily add no-unknown-warning-option to deal with old clang versions.
122 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
123 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700124 // Pass "-fcommon" till the packages are fixed to work with new clang default
125 // "-fno-common", crbug.com/1060413.
Bob Haarman94fd6222020-07-09 17:14:53 -0700126 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
127 // removed that flag for now.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700128 clangFlags: []string{
129 "-Qunused-arguments",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700130 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700131 "-fcommon",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700132 "-Wno-tautological-constant-compare",
133 "-Wno-tautological-unsigned-enum-zero-compare",
134 "-Wno-unknown-warning-option",
135 "-Wno-section",
136 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700137 "-fuse-ld=lld",
Caroline Ticeb9228602019-10-29 13:21:19 -0700138 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700139 "-Werror=poison-system-directories",
Jian Caid1a9a252020-07-29 18:03:33 -0700140 "-fexperimental-new-pass-manager",
Caroline Ticeb9228602019-10-29 13:21:19 -0700141 },
142 clangPostFlags: []string{
143 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700144 },
145 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700146 triciumNitsDir: "/tmp/linting_output/clang-tidy",
George Burgess IV0a377f42020-08-05 15:03:36 -0700147 crashArtifactsDir: "/tmp/clang_crash_diagnostics",
Tobias Boschef8f9692019-06-10 15:50:33 -0700148}
149
150// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700151var crosNonHardenedConfig = &config{
152 rootRelPath: "../../../../..",
153 commonFlags: []string{},
154 gccFlags: []string{
155 "-Wno-maybe-uninitialized",
156 "-Wno-unused-local-typedefs",
157 "-Wno-deprecated-declarations",
158 "-Wtrampolines",
159 },
160 // Temporarily disable tautological-*-compare chromium:778316.
161 // Temporarily add no-unknown-warning-option to deal with old clang versions.
162 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
163 clangFlags: []string{
164 "-Qunused-arguments",
165 "-Wno-tautological-constant-compare",
166 "-Wno-tautological-unsigned-enum-zero-compare",
167 "-Wno-unknown-warning-option",
168 "-Wno-section",
169 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700170 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700171 "-Werror=poison-system-directories",
Jian Caid1a9a252020-07-29 18:03:33 -0700172 "-fexperimental-new-pass-manager",
Caroline Ticeb9228602019-10-29 13:21:19 -0700173 },
174 clangPostFlags: []string{
175 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700176 },
George Burgess IV0a377f42020-08-05 15:03:36 -0700177 newWarningsDir: "/tmp/fatal_clang_warnings",
178 triciumNitsDir: "/tmp/linting_output/clang-tidy",
179 crashArtifactsDir: "/tmp/clang_crash_diagnostics",
Tobias Boschef8f9692019-06-10 15:50:33 -0700180}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700181
182// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700183var crosHostConfig = &config{
184 isHostWrapper: true,
185 rootRelPath: "../..",
186 commonFlags: []string{},
187 gccFlags: []string{
188 "-Wno-maybe-uninitialized",
189 "-Wno-unused-local-typedefs",
190 "-Wno-deprecated-declarations",
191 },
192 // Temporarily disable tautological-*-compare chromium:778316.
193 // Temporarily add no-unknown-warning-option to deal with old clang versions.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700194 // Pass "-fcommon" till the packages are fixed to work with new clang default
195 // "-fno-common", crbug.com/1060413.
Bob Haarman94fd6222020-07-09 17:14:53 -0700196 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
197 // removed that flag for now.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700198 clangFlags: []string{
199 "-Qunused-arguments",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700200 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700201 "-fcommon",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700202 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700203 "-Wno-unused-local-typedefs",
204 "-Wno-deprecated-declarations",
205 "-Wno-tautological-constant-compare",
206 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700207 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700208 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700209 "-Wno-unknown-warning-option",
Jian Caid1a9a252020-07-29 18:03:33 -0700210 "-fexperimental-new-pass-manager",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700211 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700212 clangPostFlags: []string{
213 "-Wno-implicit-int-float-conversion",
214 },
George Burgess IV0a377f42020-08-05 15:03:36 -0700215 newWarningsDir: "/tmp/fatal_clang_warnings",
216 triciumNitsDir: "/tmp/linting_output/clang-tidy",
217 crashArtifactsDir: "/tmp/clang_crash_diagnostics",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700218}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700219
220var androidConfig = &config{
George Burgess IV0a377f42020-08-05 15:03:36 -0700221 isHostWrapper: false,
222 isAndroidWrapper: true,
223 rootRelPath: "./",
224 commonFlags: []string{},
225 gccFlags: []string{},
226 clangFlags: []string{},
227 clangPostFlags: []string{},
228 newWarningsDir: "",
229 triciumNitsDir: "",
230 crashArtifactsDir: "",
Tobias Boschbbb3c812019-09-30 10:11:25 -0700231}