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 |
| 23 | // Toolchain root path relative to the wrapper binary. |
| 24 | rootRelPath string |
| 25 | // Path of the old wrapper using the toolchain root. |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 26 | oldWrapperPath string |
| 27 | // Whether to mock out the calls that the old wrapper does. |
| 28 | mockOldWrapperCmds bool |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 29 | // Directory to store errors that were prevented with -Wno-error. |
| 30 | newWarningsDir string |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 31 | // Version. Only used for printing via -print-cmd. |
| 32 | version string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 35 | // Version can be set via a linker flag. |
| 36 | // Values fills config.version. |
| 37 | var Version = "" |
| 38 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 39 | // UseCCache can be set via a linker flag. |
| 40 | // Value will be passed to strconv.ParseBool. |
| 41 | // E.g. go build -ldflags '-X config.UseCCache=true'. |
| 42 | var UseCCache = "unknown" |
| 43 | |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 44 | // UseLlvmNext can be set via a linker flag. |
| 45 | // Value will be passed to strconv.ParseBool. |
| 46 | // E.g. go build -ldflags '-X config.UseLlvmNext=true'. |
| 47 | var UseLlvmNext = "unknown" |
| 48 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 49 | // ConfigName can be set via a linker flag. |
| 50 | // Value has to be one of: |
| 51 | // - "cros.hardened" |
| 52 | // - "cros.nonhardened" |
| 53 | var ConfigName = "unknown" |
| 54 | |
| 55 | // Returns the configuration matching the UseCCache and ConfigName. |
| 56 | func getRealConfig() (*config, error) { |
| 57 | useCCache, err := strconv.ParseBool(UseCCache) |
| 58 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 59 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 60 | } |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 61 | useLlvmNext, err := strconv.ParseBool(UseLlvmNext) |
| 62 | if err != nil { |
| 63 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext") |
| 64 | } |
| 65 | // FIXME: Remove comparison to old wrapper once the new wrapper has landed. |
| 66 | oldWrapperPath := "" |
| 67 | config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, Version) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | return config, nil |
| 72 | } |
| 73 | |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 74 | func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) { |
| 75 | cfg := config{} |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 76 | switch configName { |
| 77 | case "cros.hardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 78 | cfg = *crosHardenedConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 79 | case "cros.nonhardened": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 80 | cfg = *crosNonHardenedConfig |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 81 | case "cros.host": |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 82 | cfg = *crosHostConfig |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 83 | case "android": |
| 84 | cfg = *androidConfig |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 85 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 86 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 87 | } |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 88 | cfg.useCCache = useCCache |
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.oldWrapperPath = oldWrapperPath |
| 93 | cfg.version = version |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 94 | return &cfg, nil |
| 95 | } |
| 96 | |
| 97 | var llvmNextFlags = []string{ |
| 98 | "-Wno-reorder-init-list", |
| 99 | "-Wno-final-dtor-non-final-class", |
| 100 | "-Wno-implicit-int-float-conversion", |
| 101 | "-Wno-return-stack-address", |
Denis Nikitin | 1fe6209 | 2019-10-03 16:33:17 -0700 | [diff] [blame] | 102 | "-Werror=poison-system-directories", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 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. |
| 125 | clangFlags: []string{ |
| 126 | "-Qunused-arguments", |
| 127 | "-grecord-gcc-switches", |
| 128 | "-fno-addrsig", |
| 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", |
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", |
| 159 | }, |
| 160 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 161 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 162 | |
| 163 | // Flags to be added to host toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 164 | var crosHostConfig = &config{ |
| 165 | isHostWrapper: true, |
| 166 | rootRelPath: "../..", |
| 167 | commonFlags: []string{}, |
| 168 | gccFlags: []string{ |
| 169 | "-Wno-maybe-uninitialized", |
| 170 | "-Wno-unused-local-typedefs", |
| 171 | "-Wno-deprecated-declarations", |
| 172 | }, |
| 173 | // Temporarily disable tautological-*-compare chromium:778316. |
| 174 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 175 | clangFlags: []string{ |
| 176 | "-Qunused-arguments", |
| 177 | "-grecord-gcc-switches", |
| 178 | "-fno-addrsig", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 179 | "-fuse-ld=lld", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 180 | "-Wno-unused-local-typedefs", |
| 181 | "-Wno-deprecated-declarations", |
| 182 | "-Wno-tautological-constant-compare", |
| 183 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 184 | "-Wno-unknown-warning-option", |
| 185 | }, |
| 186 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 187 | } |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 188 | |
| 189 | var androidConfig = &config{ |
| 190 | isHostWrapper: false, |
| 191 | isAndroidWrapper: true, |
| 192 | rootRelPath: "./", |
| 193 | commonFlags: []string{}, |
| 194 | gccFlags: []string{}, |
| 195 | clangFlags: []string{}, |
| 196 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 197 | } |