blob: 8b5432b8acd5aa7fdccd517e7a7620dce6230314 [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...)
Caroline Tice8a9125c2020-09-22 08:34:51 -070099 cfg.clangPostFlags = append(cfg.clangPostFlags, llvmNextPostFlags...)
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -0700100 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700101 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -0700102 return &cfg, nil
103}
104
Tobias Boschef8f9692019-06-10 15:50:33 -0700105// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700106// Temporarily disable function splitting because of chromium:434751.
107var crosHardenedConfig = &config{
108 rootRelPath: "../../../../..",
109 commonFlags: []string{
110 "-fstack-protector-strong",
111 "-fPIE",
112 "-pie",
113 "-D_FORTIFY_SOURCE=2",
114 "-fno-omit-frame-pointer",
115 },
116 gccFlags: []string{
117 "-fno-reorder-blocks-and-partition",
118 "-Wno-unused-local-typedefs",
119 "-Wno-maybe-uninitialized",
120 },
121 // Temporarily disable tautological-*-compare chromium:778316.
122 // Temporarily add no-unknown-warning-option to deal with old clang versions.
123 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
124 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700125 // Pass "-fcommon" till the packages are fixed to work with new clang default
126 // "-fno-common", crbug.com/1060413.
Bob Haarman94fd6222020-07-09 17:14:53 -0700127 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
128 // removed that flag for now.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700129 clangFlags: []string{
130 "-Qunused-arguments",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700131 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700132 "-fcommon",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700133 "-Wno-tautological-constant-compare",
134 "-Wno-tautological-unsigned-enum-zero-compare",
135 "-Wno-unknown-warning-option",
136 "-Wno-section",
137 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700138 "-fuse-ld=lld",
Caroline Ticeb9228602019-10-29 13:21:19 -0700139 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700140 "-Werror=poison-system-directories",
Jian Caid1a9a252020-07-29 18:03:33 -0700141 "-fexperimental-new-pass-manager",
inglorion4a8085e2020-11-25 17:18:50 -0800142 "-Wno-compound-token-split-by-macro",
Caroline Ticeb9228602019-10-29 13:21:19 -0700143 },
144 clangPostFlags: []string{
145 "-Wno-implicit-int-float-conversion",
inglorion4a8085e2020-11-25 17:18:50 -0800146 "-Wno-compound-token-split-by-space",
147 "-Wno-string-concatenation",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700148 },
Caroline Tice8a9125c2020-09-22 08:34:51 -0700149 newWarningsDir: "/tmp/fatal_clang_warnings",
150 triciumNitsDir: "/tmp/linting_output/clang-tidy",
George Burgess IV0a377f42020-08-05 15:03:36 -0700151 crashArtifactsDir: "/tmp/clang_crash_diagnostics",
Tobias Boschef8f9692019-06-10 15:50:33 -0700152}
153
154// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700155var crosNonHardenedConfig = &config{
156 rootRelPath: "../../../../..",
157 commonFlags: []string{},
158 gccFlags: []string{
159 "-Wno-maybe-uninitialized",
160 "-Wno-unused-local-typedefs",
161 "-Wno-deprecated-declarations",
162 "-Wtrampolines",
163 },
164 // Temporarily disable tautological-*-compare chromium:778316.
165 // Temporarily add no-unknown-warning-option to deal with old clang versions.
166 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
167 clangFlags: []string{
168 "-Qunused-arguments",
169 "-Wno-tautological-constant-compare",
170 "-Wno-tautological-unsigned-enum-zero-compare",
171 "-Wno-unknown-warning-option",
172 "-Wno-section",
173 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700174 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700175 "-Werror=poison-system-directories",
Jian Caid1a9a252020-07-29 18:03:33 -0700176 "-fexperimental-new-pass-manager",
inglorion4a8085e2020-11-25 17:18:50 -0800177 "-Wno-compound-token-split-by-macro",
Caroline Ticeb9228602019-10-29 13:21:19 -0700178 },
179 clangPostFlags: []string{
180 "-Wno-implicit-int-float-conversion",
inglorion4a8085e2020-11-25 17:18:50 -0800181 "-Wno-compound-token-split-by-space",
182 "-Wno-string-concatenation",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700183 },
George Burgess IV0a377f42020-08-05 15:03:36 -0700184 newWarningsDir: "/tmp/fatal_clang_warnings",
185 triciumNitsDir: "/tmp/linting_output/clang-tidy",
186 crashArtifactsDir: "/tmp/clang_crash_diagnostics",
Tobias Boschef8f9692019-06-10 15:50:33 -0700187}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700188
189// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700190var crosHostConfig = &config{
191 isHostWrapper: true,
192 rootRelPath: "../..",
193 commonFlags: []string{},
194 gccFlags: []string{
195 "-Wno-maybe-uninitialized",
196 "-Wno-unused-local-typedefs",
197 "-Wno-deprecated-declarations",
198 },
199 // Temporarily disable tautological-*-compare chromium:778316.
200 // Temporarily add no-unknown-warning-option to deal with old clang versions.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700201 // Pass "-fcommon" till the packages are fixed to work with new clang default
202 // "-fno-common", crbug.com/1060413.
Bob Haarman94fd6222020-07-09 17:14:53 -0700203 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
204 // removed that flag for now.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700205 clangFlags: []string{
206 "-Qunused-arguments",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700207 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700208 "-fcommon",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700209 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700210 "-Wno-unused-local-typedefs",
211 "-Wno-deprecated-declarations",
212 "-Wno-tautological-constant-compare",
213 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700214 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700215 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700216 "-Wno-unknown-warning-option",
Jian Caid1a9a252020-07-29 18:03:33 -0700217 "-fexperimental-new-pass-manager",
inglorion4a8085e2020-11-25 17:18:50 -0800218 "-Wno-compound-token-split-by-macro",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700219 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700220 clangPostFlags: []string{
221 "-Wno-implicit-int-float-conversion",
inglorion4a8085e2020-11-25 17:18:50 -0800222 "-Wno-compound-token-split-by-space",
223 "-Wno-string-concatenation",
Caroline Ticeb9228602019-10-29 13:21:19 -0700224 },
George Burgess IV0a377f42020-08-05 15:03:36 -0700225 newWarningsDir: "/tmp/fatal_clang_warnings",
226 triciumNitsDir: "/tmp/linting_output/clang-tidy",
227 crashArtifactsDir: "/tmp/clang_crash_diagnostics",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700228}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700229
230var androidConfig = &config{
George Burgess IV0a377f42020-08-05 15:03:36 -0700231 isHostWrapper: false,
232 isAndroidWrapper: true,
233 rootRelPath: "./",
234 commonFlags: []string{},
235 gccFlags: []string{},
236 clangFlags: []string{},
237 clangPostFlags: []string{},
238 newWarningsDir: "",
239 triciumNitsDir: "",
240 crashArtifactsDir: "",
Tobias Boschbbb3c812019-09-30 10:11:25 -0700241}