blob: 62b008b0678a25c8f971589d1ee13d2d9841e635 [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{
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000032 // Enable some optimization by default.
33 "-O2",
34
35 // Warnings enabled by default. Reference:
36 // https://clang.llvm.org/docs/DiagnosticsReference.html
Colin Cross4d9c2d12016-07-29 12:48:20 -070037 "-Wall",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000038 "-Wextra",
Colin Cross4d9c2d12016-07-29 12:48:20 -070039 "-Winit-self",
40 "-Wpointer-arith",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000041 "-Wunguarded-availability",
Colin Cross4d9c2d12016-07-29 12:48:20 -070042
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000043 // Warnings treated as errors by default.
44 // See also noOverrideGlobalCflags for errors that cannot be disabled
45 // from Android.bp files.
Colin Cross7278afc2017-11-02 22:38:32 -070046
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000047 // Using __DATE__/__TIME__ causes build nondeterminism.
Dan Willemsen5d980c82019-08-27 19:37:10 -070048 "-Werror=date-time",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000049 // Detects forgotten */& that usually cause a crash
50 "-Werror=int-conversion",
51 // Detects unterminated alignment modification pragmas, which often lead
52 // to ABI mismatch between modules and hard-to-debug crashes.
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080053 "-Werror=pragma-pack",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000054 // Same as above, but detects alignment pragmas around a header
55 // inclusion.
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080056 "-Werror=pragma-pack-suspicious-include",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000057 // Detects dividing an array size by itself, which is a common typo that
58 // leads to bugs.
59 "-Werror=sizeof-array-div",
60 // Detects a typo that cuts off a prefix from a string literal.
Christopher Di Bella23a991c2020-11-11 22:41:32 +000061 "-Werror=string-plus-int",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000062 // Detects for loops that will never execute more than once (for example
63 // due to unconditional break), but have a non-empty loop increment
64 // clause. Often a mistake/bug.
George Burgess IVfb81db22020-02-22 19:28:56 -080065 "-Werror=unreachable-code-loop-increment",
Colin Cross9cc59c12021-07-14 17:59:54 -070066
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000067 // Warnings that should not be errors even for modules with -Werror.
Krzysztof Kosiński6934b0e2022-08-07 06:56:54 +000068
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000069 // Making deprecated usages an error causes extreme pain when trying to
70 // deprecate anything.
71 "-Wno-error=deprecated-declarations",
72 // This rarely indicates a bug. http://b/145210666
73 "-Wno-error=reorder-init-list",
74
75 // Warnings disabled by default.
76
77 // Designated initializer syntax is recommended by the Google C++ style
78 // and is OK to use even if not formally supported by the chosen C++
79 // version.
80 "-Wno-c99-designator",
81 // Detects uses of a GNU C extension equivalent to a limited form of
82 // constexpr. Enabling this would require replacing many constants with
83 // macros, which is not a good trade-off.
84 "-Wno-gnu-folding-constant",
85 // AIDL generated code redeclares pure virtual methods in each
86 // subsequent version of an interface, so this warning is currently
87 // infeasible to enable.
88 "-Wno-inconsistent-missing-override",
89 // Incompatible with the Google C++ style guidance to use 'int' for loop
90 // indices; poor signal to noise ratio.
91 "-Wno-sign-compare",
92 // Poor signal to noise ratio.
93 "-Wno-unused",
94
95 // Global preprocessor constants.
96
97 "-DANDROID",
98 "-DNDEBUG",
99 "-UDEBUG",
Colin Cross9cc59c12021-07-14 17:59:54 -0700100 "-D__compiler_offsetof=__builtin_offsetof",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000101 // Allows the bionic versioning.h to indirectly determine whether the
102 // option -Wunguarded-availability is on or not.
103 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
104
105 // -f and -g options.
Colin Cross9cc59c12021-07-14 17:59:54 -0700106
107 // Emit address-significance table which allows linker to perform safe ICF. Clang does
108 // not emit the table by default on Android since NDK still uses GNU binutils.
109 "-faddrsig",
110
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000111 // Emit debugging data in a modern format (DWARF v5).
112 "-fdebug-default-version=5",
Colin Cross9cc59c12021-07-14 17:59:54 -0700113
Colin Cross9cc59c12021-07-14 17:59:54 -0700114 // Force clang to always output color diagnostics. Ninja will strip the ANSI
115 // color codes if it is not running in a terminal.
116 "-fcolor-diagnostics",
117
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800118 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
119 "-ffp-contract=off",
AdityaK423e4ce2023-05-26 12:08:05 -0700120
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000121 // Google C++ style does not allow exceptions, turn them off by default.
122 "-fno-exceptions",
123
124 // Disable optimizations based on strict aliasing by default.
125 // The performance benefit of enabling them currently does not outweigh
126 // the risk of hard-to-reproduce bugs.
127 "-fno-strict-aliasing",
128
129 // Disable line wrapping for error messages - it interferes with
130 // displaying logs in web browsers.
131 "-fmessage-length=0",
132
AdityaK423e4ce2023-05-26 12:08:05 -0700133 // Using simple template names reduces the size of debug builds.
134 "-gsimple-template-names",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000135
136 // Make paths in deps files relative.
137 "-no-canonical-prefixes",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 }
139
Colin Cross6f6a4282016-10-17 14:19:06 -0700140 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700141
Liz Kammere4d1bda2022-06-22 21:02:08 +0000142 commonGlobalAsflags = []string{
143 "-D__ASSEMBLY__",
144 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to
145 // end up in the dwarf data for crtend_so.S.
146 "-fdebug-default-version=4",
147 }
148
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000149 // Compilation flags for device code; not applied to host code.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700151 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800152 "-fdata-sections",
153 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700154 "-funwind-tables",
155 "-fstack-protector-strong",
156 "-Wa,--noexecstack",
157 "-D_FORTIFY_SOURCE=2",
158
159 "-Wstrict-aliasing=2",
160
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 "-Werror=return-type",
162 "-Werror=non-virtual-dtor",
163 "-Werror=address",
164 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700165 "-Werror=format-security",
Colin Crossc8bed312021-07-14 17:56:21 -0700166 "-nostdlibinc",
Yi Kong196b9262021-12-07 13:51:38 +0800167
Yi Kong67a2dee2023-09-01 21:52:36 +0900168 // Enable MLGO for register allocation.
169 "-mllvm -regalloc-enable-advisor=release",
170
Yi Kong196b9262021-12-07 13:51:38 +0800171 // Emit additional debug info for AutoFDO
172 "-fdebug-info-for-profiling",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 }
174
Yi Konga9e1df12022-10-20 14:45:52 +0900175 commonGlobalLldflags = []string{
176 "-fuse-ld=lld",
177 "-Wl,--icf=safe",
Steven Moreland8fe37e42023-07-22 00:14:55 +0000178 "-Wl,--no-demangle",
Yi Konga9e1df12022-10-20 14:45:52 +0900179 }
180
Colin Cross26f14502017-11-06 13:59:48 -0800181 deviceGlobalCppflags = []string{
182 "-fvisibility-inlines-hidden",
183 }
184
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000185 // Linking flags for device code; not applied to host binaries.
Colin Cross324a4572017-11-02 23:09:41 -0700186 deviceGlobalLdflags = []string{
187 "-Wl,-z,noexecstack",
188 "-Wl,-z,relro",
189 "-Wl,-z,now",
190 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700191 "-Wl,--fatal-warnings",
192 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800193 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
194 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700195 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700196 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800197 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800198 "-Wl,--exclude-libs,libunwind.a",
Yi Kong67a2dee2023-09-01 21:52:36 +0900199 // Enable MLGO for register allocation.
200 "-Wl,-mllvm,-regalloc-enable-advisor=release",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 }
202
Matías Hernándezd0500f12023-10-20 13:07:14 +0000203 deviceGlobalLldflags = append(deviceGlobalLdflags, commonGlobalLldflags...)
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700204
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 hostGlobalCflags = []string{}
206
Colin Cross26f14502017-11-06 13:59:48 -0800207 hostGlobalCppflags = []string{}
208
Colin Cross324a4572017-11-02 23:09:41 -0700209 hostGlobalLdflags = []string{}
210
Yi Konga9e1df12022-10-20 14:45:52 +0900211 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700212
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 commonGlobalCppflags = []string{
Colin Crossc8bed312021-07-14 17:56:21 -0700214 // -Wimplicit-fallthrough is not enabled by -Wall.
215 "-Wimplicit-fallthrough",
216
217 // Enable clang's thread-safety annotations in libcxx.
218 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
219
220 // libc++'s math.h has an #include_next outside of system_headers.
221 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
223
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000224 // These flags are appended after the module's cflags, so they cannot be
225 // overridden from Android.bp files.
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000226 //
227 // NOTE: if you need to disable a warning to unblock a compiler upgrade
228 // and it is only triggered by third party code, add it to
229 // extraExternalCflags (if possible) or noOverrideExternalGlobalCflags
230 // (if the former doesn't work). If the new warning also occurs in first
231 // party code, try adding it to commonGlobalCflags first. Adding it here
232 // should be the last resort, because it prevents all code in Android from
233 // opting into the warning.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000235 "-Werror=bool-operation",
Zijun Zhaod1569082023-03-08 23:48:45 +0000236 "-Werror=format-insufficient-args",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000237 "-Werror=implicit-int-float-conversion",
238 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 "-Werror=int-to-pointer-cast",
240 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000241 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700242 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
243 "-Wno-void-pointer-to-enum-cast",
244 // http://b/161386391 for -Wno-void-pointer-to-int-cast
245 "-Wno-void-pointer-to-int-cast",
246 // http://b/161386391 for -Wno-pointer-to-int-cast
247 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700248 "-Werror=fortify-source",
Colin Crossc8bed312021-07-14 17:56:21 -0700249
250 "-Werror=address-of-temporary",
zijunzhao2863c0a2023-02-06 21:28:30 +0000251 "-Werror=null-dereference",
Colin Crossc8bed312021-07-14 17:56:21 -0700252 "-Werror=return-type",
253
254 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
255 // new warnings are fixed.
256 "-Wno-tautological-constant-compare",
257 "-Wno-tautological-type-limit-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700258 // http://b/145211066
259 "-Wno-implicit-int-float-conversion",
260 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700261 "-Wno-tautological-overlap-compare", // http://b/148815696
262 // New warnings to be fixed after clang-r383902.
263 "-Wno-deprecated-copy", // http://b/153746672
264 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700265 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
266 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Colin Crossc8bed312021-07-14 17:56:21 -0700267 "-Wno-pessimizing-move", // http://b/154270751
268 // New warnings to be fixed after clang-r399163
269 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000270 // New warnings to be fixed after clang-r428724
271 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800272 // New warnings to be fixed after clang-r433403
273 "-Wno-error=unused-but-set-variable", // http://b/197240255
274 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000275 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000276 "-Wno-error=deprecated-builtins", // http://b/241601211
277 "-Wno-error=deprecated", // in external/googletest/googletest
Yabin Cui29f248b2022-11-30 13:32:10 -0800278 // New warnings to be fixed after clang-r475365
279 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903
Yabin Cui29f248b2022-11-30 13:32:10 -0800280 "-Wno-error=enum-constexpr-conversion", // http://b/243964282
Colin Crossc8bed312021-07-14 17:56:21 -0700281 }
282
zijunzhao933e3802023-01-12 07:26:20 +0000283 noOverride64GlobalCflags = []string{}
284
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000285 // Extra cflags applied to third-party code (anything for which
286 // IsThirdPartyPath() in build/soong/android/paths.go returns true;
287 // includes external/, most of vendor/ and most of hardware/)
Colin Crossc8bed312021-07-14 17:56:21 -0700288 extraExternalCflags = []string{
289 "-Wno-enum-compare",
290 "-Wno-enum-compare-switch",
291
292 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
293 // this new warning are fixed.
294 "-Wno-null-pointer-arithmetic",
295
296 // Bug: http://b/29823425 Disable -Wnull-dereference until the
297 // new instances detected by this warning are fixed.
298 "-Wno-null-dereference",
299
300 // http://b/145211477
301 "-Wno-pointer-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700302 "-Wno-final-dtor-non-final-class",
303
304 // http://b/165945989
305 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800306
307 // http://b/199369603
308 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800309
310 // http://b/175068488
311 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800312
313 // http://b/239661264
314 "-Wno-deprecated-non-prototype",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 }
316
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000317 // Similar to noOverrideGlobalCflags, but applies only to third-party code
318 // (see extraExternalCflags).
319 // This section can unblock compiler upgrades when a third party module that
320 // enables -Werror and some group of warnings explicitly triggers newly
321 // added warnings.
322 noOverrideExternalGlobalCflags = []string{
323 // http://b/151457797
324 "-fcommon",
325 // http://b/191699019
326 "-Wno-format-insufficient-args",
Ramya Subramanian5e759752023-10-11 23:00:33 +0000327 // http://b/296321145
328 // Indicates potential memory or stack corruption, so should be changed
329 // to a hard error. Currently triggered by some vendor code.
330 "-Wno-incompatible-function-pointer-types",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000331 // http://b/296321508
332 // Introduced in response to a critical security vulnerability and
333 // should be a hard error - it requires only whitespace changes to fix.
334 "-Wno-misleading-indentation",
335 // Triggered by old LLVM code in external/llvm. Likely not worth
336 // enabling since it's a cosmetic issue.
337 "-Wno-bitwise-instead-of-logical",
338
339 "-Wno-unused-but-set-variable",
340 "-Wno-unused-but-set-parameter",
341 "-Wno-unqualified-std-cast-call",
342 "-Wno-array-parameter",
343 "-Wno-gnu-offsetof-extensions",
344 }
345
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700346 llvmNextExtraCommonGlobalCflags = []string{
Yi Konga8b0bfb2022-12-26 16:02:50 +0900347 // Do not report warnings when testing with the top of trunk LLVM.
348 "-Wno-error",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700349 }
Yi Kongb79fc582022-07-13 14:50:33 +0800350
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000351 // Flags that must not appear in any command line.
Colin Crossb98c8b02016-07-29 13:44:28 -0700352 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 "-w",
354 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700355
Elliott Hughesfb294e32023-06-14 10:42:45 -0700356 CStdVersion = "gnu17"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000357 CppStdVersion = "gnu++17"
Elliott Hughesfb294e32023-06-14 10:42:45 -0700358 ExperimentalCStdVersion = "gnu2x"
Elliott Hughes37976122018-11-28 14:16:39 -0800359 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700360
Leo Li8756b372017-05-22 16:11:34 -0700361 // prebuilts/clang default settings.
362 ClangDefaultBase = "prebuilts/clang/host"
Yi Konged7c73e2023-08-25 15:18:48 +0900363 ClangDefaultVersion = "clang-r498229b"
Yi Kongfd07ed22023-02-16 17:42:27 +0900364 ClangDefaultShortVersion = "17"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800365
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800366 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800367 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800368 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800369 "vendor/",
370 }
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000371
372 VersionScriptFlagPrefix = "-Wl,--version-script,"
Trevor Radcliffea772d652023-04-11 13:41:17 +0000373
374 VisibilityHiddenFlag = "-fvisibility=hidden"
375 VisibilityDefaultFlag = "-fvisibility=default"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376)
377
Sam Delmerico7f889562022-03-25 14:55:40 +0000378// BazelCcToolchainVars generates bzl file content containing variables for
379// Bazel's cc_toolchain configuration.
380func BazelCcToolchainVars(config android.Config) string {
381 return android.BazelToolchainVars(config, exportedVars)
382}
383
384func ExportStringList(name string, value []string) {
385 exportedVars.ExportStringList(name, value)
386}
Colin Crossb98c8b02016-07-29 13:44:28 -0700387
Colin Cross4d9c2d12016-07-29 12:48:20 -0700388func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700389 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000390 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
391 }
392
Sam Delmerico7f889562022-03-25 14:55:40 +0000393 exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
Liz Kammere4d1bda2022-06-22 21:02:08 +0000394 exportedVars.ExportStringListStaticVariable("CommonGlobalAsflags", commonGlobalAsflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000395 exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
396 exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
397 exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
398 exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
399 exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
400 exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000401
Colin Cross0523ba22021-07-14 18:45:05 -0700402 // Export the static default CommonGlobalCflags to Bazel.
Yu Liu19ea53d2022-12-21 12:39:30 -0800403 exportedVars.ExportStringList("CommonGlobalCflags", commonGlobalCflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700404
Colin Cross0523ba22021-07-14 18:45:05 -0700405 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Cross33bac242021-07-14 17:03:16 -0700406 flags := commonGlobalCflags
Stephen Hines66c8b442019-06-10 17:40:12 -0700407
408 // http://b/131390872
409 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000410 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700411 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000412 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700413 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
414 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800415 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
416 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800417 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000418 // Default to zero initialization.
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000419 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700420 }
Yi Kong62e75f52021-08-18 15:38:20 +0800421
422 // Workaround for ccache with clang.
423 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
424 if ctx.Config().IsEnvTrue("USE_CCACHE") {
425 flags = append(flags, "-Wno-unused-command-line-argument")
426 }
Yi Kongb79fc582022-07-13 14:50:33 +0800427
Yi Kongf7f69e42022-07-21 15:49:05 +0800428 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
429 flags = append(flags, "-Wno-error=unknown-warning-option")
430 }
431
Fabián Cañasbc105442023-06-30 17:53:06 +0000432 switch ctx.Config().Getenv("CLANG_DEFAULT_DEBUG_LEVEL") {
433 case "debug_level_0":
434 flags = append(flags, "-g0")
435 case "debug_level_1":
436 flags = append(flags, "-g1")
437 case "debug_level_2":
438 flags = append(flags, "-g2")
439 case "debug_level_3":
440 flags = append(flags, "-g3")
441 case "debug_level_g":
442 flags = append(flags, "-g")
443 default:
444 flags = append(flags, "-g")
445 }
446
Stephen Hines66c8b442019-06-10 17:40:12 -0700447 return strings.Join(flags, " ")
448 })
449
Colin Cross0523ba22021-07-14 18:45:05 -0700450 // Export the static default DeviceGlobalCflags to Bazel.
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000451 // TODO(187086342): handle cflags that are set in VariableFuncs.
Sam Delmerico7f889562022-03-25 14:55:40 +0000452 exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000453
Colin Cross0523ba22021-07-14 18:45:05 -0700454 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700455 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800456 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700457
zijunzhao14e68a22023-01-25 22:41:43 +0000458 // Export the static default NoOverrideGlobalCflags to Bazel.
Yi Kong60a20102023-01-13 04:40:18 +0900459 exportedVars.ExportStringList("NoOverrideGlobalCflags", noOverrideGlobalCflags)
460 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string {
461 flags := noOverrideGlobalCflags
462 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
463 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...)
zijunzhao933e3802023-01-12 07:26:20 +0000464 }
465 return strings.Join(flags, " ")
466 })
467
zijunzhao14e68a22023-01-25 22:41:43 +0000468 exportedVars.ExportStringListStaticVariable("NoOverride64GlobalCflags", noOverride64GlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000469 exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000470 exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
471 exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
472 exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700473
Liz Kammera5a29de2022-05-25 23:19:37 -0400474 exportedVars.ExportString("CStdVersion", CStdVersion)
475 exportedVars.ExportString("CppStdVersion", CppStdVersion)
476 exportedVars.ExportString("ExperimentalCStdVersion", ExperimentalCStdVersion)
477 exportedVars.ExportString("ExperimentalCppStdVersion", ExperimentalCppStdVersion)
478
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000479 exportedVars.ExportString("VersionScriptFlagPrefix", VersionScriptFlagPrefix)
480
Trevor Radcliffea772d652023-04-11 13:41:17 +0000481 exportedVars.ExportString("VisibilityHiddenFlag", VisibilityHiddenFlag)
482 exportedVars.ExportString("VisibilityDefaultFlag", VisibilityDefaultFlag)
483
Colin Cross1cfd89a2016-09-15 09:30:46 -0700484 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700485 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000486 commonGlobalIncludes := []string{
487 "system/core/include",
488 "system/logging/liblog/include",
489 "system/media/audio/include",
490 "hardware/libhardware/include",
491 "hardware/libhardware_legacy/include",
492 "hardware/ril/include",
493 "frameworks/native/include",
494 "frameworks/native/opengl/include",
495 "frameworks/av/include",
496 }
Sam Delmerico7f889562022-03-25 14:55:40 +0000497 exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000498 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499
Sam Delmerico3eda0192023-04-14 18:07:25 +0000500 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
501 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
502
Dan Willemsen9fe14102021-07-13 21:52:04 -0700503 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000504 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700505 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
506 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
507
Sam Delmerico3eda0192023-04-14 18:07:25 +0000508 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Yi Kongbd188812023-02-08 19:51:59 +0900509 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700510
Sam Delmerico036afab2023-05-04 16:43:36 -0400511 exportedVars.ExportStringListStaticVariable("WarningAllowedProjects", WarningAllowedProjects)
512
Jayant Chowdharye622d202017-02-01 19:19:52 -0800513 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
514 // being used for the rest of the build process.
515 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
516 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
517 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
518 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
519 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
520
Alixe2667872023-04-24 14:57:32 +0000521 rsGlobalIncludes := []string{
522 "external/clang/lib/Headers",
523 "frameworks/rs/script_api/include",
524 }
525 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", rsGlobalIncludes)
526 exportedVars.ExportStringList("RsGlobalIncludes", rsGlobalIncludes)
Colin Cross2a252be2017-05-01 17:37:24 -0700527
Dan Willemsen54daaf02018-03-12 13:24:09 -0700528 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
529 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
530 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700531 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700532 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700533 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400534
Colin Cross77cdcfd2021-03-12 11:28:25 -0800535 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
536 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
537 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
538 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
539 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
540 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
541 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700542}
543
Sam Delmerico7f889562022-03-25 14:55:40 +0000544var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700545
546func ClangPath(ctx android.PathContext, file string) android.SourcePath {
547 type clangToolKey string
548
549 key := android.NewCustomOnceKey(clangToolKey(file))
550
551 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
552 return clangPath(ctx).Join(ctx, file)
553 })
554}
555
556var clangPathKey = android.NewOnceKey("clangPath")
557
558func clangPath(ctx android.PathContext) android.SourcePath {
559 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
560 clangBase := ClangDefaultBase
561 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
562 clangBase = override
563 }
564 clangVersion := ClangDefaultVersion
565 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
566 clangVersion = override
567 }
568 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
569 })
570}