Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 3 | import ( |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 4 | "strconv" |
| 5 | ) |
| 6 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 7 | type config struct { |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 8 | // Whether to use ccache. |
| 9 | useCCache bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 10 | // 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 Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 19 | 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 Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame^] | 24 | // Directory to store errors that were prevented with -Wno-error. |
| 25 | newWarningsDir string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 28 | // 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'. |
| 31 | var 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" |
| 37 | var ConfigName = "unknown" |
| 38 | |
| 39 | // Returns the configuration matching the UseCCache and ConfigName. |
| 40 | func getRealConfig() (*config, error) { |
| 41 | useCCache, err := strconv.ParseBool(UseCCache) |
| 42 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 43 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 44 | } |
| 45 | config, err := getConfig(useCCache, ConfigName) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return config, nil |
| 50 | } |
| 51 | |
| 52 | func 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 Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 59 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 63 | // Full hardening. |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 64 | func 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{ |
| 71 | "-fPIE", |
| 72 | "-D_FORTIFY_SOURCE=2", |
| 73 | "-fstack-protector-strong", |
| 74 | "-pie", |
| 75 | "-fno-omit-frame-pointer", |
| 76 | }, |
| 77 | gccFlags: []string{ |
| 78 | "-Wno-unused-local-typedefs", |
| 79 | "-Wno-maybe-uninitialized", |
| 80 | "-fno-reorder-blocks-and-partition", |
| 81 | }, |
| 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{ |
| 87 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 88 | "-Qunused-arguments", |
| 89 | "-grecord-gcc-switches", |
| 90 | "-Wno-section", |
| 91 | "-Wno-unknown-warning-option", |
| 92 | "-fno-addrsig", |
| 93 | "-Wno-tautological-constant-compare", |
| 94 | }, |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame^] | 95 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 96 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 100 | func getCrosNonHardenedConfig(useCCache bool) *config { |
| 101 | return &config{ |
| 102 | useCCache: useCCache, |
| 103 | rootRelPath: "../../../../..", |
| 104 | oldWrapperPath: "./sysroot_wrapper.old", |
| 105 | commonFlags: []string{}, |
| 106 | gccFlags: []string{ |
| 107 | "-Wno-unused-local-typedefs", |
| 108 | "-Wno-maybe-uninitialized", |
| 109 | "-Wtrampolines", |
| 110 | "-Wno-deprecated-declarations", |
| 111 | }, |
| 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{ |
| 116 | "-Wno-unknown-warning-option", |
| 117 | "-Qunused-arguments", |
| 118 | "-Wno-section", |
| 119 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 120 | "-Wno-tautological-constant-compare", |
| 121 | }, |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame^] | 122 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 123 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 124 | } |