blob: 546a0e118e0a60339d46669a26ba270a5ecce2a3 [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
Tobias Boschd8aa0d02019-08-19 09:55:30 -070034 // Version. Only used for printing via -print-cmd.
35 version string
Tobias Boschef8f9692019-06-10 15:50:33 -070036}
37
Tobias Boschd8aa0d02019-08-19 09:55:30 -070038// Version can be set via a linker flag.
39// Values fills config.version.
40var Version = ""
41
Tobias Boschaa311162019-06-20 17:47:19 -070042// UseCCache can be set via a linker flag.
43// Value will be passed to strconv.ParseBool.
44// E.g. go build -ldflags '-X config.UseCCache=true'.
45var UseCCache = "unknown"
46
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070047// UseLlvmNext can be set via a linker flag.
48// Value will be passed to strconv.ParseBool.
49// E.g. go build -ldflags '-X config.UseLlvmNext=true'.
50var UseLlvmNext = "unknown"
51
Tobias Boschaa311162019-06-20 17:47:19 -070052// ConfigName can be set via a linker flag.
53// Value has to be one of:
54// - "cros.hardened"
55// - "cros.nonhardened"
56var ConfigName = "unknown"
57
58// Returns the configuration matching the UseCCache and ConfigName.
59func getRealConfig() (*config, error) {
60 useCCache, err := strconv.ParseBool(UseCCache)
61 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070062 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070063 }
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070064 useLlvmNext, err := strconv.ParseBool(UseLlvmNext)
65 if err != nil {
66 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext")
67 }
Tobias Bosch8dd67e12019-10-28 14:26:51 -070068 config, err := getConfig(ConfigName, useCCache, useLlvmNext, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070069 if err != nil {
70 return nil, err
71 }
72 return config, nil
73}
74
George Burgess IV2efe72e2020-06-18 20:37:28 -070075func isAndroidConfig() bool {
76 return ConfigName == "android"
77}
78
Tobias Bosch8dd67e12019-10-28 14:26:51 -070079func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) {
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070080 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070081 switch configName {
82 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070083 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070084 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070085 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070086 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070087 cfg = *crosHostConfig
Tobias Boschbbb3c812019-09-30 10:11:25 -070088 case "android":
89 cfg = *androidConfig
Tobias Boschaa311162019-06-20 17:47:19 -070090 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070091 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070092 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070093 cfg.useCCache = useCCache
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -080094 cfg.useLlvmNext = useLlvmNext
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070095 if useLlvmNext {
96 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
97 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070098 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070099 return &cfg, nil
100}
101
Tobias Boschef8f9692019-06-10 15:50:33 -0700102// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700103// Temporarily disable function splitting because of chromium:434751.
104var crosHardenedConfig = &config{
105 rootRelPath: "../../../../..",
106 commonFlags: []string{
107 "-fstack-protector-strong",
108 "-fPIE",
109 "-pie",
110 "-D_FORTIFY_SOURCE=2",
111 "-fno-omit-frame-pointer",
112 },
113 gccFlags: []string{
114 "-fno-reorder-blocks-and-partition",
115 "-Wno-unused-local-typedefs",
116 "-Wno-maybe-uninitialized",
117 },
118 // Temporarily disable tautological-*-compare chromium:778316.
119 // Temporarily add no-unknown-warning-option to deal with old clang versions.
120 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
121 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700122 // Pass "-fcommon" till the packages are fixed to work with new clang default
123 // "-fno-common", crbug.com/1060413.
Bob Haarman94fd6222020-07-09 17:14:53 -0700124 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
125 // removed that flag for now.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700126 clangFlags: []string{
127 "-Qunused-arguments",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700128 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700129 "-fcommon",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700130 "-Wno-tautological-constant-compare",
131 "-Wno-tautological-unsigned-enum-zero-compare",
132 "-Wno-unknown-warning-option",
133 "-Wno-section",
134 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700135 "-fuse-ld=lld",
Caroline Ticeb9228602019-10-29 13:21:19 -0700136 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700137 "-Werror=poison-system-directories",
Jian Caidc996b92020-06-17 15:27:24 -0700138 "-fcrash-diagnostics-dir=/tmp/clang_crash_diagnostics",
Jian Caid1a9a252020-07-29 18:03:33 -0700139 "-fexperimental-new-pass-manager",
Caroline Ticeb9228602019-10-29 13:21:19 -0700140 },
141 clangPostFlags: []string{
142 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700143 },
144 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700145 triciumNitsDir: "/tmp/linting_output/clang-tidy",
Tobias Boschef8f9692019-06-10 15:50:33 -0700146}
147
148// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700149var crosNonHardenedConfig = &config{
150 rootRelPath: "../../../../..",
151 commonFlags: []string{},
152 gccFlags: []string{
153 "-Wno-maybe-uninitialized",
154 "-Wno-unused-local-typedefs",
155 "-Wno-deprecated-declarations",
156 "-Wtrampolines",
157 },
158 // Temporarily disable tautological-*-compare chromium:778316.
159 // Temporarily add no-unknown-warning-option to deal with old clang versions.
160 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
161 clangFlags: []string{
162 "-Qunused-arguments",
163 "-Wno-tautological-constant-compare",
164 "-Wno-tautological-unsigned-enum-zero-compare",
165 "-Wno-unknown-warning-option",
166 "-Wno-section",
167 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700168 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700169 "-Werror=poison-system-directories",
Jian Caidc996b92020-06-17 15:27:24 -0700170 "-fcrash-diagnostics-dir=/tmp/clang_crash_diagnostics",
Jian Caid1a9a252020-07-29 18:03:33 -0700171 "-fexperimental-new-pass-manager",
Caroline Ticeb9228602019-10-29 13:21:19 -0700172 },
173 clangPostFlags: []string{
174 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700175 },
176 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700177 triciumNitsDir: "/tmp/linting_output/clang-tidy",
Tobias Boschef8f9692019-06-10 15:50:33 -0700178}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700179
180// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700181var crosHostConfig = &config{
182 isHostWrapper: true,
183 rootRelPath: "../..",
184 commonFlags: []string{},
185 gccFlags: []string{
186 "-Wno-maybe-uninitialized",
187 "-Wno-unused-local-typedefs",
188 "-Wno-deprecated-declarations",
189 },
190 // Temporarily disable tautological-*-compare chromium:778316.
191 // Temporarily add no-unknown-warning-option to deal with old clang versions.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700192 // Pass "-fcommon" till the packages are fixed to work with new clang default
193 // "-fno-common", crbug.com/1060413.
Bob Haarman94fd6222020-07-09 17:14:53 -0700194 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
195 // removed that flag for now.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700196 clangFlags: []string{
197 "-Qunused-arguments",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700198 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700199 "-fcommon",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700200 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700201 "-Wno-unused-local-typedefs",
202 "-Wno-deprecated-declarations",
203 "-Wno-tautological-constant-compare",
204 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700205 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700206 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700207 "-Wno-unknown-warning-option",
Jian Caidc996b92020-06-17 15:27:24 -0700208 "-fcrash-diagnostics-dir=/tmp/clang_crash_diagnostics",
Jian Caid1a9a252020-07-29 18:03:33 -0700209 "-fexperimental-new-pass-manager",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700210 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700211 clangPostFlags: []string{
212 "-Wno-implicit-int-float-conversion",
213 },
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700214 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700215 triciumNitsDir: "/tmp/linting_output/clang-tidy",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700216}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700217
218var androidConfig = &config{
219 isHostWrapper: false,
220 isAndroidWrapper: true,
221 rootRelPath: "./",
222 commonFlags: []string{},
223 gccFlags: []string{},
224 clangFlags: []string{},
Caroline Tice8ac33e02019-10-28 09:48:18 -0700225 clangPostFlags: []string{},
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -0800226 newWarningsDir: "",
George Burgess IV163efaa2020-06-08 10:58:36 -0700227 triciumNitsDir: "",
Tobias Boschbbb3c812019-09-30 10:11:25 -0700228}