blob: 7567d0e224b880e72cd8e788d6612e6b2ba6530c [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 Boschaa311162019-06-20 17:47:19 -070032// UseCCache can be set via a linker flag.
33// Value will be passed to strconv.ParseBool.
34// E.g. go build -ldflags '-X config.UseCCache=true'.
35var UseCCache = "unknown"
36
37// ConfigName can be set via a linker flag.
38// Value has to be one of:
39// - "cros.hardened"
40// - "cros.nonhardened"
41var ConfigName = "unknown"
42
43// Returns the configuration matching the UseCCache and ConfigName.
44func getRealConfig() (*config, error) {
45 useCCache, err := strconv.ParseBool(UseCCache)
46 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070047 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070048 }
49 config, err := getConfig(useCCache, ConfigName)
50 if err != nil {
51 return nil, err
52 }
53 return config, nil
54}
55
56func getConfig(useCCache bool, configName string) (*config, error) {
57 switch configName {
58 case "cros.hardened":
59 return getCrosHardenedConfig(useCCache), nil
60 case "cros.nonhardened":
61 return getCrosNonHardenedConfig(useCCache), nil
Tobias Bosch31dec2c2019-07-18 07:34:03 -070062 case "cros.host":
63 return getCrosHostConfig(), nil
Tobias Boschaa311162019-06-20 17:47:19 -070064 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070065 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070066 }
67}
68
Tobias Boschef8f9692019-06-10 15:50:33 -070069// Full hardening.
Tobias Boschaa311162019-06-20 17:47:19 -070070func getCrosHardenedConfig(useCCache bool) *config {
71 // Temporarily disable function splitting because of chromium:434751.
72 return &config{
73 useCCache: useCCache,
74 rootRelPath: "../../../../..",
75 oldWrapperPath: "./sysroot_wrapper.hardened.old",
76 commonFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070077 "-fstack-protector-strong",
Tobias Bosch198a3c92019-07-17 04:22:34 -070078 "-fPIE",
Tobias Boschaa311162019-06-20 17:47:19 -070079 "-pie",
Tobias Bosch198a3c92019-07-17 04:22:34 -070080 "-D_FORTIFY_SOURCE=2",
Tobias Boschaa311162019-06-20 17:47:19 -070081 "-fno-omit-frame-pointer",
82 },
83 gccFlags: []string{
Tobias Bosch198a3c92019-07-17 04:22:34 -070084 "-fno-reorder-blocks-and-partition",
Tobias Boschaa311162019-06-20 17:47:19 -070085 "-Wno-unused-local-typedefs",
86 "-Wno-maybe-uninitialized",
Tobias Boschaa311162019-06-20 17:47:19 -070087 },
88 // Temporarily disable tautological-*-compare chromium:778316.
89 // Temporarily add no-unknown-warning-option to deal with old clang versions.
90 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
91 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
92 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070093 "-Qunused-arguments",
94 "-grecord-gcc-switches",
Tobias Boschaa311162019-06-20 17:47:19 -070095 "-fno-addrsig",
96 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -070097 "-Wno-tautological-unsigned-enum-zero-compare",
98 "-Wno-unknown-warning-option",
99 "-Wno-section",
Tobias Bosche23905c2019-07-19 02:56:31 -0700100 "-static-libgcc",
Tobias Boschaa311162019-06-20 17:47:19 -0700101 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700102 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700103 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700104}
105
106// Flags to be added to non-hardened toolchain.
Tobias Boschaa311162019-06-20 17:47:19 -0700107func getCrosNonHardenedConfig(useCCache bool) *config {
108 return &config{
109 useCCache: useCCache,
110 rootRelPath: "../../../../..",
111 oldWrapperPath: "./sysroot_wrapper.old",
112 commonFlags: []string{},
113 gccFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700114 "-Wno-maybe-uninitialized",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700115 "-Wno-unused-local-typedefs",
Tobias Boschaa311162019-06-20 17:47:19 -0700116 "-Wno-deprecated-declarations",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700117 "-Wtrampolines",
Tobias Boschaa311162019-06-20 17:47:19 -0700118 },
119 // Temporarily disable tautological-*-compare chromium:778316.
120 // Temporarily add no-unknown-warning-option to deal with old clang versions.
121 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
122 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700123 "-Qunused-arguments",
Tobias Boschaa311162019-06-20 17:47:19 -0700124 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700125 "-Wno-tautological-unsigned-enum-zero-compare",
126 "-Wno-unknown-warning-option",
127 "-Wno-section",
Tobias Bosche23905c2019-07-19 02:56:31 -0700128 "-static-libgcc",
Tobias Boschaa311162019-06-20 17:47:19 -0700129 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700130 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700131 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700132}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700133
134// Flags to be added to host toolchain.
135func getCrosHostConfig() *config {
136 return &config{
137 isHostWrapper: true,
138 useCCache: false,
139 rootRelPath: "../..",
140 oldWrapperPath: "./host_wrapper.old",
141 commonFlags: []string{},
Tobias Boschb27c8f22019-07-19 06:53:07 -0700142 gccFlags: []string{
143 "-Wno-maybe-uninitialized",
144 "-Wno-unused-local-typedefs",
145 "-Wno-deprecated-declarations",
146 },
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700147 // Temporarily disable tautological-*-compare chromium:778316.
148 // Temporarily add no-unknown-warning-option to deal with old clang versions.
149 clangFlags: []string{
150 "-Qunused-arguments",
151 "-grecord-gcc-switches",
152 "-fno-addrsig",
153 "-Wno-unused-local-typedefs",
154 "-Wno-deprecated-declarations",
155 "-Wno-tautological-constant-compare",
156 "-Wno-tautological-unsigned-enum-zero-compare",
157 "-Wno-unknown-warning-option",
158 },
159 newWarningsDir: "/tmp/fatal_clang_warnings",
160 }
161}