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 5package main 6 7import ( 8 "strconv" 9) 10 11type config struct { 12 // TODO: Refactor this flag into more generic configuration properties. 13 isHostWrapper bool 14 isAndroidWrapper bool 15 // Whether to use ccache. 16 useCCache bool 17 // Whether llvmNext wrapper. 18 useLlvmNext bool 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 25 // Flags to add to clang only, AFTER user flags (cannot be overridden 26 // by the user). 27 clangPostFlags []string 28 // Toolchain root path relative to the wrapper binary. 29 rootRelPath string 30 // Directory to store errors that were prevented with -Wno-error. 31 newWarningsDir string 32 // Directory to store nits in when using `WITH_TIDY=tricium`. 33 triciumNitsDir string 34 // Directory to store crash artifacts in. 35 crashArtifactsDir string 36 // Version. Only used for printing via -print-cmd. 37 version string 38} 39 40// Version can be set via a linker flag. 41// Values fills config.version. 42var Version = "" 43 44// UseCCache can be set via a linker flag. 45// Value will be passed to strconv.ParseBool. 46// E.g. go build -ldflags '-X config.UseCCache=true'. 47var UseCCache = "unknown" 48 49// UseLlvmNext can be set via a linker flag. 50// Value will be passed to strconv.ParseBool. 51// E.g. go build -ldflags '-X config.UseLlvmNext=true'. 52var UseLlvmNext = "unknown" 53 54// ConfigName can be set via a linker flag. 55// Value has to be one of: 56// - "cros.hardened" 57// - "cros.nonhardened" 58var ConfigName = "unknown" 59 60// Returns the configuration matching the UseCCache and ConfigName. 61func getRealConfig() (*config, error) { 62 useCCache, err := strconv.ParseBool(UseCCache) 63 if err != nil { 64 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") 65 } 66 useLlvmNext, err := strconv.ParseBool(UseLlvmNext) 67 if err != nil { 68 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext") 69 } 70 config, err := getConfig(ConfigName, useCCache, useLlvmNext, Version) 71 if err != nil { 72 return nil, err 73 } 74 return config, nil 75} 76 77func isAndroidConfig() bool { 78 return ConfigName == "android" 79} 80 81func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) { 82 cfg := config{} 83 switch configName { 84 case "cros.hardened": 85 cfg = *crosHardenedConfig 86 case "cros.nonhardened": 87 cfg = *crosNonHardenedConfig 88 case "cros.host": 89 cfg = *crosHostConfig 90 case "android": 91 cfg = *androidConfig 92 default: 93 return nil, newErrorwithSourceLocf("unknown config name: %s", configName) 94 } 95 cfg.useCCache = useCCache 96 cfg.useLlvmNext = useLlvmNext 97 if useLlvmNext { 98 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...) 99 cfg.clangPostFlags = append(cfg.clangPostFlags, llvmNextPostFlags...) 100 } 101 cfg.version = version 102 return &cfg, nil 103} 104 105// Full hardening. 106// Temporarily disable function splitting because of chromium:434751. 107var 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 // Pass "-fcommon" till the packages are fixed to work with new clang default 126 // "-fno-common", crbug.com/1060413. 127 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache; 128 // removed that flag for now. 129 clangFlags: []string{ 130 "-Qunused-arguments", 131 "-fno-addrsig", 132 "-fcommon", 133 "-Wno-tautological-constant-compare", 134 "-Wno-tautological-unsigned-enum-zero-compare", 135 "-Wno-unknown-warning-option", 136 "-Wno-section", 137 "-static-libgcc", 138 "-fuse-ld=lld", 139 "-Wno-final-dtor-non-final-class", 140 "-Werror=poison-system-directories", 141 "-fexperimental-new-pass-manager", 142 "-Wno-compound-token-split-by-macro", 143 }, 144 clangPostFlags: []string{ 145 "-Wno-implicit-int-float-conversion", 146 "-Wno-compound-token-split-by-space", 147 "-Wno-string-concatenation", 148 }, 149 newWarningsDir: "/tmp/fatal_clang_warnings", 150 triciumNitsDir: "/tmp/linting_output/clang-tidy", 151 crashArtifactsDir: "/tmp/clang_crash_diagnostics", 152} 153 154// Flags to be added to non-hardened toolchain. 155var crosNonHardenedConfig = &config{ 156 rootRelPath: "../../../../..", 157 commonFlags: []string{}, 158 gccFlags: []string{ 159 "-Wno-maybe-uninitialized", 160 "-Wno-unused-local-typedefs", 161 "-Wno-deprecated-declarations", 162 "-Wtrampolines", 163 }, 164 // Temporarily disable tautological-*-compare chromium:778316. 165 // Temporarily add no-unknown-warning-option to deal with old clang versions. 166 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 167 clangFlags: []string{ 168 "-Qunused-arguments", 169 "-Wno-tautological-constant-compare", 170 "-Wno-tautological-unsigned-enum-zero-compare", 171 "-Wno-unknown-warning-option", 172 "-Wno-section", 173 "-static-libgcc", 174 "-Wno-final-dtor-non-final-class", 175 "-Werror=poison-system-directories", 176 "-fexperimental-new-pass-manager", 177 "-Wno-compound-token-split-by-macro", 178 }, 179 clangPostFlags: []string{ 180 "-Wno-implicit-int-float-conversion", 181 "-Wno-compound-token-split-by-space", 182 "-Wno-string-concatenation", 183 }, 184 newWarningsDir: "/tmp/fatal_clang_warnings", 185 triciumNitsDir: "/tmp/linting_output/clang-tidy", 186 crashArtifactsDir: "/tmp/clang_crash_diagnostics", 187} 188 189// Flags to be added to host toolchain. 190var crosHostConfig = &config{ 191 isHostWrapper: true, 192 rootRelPath: "../..", 193 commonFlags: []string{}, 194 gccFlags: []string{ 195 "-Wno-maybe-uninitialized", 196 "-Wno-unused-local-typedefs", 197 "-Wno-deprecated-declarations", 198 }, 199 // Temporarily disable tautological-*-compare chromium:778316. 200 // Temporarily add no-unknown-warning-option to deal with old clang versions. 201 // Pass "-fcommon" till the packages are fixed to work with new clang default 202 // "-fno-common", crbug.com/1060413. 203 // crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache; 204 // removed that flag for now. 205 clangFlags: []string{ 206 "-Qunused-arguments", 207 "-fno-addrsig", 208 "-fcommon", 209 "-fuse-ld=lld", 210 "-Wno-unused-local-typedefs", 211 "-Wno-deprecated-declarations", 212 "-Wno-tautological-constant-compare", 213 "-Wno-tautological-unsigned-enum-zero-compare", 214 "-Wno-final-dtor-non-final-class", 215 "-Werror=poison-system-directories", 216 "-Wno-unknown-warning-option", 217 "-fexperimental-new-pass-manager", 218 "-Wno-compound-token-split-by-macro", 219 }, 220 clangPostFlags: []string{ 221 "-Wno-implicit-int-float-conversion", 222 "-Wno-compound-token-split-by-space", 223 "-Wno-string-concatenation", 224 }, 225 newWarningsDir: "/tmp/fatal_clang_warnings", 226 triciumNitsDir: "/tmp/linting_output/clang-tidy", 227 crashArtifactsDir: "/tmp/clang_crash_diagnostics", 228} 229 230var androidConfig = &config{ 231 isHostWrapper: false, 232 isAndroidWrapper: true, 233 rootRelPath: "./", 234 commonFlags: []string{}, 235 gccFlags: []string{}, 236 clangFlags: []string{}, 237 clangPostFlags: []string{}, 238 newWarningsDir: "", 239 triciumNitsDir: "", 240 crashArtifactsDir: "", 241} 242