blob: 4270cf5ebce76efcd227f420dc639d2ad8fac9cd [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.
13 isHostWrapper bool
Tobias Boschaa311162019-06-20 17:47:19 -070014 // Whether to use ccache.
15 useCCache bool
Tobias Boschef8f9692019-06-10 15:50:33 -070016 // Flags to add to gcc and clang.
17 commonFlags []string
18 // Flags to add to gcc only.
19 gccFlags []string
20 // Flags to add to clang only.
21 clangFlags []string
22 // Toolchain root path relative to the wrapper binary.
23 rootRelPath string
24 // Path of the old wrapper using the toolchain root.
Tobias Bosch900dbc92019-06-24 09:31:39 -070025 oldWrapperPath string
26 // Whether to mock out the calls that the old wrapper does.
27 mockOldWrapperCmds bool
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070028 // Directory to store errors that were prevented with -Wno-error.
29 newWarningsDir string
Tobias Boschd8aa0d02019-08-19 09:55:30 -070030 // Version. Only used for printing via -print-cmd.
31 version string
Tobias Boschef8f9692019-06-10 15:50:33 -070032}
33
Tobias Boschd8aa0d02019-08-19 09:55:30 -070034// Version can be set via a linker flag.
35// Values fills config.version.
36var Version = ""
37
Tobias Boschaa311162019-06-20 17:47:19 -070038// UseCCache can be set via a linker flag.
39// Value will be passed to strconv.ParseBool.
40// E.g. go build -ldflags '-X config.UseCCache=true'.
41var UseCCache = "unknown"
42
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070043// UseLlvmNext can be set via a linker flag.
44// Value will be passed to strconv.ParseBool.
45// E.g. go build -ldflags '-X config.UseLlvmNext=true'.
46var UseLlvmNext = "unknown"
47
Tobias Boschaa311162019-06-20 17:47:19 -070048// ConfigName can be set via a linker flag.
49// Value has to be one of:
50// - "cros.hardened"
51// - "cros.nonhardened"
52var ConfigName = "unknown"
53
54// Returns the configuration matching the UseCCache and ConfigName.
55func getRealConfig() (*config, error) {
56 useCCache, err := strconv.ParseBool(UseCCache)
57 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070058 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070059 }
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070060 useLlvmNext, err := strconv.ParseBool(UseLlvmNext)
61 if err != nil {
62 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext")
63 }
64 // FIXME: Remove comparison to old wrapper once the new wrapper has landed.
65 oldWrapperPath := ""
66 config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070067 if err != nil {
68 return nil, err
69 }
70 return config, nil
71}
72
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070073func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) {
74 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070075 switch configName {
76 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070077 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070078 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070079 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070080 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070081 cfg = *crosHostConfig
Tobias Boschaa311162019-06-20 17:47:19 -070082 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070083 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070084 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070085 cfg.useCCache = useCCache
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070086 if useLlvmNext {
87 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
88 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070089 cfg.oldWrapperPath = oldWrapperPath
90 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070091 return &cfg, nil
92}
93
94var llvmNextFlags = []string{
95 "-Wno-reorder-init-list",
96 "-Wno-final-dtor-non-final-class",
97 "-Wno-implicit-int-float-conversion",
98 "-Wno-return-stack-address",
Tobias Boschaa311162019-06-20 17:47:19 -070099}
100
Tobias Boschef8f9692019-06-10 15:50:33 -0700101// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700102// Temporarily disable function splitting because of chromium:434751.
103var crosHardenedConfig = &config{
104 rootRelPath: "../../../../..",
105 commonFlags: []string{
106 "-fstack-protector-strong",
107 "-fPIE",
108 "-pie",
109 "-D_FORTIFY_SOURCE=2",
110 "-fno-omit-frame-pointer",
111 },
112 gccFlags: []string{
113 "-fno-reorder-blocks-and-partition",
114 "-Wno-unused-local-typedefs",
115 "-Wno-maybe-uninitialized",
116 },
117 // Temporarily disable tautological-*-compare chromium:778316.
118 // Temporarily add no-unknown-warning-option to deal with old clang versions.
119 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
120 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
121 clangFlags: []string{
122 "-Qunused-arguments",
123 "-grecord-gcc-switches",
124 "-fno-addrsig",
125 "-Wno-tautological-constant-compare",
126 "-Wno-tautological-unsigned-enum-zero-compare",
127 "-Wno-unknown-warning-option",
128 "-Wno-section",
129 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700130 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700131 },
132 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700133}
134
135// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700136var crosNonHardenedConfig = &config{
137 rootRelPath: "../../../../..",
138 commonFlags: []string{},
139 gccFlags: []string{
140 "-Wno-maybe-uninitialized",
141 "-Wno-unused-local-typedefs",
142 "-Wno-deprecated-declarations",
143 "-Wtrampolines",
144 },
145 // Temporarily disable tautological-*-compare chromium:778316.
146 // Temporarily add no-unknown-warning-option to deal with old clang versions.
147 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
148 clangFlags: []string{
149 "-Qunused-arguments",
150 "-Wno-tautological-constant-compare",
151 "-Wno-tautological-unsigned-enum-zero-compare",
152 "-Wno-unknown-warning-option",
153 "-Wno-section",
154 "-static-libgcc",
155 },
156 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700157}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700158
159// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700160var crosHostConfig = &config{
161 isHostWrapper: true,
162 rootRelPath: "../..",
163 commonFlags: []string{},
164 gccFlags: []string{
165 "-Wno-maybe-uninitialized",
166 "-Wno-unused-local-typedefs",
167 "-Wno-deprecated-declarations",
168 },
169 // Temporarily disable tautological-*-compare chromium:778316.
170 // Temporarily add no-unknown-warning-option to deal with old clang versions.
171 clangFlags: []string{
172 "-Qunused-arguments",
173 "-grecord-gcc-switches",
174 "-fno-addrsig",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700175 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700176 "-Wno-unused-local-typedefs",
177 "-Wno-deprecated-declarations",
178 "-Wno-tautological-constant-compare",
179 "-Wno-tautological-unsigned-enum-zero-compare",
180 "-Wno-unknown-warning-option",
181 },
182 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700183}