global.go 18 KB

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