blob: 34bfc122a3685a00531eadd42c7bdf169c50658c [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 Boschef8f9692019-06-10 15:50:33 -070024}
25
Tobias Boschaa311162019-06-20 17:47:19 -070026// UseCCache can be set via a linker flag.
27// Value will be passed to strconv.ParseBool.
28// E.g. go build -ldflags '-X config.UseCCache=true'.
29var UseCCache = "unknown"
30
31// ConfigName can be set via a linker flag.
32// Value has to be one of:
33// - "cros.hardened"
34// - "cros.nonhardened"
35var ConfigName = "unknown"
36
37// Returns the configuration matching the UseCCache and ConfigName.
38func getRealConfig() (*config, error) {
39 useCCache, err := strconv.ParseBool(UseCCache)
40 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070041 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070042 }
43 config, err := getConfig(useCCache, ConfigName)
44 if err != nil {
45 return nil, err
46 }
47 return config, nil
48}
49
50func getConfig(useCCache bool, configName string) (*config, error) {
51 switch configName {
52 case "cros.hardened":
53 return getCrosHardenedConfig(useCCache), nil
54 case "cros.nonhardened":
55 return getCrosNonHardenedConfig(useCCache), nil
56 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070057 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070058 }
59}
60
Tobias Boschef8f9692019-06-10 15:50:33 -070061// Full hardening.
Tobias Boschaa311162019-06-20 17:47:19 -070062func getCrosHardenedConfig(useCCache bool) *config {
63 // Temporarily disable function splitting because of chromium:434751.
64 return &config{
65 useCCache: useCCache,
66 rootRelPath: "../../../../..",
67 oldWrapperPath: "./sysroot_wrapper.hardened.old",
68 commonFlags: []string{
69 "-fPIE",
70 "-D_FORTIFY_SOURCE=2",
71 "-fstack-protector-strong",
72 "-pie",
73 "-fno-omit-frame-pointer",
74 },
75 gccFlags: []string{
76 "-Wno-unused-local-typedefs",
77 "-Wno-maybe-uninitialized",
78 "-fno-reorder-blocks-and-partition",
79 },
80 // Temporarily disable tautological-*-compare chromium:778316.
81 // Temporarily add no-unknown-warning-option to deal with old clang versions.
82 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
83 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
84 clangFlags: []string{
85 "-Wno-tautological-unsigned-enum-zero-compare",
86 "-Qunused-arguments",
87 "-grecord-gcc-switches",
88 "-Wno-section",
89 "-Wno-unknown-warning-option",
90 "-fno-addrsig",
91 "-Wno-tautological-constant-compare",
92 },
93 }
Tobias Boschef8f9692019-06-10 15:50:33 -070094}
95
96// Flags to be added to non-hardened toolchain.
Tobias Boschaa311162019-06-20 17:47:19 -070097func getCrosNonHardenedConfig(useCCache bool) *config {
98 return &config{
99 useCCache: useCCache,
100 rootRelPath: "../../../../..",
101 oldWrapperPath: "./sysroot_wrapper.old",
102 commonFlags: []string{},
103 gccFlags: []string{
104 "-Wno-unused-local-typedefs",
105 "-Wno-maybe-uninitialized",
106 "-Wtrampolines",
107 "-Wno-deprecated-declarations",
108 },
109 // Temporarily disable tautological-*-compare chromium:778316.
110 // Temporarily add no-unknown-warning-option to deal with old clang versions.
111 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
112 clangFlags: []string{
113 "-Wno-unknown-warning-option",
114 "-Qunused-arguments",
115 "-Wno-section",
116 "-Wno-tautological-unsigned-enum-zero-compare",
117 "-Wno-tautological-constant-compare",
118 },
119 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700120}