blob: 8047d8eb0524330d779cec46ea8298b163598ef6 [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",
48 "-Wno-multichar",
49
50 "-O2",
51 "-g",
Stephen Hines8afd1752022-01-20 11:01:16 -080052 "-fdebug-default-version=5",
Colin Cross7278afc2017-11-02 22:38:32 -070053
54 "-fno-strict-aliasing",
Dan Willemsen5d980c82019-08-27 19:37:10 -070055
56 "-Werror=date-time",
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080057 "-Werror=pragma-pack",
58 "-Werror=pragma-pack-suspicious-include",
Christopher Di Bella23a991c2020-11-11 22:41:32 +000059 "-Werror=string-plus-int",
George Burgess IVfb81db22020-02-22 19:28:56 -080060 "-Werror=unreachable-code-loop-increment",
Colin Cross9cc59c12021-07-14 17:59:54 -070061
Krzysztof Kosiński6934b0e2022-08-07 06:56:54 +000062 // Force deprecation warnings to be warnings for code that compiles with -Werror.
63 // Making deprecated usages an error causes extreme pain when trying to deprecate anything.
64 "-Wno-error=deprecated-declarations",
65
Colin Cross9cc59c12021-07-14 17:59:54 -070066 "-D__compiler_offsetof=__builtin_offsetof",
67
68 // Emit address-significance table which allows linker to perform safe ICF. Clang does
69 // not emit the table by default on Android since NDK still uses GNU binutils.
70 "-faddrsig",
71
72 // Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug
73 // tracking this is http://b/151457797.
74 "-fcommon",
75
76 // Help catch common 32/64-bit errors.
77 "-Werror=int-conversion",
78
Colin Cross9cc59c12021-07-14 17:59:54 -070079 // Disable overly aggressive warning for macros defined with a leading underscore
80 // This happens in AndroidConfig.h, which is included nearly everywhere.
81 // TODO: can we remove this now?
82 "-Wno-reserved-id-macro",
83
Colin Cross9cc59c12021-07-14 17:59:54 -070084 // Force clang to always output color diagnostics. Ninja will strip the ANSI
85 // color codes if it is not running in a terminal.
86 "-fcolor-diagnostics",
87
88 // Warnings from clang-7.0
89 "-Wno-sign-compare",
90
Colin Cross9cc59c12021-07-14 17:59:54 -070091 // Disable -Winconsistent-missing-override until we can clean up the existing
92 // codebase for it.
93 "-Wno-inconsistent-missing-override",
94
95 // Warnings from clang-10
96 // Nested and array designated initialization is nice to have.
97 "-Wno-c99-designator",
98
Chih-Hung Hsieh9df9dd42022-02-03 19:03:49 -080099 // Many old files still have GNU designator syntax.
100 "-Wno-gnu-designator",
101
Colin Cross9cc59c12021-07-14 17:59:54 -0700102 // Warnings from clang-12
103 "-Wno-gnu-folding-constant",
104
105 // Calls to the APIs that are newer than the min sdk version of the caller should be
106 // guarded with __builtin_available.
107 "-Wunguarded-availability",
108 // This macro 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__",
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800111
112 // Turn off FMA which got enabled by default in clang-r445002 (http://b/218805949)
113 "-ffp-contract=off",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 }
115
Colin Cross6f6a4282016-10-17 14:19:06 -0700116 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700117
Liz Kammere4d1bda2022-06-22 21:02:08 +0000118 commonGlobalAsflags = []string{
119 "-D__ASSEMBLY__",
120 // TODO(b/235105792): override global -fdebug-default-version=5, it is causing $TMPDIR to
121 // end up in the dwarf data for crtend_so.S.
122 "-fdebug-default-version=4",
123 }
124
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 deviceGlobalCflags = []string{
Colin Cross133dbe72017-11-02 22:55:19 -0700126 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -0800127 "-fdata-sections",
128 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -0700129 "-funwind-tables",
130 "-fstack-protector-strong",
131 "-Wa,--noexecstack",
132 "-D_FORTIFY_SOURCE=2",
133
134 "-Wstrict-aliasing=2",
135
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 "-Werror=return-type",
137 "-Werror=non-virtual-dtor",
138 "-Werror=address",
139 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -0700140 "-Werror=format-security",
Colin Crossc8bed312021-07-14 17:56:21 -0700141 "-nostdlibinc",
Yi Kong196b9262021-12-07 13:51:38 +0800142
143 // Emit additional debug info for AutoFDO
144 "-fdebug-info-for-profiling",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 }
146
Yi Konga9e1df12022-10-20 14:45:52 +0900147 commonGlobalLldflags = []string{
148 "-fuse-ld=lld",
149 "-Wl,--icf=safe",
150 }
151
Colin Cross26f14502017-11-06 13:59:48 -0800152 deviceGlobalCppflags = []string{
153 "-fvisibility-inlines-hidden",
154 }
155
Colin Cross324a4572017-11-02 23:09:41 -0700156 deviceGlobalLdflags = []string{
157 "-Wl,-z,noexecstack",
158 "-Wl,-z,relro",
159 "-Wl,-z,now",
160 "-Wl,--build-id=md5",
Colin Cross324a4572017-11-02 23:09:41 -0700161 "-Wl,--fatal-warnings",
162 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800163 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
164 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700165 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -0700166 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800167 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800168 "-Wl,--exclude-libs,libunwind.a",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 }
170
Yi Konga9e1df12022-10-20 14:45:52 +0900171 deviceGlobalLldflags = append(deviceGlobalLdflags, commonGlobalLldflags...)
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700172
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 hostGlobalCflags = []string{}
174
Colin Cross26f14502017-11-06 13:59:48 -0800175 hostGlobalCppflags = []string{}
176
Colin Cross324a4572017-11-02 23:09:41 -0700177 hostGlobalLdflags = []string{}
178
Yi Konga9e1df12022-10-20 14:45:52 +0900179 hostGlobalLldflags = commonGlobalLldflags
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 commonGlobalCppflags = []string{
182 "-Wsign-promo",
Colin Crossc8bed312021-07-14 17:56:21 -0700183
184 // -Wimplicit-fallthrough is not enabled by -Wall.
185 "-Wimplicit-fallthrough",
186
187 // Enable clang's thread-safety annotations in libcxx.
188 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
189
190 // libc++'s math.h has an #include_next outside of system_headers.
191 "-Wno-gnu-include-next",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192 }
193
194 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000195 "-Werror=bool-operation",
196 "-Werror=implicit-int-float-conversion",
197 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 "-Werror=int-to-pointer-cast",
199 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000200 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700201 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
202 "-Wno-void-pointer-to-enum-cast",
203 // http://b/161386391 for -Wno-void-pointer-to-int-cast
204 "-Wno-void-pointer-to-int-cast",
205 // http://b/161386391 for -Wno-pointer-to-int-cast
206 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700207 "-Werror=fortify-source",
Colin Crossc8bed312021-07-14 17:56:21 -0700208
209 "-Werror=address-of-temporary",
210 // Bug: http://b/29823425 Disable -Wnull-dereference until the
211 // new cases detected by this warning in Clang r271374 are
212 // fixed.
213 //"-Werror=null-dereference",
214 "-Werror=return-type",
215
216 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
217 // new warnings are fixed.
218 "-Wno-tautological-constant-compare",
219 "-Wno-tautological-type-limit-compare",
220 // http://b/145210666
221 "-Wno-reorder-init-list",
222 // http://b/145211066
223 "-Wno-implicit-int-float-conversion",
224 // New warnings to be fixed after clang-r377782.
Colin Crossc8bed312021-07-14 17:56:21 -0700225 "-Wno-sizeof-array-div", // http://b/148815709
226 "-Wno-tautological-overlap-compare", // http://b/148815696
227 // New warnings to be fixed after clang-r383902.
228 "-Wno-deprecated-copy", // http://b/153746672
229 "-Wno-range-loop-construct", // http://b/153747076
Colin Crossc8bed312021-07-14 17:56:21 -0700230 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
231 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
Colin Crossc8bed312021-07-14 17:56:21 -0700232 "-Wno-pessimizing-move", // http://b/154270751
233 // New warnings to be fixed after clang-r399163
234 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cui10bf3b82021-08-10 15:42:10 +0000235 // New warnings to be fixed after clang-r428724
236 "-Wno-align-mismatch", // http://b/193679946
Yi Konge8273292021-08-31 14:04:18 +0800237 // New warnings to be fixed after clang-r433403
238 "-Wno-error=unused-but-set-variable", // http://b/197240255
239 "-Wno-error=unused-but-set-parameter", // http://b/197240255
Yi Kongeb8d04e2022-07-08 13:47:09 +0800240 // New warnings to be fixed after clang-r458507
241 "-Wno-error=unqualified-std-cast-call", // http://b/239662094
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000242 // New warnings to be fixed after clang-r468909
Chih-hung Hsieh5c40a922022-09-27 07:19:03 +0000243 "-Wno-error=deprecated-builtins", // http://b/241601211
244 "-Wno-error=deprecated", // in external/googletest/googletest
Colin Crossc8bed312021-07-14 17:56:21 -0700245 }
246
Stephen Hinese24303f2021-12-14 15:07:08 -0800247 noOverrideExternalGlobalCflags = []string{
248 // http://b/197240255
249 "-Wno-unused-but-set-variable",
250 "-Wno-unused-but-set-parameter",
Pirama Arumuga Nainar8a4804f2022-02-14 11:19:26 -0800251 // http://b/215753485
252 "-Wno-bitwise-instead-of-logical",
Elliott Hughesf08a6642022-05-17 08:08:14 -0700253 // http://b/232926688
254 "-Wno-misleading-indentation",
Chih-Hung Hsieh823e0b72022-10-07 10:23:51 -0700255 // http://b/241941550
256 "-Wno-array-parameter",
Stephen Hinese24303f2021-12-14 15:07:08 -0800257 }
258
Colin Crossc8bed312021-07-14 17:56:21 -0700259 // Extra cflags for external third-party projects to disable warnings that
260 // are infeasible to fix in all the external projects and their upstream repos.
261 extraExternalCflags = []string{
262 "-Wno-enum-compare",
263 "-Wno-enum-compare-switch",
264
265 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
266 // this new warning are fixed.
267 "-Wno-null-pointer-arithmetic",
268
269 // Bug: http://b/29823425 Disable -Wnull-dereference until the
270 // new instances detected by this warning are fixed.
271 "-Wno-null-dereference",
272
273 // http://b/145211477
274 "-Wno-pointer-compare",
275 // http://b/145211022
276 "-Wno-xor-used-as-pow",
277 // http://b/145211022
278 "-Wno-final-dtor-non-final-class",
279
280 // http://b/165945989
281 "-Wno-psabi",
Yi Konge8273292021-08-31 14:04:18 +0800282
283 // http://b/199369603
284 "-Wno-null-pointer-subtraction",
Yi Kong9feb0292021-12-15 13:20:27 +0800285
286 // http://b/175068488
287 "-Wno-string-concatenation",
Yi Kongeb8d04e2022-07-08 13:47:09 +0800288
289 // http://b/239661264
290 "-Wno-deprecated-non-prototype",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291 }
292
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700293 llvmNextExtraCommonGlobalCflags = []string{
Yabin Cui0ebe7c82022-10-27 10:17:16 -0700294 // New warnings to be fixed after clang-r475365
295 "-Wno-error=single-bit-bitfield-constant-conversion", // http://b/243965903
Yabin Cui758dfa12022-11-15 14:56:09 -0800296 // Skip deprecated flags.
297 "-Wno-unused-command-line-argument",
Chih-Hung Hsiehec450d92022-08-02 12:03:50 -0700298 }
Yi Kongb79fc582022-07-13 14:50:33 +0800299
Colin Crossb98c8b02016-07-29 13:44:28 -0700300 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 "-w",
302 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700303
Elliott Hughesab5e4c62022-03-28 16:47:17 -0700304 CStdVersion = "gnu11"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000305 CppStdVersion = "gnu++17"
Elliott Hughes6741d0e2022-05-12 09:53:11 -0700306 ExperimentalCStdVersion = "gnu17"
Elliott Hughes37976122018-11-28 14:16:39 -0800307 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700308
Leo Li8756b372017-05-22 16:11:34 -0700309 // prebuilts/clang default settings.
310 ClangDefaultBase = "prebuilts/clang/host"
Stephen Hines61863182022-10-07 00:33:59 -0700311 ClangDefaultVersion = "clang-r468909b"
312 ClangDefaultShortVersion = "15.0.3"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800313
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800314 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800315 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800316 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800317 "vendor/",
318 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700319)
320
Sam Delmerico7f889562022-03-25 14:55:40 +0000321// BazelCcToolchainVars generates bzl file content containing variables for
322// Bazel's cc_toolchain configuration.
323func BazelCcToolchainVars(config android.Config) string {
324 return android.BazelToolchainVars(config, exportedVars)
325}
326
327func ExportStringList(name string, value []string) {
328 exportedVars.ExportStringList(name, value)
329}
Colin Crossb98c8b02016-07-29 13:44:28 -0700330
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331func init() {
Colin Cross0c66bc62021-07-20 09:47:41 -0700332 if runtime.GOOS == "linux" {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000333 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
334 }
335
Sam Delmerico7f889562022-03-25 14:55:40 +0000336 exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
Liz Kammere4d1bda2022-06-22 21:02:08 +0000337 exportedVars.ExportStringListStaticVariable("CommonGlobalAsflags", commonGlobalAsflags)
Sam Delmerico7f889562022-03-25 14:55:40 +0000338 exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
339 exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
340 exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
341 exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
342 exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
343 exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000344
Colin Cross0523ba22021-07-14 18:45:05 -0700345 // Export the static default CommonGlobalCflags to Bazel.
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000346 // TODO(187086342): handle cflags that are set in VariableFuncs.
Colin Cross0523ba22021-07-14 18:45:05 -0700347 bazelCommonGlobalCflags := append(
Colin Cross33bac242021-07-14 17:03:16 -0700348 commonGlobalCflags,
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000349 []string{
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000350 // Default to zero initialization.
351 "-ftrivial-auto-var-init=zero",
352 "-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang",
353 }...)
Sam Delmerico7f889562022-03-25 14:55:40 +0000354 exportedVars.ExportStringList("CommonGlobalCflags", bazelCommonGlobalCflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700355
Colin Cross0523ba22021-07-14 18:45:05 -0700356 pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Cross33bac242021-07-14 17:03:16 -0700357 flags := commonGlobalCflags
Stephen Hines66c8b442019-06-10 17:40:12 -0700358
359 // http://b/131390872
360 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000361 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700362 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
363 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
364 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
365 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800366 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
367 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800368 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000369 // Default to zero initialization.
370 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
Stephen Hines66c8b442019-06-10 17:40:12 -0700371 }
Yi Kong62e75f52021-08-18 15:38:20 +0800372
373 // Workaround for ccache with clang.
374 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
375 if ctx.Config().IsEnvTrue("USE_CCACHE") {
376 flags = append(flags, "-Wno-unused-command-line-argument")
377 }
Yi Kongb79fc582022-07-13 14:50:33 +0800378
379 if ctx.Config().IsEnvTrue("LLVM_NEXT") {
380 flags = append(flags, llvmNextExtraCommonGlobalCflags...)
381 }
382
Yi Kongf7f69e42022-07-21 15:49:05 +0800383 if ctx.Config().IsEnvTrue("ALLOW_UNKNOWN_WARNING_OPTION") {
384 flags = append(flags, "-Wno-error=unknown-warning-option")
385 }
386
Stephen Hines66c8b442019-06-10 17:40:12 -0700387 return strings.Join(flags, " ")
388 })
389
Colin Cross0523ba22021-07-14 18:45:05 -0700390 // Export the static default DeviceGlobalCflags to Bazel.
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000391 // TODO(187086342): handle cflags that are set in VariableFuncs.
Sam Delmerico7f889562022-03-25 14:55:40 +0000392 exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000393
Colin Cross0523ba22021-07-14 18:45:05 -0700394 pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
Colin Crossc8bed312021-07-14 17:56:21 -0700395 return strings.Join(deviceGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800396 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397
Sam Delmerico7f889562022-03-25 14:55:40 +0000398 exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
399 exportedVars.ExportStringListStaticVariable("NoOverrideGlobalCflags", noOverrideGlobalCflags)
400 exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
401 exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
402 exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700403
Liz Kammera5a29de2022-05-25 23:19:37 -0400404 exportedVars.ExportString("CStdVersion", CStdVersion)
405 exportedVars.ExportString("CppStdVersion", CppStdVersion)
406 exportedVars.ExportString("ExperimentalCStdVersion", ExperimentalCStdVersion)
407 exportedVars.ExportString("ExperimentalCppStdVersion", ExperimentalCppStdVersion)
408
Colin Cross1cfd89a2016-09-15 09:30:46 -0700409 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700410 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000411 commonGlobalIncludes := []string{
412 "system/core/include",
413 "system/logging/liblog/include",
414 "system/media/audio/include",
415 "hardware/libhardware/include",
416 "hardware/libhardware_legacy/include",
417 "hardware/ril/include",
418 "frameworks/native/include",
419 "frameworks/native/opengl/include",
420 "frameworks/av/include",
421 }
Sam Delmerico7f889562022-03-25 14:55:40 +0000422 exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000423 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700424
Sam Delmerico7f889562022-03-25 14:55:40 +0000425 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
426 exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa38e5182021-08-12 21:37:35 +0000427
Dan Willemsen9fe14102021-07-13 21:52:04 -0700428 pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
429 pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
Colin Crossb98c8b02016-07-29 13:44:28 -0700430 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
431 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
432
Dan Willemsen9fe14102021-07-13 21:52:04 -0700433 pctx.StaticVariableWithEnvOverride("ClangShortVersion", "LLVM_RELEASE_VERSION", ClangDefaultShortVersion)
Stephen Hines755fe072018-01-24 19:58:36 -0800434 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700435
Jayant Chowdharye622d202017-02-01 19:19:52 -0800436 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
437 // being used for the rest of the build process.
438 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
439 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
440 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
441 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
442 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
443
Jeff Gaston734e3802017-04-10 15:47:24 -0700444 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700445 []string{
446 "external/clang/lib/Headers",
447 "frameworks/rs/script_api/include",
448 })
449
Dan Willemsen54daaf02018-03-12 13:24:09 -0700450 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
451 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
452 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700453 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700454 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700455 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400456
Colin Cross77cdcfd2021-03-12 11:28:25 -0800457 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
458 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
459 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
460 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
461 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
462 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
463 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700464}
465
Sam Delmerico7f889562022-03-25 14:55:40 +0000466var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700467
468func ClangPath(ctx android.PathContext, file string) android.SourcePath {
469 type clangToolKey string
470
471 key := android.NewCustomOnceKey(clangToolKey(file))
472
473 return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
474 return clangPath(ctx).Join(ctx, file)
475 })
476}
477
478var clangPathKey = android.NewOnceKey("clangPath")
479
480func clangPath(ctx android.PathContext) android.SourcePath {
481 return ctx.Config().OnceSourcePath(clangPathKey, func() android.SourcePath {
482 clangBase := ClangDefaultBase
483 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
484 clangBase = override
485 }
486 clangVersion := ClangDefaultVersion
487 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
488 clangVersion = override
489 }
490 return android.PathForSource(ctx, clangBase, ctx.Config().PrebuiltOS(), clangVersion)
491 })
492}