blob: 9b531e938b9df586e7c8af656938fd4dd8c3f026 [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...)
Caroline Tice8ac33e02019-10-28 09:48:18 -070094 cfg.clangPostFlags = append(cfg.clangPostFlags, llvmNextPostFlags...)
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070095 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070096 cfg.oldWrapperPath = oldWrapperPath
97 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070098 return &cfg, nil
99}
100
101var llvmNextFlags = []string{
102 "-Wno-reorder-init-list",
103 "-Wno-final-dtor-non-final-class",
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -0700104 "-Wno-return-stack-address",
Denis Nikitin1fe62092019-10-03 16:33:17 -0700105 "-Werror=poison-system-directories",
Tobias Boschaa311162019-06-20 17:47:19 -0700106}
107
Caroline Tice8ac33e02019-10-28 09:48:18 -0700108var llvmNextPostFlags = []string{
109 "-Wno-implicit-int-float-conversion",
110}
111
Tobias Boschef8f9692019-06-10 15:50:33 -0700112// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700113// Temporarily disable function splitting because of chromium:434751.
114var crosHardenedConfig = &config{
115 rootRelPath: "../../../../..",
116 commonFlags: []string{
117 "-fstack-protector-strong",
118 "-fPIE",
119 "-pie",
120 "-D_FORTIFY_SOURCE=2",
121 "-fno-omit-frame-pointer",
122 },
123 gccFlags: []string{
124 "-fno-reorder-blocks-and-partition",
125 "-Wno-unused-local-typedefs",
126 "-Wno-maybe-uninitialized",
127 },
128 // Temporarily disable tautological-*-compare chromium:778316.
129 // Temporarily add no-unknown-warning-option to deal with old clang versions.
130 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
131 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
132 clangFlags: []string{
133 "-Qunused-arguments",
134 "-grecord-gcc-switches",
135 "-fno-addrsig",
136 "-Wno-tautological-constant-compare",
137 "-Wno-tautological-unsigned-enum-zero-compare",
138 "-Wno-unknown-warning-option",
139 "-Wno-section",
140 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700141 "-fuse-ld=lld",
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",
166 },
167 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700168}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700169
170// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700171var crosHostConfig = &config{
172 isHostWrapper: true,
173 rootRelPath: "../..",
174 commonFlags: []string{},
175 gccFlags: []string{
176 "-Wno-maybe-uninitialized",
177 "-Wno-unused-local-typedefs",
178 "-Wno-deprecated-declarations",
179 },
180 // Temporarily disable tautological-*-compare chromium:778316.
181 // Temporarily add no-unknown-warning-option to deal with old clang versions.
182 clangFlags: []string{
183 "-Qunused-arguments",
184 "-grecord-gcc-switches",
185 "-fno-addrsig",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700186 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700187 "-Wno-unused-local-typedefs",
188 "-Wno-deprecated-declarations",
189 "-Wno-tautological-constant-compare",
190 "-Wno-tautological-unsigned-enum-zero-compare",
191 "-Wno-unknown-warning-option",
192 },
193 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700194}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700195
196var androidConfig = &config{
197 isHostWrapper: false,
198 isAndroidWrapper: true,
199 rootRelPath: "./",
200 commonFlags: []string{},
201 gccFlags: []string{},
202 clangFlags: []string{},
Caroline Tice8ac33e02019-10-28 09:48:18 -0700203 clangPostFlags: []string{},
Tobias Boschbbb3c812019-09-30 10:11:25 -0700204 newWarningsDir: "/tmp/fatal_clang_warnings",
205}