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 | |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 96 | // TODO: Enable test in config_test.go, once we have new llvm-next flags. |
| 97 | var llvmNextFlags = []string{} |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 98 | |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 99 | var llvmNextPostFlags = []string{} |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 100 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 101 | // Full hardening. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 102 | // Temporarily disable function splitting because of chromium:434751. |
| 103 | var crosHardenedConfig = &config{ |
| 104 | rootRelPath: "../../../../..", |
| 105 | commonFlags: []string{ |
| 106 | "-fstack-protector-strong", |
| 107 | "-fPIE", |
| 108 | "-pie", |
| 109 | "-D_FORTIFY_SOURCE=2", |
| 110 | "-fno-omit-frame-pointer", |
| 111 | }, |
| 112 | gccFlags: []string{ |
| 113 | "-fno-reorder-blocks-and-partition", |
| 114 | "-Wno-unused-local-typedefs", |
| 115 | "-Wno-maybe-uninitialized", |
| 116 | }, |
| 117 | // Temporarily disable tautological-*-compare chromium:778316. |
| 118 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 119 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 120 | // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742. |
| 121 | clangFlags: []string{ |
| 122 | "-Qunused-arguments", |
| 123 | "-grecord-gcc-switches", |
| 124 | "-fno-addrsig", |
| 125 | "-Wno-tautological-constant-compare", |
| 126 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 127 | "-Wno-unknown-warning-option", |
| 128 | "-Wno-section", |
| 129 | "-static-libgcc", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 130 | "-fuse-ld=lld", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 131 | "-Wno-reorder-init-list", |
| 132 | "-Wno-final-dtor-non-final-class", |
| 133 | "-Wno-return-stack-address", |
| 134 | "-Werror=poison-system-directories", |
| 135 | }, |
| 136 | clangPostFlags: []string{ |
| 137 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 138 | }, |
| 139 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 143 | var crosNonHardenedConfig = &config{ |
| 144 | rootRelPath: "../../../../..", |
| 145 | commonFlags: []string{}, |
| 146 | gccFlags: []string{ |
| 147 | "-Wno-maybe-uninitialized", |
| 148 | "-Wno-unused-local-typedefs", |
| 149 | "-Wno-deprecated-declarations", |
| 150 | "-Wtrampolines", |
| 151 | }, |
| 152 | // Temporarily disable tautological-*-compare chromium:778316. |
| 153 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 154 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 155 | clangFlags: []string{ |
| 156 | "-Qunused-arguments", |
| 157 | "-Wno-tautological-constant-compare", |
| 158 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 159 | "-Wno-unknown-warning-option", |
| 160 | "-Wno-section", |
| 161 | "-static-libgcc", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 162 | "-Wno-reorder-init-list", |
| 163 | "-Wno-final-dtor-non-final-class", |
| 164 | "-Wno-return-stack-address", |
| 165 | "-Werror=poison-system-directories", |
| 166 | }, |
| 167 | clangPostFlags: []string{ |
| 168 | "-Wno-implicit-int-float-conversion", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 169 | }, |
| 170 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 171 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 172 | |
| 173 | // Flags to be added to host toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 174 | var crosHostConfig = &config{ |
| 175 | isHostWrapper: true, |
| 176 | rootRelPath: "../..", |
| 177 | commonFlags: []string{}, |
| 178 | gccFlags: []string{ |
| 179 | "-Wno-maybe-uninitialized", |
| 180 | "-Wno-unused-local-typedefs", |
| 181 | "-Wno-deprecated-declarations", |
| 182 | }, |
| 183 | // Temporarily disable tautological-*-compare chromium:778316. |
| 184 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 185 | clangFlags: []string{ |
| 186 | "-Qunused-arguments", |
| 187 | "-grecord-gcc-switches", |
| 188 | "-fno-addrsig", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 189 | "-fuse-ld=lld", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 190 | "-Wno-unused-local-typedefs", |
| 191 | "-Wno-deprecated-declarations", |
| 192 | "-Wno-tautological-constant-compare", |
| 193 | "-Wno-tautological-unsigned-enum-zero-compare", |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 194 | "-Wno-reorder-init-list", |
| 195 | "-Wno-final-dtor-non-final-class", |
| 196 | "-Wno-return-stack-address", |
| 197 | "-Werror=poison-system-directories", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 198 | "-Wno-unknown-warning-option", |
| 199 | }, |
Caroline Tice | b922860 | 2019-10-29 13:21:19 -0700 | [diff] [blame] | 200 | clangPostFlags: []string{ |
| 201 | "-Wno-implicit-int-float-conversion", |
| 202 | }, |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 203 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 204 | } |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 205 | |
| 206 | var androidConfig = &config{ |
| 207 | isHostWrapper: false, |
| 208 | isAndroidWrapper: true, |
| 209 | rootRelPath: "./", |
| 210 | commonFlags: []string{}, |
| 211 | gccFlags: []string{}, |
| 212 | clangFlags: []string{}, |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 213 | clangPostFlags: []string{}, |
Pirama Arumuga Nainar | ee7e22c | 2020-02-20 15:08:30 -0800 | [diff] [blame^] | 214 | newWarningsDir: "", |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 215 | } |