blob: 3720018e2b933d3b7795bf51b4a750fd63df4e4e [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
23 // Toolchain root path relative to the wrapper binary.
24 rootRelPath string
25 // Path of the old wrapper using the toolchain root.
Tobias Bosch900dbc92019-06-24 09:31:39 -070026 oldWrapperPath string
27 // Whether to mock out the calls that the old wrapper does.
28 mockOldWrapperCmds bool
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070029 // Directory to store errors that were prevented with -Wno-error.
30 newWarningsDir string
Tobias Boschd8aa0d02019-08-19 09:55:30 -070031 // Version. Only used for printing via -print-cmd.
32 version string
Tobias Boschef8f9692019-06-10 15:50:33 -070033}
34
Tobias Boschd8aa0d02019-08-19 09:55:30 -070035// Version can be set via a linker flag.
36// Values fills config.version.
37var Version = ""
38
Tobias Boschaa311162019-06-20 17:47:19 -070039// UseCCache can be set via a linker flag.
40// Value will be passed to strconv.ParseBool.
41// E.g. go build -ldflags '-X config.UseCCache=true'.
42var UseCCache = "unknown"
43
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070044// UseLlvmNext can be set via a linker flag.
45// Value will be passed to strconv.ParseBool.
46// E.g. go build -ldflags '-X config.UseLlvmNext=true'.
47var UseLlvmNext = "unknown"
48
Tobias Boschaa311162019-06-20 17:47:19 -070049// ConfigName can be set via a linker flag.
50// Value has to be one of:
51// - "cros.hardened"
52// - "cros.nonhardened"
53var ConfigName = "unknown"
54
55// Returns the configuration matching the UseCCache and ConfigName.
56func getRealConfig() (*config, error) {
57 useCCache, err := strconv.ParseBool(UseCCache)
58 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070059 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070060 }
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070061 useLlvmNext, err := strconv.ParseBool(UseLlvmNext)
62 if err != nil {
63 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext")
64 }
65 // FIXME: Remove comparison to old wrapper once the new wrapper has landed.
66 oldWrapperPath := ""
67 config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070068 if err != nil {
69 return nil, err
70 }
71 return config, nil
72}
73
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070074func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) {
75 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070076 switch configName {
77 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070078 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070079 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070080 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070081 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070082 cfg = *crosHostConfig
Tobias Boschbbb3c812019-09-30 10:11:25 -070083 case "android":
84 cfg = *androidConfig
Tobias Boschaa311162019-06-20 17:47:19 -070085 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070086 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070087 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070088 cfg.useCCache = useCCache
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070089 if useLlvmNext {
90 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
91 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070092 cfg.oldWrapperPath = oldWrapperPath
93 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070094 return &cfg, nil
95}
96
97var llvmNextFlags = []string{
98 "-Wno-reorder-init-list",
99 "-Wno-final-dtor-non-final-class",
100 "-Wno-implicit-int-float-conversion",
101 "-Wno-return-stack-address",
Denis Nikitin1fe62092019-10-03 16:33:17 -0700102 "-Werror=poison-system-directories",
Tobias Boschaa311162019-06-20 17:47:19 -0700103}
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.
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",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700135 },
136 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700137}
138
139// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700140var crosNonHardenedConfig = &config{
141 rootRelPath: "../../../../..",
142 commonFlags: []string{},
143 gccFlags: []string{
144 "-Wno-maybe-uninitialized",
145 "-Wno-unused-local-typedefs",
146 "-Wno-deprecated-declarations",
147 "-Wtrampolines",
148 },
149 // Temporarily disable tautological-*-compare chromium:778316.
150 // Temporarily add no-unknown-warning-option to deal with old clang versions.
151 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
152 clangFlags: []string{
153 "-Qunused-arguments",
154 "-Wno-tautological-constant-compare",
155 "-Wno-tautological-unsigned-enum-zero-compare",
156 "-Wno-unknown-warning-option",
157 "-Wno-section",
158 "-static-libgcc",
159 },
160 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700161}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700162
163// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700164var crosHostConfig = &config{
165 isHostWrapper: true,
166 rootRelPath: "../..",
167 commonFlags: []string{},
168 gccFlags: []string{
169 "-Wno-maybe-uninitialized",
170 "-Wno-unused-local-typedefs",
171 "-Wno-deprecated-declarations",
172 },
173 // Temporarily disable tautological-*-compare chromium:778316.
174 // Temporarily add no-unknown-warning-option to deal with old clang versions.
175 clangFlags: []string{
176 "-Qunused-arguments",
177 "-grecord-gcc-switches",
178 "-fno-addrsig",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700179 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700180 "-Wno-unused-local-typedefs",
181 "-Wno-deprecated-declarations",
182 "-Wno-tautological-constant-compare",
183 "-Wno-tautological-unsigned-enum-zero-compare",
184 "-Wno-unknown-warning-option",
185 },
186 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700187}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700188
189var androidConfig = &config{
190 isHostWrapper: false,
191 isAndroidWrapper: true,
192 rootRelPath: "./",
193 commonFlags: []string{},
194 gccFlags: []string{},
195 clangFlags: []string{},
196 newWarningsDir: "/tmp/fatal_clang_warnings",
197}