blob: e56fd777e21c5e510f9e06f6aae5fb6ee65505f9 [file] [log] [blame]
Colin Cross7f64b6d2015-07-09 13:57:48 -07001// Copyright 2015 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross7f64b6d2015-07-09 13:57:48 -070016
17import (
18 "fmt"
19 "reflect"
Dan Willemsen490fd492015-11-24 17:53:15 -080020 "runtime"
Colin Cross7f64b6d2015-07-09 13:57:48 -070021 "strings"
22
Cole Faust701ca252021-11-23 19:02:08 -080023 "android/soong/android/soongconfig"
24 "android/soong/bazel"
25
Colin Cross7f64b6d2015-07-09 13:57:48 -070026 "github.com/google/blueprint/proptools"
27)
28
29func init() {
Paul Duffine7a055c2021-03-07 15:46:33 +000030 registerVariableBuildComponents(InitRegistrationContext)
31}
32
33func registerVariableBuildComponents(ctx RegistrationContext) {
34 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
Colin Crosseabaedd2020-02-06 17:01:55 -080035 ctx.BottomUp("variable", VariableMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070036 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070037}
38
Paul Duffine7a055c2021-03-07 15:46:33 +000039var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
40
Colin Cross7f64b6d2015-07-09 13:57:48 -070041type variableProperties struct {
42 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070043 Platform_sdk_version struct {
44 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070045 Cflags []string
Dan Willemsen9fe14102021-07-13 21:52:04 -070046 Cmd *string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070047 }
Colin Cross83163192015-12-01 16:31:16 -080048
Yuntao Xu402e9b02021-08-09 15:44:44 -070049 Platform_sdk_version_or_codename struct {
50 Java_resource_dirs []string
51 }
52
Anton Hanssonb021bf82021-08-27 17:35:40 +010053 Platform_sdk_extension_version struct {
54 Cmd *string
55 }
56
Dan Willemsen9fe14102021-07-13 21:52:04 -070057 Platform_version_name struct {
58 Base_dir *string
59 }
60
Colin Cross83163192015-12-01 16:31:16 -080061 // unbundled_build is a catch-all property to annotate modules that don't build in one or
62 // more unbundled branches, usually due to dependencies missing from the manifest.
63 Unbundled_build struct {
64 Enabled *bool `android:"arch_variant"`
65 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080066
Dan Willemsen97cae012016-03-01 14:08:42 -080067 Malloc_not_svelte struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080068 Cflags []string `android:"arch_variant"`
69 Shared_libs []string `android:"arch_variant"`
70 Whole_static_libs []string `android:"arch_variant"`
71 Exclude_static_libs []string `android:"arch_variant"`
Christopher Ferrisdea66172021-09-24 00:07:53 -070072 Srcs []string `android:"arch_variant"`
73 Header_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070074 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070075
Quallenaugebed769c2020-10-21 21:19:01 +020076 Malloc_not_svelte_libc32 struct {
77 Cflags []string `android:"arch_variant"`
78 Shared_libs []string `android:"arch_variant"`
79 Whole_static_libs []string `android:"arch_variant"`
80 Exclude_static_libs []string `android:"arch_variant"`
81 } `android:"arch_variant"`
82
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070083 Malloc_zero_contents struct {
84 Cflags []string `android:"arch_variant"`
85 } `android:"arch_variant"`
86
87 Malloc_pattern_fill_contents struct {
88 Cflags []string `android:"arch_variant"`
89 } `android:"arch_variant"`
90
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070091 Safestack struct {
92 Cflags []string `android:"arch_variant"`
93 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070094
Dan Willemsen1be35382016-07-25 17:42:18 -070095 Binder32bit struct {
96 Cflags []string
97 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070098
Colin Cross95436422017-05-04 13:57:05 -070099 Override_rs_driver struct {
100 Cflags []string
101 }
102
Steven Morelandc2b9f062017-12-13 14:06:20 -0800103 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
104 // enabled.
105 Treble_linker_namespaces struct {
106 Cflags []string
107 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800108 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
109 Enforce_vintf_manifest struct {
110 Cflags []string
111 }
112
Colin Cross6bc59ef2016-12-08 09:46:35 -0800113 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
114 // debugging features that don't significantly impact runtime behavior. userdebug builds
115 // are used for dogfooding and performance testing, and should be as similar to user builds
116 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700117 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700118 Cflags []string
119 Cppflags []string
120 Init_rc []string
121 Required []string
122 Host_required []string
123 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700124 Strip struct {
125 All *bool
126 Keep_symbols *bool
127 Keep_symbols_and_debug_frame *bool
128 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700129 Static_libs []string
130 Whole_static_libs []string
131 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900132
133 Cmdline []string
Inseob Kimf5ed3002022-01-07 19:17:29 +0900134
Jay Aliomer2599d1d2021-09-23 15:49:54 -0400135 Srcs []string
136 Exclude_srcs []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700137 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800138
Usta Shresthac725f472022-01-11 02:44:21 -0500139 // eng is true for -eng builds, and can be used to turn on additional heavyweight debugging
Colin Cross6bc59ef2016-12-08 09:46:35 -0800140 // features.
141 Eng struct {
142 Cflags []string
143 Cppflags []string
dianlujitaob65e8c72022-02-20 23:57:22 +0800144 Init_rc []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700145 Lto struct {
146 Never *bool
147 }
Colin Cross3273cc22018-10-26 23:58:42 -0700148 Sanitize struct {
149 Address *bool
150 }
Felka Changf13642e2019-06-21 20:58:27 +0800151 Optimize struct {
152 Enabled *bool
153 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800154 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700155
156 Pdk struct {
Colin Cross99967a72017-10-17 13:55:02 -0700157 Enabled *bool `android:"arch_variant"`
158 } `android:"arch_variant"`
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700159
160 Uml struct {
161 Cppflags []string
162 }
Risanea004dd2017-11-27 18:14:46 +0900163
164 Arc struct {
Satoshi Niwa54f48262020-12-18 09:53:26 +0900165 Cflags []string `android:"arch_variant"`
166 Exclude_srcs []string `android:"arch_variant"`
Jasmine Chenacace422021-03-05 00:56:40 +0800167 Header_libs []string `android:"arch_variant"`
Satoshi Niwa54f48262020-12-18 09:53:26 +0900168 Include_dirs []string `android:"arch_variant"`
169 Shared_libs []string `android:"arch_variant"`
170 Static_libs []string `android:"arch_variant"`
171 Srcs []string `android:"arch_variant"`
172 Whole_static_libs []string `android:"arch_variant"`
173 } `android:"arch_variant"`
Roland Levillain2879d412019-08-13 15:00:18 +0100174
175 Flatten_apex struct {
176 Enabled *bool
177 }
Peter Collingbournec66baf72019-10-29 11:00:15 -0700178
Colin Crossd9a121b2020-01-27 13:26:42 -0800179 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800180 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800181 Srcs []string `android:"arch_variant"`
182 Exclude_srcs []string `android:"arch_variant"`
183 } `android:"arch_variant"`
Colin Cross83163192015-12-01 16:31:16 -0800184 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700185}
186
Colin Cross6961a492020-02-06 16:57:45 -0800187var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700188
Colin Cross485e5722015-08-27 13:28:01 -0700189type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700190 // Suffix to add to generated Makefiles
191 Make_suffix *string `json:",omitempty"`
192
Colin Cross2a2e0db2020-02-21 16:55:46 -0800193 BuildId *string `json:",omitempty"`
194 BuildNumberFile *string `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800195
Colin Cross092c9da2019-04-02 22:56:43 -0700196 Platform_version_name *string `json:",omitempty"`
197 Platform_sdk_version *int `json:",omitempty"`
198 Platform_sdk_codename *string `json:",omitempty"`
Yuntao Xu402e9b02021-08-09 15:44:44 -0700199 Platform_sdk_version_or_codename *string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700200 Platform_sdk_final *bool `json:",omitempty"`
Anton Hanssonb021bf82021-08-27 17:35:40 +0100201 Platform_sdk_extension_version *int `json:",omitempty"`
Anton Hansson97d0bae2022-02-16 16:15:10 +0000202 Platform_base_sdk_extension_version *int `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700203 Platform_version_active_codenames []string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700204 Platform_vndk_version *string `json:",omitempty"`
205 Platform_systemsdk_versions []string `json:",omitempty"`
206 Platform_security_patch *string `json:",omitempty"`
207 Platform_preview_sdk_version *string `json:",omitempty"`
208 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
209 Platform_base_os *string `json:",omitempty"`
Inseob Kimc7c47462022-04-25 18:23:58 +0900210 Platform_version_last_stable *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700211
Jeongik Cha219141c2020-08-06 23:00:37 +0900212 DeviceName *string `json:",omitempty"`
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000213 DeviceProduct *string `json:",omitempty"`
Jeongik Cha219141c2020-08-06 23:00:37 +0900214 DeviceArch *string `json:",omitempty"`
215 DeviceArchVariant *string `json:",omitempty"`
216 DeviceCpuVariant *string `json:",omitempty"`
217 DeviceAbi []string `json:",omitempty"`
218 DeviceVndkVersion *string `json:",omitempty"`
219 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
220 DeviceSystemSdkVersions []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700221
Jose Galmes6f843bc2020-12-11 13:36:29 -0800222 RecoverySnapshotVersion *string `json:",omitempty"`
223
Colin Crossa74ca042019-01-31 14:31:51 -0800224 DeviceSecondaryArch *string `json:",omitempty"`
225 DeviceSecondaryArchVariant *string `json:",omitempty"`
226 DeviceSecondaryCpuVariant *string `json:",omitempty"`
227 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700228
dimitry8d6dde82019-07-11 10:23:53 +0200229 NativeBridgeArch *string `json:",omitempty"`
230 NativeBridgeArchVariant *string `json:",omitempty"`
231 NativeBridgeCpuVariant *string `json:",omitempty"`
232 NativeBridgeAbi []string `json:",omitempty"`
233 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100234
dimitry8d6dde82019-07-11 10:23:53 +0200235 NativeBridgeSecondaryArch *string `json:",omitempty"`
236 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
237 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
238 NativeBridgeSecondaryAbi []string `json:",omitempty"`
239 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100240
Colin Cross4225f652015-09-17 14:33:42 -0700241 HostArch *string `json:",omitempty"`
242 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000243 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800244
245 CrossHost *string `json:",omitempty"`
246 CrossHostArch *string `json:",omitempty"`
247 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800248
Jeongik Chacee5ba92021-02-19 12:11:51 +0900249 DeviceResourceOverlays []string `json:",omitempty"`
250 ProductResourceOverlays []string `json:",omitempty"`
251 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800252 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700253
Colin Crossa74ca042019-01-31 14:31:51 -0800254 AAPTCharacteristics *string `json:",omitempty"`
255 AAPTConfig []string `json:",omitempty"`
256 AAPTPreferredConfig *string `json:",omitempty"`
257 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700258
Colin Cross61ae0b72017-12-01 17:16:02 -0800259 DefaultAppCertificate *string `json:",omitempty"`
260
Colin Crossface4e42017-10-30 17:32:15 -0700261 AppsDefaultVersionName *string `json:",omitempty"`
262
Cole Faust701ca252021-11-23 19:02:08 -0800263 Allow_missing_dependencies *bool `json:",omitempty"`
264 Unbundled_build *bool `json:",omitempty"`
265 Unbundled_build_apps []string `json:",omitempty"`
266 Unbundled_build_image *bool `json:",omitempty"`
267 Always_use_prebuilt_sdks *bool `json:",omitempty"`
268 Skip_boot_jars_check *bool `json:",omitempty"`
269 Malloc_not_svelte *bool `json:",omitempty"`
Quallenaugebed769c2020-10-21 21:19:01 +0200270 Malloc_not_svelte_libc32 *bool `json:",omitempty"`
Cole Faust701ca252021-11-23 19:02:08 -0800271 Malloc_zero_contents *bool `json:",omitempty"`
272 Malloc_pattern_fill_contents *bool `json:",omitempty"`
273 Safestack *bool `json:",omitempty"`
274 HostStaticBinaries *bool `json:",omitempty"`
275 Binder32bit *bool `json:",omitempty"`
276 UseGoma *bool `json:",omitempty"`
277 UseRBE *bool `json:",omitempty"`
278 UseRBEJAVAC *bool `json:",omitempty"`
279 UseRBER8 *bool `json:",omitempty"`
280 UseRBED8 *bool `json:",omitempty"`
281 Debuggable *bool `json:",omitempty"`
282 Eng *bool `json:",omitempty"`
283 Treble_linker_namespaces *bool `json:",omitempty"`
284 Enforce_vintf_manifest *bool `json:",omitempty"`
285 Uml *bool `json:",omitempty"`
286 Arc *bool `json:",omitempty"`
287 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800288
Logan Chien4fcea3d2018-11-20 11:59:08 +0800289 Check_elf_files *bool `json:",omitempty"`
290
Colin Cross5a0dcd52018-10-05 14:20:06 -0700291 UncompressPrivAppDex *bool `json:",omitempty"`
292 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800293
satayevd604b212021-07-21 14:23:52 +0100294 BootJars ConfiguredJarList `json:",omitempty"`
295 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000296
Colin Crossa74ca042019-01-31 14:31:51 -0800297 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700298
Colin Crossa74ca042019-01-31 14:31:51 -0800299 EnableCFI *bool `json:",omitempty"`
300 CFIExcludePaths []string `json:",omitempty"`
301 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700302
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800303 DisableScudo *bool `json:",omitempty"`
304
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800305 MemtagHeapExcludePaths []string `json:",omitempty"`
306 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
307 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
308
Justin Yund5f6c822019-06-25 16:47:17 +0900309 VendorPath *string `json:",omitempty"`
310 OdmPath *string `json:",omitempty"`
311 ProductPath *string `json:",omitempty"`
312 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800313
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700314 ClangTidy *bool `json:",omitempty"`
315 TidyChecks *string `json:",omitempty"`
316
Roland Levillainada12702020-06-09 13:07:36 +0100317 JavaCoveragePaths []string `json:",omitempty"`
318 JavaCoverageExcludePaths []string `json:",omitempty"`
319
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800320 GcovCoverage *bool `json:",omitempty"`
321 ClangCoverage *bool `json:",omitempty"`
322 NativeCoveragePaths []string `json:",omitempty"`
323 NativeCoverageExcludePaths []string `json:",omitempty"`
324 ClangCoverageContinuousMode *bool `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800325
Colin Cross1a6acd42020-06-16 17:51:46 -0700326 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400327 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700328
Colin Cross23ae82a2016-11-02 14:34:39 -0700329 SanitizeHost []string `json:",omitempty"`
330 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700331 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700332 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800333
334 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800335
336 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700337
338 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900339
340 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900341
Jan Altensen3bebd4f2020-09-10 15:21:40 +0200342 TargetSpecificHeaderPath *string `json:",omitempty"`
343
Justin Yun71549282017-11-17 12:10:28 +0900344 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800345
346 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800347
Yi Kongeb8efc92021-12-09 18:06:29 +0800348 AfdoAdditionalProfileDirs []string `json:",omitempty"`
349 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700350
Inseob Kimae553032019-05-14 18:52:49 +0900351 VndkUseCoreVariant *bool `json:",omitempty"`
352 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800353
Inseob Kim7cf14652021-01-06 23:06:52 +0900354 DirectedVendorSnapshot bool `json:",omitempty"`
355 VendorSnapshotModules map[string]bool `json:",omitempty"`
356
Jose Galmes4c6895e2021-02-09 07:44:30 -0800357 DirectedRecoverySnapshot bool `json:",omitempty"`
358 RecoverySnapshotModules map[string]bool `json:",omitempty"`
359
Justin DeMartino383bfb32021-02-24 10:49:43 -0800360 VendorSnapshotDirsIncluded []string `json:",omitempty"`
361 VendorSnapshotDirsExcluded []string `json:",omitempty"`
362 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
363 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000364 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800365
Inseob Kim0f46e7c2021-12-15 22:48:14 +0900366 BoardVendorSepolicyDirs []string `json:",omitempty"`
367 BoardOdmSepolicyDirs []string `json:",omitempty"`
368 BoardReqdMaskPolicy []string `json:",omitempty"`
369 BoardPlatVendorPolicy []string `json:",omitempty"`
370 BoardSystemExtPublicPrebuiltDirs []string `json:",omitempty"`
371 BoardSystemExtPrivatePrebuiltDirs []string `json:",omitempty"`
372 BoardProductPublicPrebuiltDirs []string `json:",omitempty"`
373 BoardProductPrivatePrebuiltDirs []string `json:",omitempty"`
374 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
375 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
376 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700377
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900378 BoardSepolicyVers *string `json:",omitempty"`
379 PlatformSepolicyVersion *string `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000380 TotSepolicyVersion *string `json:",omitempty"`
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900381
Inseob Kim1a0afcc2022-02-14 23:10:51 +0900382 SystemExtSepolicyPrebuiltApiDir *string `json:",omitempty"`
383 ProductSepolicyPrebuiltApiDir *string `json:",omitempty"`
384
Inseob Kim843f6642022-01-07 09:11:23 +0900385 PlatformSepolicyCompatVersions []string `json:",omitempty"`
386
Dan Willemsen0fe78662018-03-26 12:41:18 -0700387 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700388
Dan Albertd05ba002021-04-13 15:55:47 -0700389 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900390
Jiyong Park4da07972021-01-05 21:01:11 +0900391 Flatten_apex *bool `json:",omitempty"`
392 ForceApexSymlinkOptimization *bool `json:",omitempty"`
393 CompressedApex *bool `json:",omitempty"`
394 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800395
396 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900397
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900398 WithDexpreopt bool `json:",omitempty"`
399
Jiyong Park7f67f482019-01-05 12:57:48 +0900400 ManifestPackageNameOverrides []string `json:",omitempty"`
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800401 CertificateOverrides []string `json:",omitempty"`
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800402 PackageNameOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900403
Albert Martin55ccba22022-03-21 20:11:16 +0000404 ApexGlobalMinSdkVersionOverride *string `json:",omitempty"`
405
Jeongik Chac9464142019-01-07 12:07:27 +0900406 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700407 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800408
Colin Crossf24a22a2019-01-31 14:12:44 -0800409 ProductHiddenAPIStubs []string `json:",omitempty"`
410 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
411 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700412
Inseob Kim0866b002019-04-15 20:21:29 +0900413 ProductPublicSepolicyDirs []string `json:",omitempty"`
414 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900415
Justin Yun5f7f7e82019-11-18 19:52:14 +0900416 ProductVndkVersion *string `json:",omitempty"`
417
Dan Willemsen54879d12019-04-18 10:08:46 -0700418 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700419
420 MissingUsesLibraries []string `json:",omitempty"`
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900421
422 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900423
JaeMan Parkff715562020-10-19 17:25:58 +0900424 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
425 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
426
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900427 InstallExtraFlattenedApexes *bool `json:",omitempty"`
Yifan Hong82db7352020-01-21 16:12:26 -0800428
429 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700430
Yifan Hong42bef8d2020-08-05 14:36:09 -0700431 BoardKernelBinaries []string `json:",omitempty"`
432 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700433
434 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800435
436 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900437
438 ShippingApiLevel *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900439
Liz Kammer619be462022-01-28 15:13:39 -0500440 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
441 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
442 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
443 BuildBrokenInputDirModules []string `json:",omitempty"`
Inseob Kim67e5add2021-03-17 18:05:33 +0900444
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700445 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
446
Inseob Kim67e5add2021-03-17 18:05:33 +0900447 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
448
449 SelinuxIgnoreNeverallows bool `json:",omitempty"`
450
451 SepolicySplit bool `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000452
453 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
454 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900455
456 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Christopher Ferris3c0ae332022-07-13 23:16:52 -0700457
458 ForceMultilibFirstOnDevice bool `json:",omitempty"`
Spandan Das82747772022-11-08 18:42:16 +0000459
460 IncludeTags []string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700461}
462
463func boolPtr(v bool) *bool {
464 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700465}
466
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700467func intPtr(v int) *int {
468 return &v
469}
470
Colin Cross4225f652015-09-17 14:33:42 -0700471func stringPtr(v string) *string {
472 return &v
473}
474
Colin Cross27385972015-09-18 10:57:10 -0700475func (v *productVariables) SetDefaultConfig() {
476 *v = productVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800477 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700478
Jiyong Park84d8a7e2020-09-25 13:44:51 +0900479 Platform_version_name: stringPtr("S"),
480 Platform_sdk_version: intPtr(30),
481 Platform_sdk_codename: stringPtr("S"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700482 Platform_sdk_final: boolPtr(false),
Jiyong Park84d8a7e2020-09-25 13:44:51 +0900483 Platform_version_active_codenames: []string{"S"},
484 Platform_vndk_version: stringPtr("S"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800485
Colin Cross4225f652015-09-17 14:33:42 -0700486 HostArch: stringPtr("x86_64"),
487 HostSecondaryArch: stringPtr("x86"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800488 DeviceName: stringPtr("generic_arm64"),
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000489 DeviceProduct: stringPtr("aosp_arm-eng"),
Colin Cross4225f652015-09-17 14:33:42 -0700490 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700491 DeviceArchVariant: stringPtr("armv8-a"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800492 DeviceCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800493 DeviceAbi: []string{"arm64-v8a"},
Colin Cross4225f652015-09-17 14:33:42 -0700494 DeviceSecondaryArch: stringPtr("arm"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800495 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
496 DeviceSecondaryCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800497 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
Colin Crossface4e42017-10-30 17:32:15 -0700498
Colin Crossa74ca042019-01-31 14:31:51 -0800499 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700500 AAPTPreferredConfig: stringPtr("xhdpi"),
501 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800502 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700503
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700504 Malloc_not_svelte: boolPtr(true),
Quallenaugebed769c2020-10-21 21:19:01 +0200505 Malloc_not_svelte_libc32: boolPtr(true),
Steven Morelandd1342012020-07-30 20:38:49 +0000506 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700507 Malloc_pattern_fill_contents: boolPtr(false),
508 Safestack: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100509
satayevd604b212021-07-21 14:23:52 +0100510 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
511 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700512 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800513
514 if runtime.GOOS == "linux" {
515 v.CrossHost = stringPtr("windows")
516 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800517 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800518 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700519}
520
Liz Kammera060c452021-03-24 10:14:47 -0400521// ProductConfigContext requires the access to the Module to get product config properties.
522type ProductConfigContext interface {
523 Module() Module
524}
525
526// ProductConfigProperty contains the information for a single property (may be a struct) paired
527// with the appropriate ProductConfigVariable.
528type ProductConfigProperty struct {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000529 // The name of the product variable, e.g. "safestack", "malloc_not_svelte",
530 // "board"
Jingwen Chen25825ca2021-11-15 12:28:43 +0000531 Name string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000532
533 // Namespace of the variable, if this is a soong_config_module_type variable
Jingwen Chen25825ca2021-11-15 12:28:43 +0000534 // e.g. "acme", "ANDROID", "vendor_name"
535 Namespace string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000536
537 // Unique configuration to identify this product config property (i.e. a
538 // primary key), as just using the product variable name is not sufficient.
539 //
540 // For product variables, this is the product variable name + optional
541 // archvariant information. e.g.
542 //
543 // product_variables: {
544 // foo: {
545 // cflags: ["-Dfoo"],
546 // },
547 // },
548 //
549 // FullConfig would be "foo".
550 //
551 // target: {
552 // android: {
553 // product_variables: {
554 // foo: {
555 // cflags: ["-Dfoo-android"],
556 // },
557 // },
558 // },
559 // },
560 //
561 // FullConfig would be "foo-android".
562 //
563 // For soong config variables, this is the namespace + product variable name
564 // + value of the variable, if applicable. The value can also be
565 // conditions_default.
566 //
567 // e.g.
568 //
569 // soong_config_variables: {
570 // feature1: {
571 // conditions_default: {
572 // cflags: ["-DDEFAULT1"],
573 // },
574 // cflags: ["-DFEATURE1"],
575 // },
576 // }
577 //
578 // where feature1 is created in the "acme" namespace, so FullConfig would be
579 // "acme__feature1" and "acme__feature1__conditions_default".
580 //
581 // e.g.
582 //
583 // soong_config_variables: {
584 // board: {
585 // soc_a: {
586 // cflags: ["-DSOC_A"],
587 // },
588 // soc_b: {
589 // cflags: ["-DSOC_B"],
590 // },
591 // soc_c: {},
592 // conditions_default: {
593 // cflags: ["-DSOC_DEFAULT"]
594 // },
595 // },
596 // }
597 //
598 // where board is created in the "acme" namespace, so FullConfig would be
599 // "acme__board__soc_a", "acme__board__soc_b", and
600 // "acme__board__conditions_default"
601 FullConfig string
Liz Kammera060c452021-03-24 10:14:47 -0400602}
603
Jingwen Chen58ff6802021-11-17 12:14:41 +0000604func (p *ProductConfigProperty) AlwaysEmit() bool {
605 return p.Namespace != ""
606}
607
Jingwen Chena47f28d2021-11-02 16:43:57 +0000608func (p *ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
609 if p.Namespace == "" {
610 return bazel.ProductVariableConfigurationAxis(p.FullConfig)
611 } else {
612 // Soong config variables can be uniquely identified by the namespace
613 // (e.g. acme, android) and the product variable name (e.g. board, size)
Jingwen Chen25825ca2021-11-15 12:28:43 +0000614 return bazel.ProductVariableConfigurationAxis(p.Namespace + "__" + p.Name)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000615 }
616}
617
Jingwen Chen25825ca2021-11-15 12:28:43 +0000618// SelectKey returns the literal string that represents this variable in a BUILD
619// select statement.
620func (p *ProductConfigProperty) SelectKey() string {
621 if p.Namespace == "" {
622 return strings.ToLower(p.FullConfig)
623 }
624
625 if p.FullConfig == bazel.ConditionsDefaultConfigKey {
626 return bazel.ConditionsDefaultConfigKey
627 }
628
629 value := p.FullConfig
630 if value == p.Name {
Sam Delmerico0e33c9d2022-01-07 20:39:21 +0000631 value = ""
Jingwen Chen25825ca2021-11-15 12:28:43 +0000632 }
Sam Delmerico0e33c9d2022-01-07 20:39:21 +0000633
634 // e.g. acme__feature1, android__board__soc_a
635 selectKey := strings.ToLower(strings.Join([]string{p.Namespace, p.Name}, "__"))
636 if value != "" {
637 selectKey = strings.ToLower(strings.Join([]string{selectKey, value}, "__"))
638 }
639
640 return selectKey
Jingwen Chen25825ca2021-11-15 12:28:43 +0000641}
642
643// ProductConfigProperties is a map of maps to group property values according
644// their property name and the product config variable they're set under.
645//
646// The outer map key is the name of the property, like "cflags".
647//
648// The inner map key is a ProductConfigProperty, which is a struct of product
649// variable name, namespace, and the "full configuration" of the product
650// variable.
651//
652// e.g. product variable name: board, namespace: acme, full config: vendor_chip_foo
653//
654// The value of the map is the interface{} representing the value of the
655// property, like ["-DDEFINES"] for cflags.
656type ProductConfigProperties map[string]map[ProductConfigProperty]interface{}
Liz Kammera060c452021-03-24 10:14:47 -0400657
658// ProductVariableProperties returns a ProductConfigProperties containing only the properties which
659// have been set for the module in the given context.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000660func ProductVariableProperties(ctx BazelConversionPathContext) ProductConfigProperties {
Liz Kammera060c452021-03-24 10:14:47 -0400661 module := ctx.Module()
662 moduleBase := module.base()
663
664 productConfigProperties := ProductConfigProperties{}
665
Jingwen Chena47f28d2021-11-02 16:43:57 +0000666 if moduleBase.variableProperties != nil {
667 productVariablesProperty := proptools.FieldNameForProperty("product_variables")
668 productVariableValues(
669 productVariablesProperty,
670 moduleBase.variableProperties,
671 "",
672 "",
673 &productConfigProperties)
674
675 for _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
676 for config, props := range configToProps {
677 // GetArchVariantProperties is creating an instance of the requested type
678 // and productVariablesValues expects an interface, so no need to cast
679 productVariableValues(
680 productVariablesProperty,
681 props,
682 "",
683 config,
684 &productConfigProperties)
685 }
686 }
Liz Kammera060c452021-03-24 10:14:47 -0400687 }
688
Jingwen Chena47f28d2021-11-02 16:43:57 +0000689 if m, ok := module.(Bazelable); ok && m.namespacedVariableProps() != nil {
Jingwen Chen84817de2021-11-17 10:57:35 +0000690 for namespace, namespacedVariableProps := range m.namespacedVariableProps() {
691 for _, namespacedVariableProp := range namespacedVariableProps {
692 productVariableValues(
693 soongconfig.SoongConfigProperty,
694 namespacedVariableProp,
695 namespace,
696 "",
697 &productConfigProperties)
698 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400699 }
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400700 }
701
Jingwen Chen58ff6802021-11-17 12:14:41 +0000702 productConfigProperties.zeroValuesForNamespacedVariables()
703
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400704 return productConfigProperties
705}
706
Jingwen Chen58ff6802021-11-17 12:14:41 +0000707// zeroValuesForNamespacedVariables ensures that selects that contain __only__
708// conditions default values have zero values set for the other non-default
709// values for that select statement.
710//
711// If the ProductConfigProperties map contains these items, as parsed from the .bp file:
712//
713// library_linking_strategy: {
714// prefer_static: {
715// static_libs: [
716// "lib_a",
717// "lib_b",
718// ],
719// },
720// conditions_default: {
721// shared_libs: [
722// "lib_a",
723// "lib_b",
724// ],
725// },
726// },
727//
728// Static_libs {Library_linking_strategy ANDROID prefer_static} [lib_a lib_b]
729// Shared_libs {Library_linking_strategy ANDROID conditions_default} [lib_a lib_b]
730//
731// We need to add this:
732//
733// Shared_libs {Library_linking_strategy ANDROID prefer_static} []
734//
735// so that the following gets generated for the "dynamic_deps" attribute,
736// instead of putting lib_a and lib_b directly into dynamic_deps without a
737// select:
738//
739// dynamic_deps = select({
740// "//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
741// "//conditions:default": [
742// "//foo/bar:lib_a",
743// "//foo/bar:lib_b",
744// ],
745// }),
746func (props *ProductConfigProperties) zeroValuesForNamespacedVariables() {
747 // A map of product config properties to the zero values of their respective
748 // property value.
749 zeroValues := make(map[ProductConfigProperty]interface{})
750
751 // A map of prop names (e.g. cflags) to product config properties where the
752 // (prop name, ProductConfigProperty) tuple contains a non-conditions_default key.
753 //
754 // e.g.
755 //
756 // prefer_static: {
757 // static_libs: [
758 // "lib_a",
759 // "lib_b",
760 // ],
761 // },
762 // conditions_default: {
763 // shared_libs: [
764 // "lib_a",
765 // "lib_b",
766 // ],
767 // },
768 //
769 // The tuple of ("static_libs", prefer_static) would be in this map.
770 hasNonDefaultValue := make(map[string]map[ProductConfigProperty]bool)
771
772 // Iterate over all added soong config variables.
773 for propName, v := range *props {
774 for p, intf := range v {
775 if p.Namespace == "" {
776 // If there's no namespace, this isn't a soong config variable,
777 // i.e. this is a product variable. product variables have no
778 // conditions_defaults, so skip them.
779 continue
780 }
781 if p.FullConfig == bazel.ConditionsDefaultConfigKey {
782 // Skip conditions_defaults.
783 continue
784 }
785 if hasNonDefaultValue[propName] == nil {
786 hasNonDefaultValue[propName] = make(map[ProductConfigProperty]bool)
787 hasNonDefaultValue[propName][p] = false
788 }
789 // Create the zero value of the variable.
790 if _, exists := zeroValues[p]; !exists {
791 zeroValue := reflect.Zero(reflect.ValueOf(intf).Type()).Interface()
792 if zeroValue == nil {
793 panic(fmt.Errorf("Expected non-nil zero value for product/config variable %+v\n", intf))
794 }
795 zeroValues[p] = zeroValue
796 }
797 hasNonDefaultValue[propName][p] = true
798 }
799 }
800
801 for propName := range *props {
802 for p, zeroValue := range zeroValues {
803 // Ignore variables that already have a non-default value for that axis
804 if exists, _ := hasNonDefaultValue[propName][p]; !exists {
805 // fmt.Println(propName, p.Namespace, p.Name, p.FullConfig, zeroValue)
806 // Insert the zero value for this propname + product config value.
807 props.AddProductConfigProperty(
808 propName,
809 p.Namespace,
810 p.Name,
811 p.FullConfig,
812 zeroValue,
813 )
814 }
815 }
816 }
817}
818
Jingwen Chena47f28d2021-11-02 16:43:57 +0000819func (p *ProductConfigProperties) AddProductConfigProperty(
820 propertyName, namespace, productVariableName, config string, property interface{}) {
821 if (*p)[propertyName] == nil {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000822 (*p)[propertyName] = make(map[ProductConfigProperty]interface{})
Jingwen Chena47f28d2021-11-02 16:43:57 +0000823 }
824
Jingwen Chen25825ca2021-11-15 12:28:43 +0000825 productConfigProp := ProductConfigProperty{
826 Namespace: namespace, // e.g. acme, android
827 Name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
828 FullConfig: config, // e.g. size, feature1-x86, size__conditions_default
Jingwen Chena47f28d2021-11-02 16:43:57 +0000829 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000830
Jingwen Chen84817de2021-11-17 10:57:35 +0000831 if existing, ok := (*p)[propertyName][productConfigProp]; ok && namespace != "" {
832 switch dst := existing.(type) {
833 case []string:
834 if src, ok := property.([]string); ok {
835 dst = append(dst, src...)
836 (*p)[propertyName][productConfigProp] = dst
837 }
838 default:
Jingwen Chen58ff6802021-11-17 12:14:41 +0000839 panic(fmt.Errorf("TODO: handle merging value %s", existing))
Jingwen Chen84817de2021-11-17 10:57:35 +0000840 }
841 } else {
842 (*p)[propertyName][productConfigProp] = property
843 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000844}
845
846var (
847 conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
848)
849
850// maybeExtractConfigVarProp attempts to read this value as a config var struct
851// wrapped by interfaces and ptrs. If it's not the right type, the second return
852// value is false.
853func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
854 if v.Kind() == reflect.Interface {
855 // The conditions_default value can be either
856 // 1) an ptr to an interface of a struct (bool config variables and product variables)
857 // 2) an interface of 1) (config variables with nested structs, like string vars)
858 v = v.Elem()
859 }
860 if v.Kind() != reflect.Ptr {
861 return v, false
862 }
863 v = reflect.Indirect(v)
864 if v.Kind() == reflect.Interface {
865 // Extract the struct from the interface
866 v = v.Elem()
867 }
868
869 if !v.IsValid() {
870 return v, false
871 }
872
873 if v.Kind() != reflect.Struct {
874 return v, false
875 }
876 return v, true
877}
878
Jingwen Chen84817de2021-11-17 10:57:35 +0000879func (productConfigProperties *ProductConfigProperties) AddProductConfigProperties(namespace, suffix string, variableValues reflect.Value) {
880 // variableValues can either be a product_variables or
881 // soong_config_variables struct.
882 //
Jingwen Chena47f28d2021-11-02 16:43:57 +0000883 // Example of product_variables:
884 //
885 // product_variables: {
886 // malloc_not_svelte: {
887 // shared_libs: ["malloc_not_svelte_shared_lib"],
888 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
889 // exclude_static_libs: [
890 // "malloc_not_svelte_static_lib_excludes",
891 // "malloc_not_svelte_whole_static_lib_excludes",
892 // ],
893 // },
894 // },
895 //
896 // Example of soong_config_variables:
897 //
898 // soong_config_variables: {
899 // feature1: {
900 // conditions_default: {
901 // ...
902 // },
903 // cflags: ...
904 // },
905 // feature2: {
906 // cflags: ...
907 // conditions_default: {
908 // ...
909 // },
910 // },
911 // board: {
912 // soc_a: {
913 // ...
914 // },
915 // soc_a: {
916 // ...
917 // },
918 // soc_c: {},
919 // conditions_default: {
920 // ...
921 // },
922 // },
923 // }
Liz Kammera060c452021-03-24 10:14:47 -0400924 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000925 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
926 productVariableName := variableValues.Type().Field(i).Name
927
Liz Kammera060c452021-03-24 10:14:47 -0400928 variableValue := variableValues.Field(i)
929 // Check if any properties were set for the module
930 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000931 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400932 continue
933 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000934
935 // Unlike product variables, config variables require a few more
936 // indirections to extract the struct from the reflect.Value.
937 if v, ok := maybeExtractConfigVarProp(variableValue); ok {
938 variableValue = v
939 }
940
Liz Kammera060c452021-03-24 10:14:47 -0400941 for j := 0; j < variableValue.NumField(); j++ {
942 property := variableValue.Field(j)
943 // If the property wasn't set, no need to pass it along
944 if property.IsZero() {
945 continue
946 }
947
948 // e.g. Asflags, Cflags, Enabled, etc.
949 propertyName := variableValue.Type().Field(j).Name
Jingwen Chena47f28d2021-11-02 16:43:57 +0000950
951 if v, ok := maybeExtractConfigVarProp(property); ok {
952 // The field is a struct, which is used by:
953 // 1) soong_config_string_variables
954 //
955 // soc_a: {
956 // cflags: ...,
957 // }
958 //
959 // soc_b: {
960 // cflags: ...,
961 // }
962 //
963 // 2) conditions_default structs for all soong config variable types.
964 //
965 // conditions_default: {
966 // cflags: ...,
967 // static_libs: ...
968 // }
969 field := v
970 for k := 0; k < field.NumField(); k++ {
971 // Iterate over fields of this struct prop.
972 if field.Field(k).IsZero() {
973 continue
974 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000975 // config can also be "conditions_default".
976 config := proptools.PropertyNameForField(propertyName)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000977 actualPropertyName := field.Type().Field(k).Name
978
979 productConfigProperties.AddProductConfigProperty(
980 actualPropertyName, // e.g. cflags, static_libs
981 namespace, // e.g. acme, android
982 productVariableName, // e.g. size, feature1, FEATURE2, board
983 config,
984 field.Field(k).Interface(), // e.g. ["-DDEFAULT"], ["foo", "bar"]
985 )
986 }
Jingwen Chen58ff6802021-11-17 12:14:41 +0000987 } else if property.Kind() != reflect.Interface {
988 // If not an interface, then this is not a conditions_default or
989 // a struct prop. That is, this is a regular product variable,
990 // or a bool/value config variable.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000991 config := productVariableName + suffix
Jingwen Chena47f28d2021-11-02 16:43:57 +0000992 productConfigProperties.AddProductConfigProperty(
993 propertyName,
994 namespace,
995 productVariableName,
996 config,
997 property.Interface(),
998 )
Liz Kammer47535c52021-06-02 16:02:22 -0400999 }
Liz Kammera060c452021-03-24 10:14:47 -04001000 }
1001 }
Liz Kammera060c452021-03-24 10:14:47 -04001002}
1003
Jingwen Chen84817de2021-11-17 10:57:35 +00001004// productVariableValues uses reflection to convert a property struct for
1005// product_variables and soong_config_variables to structs that can be generated
1006// as select statements.
1007func productVariableValues(
1008 fieldName string, variableProps interface{}, namespace, suffix string, productConfigProperties *ProductConfigProperties) {
1009 if suffix != "" {
1010 suffix = "-" + suffix
1011 }
1012
1013 // variableValues represent the product_variables or soong_config_variables struct.
1014 variableValues := reflect.ValueOf(variableProps).Elem().FieldByName(fieldName)
1015 productConfigProperties.AddProductConfigProperties(namespace, suffix, variableValues)
1016}
1017
Colin Crosseabaedd2020-02-06 17:01:55 -08001018func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001019 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -07001020 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -07001021 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001022 return
1023 }
1024
1025 // TODO: depend on config variable, create variants, propagate variants up tree
1026 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -07001027
1028 if a.variableProperties == nil {
1029 return
1030 }
1031
1032 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001033
Colin Cross0cec3122021-01-20 11:27:32 -08001034 productVariables := reflect.ValueOf(mctx.Config().productVariables)
1035
Colin Cross7f64b6d2015-07-09 13:57:48 -07001036 for i := 0; i < variableValues.NumField(); i++ {
1037 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -07001038 name := variableValues.Type().Field(i).Name
1039 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001040
Colin Crossa6bc19e2015-09-16 13:53:42 -07001041 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -08001042 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -07001043 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
1044 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -07001045 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001046
1047 val = val.Elem()
1048
1049 // For bools, check that the value is true
1050 if val.Kind() == reflect.Bool && val.Bool() == false {
1051 continue
1052 }
1053
1054 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -08001055 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -07001056 continue
1057 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001058 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -07001059 }
1060}
1061
Colin Cross4157e882019-06-06 16:57:04 -07001062func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -07001063 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
1064
Colin Cross7e0eaf12017-05-05 16:16:24 -07001065 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001066
Usta851a3272022-01-05 23:42:33 -05001067 err := proptools.AppendMatchingProperties(m.GetProperties(),
Colin Cross06a931b2015-10-28 17:23:31 -07001068 productVariablePropertyValue.Addr().Interface(), nil)
1069 if err != nil {
1070 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1071 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1072 } else {
1073 panic(err)
1074 }
1075 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001076}
1077
Colin Cross7e0eaf12017-05-05 16:16:24 -07001078func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
1079 productVariablePropertyValue reflect.Value, i int, err error) {
1080
1081 field := productVariablePropertyValue.Type().Field(i).Name
1082 property := prefix + "." + proptools.PropertyNameForField(field)
1083 ctx.PropertyErrorf(property, "%s", err)
1084}
1085
1086func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
1087 productVariablePropertyValue reflect.Value, variableValue interface{}) {
1088
Colin Cross7f64b6d2015-07-09 13:57:48 -07001089 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
1090 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -08001091 kind := propertyValue.Kind()
1092 if kind == reflect.Ptr {
1093 if propertyValue.IsNil() {
1094 continue
1095 }
1096 propertyValue = propertyValue.Elem()
1097 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001098 switch propertyValue.Kind() {
1099 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001100 err := printfIntoProperty(propertyValue, variableValue)
1101 if err != nil {
1102 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1103 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001104 case reflect.Slice:
1105 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001106 err := printfIntoProperty(propertyValue.Index(j), variableValue)
1107 if err != nil {
1108 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1109 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001110 }
Colin Crossdd0dbe62015-12-02 15:24:38 -08001111 case reflect.Bool:
1112 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001113 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001114 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001115 default:
1116 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
1117 }
1118 }
1119}
1120
Colin Cross7e0eaf12017-05-05 16:16:24 -07001121func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001122 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -07001123
1124 count := strings.Count(s, "%")
1125 if count == 0 {
1126 return nil
1127 }
1128
1129 if count > 1 {
1130 return fmt.Errorf("product variable properties only support a single '%%'")
1131 }
1132
Colin Cross7f64b6d2015-07-09 13:57:48 -07001133 if strings.Contains(s, "%d") {
1134 switch v := variableValue.(type) {
1135 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001136 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001137 case bool:
1138 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001139 variableValue = 1
1140 } else {
1141 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -07001142 }
1143 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001144 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001145 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001146 } else if strings.Contains(s, "%s") {
1147 switch variableValue.(type) {
1148 case string:
1149 // Nothing
1150 default:
1151 return fmt.Errorf("unsupported type %T for %%s", variableValue)
1152 }
1153 } else {
1154 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001155 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001156
1157 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
1158
1159 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -07001160}
Colin Cross18c46802019-09-24 22:19:02 -07001161
1162var variablePropTypeMap OncePer
1163
1164// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
1165// reflect.Types of each property struct. The result can be used as a key in a map.
1166func sliceToTypeArray(s []interface{}) interface{} {
1167 // Create an array using reflection whose length is the length of the input slice
1168 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
1169 for i, e := range s {
1170 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
1171 }
1172 return ret.Interface()
1173}
1174
Colin Crosseabaedd2020-02-06 17:01:55 -08001175func initProductVariableModule(m Module) {
1176 base := m.base()
1177
1178 // Allow tests to override the default product variables
1179 if base.variableProperties == nil {
1180 base.variableProperties = defaultProductVariables
1181 }
1182 // Filter the product variables properties to the ones that exist on this module
1183 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
1184 if base.variableProperties != nil {
1185 m.AddProperties(base.variableProperties)
1186 }
1187}
1188
Colin Cross18c46802019-09-24 22:19:02 -07001189// createVariableProperties takes the list of property structs for a module and returns a property struct that
1190// contains the product variable properties that exist in the property structs, or nil if there are none. It
1191// caches the result.
1192func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
1193 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
1194 key := sliceToTypeArray(moduleTypeProps)
1195
1196 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
1197 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
1198 // Compute the filtered property struct type.
1199 return createVariablePropertiesType(moduleTypeProps, productVariables)
1200 }).(reflect.Type)
1201
1202 if typ == nil {
1203 return nil
1204 }
1205
1206 // Create a new pointer to a filtered property struct.
1207 return reflect.New(typ).Interface()
1208}
1209
1210// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1211// a list of property structs.
1212func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1213 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1214 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1215 // Filter function, returns true if the field should be in the resulting struct
1216 if prefix == "" {
1217 // Keep the top level Product_variables field
1218 return true, field
1219 }
1220 _, rest := splitPrefix(prefix)
1221 if rest == "" {
1222 // Keep the 2nd level field (i.e. Product_variables.Eng)
1223 return true, field
1224 }
1225
1226 // Strip off the first 2 levels of the prefix
1227 _, prefix = splitPrefix(rest)
1228
1229 for _, p := range moduleTypeProps {
1230 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1231 // Keep any fields that exist in one of the property structs
1232 return true, field
1233 }
1234 }
1235
1236 return false, field
1237 })
1238 return typ
1239}
1240
1241func splitPrefix(prefix string) (first, rest string) {
1242 index := strings.IndexByte(prefix, '.')
1243 if index == -1 {
1244 return prefix, ""
1245 }
1246 return prefix[:index], prefix[index+1:]
1247}
1248
1249func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1250 if t.Kind() != reflect.Struct {
1251 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1252 }
1253
1254 if prefix != "" {
1255 split := strings.SplitN(prefix, ".", 2)
1256 firstPrefix := split[0]
1257 rest := ""
1258 if len(split) > 1 {
1259 rest = split[1]
1260 }
1261 f, exists := t.FieldByName(firstPrefix)
1262 if !exists {
1263 return false
1264 }
1265 ft := f.Type
1266 if ft.Kind() == reflect.Ptr {
1267 ft = ft.Elem()
1268 }
1269 if ft.Kind() != reflect.Struct {
1270 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1271 }
1272 return fieldExistsByNameRecursive(ft, rest, name)
1273 } else {
1274 _, exists := t.FieldByName(name)
1275 return exists
1276 }
1277}