blob: 2a3c5971d893d6890c35a6b658b187831d97f0ce [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",
Tobias Boschaa311162019-06-20 17:47:19 -0700102}
103
Tobias Boschef8f9692019-06-10 15:50:33 -0700104// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700105// Temporarily disable function splitting because of chromium:434751.
106var crosHardenedConfig = &config{
107 rootRelPath: "../../../../..",
108 commonFlags: []string{
109 "-fstack-protector-strong",
110 "-fPIE",
111 "-pie",
112 "-D_FORTIFY_SOURCE=2",
113 "-fno-omit-frame-pointer",
114 },
115 gccFlags: []string{
116 "-fno-reorder-blocks-and-partition",
117 "-Wno-unused-local-typedefs",
118 "-Wno-maybe-uninitialized",
119 },
120 // Temporarily disable tautological-*-compare chromium:778316.
121 // Temporarily add no-unknown-warning-option to deal with old clang versions.
122 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
123 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
124 clangFlags: []string{
125 "-Qunused-arguments",
126 "-grecord-gcc-switches",
127 "-fno-addrsig",
128 "-Wno-tautological-constant-compare",
129 "-Wno-tautological-unsigned-enum-zero-compare",
130 "-Wno-unknown-warning-option",
131 "-Wno-section",
132 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700133 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700134 },
135 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700136}
137
138// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700139var crosNonHardenedConfig = &config{
140 rootRelPath: "../../../../..",
141 commonFlags: []string{},
142 gccFlags: []string{
143 "-Wno-maybe-uninitialized",
144 "-Wno-unused-local-typedefs",
145 "-Wno-deprecated-declarations",
146 "-Wtrampolines",
147 },
148 // Temporarily disable tautological-*-compare chromium:778316.
149 // Temporarily add no-unknown-warning-option to deal with old clang versions.
150 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
151 clangFlags: []string{
152 "-Qunused-arguments",
153 "-Wno-tautological-constant-compare",
154 "-Wno-tautological-unsigned-enum-zero-compare",
155 "-Wno-unknown-warning-option",
156 "-Wno-section",
157 "-static-libgcc",
158 },
159 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700160}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700161
162// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700163var crosHostConfig = &config{
164 isHostWrapper: true,
165 rootRelPath: "../..",
166 commonFlags: []string{},
167 gccFlags: []string{
168 "-Wno-maybe-uninitialized",
169 "-Wno-unused-local-typedefs",
170 "-Wno-deprecated-declarations",
171 },
172 // Temporarily disable tautological-*-compare chromium:778316.
173 // Temporarily add no-unknown-warning-option to deal with old clang versions.
174 clangFlags: []string{
175 "-Qunused-arguments",
176 "-grecord-gcc-switches",
177 "-fno-addrsig",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700178 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700179 "-Wno-unused-local-typedefs",
180 "-Wno-deprecated-declarations",
181 "-Wno-tautological-constant-compare",
182 "-Wno-tautological-unsigned-enum-zero-compare",
183 "-Wno-unknown-warning-option",
184 },
185 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700186}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700187
188var androidConfig = &config{
189 isHostWrapper: false,
190 isAndroidWrapper: true,
191 rootRelPath: "./",
192 commonFlags: []string{},
193 gccFlags: []string{},
194 clangFlags: []string{},
195 newWarningsDir: "/tmp/fatal_clang_warnings",
196}