blob: 62a4765f4526d28ad12a1ea753c90507ab6fb321 [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
75 // Designated initializer syntax is recommended by the Google C++ style
76 // and is OK to use even if not formally supported by the chosen C++
77 // version.
78 "-Wno-c99-designator",
79 // Detects uses of a GNU C extension equivalent to a limited form of
80 // constexpr. Enabling this would require replacing many constants with
81 // macros, which is not a good trade-off.
82 "-Wno-gnu-folding-constant",
83 // AIDL generated code redeclares pure virtual methods in each
84 // subsequent version of an interface, so this warning is currently
85 // infeasible to enable.
86 "-Wno-inconsistent-missing-override",
Krzysztof Kosiński8f8cc162023-10-20 01:02:58 +000087 // Detects designated initializers that are in a different order than
88 // the fields in the initialized type, which causes the side effects
89 // of initializers to occur out of order with the source code.
90 // In practice, this warning has extremely poor signal to noise ratio,
91 // because it is triggered even for initializers with no side effects.
92 // Individual modules can still opt into it via cflags.
93 "-Wno-error=reorder-init-list",
94 "-Wno-reorder-init-list",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000095 // Incompatible with the Google C++ style guidance to use 'int' for loop
96 // indices; poor signal to noise ratio.
97 "-Wno-sign-compare",
98 // Poor signal to noise ratio.
99 "-Wno-unused",
100
101 // Global preprocessor constants.
102
103 "-DANDROID",
104 "-DNDEBUG",
105 "-UDEBUG",
Colin Cross9cc59c12021-07-14 17:59:54 -0700106 "-D__compiler_offsetof=__builtin_offsetof",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000107 // Allows the bionic versioning.h to indirectly determine whether the
108 // option -Wunguarded-availability is on or not.
109 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
110
111 // -f and -g options.
Colin Cross9cc59c12021-07-14 17:59:54 -0700112
113 // Emit address-significance table which allows linker to perform safe ICF. Clang does
114 // not emit the table by default on Android since NDK still uses GNU binutils.
115 "-faddrsig",
116
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000117 // Emit debugging data in a modern format (DWARF v5).
118 "-fdebug-default-version=5",
Colin Cross9cc59c12021-07-14 17:59:54 -0700119
Colin Cross9cc59c12021-07-14 17:59:54 -0700120 // Force clang to always output color diagnostics. Ninja will strip the ANSI
121 // color codes if it is not running in a terminal.
122 "-fcolor-diagnostics",
123
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800124 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
125 "-ffp-contract=off",
AdityaK423e4ce2023-05-26 12:08:05 -0700126
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000127 // Google C++ style does not allow exceptions, turn them off by default.
128 "-fno-exceptions",
129
130 // Disable optimizations based on strict aliasing by default.
131 // The performance benefit of enabling them currently does not outweigh
132 // the risk of hard-to-reproduce bugs.
133 "-fno-strict-aliasing",
134
135 // Disable line wrapping for error messages - it interferes with
136 // displaying logs in web browsers.
137 "-fmessage-length=0",
138
Yi Kong9b9d29b2024-05-20 02:48:42 +0900139 // Disable C++17 "relaxed template template argument matching" as a workaround for
140 // our out-dated libcxx.
141 // http://b/341084395
142 "-fno-relaxed-template-template-args",
143
AdityaK423e4ce2023-05-26 12:08:05 -0700144 // Using simple template names reduces the size of debug builds.
145 "-gsimple-template-names",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000146
Eric Rahm5571ab82023-10-20 15:56:17 +0000147 // Use zstd to compress debug data.
148 "-gz=zstd",
149
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000150 // Make paths in deps files relative.
151 "-no-canonical-prefixes",
David Duarteeefabc82024-04-10 23:15:08 +0000152
153 // http://b/315250603 temporarily disabled
154 "-Wno-error=format",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 }
156
Colin Cross6f6a4282016-10-17 14:19:06 -0700157 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700158
Liz Kammere4d1bda2022-06-22 21:02:08 +0000159 commonGlobalAsflags = []string{
160 "-D__ASSEMBLY__",
161 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to
162 // end up in the dwarf data for crtend_so.S.
163 "-fdebug-default-version=4",
164 }
165
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000166 // Compilation flags for device code; not applied to host code.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700168 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800169 "-fdata-sections",
170 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700171 "-funwind-tables",
172 "-fstack-protector-strong",
173 "-Wa,--noexecstack",
174 "-D_FORTIFY_SOURCE=2",
175
176 "-Wstrict-aliasing=2",
177
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 "-Werror=return-type",
179 "-Werror=non-virtual-dtor",
180 "-Werror=address",
181 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700182 "-Werror=format-security",
Colin Crossc8bed312021-07-14 17:56:21 -0700183 "-nostdlibinc",
Yi Kong196b9262021-12-07 13:51:38 +0800184
185 // Emit additional debug info for AutoFDO
186 "-fdebug-info-for-profiling",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 }
188
Yi Konga9e1df12022-10-20 14:45:52 +0900189 commonGlobalLldflags = []string{
190 "-fuse-ld=lld",
191 "-Wl,--icf=safe",
Steven Moreland8fe37e42023-07-22 00:14:55 +0000192 "-Wl,--no-demangle",
Yi Konga9e1df12022-10-20 14:45:52 +0900193 }
194
Colin Cross26f14502017-11-06 13:59:48 -0800195 deviceGlobalCppflags = []string{
196 "-fvisibility-inlines-hidden",
197 }
198
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000199 // Linking flags for device code; not applied to host binaries.
Colin Cross324a4572017-11-02 23:09:41 -0700200 deviceGlobalLdflags = []string{
201 "-Wl,-z,noexecstack",
202 "-Wl,-z,relro",
203 "-Wl,-z,now",
204 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700205 "-Wl,--fatal-warnings",
206 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800207 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
208 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700209 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700210 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800211 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800212 "-Wl,--exclude-libs,libunwind.a",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 }
214
Eric Rahm19524712023-10-20 15:56:27 +0000215 deviceGlobalLldflags = append(append(deviceGlobalLdflags, commonGlobalLldflags...),
216 "-Wl,--compress-debug-sections=zstd",
217 )
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700218
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 hostGlobalCflags = []string{}
220
Colin Cross26f14502017-11-06 13:59:48 -0800221 hostGlobalCppflags = []string{}
222
Colin Cross324a4572017-11-02 23:09:41 -0700223 hostGlobalLdflags = []string{}
224
Yi Konga9e1df12022-10-20 14:45:52 +0900225 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700226
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 commonGlobalCppflags = []string{
Colin Crossc8bed312021-07-14 17:56:21 -0700228 // -Wimplicit-fallthrough is not enabled by -Wall.
229 "-Wimplicit-fallthrough",
230
231 // Enable clang's thread-safety annotations in libcxx.
232 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
233
234 // libc++'s math.h has an #include_next outside of system_headers.
235 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 }
237
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000238 // These flags are appended after the module's cflags, so they cannot be
239 // overridden from Android.bp files.
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000240 //
241 // NOTE: if you need to disable a warning to unblock a compiler upgrade
242 // and it is only triggered by third party code, add it to
243 // extraExternalCflags (if possible) or noOverrideExternalGlobalCflags
244 // (if the former doesn't work). If the new warning also occurs in first
245 // party code, try adding it to commonGlobalCflags first. Adding it here
246 // should be the last resort, because it prevents all code in Android from
247 // opting into the warning.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000249 "-Werror=bool-operation",
Zijun Zhaod1569082023-03-08 23:48:45 +0000250 "-Werror=format-insufficient-args",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000251 "-Werror=implicit-int-float-conversion",
252 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 "-Werror=int-to-pointer-cast",
254 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000255 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700256 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
257 "-Wno-void-pointer-to-enum-cast",
258 // http://b/161386391 for -Wno-void-pointer-to-int-cast
259 "-Wno-void-pointer-to-int-cast",
260 // http://b/161386391 for -Wno-pointer-to-int-cast
261 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700262 "-Werror=fortify-source",
Aditya Kumaref7c1212024-02-02 17:22:03 +0000263 // http://b/315246135 temporarily disabled
AdityaK94688b52024-02-08 13:50:18 -0800264 "-Wno-unused-variable",
Aditya Kumaref7c1212024-02-02 17:22:03 +0000265 // Disabled because it produces many false positives. http://b/323050926
266 "-Wno-missing-field-initializers",
267 // http://b/323050889
268 "-Wno-packed-non-pod",
Colin Crossc8bed312021-07-14 17:56:21 -0700269
270 "-Werror=address-of-temporary",
Krzysztof Kosiński83199b52023-10-26 07:16:29 +0000271 "-Werror=incompatible-function-pointer-types",
zijunzhao2863c0a2023-02-06 21:28:30 +0000272 "-Werror=null-dereference",
Colin Crossc8bed312021-07-14 17:56:21 -0700273 "-Werror=return-type",
274
275 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
276 // new warnings are fixed.
277 "-Wno-tautological-constant-compare",
278 "-Wno-tautological-type-limit-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700279 // http://b/145211066
280 "-Wno-implicit-int-float-conversion",
281 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700282 "-Wno-tautological-overlap-compare", // http://b/148815696
283 // New warnings to be fixed after clang-r383902.
284 "-Wno-deprecated-copy", // http://b/153746672
285 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700286 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
287 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800288 "-Wno-deprecated-enum-enum-conversion",
289 "-Wno-pessimizing-move", // http://b/154270751
Colin Crossc8bed312021-07-14 17:56:21 -0700290 // New warnings to be fixed after clang-r399163
291 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000292 // New warnings to be fixed after clang-r428724
293 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800294 // New warnings to be fixed after clang-r433403
295 "-Wno-error=unused-but-set-variable", // http://b/197240255
296 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000297 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000298 "-Wno-error=deprecated-builtins", // http://b/241601211
299 "-Wno-error=deprecated", // in external/googletest/googletest
Yabin Cui29f248b2022-11-30 13:32:10 -0800300 // New warnings to be fixed after clang-r475365
301 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903
Yabin Cui29f248b2022-11-30 13:32:10 -0800302 "-Wno-error=enum-constexpr-conversion", // http://b/243964282
Chris Wailesffebc5b2024-06-10 18:06:52 +0000303 // New warnings to be fixed after clang-r522817
304 "-Wno-error=invalid-offsetof",
305 "-Wno-error=thread-safety-reference-return",
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800306
307 // Irrelevant on Android because _we_ don't use exceptions, but causes
308 // lots of build noise because libcxx/libcxxabi do. This can probably
309 // go away when we're on a new enough libc++, but has to be global
310 // until then because it causes warnings in the _callers_, not the
311 // project itself.
312 "-Wno-deprecated-dynamic-exception-spec",
Chris Wailesffebc5b2024-06-10 18:06:52 +0000313
314 // Allow using VLA CXX extension.
315 "-Wno-vla-cxx-extension",
Colin Crossc8bed312021-07-14 17:56:21 -0700316 }
317
zijunzhao933e3802023-01-12 07:26:20 +0000318 noOverride64GlobalCflags = []string{}
319
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000320 // Extra cflags applied to third-party code (anything for which
321 // IsThirdPartyPath() in build/soong/android/paths.go returns true;
322 // includes external/, most of vendor/ and most of hardware/)
Colin Crossc8bed312021-07-14 17:56:21 -0700323 extraExternalCflags = []string{
324 "-Wno-enum-compare",
325 "-Wno-enum-compare-switch",
326
327 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
328 // this new warning are fixed.
329 "-Wno-null-pointer-arithmetic",
330
331 // Bug: http://b/29823425 Disable -Wnull-dereference until the
332 // new instances detected by this warning are fixed.
333 "-Wno-null-dereference",
334
335 // http://b/145211477
336 "-Wno-pointer-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700337 "-Wno-final-dtor-non-final-class",
338
339 // http://b/165945989
340 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800341
342 // http://b/199369603
343 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800344
345 // http://b/175068488
346 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800347
348 // http://b/239661264
349 "-Wno-deprecated-non-prototype",
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800350
351 "-Wno-unused",
352 "-Wno-deprecated",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 }
354
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000355 // Similar to noOverrideGlobalCflags, but applies only to third-party code
356 // (see extraExternalCflags).
357 // This section can unblock compiler upgrades when a third party module that
358 // enables -Werror and some group of warnings explicitly triggers newly
359 // added warnings.
360 noOverrideExternalGlobalCflags = []string{
361 // http://b/151457797
362 "-fcommon",
363 // http://b/191699019
364 "-Wno-format-insufficient-args",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000365 // http://b/296321508
366 // Introduced in response to a critical security vulnerability and
367 // should be a hard error - it requires only whitespace changes to fix.
368 "-Wno-misleading-indentation",
369 // Triggered by old LLVM code in external/llvm. Likely not worth
370 // enabling since it's a cosmetic issue.
371 "-Wno-bitwise-instead-of-logical",
372
Prashanth Swaminathan13b06372024-01-22 19:41:52 -0800373 "-Wno-unused",
374 "-Wno-unused-parameter",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000375 "-Wno-unused-but-set-parameter",
376 "-Wno-unqualified-std-cast-call",
377 "-Wno-array-parameter",
378 "-Wno-gnu-offsetof-extensions",
Liana Kazanova885f2ee2024-05-28 23:33:24 +0000379 // TODO: Enable this warning http://b/315245071
380 "-Wno-fortify-source",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000381 }
382
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700383 llvmNextExtraCommonGlobalCflags = []string{
Yi Konga8b0bfb2022-12-26 16:02:50 +0900384 // Do not report warnings when testing with the top of trunk LLVM.
AdityaKe7b60672023-10-27 11:26:00 -0700385 "-Wno-everything",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700386 }
Yi Kongb79fc582022-07-13 14:50:33 +0800387
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000388 // Flags that must not appear in any command line.
Colin Crossb98c8b02016-07-29 13:44:28 -0700389 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700390 "-w",
391 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700392
Elliott Hughesfb294e32023-06-14 10:42:45 -0700393 CStdVersion = "gnu17"
Elliott Hughesc79d9e32022-01-13 14:56:02 -0800394 CppStdVersion = "gnu++20"
Elliott Hughesfb294e32023-06-14 10:42:45 -0700395 ExperimentalCStdVersion = "gnu2x"
Tomasz Wasilczyke1496162023-11-15 11:46:26 -0800396 ExperimentalCppStdVersion = "gnu++2b"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700397
Leo Li8756b372017-05-22 16:11:34 -0700398 // prebuilts/clang default settings.
399 ClangDefaultBase = "prebuilts/clang/host"
Chris Wailesffebc5b2024-06-10 18:06:52 +0000400 ClangDefaultVersion = "clang-r522817"
Aditya Kumaref7c1212024-02-02 17:22:03 +0000401 ClangDefaultShortVersion = "18"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800402
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800403 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800404 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800405 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800406 "vendor/",
407 }
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000408
409 VersionScriptFlagPrefix = "-Wl,--version-script,"
Trevor Radcliffea772d652023-04-11 13:41:17 +0000410
411 VisibilityHiddenFlag = "-fvisibility=hidden"
412 VisibilityDefaultFlag = "-fvisibility=default"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413)
414
415func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700416 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000417 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
418 }
419
Cole Faust8982b1c2024-04-08 16:54:45 -0700420 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
421 pctx.StaticVariable("CommonGlobalAsflags", strings.Join(commonGlobalAsflags, " "))
422 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
423 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
424 pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
425 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
426 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
427 pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700428
Colin Cross0523ba22021-07-14 18:45:05 -0700429 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossbcf53702024-01-17 11:11:03 -0800430 flags := slices.Clone(commonGlobalCflags)
Stephen Hines66c8b442019-06-10 17:40:12 -0700431
432 // http://b/131390872
433 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000434 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700435 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000436 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700437 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
438 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800439 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
440 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800441 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000442 // Default to zero initialization.
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000443 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700444 }
Yi Kong62e75f52021-08-18 15:38:20 +0800445
446 // Workaround for ccache with clang.
447 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
448 if ctx.Config().IsEnvTrue("USE_CCACHE") {
449 flags = append(flags, "-Wno-unused-command-line-argument")
450 }
Yi Kongb79fc582022-07-13 14:50:33 +0800451
Yi Kongf7f69e42022-07-21 15:49:05 +0800452 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
453 flags = append(flags, "-Wno-error=unknown-warning-option")
454 }
455
Fabián Cañasbc105442023-06-30 17:53:06 +0000456 switch ctx.Config().Getenv("CLANG_DEFAULT_DEBUG_LEVEL") {
457 case "debug_level_0":
458 flags = append(flags, "-g0")
459 case "debug_level_1":
460 flags = append(flags, "-g1")
461 case "debug_level_2":
462 flags = append(flags, "-g2")
463 case "debug_level_3":
464 flags = append(flags, "-g3")
465 case "debug_level_g":
466 flags = append(flags, "-g")
467 default:
468 flags = append(flags, "-g")
469 }
470
Stephen Hines66c8b442019-06-10 17:40:12 -0700471 return strings.Join(flags, " ")
472 })
473
Colin Cross0523ba22021-07-14 18:45:05 -0700474 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700475 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800476 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700477
Yi Kong60a20102023-01-13 04:40:18 +0900478 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string {
479 flags := noOverrideGlobalCflags
480 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
481 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...)
AdityaKe7b60672023-10-27 11:26:00 -0700482 IllegalFlags = []string{} // Don't fail build while testing a new compiler.
zijunzhao933e3802023-01-12 07:26:20 +0000483 }
484 return strings.Join(flags, " ")
485 })
486
Cole Faust8982b1c2024-04-08 16:54:45 -0700487 pctx.StaticVariable("NoOverride64GlobalCflags", strings.Join(noOverride64GlobalCflags, " "))
488 pctx.StaticVariable("HostGlobalCflags", strings.Join(hostGlobalCflags, " "))
489 pctx.StaticVariable("NoOverrideExternalGlobalCflags", strings.Join(noOverrideExternalGlobalCflags, " "))
490 pctx.StaticVariable("CommonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
491 pctx.StaticVariable("ExternalCflags", strings.Join(extraExternalCflags, " "))
Trevor Radcliffea772d652023-04-11 13:41:17 +0000492
Colin Cross1cfd89a2016-09-15 09:30:46 -0700493 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700494 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000495 commonGlobalIncludes := []string{
496 "system/core/include",
497 "system/logging/liblog/include",
498 "system/media/audio/include",
499 "hardware/libhardware/include",
500 "hardware/libhardware_legacy/include",
501 "hardware/ril/include",
502 "frameworks/native/include",
503 "frameworks/native/opengl/include",
504 "frameworks/av/include",
505 }
Jingwen Chen21999952021-05-19 05:53:51 +0000506 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700507
Cole Faust8982b1c2024-04-08 16:54:45 -0700508 pctx.StaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
509 pctx.StaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000510
Dan Willemsen9fe14102021-07-13 21:52:04 -0700511 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000512 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700513 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
514 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
515
Sam Delmerico3eda0192023-04-14 18:07:25 +0000516 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Yi Kongbd188812023-02-08 19:51:59 +0900517 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700518
Cole Faust8982b1c2024-04-08 16:54:45 -0700519 pctx.StaticVariable("WarningAllowedProjects", strings.Join(WarningAllowedProjects, " "))
Sam Delmerico036afab2023-05-04 16:43:36 -0400520
Jayant Chowdharye622d202017-02-01 19:19:52 -0800521 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
522 // being used for the rest of the build process.
523 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
524 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
525 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
526 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
527 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
528
Alixe2667872023-04-24 14:57:32 +0000529 rsGlobalIncludes := []string{
530 "external/clang/lib/Headers",
531 "frameworks/rs/script_api/include",
532 }
533 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", rsGlobalIncludes)
Colin Cross2a252be2017-05-01 17:37:24 -0700534
Dan Willemsen54daaf02018-03-12 13:24:09 -0700535 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
536 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
537 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700538 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700539 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700540 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400541
Colin Cross77cdcfd2021-03-12 11:28:25 -0800542 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
543 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
544 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
545 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
546 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
547 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
548 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700549}
550
Cole Faust8982b1c2024-04-08 16:54:45 -0700551var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700552
553func ClangPath(ctx android.PathContext, file string) android.SourcePath {
554 type clangToolKey string
555
556 key := android.NewCustomOnceKey(clangToolKey(file))
557
558 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
559 return clangPath(ctx).Join(ctx, file)
560 })
561}
562
563var clangPathKey = android.NewOnceKey("clangPath")
564
565func clangPath(ctx android.PathContext) android.SourcePath {
566 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
567 clangBase := ClangDefaultBase
568 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
569 clangBase = override
570 }
571 clangVersion := ClangDefaultVersion
572 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
573 clangVersion = override
574 }
575 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
576 })
577}