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 |
Pirama Arumuga Nainar | ee7e22c | 2020-02-20 15:08:30 -0800 | [diff] [blame] | 17 | // Whether llvmNext wrapper. |
| 18 | useLlvmNext bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 19 | // Flags to add to gcc and clang. |
| 20 | commonFlags []string |
| 21 | // Flags to add to gcc only. |
| 22 | gccFlags []string |
| 23 | // Flags to add to clang only. |
| 24 | clangFlags []string |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 25 | // Flags to add to clang only, AFTER user flags (cannot be overridden |
| 26 | // by the user). |
| 27 | clangPostFlags []string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 28 | // Toolchain root path relative to the wrapper binary. |
| 29 | rootRelPath string |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 30 | // Directory to store errors that were prevented with -Wno-error. |
| 31 | newWarningsDir string |
George Burgess IV | 163efaa | 2020-06-08 10:58:36 -0700 | [diff] [blame] | 32 | // Directory to store nits in when using `WITH_TIDY=tricium`. |
| 33 | triciumNitsDir string |
George Burgess IV | 0a377f4 | 2020-08-05 15:03:36 -0700 | [diff] [blame] | 34 | // Directory to store crash artifacts in. |
| 35 | crashArtifactsDir string |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 36 | // Version. Only used for printing via -print-cmd. |
| 37 | version string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 40 | // Version can be set via a linker flag. |
| 41 | // Values fills config.version. |
| 42 | var Version = "" |
| 43 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 44 | // UseCCache can be set via a linker flag. |
| 45 | // Value will be passed to strconv.ParseBool. |
| 46 | // E.g. go build -ldflags '-X config.UseCCache=true'. |
| 47 | var UseCCache = "unknown" |
| 48 | |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 49 | // UseLlvmNext can be set via a linker flag. |
| 50 | // Value will be passed to strconv.ParseBool. |
| 51 | // E.g. go build -ldflags '-X config.UseLlvmNext=true'. |
| 52 | var UseLlvmNext = "unknown" |
| 53 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 54 | // ConfigName can be set via a linker flag. |
| 55 | // Value has to be one of: |
| 56 | // - "cros.hardened" |
| 57 | // - "cros.nonhardened" |
| 58 | var ConfigName = "unknown" |
| 59 | |
| 60 | // Returns the configuration matching the UseCCache and ConfigName. |
| 61 | func getRealConfig() (*config, error) { |
| 62 | useCCache, err := strconv.ParseBool(UseCCache) |
| 63 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 64 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 65 | } |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 66 | useLlvmNext, err := strconv.ParseBool(UseLlvmNext) |
| 67 | if err != nil { |
| 68 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext") |
| 69 | } |
Tobias Bosch | 8dd67e1 | 2019-10-28 14:26:51 -0700 | [diff] [blame] | 70 | config, err := getConfig(ConfigName, useCCache, useLlvmNext, 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 | |
George Burgess IV | 2efe72e | 2020-06-18 20:37:28 -0700 | [diff] [blame] | 77 | func isAndroidConfig() bool { |
| 78 | return ConfigName == "android" |
| 79 | } |
| 80 | |
Tobias Bosch | 8dd67e1 | 2019-10-28 14:26:51 -0700 | [diff] [blame] | 81 | func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) { |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 82 | cfg := config{} |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 83 | switch configName { |
| 84 | case "cros.hardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 85 | cfg = *crosHardenedConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 86 | case "cros.nonhardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 87 | cfg = *crosNonHardenedConfig |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 88 | case "cros.host": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 89 | cfg = *crosHostConfig |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 90 | case "android": |
| 91 | cfg = *androidConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 92 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 93 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 94 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 95 | cfg.useCCache = useCCache |
Pirama Arumuga Nainar | ee7e22c | 2020-02-20 15:08:30 -0800 | [diff] [blame] | 96 | cfg.useLlvmNext = useLlvmNext |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 97 | if useLlvmNext { |
| 98 | cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...) |
Caroline Tice | 8a9125c | 2020-09-22 08:34:51 -0700 | [diff] [blame^] | 99 | cfg.clangPostFlags = append(cfg.clangPostFlags, llvmNextPostFlags...) |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 100 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 101 | cfg.version = version |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 102 | return &cfg, nil |
| 103 | } |
| 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. |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 125 | // Pass "-fcommon" till the packages are fixed to work with new clang default |
| 126 | // "-fno-common", crbug.com/1060413. |
Bob Haarman | 94fd622 | 2020-07-09 17:14:53 -0700 | [diff] [blame] | 127 | // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache; |
| 128 | // removed that flag for now. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 129 | clangFlags: []string{ |
| 130 | "-Qunused-arguments", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 131 | "-fno-addrsig", |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 132 | "-fcommon", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 133 | "-Wno-tautological-constant-compare", |
| 134 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 135 | "-Wno-unknown-warning-option", |
| 136 | "-Wno-section", |
| 137 | "-static-libgcc", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 138 | "-fuse-ld=lld", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 139 | "-Wno-final-dtor-non-final-class", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 140 | "-Werror=poison-system-directories", |
Jian Cai | d1a9a25 | 2020-07-29 18:03:33 -0700 | [diff] [blame] | 141 | "-fexperimental-new-pass-manager", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 142 | }, |
| 143 | clangPostFlags: []string{ |
| 144 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 145 | }, |
Caroline Tice | 8a9125c | 2020-09-22 08:34:51 -0700 | [diff] [blame^] | 146 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 147 | triciumNitsDir: "/tmp/linting_output/clang-tidy", |
George Burgess IV | 0a377f4 | 2020-08-05 15:03:36 -0700 | [diff] [blame] | 148 | crashArtifactsDir: "/tmp/clang_crash_diagnostics", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 152 | var crosNonHardenedConfig = &config{ |
| 153 | rootRelPath: "../../../../..", |
| 154 | commonFlags: []string{}, |
| 155 | gccFlags: []string{ |
| 156 | "-Wno-maybe-uninitialized", |
| 157 | "-Wno-unused-local-typedefs", |
| 158 | "-Wno-deprecated-declarations", |
| 159 | "-Wtrampolines", |
| 160 | }, |
| 161 | // Temporarily disable tautological-*-compare chromium:778316. |
| 162 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 163 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 164 | clangFlags: []string{ |
| 165 | "-Qunused-arguments", |
| 166 | "-Wno-tautological-constant-compare", |
| 167 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 168 | "-Wno-unknown-warning-option", |
| 169 | "-Wno-section", |
| 170 | "-static-libgcc", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 171 | "-Wno-final-dtor-non-final-class", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 172 | "-Werror=poison-system-directories", |
Jian Cai | d1a9a25 | 2020-07-29 18:03:33 -0700 | [diff] [blame] | 173 | "-fexperimental-new-pass-manager", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 174 | }, |
| 175 | clangPostFlags: []string{ |
| 176 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 177 | }, |
George Burgess IV | 0a377f4 | 2020-08-05 15:03:36 -0700 | [diff] [blame] | 178 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 179 | triciumNitsDir: "/tmp/linting_output/clang-tidy", |
| 180 | crashArtifactsDir: "/tmp/clang_crash_diagnostics", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 181 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 182 | |
| 183 | // Flags to be added to host toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 184 | var crosHostConfig = &config{ |
| 185 | isHostWrapper: true, |
| 186 | rootRelPath: "../..", |
| 187 | commonFlags: []string{}, |
| 188 | gccFlags: []string{ |
| 189 | "-Wno-maybe-uninitialized", |
| 190 | "-Wno-unused-local-typedefs", |
| 191 | "-Wno-deprecated-declarations", |
| 192 | }, |
| 193 | // Temporarily disable tautological-*-compare chromium:778316. |
| 194 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 195 | // Pass "-fcommon" till the packages are fixed to work with new clang default |
| 196 | // "-fno-common", crbug.com/1060413. |
Bob Haarman | 94fd622 | 2020-07-09 17:14:53 -0700 | [diff] [blame] | 197 | // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache; |
| 198 | // removed that flag for now. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 199 | clangFlags: []string{ |
| 200 | "-Qunused-arguments", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 201 | "-fno-addrsig", |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 202 | "-fcommon", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 203 | "-fuse-ld=lld", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 204 | "-Wno-unused-local-typedefs", |
| 205 | "-Wno-deprecated-declarations", |
| 206 | "-Wno-tautological-constant-compare", |
| 207 | "-Wno-tautological-unsigned-enum-zero-compare", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 208 | "-Wno-final-dtor-non-final-class", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 209 | "-Werror=poison-system-directories", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 210 | "-Wno-unknown-warning-option", |
Jian Cai | d1a9a25 | 2020-07-29 18:03:33 -0700 | [diff] [blame] | 211 | "-fexperimental-new-pass-manager", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 212 | }, |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 213 | clangPostFlags: []string{ |
| 214 | "-Wno-implicit-int-float-conversion", |
| 215 | }, |
George Burgess IV | 0a377f4 | 2020-08-05 15:03:36 -0700 | [diff] [blame] | 216 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 217 | triciumNitsDir: "/tmp/linting_output/clang-tidy", |
| 218 | crashArtifactsDir: "/tmp/clang_crash_diagnostics", |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 219 | } |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 220 | |
| 221 | var androidConfig = &config{ |
George Burgess IV | 0a377f4 | 2020-08-05 15:03:36 -0700 | [diff] [blame] | 222 | isHostWrapper: false, |
| 223 | isAndroidWrapper: true, |
| 224 | rootRelPath: "./", |
| 225 | commonFlags: []string{}, |
| 226 | gccFlags: []string{}, |
| 227 | clangFlags: []string{}, |
| 228 | clangPostFlags: []string{}, |
| 229 | newWarningsDir: "", |
| 230 | triciumNitsDir: "", |
| 231 | crashArtifactsDir: "", |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 232 | } |