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