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. |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 13 | isHostWrapper bool |
| 14 | isAndroidWrapper bool |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 15 | // Whether to use ccache. |
| 16 | useCCache bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 17 | // Flags to add to gcc and clang. |
| 18 | commonFlags []string |
| 19 | // Flags to add to gcc only. |
| 20 | gccFlags []string |
| 21 | // Flags to add to clang only. |
| 22 | clangFlags []string |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 23 | // Flags to add to clang only, AFTER user flags (cannot be overridden |
| 24 | // by the user). |
| 25 | clangPostFlags []string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 26 | // Toolchain root path relative to the wrapper binary. |
| 27 | rootRelPath string |
| 28 | // Path of the old wrapper using the toolchain root. |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 29 | oldWrapperPath string |
| 30 | // Whether to mock out the calls that the old wrapper does. |
| 31 | mockOldWrapperCmds bool |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 32 | // Directory to store errors that were prevented with -Wno-error. |
| 33 | newWarningsDir string |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 34 | // Version. Only used for printing via -print-cmd. |
| 35 | version string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 38 | // Version can be set via a linker flag. |
| 39 | // Values fills config.version. |
| 40 | var Version = "" |
| 41 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 42 | // UseCCache can be set via a linker flag. |
| 43 | // Value will be passed to strconv.ParseBool. |
| 44 | // E.g. go build -ldflags '-X config.UseCCache=true'. |
| 45 | var UseCCache = "unknown" |
| 46 | |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 47 | // UseLlvmNext can be set via a linker flag. |
| 48 | // Value will be passed to strconv.ParseBool. |
| 49 | // E.g. go build -ldflags '-X config.UseLlvmNext=true'. |
| 50 | var UseLlvmNext = "unknown" |
| 51 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 52 | // ConfigName can be set via a linker flag. |
| 53 | // Value has to be one of: |
| 54 | // - "cros.hardened" |
| 55 | // - "cros.nonhardened" |
| 56 | var ConfigName = "unknown" |
| 57 | |
| 58 | // Returns the configuration matching the UseCCache and ConfigName. |
| 59 | func getRealConfig() (*config, error) { |
| 60 | useCCache, err := strconv.ParseBool(UseCCache) |
| 61 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 62 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 63 | } |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 64 | useLlvmNext, err := strconv.ParseBool(UseLlvmNext) |
| 65 | if err != nil { |
| 66 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext") |
| 67 | } |
| 68 | // FIXME: Remove comparison to old wrapper once the new wrapper has landed. |
| 69 | oldWrapperPath := "" |
| 70 | config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, Version) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | return config, nil |
| 75 | } |
| 76 | |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 77 | func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) { |
| 78 | cfg := config{} |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 79 | switch configName { |
| 80 | case "cros.hardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 81 | cfg = *crosHardenedConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 82 | case "cros.nonhardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 83 | cfg = *crosNonHardenedConfig |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 84 | case "cros.host": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 85 | cfg = *crosHostConfig |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 86 | case "android": |
| 87 | cfg = *androidConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 88 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 89 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 90 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 91 | cfg.useCCache = useCCache |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 92 | if useLlvmNext { |
| 93 | cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...) |
| 94 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 95 | cfg.oldWrapperPath = oldWrapperPath |
| 96 | cfg.version = version |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 97 | return &cfg, nil |
| 98 | } |
| 99 | |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame^] | 100 | // TODO: Enable test in config_test.go, once we have new llvm-next flags. |
| 101 | var llvmNextFlags = []string{} |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 102 | |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame^] | 103 | var llvmNextPostFlags = []string{} |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 104 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 105 | // Full hardening. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 106 | // Temporarily disable function splitting because of chromium:434751. |
| 107 | var crosHardenedConfig = &config{ |
| 108 | rootRelPath: "../../../../..", |
| 109 | commonFlags: []string{ |
| 110 | "-fstack-protector-strong", |
| 111 | "-fPIE", |
| 112 | "-pie", |
| 113 | "-D_FORTIFY_SOURCE=2", |
| 114 | "-fno-omit-frame-pointer", |
| 115 | }, |
| 116 | gccFlags: []string{ |
| 117 | "-fno-reorder-blocks-and-partition", |
| 118 | "-Wno-unused-local-typedefs", |
| 119 | "-Wno-maybe-uninitialized", |
| 120 | }, |
| 121 | // Temporarily disable tautological-*-compare chromium:778316. |
| 122 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 123 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 124 | // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742. |
| 125 | clangFlags: []string{ |
| 126 | "-Qunused-arguments", |
| 127 | "-grecord-gcc-switches", |
| 128 | "-fno-addrsig", |
| 129 | "-Wno-tautological-constant-compare", |
| 130 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 131 | "-Wno-unknown-warning-option", |
| 132 | "-Wno-section", |
| 133 | "-static-libgcc", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 134 | "-fuse-ld=lld", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame^] | 135 | "-Wno-reorder-init-list", |
| 136 | "-Wno-final-dtor-non-final-class", |
| 137 | "-Wno-return-stack-address", |
| 138 | "-Werror=poison-system-directories", |
| 139 | }, |
| 140 | clangPostFlags: []string{ |
| 141 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 142 | }, |
| 143 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 147 | var crosNonHardenedConfig = &config{ |
| 148 | rootRelPath: "../../../../..", |
| 149 | commonFlags: []string{}, |
| 150 | gccFlags: []string{ |
| 151 | "-Wno-maybe-uninitialized", |
| 152 | "-Wno-unused-local-typedefs", |
| 153 | "-Wno-deprecated-declarations", |
| 154 | "-Wtrampolines", |
| 155 | }, |
| 156 | // Temporarily disable tautological-*-compare chromium:778316. |
| 157 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 158 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 159 | clangFlags: []string{ |
| 160 | "-Qunused-arguments", |
| 161 | "-Wno-tautological-constant-compare", |
| 162 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 163 | "-Wno-unknown-warning-option", |
| 164 | "-Wno-section", |
| 165 | "-static-libgcc", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame^] | 166 | "-Wno-reorder-init-list", |
| 167 | "-Wno-final-dtor-non-final-class", |
| 168 | "-Wno-return-stack-address", |
| 169 | "-Werror=poison-system-directories", |
| 170 | }, |
| 171 | clangPostFlags: []string{ |
| 172 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 173 | }, |
| 174 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 175 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 176 | |
| 177 | // Flags to be added to host toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 178 | var crosHostConfig = &config{ |
| 179 | isHostWrapper: true, |
| 180 | rootRelPath: "../..", |
| 181 | commonFlags: []string{}, |
| 182 | gccFlags: []string{ |
| 183 | "-Wno-maybe-uninitialized", |
| 184 | "-Wno-unused-local-typedefs", |
| 185 | "-Wno-deprecated-declarations", |
| 186 | }, |
| 187 | // Temporarily disable tautological-*-compare chromium:778316. |
| 188 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 189 | clangFlags: []string{ |
| 190 | "-Qunused-arguments", |
| 191 | "-grecord-gcc-switches", |
| 192 | "-fno-addrsig", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 193 | "-fuse-ld=lld", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 194 | "-Wno-unused-local-typedefs", |
| 195 | "-Wno-deprecated-declarations", |
| 196 | "-Wno-tautological-constant-compare", |
| 197 | "-Wno-tautological-unsigned-enum-zero-compare", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame^] | 198 | "-Wno-reorder-init-list", |
| 199 | "-Wno-final-dtor-non-final-class", |
| 200 | "-Wno-return-stack-address", |
| 201 | "-Werror=poison-system-directories", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 202 | "-Wno-unknown-warning-option", |
| 203 | }, |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame^] | 204 | clangPostFlags: []string{ |
| 205 | "-Wno-implicit-int-float-conversion", |
| 206 | }, |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 207 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 208 | } |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 209 | |
| 210 | var androidConfig = &config{ |
| 211 | isHostWrapper: false, |
| 212 | isAndroidWrapper: true, |
| 213 | rootRelPath: "./", |
| 214 | commonFlags: []string{}, |
| 215 | gccFlags: []string{}, |
| 216 | clangFlags: []string{}, |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 217 | clangPostFlags: []string{}, |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 218 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 219 | } |