Tobias Bosch | cfa8c24 | 2019-07-19 03:29:40 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 5 | package main |
| 6 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 7 | import ( |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 8 | "strconv" |
| 9 | ) |
| 10 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 11 | type config struct { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 12 | // TODO: Refactor this flag into more generic configuration properties. |
| 13 | isHostWrapper bool |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 14 | // Whether to use ccache. |
| 15 | useCCache bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 16 | // Flags to add to gcc and clang. |
| 17 | commonFlags []string |
| 18 | // Flags to add to gcc only. |
| 19 | gccFlags []string |
| 20 | // Flags to add to clang only. |
| 21 | clangFlags []string |
| 22 | // Toolchain root path relative to the wrapper binary. |
| 23 | rootRelPath string |
| 24 | // Path of the old wrapper using the toolchain root. |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 25 | oldWrapperPath string |
| 26 | // Whether to mock out the calls that the old wrapper does. |
| 27 | mockOldWrapperCmds bool |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 28 | // Directory to store errors that were prevented with -Wno-error. |
| 29 | newWarningsDir string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 32 | // OldWrapperPath can be set via a linker flag. |
| 33 | // Value fills config.oldWrapperPath. |
| 34 | var OldWrapperPath = "" |
| 35 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 36 | // UseCCache can be set via a linker flag. |
| 37 | // Value will be passed to strconv.ParseBool. |
| 38 | // E.g. go build -ldflags '-X config.UseCCache=true'. |
| 39 | var UseCCache = "unknown" |
| 40 | |
| 41 | // ConfigName can be set via a linker flag. |
| 42 | // Value has to be one of: |
| 43 | // - "cros.hardened" |
| 44 | // - "cros.nonhardened" |
| 45 | var ConfigName = "unknown" |
| 46 | |
| 47 | // Returns the configuration matching the UseCCache and ConfigName. |
| 48 | func getRealConfig() (*config, error) { |
| 49 | useCCache, err := strconv.ParseBool(UseCCache) |
| 50 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 51 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 52 | } |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 53 | config, err := getConfig(useCCache, ConfigName, OldWrapperPath) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return config, nil |
| 58 | } |
| 59 | |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 60 | func getConfig(useCCache bool, configName string, oldWrapperPath string) (*config, error) { |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 61 | switch configName { |
| 62 | case "cros.hardened": |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 63 | return getCrosHardenedConfig(useCCache, oldWrapperPath), nil |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 64 | case "cros.nonhardened": |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 65 | return getCrosNonHardenedConfig(useCCache, oldWrapperPath), nil |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 66 | case "cros.host": |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 67 | return getCrosHostConfig(oldWrapperPath), nil |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 68 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 69 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 73 | // Full hardening. |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 74 | func getCrosHardenedConfig(useCCache bool, oldWrapperPath string) *config { |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 75 | // Temporarily disable function splitting because of chromium:434751. |
| 76 | return &config{ |
| 77 | useCCache: useCCache, |
| 78 | rootRelPath: "../../../../..", |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 79 | oldWrapperPath: oldWrapperPath, |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 80 | commonFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 81 | "-fstack-protector-strong", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 82 | "-fPIE", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 83 | "-pie", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 84 | "-D_FORTIFY_SOURCE=2", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 85 | "-fno-omit-frame-pointer", |
| 86 | }, |
| 87 | gccFlags: []string{ |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 88 | "-fno-reorder-blocks-and-partition", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 89 | "-Wno-unused-local-typedefs", |
| 90 | "-Wno-maybe-uninitialized", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 91 | }, |
| 92 | // Temporarily disable tautological-*-compare chromium:778316. |
| 93 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 94 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 95 | // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742. |
| 96 | clangFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 97 | "-Qunused-arguments", |
| 98 | "-grecord-gcc-switches", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 99 | "-fno-addrsig", |
| 100 | "-Wno-tautological-constant-compare", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 101 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 102 | "-Wno-unknown-warning-option", |
| 103 | "-Wno-section", |
Tobias Bosch | e23905c | 2019-07-19 02:56:31 -0700 | [diff] [blame] | 104 | "-static-libgcc", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 105 | }, |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 106 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 107 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 111 | func getCrosNonHardenedConfig(useCCache bool, oldWrapperPath string) *config { |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 112 | return &config{ |
| 113 | useCCache: useCCache, |
| 114 | rootRelPath: "../../../../..", |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 115 | oldWrapperPath: oldWrapperPath, |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 116 | commonFlags: []string{}, |
| 117 | gccFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 118 | "-Wno-maybe-uninitialized", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 119 | "-Wno-unused-local-typedefs", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 120 | "-Wno-deprecated-declarations", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 121 | "-Wtrampolines", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 122 | }, |
| 123 | // Temporarily disable tautological-*-compare chromium:778316. |
| 124 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 125 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 126 | clangFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 127 | "-Qunused-arguments", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 128 | "-Wno-tautological-constant-compare", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 129 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 130 | "-Wno-unknown-warning-option", |
| 131 | "-Wno-section", |
Tobias Bosch | e23905c | 2019-07-19 02:56:31 -0700 | [diff] [blame] | 132 | "-static-libgcc", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 133 | }, |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 134 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 135 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 136 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 137 | |
| 138 | // Flags to be added to host toolchain. |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 139 | func getCrosHostConfig(oldWrapperPath string) *config { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 140 | return &config{ |
| 141 | isHostWrapper: true, |
| 142 | useCCache: false, |
| 143 | rootRelPath: "../..", |
Tobias Bosch | 47f580f | 2019-08-14 15:15:21 -0700 | [diff] [blame] | 144 | oldWrapperPath: oldWrapperPath, |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 145 | commonFlags: []string{}, |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 146 | gccFlags: []string{ |
| 147 | "-Wno-maybe-uninitialized", |
| 148 | "-Wno-unused-local-typedefs", |
| 149 | "-Wno-deprecated-declarations", |
| 150 | }, |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 151 | // Temporarily disable tautological-*-compare chromium:778316. |
| 152 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 153 | clangFlags: []string{ |
| 154 | "-Qunused-arguments", |
| 155 | "-grecord-gcc-switches", |
| 156 | "-fno-addrsig", |
| 157 | "-Wno-unused-local-typedefs", |
| 158 | "-Wno-deprecated-declarations", |
| 159 | "-Wno-tautological-constant-compare", |
| 160 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 161 | "-Wno-unknown-warning-option", |
| 162 | }, |
| 163 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 164 | } |
| 165 | } |