blob: 45e825756cc3a673d9c89e4c9d55b8da03d59752 [file] [log] [blame]
Tobias Boschef8f9692019-06-10 15:50:33 -07001package main
2
Tobias Boschaa311162019-06-20 17:47:19 -07003import (
Tobias Boschaa311162019-06-20 17:47:19 -07004 "strconv"
5)
6
Tobias Boschef8f9692019-06-10 15:50:33 -07007type config struct {
Tobias Boschaa311162019-06-20 17:47:19 -07008 // Whether to use ccache.
9 useCCache bool
Tobias Boschef8f9692019-06-10 15:50:33 -070010 // Flags to add to gcc and clang.
11 commonFlags []string
12 // Flags to add to gcc only.
13 gccFlags []string
14 // Flags to add to clang only.
15 clangFlags []string
16 // Toolchain root path relative to the wrapper binary.
17 rootRelPath string
18 // Path of the old wrapper using the toolchain root.
Tobias Bosch900dbc92019-06-24 09:31:39 -070019 oldWrapperPath string
20 // Whether to mock out the calls that the old wrapper does.
21 mockOldWrapperCmds bool
22 // Whether to overwrite the config in the old wrapper.
23 overwriteOldWrapperCfg bool
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070024 // Directory to store errors that were prevented with -Wno-error.
25 newWarningsDir string
Tobias Boschef8f9692019-06-10 15:50:33 -070026}
27
Tobias Boschaa311162019-06-20 17:47:19 -070028// UseCCache can be set via a linker flag.
29// Value will be passed to strconv.ParseBool.
30// E.g. go build -ldflags '-X config.UseCCache=true'.
31var UseCCache = "unknown"
32
33// ConfigName can be set via a linker flag.
34// Value has to be one of:
35// - "cros.hardened"
36// - "cros.nonhardened"
37var ConfigName = "unknown"
38
39// Returns the configuration matching the UseCCache and ConfigName.
40func getRealConfig() (*config, error) {
41 useCCache, err := strconv.ParseBool(UseCCache)
42 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070043 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070044 }
45 config, err := getConfig(useCCache, ConfigName)
46 if err != nil {
47 return nil, err
48 }
49 return config, nil
50}
51
52func getConfig(useCCache bool, configName string) (*config, error) {
53 switch configName {
54 case "cros.hardened":
55 return getCrosHardenedConfig(useCCache), nil
56 case "cros.nonhardened":
57 return getCrosNonHardenedConfig(useCCache), nil
58 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070059 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070060 }
61}
62
Tobias Boschef8f9692019-06-10 15:50:33 -070063// Full hardening.
Tobias Boschaa311162019-06-20 17:47:19 -070064func getCrosHardenedConfig(useCCache bool) *config {
65 // Temporarily disable function splitting because of chromium:434751.
66 return &config{
67 useCCache: useCCache,
68 rootRelPath: "../../../../..",
69 oldWrapperPath: "./sysroot_wrapper.hardened.old",
70 commonFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070071 "-fstack-protector-strong",
Tobias Bosch198a3c92019-07-17 04:22:34 -070072 "-fPIE",
Tobias Boschaa311162019-06-20 17:47:19 -070073 "-pie",
Tobias Bosch198a3c92019-07-17 04:22:34 -070074 "-D_FORTIFY_SOURCE=2",
Tobias Boschaa311162019-06-20 17:47:19 -070075 "-fno-omit-frame-pointer",
76 },
77 gccFlags: []string{
Tobias Bosch198a3c92019-07-17 04:22:34 -070078 "-fno-reorder-blocks-and-partition",
Tobias Boschaa311162019-06-20 17:47:19 -070079 "-Wno-unused-local-typedefs",
80 "-Wno-maybe-uninitialized",
Tobias Boschaa311162019-06-20 17:47:19 -070081 },
82 // Temporarily disable tautological-*-compare chromium:778316.
83 // Temporarily add no-unknown-warning-option to deal with old clang versions.
84 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
85 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
86 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070087 "-Qunused-arguments",
88 "-grecord-gcc-switches",
Tobias Boschaa311162019-06-20 17:47:19 -070089 "-fno-addrsig",
90 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -070091 "-Wno-tautological-unsigned-enum-zero-compare",
92 "-Wno-unknown-warning-option",
93 "-Wno-section",
Tobias Boschaa311162019-06-20 17:47:19 -070094 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070095 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -070096 }
Tobias Boschef8f9692019-06-10 15:50:33 -070097}
98
99// Flags to be added to non-hardened toolchain.
Tobias Boschaa311162019-06-20 17:47:19 -0700100func getCrosNonHardenedConfig(useCCache bool) *config {
101 return &config{
102 useCCache: useCCache,
103 rootRelPath: "../../../../..",
104 oldWrapperPath: "./sysroot_wrapper.old",
105 commonFlags: []string{},
106 gccFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700107 "-Wno-maybe-uninitialized",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700108 "-Wno-unused-local-typedefs",
Tobias Boschaa311162019-06-20 17:47:19 -0700109 "-Wno-deprecated-declarations",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700110 "-Wtrampolines",
Tobias Boschaa311162019-06-20 17:47:19 -0700111 },
112 // Temporarily disable tautological-*-compare chromium:778316.
113 // Temporarily add no-unknown-warning-option to deal with old clang versions.
114 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
115 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700116 "-Qunused-arguments",
Tobias Boschaa311162019-06-20 17:47:19 -0700117 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700118 "-Wno-tautological-unsigned-enum-zero-compare",
119 "-Wno-unknown-warning-option",
120 "-Wno-section",
Tobias Boschaa311162019-06-20 17:47:19 -0700121 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700122 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700123 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700124}