global.go 15 KB

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