blob: 3e4462012ce505ea4c3c3b0dd9d2e8e2d1a09f99 [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
Tobias Boschef8f9692019-06-10 15:50:33 -070017 // Flags to add to gcc and clang.
18 commonFlags []string
19 // Flags to add to gcc only.
20 gccFlags []string
21 // Flags to add to clang only.
22 clangFlags []string
Caroline Tice8ac33e02019-10-28 09:48:18 -070023 // Flags to add to clang only, AFTER user flags (cannot be overridden
24 // by the user).
25 clangPostFlags []string
Tobias Boschef8f9692019-06-10 15:50:33 -070026 // Toolchain root path relative to the wrapper binary.
27 rootRelPath string
28 // Path of the old wrapper using the toolchain root.
Tobias Bosch900dbc92019-06-24 09:31:39 -070029 oldWrapperPath string
30 // Whether to mock out the calls that the old wrapper does.
31 mockOldWrapperCmds bool
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070032 // Directory to store errors that were prevented with -Wno-error.
33 newWarningsDir 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 }
68 // FIXME: Remove comparison to old wrapper once the new wrapper has landed.
69 oldWrapperPath := ""
70 config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070071 if err != nil {
72 return nil, err
73 }
74 return config, nil
75}
76
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070077func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) {
78 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070079 switch configName {
80 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070081 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070082 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070083 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070084 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070085 cfg = *crosHostConfig
Tobias Boschbbb3c812019-09-30 10:11:25 -070086 case "android":
87 cfg = *androidConfig
Tobias Boschaa311162019-06-20 17:47:19 -070088 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070089 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070090 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070091 cfg.useCCache = useCCache
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070092 if useLlvmNext {
93 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
94 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070095 cfg.oldWrapperPath = oldWrapperPath
96 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070097 return &cfg, nil
98}
99
Caroline Ticeb9228602019-10-29 13:21:19 -0700100// TODO: Enable test in config_test.go, once we have new llvm-next flags.
101var llvmNextFlags = []string{}
Tobias Boschaa311162019-06-20 17:47:19 -0700102
Caroline Ticeb9228602019-10-29 13:21:19 -0700103var llvmNextPostFlags = []string{}
Caroline Tice8ac33e02019-10-28 09:48:18 -0700104
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.
125 clangFlags: []string{
126 "-Qunused-arguments",
127 "-grecord-gcc-switches",
128 "-fno-addrsig",
129 "-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-reorder-init-list",
136 "-Wno-final-dtor-non-final-class",
137 "-Wno-return-stack-address",
138 "-Werror=poison-system-directories",
139 },
140 clangPostFlags: []string{
141 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700142 },
143 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700144}
145
146// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700147var crosNonHardenedConfig = &config{
148 rootRelPath: "../../../../..",
149 commonFlags: []string{},
150 gccFlags: []string{
151 "-Wno-maybe-uninitialized",
152 "-Wno-unused-local-typedefs",
153 "-Wno-deprecated-declarations",
154 "-Wtrampolines",
155 },
156 // Temporarily disable tautological-*-compare chromium:778316.
157 // Temporarily add no-unknown-warning-option to deal with old clang versions.
158 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
159 clangFlags: []string{
160 "-Qunused-arguments",
161 "-Wno-tautological-constant-compare",
162 "-Wno-tautological-unsigned-enum-zero-compare",
163 "-Wno-unknown-warning-option",
164 "-Wno-section",
165 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700166 "-Wno-reorder-init-list",
167 "-Wno-final-dtor-non-final-class",
168 "-Wno-return-stack-address",
169 "-Werror=poison-system-directories",
170 },
171 clangPostFlags: []string{
172 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700173 },
174 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700175}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700176
177// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700178var crosHostConfig = &config{
179 isHostWrapper: true,
180 rootRelPath: "../..",
181 commonFlags: []string{},
182 gccFlags: []string{
183 "-Wno-maybe-uninitialized",
184 "-Wno-unused-local-typedefs",
185 "-Wno-deprecated-declarations",
186 },
187 // Temporarily disable tautological-*-compare chromium:778316.
188 // Temporarily add no-unknown-warning-option to deal with old clang versions.
189 clangFlags: []string{
190 "-Qunused-arguments",
191 "-grecord-gcc-switches",
192 "-fno-addrsig",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700193 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700194 "-Wno-unused-local-typedefs",
195 "-Wno-deprecated-declarations",
196 "-Wno-tautological-constant-compare",
197 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700198 "-Wno-reorder-init-list",
199 "-Wno-final-dtor-non-final-class",
200 "-Wno-return-stack-address",
201 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700202 "-Wno-unknown-warning-option",
203 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700204 clangPostFlags: []string{
205 "-Wno-implicit-int-float-conversion",
206 },
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700207 newWarningsDir: "/tmp/fatal_clang_warnings",
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{},
Tobias Boschbbb3c812019-09-30 10:11:25 -0700218 newWarningsDir: "/tmp/fatal_clang_warnings",
219}