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 |
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 | } |
Tobias Bosch | 8dd67e1 | 2019-10-28 14:26:51 -0700 | [diff] [blame] | 68 | config, err := getConfig(ConfigName, useCCache, useLlvmNext, Version) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | return config, nil |
| 73 | } |
| 74 | |
George Burgess IV | 2efe72e | 2020-06-18 20:37:28 -0700 | [diff] [blame] | 75 | func isAndroidConfig() bool { |
| 76 | return ConfigName == "android" |
| 77 | } |
| 78 | |
Tobias Bosch | 8dd67e1 | 2019-10-28 14:26:51 -0700 | [diff] [blame] | 79 | func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) { |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 80 | cfg := config{} |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 81 | switch configName { |
| 82 | case "cros.hardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 83 | cfg = *crosHardenedConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 84 | case "cros.nonhardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 85 | cfg = *crosNonHardenedConfig |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 86 | case "cros.host": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 87 | cfg = *crosHostConfig |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 88 | case "android": |
| 89 | cfg = *androidConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 90 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 91 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 92 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 93 | cfg.useCCache = useCCache |
Pirama Arumuga Nainar | ee7e22c | 2020-02-20 15:08:30 -0800 | [diff] [blame] | 94 | cfg.useLlvmNext = useLlvmNext |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 95 | if useLlvmNext { |
| 96 | cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...) |
| 97 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 98 | cfg.version = version |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 99 | return &cfg, nil |
| 100 | } |
| 101 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 102 | // Full hardening. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 103 | // Temporarily disable function splitting because of chromium:434751. |
| 104 | var crosHardenedConfig = &config{ |
| 105 | rootRelPath: "../../../../..", |
| 106 | commonFlags: []string{ |
| 107 | "-fstack-protector-strong", |
| 108 | "-fPIE", |
| 109 | "-pie", |
| 110 | "-D_FORTIFY_SOURCE=2", |
| 111 | "-fno-omit-frame-pointer", |
| 112 | }, |
| 113 | gccFlags: []string{ |
| 114 | "-fno-reorder-blocks-and-partition", |
| 115 | "-Wno-unused-local-typedefs", |
| 116 | "-Wno-maybe-uninitialized", |
| 117 | }, |
| 118 | // Temporarily disable tautological-*-compare chromium:778316. |
| 119 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 120 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 121 | // 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] | 122 | // Pass "-fcommon" till the packages are fixed to work with new clang default |
| 123 | // "-fno-common", crbug.com/1060413. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 124 | clangFlags: []string{ |
| 125 | "-Qunused-arguments", |
| 126 | "-grecord-gcc-switches", |
| 127 | "-fno-addrsig", |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 128 | "-fcommon", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 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-final-dtor-non-final-class", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 136 | "-Werror=poison-system-directories", |
| 137 | }, |
| 138 | clangPostFlags: []string{ |
| 139 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 140 | }, |
| 141 | newWarningsDir: "/tmp/fatal_clang_warnings", |
George Burgess IV | 163efaa | 2020-06-08 10:58:36 -0700 | [diff] [blame^] | 142 | triciumNitsDir: "/tmp/linting_output/clang-tidy", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 146 | var crosNonHardenedConfig = &config{ |
| 147 | rootRelPath: "../../../../..", |
| 148 | commonFlags: []string{}, |
| 149 | gccFlags: []string{ |
| 150 | "-Wno-maybe-uninitialized", |
| 151 | "-Wno-unused-local-typedefs", |
| 152 | "-Wno-deprecated-declarations", |
| 153 | "-Wtrampolines", |
| 154 | }, |
| 155 | // Temporarily disable tautological-*-compare chromium:778316. |
| 156 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 157 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 158 | clangFlags: []string{ |
| 159 | "-Qunused-arguments", |
| 160 | "-Wno-tautological-constant-compare", |
| 161 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 162 | "-Wno-unknown-warning-option", |
| 163 | "-Wno-section", |
| 164 | "-static-libgcc", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 165 | "-Wno-final-dtor-non-final-class", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 166 | "-Werror=poison-system-directories", |
| 167 | }, |
| 168 | clangPostFlags: []string{ |
| 169 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 170 | }, |
| 171 | newWarningsDir: "/tmp/fatal_clang_warnings", |
George Burgess IV | 163efaa | 2020-06-08 10:58:36 -0700 | [diff] [blame^] | 172 | triciumNitsDir: "/tmp/linting_output/clang-tidy", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 173 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 174 | |
| 175 | // Flags to be added to host toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 176 | var crosHostConfig = &config{ |
| 177 | isHostWrapper: true, |
| 178 | rootRelPath: "../..", |
| 179 | commonFlags: []string{}, |
| 180 | gccFlags: []string{ |
| 181 | "-Wno-maybe-uninitialized", |
| 182 | "-Wno-unused-local-typedefs", |
| 183 | "-Wno-deprecated-declarations", |
| 184 | }, |
| 185 | // Temporarily disable tautological-*-compare chromium:778316. |
| 186 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 187 | // Pass "-fcommon" till the packages are fixed to work with new clang default |
| 188 | // "-fno-common", crbug.com/1060413. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 189 | clangFlags: []string{ |
| 190 | "-Qunused-arguments", |
| 191 | "-grecord-gcc-switches", |
| 192 | "-fno-addrsig", |
Manoj Gupta | 99b3ff9 | 2020-03-13 10:33:13 -0700 | [diff] [blame] | 193 | "-fcommon", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 194 | "-fuse-ld=lld", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 195 | "-Wno-unused-local-typedefs", |
| 196 | "-Wno-deprecated-declarations", |
| 197 | "-Wno-tautological-constant-compare", |
| 198 | "-Wno-tautological-unsigned-enum-zero-compare", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 199 | "-Wno-final-dtor-non-final-class", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 200 | "-Werror=poison-system-directories", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 201 | "-Wno-unknown-warning-option", |
| 202 | }, |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 203 | clangPostFlags: []string{ |
| 204 | "-Wno-implicit-int-float-conversion", |
| 205 | }, |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 206 | newWarningsDir: "/tmp/fatal_clang_warnings", |
George Burgess IV | 163efaa | 2020-06-08 10:58:36 -0700 | [diff] [blame^] | 207 | triciumNitsDir: "/tmp/linting_output/clang-tidy", |
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{}, |
Pirama Arumuga Nainar | ee7e22c | 2020-02-20 15:08:30 -0800 | [diff] [blame] | 218 | newWarningsDir: "", |
George Burgess IV | 163efaa | 2020-06-08 10:58:36 -0700 | [diff] [blame^] | 219 | triciumNitsDir: "", |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 220 | } |