blob: b19682d2ad2b94cdb0bafcff49b7e70897b5a138 [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 Crossbcf53702024-01-17 11:11:03 -080019 "slices"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "strings"
21
22 "android/soong/android"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040023 "android/soong/remoteexec"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024)
25
Colin Cross4d9c2d12016-07-29 12:48:20 -070026var (
Cole Faust8982b1c2024-04-08 16:54:45 -070027 pctx = android.NewPackageContext("android/soong/cc/config")
Sam Delmerico7f889562022-03-25 14:55:40 +000028
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",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000072
73 // Warnings disabled by default.
74
Elliott Hughesafd8aac2024-10-31 21:42:40 +000075 // We should encourage use of C23 features even when the whole project
76 // isn't C23-ready.
77 "-Wno-c23-extensions",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000078 // Designated initializer syntax is recommended by the Google C++ style
79 // and is OK to use even if not formally supported by the chosen C++
80 // version.
81 "-Wno-c99-designator",
82 // Detects uses of a GNU C extension equivalent to a limited form of
83 // constexpr. Enabling this would require replacing many constants with
84 // macros, which is not a good trade-off.
85 "-Wno-gnu-folding-constant",
86 // AIDL generated code redeclares pure virtual methods in each
87 // subsequent version of an interface, so this warning is currently
88 // infeasible to enable.
89 "-Wno-inconsistent-missing-override",
Krzysztof Kosiński8f8cc162023-10-20 01:02:58 +000090 // Detects designated initializers that are in a different order than
91 // the fields in the initialized type, which causes the side effects
92 // of initializers to occur out of order with the source code.
93 // In practice, this warning has extremely poor signal to noise ratio,
94 // because it is triggered even for initializers with no side effects.
95 // Individual modules can still opt into it via cflags.
96 "-Wno-error=reorder-init-list",
97 "-Wno-reorder-init-list",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000098 // Incompatible with the Google C++ style guidance to use 'int' for loop
99 // indices; poor signal to noise ratio.
100 "-Wno-sign-compare",
101 // Poor signal to noise ratio.
102 "-Wno-unused",
103
104 // Global preprocessor constants.
105
106 "-DANDROID",
107 "-DNDEBUG",
108 "-UDEBUG",
Colin Cross9cc59c12021-07-14 17:59:54 -0700109 "-D__compiler_offsetof=__builtin_offsetof",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000110 // Allows the bionic versioning.h to indirectly determine whether the
111 // option -Wunguarded-availability is on or not.
112 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
113
114 // -f and -g options.
Colin Cross9cc59c12021-07-14 17:59:54 -0700115
116 // Emit address-significance table which allows linker to perform safe ICF. Clang does
117 // not emit the table by default on Android since NDK still uses GNU binutils.
118 "-faddrsig",
119
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000120 // Emit debugging data in a modern format (DWARF v5).
121 "-fdebug-default-version=5",
Colin Cross9cc59c12021-07-14 17:59:54 -0700122
Colin Cross9cc59c12021-07-14 17:59:54 -0700123 // Force clang to always output color diagnostics. Ninja will strip the ANSI
124 // color codes if it is not running in a terminal.
125 "-fcolor-diagnostics",
126
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800127 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
128 "-ffp-contract=off",
AdityaK423e4ce2023-05-26 12:08:05 -0700129
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000130 // Google C++ style does not allow exceptions, turn them off by default.
131 "-fno-exceptions",
132
133 // Disable optimizations based on strict aliasing by default.
134 // The performance benefit of enabling them currently does not outweigh
135 // the risk of hard-to-reproduce bugs.
136 "-fno-strict-aliasing",
137
138 // Disable line wrapping for error messages - it interferes with
139 // displaying logs in web browsers.
140 "-fmessage-length=0",
141
AdityaK423e4ce2023-05-26 12:08:05 -0700142 // Using simple template names reduces the size of debug builds.
143 "-gsimple-template-names",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000144
Eric Rahm5571ab82023-10-20 15:56:17 +0000145 // Use zstd to compress debug data.
146 "-gz=zstd",
147
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000148 // Make paths in deps files relative.
149 "-no-canonical-prefixes",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 }
151
Colin Cross6f6a4282016-10-17 14:19:06 -0700152 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700153
Liz Kammere4d1bda2022-06-22 21:02:08 +0000154 commonGlobalAsflags = []string{
155 "-D__ASSEMBLY__",
156 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to
157 // end up in the dwarf data for crtend_so.S.
158 "-fdebug-default-version=4",
159 }
160
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000161 // Compilation flags for device code; not applied to host code.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700163 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800164 "-fdata-sections",
165 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700166 "-funwind-tables",
167 "-fstack-protector-strong",
168 "-Wa,--noexecstack",
169 "-D_FORTIFY_SOURCE=2",
170
171 "-Wstrict-aliasing=2",
172
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 "-Werror=return-type",
174 "-Werror=non-virtual-dtor",
175 "-Werror=address",
176 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700177 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 }
179
Yi Konga9e1df12022-10-20 14:45:52 +0900180 commonGlobalLldflags = []string{
181 "-fuse-ld=lld",
182 "-Wl,--icf=safe",
Steven Moreland8fe37e42023-07-22 00:14:55 +0000183 "-Wl,--no-demangle",
Yi Konga9e1df12022-10-20 14:45:52 +0900184 }
185
Colin Cross26f14502017-11-06 13:59:48 -0800186 deviceGlobalCppflags = []string{
187 "-fvisibility-inlines-hidden",
188 }
189
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000190 // Linking flags for device code; not applied to host binaries.
Colin Cross324a4572017-11-02 23:09:41 -0700191 deviceGlobalLdflags = []string{
192 "-Wl,-z,noexecstack",
193 "-Wl,-z,relro",
194 "-Wl,-z,now",
195 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700196 "-Wl,--fatal-warnings",
197 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800198 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
199 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700200 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700201 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800202 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800203 "-Wl,--exclude-libs,libunwind.a",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 }
205
Eric Rahm19524712023-10-20 15:56:27 +0000206 deviceGlobalLldflags = append(append(deviceGlobalLdflags, commonGlobalLldflags...),
207 "-Wl,--compress-debug-sections=zstd",
208 )
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700209
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 hostGlobalCflags = []string{}
211
Colin Cross26f14502017-11-06 13:59:48 -0800212 hostGlobalCppflags = []string{}
213
Colin Cross324a4572017-11-02 23:09:41 -0700214 hostGlobalLdflags = []string{}
215
Yi Konga9e1df12022-10-20 14:45:52 +0900216 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700217
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 commonGlobalCppflags = []string{
Colin Crossc8bed312021-07-14 17:56:21 -0700219 // -Wimplicit-fallthrough is not enabled by -Wall.
220 "-Wimplicit-fallthrough",
221
222 // Enable clang's thread-safety annotations in libcxx.
223 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
224
225 // libc++'s math.h has an #include_next outside of system_headers.
226 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 }
228
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000229 // These flags are appended after the module's cflags, so they cannot be
230 // overridden from Android.bp files.
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000231 //
232 // NOTE: if you need to disable a warning to unblock a compiler upgrade
233 // and it is only triggered by third party code, add it to
234 // extraExternalCflags (if possible) or noOverrideExternalGlobalCflags
235 // (if the former doesn't work). If the new warning also occurs in first
236 // party code, try adding it to commonGlobalCflags first. Adding it here
237 // should be the last resort, because it prevents all code in Android from
238 // opting into the warning.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000240 "-Werror=bool-operation",
Zijun74368342024-11-26 07:58:35 +0000241 "-Werror=dangling",
Zijun Zhaod1569082023-03-08 23:48:45 +0000242 "-Werror=format-insufficient-args",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000243 "-Werror=implicit-int-float-conversion",
244 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 "-Werror=int-to-pointer-cast",
246 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000247 "-Werror=xor-used-as-pow",
Zijunde4f1bf2024-11-27 19:52:37 +0000248 "-Wimplicit-int-float-conversion",
Stephen Hines2210e722020-07-15 11:11:57 -0700249 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
250 "-Wno-void-pointer-to-enum-cast",
251 // http://b/161386391 for -Wno-void-pointer-to-int-cast
252 "-Wno-void-pointer-to-int-cast",
253 // http://b/161386391 for -Wno-pointer-to-int-cast
254 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700255 "-Werror=fortify-source",
Aditya Kumaref7c1212024-02-02 17:22:03 +0000256 // http://b/315246135 temporarily disabled
AdityaK94688b52024-02-08 13:50:18 -0800257 "-Wno-unused-variable",
Aditya Kumaref7c1212024-02-02 17:22:03 +0000258 // Disabled because it produces many false positives. http://b/323050926
259 "-Wno-missing-field-initializers",
260 // http://b/323050889
261 "-Wno-packed-non-pod",
Colin Crossc8bed312021-07-14 17:56:21 -0700262
263 "-Werror=address-of-temporary",
Krzysztof Kosiński83199b52023-10-26 07:16:29 +0000264 "-Werror=incompatible-function-pointer-types",
zijunzhao2863c0a2023-02-06 21:28:30 +0000265 "-Werror=null-dereference",
Colin Crossc8bed312021-07-14 17:56:21 -0700266 "-Werror=return-type",
267
268 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
269 // new warnings are fixed.
270 "-Wno-tautological-constant-compare",
271 "-Wno-tautological-type-limit-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700272 // http://b/145211066
273 "-Wno-implicit-int-float-conversion",
274 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700275 "-Wno-tautological-overlap-compare", // http://b/148815696
276 // New warnings to be fixed after clang-r383902.
277 "-Wno-deprecated-copy", // http://b/153746672
278 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700279 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
280 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800281 "-Wno-deprecated-enum-enum-conversion",
Yi Kongfd50d172024-08-12 14:41:09 +0800282 "-Wno-error=pessimizing-move", // http://b/154270751
Colin Crossc8bed312021-07-14 17:56:21 -0700283 // New warnings to be fixed after clang-r399163
284 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000285 // New warnings to be fixed after clang-r428724
286 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800287 // New warnings to be fixed after clang-r433403
288 "-Wno-error=unused-but-set-variable", // http://b/197240255
289 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000290 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000291 "-Wno-error=deprecated-builtins", // http://b/241601211
292 "-Wno-error=deprecated", // in external/googletest/googletest
Aditya Kumar2c6b4ac2024-09-12 18:40:17 +0000293 // Disabling until the warning is fixed in libc++abi header files b/366180429
294 "-Wno-deprecated-dynamic-exception-spec",
Chris Wailesffebc5b2024-06-10 18:06:52 +0000295 // New warnings to be fixed after clang-r522817
296 "-Wno-error=invalid-offsetof",
297 "-Wno-error=thread-safety-reference-return",
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800298
Chris Wailesffebc5b2024-06-10 18:06:52 +0000299 // Allow using VLA CXX extension.
300 "-Wno-vla-cxx-extension",
Aditya Kumar06540162024-09-05 10:36:49 -0700301 "-Wno-cast-function-type-mismatch",
Colin Crossc8bed312021-07-14 17:56:21 -0700302 }
303
zijunzhao933e3802023-01-12 07:26:20 +0000304 noOverride64GlobalCflags = []string{}
305
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000306 // Extra cflags applied to third-party code (anything for which
307 // IsThirdPartyPath() in build/soong/android/paths.go returns true;
308 // includes external/, most of vendor/ and most of hardware/)
Colin Crossc8bed312021-07-14 17:56:21 -0700309 extraExternalCflags = []string{
310 "-Wno-enum-compare",
311 "-Wno-enum-compare-switch",
312
313 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
314 // this new warning are fixed.
315 "-Wno-null-pointer-arithmetic",
316
317 // Bug: http://b/29823425 Disable -Wnull-dereference until the
318 // new instances detected by this warning are fixed.
319 "-Wno-null-dereference",
320
321 // http://b/145211477
322 "-Wno-pointer-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700323 "-Wno-final-dtor-non-final-class",
324
325 // http://b/165945989
326 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800327
328 // http://b/199369603
329 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800330
331 // http://b/175068488
332 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800333
334 // http://b/239661264
335 "-Wno-deprecated-non-prototype",
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800336
337 "-Wno-unused",
338 "-Wno-deprecated",
Yi Kong2e2d7532024-08-09 14:56:58 +0900339
340 // http://b/315250603 temporarily disabled
341 "-Wno-error=format",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 }
343
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000344 // Similar to noOverrideGlobalCflags, but applies only to third-party code
345 // (see extraExternalCflags).
346 // This section can unblock compiler upgrades when a third party module that
347 // enables -Werror and some group of warnings explicitly triggers newly
348 // added warnings.
349 noOverrideExternalGlobalCflags = []string{
350 // http://b/151457797
351 "-fcommon",
352 // http://b/191699019
353 "-Wno-format-insufficient-args",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000354 // http://b/296321508
355 // Introduced in response to a critical security vulnerability and
356 // should be a hard error - it requires only whitespace changes to fix.
357 "-Wno-misleading-indentation",
358 // Triggered by old LLVM code in external/llvm. Likely not worth
359 // enabling since it's a cosmetic issue.
360 "-Wno-bitwise-instead-of-logical",
361
Prashanth Swaminathan13b06372024-01-22 19:41:52 -0800362 "-Wno-unused",
363 "-Wno-unused-parameter",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000364 "-Wno-unused-but-set-parameter",
365 "-Wno-unqualified-std-cast-call",
366 "-Wno-array-parameter",
367 "-Wno-gnu-offsetof-extensions",
Yi Kongfd50d172024-08-12 14:41:09 +0800368 "-Wno-pessimizing-move",
Priyanka Advani (xWF)435c4ed2024-09-05 18:24:53 +0000369 // TODO: Enable this warning http://b/315245071
370 "-Wno-fortify-source",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000371 }
372
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700373 llvmNextExtraCommonGlobalCflags = []string{
Yi Konga8b0bfb2022-12-26 16:02:50 +0900374 // Do not report warnings when testing with the top of trunk LLVM.
AdityaKe7b60672023-10-27 11:26:00 -0700375 "-Wno-everything",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700376 }
Yi Kongb79fc582022-07-13 14:50:33 +0800377
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000378 // Flags that must not appear in any command line.
Colin Crossb98c8b02016-07-29 13:44:28 -0700379 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700380 "-w",
381 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700382
Elliott Hughesfb294e32023-06-14 10:42:45 -0700383 CStdVersion = "gnu17"
Elliott Hughesc79d9e32022-01-13 14:56:02 -0800384 CppStdVersion = "gnu++20"
Elliott Hughesfb294e32023-06-14 10:42:45 -0700385 ExperimentalCStdVersion = "gnu2x"
Tomasz Wasilczyke1496162023-11-15 11:46:26 -0800386 ExperimentalCppStdVersion = "gnu++2b"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700387
Leo Li8756b372017-05-22 16:11:34 -0700388 // prebuilts/clang default settings.
389 ClangDefaultBase = "prebuilts/clang/host"
Aditya Kumar06540162024-09-05 10:36:49 -0700390 ClangDefaultVersion = "clang-r536225"
Yabin Cui8c69b782024-07-13 23:38:11 -0700391 ClangDefaultShortVersion = "19"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800392
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800393 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800394 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800395 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800396 "vendor/",
397 }
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000398
399 VersionScriptFlagPrefix = "-Wl,--version-script,"
Trevor Radcliffea772d652023-04-11 13:41:17 +0000400
401 VisibilityHiddenFlag = "-fvisibility=hidden"
402 VisibilityDefaultFlag = "-fvisibility=default"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700403)
404
405func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700406 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000407 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
408 }
409
Cole Faust8982b1c2024-04-08 16:54:45 -0700410 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
411 pctx.StaticVariable("CommonGlobalAsflags", strings.Join(commonGlobalAsflags, " "))
412 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
413 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
414 pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
415 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
416 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
417 pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700418
Colin Cross0523ba22021-07-14 18:45:05 -0700419 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossbcf53702024-01-17 11:11:03 -0800420 flags := slices.Clone(commonGlobalCflags)
Stephen Hines66c8b442019-06-10 17:40:12 -0700421
422 // http://b/131390872
423 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000424 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700425 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000426 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700427 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
428 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800429 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
430 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800431 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000432 // Default to zero initialization.
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000433 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700434 }
Yi Kong62e75f52021-08-18 15:38:20 +0800435
436 // Workaround for ccache with clang.
437 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
438 if ctx.Config().IsEnvTrue("USE_CCACHE") {
439 flags = append(flags, "-Wno-unused-command-line-argument")
440 }
Yi Kongb79fc582022-07-13 14:50:33 +0800441
Yi Kongf7f69e42022-07-21 15:49:05 +0800442 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
443 flags = append(flags, "-Wno-error=unknown-warning-option")
444 }
445
Fabián Cañasbc105442023-06-30 17:53:06 +0000446 switch ctx.Config().Getenv("CLANG_DEFAULT_DEBUG_LEVEL") {
447 case "debug_level_0":
448 flags = append(flags, "-g0")
449 case "debug_level_1":
450 flags = append(flags, "-g1")
451 case "debug_level_2":
452 flags = append(flags, "-g2")
453 case "debug_level_3":
454 flags = append(flags, "-g3")
455 case "debug_level_g":
456 flags = append(flags, "-g")
457 default:
458 flags = append(flags, "-g")
459 }
460
Stephen Hines66c8b442019-06-10 17:40:12 -0700461 return strings.Join(flags, " ")
462 })
463
Colin Cross0523ba22021-07-14 18:45:05 -0700464 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700465 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800466 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700467
Yi Kong60a20102023-01-13 04:40:18 +0900468 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string {
469 flags := noOverrideGlobalCflags
470 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
471 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...)
AdityaKe7b60672023-10-27 11:26:00 -0700472 IllegalFlags = []string{} // Don't fail build while testing a new compiler.
zijunzhao933e3802023-01-12 07:26:20 +0000473 }
474 return strings.Join(flags, " ")
475 })
476
Cole Faust8982b1c2024-04-08 16:54:45 -0700477 pctx.StaticVariable("NoOverride64GlobalCflags", strings.Join(noOverride64GlobalCflags, " "))
478 pctx.StaticVariable("HostGlobalCflags", strings.Join(hostGlobalCflags, " "))
479 pctx.StaticVariable("NoOverrideExternalGlobalCflags", strings.Join(noOverrideExternalGlobalCflags, " "))
480 pctx.StaticVariable("CommonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
481 pctx.StaticVariable("ExternalCflags", strings.Join(extraExternalCflags, " "))
Trevor Radcliffea772d652023-04-11 13:41:17 +0000482
Colin Cross1cfd89a2016-09-15 09:30:46 -0700483 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700484 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000485 commonGlobalIncludes := []string{
486 "system/core/include",
487 "system/logging/liblog/include",
488 "system/media/audio/include",
489 "hardware/libhardware/include",
490 "hardware/libhardware_legacy/include",
491 "hardware/ril/include",
492 "frameworks/native/include",
493 "frameworks/native/opengl/include",
494 "frameworks/av/include",
495 }
Jingwen Chen21999952021-05-19 05:53:51 +0000496 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700497
Cole Faust8982b1c2024-04-08 16:54:45 -0700498 pctx.StaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
499 pctx.StaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000500
Dan Willemsen9fe14102021-07-13 21:52:04 -0700501 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000502 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700503 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
504 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
505
Sam Delmerico3eda0192023-04-14 18:07:25 +0000506 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Yi Kongbd188812023-02-08 19:51:59 +0900507 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700508
Cole Faust8982b1c2024-04-08 16:54:45 -0700509 pctx.StaticVariable("WarningAllowedProjects", strings.Join(WarningAllowedProjects, " "))
Sam Delmerico036afab2023-05-04 16:43:36 -0400510
Jayant Chowdharye622d202017-02-01 19:19:52 -0800511 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
512 // being used for the rest of the build process.
513 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
514 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
515 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
516 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
517 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
518
Alixe2667872023-04-24 14:57:32 +0000519 rsGlobalIncludes := []string{
520 "external/clang/lib/Headers",
521 "frameworks/rs/script_api/include",
522 }
523 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", rsGlobalIncludes)
Colin Cross2a252be2017-05-01 17:37:24 -0700524
Dan Willemsen54daaf02018-03-12 13:24:09 -0700525 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
526 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
527 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700528 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700529 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700530 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400531
Colin Cross77cdcfd2021-03-12 11:28:25 -0800532 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
533 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
534 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
535 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
536 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
537 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
538 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539}
540
Cole Faust8982b1c2024-04-08 16:54:45 -0700541var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700542
543func ClangPath(ctx android.PathContext, file string) android.SourcePath {
544 type clangToolKey string
545
546 key := android.NewCustomOnceKey(clangToolKey(file))
547
548 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
549 return clangPath(ctx).Join(ctx, file)
550 })
551}
552
553var clangPathKey = android.NewOnceKey("clangPath")
554
555func clangPath(ctx android.PathContext) android.SourcePath {
556 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
557 clangBase := ClangDefaultBase
558 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
559 clangBase = override
560 }
561 clangVersion := ClangDefaultVersion
562 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
563 clangVersion = override
564 }
565 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
566 })
567}