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