blob: 92e503d9a7c5ca0b18663c1e9b15c41f237ebc74 [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 Bosch47f580f2019-08-14 15:15:21 -070038// OldWrapperPath can be set via a linker flag.
39// Value fills config.oldWrapperPath.
40var OldWrapperPath = ""
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
47// ConfigName can be set via a linker flag.
48// Value has to be one of:
49// - "cros.hardened"
50// - "cros.nonhardened"
51var ConfigName = "unknown"
52
53// Returns the configuration matching the UseCCache and ConfigName.
54func getRealConfig() (*config, error) {
55 useCCache, err := strconv.ParseBool(UseCCache)
56 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070057 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070058 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070059 config, err := getConfig(ConfigName, useCCache, OldWrapperPath, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070060 if err != nil {
61 return nil, err
62 }
63 return config, nil
64}
65
Tobias Boschd8aa0d02019-08-19 09:55:30 -070066func getConfig(configName string, useCCache bool, oldWrapperPath string, version string) (*config, error) {
67 var cfg *config
Tobias Boschaa311162019-06-20 17:47:19 -070068 switch configName {
69 case "cros.hardened":
Tobias Boschd8aa0d02019-08-19 09:55:30 -070070 cfg = crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070071 case "cros.nonhardened":
Tobias Boschd8aa0d02019-08-19 09:55:30 -070072 cfg = crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070073 case "cros.host":
Tobias Boschd8aa0d02019-08-19 09:55:30 -070074 cfg = crosHostConfig
Tobias Boschaa311162019-06-20 17:47:19 -070075 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070076 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070077 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070078 cfg.useCCache = useCCache
79 cfg.oldWrapperPath = oldWrapperPath
80 cfg.version = version
81 return cfg, nil
Tobias Boschaa311162019-06-20 17:47:19 -070082}
83
Tobias Boschef8f9692019-06-10 15:50:33 -070084// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -070085// Temporarily disable function splitting because of chromium:434751.
86var crosHardenedConfig = &config{
87 rootRelPath: "../../../../..",
88 commonFlags: []string{
89 "-fstack-protector-strong",
90 "-fPIE",
91 "-pie",
92 "-D_FORTIFY_SOURCE=2",
93 "-fno-omit-frame-pointer",
94 },
95 gccFlags: []string{
96 "-fno-reorder-blocks-and-partition",
97 "-Wno-unused-local-typedefs",
98 "-Wno-maybe-uninitialized",
99 },
100 // Temporarily disable tautological-*-compare chromium:778316.
101 // Temporarily add no-unknown-warning-option to deal with old clang versions.
102 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
103 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
104 clangFlags: []string{
105 "-Qunused-arguments",
106 "-grecord-gcc-switches",
107 "-fno-addrsig",
108 "-Wno-tautological-constant-compare",
109 "-Wno-tautological-unsigned-enum-zero-compare",
110 "-Wno-unknown-warning-option",
111 "-Wno-section",
112 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700113 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700114 },
115 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700116}
117
118// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700119var crosNonHardenedConfig = &config{
120 rootRelPath: "../../../../..",
121 commonFlags: []string{},
122 gccFlags: []string{
123 "-Wno-maybe-uninitialized",
124 "-Wno-unused-local-typedefs",
125 "-Wno-deprecated-declarations",
126 "-Wtrampolines",
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 clangFlags: []string{
132 "-Qunused-arguments",
133 "-Wno-tautological-constant-compare",
134 "-Wno-tautological-unsigned-enum-zero-compare",
135 "-Wno-unknown-warning-option",
136 "-Wno-section",
137 "-static-libgcc",
138 },
139 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700140}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700141
142// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700143var crosHostConfig = &config{
144 isHostWrapper: true,
145 rootRelPath: "../..",
146 commonFlags: []string{},
147 gccFlags: []string{
148 "-Wno-maybe-uninitialized",
149 "-Wno-unused-local-typedefs",
150 "-Wno-deprecated-declarations",
151 },
152 // Temporarily disable tautological-*-compare chromium:778316.
153 // Temporarily add no-unknown-warning-option to deal with old clang versions.
154 clangFlags: []string{
155 "-Qunused-arguments",
156 "-grecord-gcc-switches",
157 "-fno-addrsig",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700158 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700159 "-Wno-unused-local-typedefs",
160 "-Wno-deprecated-declarations",
161 "-Wno-tautological-constant-compare",
162 "-Wno-tautological-unsigned-enum-zero-compare",
163 "-Wno-unknown-warning-option",
164 },
165 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700166}