blob: 5fed7f18929237df2450cdd0da74ed3c4c55f6ef [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 (
Sam Delmerico7f889562022-03-25 14:55:40 +000027 pctx = android.NewPackageContext("android/soong/cc/config")
28 exportedVars = android.NewExportedVariables(pctx)
29
Leo Li8756b372017-05-22 16:11:34 -070030 // Flags used by lots of devices. Putting them in package static variables
31 // will save bytes in build.ninja so they aren't repeated for every file
Colin Cross4d9c2d12016-07-29 12:48:20 -070032 commonGlobalCflags = []string{
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000033 // Enable some optimization by default.
34 "-O2",
35
36 // Warnings enabled by default. Reference:
37 // https://clang.llvm.org/docs/DiagnosticsReference.html
Colin Cross4d9c2d12016-07-29 12:48:20 -070038 "-Wall",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000039 "-Wextra",
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 "-Winit-self",
41 "-Wpointer-arith",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000042 "-Wunguarded-availability",
Colin Cross4d9c2d12016-07-29 12:48:20 -070043
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000044 // Warnings treated as errors by default.
45 // See also noOverrideGlobalCflags for errors that cannot be disabled
46 // from Android.bp files.
Colin Cross7278afc2017-11-02 22:38:32 -070047
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000048 // Using __DATE__/__TIME__ causes build nondeterminism.
Dan Willemsen5d980c82019-08-27 19:37:10 -070049 "-Werror=date-time",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000050 // Detects forgotten */& that usually cause a crash
51 "-Werror=int-conversion",
52 // Detects unterminated alignment modification pragmas, which often lead
53 // to ABI mismatch between modules and hard-to-debug crashes.
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080054 "-Werror=pragma-pack",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000055 // Same as above, but detects alignment pragmas around a header
56 // inclusion.
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080057 "-Werror=pragma-pack-suspicious-include",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000058 // Detects dividing an array size by itself, which is a common typo that
59 // leads to bugs.
60 "-Werror=sizeof-array-div",
61 // Detects a typo that cuts off a prefix from a string literal.
Christopher Di Bella23a991c2020-11-11 22:41:32 +000062 "-Werror=string-plus-int",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000063 // Detects for loops that will never execute more than once (for example
64 // due to unconditional break), but have a non-empty loop increment
65 // clause. Often a mistake/bug.
George Burgess IVfb81db22020-02-22 19:28:56 -080066 "-Werror=unreachable-code-loop-increment",
Colin Cross9cc59c12021-07-14 17:59:54 -070067
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000068 // Warnings that should not be errors even for modules with -Werror.
Krzysztof Kosiński6934b0e2022-08-07 06:56:54 +000069
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000070 // Making deprecated usages an error causes extreme pain when trying to
71 // deprecate anything.
72 "-Wno-error=deprecated-declarations",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000073
74 // Warnings disabled by default.
75
76 // Designated initializer syntax is recommended by the Google C++ style
77 // and is OK to use even if not formally supported by the chosen C++
78 // version.
79 "-Wno-c99-designator",
80 // Detects uses of a GNU C extension equivalent to a limited form of
81 // constexpr. Enabling this would require replacing many constants with
82 // macros, which is not a good trade-off.
83 "-Wno-gnu-folding-constant",
84 // AIDL generated code redeclares pure virtual methods in each
85 // subsequent version of an interface, so this warning is currently
86 // infeasible to enable.
87 "-Wno-inconsistent-missing-override",
Krzysztof Kosiński8f8cc162023-10-20 01:02:58 +000088 // Detects designated initializers that are in a different order than
89 // the fields in the initialized type, which causes the side effects
90 // of initializers to occur out of order with the source code.
91 // In practice, this warning has extremely poor signal to noise ratio,
92 // because it is triggered even for initializers with no side effects.
93 // Individual modules can still opt into it via cflags.
94 "-Wno-error=reorder-init-list",
95 "-Wno-reorder-init-list",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +000096 // Incompatible with the Google C++ style guidance to use 'int' for loop
97 // indices; poor signal to noise ratio.
98 "-Wno-sign-compare",
99 // Poor signal to noise ratio.
100 "-Wno-unused",
101
102 // Global preprocessor constants.
103
104 "-DANDROID",
105 "-DNDEBUG",
106 "-UDEBUG",
Colin Cross9cc59c12021-07-14 17:59:54 -0700107 "-D__compiler_offsetof=__builtin_offsetof",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000108 // Allows the bionic versioning.h to indirectly determine whether the
109 // option -Wunguarded-availability is on or not.
110 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
111
112 // -f and -g options.
Colin Cross9cc59c12021-07-14 17:59:54 -0700113
114 // Emit address-significance table which allows linker to perform safe ICF. Clang does
115 // not emit the table by default on Android since NDK still uses GNU binutils.
116 "-faddrsig",
117
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000118 // Emit debugging data in a modern format (DWARF v5).
119 "-fdebug-default-version=5",
Colin Cross9cc59c12021-07-14 17:59:54 -0700120
Colin Cross9cc59c12021-07-14 17:59:54 -0700121 // Force clang to always output color diagnostics. Ninja will strip the ANSI
122 // color codes if it is not running in a terminal.
123 "-fcolor-diagnostics",
124
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800125 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
126 "-ffp-contract=off",
AdityaK423e4ce2023-05-26 12:08:05 -0700127
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000128 // Google C++ style does not allow exceptions, turn them off by default.
129 "-fno-exceptions",
130
131 // Disable optimizations based on strict aliasing by default.
132 // The performance benefit of enabling them currently does not outweigh
133 // the risk of hard-to-reproduce bugs.
134 "-fno-strict-aliasing",
135
136 // Disable line wrapping for error messages - it interferes with
137 // displaying logs in web browsers.
138 "-fmessage-length=0",
139
AdityaK423e4ce2023-05-26 12:08:05 -0700140 // Using simple template names reduces the size of debug builds.
141 "-gsimple-template-names",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000142
Eric Rahm5571ab82023-10-20 15:56:17 +0000143 // Use zstd to compress debug data.
144 "-gz=zstd",
145
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000146 // Make paths in deps files relative.
147 "-no-canonical-prefixes",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
149
Colin Cross6f6a4282016-10-17 14:19:06 -0700150 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700151
Liz Kammere4d1bda2022-06-22 21:02:08 +0000152 commonGlobalAsflags = []string{
153 "-D__ASSEMBLY__",
154 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to
155 // end up in the dwarf data for crtend_so.S.
156 "-fdebug-default-version=4",
157 }
158
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000159 // Compilation flags for device code; not applied to host code.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700161 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800162 "-fdata-sections",
163 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700164 "-funwind-tables",
165 "-fstack-protector-strong",
166 "-Wa,--noexecstack",
167 "-D_FORTIFY_SOURCE=2",
168
169 "-Wstrict-aliasing=2",
170
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 "-Werror=return-type",
172 "-Werror=non-virtual-dtor",
173 "-Werror=address",
174 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700175 "-Werror=format-security",
Colin Crossc8bed312021-07-14 17:56:21 -0700176 "-nostdlibinc",
Yi Kong196b9262021-12-07 13:51:38 +0800177
178 // Emit additional debug info for AutoFDO
179 "-fdebug-info-for-profiling",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 }
181
Yi Konga9e1df12022-10-20 14:45:52 +0900182 commonGlobalLldflags = []string{
183 "-fuse-ld=lld",
184 "-Wl,--icf=safe",
Steven Moreland8fe37e42023-07-22 00:14:55 +0000185 "-Wl,--no-demangle",
Yi Konga9e1df12022-10-20 14:45:52 +0900186 }
187
Colin Cross26f14502017-11-06 13:59:48 -0800188 deviceGlobalCppflags = []string{
189 "-fvisibility-inlines-hidden",
190 }
191
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000192 // Linking flags for device code; not applied to host binaries.
Colin Cross324a4572017-11-02 23:09:41 -0700193 deviceGlobalLdflags = []string{
194 "-Wl,-z,noexecstack",
195 "-Wl,-z,relro",
196 "-Wl,-z,now",
197 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700198 "-Wl,--fatal-warnings",
199 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800200 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
201 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700202 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700203 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800204 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800205 "-Wl,--exclude-libs,libunwind.a",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 }
207
Eric Rahm19524712023-10-20 15:56:27 +0000208 deviceGlobalLldflags = append(append(deviceGlobalLdflags, commonGlobalLldflags...),
209 "-Wl,--compress-debug-sections=zstd",
210 )
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700211
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 hostGlobalCflags = []string{}
213
Colin Cross26f14502017-11-06 13:59:48 -0800214 hostGlobalCppflags = []string{}
215
Colin Cross324a4572017-11-02 23:09:41 -0700216 hostGlobalLdflags = []string{}
217
Yi Konga9e1df12022-10-20 14:45:52 +0900218 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700219
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 commonGlobalCppflags = []string{
Colin Crossc8bed312021-07-14 17:56:21 -0700221 // -Wimplicit-fallthrough is not enabled by -Wall.
222 "-Wimplicit-fallthrough",
223
224 // Enable clang's thread-safety annotations in libcxx.
225 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
226
227 // libc++'s math.h has an #include_next outside of system_headers.
228 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 }
230
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000231 // These flags are appended after the module's cflags, so they cannot be
232 // overridden from Android.bp files.
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000233 //
234 // NOTE: if you need to disable a warning to unblock a compiler upgrade
235 // and it is only triggered by third party code, add it to
236 // extraExternalCflags (if possible) or noOverrideExternalGlobalCflags
237 // (if the former doesn't work). If the new warning also occurs in first
238 // party code, try adding it to commonGlobalCflags first. Adding it here
239 // should be the last resort, because it prevents all code in Android from
240 // opting into the warning.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000242 "-Werror=bool-operation",
Zijun Zhaod1569082023-03-08 23:48:45 +0000243 "-Werror=format-insufficient-args",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000244 "-Werror=implicit-int-float-conversion",
245 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 "-Werror=int-to-pointer-cast",
247 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000248 "-Werror=xor-used-as-pow",
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",
Colin Crossc8bed312021-07-14 17:56:21 -0700256
257 "-Werror=address-of-temporary",
Krzysztof Kosiński83199b52023-10-26 07:16:29 +0000258 "-Werror=incompatible-function-pointer-types",
zijunzhao2863c0a2023-02-06 21:28:30 +0000259 "-Werror=null-dereference",
Colin Crossc8bed312021-07-14 17:56:21 -0700260 "-Werror=return-type",
261
262 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
263 // new warnings are fixed.
264 "-Wno-tautological-constant-compare",
265 "-Wno-tautological-type-limit-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700266 // http://b/145211066
267 "-Wno-implicit-int-float-conversion",
268 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700269 "-Wno-tautological-overlap-compare", // http://b/148815696
270 // New warnings to be fixed after clang-r383902.
271 "-Wno-deprecated-copy", // http://b/153746672
272 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700273 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
274 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800275 "-Wno-deprecated-enum-enum-conversion",
276 "-Wno-pessimizing-move", // http://b/154270751
Colin Crossc8bed312021-07-14 17:56:21 -0700277 // New warnings to be fixed after clang-r399163
278 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000279 // New warnings to be fixed after clang-r428724
280 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800281 // New warnings to be fixed after clang-r433403
282 "-Wno-error=unused-but-set-variable", // http://b/197240255
283 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000284 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000285 "-Wno-error=deprecated-builtins", // http://b/241601211
286 "-Wno-error=deprecated", // in external/googletest/googletest
Yabin Cui29f248b2022-11-30 13:32:10 -0800287 // New warnings to be fixed after clang-r475365
288 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903
Yabin Cui29f248b2022-11-30 13:32:10 -0800289 "-Wno-error=enum-constexpr-conversion", // http://b/243964282
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800290
291 // Irrelevant on Android because _we_ don't use exceptions, but causes
292 // lots of build noise because libcxx/libcxxabi do. This can probably
293 // go away when we're on a new enough libc++, but has to be global
294 // until then because it causes warnings in the _callers_, not the
295 // project itself.
296 "-Wno-deprecated-dynamic-exception-spec",
Colin Crossc8bed312021-07-14 17:56:21 -0700297 }
298
zijunzhao933e3802023-01-12 07:26:20 +0000299 noOverride64GlobalCflags = []string{}
300
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000301 // Extra cflags applied to third-party code (anything for which
302 // IsThirdPartyPath() in build/soong/android/paths.go returns true;
303 // includes external/, most of vendor/ and most of hardware/)
Colin Crossc8bed312021-07-14 17:56:21 -0700304 extraExternalCflags = []string{
305 "-Wno-enum-compare",
306 "-Wno-enum-compare-switch",
307
308 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
309 // this new warning are fixed.
310 "-Wno-null-pointer-arithmetic",
311
312 // Bug: http://b/29823425 Disable -Wnull-dereference until the
313 // new instances detected by this warning are fixed.
314 "-Wno-null-dereference",
315
316 // http://b/145211477
317 "-Wno-pointer-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700318 "-Wno-final-dtor-non-final-class",
319
320 // http://b/165945989
321 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800322
323 // http://b/199369603
324 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800325
326 // http://b/175068488
327 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800328
329 // http://b/239661264
330 "-Wno-deprecated-non-prototype",
Elliott Hughes2ced4b52024-01-22 15:27:24 -0800331
332 "-Wno-unused",
333 "-Wno-deprecated",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 }
335
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000336 // Similar to noOverrideGlobalCflags, but applies only to third-party code
337 // (see extraExternalCflags).
338 // This section can unblock compiler upgrades when a third party module that
339 // enables -Werror and some group of warnings explicitly triggers newly
340 // added warnings.
341 noOverrideExternalGlobalCflags = []string{
342 // http://b/151457797
343 "-fcommon",
344 // http://b/191699019
345 "-Wno-format-insufficient-args",
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000346 // http://b/296321508
347 // Introduced in response to a critical security vulnerability and
348 // should be a hard error - it requires only whitespace changes to fix.
349 "-Wno-misleading-indentation",
350 // Triggered by old LLVM code in external/llvm. Likely not worth
351 // enabling since it's a cosmetic issue.
352 "-Wno-bitwise-instead-of-logical",
353
354 "-Wno-unused-but-set-variable",
355 "-Wno-unused-but-set-parameter",
356 "-Wno-unqualified-std-cast-call",
357 "-Wno-array-parameter",
358 "-Wno-gnu-offsetof-extensions",
359 }
360
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700361 llvmNextExtraCommonGlobalCflags = []string{
Yi Konga8b0bfb2022-12-26 16:02:50 +0900362 // Do not report warnings when testing with the top of trunk LLVM.
AdityaKe7b60672023-10-27 11:26:00 -0700363 "-Wno-everything",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700364 }
Yi Kongb79fc582022-07-13 14:50:33 +0800365
Krzysztof Kosiński1a4572e2023-09-02 01:08:37 +0000366 // Flags that must not appear in any command line.
Colin Crossb98c8b02016-07-29 13:44:28 -0700367 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368 "-w",
369 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700370
Elliott Hughesfb294e32023-06-14 10:42:45 -0700371 CStdVersion = "gnu17"
Elliott Hughesc79d9e32022-01-13 14:56:02 -0800372 CppStdVersion = "gnu++20"
Elliott Hughesfb294e32023-06-14 10:42:45 -0700373 ExperimentalCStdVersion = "gnu2x"
Tomasz Wasilczyke1496162023-11-15 11:46:26 -0800374 ExperimentalCppStdVersion = "gnu++2b"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700375
Leo Li8756b372017-05-22 16:11:34 -0700376 // prebuilts/clang default settings.
377 ClangDefaultBase = "prebuilts/clang/host"
Yi Konged7c73e2023-08-25 15:18:48 +0900378 ClangDefaultVersion = "clang-r498229b"
Yi Kongfd07ed22023-02-16 17:42:27 +0900379 ClangDefaultShortVersion = "17"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800380
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800381 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800382 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800383 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800384 "vendor/",
385 }
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000386
387 VersionScriptFlagPrefix = "-Wl,--version-script,"
Trevor Radcliffea772d652023-04-11 13:41:17 +0000388
389 VisibilityHiddenFlag = "-fvisibility=hidden"
390 VisibilityDefaultFlag = "-fvisibility=default"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700391)
392
Sam Delmerico7f889562022-03-25 14:55:40 +0000393func ExportStringList(name string, value []string) {
394 exportedVars.ExportStringList(name, value)
395}
Colin Crossb98c8b02016-07-29 13:44:28 -0700396
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700398 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000399 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
400 }
401
Sam Delmerico7f889562022-03-25 14:55:40 +0000402 exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
Liz Kammere4d1bda2022-06-22 21:02:08 +0000403 exportedVars.ExportStringListStaticVariable("CommonGlobalAsflags", commonGlobalAsflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000404 exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
405 exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
406 exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
407 exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
408 exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
409 exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000410
Colin Cross0523ba22021-07-14 18:45:05 -0700411 // Export the static default CommonGlobalCflags to Bazel.
Yu Liu19ea53d2022-12-21 12:39:30 -0800412 exportedVars.ExportStringList("CommonGlobalCflags", commonGlobalCflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413
Colin Cross0523ba22021-07-14 18:45:05 -0700414 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossbcf53702024-01-17 11:11:03 -0800415 flags := slices.Clone(commonGlobalCflags)
Stephen Hines66c8b442019-06-10 17:40:12 -0700416
417 // http://b/131390872
418 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000419 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700420 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000421 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700422 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
423 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800424 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
425 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800426 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000427 // Default to zero initialization.
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000428 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700429 }
Yi Kong62e75f52021-08-18 15:38:20 +0800430
431 // Workaround for ccache with clang.
432 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
433 if ctx.Config().IsEnvTrue("USE_CCACHE") {
434 flags = append(flags, "-Wno-unused-command-line-argument")
435 }
Yi Kongb79fc582022-07-13 14:50:33 +0800436
Yi Kongf7f69e42022-07-21 15:49:05 +0800437 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
438 flags = append(flags, "-Wno-error=unknown-warning-option")
439 }
440
Fabián Cañasbc105442023-06-30 17:53:06 +0000441 switch ctx.Config().Getenv("CLANG_DEFAULT_DEBUG_LEVEL") {
442 case "debug_level_0":
443 flags = append(flags, "-g0")
444 case "debug_level_1":
445 flags = append(flags, "-g1")
446 case "debug_level_2":
447 flags = append(flags, "-g2")
448 case "debug_level_3":
449 flags = append(flags, "-g3")
450 case "debug_level_g":
451 flags = append(flags, "-g")
452 default:
453 flags = append(flags, "-g")
454 }
455
Stephen Hines66c8b442019-06-10 17:40:12 -0700456 return strings.Join(flags, " ")
457 })
458
Colin Cross0523ba22021-07-14 18:45:05 -0700459 // Export the static default DeviceGlobalCflags to Bazel.
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000460 // TODO(187086342): handle cflags that are set in VariableFuncs.
Sam Delmerico7f889562022-03-25 14:55:40 +0000461 exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000462
Colin Cross0523ba22021-07-14 18:45:05 -0700463 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700464 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800465 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700466
zijunzhao14e68a22023-01-25 22:41:43 +0000467 // Export the static default NoOverrideGlobalCflags to Bazel.
Yi Kong60a20102023-01-13 04:40:18 +0900468 exportedVars.ExportStringList("NoOverrideGlobalCflags", noOverrideGlobalCflags)
469 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string {
470 flags := noOverrideGlobalCflags
471 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
472 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...)
AdityaKe7b60672023-10-27 11:26:00 -0700473 IllegalFlags = []string{} // Don't fail build while testing a new compiler.
zijunzhao933e3802023-01-12 07:26:20 +0000474 }
475 return strings.Join(flags, " ")
476 })
477
zijunzhao14e68a22023-01-25 22:41:43 +0000478 exportedVars.ExportStringListStaticVariable("NoOverride64GlobalCflags", noOverride64GlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000479 exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000480 exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
481 exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
482 exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700483
Liz Kammera5a29de2022-05-25 23:19:37 -0400484 exportedVars.ExportString("CStdVersion", CStdVersion)
485 exportedVars.ExportString("CppStdVersion", CppStdVersion)
486 exportedVars.ExportString("ExperimentalCStdVersion", ExperimentalCStdVersion)
487 exportedVars.ExportString("ExperimentalCppStdVersion", ExperimentalCppStdVersion)
488
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000489 exportedVars.ExportString("VersionScriptFlagPrefix", VersionScriptFlagPrefix)
490
Trevor Radcliffea772d652023-04-11 13:41:17 +0000491 exportedVars.ExportString("VisibilityHiddenFlag", VisibilityHiddenFlag)
492 exportedVars.ExportString("VisibilityDefaultFlag", VisibilityDefaultFlag)
493
Colin Cross1cfd89a2016-09-15 09:30:46 -0700494 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000496 commonGlobalIncludes := []string{
497 "system/core/include",
498 "system/logging/liblog/include",
499 "system/media/audio/include",
500 "hardware/libhardware/include",
501 "hardware/libhardware_legacy/include",
502 "hardware/ril/include",
503 "frameworks/native/include",
504 "frameworks/native/opengl/include",
505 "frameworks/av/include",
506 }
Sam Delmerico7f889562022-03-25 14:55:40 +0000507 exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000508 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700509
Sam Delmerico3eda0192023-04-14 18:07:25 +0000510 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
511 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
512
Dan Willemsen9fe14102021-07-13 21:52:04 -0700513 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000514 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700515 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
516 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
517
Sam Delmerico3eda0192023-04-14 18:07:25 +0000518 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Yi Kongbd188812023-02-08 19:51:59 +0900519 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700520
Sam Delmerico036afab2023-05-04 16:43:36 -0400521 exportedVars.ExportStringListStaticVariable("WarningAllowedProjects", WarningAllowedProjects)
522
Jayant Chowdharye622d202017-02-01 19:19:52 -0800523 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
524 // being used for the rest of the build process.
525 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
526 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
527 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
528 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
529 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
530
Alixe2667872023-04-24 14:57:32 +0000531 rsGlobalIncludes := []string{
532 "external/clang/lib/Headers",
533 "frameworks/rs/script_api/include",
534 }
535 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", rsGlobalIncludes)
536 exportedVars.ExportStringList("RsGlobalIncludes", rsGlobalIncludes)
Colin Cross2a252be2017-05-01 17:37:24 -0700537
Dan Willemsen54daaf02018-03-12 13:24:09 -0700538 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
539 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
540 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700541 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700542 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700543 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400544
Colin Cross77cdcfd2021-03-12 11:28:25 -0800545 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
546 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
547 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
548 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
549 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
550 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
551 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552}
553
Sam Delmerico7f889562022-03-25 14:55:40 +0000554var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700555
556func ClangPath(ctx android.PathContext, file string) android.SourcePath {
557 type clangToolKey string
558
559 key := android.NewCustomOnceKey(clangToolKey(file))
560
561 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
562 return clangPath(ctx).Join(ctx, file)
563 })
564}
565
566var clangPathKey = android.NewOnceKey("clangPath")
567
568func clangPath(ctx android.PathContext) android.SourcePath {
569 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
570 clangBase := ClangDefaultBase
571 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
572 clangBase = override
573 }
574 clangVersion := ClangDefaultVersion
575 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
576 clangVersion = override
577 }
578 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
579 })
580}