blob: 7f68e623a202366e79925c158383d0018d695d98 [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/bazel"
24
Colin Cross7f64b6d2015-07-09 13:57:48 -070025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
Paul Duffine7a055c2021-03-07 15:46:33 +000029 registerVariableBuildComponents(InitRegistrationContext)
30}
31
32func registerVariableBuildComponents(ctx RegistrationContext) {
33 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
Colin Crosseabaedd2020-02-06 17:01:55 -080034 ctx.BottomUp("variable", VariableMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070035 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070036}
37
Paul Duffine7a055c2021-03-07 15:46:33 +000038var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
39
Colin Cross7f64b6d2015-07-09 13:57:48 -070040type variableProperties struct {
41 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070042 Platform_sdk_version struct {
43 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070044 Cflags []string
Dan Willemsen9fe14102021-07-13 21:52:04 -070045 Cmd *string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070046 }
Colin Cross83163192015-12-01 16:31:16 -080047
Yuntao Xu402e9b02021-08-09 15:44:44 -070048 Platform_sdk_version_or_codename struct {
49 Java_resource_dirs []string
50 }
51
Anton Hanssonb021bf82021-08-27 17:35:40 +010052 Platform_sdk_extension_version struct {
53 Cmd *string
54 }
55
Dan Willemsen9fe14102021-07-13 21:52:04 -070056 Platform_version_name struct {
57 Base_dir *string
58 }
59
Colin Cross83163192015-12-01 16:31:16 -080060 // unbundled_build is a catch-all property to annotate modules that don't build in one or
61 // more unbundled branches, usually due to dependencies missing from the manifest.
62 Unbundled_build struct {
63 Enabled *bool `android:"arch_variant"`
64 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080065
Jeongik Chaf08e59c2023-02-09 10:47:59 +090066 // similar to `Unbundled_build`, but `Always_use_prebuilt_sdks` means that it uses prebuilt
67 // sdk specifically.
68 Always_use_prebuilt_sdks struct {
69 Enabled *bool `android:"arch_variant"`
70 } `android:"arch_variant"`
71
Dan Willemsen97cae012016-03-01 14:08:42 -080072 Malloc_not_svelte struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080073 Cflags []string `android:"arch_variant"`
74 Shared_libs []string `android:"arch_variant"`
75 Whole_static_libs []string `android:"arch_variant"`
Florian Mayerca2854e2024-01-16 16:42:34 -080076 Static_libs []string `android:"arch_variant"`
Christopher Ferrisc71193a2019-12-16 09:55:10 -080077 Exclude_static_libs []string `android:"arch_variant"`
Christopher Ferrisdea66172021-09-24 00:07:53 -070078 Srcs []string `android:"arch_variant"`
79 Header_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070080 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070081
Quallenauge55ba6ec2020-10-21 21:19:01 +020082 Malloc_not_svelte_libc32 struct {
83 Cflags []string `android:"arch_variant"`
84 Shared_libs []string `android:"arch_variant"`
85 Whole_static_libs []string `android:"arch_variant"`
86 Exclude_static_libs []string `android:"arch_variant"`
87 } `android:"arch_variant"`
88
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070089 Malloc_zero_contents struct {
90 Cflags []string `android:"arch_variant"`
91 } `android:"arch_variant"`
92
93 Malloc_pattern_fill_contents struct {
94 Cflags []string `android:"arch_variant"`
95 } `android:"arch_variant"`
96
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070097 Safestack struct {
98 Cflags []string `android:"arch_variant"`
99 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -0700100
Dan Willemsen1be35382016-07-25 17:42:18 -0700101 Binder32bit struct {
102 Cflags []string
103 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700104
Colin Cross95436422017-05-04 13:57:05 -0700105 Override_rs_driver struct {
106 Cflags []string
107 }
108
Steven Morelandc2b9f062017-12-13 14:06:20 -0800109 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
110 // enabled.
111 Treble_linker_namespaces struct {
112 Cflags []string
113 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800114 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
115 Enforce_vintf_manifest struct {
116 Cflags []string
117 }
118
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000119 Build_from_text_stub struct {
120 Static_libs []string
121 Exclude_static_libs []string
122 }
123
Colin Cross6bc59ef2016-12-08 09:46:35 -0800124 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
125 // debugging features that don't significantly impact runtime behavior. userdebug builds
126 // are used for dogfooding and performance testing, and should be as similar to user builds
127 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700128 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700129 Cflags []string
130 Cppflags []string
131 Init_rc []string
132 Required []string
133 Host_required []string
134 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700135 Strip struct {
136 All *bool
137 Keep_symbols *bool
138 Keep_symbols_and_debug_frame *bool
139 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700140 Static_libs []string
141 Whole_static_libs []string
142 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900143
144 Cmdline []string
Inseob Kimf5ed3002022-01-07 19:17:29 +0900145
Jay Aliomer2599d1d2021-09-23 15:49:54 -0400146 Srcs []string
147 Exclude_srcs []string
Cole Fausted940002023-08-15 11:59:24 -0700148 Cmd *string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700149 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800150
Usta Shresthac725f472022-01-11 02:44:21 -0500151 // eng is true for -eng builds, and can be used to turn on additional heavyweight debugging
Colin Cross6bc59ef2016-12-08 09:46:35 -0800152 // features.
153 Eng struct {
154 Cflags []string
155 Cppflags []string
dianlujitao532e5992022-02-20 23:57:22 +0800156 Init_rc []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700157 Lto struct {
158 Never *bool
159 }
Colin Cross3273cc22018-10-26 23:58:42 -0700160 Sanitize struct {
161 Address *bool
162 }
Felka Changf13642e2019-06-21 20:58:27 +0800163 Optimize struct {
164 Enabled *bool
165 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800166 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700167
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700168 Uml struct {
169 Cppflags []string
170 }
Risanea004dd2017-11-27 18:14:46 +0900171
172 Arc struct {
Satoshi Niwa54f48262020-12-18 09:53:26 +0900173 Cflags []string `android:"arch_variant"`
174 Exclude_srcs []string `android:"arch_variant"`
Jasmine Chenacace422021-03-05 00:56:40 +0800175 Header_libs []string `android:"arch_variant"`
Satoshi Niwa54f48262020-12-18 09:53:26 +0900176 Include_dirs []string `android:"arch_variant"`
177 Shared_libs []string `android:"arch_variant"`
178 Static_libs []string `android:"arch_variant"`
179 Srcs []string `android:"arch_variant"`
180 Whole_static_libs []string `android:"arch_variant"`
181 } `android:"arch_variant"`
Roland Levillain2879d412019-08-13 15:00:18 +0100182
Colin Crossd9a121b2020-01-27 13:26:42 -0800183 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800184 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800185 Srcs []string `android:"arch_variant"`
186 Exclude_srcs []string `android:"arch_variant"`
187 } `android:"arch_variant"`
Devin Moorec381e102023-07-06 22:03:58 +0000188
189 // release_aidl_use_unfrozen is "true" when a device can
190 // use the unfrozen versions of AIDL interfaces.
191 Release_aidl_use_unfrozen struct {
Devin Mooreb93c1a72024-02-28 00:50:23 +0000192 Cflags []string
193 Cmd *string
194 Vintf_fragments []string
Devin Moorec381e102023-07-06 22:03:58 +0000195 }
Colin Cross83163192015-12-01 16:31:16 -0800196 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700197}
198
Colin Cross6961a492020-02-06 16:57:45 -0800199var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700200
Cole Faustf8231dd2023-04-21 17:37:11 -0700201type ProductVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700202 // Suffix to add to generated Makefiles
203 Make_suffix *string `json:",omitempty"`
204
Colin Cross2a2e0db2020-02-21 16:55:46 -0800205 BuildId *string `json:",omitempty"`
206 BuildNumberFile *string `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800207
Colin Cross092c9da2019-04-02 22:56:43 -0700208 Platform_version_name *string `json:",omitempty"`
209 Platform_sdk_version *int `json:",omitempty"`
210 Platform_sdk_codename *string `json:",omitempty"`
Yuntao Xu402e9b02021-08-09 15:44:44 -0700211 Platform_sdk_version_or_codename *string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700212 Platform_sdk_final *bool `json:",omitempty"`
Anton Hanssonb021bf82021-08-27 17:35:40 +0100213 Platform_sdk_extension_version *int `json:",omitempty"`
Anton Hansson97d0bae2022-02-16 16:15:10 +0000214 Platform_base_sdk_extension_version *int `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700215 Platform_version_active_codenames []string `json:",omitempty"`
Dan Albert8c7a9942023-03-27 20:34:01 +0000216 Platform_version_all_preview_codenames []string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700217 Platform_vndk_version *string `json:",omitempty"`
218 Platform_systemsdk_versions []string `json:",omitempty"`
219 Platform_security_patch *string `json:",omitempty"`
220 Platform_preview_sdk_version *string `json:",omitempty"`
221 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
222 Platform_base_os *string `json:",omitempty"`
Inseob Kim4f1f3d92022-04-25 18:23:58 +0900223 Platform_version_last_stable *string `json:",omitempty"`
Jiyong Park37073842022-06-21 10:13:42 +0900224 Platform_version_known_codenames *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700225
Jeongik Cha219141c2020-08-06 23:00:37 +0900226 DeviceName *string `json:",omitempty"`
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000227 DeviceProduct *string `json:",omitempty"`
Jeongik Cha219141c2020-08-06 23:00:37 +0900228 DeviceArch *string `json:",omitempty"`
229 DeviceArchVariant *string `json:",omitempty"`
230 DeviceCpuVariant *string `json:",omitempty"`
231 DeviceAbi []string `json:",omitempty"`
232 DeviceVndkVersion *string `json:",omitempty"`
233 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
234 DeviceSystemSdkVersions []string `json:",omitempty"`
Juan Yescase6979a12023-04-07 10:35:35 -0700235 DeviceMaxPageSizeSupported *string `json:",omitempty"`
Vilas Bhatb3d2d222023-12-04 22:51:20 +0000236 DeviceNoBionicPageSizeMacro *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700237
Justin Yun41cbb5e2023-11-29 17:58:16 +0900238 VendorApiLevel *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700239
Jose Galmes6f843bc2020-12-11 13:36:29 -0800240 RecoverySnapshotVersion *string `json:",omitempty"`
241
Colin Crossa74ca042019-01-31 14:31:51 -0800242 DeviceSecondaryArch *string `json:",omitempty"`
243 DeviceSecondaryArchVariant *string `json:",omitempty"`
244 DeviceSecondaryCpuVariant *string `json:",omitempty"`
245 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700246
dimitry8d6dde82019-07-11 10:23:53 +0200247 NativeBridgeArch *string `json:",omitempty"`
248 NativeBridgeArchVariant *string `json:",omitempty"`
249 NativeBridgeCpuVariant *string `json:",omitempty"`
250 NativeBridgeAbi []string `json:",omitempty"`
251 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100252
dimitry8d6dde82019-07-11 10:23:53 +0200253 NativeBridgeSecondaryArch *string `json:",omitempty"`
254 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
255 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
256 NativeBridgeSecondaryAbi []string `json:",omitempty"`
257 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100258
Colin Cross4225f652015-09-17 14:33:42 -0700259 HostArch *string `json:",omitempty"`
260 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000261 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800262
263 CrossHost *string `json:",omitempty"`
264 CrossHostArch *string `json:",omitempty"`
265 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800266
Jeongik Chacee5ba92021-02-19 12:11:51 +0900267 DeviceResourceOverlays []string `json:",omitempty"`
268 ProductResourceOverlays []string `json:",omitempty"`
269 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800270 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700271
Colin Crossa74ca042019-01-31 14:31:51 -0800272 AAPTCharacteristics *string `json:",omitempty"`
273 AAPTConfig []string `json:",omitempty"`
274 AAPTPreferredConfig *string `json:",omitempty"`
275 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700276
Inseob Kim80fa7982022-08-12 21:36:25 +0900277 DefaultAppCertificate *string `json:",omitempty"`
278 MainlineSepolicyDevCertificates *string `json:",omitempty"`
Colin Cross61ae0b72017-12-01 17:16:02 -0800279
Colin Crossface4e42017-10-30 17:32:15 -0700280 AppsDefaultVersionName *string `json:",omitempty"`
281
Cole Faust701ca252021-11-23 19:02:08 -0800282 Allow_missing_dependencies *bool `json:",omitempty"`
283 Unbundled_build *bool `json:",omitempty"`
284 Unbundled_build_apps []string `json:",omitempty"`
285 Unbundled_build_image *bool `json:",omitempty"`
286 Always_use_prebuilt_sdks *bool `json:",omitempty"`
287 Skip_boot_jars_check *bool `json:",omitempty"`
288 Malloc_not_svelte *bool `json:",omitempty"`
Quallenauge55ba6ec2020-10-21 21:19:01 +0200289 Malloc_not_svelte_libc32 *bool `json:",omitempty"`
Cole Faust701ca252021-11-23 19:02:08 -0800290 Malloc_zero_contents *bool `json:",omitempty"`
291 Malloc_pattern_fill_contents *bool `json:",omitempty"`
292 Safestack *bool `json:",omitempty"`
293 HostStaticBinaries *bool `json:",omitempty"`
294 Binder32bit *bool `json:",omitempty"`
295 UseGoma *bool `json:",omitempty"`
296 UseRBE *bool `json:",omitempty"`
297 UseRBEJAVAC *bool `json:",omitempty"`
298 UseRBER8 *bool `json:",omitempty"`
299 UseRBED8 *bool `json:",omitempty"`
300 Debuggable *bool `json:",omitempty"`
301 Eng *bool `json:",omitempty"`
302 Treble_linker_namespaces *bool `json:",omitempty"`
303 Enforce_vintf_manifest *bool `json:",omitempty"`
304 Uml *bool `json:",omitempty"`
305 Arc *bool `json:",omitempty"`
306 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000307 Build_from_text_stub *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800308
Logan Chien4fcea3d2018-11-20 11:59:08 +0800309 Check_elf_files *bool `json:",omitempty"`
310
Colin Cross5a0dcd52018-10-05 14:20:06 -0700311 UncompressPrivAppDex *bool `json:",omitempty"`
312 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800313
satayevd604b212021-07-21 14:23:52 +0100314 BootJars ConfiguredJarList `json:",omitempty"`
315 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000316
Colin Crossa74ca042019-01-31 14:31:51 -0800317 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700318
Colin Crossa74ca042019-01-31 14:31:51 -0800319 EnableCFI *bool `json:",omitempty"`
320 CFIExcludePaths []string `json:",omitempty"`
321 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700322
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800323 DisableScudo *bool `json:",omitempty"`
324
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800325 MemtagHeapExcludePaths []string `json:",omitempty"`
326 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
327 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
328
Hang Lu88086622023-03-17 13:17:22 +0800329 HWASanIncludePaths []string `json:",omitempty"`
Tomislav Novakf734f002023-08-22 10:51:44 -0700330 HWASanExcludePaths []string `json:",omitempty"`
Hang Lu88086622023-03-17 13:17:22 +0800331
Justin Yund5f6c822019-06-25 16:47:17 +0900332 VendorPath *string `json:",omitempty"`
333 OdmPath *string `json:",omitempty"`
334 ProductPath *string `json:",omitempty"`
335 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800336
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700337 ClangTidy *bool `json:",omitempty"`
338 TidyChecks *string `json:",omitempty"`
339
Roland Levillainada12702020-06-09 13:07:36 +0100340 JavaCoveragePaths []string `json:",omitempty"`
341 JavaCoverageExcludePaths []string `json:",omitempty"`
342
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800343 GcovCoverage *bool `json:",omitempty"`
344 ClangCoverage *bool `json:",omitempty"`
345 NativeCoveragePaths []string `json:",omitempty"`
346 NativeCoverageExcludePaths []string `json:",omitempty"`
347 ClangCoverageContinuousMode *bool `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800348
Colin Cross1a6acd42020-06-16 17:51:46 -0700349 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400350 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700351
Colin Cross23ae82a2016-11-02 14:34:39 -0700352 SanitizeHost []string `json:",omitempty"`
353 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700354 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700355 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800356
357 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800358
359 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700360
361 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900362
363 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900364
Jan Altensen364f1802020-09-10 15:21:40 +0200365 TargetSpecificHeaderPath *string `json:",omitempty"`
366
Justin Yun71549282017-11-17 12:10:28 +0900367 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800368
369 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800370
Vinh Tran7a8362c2022-11-01 15:33:51 -0400371 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700372
Inseob Kimae553032019-05-14 18:52:49 +0900373 VndkUseCoreVariant *bool `json:",omitempty"`
374 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800375
Inseob Kim7cf14652021-01-06 23:06:52 +0900376 DirectedVendorSnapshot bool `json:",omitempty"`
377 VendorSnapshotModules map[string]bool `json:",omitempty"`
378
Jose Galmes4c6895e2021-02-09 07:44:30 -0800379 DirectedRecoverySnapshot bool `json:",omitempty"`
380 RecoverySnapshotModules map[string]bool `json:",omitempty"`
381
Justin DeMartino383bfb32021-02-24 10:49:43 -0800382 VendorSnapshotDirsIncluded []string `json:",omitempty"`
383 VendorSnapshotDirsExcluded []string `json:",omitempty"`
384 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
385 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000386 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800387
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900388 MultitreeUpdateMeta bool `json:",omitempty"`
389
Inseob Kim6077b232023-08-31 16:49:59 +0900390 BoardVendorSepolicyDirs []string `json:",omitempty"`
391 BoardOdmSepolicyDirs []string `json:",omitempty"`
392 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
393 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
394 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700395
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900396 BoardSepolicyVers *string `json:",omitempty"`
397 PlatformSepolicyVersion *string `json:",omitempty"`
398
Inseob Kim1a0afcc2022-02-14 23:10:51 +0900399 SystemExtSepolicyPrebuiltApiDir *string `json:",omitempty"`
400 ProductSepolicyPrebuiltApiDir *string `json:",omitempty"`
401
Inseob Kim843f6642022-01-07 09:11:23 +0900402 PlatformSepolicyCompatVersions []string `json:",omitempty"`
403
Dan Willemsen0fe78662018-03-26 12:41:18 -0700404 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700405
Dan Albertd05ba002021-04-13 15:55:47 -0700406 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900407
Dennis Shene2ed70c2023-01-11 14:15:43 +0000408 TrimmedApex *bool `json:",omitempty"`
Jiyong Park4da07972021-01-05 21:01:11 +0900409 ForceApexSymlinkOptimization *bool `json:",omitempty"`
410 CompressedApex *bool `json:",omitempty"`
411 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800412
413 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900414
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900415 WithDexpreopt bool `json:",omitempty"`
416
Jiakai Zhang4d90da22023-07-12 16:51:48 +0100417 ManifestPackageNameOverrides []string `json:",omitempty"`
418 CertificateOverrides []string `json:",omitempty"`
419 PackageNameOverrides []string `json:",omitempty"`
420 ConfiguredJarLocationOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900421
Albert Martineefabcf2022-03-21 20:11:16 +0000422 ApexGlobalMinSdkVersionOverride *string `json:",omitempty"`
423
Jeongik Chac9464142019-01-07 12:07:27 +0900424 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700425 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800426
Colin Crossf24a22a2019-01-31 14:12:44 -0800427 ProductHiddenAPIStubs []string `json:",omitempty"`
428 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
429 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700430
Inseob Kim0866b002019-04-15 20:21:29 +0900431 ProductPublicSepolicyDirs []string `json:",omitempty"`
432 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900433
Dan Willemsen54879d12019-04-18 10:08:46 -0700434 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700435
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900436 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900437
JaeMan Parkff715562020-10-19 17:25:58 +0900438 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
439 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
440
Yifan Hong82db7352020-01-21 16:12:26 -0800441 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700442
Yifan Hong42bef8d2020-08-05 14:36:09 -0700443 BoardKernelBinaries []string `json:",omitempty"`
444 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700445
446 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800447
448 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900449
450 ShippingApiLevel *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900451
Cole Faustded79602023-09-05 17:48:11 -0700452 BuildBrokenPluginValidation []string `json:",omitempty"`
453 BuildBrokenClangAsFlags bool `json:",omitempty"`
454 BuildBrokenClangCFlags bool `json:",omitempty"`
455 BuildBrokenClangProperty bool `json:",omitempty"`
456 GenruleSandboxing *bool `json:",omitempty"`
457 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
458 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
459 BuildBrokenUsesSoongPython2Modules bool `json:",omitempty"`
460 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
461 BuildBrokenIncorrectPartitionImages bool `json:",omitempty"`
462 BuildBrokenInputDirModules []string `json:",omitempty"`
Jiyong Park3bb99242024-01-04 23:21:26 +0000463 BuildBrokenDontCheckSystemSdk bool `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900464
Jiakai Zhang48203e32023-06-02 23:42:21 +0100465 BuildWarningBadOptionalUsesLibsAllowlist []string `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900466
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700467 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
468
Inseob Kim67e5add192021-03-17 18:05:33 +0900469 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
470
471 SelinuxIgnoreNeverallows bool `json:",omitempty"`
472
Devin Moorec381e102023-07-06 22:03:58 +0000473 Release_aidl_use_unfrozen *bool `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000474
475 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
476 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900477
478 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Christopher Ferris98f10222022-07-13 23:16:52 -0700479
480 IgnorePrefer32OnDevice bool `json:",omitempty"`
Spandan Dasc5763832022-11-08 18:42:16 +0000481
Sam Delmerico98a73292023-02-21 11:50:29 -0500482 IncludeTags []string `json:",omitempty"`
483 SourceRootDirs []string `json:",omitempty"`
Vinh Tran0ccc2322023-03-09 22:07:19 -0500484
485 AfdoProfiles []string `json:",omitempty"`
Wei Li5cfedc82023-05-04 09:06:06 -0700486
487 ProductManufacturer string `json:",omitempty"`
488 ProductBrand string `json:",omitempty"`
489 BuildVersionTags []string `json:",omitempty"`
Joe Onoratofee845a2023-05-09 08:14:14 -0700490
Yu Liueebb2592023-10-12 20:31:27 -0700491 ReleaseVersion string `json:",omitempty"`
492 ReleaseAconfigValueSets []string `json:",omitempty"`
Kiyoung Kime623c582023-07-06 16:43:44 +0900493
Zhi Dou3f65a412023-08-10 21:47:40 +0000494 ReleaseAconfigFlagDefaultPermission string `json:",omitempty"`
495
Alyssa Ketpreechasawat34ab8792023-10-06 07:01:22 +0000496 ReleaseDefaultModuleBuildFromSource *bool `json:",omitempty"`
497
Kiyoung Kime623c582023-07-06 16:43:44 +0900498 KeepVndk *bool `json:",omitempty"`
Inseob Kim5bac3b62023-08-25 21:29:46 +0900499
500 CheckVendorSeappViolations *bool `json:",omitempty"`
Cole Faustb4cb0c82023-09-14 15:16:58 -0700501
Cole Faustcb193ec2023-09-20 16:01:18 -0700502 // PartitionVarsForBazelMigrationOnlyDoNotUse are extra variables that are used to define the
503 // partition images. They should not be read from soong modules.
504 PartitionVarsForBazelMigrationOnlyDoNotUse PartitionVariables `json:",omitempty"`
Jihoon Kangcfbc4072023-09-20 01:02:18 +0000505
Inseob Kime4e85d52023-10-25 23:53:58 +0900506 BuildFlags map[string]string `json:",omitempty"`
Jihoon Kangc1b04d62023-11-16 19:07:04 +0000507
508 BuildFromSourceStub *bool `json:",omitempty"`
Spandan Das8ab28dd2024-02-17 03:31:45 +0000509
510 BuildIgnoreApexContributionContents []string `json:",omitempty"`
Jihoon Kangb36fc542024-02-22 19:35:26 +0000511
512 HiddenapiExportableStubs *bool `json:",omitempty"`
Jihoon Kang35349462024-02-22 19:52:46 +0000513
514 ExportRuntimeApis *bool `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700515}
516
Cole Faust11edf552023-10-13 11:32:14 -0700517type PartitionQualifiedVariablesType struct {
518 BuildingImage bool `json:",omitempty"`
519 BoardErofsCompressor string `json:",omitempty"`
520 BoardErofsCompressHints string `json:",omitempty"`
521 BoardErofsPclusterSize string `json:",omitempty"`
522 BoardExtfsInodeCount string `json:",omitempty"`
523 BoardExtfsRsvPct string `json:",omitempty"`
524 BoardF2fsSloadCompressFlags string `json:",omitempty"`
525 BoardFileSystemCompress string `json:",omitempty"`
526 BoardFileSystemType string `json:",omitempty"`
527 BoardJournalSize string `json:",omitempty"`
528 BoardPartitionReservedSize string `json:",omitempty"`
529 BoardPartitionSize string `json:",omitempty"`
530 BoardSquashfsBlockSize string `json:",omitempty"`
531 BoardSquashfsCompressor string `json:",omitempty"`
532 BoardSquashfsCompressorOpt string `json:",omitempty"`
533 BoardSquashfsDisable4kAlign string `json:",omitempty"`
534 ProductBaseFsPath string `json:",omitempty"`
535 ProductHeadroom string `json:",omitempty"`
536 ProductVerityPartition string `json:",omitempty"`
537
538 BoardAvbAddHashtreeFooterArgs string `json:",omitempty"`
539 BoardAvbKeyPath string `json:",omitempty"`
540 BoardAvbAlgorithm string `json:",omitempty"`
541 BoardAvbRollbackIndex string `json:",omitempty"`
542 BoardAvbRollbackIndexLocation string `json:",omitempty"`
543}
544
Cole Faustcb193ec2023-09-20 16:01:18 -0700545type PartitionVariables struct {
546 ProductDirectory string `json:",omitempty"`
Cole Faust11edf552023-10-13 11:32:14 -0700547 PartitionQualifiedVariables map[string]PartitionQualifiedVariablesType
548 TargetUserimagesUseExt2 bool `json:",omitempty"`
549 TargetUserimagesUseExt3 bool `json:",omitempty"`
550 TargetUserimagesUseExt4 bool `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700551
552 TargetUserimagesSparseExtDisabled bool `json:",omitempty"`
553 TargetUserimagesSparseErofsDisabled bool `json:",omitempty"`
554 TargetUserimagesSparseSquashfsDisabled bool `json:",omitempty"`
555 TargetUserimagesSparseF2fsDisabled bool `json:",omitempty"`
556
Yi-Yo Chiang939fe1a2023-11-24 14:58:50 +0800557 BoardErofsCompressor string `json:",omitempty"`
558 BoardErofsCompressorHints string `json:",omitempty"`
559 BoardErofsPclusterSize string `json:",omitempty"`
560 BoardErofsShareDupBlocks string `json:",omitempty"`
561 BoardErofsUseLegacyCompression string `json:",omitempty"`
562 BoardExt4ShareDupBlocks string `json:",omitempty"`
563 BoardFlashLogicalBlockSize string `json:",omitempty"`
564 BoardFlashEraseBlockSize string `json:",omitempty"`
565 BoardUsesRecoveryAsBoot bool `json:",omitempty"`
566 ProductUseDynamicPartitionSize bool `json:",omitempty"`
567 CopyImagesForTargetFilesZip bool `json:",omitempty"`
Cole Faustb5055392023-09-27 13:44:40 -0700568
569 BoardAvbEnable bool `json:",omitempty"`
Cole Faust11edf552023-10-13 11:32:14 -0700570
571 ProductPackages []string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700572}
573
574func boolPtr(v bool) *bool {
575 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700576}
577
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700578func intPtr(v int) *int {
579 return &v
580}
581
Colin Cross4225f652015-09-17 14:33:42 -0700582func stringPtr(v string) *string {
583 return &v
584}
585
Cole Faustf8231dd2023-04-21 17:37:11 -0700586func (v *ProductVariables) SetDefaultConfig() {
587 *v = ProductVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800588 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700589
Dan Albert8c7a9942023-03-27 20:34:01 +0000590 Platform_version_name: stringPtr("S"),
591 Platform_base_sdk_extension_version: intPtr(30),
592 Platform_sdk_version: intPtr(30),
593 Platform_sdk_codename: stringPtr("S"),
594 Platform_sdk_final: boolPtr(false),
595 Platform_version_active_codenames: []string{"S"},
596 Platform_version_all_preview_codenames: []string{"S"},
597 Platform_vndk_version: stringPtr("S"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800598
Vilas Bhatb3d2d222023-12-04 22:51:20 +0000599 HostArch: stringPtr("x86_64"),
600 HostSecondaryArch: stringPtr("x86"),
601 DeviceName: stringPtr("generic_arm64"),
602 DeviceProduct: stringPtr("aosp_arm-eng"),
603 DeviceArch: stringPtr("arm64"),
604 DeviceArchVariant: stringPtr("armv8-a"),
605 DeviceCpuVariant: stringPtr("generic"),
606 DeviceAbi: []string{"arm64-v8a"},
607 DeviceSecondaryArch: stringPtr("arm"),
608 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
609 DeviceSecondaryCpuVariant: stringPtr("generic"),
610 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
611 DeviceMaxPageSizeSupported: stringPtr("4096"),
612 DeviceNoBionicPageSizeMacro: boolPtr(false),
Colin Crossface4e42017-10-30 17:32:15 -0700613
Colin Crossa74ca042019-01-31 14:31:51 -0800614 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700615 AAPTPreferredConfig: stringPtr("xhdpi"),
616 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800617 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700618
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700619 Malloc_not_svelte: boolPtr(true),
Quallenauge55ba6ec2020-10-21 21:19:01 +0200620 Malloc_not_svelte_libc32: boolPtr(true),
Steven Morelandd1342012020-07-30 20:38:49 +0000621 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700622 Malloc_pattern_fill_contents: boolPtr(false),
623 Safestack: boolPtr(false),
Dennis Shene2ed70c2023-01-11 14:15:43 +0000624 TrimmedApex: boolPtr(false),
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000625 Build_from_text_stub: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100626
satayevd604b212021-07-21 14:23:52 +0100627 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
628 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700629 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800630
631 if runtime.GOOS == "linux" {
632 v.CrossHost = stringPtr("windows")
633 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800634 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800635 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700636}
637
Joe Onorato3fefc232023-12-04 17:35:15 +0000638func (this *ProductVariables) GetBuildFlagBool(flag string) bool {
639 val, ok := this.BuildFlags[flag]
640 if !ok {
641 return false
642 }
643 return val == "true"
644}
645
Liz Kammera060c452021-03-24 10:14:47 -0400646// ProductConfigContext requires the access to the Module to get product config properties.
647type ProductConfigContext interface {
648 Module() Module
649}
650
Cole Faust150f9a52023-04-26 10:52:24 -0700651// ProductConfigOrSoongConfigProperty represents either a soong config variable + its value
652// or a product config variable. You can get both a ConfigurationAxis and a SelectKey from it
653// for use in bazel attributes. ProductVariableProperties() will return a map from properties ->
654// this interface -> property structs for use in bp2build converters
655type ProductConfigOrSoongConfigProperty interface {
656 // Name of the product variable or soong config variable
657 Name() string
658 // AlwaysEmit returns true for soong config variables but false for product variables. This
659 // is intended to indicate if we need to always emit empty lists in the select statements.
660 AlwaysEmit() bool
661 // ConfigurationAxis returns the bazel.ConfigurationAxis that represents this variable. The
662 // configuration axis will change depending on the variable and whether it's arch/os variant
663 // as well.
664 ConfigurationAxis() bazel.ConfigurationAxis
665 // SelectKey returns a string that represents the key of a select branch, however, it is not
666 // actually the real label written out to the build file.
667 // this.ConfigurationAxis().SelectKey(this.SelectKey()) will give the actual label.
668 SelectKey() string
669}
670
671// ProductConfigProperty represents a product config variable, and if it is arch-variant or not.
Liz Kammera060c452021-03-24 10:14:47 -0400672type ProductConfigProperty struct {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000673 // The name of the product variable, e.g. "safestack", "malloc_not_svelte",
674 // "board"
Cole Faust150f9a52023-04-26 10:52:24 -0700675 name string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000676
Cole Faust150f9a52023-04-26 10:52:24 -0700677 arch string
Liz Kammera060c452021-03-24 10:14:47 -0400678}
679
Cole Faust150f9a52023-04-26 10:52:24 -0700680func (p ProductConfigProperty) Name() string {
681 return p.name
Jingwen Chen58ff6802021-11-17 12:14:41 +0000682}
683
Cole Faust150f9a52023-04-26 10:52:24 -0700684func (p ProductConfigProperty) AlwaysEmit() bool {
685 return false
686}
687
688func (p ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
689 return bazel.ProductVariableConfigurationAxis(p.arch != "", p.name+"__"+p.arch)
690}
691
692func (p ProductConfigProperty) SelectKey() string {
693 if p.arch == "" {
694 return strings.ToLower(p.name)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000695 } else {
Cole Faust150f9a52023-04-26 10:52:24 -0700696 return strings.ToLower(p.name + "-" + p.arch)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000697 }
698}
699
Cole Faust150f9a52023-04-26 10:52:24 -0700700// SoongConfigProperty represents a soong config variable, its value if it's a string variable,
701// and if it's dependent on the OS or not
702type SoongConfigProperty struct {
703 name string
704 namespace string
705 // Can be an empty string for bool/value soong config variables
706 value string
707 // If there is a target: field inside a soong config property struct, the os that it selects
708 // on will be represented here.
709 os string
710}
711
712func (p SoongConfigProperty) Name() string {
713 return p.name
714}
715
716func (p SoongConfigProperty) AlwaysEmit() bool {
717 return true
718}
719
720func (p SoongConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
721 return bazel.ProductVariableConfigurationAxis(false, p.namespace+"__"+p.name+"__"+p.os)
722}
723
Jingwen Chen25825ca2021-11-15 12:28:43 +0000724// SelectKey returns the literal string that represents this variable in a BUILD
725// select statement.
Cole Faust150f9a52023-04-26 10:52:24 -0700726func (p SoongConfigProperty) SelectKey() string {
727 // p.value being conditions_default can happen with or without a desired os. When not using
728 // an os, we want to emit literally just //conditions:default in the select statement, but
729 // when using an os, we want to emit namespace__name__conditions_default__os, so that
730 // the branch is only taken if the variable is not set, and we're on the desired os.
731 // ConfigurationAxis#SelectKey will map the conditions_default result of this function to
732 // //conditions:default.
733 if p.value == bazel.ConditionsDefaultConfigKey && p.os == "" {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000734 return bazel.ConditionsDefaultConfigKey
735 }
736
Cole Faust150f9a52023-04-26 10:52:24 -0700737 parts := []string{p.namespace, p.name}
738 if p.value != "" && p.value != bazel.ConditionsDefaultSelectKey {
739 parts = append(parts, p.value)
740 }
741 if p.os != "" {
742 parts = append(parts, p.os)
Jingwen Chen25825ca2021-11-15 12:28:43 +0000743 }
Sam Delmerico0e33c9d2022-01-07 20:39:21 +0000744
Cole Faust150f9a52023-04-26 10:52:24 -0700745 // e.g. acme__feature1, android__board__soc_a, my_namespace__my_variables__my_value__my_os
746 return strings.ToLower(strings.Join(parts, "__"))
Jingwen Chen25825ca2021-11-15 12:28:43 +0000747}
748
749// ProductConfigProperties is a map of maps to group property values according
750// their property name and the product config variable they're set under.
751//
752// The outer map key is the name of the property, like "cflags".
753//
754// The inner map key is a ProductConfigProperty, which is a struct of product
755// variable name, namespace, and the "full configuration" of the product
756// variable.
757//
758// e.g. product variable name: board, namespace: acme, full config: vendor_chip_foo
759//
760// The value of the map is the interface{} representing the value of the
761// property, like ["-DDEFINES"] for cflags.
Cole Faust150f9a52023-04-26 10:52:24 -0700762type ProductConfigProperties map[string]map[ProductConfigOrSoongConfigProperty]interface{}
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400763
Jingwen Chena47f28d2021-11-02 16:43:57 +0000764func (p *ProductConfigProperties) AddProductConfigProperty(
Cole Faust150f9a52023-04-26 10:52:24 -0700765 propertyName, productVariableName, arch string, propertyValue interface{}) {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000766
Jingwen Chen25825ca2021-11-15 12:28:43 +0000767 productConfigProp := ProductConfigProperty{
Cole Faust150f9a52023-04-26 10:52:24 -0700768 name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
769 arch: arch, // e.g. "", x86, arm64
Jingwen Chena47f28d2021-11-02 16:43:57 +0000770 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000771
Cole Faust150f9a52023-04-26 10:52:24 -0700772 p.AddEitherProperty(propertyName, productConfigProp, propertyValue)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000773}
774
Cole Faust150f9a52023-04-26 10:52:24 -0700775func (p *ProductConfigProperties) AddSoongConfigProperty(
776 propertyName, namespace, variableName, value, os string, propertyValue interface{}) {
777
778 soongConfigProp := SoongConfigProperty{
779 namespace: namespace,
780 name: variableName, // e.g. size, feature1, feature2, FEATURE3, board
781 value: value,
782 os: os, // e.g. android, linux_x86
783 }
784
785 p.AddEitherProperty(propertyName, soongConfigProp, propertyValue)
786}
787
788func (p *ProductConfigProperties) AddEitherProperty(
789 propertyName string, key ProductConfigOrSoongConfigProperty, propertyValue interface{}) {
790 if (*p)[propertyName] == nil {
791 (*p)[propertyName] = make(map[ProductConfigOrSoongConfigProperty]interface{})
792 }
793
794 if existing, ok := (*p)[propertyName][key]; ok {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000795 switch dst := existing.(type) {
796 case []string:
Cole Faust150f9a52023-04-26 10:52:24 -0700797 src, ok := propertyValue.([]string)
798 if !ok {
799 panic("Conflicting types")
Jingwen Chena47f28d2021-11-02 16:43:57 +0000800 }
Cole Faust150f9a52023-04-26 10:52:24 -0700801 dst = append(dst, src...)
802 (*p)[propertyName][key] = dst
Jingwen Chena47f28d2021-11-02 16:43:57 +0000803 default:
Spandan Das7b26e0f2023-07-28 01:31:15 +0000804 if existing != propertyValue {
805 panic(fmt.Errorf("TODO: handle merging value %#v", existing))
806 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000807 }
808 } else {
Cole Faust150f9a52023-04-26 10:52:24 -0700809 (*p)[propertyName][key] = propertyValue
Jingwen Chena47f28d2021-11-02 16:43:57 +0000810 }
811}
Jingwen Chena47f28d2021-11-02 16:43:57 +0000812
813// maybeExtractConfigVarProp attempts to read this value as a config var struct
814// wrapped by interfaces and ptrs. If it's not the right type, the second return
815// value is false.
816func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
817 if v.Kind() == reflect.Interface {
818 // The conditions_default value can be either
819 // 1) an ptr to an interface of a struct (bool config variables and product variables)
820 // 2) an interface of 1) (config variables with nested structs, like string vars)
821 v = v.Elem()
822 }
823 if v.Kind() != reflect.Ptr {
824 return v, false
825 }
826 v = reflect.Indirect(v)
827 if v.Kind() == reflect.Interface {
828 // Extract the struct from the interface
829 v = v.Elem()
830 }
831
832 if !v.IsValid() {
833 return v, false
834 }
835
836 if v.Kind() != reflect.Struct {
837 return v, false
838 }
839 return v, true
840}
841
Cole Faust150f9a52023-04-26 10:52:24 -0700842func (productConfigProperties *ProductConfigProperties) AddProductConfigProperties(variableValues reflect.Value, arch string) {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000843 // Example of product_variables:
844 //
845 // product_variables: {
846 // malloc_not_svelte: {
847 // shared_libs: ["malloc_not_svelte_shared_lib"],
848 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
849 // exclude_static_libs: [
850 // "malloc_not_svelte_static_lib_excludes",
851 // "malloc_not_svelte_whole_static_lib_excludes",
852 // ],
853 // },
854 // },
Cole Faust150f9a52023-04-26 10:52:24 -0700855
Liz Kammera060c452021-03-24 10:14:47 -0400856 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000857 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
858 productVariableName := variableValues.Type().Field(i).Name
859
Liz Kammera060c452021-03-24 10:14:47 -0400860 variableValue := variableValues.Field(i)
861 // Check if any properties were set for the module
862 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000863 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400864 continue
865 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000866
Liz Kammera060c452021-03-24 10:14:47 -0400867 for j := 0; j < variableValue.NumField(); j++ {
868 property := variableValue.Field(j)
Liz Kammerf3963f82022-12-21 14:47:18 -0500869 // e.g. Asflags, Cflags, Enabled, etc.
870 propertyName := variableValue.Type().Field(j).Name
Cole Faust150f9a52023-04-26 10:52:24 -0700871 if property.Kind() != reflect.Interface {
872 productConfigProperties.AddProductConfigProperty(propertyName, productVariableName, arch, property.Interface())
873 }
874 }
875 }
876
877}
878
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400879func (productConfigProperties *ProductConfigProperties) AddSoongConfigProperties(namespace string, soongConfigVariablesStruct reflect.Value) error {
Cole Faust150f9a52023-04-26 10:52:24 -0700880 //
881 // Example of soong_config_variables:
882 //
883 // soong_config_variables: {
884 // feature1: {
885 // conditions_default: {
886 // ...
887 // },
888 // cflags: ...
889 // },
890 // feature2: {
891 // cflags: ...
892 // conditions_default: {
893 // ...
894 // },
895 // },
896 // board: {
897 // soc_a: {
898 // ...
899 // },
900 // soc_b: {
901 // ...
902 // },
903 // soc_c: {},
904 // conditions_default: {
905 // ...
906 // },
907 // },
908 // }
909 for i := 0; i < soongConfigVariablesStruct.NumField(); i++ {
910 // e.g. feature1, feature2, board
911 variableName := soongConfigVariablesStruct.Type().Field(i).Name
912 variableStruct := soongConfigVariablesStruct.Field(i)
913 // Check if any properties were set for the module
914 if variableStruct.IsZero() {
915 // e.g. feature1: {}
916 continue
917 }
918
919 // Unlike product variables, config variables require a few more
920 // indirections to extract the struct from the reflect.Value.
921 if v, ok := maybeExtractConfigVarProp(variableStruct); ok {
922 variableStruct = v
liulvping4d11d102023-05-24 10:27:44 +0800923 } else if !v.IsValid() {
924 // Skip invalid variables which may not used, else leads to panic
925 continue
Cole Faust150f9a52023-04-26 10:52:24 -0700926 }
927
928 for j := 0; j < variableStruct.NumField(); j++ {
929 propertyOrStruct := variableStruct.Field(j)
930 // propertyOrValueName can either be:
931 // - A property, like: Asflags, Cflags, Enabled, etc.
932 // - A soong config string variable's value, like soc_a, soc_b, soc_c in the example above
933 // - "conditions_default"
934 propertyOrValueName := variableStruct.Type().Field(j).Name
Liz Kammerf3963f82022-12-21 14:47:18 -0500935
Liz Kammera060c452021-03-24 10:14:47 -0400936 // If the property wasn't set, no need to pass it along
Cole Faust150f9a52023-04-26 10:52:24 -0700937 if propertyOrStruct.IsZero() {
Liz Kammera060c452021-03-24 10:14:47 -0400938 continue
939 }
940
Cole Faust150f9a52023-04-26 10:52:24 -0700941 if v, ok := maybeExtractConfigVarProp(propertyOrStruct); ok {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000942 // The field is a struct, which is used by:
943 // 1) soong_config_string_variables
944 //
945 // soc_a: {
946 // cflags: ...,
947 // }
948 //
949 // soc_b: {
950 // cflags: ...,
951 // }
952 //
953 // 2) conditions_default structs for all soong config variable types.
954 //
955 // conditions_default: {
956 // cflags: ...,
957 // static_libs: ...
958 // }
Cole Faust150f9a52023-04-26 10:52:24 -0700959 //
960 // This means that propertyOrValueName is either conditions_default, or a soong
961 // config string variable's value.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000962 field := v
Liz Kammerf3963f82022-12-21 14:47:18 -0500963 // Iterate over fields of this struct prop.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000964 for k := 0; k < field.NumField(); k++ {
Liz Kammerf3963f82022-12-21 14:47:18 -0500965 // For product variables, zero values are irrelevant; however, for soong config variables,
966 // empty values are relevant because there can also be a conditions default which is not
967 // applied for empty variables.
968 if field.Field(k).IsZero() && namespace == "" {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000969 continue
970 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000971
Cole Faust150f9a52023-04-26 10:52:24 -0700972 propertyName := field.Type().Field(k).Name
973 if propertyName == "Target" {
974 productConfigProperties.AddSoongConfigPropertiesFromTargetStruct(namespace, variableName, proptools.PropertyNameForField(propertyOrValueName), field.Field(k))
975 } else if propertyName == "Arch" || propertyName == "Multilib" {
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400976 return fmt.Errorf("Arch/Multilib are not currently supported in soong config variable structs")
Cole Faust150f9a52023-04-26 10:52:24 -0700977 } else {
978 productConfigProperties.AddSoongConfigProperty(propertyName, namespace, variableName, proptools.PropertyNameForField(propertyOrValueName), "", field.Field(k).Interface())
979 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000980 }
Cole Faust150f9a52023-04-26 10:52:24 -0700981 } else if propertyOrStruct.Kind() != reflect.Interface {
Jingwen Chen58ff6802021-11-17 12:14:41 +0000982 // If not an interface, then this is not a conditions_default or
Cole Faust150f9a52023-04-26 10:52:24 -0700983 // a struct prop. That is, this is a bool/value config variable.
984 if propertyOrValueName == "Target" {
985 productConfigProperties.AddSoongConfigPropertiesFromTargetStruct(namespace, variableName, "", propertyOrStruct)
986 } else if propertyOrValueName == "Arch" || propertyOrValueName == "Multilib" {
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400987 return fmt.Errorf("Arch/Multilib are not currently supported in soong config variable structs")
Cole Faust150f9a52023-04-26 10:52:24 -0700988 } else {
989 productConfigProperties.AddSoongConfigProperty(propertyOrValueName, namespace, variableName, "", "", propertyOrStruct.Interface())
990 }
Liz Kammer47535c52021-06-02 16:02:22 -0400991 }
Liz Kammera060c452021-03-24 10:14:47 -0400992 }
993 }
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400994 return nil
Liz Kammera060c452021-03-24 10:14:47 -0400995}
996
Cole Faust150f9a52023-04-26 10:52:24 -0700997func (productConfigProperties *ProductConfigProperties) AddSoongConfigPropertiesFromTargetStruct(namespace, soongConfigVariableName string, soongConfigVariableValue string, targetStruct reflect.Value) {
998 // targetStruct will be a struct with fields like "android", "host", "arm", "x86",
999 // "android_arm", etc. The values of each of those fields will be a regular property struct.
1000 for i := 0; i < targetStruct.NumField(); i++ {
1001 targetFieldName := targetStruct.Type().Field(i).Name
1002 archOrOsSpecificStruct := targetStruct.Field(i)
1003 for j := 0; j < archOrOsSpecificStruct.NumField(); j++ {
1004 property := archOrOsSpecificStruct.Field(j)
1005 // e.g. Asflags, Cflags, Enabled, etc.
1006 propertyName := archOrOsSpecificStruct.Type().Field(j).Name
Jingwen Chen84817de2021-11-17 10:57:35 +00001007
Cole Faust150f9a52023-04-26 10:52:24 -07001008 if targetFieldName == "Android" {
1009 productConfigProperties.AddSoongConfigProperty(propertyName, namespace, soongConfigVariableName, soongConfigVariableValue, "android", property.Interface())
1010 } else if targetFieldName == "Host" {
1011 for _, os := range osTypeList {
1012 if os.Class == Host {
1013 productConfigProperties.AddSoongConfigProperty(propertyName, namespace, soongConfigVariableName, soongConfigVariableValue, os.Name, property.Interface())
1014 }
1015 }
Spandan Das474d43b2023-07-27 23:01:06 +00001016 } else if !archOrOsSpecificStruct.IsZero() {
Cole Faust150f9a52023-04-26 10:52:24 -07001017 // One problem with supporting additional fields is that if multiple branches of
1018 // "target" overlap, we don't want them to be in the same select statement (aka
1019 // configuration axis). "android" and "host" are disjoint, so it's ok that we only
1020 // have 2 axes right now. (soongConfigVariables and soongConfigVariablesPlusOs)
1021 panic("TODO: support other target types in soong config variable structs: " + targetFieldName)
1022 }
1023 }
1024 }
Jingwen Chen84817de2021-11-17 10:57:35 +00001025}
1026
Colin Crosseabaedd2020-02-06 17:01:55 -08001027func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001028 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -07001029 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -07001030 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001031 return
1032 }
1033
1034 // TODO: depend on config variable, create variants, propagate variants up tree
1035 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -07001036
1037 if a.variableProperties == nil {
1038 return
1039 }
1040
1041 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001042
Colin Cross0cec3122021-01-20 11:27:32 -08001043 productVariables := reflect.ValueOf(mctx.Config().productVariables)
1044
Colin Cross7f64b6d2015-07-09 13:57:48 -07001045 for i := 0; i < variableValues.NumField(); i++ {
1046 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -07001047 name := variableValues.Type().Field(i).Name
1048 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001049
Colin Crossa6bc19e2015-09-16 13:53:42 -07001050 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -08001051 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -07001052 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
1053 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -07001054 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001055
1056 val = val.Elem()
1057
1058 // For bools, check that the value is true
1059 if val.Kind() == reflect.Bool && val.Bool() == false {
1060 continue
1061 }
1062
1063 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -08001064 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -07001065 continue
1066 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001067 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -07001068 }
1069}
1070
Colin Cross4157e882019-06-06 16:57:04 -07001071func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -07001072 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
1073
Colin Cross7e0eaf12017-05-05 16:16:24 -07001074 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001075
Usta851a3272022-01-05 23:42:33 -05001076 err := proptools.AppendMatchingProperties(m.GetProperties(),
Colin Cross06a931b2015-10-28 17:23:31 -07001077 productVariablePropertyValue.Addr().Interface(), nil)
1078 if err != nil {
1079 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1080 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1081 } else {
1082 panic(err)
1083 }
1084 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001085}
1086
Colin Cross7e0eaf12017-05-05 16:16:24 -07001087func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
1088 productVariablePropertyValue reflect.Value, i int, err error) {
1089
1090 field := productVariablePropertyValue.Type().Field(i).Name
1091 property := prefix + "." + proptools.PropertyNameForField(field)
1092 ctx.PropertyErrorf(property, "%s", err)
1093}
1094
1095func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
1096 productVariablePropertyValue reflect.Value, variableValue interface{}) {
1097
Colin Cross7f64b6d2015-07-09 13:57:48 -07001098 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
1099 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -08001100 kind := propertyValue.Kind()
1101 if kind == reflect.Ptr {
1102 if propertyValue.IsNil() {
1103 continue
1104 }
1105 propertyValue = propertyValue.Elem()
1106 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001107 switch propertyValue.Kind() {
1108 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001109 err := printfIntoProperty(propertyValue, variableValue)
1110 if err != nil {
1111 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1112 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001113 case reflect.Slice:
1114 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001115 err := printfIntoProperty(propertyValue.Index(j), variableValue)
1116 if err != nil {
1117 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1118 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001119 }
Colin Crossdd0dbe62015-12-02 15:24:38 -08001120 case reflect.Bool:
1121 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001122 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001123 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001124 default:
1125 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
1126 }
1127 }
1128}
1129
Colin Cross7e0eaf12017-05-05 16:16:24 -07001130func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001131 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -07001132
1133 count := strings.Count(s, "%")
1134 if count == 0 {
1135 return nil
1136 }
1137
1138 if count > 1 {
1139 return fmt.Errorf("product variable properties only support a single '%%'")
1140 }
1141
Colin Cross7f64b6d2015-07-09 13:57:48 -07001142 if strings.Contains(s, "%d") {
1143 switch v := variableValue.(type) {
1144 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001145 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001146 case bool:
1147 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001148 variableValue = 1
1149 } else {
1150 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -07001151 }
1152 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001153 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001154 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001155 } else if strings.Contains(s, "%s") {
1156 switch variableValue.(type) {
1157 case string:
1158 // Nothing
1159 default:
1160 return fmt.Errorf("unsupported type %T for %%s", variableValue)
1161 }
1162 } else {
1163 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001164 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001165
1166 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
1167
1168 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -07001169}
Colin Cross18c46802019-09-24 22:19:02 -07001170
1171var variablePropTypeMap OncePer
1172
1173// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
1174// reflect.Types of each property struct. The result can be used as a key in a map.
1175func sliceToTypeArray(s []interface{}) interface{} {
1176 // Create an array using reflection whose length is the length of the input slice
1177 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
1178 for i, e := range s {
1179 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
1180 }
1181 return ret.Interface()
1182}
1183
Colin Crosseabaedd2020-02-06 17:01:55 -08001184func initProductVariableModule(m Module) {
1185 base := m.base()
1186
1187 // Allow tests to override the default product variables
1188 if base.variableProperties == nil {
1189 base.variableProperties = defaultProductVariables
1190 }
1191 // Filter the product variables properties to the ones that exist on this module
1192 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
1193 if base.variableProperties != nil {
1194 m.AddProperties(base.variableProperties)
1195 }
1196}
1197
Colin Cross18c46802019-09-24 22:19:02 -07001198// createVariableProperties takes the list of property structs for a module and returns a property struct that
1199// contains the product variable properties that exist in the property structs, or nil if there are none. It
1200// caches the result.
1201func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
1202 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
1203 key := sliceToTypeArray(moduleTypeProps)
1204
1205 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
1206 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
1207 // Compute the filtered property struct type.
1208 return createVariablePropertiesType(moduleTypeProps, productVariables)
1209 }).(reflect.Type)
1210
1211 if typ == nil {
1212 return nil
1213 }
1214
1215 // Create a new pointer to a filtered property struct.
1216 return reflect.New(typ).Interface()
1217}
1218
1219// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1220// a list of property structs.
1221func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1222 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1223 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
dianlujitao05b1de92020-09-11 11:41:51 +08001224 if strings.HasPrefix(prefix, "Product_variables.Bliss") {
1225 // Convert Product_variables.Bliss.Foo to Bliss.Foo
1226 _, prefix = splitPrefix(prefix)
1227 }
1228
Colin Cross18c46802019-09-24 22:19:02 -07001229 // Filter function, returns true if the field should be in the resulting struct
1230 if prefix == "" {
1231 // Keep the top level Product_variables field
1232 return true, field
1233 }
1234 _, rest := splitPrefix(prefix)
1235 if rest == "" {
1236 // Keep the 2nd level field (i.e. Product_variables.Eng)
1237 return true, field
1238 }
1239
1240 // Strip off the first 2 levels of the prefix
1241 _, prefix = splitPrefix(rest)
1242
1243 for _, p := range moduleTypeProps {
1244 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1245 // Keep any fields that exist in one of the property structs
1246 return true, field
1247 }
1248 }
1249
1250 return false, field
1251 })
1252 return typ
1253}
1254
1255func splitPrefix(prefix string) (first, rest string) {
1256 index := strings.IndexByte(prefix, '.')
1257 if index == -1 {
1258 return prefix, ""
1259 }
1260 return prefix[:index], prefix[index+1:]
1261}
1262
1263func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1264 if t.Kind() != reflect.Struct {
1265 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1266 }
1267
1268 if prefix != "" {
1269 split := strings.SplitN(prefix, ".", 2)
1270 firstPrefix := split[0]
1271 rest := ""
1272 if len(split) > 1 {
1273 rest = split[1]
1274 }
1275 f, exists := t.FieldByName(firstPrefix)
1276 if !exists {
1277 return false
1278 }
1279 ft := f.Type
1280 if ft.Kind() == reflect.Ptr {
1281 ft = ft.Elem()
1282 }
1283 if ft.Kind() != reflect.Struct {
1284 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1285 }
1286 return fieldExistsByNameRecursive(ft, rest, name)
1287 } else {
1288 _, exists := t.FieldByName(name)
1289 return exists
1290 }
1291}