global.go 14 KB

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