blob: 70d08c15008fc6e8fe5f9941851ec92b22502af5 [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 Boschef8f9692019-06-10 15:50:33 -070030}
31
Tobias Bosch47f580f2019-08-14 15:15:21 -070032// OldWrapperPath can be set via a linker flag.
33// Value fills config.oldWrapperPath.
34var OldWrapperPath = ""
35
Tobias Boschaa311162019-06-20 17:47:19 -070036// UseCCache can be set via a linker flag.
37// Value will be passed to strconv.ParseBool.
38// E.g. go build -ldflags '-X config.UseCCache=true'.
39var UseCCache = "unknown"
40
41// ConfigName can be set via a linker flag.
42// Value has to be one of:
43// - "cros.hardened"
44// - "cros.nonhardened"
45var ConfigName = "unknown"
46
47// Returns the configuration matching the UseCCache and ConfigName.
48func getRealConfig() (*config, error) {
49 useCCache, err := strconv.ParseBool(UseCCache)
50 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070051 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070052 }
Tobias Bosch47f580f2019-08-14 15:15:21 -070053 config, err := getConfig(useCCache, ConfigName, OldWrapperPath)
Tobias Boschaa311162019-06-20 17:47:19 -070054 if err != nil {
55 return nil, err
56 }
57 return config, nil
58}
59
Tobias Bosch47f580f2019-08-14 15:15:21 -070060func getConfig(useCCache bool, configName string, oldWrapperPath string) (*config, error) {
Tobias Boschaa311162019-06-20 17:47:19 -070061 switch configName {
62 case "cros.hardened":
Tobias Bosch47f580f2019-08-14 15:15:21 -070063 return getCrosHardenedConfig(useCCache, oldWrapperPath), nil
Tobias Boschaa311162019-06-20 17:47:19 -070064 case "cros.nonhardened":
Tobias Bosch47f580f2019-08-14 15:15:21 -070065 return getCrosNonHardenedConfig(useCCache, oldWrapperPath), nil
Tobias Bosch31dec2c2019-07-18 07:34:03 -070066 case "cros.host":
Tobias Bosch47f580f2019-08-14 15:15:21 -070067 return getCrosHostConfig(oldWrapperPath), nil
Tobias Boschaa311162019-06-20 17:47:19 -070068 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070069 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070070 }
71}
72
Tobias Boschef8f9692019-06-10 15:50:33 -070073// Full hardening.
Tobias Bosch47f580f2019-08-14 15:15:21 -070074func getCrosHardenedConfig(useCCache bool, oldWrapperPath string) *config {
Tobias Boschaa311162019-06-20 17:47:19 -070075 // Temporarily disable function splitting because of chromium:434751.
76 return &config{
77 useCCache: useCCache,
78 rootRelPath: "../../../../..",
Tobias Bosch47f580f2019-08-14 15:15:21 -070079 oldWrapperPath: oldWrapperPath,
Tobias Boschaa311162019-06-20 17:47:19 -070080 commonFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070081 "-fstack-protector-strong",
Tobias Bosch198a3c92019-07-17 04:22:34 -070082 "-fPIE",
Tobias Boschaa311162019-06-20 17:47:19 -070083 "-pie",
Tobias Bosch198a3c92019-07-17 04:22:34 -070084 "-D_FORTIFY_SOURCE=2",
Tobias Boschaa311162019-06-20 17:47:19 -070085 "-fno-omit-frame-pointer",
86 },
87 gccFlags: []string{
Tobias Bosch198a3c92019-07-17 04:22:34 -070088 "-fno-reorder-blocks-and-partition",
Tobias Boschaa311162019-06-20 17:47:19 -070089 "-Wno-unused-local-typedefs",
90 "-Wno-maybe-uninitialized",
Tobias Boschaa311162019-06-20 17:47:19 -070091 },
92 // Temporarily disable tautological-*-compare chromium:778316.
93 // Temporarily add no-unknown-warning-option to deal with old clang versions.
94 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
95 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
96 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070097 "-Qunused-arguments",
98 "-grecord-gcc-switches",
Tobias Boschaa311162019-06-20 17:47:19 -070099 "-fno-addrsig",
100 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700101 "-Wno-tautological-unsigned-enum-zero-compare",
102 "-Wno-unknown-warning-option",
103 "-Wno-section",
Tobias Bosche23905c2019-07-19 02:56:31 -0700104 "-static-libgcc",
Tobias Boschaa311162019-06-20 17:47:19 -0700105 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700106 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700107 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700108}
109
110// Flags to be added to non-hardened toolchain.
Tobias Bosch47f580f2019-08-14 15:15:21 -0700111func getCrosNonHardenedConfig(useCCache bool, oldWrapperPath string) *config {
Tobias Boschaa311162019-06-20 17:47:19 -0700112 return &config{
113 useCCache: useCCache,
114 rootRelPath: "../../../../..",
Tobias Bosch47f580f2019-08-14 15:15:21 -0700115 oldWrapperPath: oldWrapperPath,
Tobias Boschaa311162019-06-20 17:47:19 -0700116 commonFlags: []string{},
117 gccFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700118 "-Wno-maybe-uninitialized",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700119 "-Wno-unused-local-typedefs",
Tobias Boschaa311162019-06-20 17:47:19 -0700120 "-Wno-deprecated-declarations",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700121 "-Wtrampolines",
Tobias Boschaa311162019-06-20 17:47:19 -0700122 },
123 // Temporarily disable tautological-*-compare chromium:778316.
124 // Temporarily add no-unknown-warning-option to deal with old clang versions.
125 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
126 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700127 "-Qunused-arguments",
Tobias Boschaa311162019-06-20 17:47:19 -0700128 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700129 "-Wno-tautological-unsigned-enum-zero-compare",
130 "-Wno-unknown-warning-option",
131 "-Wno-section",
Tobias Bosche23905c2019-07-19 02:56:31 -0700132 "-static-libgcc",
Tobias Boschaa311162019-06-20 17:47:19 -0700133 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700134 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700135 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700136}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700137
138// Flags to be added to host toolchain.
Tobias Bosch47f580f2019-08-14 15:15:21 -0700139func getCrosHostConfig(oldWrapperPath string) *config {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700140 return &config{
141 isHostWrapper: true,
142 useCCache: false,
143 rootRelPath: "../..",
Tobias Bosch47f580f2019-08-14 15:15:21 -0700144 oldWrapperPath: oldWrapperPath,
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700145 commonFlags: []string{},
Tobias Boschb27c8f22019-07-19 06:53:07 -0700146 gccFlags: []string{
147 "-Wno-maybe-uninitialized",
148 "-Wno-unused-local-typedefs",
149 "-Wno-deprecated-declarations",
150 },
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700151 // Temporarily disable tautological-*-compare chromium:778316.
152 // Temporarily add no-unknown-warning-option to deal with old clang versions.
153 clangFlags: []string{
154 "-Qunused-arguments",
155 "-grecord-gcc-switches",
156 "-fno-addrsig",
157 "-Wno-unused-local-typedefs",
158 "-Wno-deprecated-declarations",
159 "-Wno-tautological-constant-compare",
160 "-Wno-tautological-unsigned-enum-zero-compare",
161 "-Wno-unknown-warning-option",
162 },
163 newWarningsDir: "/tmp/fatal_clang_warnings",
164 }
165}