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. |
| 13 | isHostWrapper bool |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 14 | // Whether to use ccache. |
| 15 | useCCache bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 16 | // Flags to add to gcc and clang. |
| 17 | commonFlags []string |
| 18 | // Flags to add to gcc only. |
| 19 | gccFlags []string |
| 20 | // Flags to add to clang only. |
| 21 | clangFlags []string |
| 22 | // Toolchain root path relative to the wrapper binary. |
| 23 | rootRelPath string |
| 24 | // Path of the old wrapper using the toolchain root. |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 25 | oldWrapperPath string |
| 26 | // Whether to mock out the calls that the old wrapper does. |
| 27 | mockOldWrapperCmds bool |
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 | } |
| 64 | // FIXME: Remove comparison to old wrapper once the new wrapper has landed. |
| 65 | oldWrapperPath := "" |
| 66 | config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, 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 | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 73 | func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) { |
| 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 | 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.oldWrapperPath = oldWrapperPath |
| 90 | cfg.version = version |
Tobias Bosch | 8bb8b5b | 2019-09-26 09:45:35 -0700 | [diff] [blame] | 91 | return &cfg, nil |
| 92 | } |
| 93 | |
| 94 | var llvmNextFlags = []string{ |
| 95 | "-Wno-reorder-init-list", |
| 96 | "-Wno-final-dtor-non-final-class", |
| 97 | "-Wno-implicit-int-float-conversion", |
| 98 | "-Wno-return-stack-address", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 99 | } |
| 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", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 131 | }, |
| 132 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 136 | var crosNonHardenedConfig = &config{ |
| 137 | rootRelPath: "../../../../..", |
| 138 | commonFlags: []string{}, |
| 139 | gccFlags: []string{ |
| 140 | "-Wno-maybe-uninitialized", |
| 141 | "-Wno-unused-local-typedefs", |
| 142 | "-Wno-deprecated-declarations", |
| 143 | "-Wtrampolines", |
| 144 | }, |
| 145 | // Temporarily disable tautological-*-compare chromium:778316. |
| 146 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 147 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 148 | clangFlags: []string{ |
| 149 | "-Qunused-arguments", |
| 150 | "-Wno-tautological-constant-compare", |
| 151 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 152 | "-Wno-unknown-warning-option", |
| 153 | "-Wno-section", |
| 154 | "-static-libgcc", |
| 155 | }, |
| 156 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 157 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 158 | |
| 159 | // Flags to be added to host toolchain. |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 160 | var crosHostConfig = &config{ |
| 161 | isHostWrapper: true, |
| 162 | rootRelPath: "../..", |
| 163 | commonFlags: []string{}, |
| 164 | gccFlags: []string{ |
| 165 | "-Wno-maybe-uninitialized", |
| 166 | "-Wno-unused-local-typedefs", |
| 167 | "-Wno-deprecated-declarations", |
| 168 | }, |
| 169 | // Temporarily disable tautological-*-compare chromium:778316. |
| 170 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 171 | clangFlags: []string{ |
| 172 | "-Qunused-arguments", |
| 173 | "-grecord-gcc-switches", |
| 174 | "-fno-addrsig", |
Manoj Gupta | 802e83e | 2019-09-13 13:08:14 -0700 | [diff] [blame] | 175 | "-fuse-ld=lld", |
Tobias Bosch | d8aa0d0 | 2019-08-19 09:55:30 -0700 | [diff] [blame] | 176 | "-Wno-unused-local-typedefs", |
| 177 | "-Wno-deprecated-declarations", |
| 178 | "-Wno-tautological-constant-compare", |
| 179 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 180 | "-Wno-unknown-warning-option", |
| 181 | }, |
| 182 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 183 | } |