blob: 3c3668df169e50af82b1ff9ccf82f605ec319158 [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.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700124 clangFlags: []string{
125 "-Qunused-arguments",
126 "-grecord-gcc-switches",
127 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700128 "-fcommon",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700129 "-Wno-tautological-constant-compare",
130 "-Wno-tautological-unsigned-enum-zero-compare",
131 "-Wno-unknown-warning-option",
132 "-Wno-section",
133 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700134 "-fuse-ld=lld",
Caroline Ticeb9228602019-10-29 13:21:19 -0700135 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700136 "-Werror=poison-system-directories",
137 },
138 clangPostFlags: []string{
139 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700140 },
141 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700142 triciumNitsDir: "/tmp/linting_output/clang-tidy",
Tobias Boschef8f9692019-06-10 15:50:33 -0700143}
144
145// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700146var crosNonHardenedConfig = &config{
147 rootRelPath: "../../../../..",
148 commonFlags: []string{},
149 gccFlags: []string{
150 "-Wno-maybe-uninitialized",
151 "-Wno-unused-local-typedefs",
152 "-Wno-deprecated-declarations",
153 "-Wtrampolines",
154 },
155 // Temporarily disable tautological-*-compare chromium:778316.
156 // Temporarily add no-unknown-warning-option to deal with old clang versions.
157 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
158 clangFlags: []string{
159 "-Qunused-arguments",
160 "-Wno-tautological-constant-compare",
161 "-Wno-tautological-unsigned-enum-zero-compare",
162 "-Wno-unknown-warning-option",
163 "-Wno-section",
164 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700165 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700166 "-Werror=poison-system-directories",
167 },
168 clangPostFlags: []string{
169 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700170 },
171 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700172 triciumNitsDir: "/tmp/linting_output/clang-tidy",
Tobias Boschef8f9692019-06-10 15:50:33 -0700173}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700174
175// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700176var crosHostConfig = &config{
177 isHostWrapper: true,
178 rootRelPath: "../..",
179 commonFlags: []string{},
180 gccFlags: []string{
181 "-Wno-maybe-uninitialized",
182 "-Wno-unused-local-typedefs",
183 "-Wno-deprecated-declarations",
184 },
185 // Temporarily disable tautological-*-compare chromium:778316.
186 // Temporarily add no-unknown-warning-option to deal with old clang versions.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700187 // Pass "-fcommon" till the packages are fixed to work with new clang default
188 // "-fno-common", crbug.com/1060413.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700189 clangFlags: []string{
190 "-Qunused-arguments",
191 "-grecord-gcc-switches",
192 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700193 "-fcommon",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700194 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700195 "-Wno-unused-local-typedefs",
196 "-Wno-deprecated-declarations",
197 "-Wno-tautological-constant-compare",
198 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700199 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700200 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700201 "-Wno-unknown-warning-option",
202 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700203 clangPostFlags: []string{
204 "-Wno-implicit-int-float-conversion",
205 },
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700206 newWarningsDir: "/tmp/fatal_clang_warnings",
George Burgess IV163efaa2020-06-08 10:58:36 -0700207 triciumNitsDir: "/tmp/linting_output/clang-tidy",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700208}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700209
210var androidConfig = &config{
211 isHostWrapper: false,
212 isAndroidWrapper: true,
213 rootRelPath: "./",
214 commonFlags: []string{},
215 gccFlags: []string{},
216 clangFlags: []string{},
Caroline Tice8ac33e02019-10-28 09:48:18 -0700217 clangPostFlags: []string{},
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -0800218 newWarningsDir: "",
George Burgess IV163efaa2020-06-08 10:58:36 -0700219 triciumNitsDir: "",
Tobias Boschbbb3c812019-09-30 10:11:25 -0700220}