blob: 3ee3fa2bbd86d23d16599a2511b655c3598e8f93 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
Colin Crossb98c8b02016-07-29 13:44:28 -070015package config
Colin Cross4d9c2d12016-07-29 12:48:20 -070016
17import (
Colin Cross0c66bc62021-07-20 09:47:41 -070018 "runtime"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019 "strings"
20
21 "android/soong/android"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040022 "android/soong/remoteexec"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023)
24
Colin Cross4d9c2d12016-07-29 12:48:20 -070025var (
Sam Delmerico7f889562022-03-25 14:55:40 +000026 pctx = android.NewPackageContext("android/soong/cc/config")
27 exportedVars = android.NewExportedVariables(pctx)
28
Leo Li8756b372017-05-22 16:11:34 -070029 // 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
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 commonGlobalCflags = []string{
32 "-DANDROID",
33 "-fmessage-length=0",
34 "-W",
35 "-Wall",
36 "-Wno-unused",
37 "-Winit-self",
38 "-Wpointer-arith",
George Burgess IVfb81db22020-02-22 19:28:56 -080039 "-Wunreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070040
Colin Cross7278afc2017-11-02 22:38:32 -070041 // Make paths in deps files relative
42 "-no-canonical-prefixes",
43
Colin Cross4d9c2d12016-07-29 12:48:20 -070044 "-DNDEBUG",
45 "-UDEBUG",
Colin Cross7278afc2017-11-02 22:38:32 -070046
47 "-fno-exceptions",
48 "-Wno-multichar",
49
50 "-O2",
Stephen Hines8afd1752022-01-20 11:01:16 -080051 "-fdebug-default-version=5",
Colin Cross7278afc2017-11-02 22:38:32 -070052
53 "-fno-strict-aliasing",
Dan Willemsen5d980c82019-08-27 19:37:10 -070054
55 "-Werror=date-time",
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080056 "-Werror=pragma-pack",
57 "-Werror=pragma-pack-suspicious-include",
Christopher Di Bella23a991c2020-11-11 22:41:32 +000058 "-Werror=string-plus-int",
George Burgess IVfb81db22020-02-22 19:28:56 -080059 "-Werror=unreachable-code-loop-increment",
Colin Cross9cc59c12021-07-14 17:59:54 -070060
Krzysztof Kosiński6934b0e2022-08-07 06:56:54 +000061 // Force deprecation warnings to be warnings for code that compiles with -Werror.
62 // Making deprecated usages an error causes extreme pain when trying to deprecate anything.
63 "-Wno-error=deprecated-declarations",
64
Colin Cross9cc59c12021-07-14 17:59:54 -070065 "-D__compiler_offsetof=__builtin_offsetof",
66
67 // Emit address-significance table which allows linker to perform safe ICF. Clang does
68 // not emit the table by default on Android since NDK still uses GNU binutils.
69 "-faddrsig",
70
Chia-Chi Teng4e0f6452023-08-11 18:51:26 +000071 // Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug
72 // tracking this is http://b/151457797.
73 "-fcommon",
74
Colin Cross9cc59c12021-07-14 17:59:54 -070075 // Help catch common 32/64-bit errors.
76 "-Werror=int-conversion",
77
Colin Cross9cc59c12021-07-14 17:59:54 -070078 // Force clang to always output color diagnostics. Ninja will strip the ANSI
79 // color codes if it is not running in a terminal.
80 "-fcolor-diagnostics",
81
Krzysztof Kosiński982c5882023-08-18 18:56:45 +000082 // -Wno-sign-compare is incompatible with the Google C++ style guidance
83 // to use 'int' for loop indices, and the signal to noise ratio is poor
84 // anyway.
Colin Cross9cc59c12021-07-14 17:59:54 -070085 "-Wno-sign-compare",
86
Krzysztof Kosiński982c5882023-08-18 18:56:45 +000087 // AIDL generated code redeclares pure virtual methods in each
88 // subsequent version of an interface, so this is currently infeasible
89 // to enable.
Colin Cross9cc59c12021-07-14 17:59:54 -070090 "-Wno-inconsistent-missing-override",
91
Krzysztof Kosiński982c5882023-08-18 18:56:45 +000092 // Designated initializer syntax is recommended by the Google C++ style
93 // guide and should not be a warning, at least by default.
Colin Cross9cc59c12021-07-14 17:59:54 -070094 "-Wno-c99-designator",
95
96 // Warnings from clang-12
97 "-Wno-gnu-folding-constant",
98
Krzysztof Kosiński78425e52023-08-18 21:23:52 +000099 // http://b/145210666
100 "-Wno-error=reorder-init-list",
101
Colin Cross9cc59c12021-07-14 17:59:54 -0700102 // Calls to the APIs that are newer than the min sdk version of the caller should be
103 // guarded with __builtin_available.
104 "-Wunguarded-availability",
105 // This macro allows the bionic versioning.h to indirectly determine whether the
106 // option -Wunguarded-availability is on or not.
107 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800108
109 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
110 "-ffp-contract=off",
AdityaK423e4ce2023-05-26 12:08:05 -0700111
112 // Using simple template names reduces the size of debug builds.
113 "-gsimple-template-names",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 }
115
Colin Cross6f6a4282016-10-17 14:19:06 -0700116 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700117
Liz Kammere4d1bda2022-06-22 21:02:08 +0000118 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
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700126 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800127 "-fdata-sections",
128 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700129 "-funwind-tables",
130 "-fstack-protector-strong",
131 "-Wa,--noexecstack",
132 "-D_FORTIFY_SOURCE=2",
133
134 "-Wstrict-aliasing=2",
135
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 "-Werror=return-type",
137 "-Werror=non-virtual-dtor",
138 "-Werror=address",
139 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700140 "-Werror=format-security",
Colin Crossc8bed312021-07-14 17:56:21 -0700141 "-nostdlibinc",
Yi Kong196b9262021-12-07 13:51:38 +0800142
Yi Kong67a2dee2023-09-01 21:52:36 +0900143 // Enable MLGO for register allocation.
144 "-mllvm -regalloc-enable-advisor=release",
145
Yi Kong196b9262021-12-07 13:51:38 +0800146 // Emit additional debug info for AutoFDO
147 "-fdebug-info-for-profiling",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
149
Yi Konga9e1df12022-10-20 14:45:52 +0900150 commonGlobalLldflags = []string{
151 "-fuse-ld=lld",
152 "-Wl,--icf=safe",
Steven Moreland8fe37e42023-07-22 00:14:55 +0000153 "-Wl,--no-demangle",
Yi Konga9e1df12022-10-20 14:45:52 +0900154 }
155
Colin Cross26f14502017-11-06 13:59:48 -0800156 deviceGlobalCppflags = []string{
157 "-fvisibility-inlines-hidden",
158 }
159
Colin Cross324a4572017-11-02 23:09:41 -0700160 deviceGlobalLdflags = []string{
161 "-Wl,-z,noexecstack",
162 "-Wl,-z,relro",
163 "-Wl,-z,now",
164 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700165 "-Wl,--fatal-warnings",
166 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800167 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
168 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700169 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700170 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800171 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800172 "-Wl,--exclude-libs,libunwind.a",
Yi Kong67a2dee2023-09-01 21:52:36 +0900173 // Enable MLGO for register allocation.
174 "-Wl,-mllvm,-regalloc-enable-advisor=release",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 }
176
Yi Konga9e1df12022-10-20 14:45:52 +0900177 deviceGlobalLldflags = append(deviceGlobalLdflags, commonGlobalLldflags...)
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700178
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 hostGlobalCflags = []string{}
180
Colin Cross26f14502017-11-06 13:59:48 -0800181 hostGlobalCppflags = []string{}
182
Colin Cross324a4572017-11-02 23:09:41 -0700183 hostGlobalLdflags = []string{}
184
Yi Konga9e1df12022-10-20 14:45:52 +0900185 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700186
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 commonGlobalCppflags = []string{
188 "-Wsign-promo",
Colin Crossc8bed312021-07-14 17:56:21 -0700189
190 // -Wimplicit-fallthrough is not enabled by -Wall.
191 "-Wimplicit-fallthrough",
192
193 // Enable clang's thread-safety annotations in libcxx.
194 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
195
196 // libc++'s math.h has an #include_next outside of system_headers.
197 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 }
199
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000200 // These flags are appended after the module's cflags, so they cannot be
201 // overridden from Android.bp files.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000203 "-Werror=bool-operation",
Zijun Zhaod1569082023-03-08 23:48:45 +0000204 "-Werror=format-insufficient-args",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000205 "-Werror=implicit-int-float-conversion",
206 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207 "-Werror=int-to-pointer-cast",
208 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000209 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700210 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
211 "-Wno-void-pointer-to-enum-cast",
212 // http://b/161386391 for -Wno-void-pointer-to-int-cast
213 "-Wno-void-pointer-to-int-cast",
214 // http://b/161386391 for -Wno-pointer-to-int-cast
215 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700216 "-Werror=fortify-source",
Colin Crossc8bed312021-07-14 17:56:21 -0700217
218 "-Werror=address-of-temporary",
zijunzhao2863c0a2023-02-06 21:28:30 +0000219 "-Werror=null-dereference",
Colin Crossc8bed312021-07-14 17:56:21 -0700220 "-Werror=return-type",
221
222 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
223 // new warnings are fixed.
224 "-Wno-tautological-constant-compare",
225 "-Wno-tautological-type-limit-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700226 // http://b/145211066
227 "-Wno-implicit-int-float-conversion",
228 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700229 "-Wno-tautological-overlap-compare", // http://b/148815696
230 // New warnings to be fixed after clang-r383902.
231 "-Wno-deprecated-copy", // http://b/153746672
232 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700233 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
234 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Colin Crossc8bed312021-07-14 17:56:21 -0700235 "-Wno-pessimizing-move", // http://b/154270751
236 // New warnings to be fixed after clang-r399163
237 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000238 // New warnings to be fixed after clang-r428724
239 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800240 // New warnings to be fixed after clang-r433403
241 "-Wno-error=unused-but-set-variable", // http://b/197240255
242 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000243 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000244 "-Wno-error=deprecated-builtins", // http://b/241601211
245 "-Wno-error=deprecated", // in external/googletest/googletest
Yabin Cui29f248b2022-11-30 13:32:10 -0800246 // New warnings to be fixed after clang-r475365
247 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903
Yabin Cui29f248b2022-11-30 13:32:10 -0800248 "-Wno-error=enum-constexpr-conversion", // http://b/243964282
Colin Crossc8bed312021-07-14 17:56:21 -0700249 }
250
zijunzhao933e3802023-01-12 07:26:20 +0000251 noOverride64GlobalCflags = []string{}
252
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000253 // Similar to noOverrideGlobalCflags, but applies only to third-party code
254 // (anything for which IsThirdPartyPath() in build/soong/android/paths.go
255 // returns true - includes external/, most of vendor/ and most of hardware/)
Stephen Hinese24303f2021-12-14 15:07:08 -0800256 noOverrideExternalGlobalCflags = []string{
Zijun Zhaod1569082023-03-08 23:48:45 +0000257 // http://b/191699019
258 "-Wno-format-insufficient-args",
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000259 // http://b/296422292
260 // Usually signals a mistake and should be a hard error.
Chih-hung Hsiehecd88692022-11-30 07:13:31 +0000261 "-Wno-sizeof-array-div",
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000262 // http://b/296321145
263 // Indicates potential memory or stack corruption, so should be changed
264 // to a hard error. Currently triggered by some vendor code.
zijunzhao2a08ccd2023-03-15 21:35:53 +0000265 "-Wno-incompatible-function-pointer-types",
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000266 // http://b/296321508
267 // Introduced in response to a critical security vulnerability and
268 // should be a hard error - it requires only whitespace changes to fix.
269 "-Wno-misleading-indentation",
270 // Triggered by old LLVM code in external/llvm. Likely not worth
271 // enabling since it's a cosmetic issue.
272 "-Wno-bitwise-instead-of-logical",
273
Stephen Hinese24303f2021-12-14 15:07:08 -0800274 "-Wno-unused-but-set-variable",
275 "-Wno-unused-but-set-parameter",
zijunzhao96ec99a2023-03-10 22:35:31 +0000276 "-Wno-unqualified-std-cast-call",
Chih-Hung Hsieh823e0b72022-10-07 10:23:51 -0700277 "-Wno-array-parameter",
Yi Kongfd07ed22023-02-16 17:42:27 +0900278 "-Wno-gnu-offsetof-extensions",
Stephen Hinese24303f2021-12-14 15:07:08 -0800279 }
280
Colin Crossc8bed312021-07-14 17:56:21 -0700281 // Extra cflags for external third-party projects to disable warnings that
282 // are infeasible to fix in all the external projects and their upstream repos.
283 extraExternalCflags = []string{
284 "-Wno-enum-compare",
285 "-Wno-enum-compare-switch",
286
287 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
288 // this new warning are fixed.
289 "-Wno-null-pointer-arithmetic",
290
291 // Bug: http://b/29823425 Disable -Wnull-dereference until the
292 // new instances detected by this warning are fixed.
293 "-Wno-null-dereference",
294
295 // http://b/145211477
296 "-Wno-pointer-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700297 "-Wno-final-dtor-non-final-class",
298
299 // http://b/165945989
300 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800301
302 // http://b/199369603
303 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800304
305 // http://b/175068488
306 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800307
308 // http://b/239661264
309 "-Wno-deprecated-non-prototype",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 }
311
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700312 llvmNextExtraCommonGlobalCflags = []string{
Yi Konga8b0bfb2022-12-26 16:02:50 +0900313 // Do not report warnings when testing with the top of trunk LLVM.
314 "-Wno-error",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700315 }
Yi Kongb79fc582022-07-13 14:50:33 +0800316
Colin Crossb98c8b02016-07-29 13:44:28 -0700317 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 "-w",
319 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700320
Elliott Hughesfb294e32023-06-14 10:42:45 -0700321 CStdVersion = "gnu17"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000322 CppStdVersion = "gnu++17"
Elliott Hughesfb294e32023-06-14 10:42:45 -0700323 ExperimentalCStdVersion = "gnu2x"
Elliott Hughes37976122018-11-28 14:16:39 -0800324 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700325
Leo Li8756b372017-05-22 16:11:34 -0700326 // prebuilts/clang default settings.
327 ClangDefaultBase = "prebuilts/clang/host"
Yi Konged7c73e2023-08-25 15:18:48 +0900328 ClangDefaultVersion = "clang-r498229b"
Yi Kongfd07ed22023-02-16 17:42:27 +0900329 ClangDefaultShortVersion = "17"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800330
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800331 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800332 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800333 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800334 "vendor/",
335 }
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000336
337 VersionScriptFlagPrefix = "-Wl,--version-script,"
Trevor Radcliffea772d652023-04-11 13:41:17 +0000338
339 VisibilityHiddenFlag = "-fvisibility=hidden"
340 VisibilityDefaultFlag = "-fvisibility=default"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341)
342
Sam Delmerico7f889562022-03-25 14:55:40 +0000343// BazelCcToolchainVars generates bzl file content containing variables for
344// Bazel's cc_toolchain configuration.
345func BazelCcToolchainVars(config android.Config) string {
346 return android.BazelToolchainVars(config, exportedVars)
347}
348
349func ExportStringList(name string, value []string) {
350 exportedVars.ExportStringList(name, value)
351}
Colin Crossb98c8b02016-07-29 13:44:28 -0700352
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700354 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000355 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
356 }
357
Sam Delmerico7f889562022-03-25 14:55:40 +0000358 exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
Liz Kammere4d1bda2022-06-22 21:02:08 +0000359 exportedVars.ExportStringListStaticVariable("CommonGlobalAsflags", commonGlobalAsflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000360 exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
361 exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
362 exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
363 exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
364 exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
365 exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000366
Colin Cross0523ba22021-07-14 18:45:05 -0700367 // Export the static default CommonGlobalCflags to Bazel.
Yu Liu19ea53d2022-12-21 12:39:30 -0800368 exportedVars.ExportStringList("CommonGlobalCflags", commonGlobalCflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369
Colin Cross0523ba22021-07-14 18:45:05 -0700370 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Cross33bac242021-07-14 17:03:16 -0700371 flags := commonGlobalCflags
Stephen Hines66c8b442019-06-10 17:40:12 -0700372
373 // http://b/131390872
374 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000375 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700376 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000377 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700378 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
379 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800380 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
381 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800382 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000383 // Default to zero initialization.
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000384 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700385 }
Yi Kong62e75f52021-08-18 15:38:20 +0800386
387 // Workaround for ccache with clang.
388 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
389 if ctx.Config().IsEnvTrue("USE_CCACHE") {
390 flags = append(flags, "-Wno-unused-command-line-argument")
391 }
Yi Kongb79fc582022-07-13 14:50:33 +0800392
Yi Kongf7f69e42022-07-21 15:49:05 +0800393 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
394 flags = append(flags, "-Wno-error=unknown-warning-option")
395 }
396
Fabián Cañasbc105442023-06-30 17:53:06 +0000397 switch ctx.Config().Getenv("CLANG_DEFAULT_DEBUG_LEVEL") {
398 case "debug_level_0":
399 flags = append(flags, "-g0")
400 case "debug_level_1":
401 flags = append(flags, "-g1")
402 case "debug_level_2":
403 flags = append(flags, "-g2")
404 case "debug_level_3":
405 flags = append(flags, "-g3")
406 case "debug_level_g":
407 flags = append(flags, "-g")
408 default:
409 flags = append(flags, "-g")
410 }
411
Stephen Hines66c8b442019-06-10 17:40:12 -0700412 return strings.Join(flags, " ")
413 })
414
Colin Cross0523ba22021-07-14 18:45:05 -0700415 // Export the static default DeviceGlobalCflags to Bazel.
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000416 // TODO(187086342): handle cflags that are set in VariableFuncs.
Sam Delmerico7f889562022-03-25 14:55:40 +0000417 exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000418
Colin Cross0523ba22021-07-14 18:45:05 -0700419 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700420 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800421 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700422
zijunzhao14e68a22023-01-25 22:41:43 +0000423 // Export the static default NoOverrideGlobalCflags to Bazel.
Yi Kong60a20102023-01-13 04:40:18 +0900424 exportedVars.ExportStringList("NoOverrideGlobalCflags", noOverrideGlobalCflags)
425 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string {
426 flags := noOverrideGlobalCflags
427 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
428 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...)
zijunzhao933e3802023-01-12 07:26:20 +0000429 }
430 return strings.Join(flags, " ")
431 })
432
zijunzhao14e68a22023-01-25 22:41:43 +0000433 exportedVars.ExportStringListStaticVariable("NoOverride64GlobalCflags", noOverride64GlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000434 exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000435 exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
436 exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
437 exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700438
Liz Kammera5a29de2022-05-25 23:19:37 -0400439 exportedVars.ExportString("CStdVersion", CStdVersion)
440 exportedVars.ExportString("CppStdVersion", CppStdVersion)
441 exportedVars.ExportString("ExperimentalCStdVersion", ExperimentalCStdVersion)
442 exportedVars.ExportString("ExperimentalCppStdVersion", ExperimentalCppStdVersion)
443
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000444 exportedVars.ExportString("VersionScriptFlagPrefix", VersionScriptFlagPrefix)
445
Trevor Radcliffea772d652023-04-11 13:41:17 +0000446 exportedVars.ExportString("VisibilityHiddenFlag", VisibilityHiddenFlag)
447 exportedVars.ExportString("VisibilityDefaultFlag", VisibilityDefaultFlag)
448
Colin Cross1cfd89a2016-09-15 09:30:46 -0700449 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700450 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000451 commonGlobalIncludes := []string{
452 "system/core/include",
453 "system/logging/liblog/include",
454 "system/media/audio/include",
455 "hardware/libhardware/include",
456 "hardware/libhardware_legacy/include",
457 "hardware/ril/include",
458 "frameworks/native/include",
459 "frameworks/native/opengl/include",
460 "frameworks/av/include",
461 }
Sam Delmerico7f889562022-03-25 14:55:40 +0000462 exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000463 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700464
Sam Delmerico3eda0192023-04-14 18:07:25 +0000465 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
466 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
467
Dan Willemsen9fe14102021-07-13 21:52:04 -0700468 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000469 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700470 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
471 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
472
Sam Delmerico3eda0192023-04-14 18:07:25 +0000473 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Yi Kongbd188812023-02-08 19:51:59 +0900474 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700475
Sam Delmerico036afab2023-05-04 16:43:36 -0400476 exportedVars.ExportStringListStaticVariable("WarningAllowedProjects", WarningAllowedProjects)
477
Jayant Chowdharye622d202017-02-01 19:19:52 -0800478 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
479 // being used for the rest of the build process.
480 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
481 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
482 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
483 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
484 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
485
Alixe2667872023-04-24 14:57:32 +0000486 rsGlobalIncludes := []string{
487 "external/clang/lib/Headers",
488 "frameworks/rs/script_api/include",
489 }
490 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", rsGlobalIncludes)
491 exportedVars.ExportStringList("RsGlobalIncludes", rsGlobalIncludes)
Colin Cross2a252be2017-05-01 17:37:24 -0700492
Dan Willemsen54daaf02018-03-12 13:24:09 -0700493 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
494 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
495 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700496 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700497 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700498 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400499
Colin Cross77cdcfd2021-03-12 11:28:25 -0800500 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
501 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
502 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
503 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
504 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
505 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
506 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700507}
508
Sam Delmerico7f889562022-03-25 14:55:40 +0000509var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700510
511func ClangPath(ctx android.PathContext, file string) android.SourcePath {
512 type clangToolKey string
513
514 key := android.NewCustomOnceKey(clangToolKey(file))
515
516 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
517 return clangPath(ctx).Join(ctx, file)
518 })
519}
520
521var clangPathKey = android.NewOnceKey("clangPath")
522
523func clangPath(ctx android.PathContext) android.SourcePath {
524 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
525 clangBase := ClangDefaultBase
526 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
527 clangBase = override
528 }
529 clangVersion := ClangDefaultVersion
530 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
531 clangVersion = override
532 }
533 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
534 })
535}