1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package config 16 17import ( 18 "runtime" 19 "strings" 20 21 "android/soong/android" 22 "android/soong/remoteexec" 23) 24 25var ( 26 pctx = android.NewPackageContext("android/soong/cc/config") 27 exportedVars = android.NewExportedVariables(pctx) 28 29 // Flags used by lots of devices. Putting them in package static variables 30 // will save bytes in build.ninja so they aren't repeated for every file 31 commonGlobalCflags = []string{ 32 "-DANDROID", 33 "-fmessage-length=0", 34 "-W", 35 "-Wall", 36 "-Wno-unused", 37 "-Winit-self", 38 "-Wpointer-arith", 39 "-Wunreachable-code-loop-increment", 40 41 // Make paths in deps files relative 42 "-no-canonical-prefixes", 43 44 "-DNDEBUG", 45 "-UDEBUG", 46 47 "-fno-exceptions", 48 "-Wno-multichar", 49 50 "-O2", 51 "-g", 52 "-fdebug-default-version=5", 53 54 "-fno-strict-aliasing", 55 56 "-Werror=date-time", 57 "-Werror=pragma-pack", 58 "-Werror=pragma-pack-suspicious-include", 59 "-Werror=string-plus-int", 60 "-Werror=unreachable-code-loop-increment", 61 62 // Force deprecation warnings to be warnings for code that compiles with -Werror. 63 // Making deprecated usages an error causes extreme pain when trying to deprecate anything. 64 "-Wno-error=deprecated-declarations", 65 66 "-D__compiler_offsetof=__builtin_offsetof", 67 68 // Emit address-significance table which allows linker to perform safe ICF. Clang does 69 // not emit the table by default on Android since NDK still uses GNU binutils. 70 "-faddrsig", 71 72 // Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug 73 // tracking this is http://b/151457797. 74 "-fcommon", 75 76 // Help catch common 32/64-bit errors. 77 "-Werror=int-conversion", 78 79 // Disable overly aggressive warning for macros defined with a leading underscore 80 // This happens in AndroidConfig.h, which is included nearly everywhere. 81 // TODO: can we remove this now? 82 "-Wno-reserved-id-macro", 83 84 // Force clang to always output color diagnostics. Ninja will strip the ANSI 85 // color codes if it is not running in a terminal. 86 "-fcolor-diagnostics", 87 88 // Warnings from clang-7.0 89 "-Wno-sign-compare", 90 91 // Disable -Winconsistent-missing-override until we can clean up the existing 92 // codebase for it. 93 "-Wno-inconsistent-missing-override", 94 95 // Warnings from clang-10 96 // Nested and array designated initialization is nice to have. 97 "-Wno-c99-designator", 98 99 // Many old files still have GNU designator syntax. 100 "-Wno-gnu-designator", 101 102 // Warnings from clang-12 103 "-Wno-gnu-folding-constant", 104 105 // Calls to the APIs that are newer than the min sdk version of the caller should be 106 // guarded with __builtin_available. 107 "-Wunguarded-availability", 108 // This macro allows the bionic versioning.h to indirectly determine whether the 109 // option -Wunguarded-availability is on or not. 110 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__", 111 112 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949) 113 "-ffp-contract=off", 114 } 115 116 commonGlobalConlyflags = []string{} 117 118 commonGlobalAsflags = []string{ 119 "-D__ASSEMBLY__", 120 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to 121 // end up in the dwarf data for crtend_so.S. 122 "-fdebug-default-version=4", 123 } 124 125 deviceGlobalCflags = []string{ 126 "-ffunction-sections", 127 "-fdata-sections", 128 "-fno-short-enums", 129 "-funwind-tables", 130 "-fstack-protector-strong", 131 "-Wa,--noexecstack", 132 "-D_FORTIFY_SOURCE=2", 133 134 "-Wstrict-aliasing=2", 135 136 "-Werror=return-type", 137 "-Werror=non-virtual-dtor", 138 "-Werror=address", 139 "-Werror=sequence-point", 140 "-Werror=format-security", 141 "-nostdlibinc", 142 143 // Emit additional debug info for AutoFDO 144 "-fdebug-info-for-profiling", 145 } 146 147 commonGlobalLldflags = []string{ 148 "-fuse-ld=lld", 149 "-Wl,--icf=safe", 150 } 151 152 deviceGlobalCppflags = []string{ 153 "-fvisibility-inlines-hidden", 154 } 155 156 deviceGlobalLdflags = []string{ 157 "-Wl,-z,noexecstack", 158 "-Wl,-z,relro", 159 "-Wl,-z,now", 160 "-Wl,--build-id=md5", 161 "-Wl,--fatal-warnings", 162 "-Wl,--no-undefined-version", 163 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these 164 // --exclude-libs arguments can be removed. 165 "-Wl,--exclude-libs,libgcc.a", 166 "-Wl,--exclude-libs,libgcc_stripped.a", 167 "-Wl,--exclude-libs,libunwind_llvm.a", 168 "-Wl,--exclude-libs,libunwind.a", 169 } 170 171 deviceGlobalLldflags = append(deviceGlobalLdflags, commonGlobalLldflags...) 172 173 hostGlobalCflags = []string{} 174 175 hostGlobalCppflags = []string{} 176 177 hostGlobalLdflags = []string{} 178 179 hostGlobalLldflags = commonGlobalLldflags 180 181 commonGlobalCppflags = []string{ 182 "-Wsign-promo", 183 184 // -Wimplicit-fallthrough is not enabled by -Wall. 185 "-Wimplicit-fallthrough", 186 187 // Enable clang's thread-safety annotations in libcxx. 188 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", 189 190 // libc++'s math.h has an #include_next outside of system_headers. 191 "-Wno-gnu-include-next", 192 } 193 194 noOverrideGlobalCflags = []string{ 195 "-Werror=bool-operation", 196 "-Werror=format-insufficient-args", 197 "-Werror=implicit-int-float-conversion", 198 "-Werror=int-in-bool-context", 199 "-Werror=int-to-pointer-cast", 200 "-Werror=pointer-to-int-cast", 201 "-Werror=xor-used-as-pow", 202 // http://b/161386391 for -Wno-void-pointer-to-enum-cast 203 "-Wno-void-pointer-to-enum-cast", 204 // http://b/161386391 for -Wno-void-pointer-to-int-cast 205 "-Wno-void-pointer-to-int-cast", 206 // http://b/161386391 for -Wno-pointer-to-int-cast 207 "-Wno-pointer-to-int-cast", 208 "-Werror=fortify-source", 209 210 "-Werror=address-of-temporary", 211 "-Werror=null-dereference", 212 "-Werror=return-type", 213 214 // http://b/72331526 Disable -Wtautological-* until the instances detected by these 215 // new warnings are fixed. 216 "-Wno-tautological-constant-compare", 217 "-Wno-tautological-type-limit-compare", 218 // http://b/145210666 219 "-Wno-reorder-init-list", 220 // http://b/145211066 221 "-Wno-implicit-int-float-conversion", 222 // New warnings to be fixed after clang-r377782. 223 "-Wno-tautological-overlap-compare", // http://b/148815696 224 // New warnings to be fixed after clang-r383902. 225 "-Wno-deprecated-copy", // http://b/153746672 226 "-Wno-range-loop-construct", // http://b/153747076 227 "-Wno-zero-as-null-pointer-constant", // http://b/68236239 228 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485 229 "-Wno-pessimizing-move", // http://b/154270751 230 // New warnings to be fixed after clang-r399163 231 "-Wno-non-c-typedef-for-linkage", // http://b/161304145 232 // New warnings to be fixed after clang-r428724 233 "-Wno-align-mismatch", // http://b/193679946 234 // New warnings to be fixed after clang-r433403 235 "-Wno-error=unused-but-set-variable", // http://b/197240255 236 "-Wno-error=unused-but-set-parameter", // http://b/197240255 237 // New warnings to be fixed after clang-r468909 238 "-Wno-error=deprecated-builtins", // http://b/241601211 239 "-Wno-error=deprecated", // in external/googletest/googletest 240 // New warnings to be fixed after clang-r475365 241 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903 242 "-Wno-error=enum-constexpr-conversion", // http://b/243964282 243 } 244 245 noOverride64GlobalCflags = []string{} 246 247 noOverrideExternalGlobalCflags = []string{ 248 // http://b/191699019 249 "-Wno-format-insufficient-args", 250 "-Wno-sizeof-array-div", 251 "-Wno-incompatible-function-pointer-types", 252 "-Wno-unused-but-set-variable", 253 "-Wno-unused-but-set-parameter", 254 "-Wno-unqualified-std-cast-call", 255 "-Wno-bitwise-instead-of-logical", 256 "-Wno-misleading-indentation", 257 "-Wno-array-parameter", 258 "-Wno-gnu-offsetof-extensions", 259 } 260 261 // Extra cflags for external third-party projects to disable warnings that 262 // are infeasible to fix in all the external projects and their upstream repos. 263 extraExternalCflags = []string{ 264 "-Wno-enum-compare", 265 "-Wno-enum-compare-switch", 266 267 // http://b/72331524 Allow null pointer arithmetic until the instances detected by 268 // this new warning are fixed. 269 "-Wno-null-pointer-arithmetic", 270 271 // Bug: http://b/29823425 Disable -Wnull-dereference until the 272 // new instances detected by this warning are fixed. 273 "-Wno-null-dereference", 274 275 // http://b/145211477 276 "-Wno-pointer-compare", 277 "-Wno-final-dtor-non-final-class", 278 279 // http://b/165945989 280 "-Wno-psabi", 281 282 // http://b/199369603 283 "-Wno-null-pointer-subtraction", 284 285 // http://b/175068488 286 "-Wno-string-concatenation", 287 288 // http://b/239661264 289 "-Wno-deprecated-non-prototype", 290 } 291 292 llvmNextExtraCommonGlobalCflags = []string{ 293 // Do not report warnings when testing with the top of trunk LLVM. 294 "-Wno-error", 295 } 296 297 IllegalFlags = []string{ 298 "-w", 299 } 300 301 CStdVersion = "gnu11" 302 CppStdVersion = "gnu++17" 303 ExperimentalCStdVersion = "gnu17" 304 ExperimentalCppStdVersion = "gnu++2a" 305 306 // prebuilts/clang default settings. 307 ClangDefaultBase = "prebuilts/clang/host" 308 ClangDefaultVersion = "clang-r487747c" 309 ClangDefaultShortVersion = "17" 310 311 // Directories with warnings from Android.bp files. 312 WarningAllowedProjects = []string{ 313 "device/", 314 "vendor/", 315 } 316 317 VersionScriptFlagPrefix = "-Wl,--version-script," 318 319 VisibilityHiddenFlag = "-fvisibility=hidden" 320 VisibilityDefaultFlag = "-fvisibility=default" 321) 322 323// BazelCcToolchainVars generates bzl file content containing variables for 324// Bazel's cc_toolchain configuration. 325func BazelCcToolchainVars(config android.Config) string { 326 return android.BazelToolchainVars(config, exportedVars) 327} 328 329func ExportStringList(name string, value []string) { 330 exportedVars.ExportStringList(name, value) 331} 332 333func init() { 334 if runtime.GOOS == "linux" { 335 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=") 336 } 337 338 exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags) 339 exportedVars.ExportStringListStaticVariable("CommonGlobalAsflags", commonGlobalAsflags) 340 exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags) 341 exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags) 342 exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags) 343 exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags) 344 exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags) 345 exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags) 346 347 // Export the static default CommonGlobalCflags to Bazel. 348 exportedVars.ExportStringList("CommonGlobalCflags", commonGlobalCflags) 349 350 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string { 351 flags := commonGlobalCflags 352 353 // http://b/131390872 354 // Automatically initialize any uninitialized stack variables. 355 // Prefer zero-init if multiple options are set. 356 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") { 357 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -Wno-unused-command-line-argument") 358 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") { 359 flags = append(flags, "-ftrivial-auto-var-init=pattern") 360 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") { 361 flags = append(flags, "-ftrivial-auto-var-init=uninitialized") 362 } else { 363 // Default to zero initialization. 364 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -Wno-unused-command-line-argument") 365 } 366 367 // Workaround for ccache with clang. 368 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html. 369 if ctx.Config().IsEnvTrue("USE_CCACHE") { 370 flags = append(flags, "-Wno-unused-command-line-argument") 371 } 372 373 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") { 374 flags = append(flags, "-Wno-error=unknown-warning-option") 375 } 376 377 return strings.Join(flags, " ") 378 }) 379 380 // Export the static default DeviceGlobalCflags to Bazel. 381 // TODO(187086342): handle cflags that are set in VariableFuncs. 382 exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags) 383 384 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string { 385 return strings.Join(deviceGlobalCflags, " ") 386 }) 387 388 // Export the static default NoOverrideGlobalCflags to Bazel. 389 exportedVars.ExportStringList("NoOverrideGlobalCflags", noOverrideGlobalCflags) 390 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string { 391 flags := noOverrideGlobalCflags 392 if ctx.Config().IsEnvTrue("LLVM_NEXT") { 393 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...) 394 } 395 return strings.Join(flags, " ") 396 }) 397 398 exportedVars.ExportStringListStaticVariable("NoOverride64GlobalCflags", noOverride64GlobalCflags) 399 exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags) 400 exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags) 401 exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags) 402 exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags) 403 404 exportedVars.ExportString("CStdVersion", CStdVersion) 405 exportedVars.ExportString("CppStdVersion", CppStdVersion) 406 exportedVars.ExportString("ExperimentalCStdVersion", ExperimentalCStdVersion) 407 exportedVars.ExportString("ExperimentalCppStdVersion", ExperimentalCppStdVersion) 408 409 exportedVars.ExportString("VersionScriptFlagPrefix", VersionScriptFlagPrefix) 410 411 exportedVars.ExportString("VisibilityHiddenFlag", VisibilityHiddenFlag) 412 exportedVars.ExportString("VisibilityDefaultFlag", VisibilityDefaultFlag) 413 414 // Everything in these lists is a crime against abstraction and dependency tracking. 415 // Do not add anything to this list. 416 commonGlobalIncludes := []string{ 417 "system/core/include", 418 "system/logging/liblog/include", 419 "system/media/audio/include", 420 "hardware/libhardware/include", 421 "hardware/libhardware_legacy/include", 422 "hardware/ril/include", 423 "frameworks/native/include", 424 "frameworks/native/opengl/include", 425 "frameworks/av/include", 426 } 427 exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes) 428 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes) 429 430 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase) 431 exportedVars.ExportStringStaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion) 432 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}") 433 pctx.StaticVariable("ClangBin", "${ClangPath}/bin") 434 435 exportedVars.ExportStringStaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion) 436 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux") 437 438 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts 439 // being used for the rest of the build process. 440 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host") 441 pctx.SourcePathVariable("RSClangVersion", "clang-3289846") 442 pctx.SourcePathVariable("RSReleaseVersion", "3.8") 443 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin") 444 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include") 445 446 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", 447 []string{ 448 "external/clang/lib/Headers", 449 "frameworks/rs/script_api/include", 450 }) 451 452 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string { 453 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" { 454 return override + " " 455 } 456 return "" 457 }) 458 459 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool) 460 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool) 461 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool) 462 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy) 463 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy) 464 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy) 465 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy) 466} 467 468var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS) 469 470func ClangPath(ctx android.PathContext, file string) android.SourcePath { 471 type clangToolKey string 472 473 key := android.NewCustomOnceKey(clangToolKey(file)) 474 475 return ctx.Config().OnceSourcePath(key, func() android.SourcePath { 476 return clangPath(ctx).Join(ctx, file) 477 }) 478} 479 480var clangPathKey = android.NewOnceKey("clangPath") 481 482func clangPath(ctx android.PathContext) android.SourcePath { 483 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath { 484 clangBase := ClangDefaultBase 485 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" { 486 clangBase = override 487 } 488 clangVersion := ClangDefaultVersion 489 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" { 490 clangVersion = override 491 } 492 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion) 493 }) 494} 495