blob: 1e78fbef95f5db8422a2846f68501a14a5d39c33 [file] [log] [blame]
Tobias Boschef8f9692019-06-10 15:50:33 -07001package main
2
Tobias Boschaa311162019-06-20 17:47:19 -07003import (
4 "fmt"
5 "strconv"
6)
7
Tobias Boschef8f9692019-06-10 15:50:33 -07008type config struct {
Tobias Boschaa311162019-06-20 17:47:19 -07009 // Whether to use ccache.
10 useCCache bool
Tobias Boschef8f9692019-06-10 15:50:33 -070011 // Flags to add to gcc and clang.
12 commonFlags []string
13 // Flags to add to gcc only.
14 gccFlags []string
15 // Flags to add to clang only.
16 clangFlags []string
17 // Toolchain root path relative to the wrapper binary.
18 rootRelPath string
19 // Path of the old wrapper using the toolchain root.
20 oldWrapperPath string
21 overrideOldWrapperConfig bool
22}
23
Tobias Boschaa311162019-06-20 17:47:19 -070024// UseCCache can be set via a linker flag.
25// Value will be passed to strconv.ParseBool.
26// E.g. go build -ldflags '-X config.UseCCache=true'.
27var UseCCache = "unknown"
28
29// ConfigName can be set via a linker flag.
30// Value has to be one of:
31// - "cros.hardened"
32// - "cros.nonhardened"
33var ConfigName = "unknown"
34
35// Returns the configuration matching the UseCCache and ConfigName.
36func getRealConfig() (*config, error) {
37 useCCache, err := strconv.ParseBool(UseCCache)
38 if err != nil {
39 return nil, fmt.Errorf("Parse error for UseCCache: %s", err)
40 }
41 config, err := getConfig(useCCache, ConfigName)
42 if err != nil {
43 return nil, err
44 }
45 return config, nil
46}
47
48func getConfig(useCCache bool, configName string) (*config, error) {
49 switch configName {
50 case "cros.hardened":
51 return getCrosHardenedConfig(useCCache), nil
52 case "cros.nonhardened":
53 return getCrosNonHardenedConfig(useCCache), nil
54 default:
55 return nil, fmt.Errorf("Unknown config name: %s", configName)
56 }
57}
58
Tobias Boschef8f9692019-06-10 15:50:33 -070059// Full hardening.
Tobias Boschaa311162019-06-20 17:47:19 -070060func getCrosHardenedConfig(useCCache bool) *config {
61 // Temporarily disable function splitting because of chromium:434751.
62 return &config{
63 useCCache: useCCache,
64 rootRelPath: "../../../../..",
65 oldWrapperPath: "./sysroot_wrapper.hardened.old",
66 commonFlags: []string{
67 "-fPIE",
68 "-D_FORTIFY_SOURCE=2",
69 "-fstack-protector-strong",
70 "-pie",
71 "-fno-omit-frame-pointer",
72 },
73 gccFlags: []string{
74 "-Wno-unused-local-typedefs",
75 "-Wno-maybe-uninitialized",
76 "-fno-reorder-blocks-and-partition",
77 },
78 // Temporarily disable tautological-*-compare chromium:778316.
79 // Temporarily add no-unknown-warning-option to deal with old clang versions.
80 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
81 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
82 clangFlags: []string{
83 "-Wno-tautological-unsigned-enum-zero-compare",
84 "-Qunused-arguments",
85 "-grecord-gcc-switches",
86 "-Wno-section",
87 "-Wno-unknown-warning-option",
88 "-fno-addrsig",
89 "-Wno-tautological-constant-compare",
90 },
91 }
Tobias Boschef8f9692019-06-10 15:50:33 -070092}
93
94// Flags to be added to non-hardened toolchain.
Tobias Boschaa311162019-06-20 17:47:19 -070095func getCrosNonHardenedConfig(useCCache bool) *config {
96 return &config{
97 useCCache: useCCache,
98 rootRelPath: "../../../../..",
99 oldWrapperPath: "./sysroot_wrapper.old",
100 commonFlags: []string{},
101 gccFlags: []string{
102 "-Wno-unused-local-typedefs",
103 "-Wno-maybe-uninitialized",
104 "-Wtrampolines",
105 "-Wno-deprecated-declarations",
106 },
107 // Temporarily disable tautological-*-compare chromium:778316.
108 // Temporarily add no-unknown-warning-option to deal with old clang versions.
109 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
110 clangFlags: []string{
111 "-Wno-unknown-warning-option",
112 "-Qunused-arguments",
113 "-Wno-section",
114 "-Wno-tautological-unsigned-enum-zero-compare",
115 "-Wno-tautological-constant-compare",
116 },
117 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700118}