blob: a586a3f70c31c35ca026ef723eb898948ea496eb [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Crossb98c8b02016-07-29 13:44:28 -070015package config
Colin Cross4d9c2d12016-07-29 12:48:20 -070016
17import (
Colin Cross0c66bc62021-07-20 09:47:41 -070018 "runtime"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019 "strings"
20
21 "android/soong/android"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040022 "android/soong/remoteexec"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023)
24
Colin Cross4d9c2d12016-07-29 12:48:20 -070025var (
Sam Delmerico7f889562022-03-25 14:55:40 +000026 pctx = android.NewPackageContext("android/soong/cc/config")
27 exportedVars = android.NewExportedVariables(pctx)
28
Leo Li8756b372017-05-22 16:11:34 -070029 // Flags used by lots of devices. Putting them in package static variables
30 // will save bytes in build.ninja so they aren't repeated for every file
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 commonGlobalCflags = []string{
32 "-DANDROID",
33 "-fmessage-length=0",
34 "-W",
35 "-Wall",
36 "-Wno-unused",
37 "-Winit-self",
38 "-Wpointer-arith",
George Burgess IVfb81db22020-02-22 19:28:56 -080039 "-Wunreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070040
Colin Cross7278afc2017-11-02 22:38:32 -070041 // Make paths in deps files relative
42 "-no-canonical-prefixes",
43
Colin Cross4d9c2d12016-07-29 12:48:20 -070044 "-DNDEBUG",
45 "-UDEBUG",
Colin Cross7278afc2017-11-02 22:38:32 -070046
47 "-fno-exceptions",
Colin Cross7278afc2017-11-02 22:38:32 -070048
49 "-O2",
Stephen Hines8afd1752022-01-20 11:01:16 -080050 "-fdebug-default-version=5",
Colin Cross7278afc2017-11-02 22:38:32 -070051
52 "-fno-strict-aliasing",
Dan Willemsen5d980c82019-08-27 19:37:10 -070053
54 "-Werror=date-time",
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080055 "-Werror=pragma-pack",
56 "-Werror=pragma-pack-suspicious-include",
Christopher Di Bella23a991c2020-11-11 22:41:32 +000057 "-Werror=string-plus-int",
George Burgess IVfb81db22020-02-22 19:28:56 -080058 "-Werror=unreachable-code-loop-increment",
Colin Cross9cc59c12021-07-14 17:59:54 -070059
Krzysztof Kosiński6934b0e2022-08-07 06:56:54 +000060 // Force deprecation warnings to be warnings for code that compiles with -Werror.
61 // Making deprecated usages an error causes extreme pain when trying to deprecate anything.
62 "-Wno-error=deprecated-declarations",
63
Colin Cross9cc59c12021-07-14 17:59:54 -070064 "-D__compiler_offsetof=__builtin_offsetof",
65
66 // Emit address-significance table which allows linker to perform safe ICF. Clang does
67 // not emit the table by default on Android since NDK still uses GNU binutils.
68 "-faddrsig",
69
Colin Cross9cc59c12021-07-14 17:59:54 -070070 // Help catch common 32/64-bit errors.
71 "-Werror=int-conversion",
72
Colin Cross9cc59c12021-07-14 17:59:54 -070073 // Force clang to always output color diagnostics. Ninja will strip the ANSI
74 // color codes if it is not running in a terminal.
75 "-fcolor-diagnostics",
76
Krzysztof Kosiński982c5882023-08-18 18:56:45 +000077 // -Wno-sign-compare is incompatible with the Google C++ style guidance
78 // to use 'int' for loop indices, and the signal to noise ratio is poor
79 // anyway.
Colin Cross9cc59c12021-07-14 17:59:54 -070080 "-Wno-sign-compare",
81
Krzysztof Kosiński982c5882023-08-18 18:56:45 +000082 // AIDL generated code redeclares pure virtual methods in each
83 // subsequent version of an interface, so this is currently infeasible
84 // to enable.
Colin Cross9cc59c12021-07-14 17:59:54 -070085 "-Wno-inconsistent-missing-override",
86
Krzysztof Kosiński982c5882023-08-18 18:56:45 +000087 // Designated initializer syntax is recommended by the Google C++ style
88 // guide and should not be a warning, at least by default.
Colin Cross9cc59c12021-07-14 17:59:54 -070089 "-Wno-c99-designator",
90
91 // Warnings from clang-12
92 "-Wno-gnu-folding-constant",
93
Krzysztof Kosiński78425e52023-08-18 21:23:52 +000094 // http://b/145210666
95 "-Wno-error=reorder-init-list",
96
Colin Cross9cc59c12021-07-14 17:59:54 -070097 // Calls to the APIs that are newer than the min sdk version of the caller should be
98 // guarded with __builtin_available.
99 "-Wunguarded-availability",
100 // This macro allows the bionic versioning.h to indirectly determine whether the
101 // option -Wunguarded-availability is on or not.
102 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800103
104 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
105 "-ffp-contract=off",
AdityaK423e4ce2023-05-26 12:08:05 -0700106
107 // Using simple template names reduces the size of debug builds.
108 "-gsimple-template-names",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 }
110
Colin Cross6f6a4282016-10-17 14:19:06 -0700111 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700112
Liz Kammere4d1bda2022-06-22 21:02:08 +0000113 commonGlobalAsflags = []string{
114 "-D__ASSEMBLY__",
115 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to
116 // end up in the dwarf data for crtend_so.S.
117 "-fdebug-default-version=4",
118 }
119
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700121 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800122 "-fdata-sections",
123 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700124 "-funwind-tables",
125 "-fstack-protector-strong",
126 "-Wa,--noexecstack",
127 "-D_FORTIFY_SOURCE=2",
128
129 "-Wstrict-aliasing=2",
130
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 "-Werror=return-type",
132 "-Werror=non-virtual-dtor",
133 "-Werror=address",
134 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700135 "-Werror=format-security",
Colin Crossc8bed312021-07-14 17:56:21 -0700136 "-nostdlibinc",
Yi Kong196b9262021-12-07 13:51:38 +0800137
Yi Kong67a2dee2023-09-01 21:52:36 +0900138 // Enable MLGO for register allocation.
139 "-mllvm -regalloc-enable-advisor=release",
140
Yi Kong196b9262021-12-07 13:51:38 +0800141 // Emit additional debug info for AutoFDO
142 "-fdebug-info-for-profiling",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 }
144
Yi Konga9e1df12022-10-20 14:45:52 +0900145 commonGlobalLldflags = []string{
146 "-fuse-ld=lld",
147 "-Wl,--icf=safe",
Steven Moreland8fe37e42023-07-22 00:14:55 +0000148 "-Wl,--no-demangle",
Yi Konga9e1df12022-10-20 14:45:52 +0900149 }
150
Colin Cross26f14502017-11-06 13:59:48 -0800151 deviceGlobalCppflags = []string{
152 "-fvisibility-inlines-hidden",
153 }
154
Colin Cross324a4572017-11-02 23:09:41 -0700155 deviceGlobalLdflags = []string{
156 "-Wl,-z,noexecstack",
157 "-Wl,-z,relro",
158 "-Wl,-z,now",
159 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700160 "-Wl,--fatal-warnings",
161 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800162 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
163 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700164 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700165 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800166 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800167 "-Wl,--exclude-libs,libunwind.a",
Yi Kong67a2dee2023-09-01 21:52:36 +0900168 // Enable MLGO for register allocation.
169 "-Wl,-mllvm,-regalloc-enable-advisor=release",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 }
171
Yi Konga9e1df12022-10-20 14:45:52 +0900172 deviceGlobalLldflags = append(deviceGlobalLdflags, commonGlobalLldflags...)
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700173
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 hostGlobalCflags = []string{}
175
Colin Cross26f14502017-11-06 13:59:48 -0800176 hostGlobalCppflags = []string{}
177
Colin Cross324a4572017-11-02 23:09:41 -0700178 hostGlobalLdflags = []string{}
179
Yi Konga9e1df12022-10-20 14:45:52 +0900180 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700181
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182 commonGlobalCppflags = []string{
183 "-Wsign-promo",
Colin Crossc8bed312021-07-14 17:56:21 -0700184
185 // -Wimplicit-fallthrough is not enabled by -Wall.
186 "-Wimplicit-fallthrough",
187
188 // Enable clang's thread-safety annotations in libcxx.
189 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
190
191 // libc++'s math.h has an #include_next outside of system_headers.
192 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 }
194
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000195 // These flags are appended after the module's cflags, so they cannot be
196 // overridden from Android.bp files.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000198 "-Werror=bool-operation",
Zijun Zhaod1569082023-03-08 23:48:45 +0000199 "-Werror=format-insufficient-args",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000200 "-Werror=implicit-int-float-conversion",
201 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 "-Werror=int-to-pointer-cast",
203 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000204 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700205 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
206 "-Wno-void-pointer-to-enum-cast",
207 // http://b/161386391 for -Wno-void-pointer-to-int-cast
208 "-Wno-void-pointer-to-int-cast",
209 // http://b/161386391 for -Wno-pointer-to-int-cast
210 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700211 "-Werror=fortify-source",
Colin Crossc8bed312021-07-14 17:56:21 -0700212
213 "-Werror=address-of-temporary",
zijunzhao2863c0a2023-02-06 21:28:30 +0000214 "-Werror=null-dereference",
Colin Crossc8bed312021-07-14 17:56:21 -0700215 "-Werror=return-type",
216
217 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
218 // new warnings are fixed.
219 "-Wno-tautological-constant-compare",
220 "-Wno-tautological-type-limit-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700221 // http://b/145211066
222 "-Wno-implicit-int-float-conversion",
223 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700224 "-Wno-tautological-overlap-compare", // http://b/148815696
225 // New warnings to be fixed after clang-r383902.
226 "-Wno-deprecated-copy", // http://b/153746672
227 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700228 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
229 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Colin Crossc8bed312021-07-14 17:56:21 -0700230 "-Wno-pessimizing-move", // http://b/154270751
231 // New warnings to be fixed after clang-r399163
232 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000233 // New warnings to be fixed after clang-r428724
234 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800235 // New warnings to be fixed after clang-r433403
236 "-Wno-error=unused-but-set-variable", // http://b/197240255
237 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000238 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000239 "-Wno-error=deprecated-builtins", // http://b/241601211
240 "-Wno-error=deprecated", // in external/googletest/googletest
Yabin Cui29f248b2022-11-30 13:32:10 -0800241 // New warnings to be fixed after clang-r475365
242 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903
Yabin Cui29f248b2022-11-30 13:32:10 -0800243 "-Wno-error=enum-constexpr-conversion", // http://b/243964282
Colin Crossc8bed312021-07-14 17:56:21 -0700244 }
245
zijunzhao933e3802023-01-12 07:26:20 +0000246 noOverride64GlobalCflags = []string{}
247
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000248 // Similar to noOverrideGlobalCflags, but applies only to third-party code
249 // (anything for which IsThirdPartyPath() in build/soong/android/paths.go
250 // returns true - includes external/, most of vendor/ and most of hardware/)
Stephen Hinese24303f2021-12-14 15:07:08 -0800251 noOverrideExternalGlobalCflags = []string{
zijunzhao5bcebba2023-08-16 22:17:12 +0000252 // http://b/151457797
253 "-fcommon",
Zijun Zhaod1569082023-03-08 23:48:45 +0000254 // http://b/191699019
255 "-Wno-format-insufficient-args",
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000256 // http://b/296321145
257 // Indicates potential memory or stack corruption, so should be changed
258 // to a hard error. Currently triggered by some vendor code.
zijunzhao2a08ccd2023-03-15 21:35:53 +0000259 "-Wno-incompatible-function-pointer-types",
Krzysztof Kosiński982c5882023-08-18 18:56:45 +0000260 // http://b/296321508
261 // Introduced in response to a critical security vulnerability and
262 // should be a hard error - it requires only whitespace changes to fix.
263 "-Wno-misleading-indentation",
264 // Triggered by old LLVM code in external/llvm. Likely not worth
265 // enabling since it's a cosmetic issue.
266 "-Wno-bitwise-instead-of-logical",
267
Stephen Hinese24303f2021-12-14 15:07:08 -0800268 "-Wno-unused-but-set-variable",
269 "-Wno-unused-but-set-parameter",
zijunzhao96ec99a2023-03-10 22:35:31 +0000270 "-Wno-unqualified-std-cast-call",
Chih-Hung Hsieh823e0b72022-10-07 10:23:51 -0700271 "-Wno-array-parameter",
Yi Kongfd07ed22023-02-16 17:42:27 +0900272 "-Wno-gnu-offsetof-extensions",
Stephen Hinese24303f2021-12-14 15:07:08 -0800273 }
274
Colin Crossc8bed312021-07-14 17:56:21 -0700275 // Extra cflags for external third-party projects to disable warnings that
276 // are infeasible to fix in all the external projects and their upstream repos.
277 extraExternalCflags = []string{
278 "-Wno-enum-compare",
279 "-Wno-enum-compare-switch",
280
281 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
282 // this new warning are fixed.
283 "-Wno-null-pointer-arithmetic",
284
285 // Bug: http://b/29823425 Disable -Wnull-dereference until the
286 // new instances detected by this warning are fixed.
287 "-Wno-null-dereference",
288
289 // http://b/145211477
290 "-Wno-pointer-compare",
Colin Crossc8bed312021-07-14 17:56:21 -0700291 "-Wno-final-dtor-non-final-class",
292
293 // http://b/165945989
294 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800295
296 // http://b/199369603
297 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800298
299 // http://b/175068488
300 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800301
302 // http://b/239661264
303 "-Wno-deprecated-non-prototype",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304 }
305
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700306 llvmNextExtraCommonGlobalCflags = []string{
Yi Konga8b0bfb2022-12-26 16:02:50 +0900307 // Do not report warnings when testing with the top of trunk LLVM.
308 "-Wno-error",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700309 }
Yi Kongb79fc582022-07-13 14:50:33 +0800310
Colin Crossb98c8b02016-07-29 13:44:28 -0700311 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 "-w",
313 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700314
Elliott Hughesfb294e32023-06-14 10:42:45 -0700315 CStdVersion = "gnu17"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000316 CppStdVersion = "gnu++17"
Elliott Hughesfb294e32023-06-14 10:42:45 -0700317 ExperimentalCStdVersion = "gnu2x"
Elliott Hughes37976122018-11-28 14:16:39 -0800318 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700319
Leo Li8756b372017-05-22 16:11:34 -0700320 // prebuilts/clang default settings.
321 ClangDefaultBase = "prebuilts/clang/host"
Yi Konged7c73e2023-08-25 15:18:48 +0900322 ClangDefaultVersion = "clang-r498229b"
Yi Kongfd07ed22023-02-16 17:42:27 +0900323 ClangDefaultShortVersion = "17"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800324
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800325 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800326 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800327 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800328 "vendor/",
329 }
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000330
331 VersionScriptFlagPrefix = "-Wl,--version-script,"
Trevor Radcliffea772d652023-04-11 13:41:17 +0000332
333 VisibilityHiddenFlag = "-fvisibility=hidden"
334 VisibilityDefaultFlag = "-fvisibility=default"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335)
336
Sam Delmerico7f889562022-03-25 14:55:40 +0000337// BazelCcToolchainVars generates bzl file content containing variables for
338// Bazel's cc_toolchain configuration.
339func BazelCcToolchainVars(config android.Config) string {
340 return android.BazelToolchainVars(config, exportedVars)
341}
342
343func ExportStringList(name string, value []string) {
344 exportedVars.ExportStringList(name, value)
345}
Colin Crossb98c8b02016-07-29 13:44:28 -0700346
Colin Cross4d9c2d12016-07-29 12:48:20 -0700347func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700348 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000349 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
350 }
351
Sam Delmerico7f889562022-03-25 14:55:40 +0000352 exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
Liz Kammere4d1bda2022-06-22 21:02:08 +0000353 exportedVars.ExportStringListStaticVariable("CommonGlobalAsflags", commonGlobalAsflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000354 exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
355 exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
356 exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
357 exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
358 exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
359 exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000360
Colin Cross0523ba22021-07-14 18:45:05 -0700361 // Export the static default CommonGlobalCflags to Bazel.
Yu Liu19ea53d2022-12-21 12:39:30 -0800362 exportedVars.ExportStringList("CommonGlobalCflags", commonGlobalCflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363
Colin Cross0523ba22021-07-14 18:45:05 -0700364 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Cross33bac242021-07-14 17:03:16 -0700365 flags := commonGlobalCflags
Stephen Hines66c8b442019-06-10 17:40:12 -0700366
367 // http://b/131390872
368 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000369 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700370 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000371 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700372 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
373 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800374 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
375 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800376 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000377 // Default to zero initialization.
Pirama Arumuga Nainar40265582023-05-11 00:01:25 +0000378 flags = append(flags, "-ftrivial-auto-var-init=zero")
Stephen Hines66c8b442019-06-10 17:40:12 -0700379 }
Yi Kong62e75f52021-08-18 15:38:20 +0800380
381 // Workaround for ccache with clang.
382 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
383 if ctx.Config().IsEnvTrue("USE_CCACHE") {
384 flags = append(flags, "-Wno-unused-command-line-argument")
385 }
Yi Kongb79fc582022-07-13 14:50:33 +0800386
Yi Kongf7f69e42022-07-21 15:49:05 +0800387 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
388 flags = append(flags, "-Wno-error=unknown-warning-option")
389 }
390
Fabián Cañasbc105442023-06-30 17:53:06 +0000391 switch ctx.Config().Getenv("CLANG_DEFAULT_DEBUG_LEVEL") {
392 case "debug_level_0":
393 flags = append(flags, "-g0")
394 case "debug_level_1":
395 flags = append(flags, "-g1")
396 case "debug_level_2":
397 flags = append(flags, "-g2")
398 case "debug_level_3":
399 flags = append(flags, "-g3")
400 case "debug_level_g":
401 flags = append(flags, "-g")
402 default:
403 flags = append(flags, "-g")
404 }
405
Stephen Hines66c8b442019-06-10 17:40:12 -0700406 return strings.Join(flags, " ")
407 })
408
Colin Cross0523ba22021-07-14 18:45:05 -0700409 // Export the static default DeviceGlobalCflags to Bazel.
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000410 // TODO(187086342): handle cflags that are set in VariableFuncs.
Sam Delmerico7f889562022-03-25 14:55:40 +0000411 exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000412
Colin Cross0523ba22021-07-14 18:45:05 -0700413 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700414 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800415 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700416
zijunzhao14e68a22023-01-25 22:41:43 +0000417 // Export the static default NoOverrideGlobalCflags to Bazel.
Yi Kong60a20102023-01-13 04:40:18 +0900418 exportedVars.ExportStringList("NoOverrideGlobalCflags", noOverrideGlobalCflags)
419 pctx.VariableFunc("NoOverrideGlobalCflags", func(ctx android.PackageVarContext) string {
420 flags := noOverrideGlobalCflags
421 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
422 flags = append(noOverrideGlobalCflags, llvmNextExtraCommonGlobalCflags...)
zijunzhao933e3802023-01-12 07:26:20 +0000423 }
424 return strings.Join(flags, " ")
425 })
426
zijunzhao14e68a22023-01-25 22:41:43 +0000427 exportedVars.ExportStringListStaticVariable("NoOverride64GlobalCflags", noOverride64GlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000428 exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000429 exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
430 exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
431 exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700432
Liz Kammera5a29de2022-05-25 23:19:37 -0400433 exportedVars.ExportString("CStdVersion", CStdVersion)
434 exportedVars.ExportString("CppStdVersion", CppStdVersion)
435 exportedVars.ExportString("ExperimentalCStdVersion", ExperimentalCStdVersion)
436 exportedVars.ExportString("ExperimentalCppStdVersion", ExperimentalCppStdVersion)
437
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000438 exportedVars.ExportString("VersionScriptFlagPrefix", VersionScriptFlagPrefix)
439
Trevor Radcliffea772d652023-04-11 13:41:17 +0000440 exportedVars.ExportString("VisibilityHiddenFlag", VisibilityHiddenFlag)
441 exportedVars.ExportString("VisibilityDefaultFlag", VisibilityDefaultFlag)
442
Colin Cross1cfd89a2016-09-15 09:30:46 -0700443 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700444 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000445 commonGlobalIncludes := []string{
446 "system/core/include",
447 "system/logging/liblog/include",
448 "system/media/audio/include",
449 "hardware/libhardware/include",
450 "hardware/libhardware_legacy/include",
451 "hardware/ril/include",
452 "frameworks/native/include",
453 "frameworks/native/opengl/include",
454 "frameworks/av/include",
455 }
Sam Delmerico7f889562022-03-25 14:55:40 +0000456 exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000457 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700458
Sam Delmerico3eda0192023-04-14 18:07:25 +0000459 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
460 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
461
Dan Willemsen9fe14102021-07-13 21:52:04 -0700462 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
Sam Delmerico3eda0192023-04-14 18:07:25 +0000463 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700464 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
465 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
466
Sam Delmerico3eda0192023-04-14 18:07:25 +0000467 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Yi Kongbd188812023-02-08 19:51:59 +0900468 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700469
Sam Delmerico036afab2023-05-04 16:43:36 -0400470 exportedVars.ExportStringListStaticVariable("WarningAllowedProjects", WarningAllowedProjects)
471
Jayant Chowdharye622d202017-02-01 19:19:52 -0800472 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
473 // being used for the rest of the build process.
474 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
475 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
476 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
477 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
478 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
479
Alixe2667872023-04-24 14:57:32 +0000480 rsGlobalIncludes := []string{
481 "external/clang/lib/Headers",
482 "frameworks/rs/script_api/include",
483 }
484 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I", rsGlobalIncludes)
485 exportedVars.ExportStringList("RsGlobalIncludes", rsGlobalIncludes)
Colin Cross2a252be2017-05-01 17:37:24 -0700486
Dan Willemsen54daaf02018-03-12 13:24:09 -0700487 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
488 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
489 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700490 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700491 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700492 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400493
Colin Cross77cdcfd2021-03-12 11:28:25 -0800494 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
495 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
496 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
497 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
498 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
499 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
500 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700501}
502
Sam Delmerico7f889562022-03-25 14:55:40 +0000503var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700504
505func ClangPath(ctx android.PathContext, file string) android.SourcePath {
506 type clangToolKey string
507
508 key := android.NewCustomOnceKey(clangToolKey(file))
509
510 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
511 return clangPath(ctx).Join(ctx, file)
512 })
513}
514
515var clangPathKey = android.NewOnceKey("clangPath")
516
517func clangPath(ctx android.PathContext) android.SourcePath {
518 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
519 clangBase := ClangDefaultBase
520 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
521 clangBase = override
522 }
523 clangVersion := ClangDefaultVersion
524 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
525 clangVersion = override
526 }
527 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
528 })
529}