blob: 93a8cc53e8f29249803d4b165fa20bd5818f5054 [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
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070022 // Directory to store errors that were prevented with -Wno-error.
23 newWarningsDir string
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{
Tobias Boschaa311162019-06-20 17:47:19 -070069 "-fstack-protector-strong",
Tobias Bosch198a3c92019-07-17 04:22:34 -070070 "-fPIE",
Tobias Boschaa311162019-06-20 17:47:19 -070071 "-pie",
Tobias Bosch198a3c92019-07-17 04:22:34 -070072 "-D_FORTIFY_SOURCE=2",
Tobias Boschaa311162019-06-20 17:47:19 -070073 "-fno-omit-frame-pointer",
74 },
75 gccFlags: []string{
Tobias Bosch198a3c92019-07-17 04:22:34 -070076 "-fno-reorder-blocks-and-partition",
Tobias Boschaa311162019-06-20 17:47:19 -070077 "-Wno-unused-local-typedefs",
78 "-Wno-maybe-uninitialized",
Tobias Boschaa311162019-06-20 17:47:19 -070079 },
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{
Tobias Boschaa311162019-06-20 17:47:19 -070085 "-Qunused-arguments",
86 "-grecord-gcc-switches",
Tobias Boschaa311162019-06-20 17:47:19 -070087 "-fno-addrsig",
88 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -070089 "-Wno-tautological-unsigned-enum-zero-compare",
90 "-Wno-unknown-warning-option",
91 "-Wno-section",
Tobias Boschaa311162019-06-20 17:47:19 -070092 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070093 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -070094 }
Tobias Boschef8f9692019-06-10 15:50:33 -070095}
96
97// Flags to be added to non-hardened toolchain.
Tobias Boschaa311162019-06-20 17:47:19 -070098func getCrosNonHardenedConfig(useCCache bool) *config {
99 return &config{
100 useCCache: useCCache,
101 rootRelPath: "../../../../..",
102 oldWrapperPath: "./sysroot_wrapper.old",
103 commonFlags: []string{},
104 gccFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700105 "-Wno-maybe-uninitialized",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700106 "-Wno-unused-local-typedefs",
Tobias Boschaa311162019-06-20 17:47:19 -0700107 "-Wno-deprecated-declarations",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700108 "-Wtrampolines",
Tobias Boschaa311162019-06-20 17:47:19 -0700109 },
110 // Temporarily disable tautological-*-compare chromium:778316.
111 // Temporarily add no-unknown-warning-option to deal with old clang versions.
112 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
113 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700114 "-Qunused-arguments",
Tobias Boschaa311162019-06-20 17:47:19 -0700115 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700116 "-Wno-tautological-unsigned-enum-zero-compare",
117 "-Wno-unknown-warning-option",
118 "-Wno-section",
Tobias Boschaa311162019-06-20 17:47:19 -0700119 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700120 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700121 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700122}