blob: d4cd863d7fe81574dbeec5ea238db47305953910 [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
Jeongik Chaf08e59c2023-02-09 10:47:59 +090067 // similar to `Unbundled_build`, but `Always_use_prebuilt_sdks` means that it uses prebuilt
68 // sdk specifically.
69 Always_use_prebuilt_sdks struct {
70 Enabled *bool `android:"arch_variant"`
71 } `android:"arch_variant"`
72
Dan Willemsen97cae012016-03-01 14:08:42 -080073 Malloc_not_svelte struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080074 Cflags []string `android:"arch_variant"`
75 Shared_libs []string `android:"arch_variant"`
76 Whole_static_libs []string `android:"arch_variant"`
77 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
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070082 Malloc_zero_contents struct {
83 Cflags []string `android:"arch_variant"`
84 } `android:"arch_variant"`
85
86 Malloc_pattern_fill_contents struct {
87 Cflags []string `android:"arch_variant"`
88 } `android:"arch_variant"`
89
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070090 Safestack struct {
91 Cflags []string `android:"arch_variant"`
92 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070093
Dan Willemsen1be35382016-07-25 17:42:18 -070094 Binder32bit struct {
95 Cflags []string
96 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070097
Colin Cross95436422017-05-04 13:57:05 -070098 Override_rs_driver struct {
99 Cflags []string
100 }
101
Steven Morelandc2b9f062017-12-13 14:06:20 -0800102 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
103 // enabled.
104 Treble_linker_namespaces struct {
105 Cflags []string
106 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800107 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
108 Enforce_vintf_manifest struct {
109 Cflags []string
110 }
111
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000112 Build_from_text_stub struct {
113 Static_libs []string
114 Exclude_static_libs []string
115 }
116
Colin Cross6bc59ef2016-12-08 09:46:35 -0800117 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
118 // debugging features that don't significantly impact runtime behavior. userdebug builds
119 // are used for dogfooding and performance testing, and should be as similar to user builds
120 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700121 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700122 Cflags []string
123 Cppflags []string
124 Init_rc []string
125 Required []string
126 Host_required []string
127 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700128 Strip struct {
129 All *bool
130 Keep_symbols *bool
131 Keep_symbols_and_debug_frame *bool
132 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700133 Static_libs []string
134 Whole_static_libs []string
135 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900136
137 Cmdline []string
Inseob Kimf5ed3002022-01-07 19:17:29 +0900138
Jay Aliomer2599d1d2021-09-23 15:49:54 -0400139 Srcs []string
140 Exclude_srcs []string
Cole Fausted940002023-08-15 11:59:24 -0700141 Cmd *string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700142 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800143
Usta Shresthac725f472022-01-11 02:44:21 -0500144 // eng is true for -eng builds, and can be used to turn on additional heavyweight debugging
Colin Cross6bc59ef2016-12-08 09:46:35 -0800145 // features.
146 Eng struct {
147 Cflags []string
148 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700149 Lto struct {
150 Never *bool
151 }
Colin Cross3273cc22018-10-26 23:58:42 -0700152 Sanitize struct {
153 Address *bool
154 }
Felka Changf13642e2019-06-21 20:58:27 +0800155 Optimize struct {
156 Enabled *bool
157 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800158 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700159
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700160 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
Colin Crossd9a121b2020-01-27 13:26:42 -0800175 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800176 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800177 Srcs []string `android:"arch_variant"`
178 Exclude_srcs []string `android:"arch_variant"`
179 } `android:"arch_variant"`
Devin Moorec381e102023-07-06 22:03:58 +0000180
181 // release_aidl_use_unfrozen is "true" when a device can
182 // use the unfrozen versions of AIDL interfaces.
183 Release_aidl_use_unfrozen struct {
184 Cflags []string
185 Cmd *string
186 }
Colin Cross83163192015-12-01 16:31:16 -0800187 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700188}
189
Colin Cross6961a492020-02-06 16:57:45 -0800190var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700191
Cole Faustf8231dd2023-04-21 17:37:11 -0700192type ProductVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700193 // Suffix to add to generated Makefiles
194 Make_suffix *string `json:",omitempty"`
195
Colin Cross2a2e0db2020-02-21 16:55:46 -0800196 BuildId *string `json:",omitempty"`
197 BuildNumberFile *string `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800198
Colin Cross092c9da2019-04-02 22:56:43 -0700199 Platform_version_name *string `json:",omitempty"`
200 Platform_sdk_version *int `json:",omitempty"`
201 Platform_sdk_codename *string `json:",omitempty"`
Yuntao Xu402e9b02021-08-09 15:44:44 -0700202 Platform_sdk_version_or_codename *string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700203 Platform_sdk_final *bool `json:",omitempty"`
Anton Hanssonb021bf82021-08-27 17:35:40 +0100204 Platform_sdk_extension_version *int `json:",omitempty"`
Anton Hansson97d0bae2022-02-16 16:15:10 +0000205 Platform_base_sdk_extension_version *int `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700206 Platform_version_active_codenames []string `json:",omitempty"`
Dan Albert8c7a9942023-03-27 20:34:01 +0000207 Platform_version_all_preview_codenames []string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700208 Platform_vndk_version *string `json:",omitempty"`
209 Platform_systemsdk_versions []string `json:",omitempty"`
210 Platform_security_patch *string `json:",omitempty"`
211 Platform_preview_sdk_version *string `json:",omitempty"`
212 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
213 Platform_base_os *string `json:",omitempty"`
Inseob Kim4f1f3d92022-04-25 18:23:58 +0900214 Platform_version_last_stable *string `json:",omitempty"`
Jiyong Park37073842022-06-21 10:13:42 +0900215 Platform_version_known_codenames *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700216
Jeongik Cha219141c2020-08-06 23:00:37 +0900217 DeviceName *string `json:",omitempty"`
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000218 DeviceProduct *string `json:",omitempty"`
Jeongik Cha219141c2020-08-06 23:00:37 +0900219 DeviceArch *string `json:",omitempty"`
220 DeviceArchVariant *string `json:",omitempty"`
221 DeviceCpuVariant *string `json:",omitempty"`
222 DeviceAbi []string `json:",omitempty"`
223 DeviceVndkVersion *string `json:",omitempty"`
224 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
225 DeviceSystemSdkVersions []string `json:",omitempty"`
Juan Yescas05d4d902023-04-07 10:35:35 -0700226 DeviceMaxPageSizeSupported *string `json:",omitempty"`
Juan Yescas01065602023-08-09 08:34:37 -0700227 DevicePageSizeAgnostic *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700228
Jose Galmes6f843bc2020-12-11 13:36:29 -0800229 RecoverySnapshotVersion *string `json:",omitempty"`
230
Colin Crossa74ca042019-01-31 14:31:51 -0800231 DeviceSecondaryArch *string `json:",omitempty"`
232 DeviceSecondaryArchVariant *string `json:",omitempty"`
233 DeviceSecondaryCpuVariant *string `json:",omitempty"`
234 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700235
dimitry8d6dde82019-07-11 10:23:53 +0200236 NativeBridgeArch *string `json:",omitempty"`
237 NativeBridgeArchVariant *string `json:",omitempty"`
238 NativeBridgeCpuVariant *string `json:",omitempty"`
239 NativeBridgeAbi []string `json:",omitempty"`
240 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100241
dimitry8d6dde82019-07-11 10:23:53 +0200242 NativeBridgeSecondaryArch *string `json:",omitempty"`
243 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
244 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
245 NativeBridgeSecondaryAbi []string `json:",omitempty"`
246 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100247
Colin Cross4225f652015-09-17 14:33:42 -0700248 HostArch *string `json:",omitempty"`
249 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000250 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800251
252 CrossHost *string `json:",omitempty"`
253 CrossHostArch *string `json:",omitempty"`
254 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800255
Jeongik Chacee5ba92021-02-19 12:11:51 +0900256 DeviceResourceOverlays []string `json:",omitempty"`
257 ProductResourceOverlays []string `json:",omitempty"`
258 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800259 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700260
Colin Crossa74ca042019-01-31 14:31:51 -0800261 AAPTCharacteristics *string `json:",omitempty"`
262 AAPTConfig []string `json:",omitempty"`
263 AAPTPreferredConfig *string `json:",omitempty"`
264 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700265
Inseob Kim80fa7982022-08-12 21:36:25 +0900266 DefaultAppCertificate *string `json:",omitempty"`
267 MainlineSepolicyDevCertificates *string `json:",omitempty"`
Colin Cross61ae0b72017-12-01 17:16:02 -0800268
Colin Crossface4e42017-10-30 17:32:15 -0700269 AppsDefaultVersionName *string `json:",omitempty"`
270
Cole Faust701ca252021-11-23 19:02:08 -0800271 Allow_missing_dependencies *bool `json:",omitempty"`
272 Unbundled_build *bool `json:",omitempty"`
273 Unbundled_build_apps []string `json:",omitempty"`
274 Unbundled_build_image *bool `json:",omitempty"`
275 Always_use_prebuilt_sdks *bool `json:",omitempty"`
276 Skip_boot_jars_check *bool `json:",omitempty"`
277 Malloc_not_svelte *bool `json:",omitempty"`
278 Malloc_zero_contents *bool `json:",omitempty"`
279 Malloc_pattern_fill_contents *bool `json:",omitempty"`
280 Safestack *bool `json:",omitempty"`
281 HostStaticBinaries *bool `json:",omitempty"`
282 Binder32bit *bool `json:",omitempty"`
283 UseGoma *bool `json:",omitempty"`
284 UseRBE *bool `json:",omitempty"`
285 UseRBEJAVAC *bool `json:",omitempty"`
286 UseRBER8 *bool `json:",omitempty"`
287 UseRBED8 *bool `json:",omitempty"`
288 Debuggable *bool `json:",omitempty"`
289 Eng *bool `json:",omitempty"`
290 Treble_linker_namespaces *bool `json:",omitempty"`
291 Enforce_vintf_manifest *bool `json:",omitempty"`
292 Uml *bool `json:",omitempty"`
293 Arc *bool `json:",omitempty"`
294 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000295 Build_from_text_stub *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800296
Logan Chien4fcea3d2018-11-20 11:59:08 +0800297 Check_elf_files *bool `json:",omitempty"`
298
Colin Cross5a0dcd52018-10-05 14:20:06 -0700299 UncompressPrivAppDex *bool `json:",omitempty"`
300 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800301
satayevd604b212021-07-21 14:23:52 +0100302 BootJars ConfiguredJarList `json:",omitempty"`
303 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000304
Colin Crossa74ca042019-01-31 14:31:51 -0800305 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700306
Colin Crossa74ca042019-01-31 14:31:51 -0800307 EnableCFI *bool `json:",omitempty"`
308 CFIExcludePaths []string `json:",omitempty"`
309 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700310
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800311 DisableScudo *bool `json:",omitempty"`
312
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800313 MemtagHeapExcludePaths []string `json:",omitempty"`
314 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
315 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
316
Hang Lua98aab92023-03-17 13:17:22 +0800317 HWASanIncludePaths []string `json:",omitempty"`
Tomislav Novakf734f002023-08-22 10:51:44 -0700318 HWASanExcludePaths []string `json:",omitempty"`
Hang Lua98aab92023-03-17 13:17:22 +0800319
Justin Yund5f6c822019-06-25 16:47:17 +0900320 VendorPath *string `json:",omitempty"`
321 OdmPath *string `json:",omitempty"`
322 ProductPath *string `json:",omitempty"`
323 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800324
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700325 ClangTidy *bool `json:",omitempty"`
326 TidyChecks *string `json:",omitempty"`
327
Roland Levillainada12702020-06-09 13:07:36 +0100328 JavaCoveragePaths []string `json:",omitempty"`
329 JavaCoverageExcludePaths []string `json:",omitempty"`
330
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800331 GcovCoverage *bool `json:",omitempty"`
332 ClangCoverage *bool `json:",omitempty"`
333 NativeCoveragePaths []string `json:",omitempty"`
334 NativeCoverageExcludePaths []string `json:",omitempty"`
335 ClangCoverageContinuousMode *bool `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800336
Colin Cross1a6acd42020-06-16 17:51:46 -0700337 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400338 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700339
Colin Cross23ae82a2016-11-02 14:34:39 -0700340 SanitizeHost []string `json:",omitempty"`
341 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700342 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700343 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800344
345 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800346
347 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700348
349 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900350
351 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900352
353 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800354
355 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800356
Vinh Tran7a8362c2022-11-01 15:33:51 -0400357 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700358
Inseob Kimae553032019-05-14 18:52:49 +0900359 VndkUseCoreVariant *bool `json:",omitempty"`
360 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800361
Inseob Kim7cf14652021-01-06 23:06:52 +0900362 DirectedVendorSnapshot bool `json:",omitempty"`
363 VendorSnapshotModules map[string]bool `json:",omitempty"`
364
Jose Galmes4c6895e2021-02-09 07:44:30 -0800365 DirectedRecoverySnapshot bool `json:",omitempty"`
366 RecoverySnapshotModules map[string]bool `json:",omitempty"`
367
Justin DeMartino383bfb32021-02-24 10:49:43 -0800368 VendorSnapshotDirsIncluded []string `json:",omitempty"`
369 VendorSnapshotDirsExcluded []string `json:",omitempty"`
370 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
371 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000372 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800373
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900374 MultitreeUpdateMeta bool `json:",omitempty"`
375
Inseob Kim6077b232023-08-31 16:49:59 +0900376 BoardVendorSepolicyDirs []string `json:",omitempty"`
377 BoardOdmSepolicyDirs []string `json:",omitempty"`
378 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
379 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
380 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700381
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900382 BoardSepolicyVers *string `json:",omitempty"`
383 PlatformSepolicyVersion *string `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000384 TotSepolicyVersion *string `json:",omitempty"`
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900385
Inseob Kim1a0afcc2022-02-14 23:10:51 +0900386 SystemExtSepolicyPrebuiltApiDir *string `json:",omitempty"`
387 ProductSepolicyPrebuiltApiDir *string `json:",omitempty"`
388
Inseob Kim843f6642022-01-07 09:11:23 +0900389 PlatformSepolicyCompatVersions []string `json:",omitempty"`
390
Dan Willemsen0fe78662018-03-26 12:41:18 -0700391 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700392
Dan Albertd05ba002021-04-13 15:55:47 -0700393 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900394
Dennis Shene2ed70c2023-01-11 14:15:43 +0000395 TrimmedApex *bool `json:",omitempty"`
Jiyong Park4da07972021-01-05 21:01:11 +0900396 ForceApexSymlinkOptimization *bool `json:",omitempty"`
397 CompressedApex *bool `json:",omitempty"`
398 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800399
400 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900401
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900402 WithDexpreopt bool `json:",omitempty"`
403
Jiakai Zhang4d90da22023-07-12 16:51:48 +0100404 ManifestPackageNameOverrides []string `json:",omitempty"`
405 CertificateOverrides []string `json:",omitempty"`
406 PackageNameOverrides []string `json:",omitempty"`
407 ConfiguredJarLocationOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900408
Albert Martineefabcf2022-03-21 20:11:16 +0000409 ApexGlobalMinSdkVersionOverride *string `json:",omitempty"`
410
Jeongik Chac9464142019-01-07 12:07:27 +0900411 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700412 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800413
Colin Crossf24a22a2019-01-31 14:12:44 -0800414 ProductHiddenAPIStubs []string `json:",omitempty"`
415 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
416 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700417
Inseob Kim0866b002019-04-15 20:21:29 +0900418 ProductPublicSepolicyDirs []string `json:",omitempty"`
419 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900420
Justin Yun5f7f7e82019-11-18 19:52:14 +0900421 ProductVndkVersion *string `json:",omitempty"`
422
Dan Willemsen54879d12019-04-18 10:08:46 -0700423 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700424
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900426
JaeMan Parkff715562020-10-19 17:25:58 +0900427 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
428 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
429
Yifan Hong82db7352020-01-21 16:12:26 -0800430 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700431
Yifan Hong42bef8d2020-08-05 14:36:09 -0700432 BoardKernelBinaries []string `json:",omitempty"`
433 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700434
435 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800436
437 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900438
439 ShippingApiLevel *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900440
Cole Faustded79602023-09-05 17:48:11 -0700441 BuildBrokenPluginValidation []string `json:",omitempty"`
442 BuildBrokenClangAsFlags bool `json:",omitempty"`
443 BuildBrokenClangCFlags bool `json:",omitempty"`
444 BuildBrokenClangProperty bool `json:",omitempty"`
445 GenruleSandboxing *bool `json:",omitempty"`
446 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
447 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
448 BuildBrokenUsesSoongPython2Modules bool `json:",omitempty"`
449 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
450 BuildBrokenIncorrectPartitionImages bool `json:",omitempty"`
451 BuildBrokenInputDirModules []string `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900452
Jiakai Zhang48203e32023-06-02 23:42:21 +0100453 BuildWarningBadOptionalUsesLibsAllowlist []string `json:",omitempty"`
454
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700455 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
456
Inseob Kim67e5add192021-03-17 18:05:33 +0900457 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
458
459 SelinuxIgnoreNeverallows bool `json:",omitempty"`
460
Devin Moorec381e102023-07-06 22:03:58 +0000461 Release_aidl_use_unfrozen *bool `json:",omitempty"`
462
Inseob Kima10ef272021-09-15 03:04:53 +0000463 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
464 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900465
466 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Christopher Ferris98f10222022-07-13 23:16:52 -0700467
468 IgnorePrefer32OnDevice bool `json:",omitempty"`
Spandan Dasc5763832022-11-08 18:42:16 +0000469
Sam Delmerico98a73292023-02-21 11:50:29 -0500470 IncludeTags []string `json:",omitempty"`
471 SourceRootDirs []string `json:",omitempty"`
Vinh Tran44cb78c2023-03-09 22:07:19 -0500472
473 AfdoProfiles []string `json:",omitempty"`
Wei Lic642d682023-05-04 09:06:06 -0700474
475 ProductManufacturer string `json:",omitempty"`
476 ProductBrand string `json:",omitempty"`
477 BuildVersionTags []string `json:",omitempty"`
Joe Onoratofee845a2023-05-09 08:14:14 -0700478
Yu Liu2cc802a2023-09-05 17:19:45 -0700479 ReleaseVersion string `json:",omitempty"`
480 ReleaseAconfigValueSets string `json:",omitempty"`
Kiyoung Kime623c582023-07-06 16:43:44 +0900481
Zhi Dou3f65a412023-08-10 21:47:40 +0000482 ReleaseAconfigFlagDefaultPermission string `json:",omitempty"`
483
Kiyoung Kime623c582023-07-06 16:43:44 +0900484 KeepVndk *bool `json:",omitempty"`
Inseob Kim5bac3b62023-08-25 21:29:46 +0900485
486 CheckVendorSeappViolations *bool `json:",omitempty"`
Cole Faustb4cb0c82023-09-14 15:16:58 -0700487
Cole Faustcb193ec2023-09-20 16:01:18 -0700488 // PartitionVarsForBazelMigrationOnlyDoNotUse are extra variables that are used to define the
489 // partition images. They should not be read from soong modules.
490 PartitionVarsForBazelMigrationOnlyDoNotUse PartitionVariables `json:",omitempty"`
Jihoon Kangcfbc4072023-09-20 01:02:18 +0000491
492 NextReleaseHideFlaggedApi *bool `json:",omitempty"`
Jihoon Kangf3aa3222023-09-26 22:32:08 +0000493
494 Release_expose_flagged_api *bool `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700495}
496
497type PartitionVariables struct {
498 ProductDirectory string `json:",omitempty"`
499 PartitionQualifiedVariables map[string]struct {
500 BuildingImage bool `json:",omitempty"`
501 BoardErofsCompressor string `json:",omitempty"`
502 BoardErofsCompressHints string `json:",omitempty"`
503 BoardErofsPclusterSize string `json:",omitempty"`
504 BoardExtfsInodeCount string `json:",omitempty"`
505 BoardExtfsRsvPct string `json:",omitempty"`
506 BoardF2fsSloadCompressFlags string `json:",omitempty"`
507 BoardFileSystemCompress string `json:",omitempty"`
508 BoardFileSystemType string `json:",omitempty"`
509 BoardJournalSize string `json:",omitempty"`
510 BoardPartitionReservedSize string `json:",omitempty"`
511 BoardPartitionSize string `json:",omitempty"`
512 BoardSquashfsBlockSize string `json:",omitempty"`
513 BoardSquashfsCompressor string `json:",omitempty"`
514 BoardSquashfsCompressorOpt string `json:",omitempty"`
515 BoardSquashfsDisable4kAlign string `json:",omitempty"`
516 ProductBaseFsPath string `json:",omitempty"`
517 ProductHeadroom string `json:",omitempty"`
518 ProductVerityPartition string `json:",omitempty"`
519 }
520 TargetUserimagesUseExt2 bool `json:",omitempty"`
521 TargetUserimagesUseExt3 bool `json:",omitempty"`
522 TargetUserimagesUseExt4 bool `json:",omitempty"`
523
524 TargetUserimagesSparseExtDisabled bool `json:",omitempty"`
525 TargetUserimagesSparseErofsDisabled bool `json:",omitempty"`
526 TargetUserimagesSparseSquashfsDisabled bool `json:",omitempty"`
527 TargetUserimagesSparseF2fsDisabled bool `json:",omitempty"`
528
529 BoardErofsCompressor string `json:",omitempty"`
530 BoardErofsCompressorHints string `json:",omitempty"`
531 BoardErofsPclusterSize string `json:",omitempty"`
532 BoardErofsShareDupBlocks string `json:",omitempty"`
533 BoardErofsUseLegacyCompression string `json:",omitempty"`
534 BoardExt4ShareDupBlocks string `json:",omitempty"`
535 BoardFlashLogicalBlockSize string `json:",omitempty"`
536 BoardFlashEraseBlockSize string `json:",omitempty"`
537 BoardUsesRecoveryAsBoot bool `json:",omitempty"`
538 BoardBuildGkiBootImageWithoutRamdisk bool `json:",omitempty"`
539 ProductUseDynamicPartitionSize bool `json:",omitempty"`
540 CopyImagesForTargetFilesZip bool `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700541}
542
543func boolPtr(v bool) *bool {
544 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700545}
546
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700547func intPtr(v int) *int {
548 return &v
549}
550
Colin Cross4225f652015-09-17 14:33:42 -0700551func stringPtr(v string) *string {
552 return &v
553}
554
Cole Faustf8231dd2023-04-21 17:37:11 -0700555func (v *ProductVariables) SetDefaultConfig() {
556 *v = ProductVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800557 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700558
Dan Albert8c7a9942023-03-27 20:34:01 +0000559 Platform_version_name: stringPtr("S"),
560 Platform_base_sdk_extension_version: intPtr(30),
561 Platform_sdk_version: intPtr(30),
562 Platform_sdk_codename: stringPtr("S"),
563 Platform_sdk_final: boolPtr(false),
564 Platform_version_active_codenames: []string{"S"},
565 Platform_version_all_preview_codenames: []string{"S"},
566 Platform_vndk_version: stringPtr("S"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800567
Colin Cross4225f652015-09-17 14:33:42 -0700568 HostArch: stringPtr("x86_64"),
569 HostSecondaryArch: stringPtr("x86"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800570 DeviceName: stringPtr("generic_arm64"),
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000571 DeviceProduct: stringPtr("aosp_arm-eng"),
Colin Cross4225f652015-09-17 14:33:42 -0700572 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700573 DeviceArchVariant: stringPtr("armv8-a"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800574 DeviceCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800575 DeviceAbi: []string{"arm64-v8a"},
Colin Cross4225f652015-09-17 14:33:42 -0700576 DeviceSecondaryArch: stringPtr("arm"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800577 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
578 DeviceSecondaryCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800579 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
Juan Yescas05d4d902023-04-07 10:35:35 -0700580 DeviceMaxPageSizeSupported: stringPtr("4096"),
Juan Yescas01065602023-08-09 08:34:37 -0700581 DevicePageSizeAgnostic: boolPtr(false),
Colin Crossface4e42017-10-30 17:32:15 -0700582
Colin Crossa74ca042019-01-31 14:31:51 -0800583 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700584 AAPTPreferredConfig: stringPtr("xhdpi"),
585 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800586 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700587
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700588 Malloc_not_svelte: boolPtr(true),
Steven Morelandd1342012020-07-30 20:38:49 +0000589 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700590 Malloc_pattern_fill_contents: boolPtr(false),
591 Safestack: boolPtr(false),
Dennis Shene2ed70c2023-01-11 14:15:43 +0000592 TrimmedApex: boolPtr(false),
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000593 Build_from_text_stub: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100594
satayevd604b212021-07-21 14:23:52 +0100595 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
596 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700597 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800598
599 if runtime.GOOS == "linux" {
600 v.CrossHost = stringPtr("windows")
601 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800602 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800603 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700604}
605
Liz Kammera060c452021-03-24 10:14:47 -0400606// ProductConfigContext requires the access to the Module to get product config properties.
607type ProductConfigContext interface {
608 Module() Module
609}
610
Cole Faust150f9a52023-04-26 10:52:24 -0700611// ProductConfigOrSoongConfigProperty represents either a soong config variable + its value
612// or a product config variable. You can get both a ConfigurationAxis and a SelectKey from it
613// for use in bazel attributes. ProductVariableProperties() will return a map from properties ->
614// this interface -> property structs for use in bp2build converters
615type ProductConfigOrSoongConfigProperty interface {
616 // Name of the product variable or soong config variable
617 Name() string
618 // AlwaysEmit returns true for soong config variables but false for product variables. This
619 // is intended to indicate if we need to always emit empty lists in the select statements.
620 AlwaysEmit() bool
621 // ConfigurationAxis returns the bazel.ConfigurationAxis that represents this variable. The
622 // configuration axis will change depending on the variable and whether it's arch/os variant
623 // as well.
624 ConfigurationAxis() bazel.ConfigurationAxis
625 // SelectKey returns a string that represents the key of a select branch, however, it is not
626 // actually the real label written out to the build file.
627 // this.ConfigurationAxis().SelectKey(this.SelectKey()) will give the actual label.
628 SelectKey() string
629}
630
631// ProductConfigProperty represents a product config variable, and if it is arch-variant or not.
Liz Kammera060c452021-03-24 10:14:47 -0400632type ProductConfigProperty struct {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000633 // The name of the product variable, e.g. "safestack", "malloc_not_svelte",
634 // "board"
Cole Faust150f9a52023-04-26 10:52:24 -0700635 name string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000636
Cole Faust150f9a52023-04-26 10:52:24 -0700637 arch string
Liz Kammera060c452021-03-24 10:14:47 -0400638}
639
Cole Faust150f9a52023-04-26 10:52:24 -0700640func (p ProductConfigProperty) Name() string {
641 return p.name
Jingwen Chen58ff6802021-11-17 12:14:41 +0000642}
643
Cole Faust150f9a52023-04-26 10:52:24 -0700644func (p ProductConfigProperty) AlwaysEmit() bool {
645 return false
646}
647
648func (p ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
649 return bazel.ProductVariableConfigurationAxis(p.arch != "", p.name+"__"+p.arch)
650}
651
652func (p ProductConfigProperty) SelectKey() string {
653 if p.arch == "" {
654 return strings.ToLower(p.name)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000655 } else {
Cole Faust150f9a52023-04-26 10:52:24 -0700656 return strings.ToLower(p.name + "-" + p.arch)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000657 }
658}
659
Cole Faust150f9a52023-04-26 10:52:24 -0700660// SoongConfigProperty represents a soong config variable, its value if it's a string variable,
661// and if it's dependent on the OS or not
662type SoongConfigProperty struct {
663 name string
664 namespace string
665 // Can be an empty string for bool/value soong config variables
666 value string
667 // If there is a target: field inside a soong config property struct, the os that it selects
668 // on will be represented here.
669 os string
670}
671
672func (p SoongConfigProperty) Name() string {
673 return p.name
674}
675
676func (p SoongConfigProperty) AlwaysEmit() bool {
677 return true
678}
679
680func (p SoongConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
681 return bazel.ProductVariableConfigurationAxis(false, p.namespace+"__"+p.name+"__"+p.os)
682}
683
Jingwen Chen25825ca2021-11-15 12:28:43 +0000684// SelectKey returns the literal string that represents this variable in a BUILD
685// select statement.
Cole Faust150f9a52023-04-26 10:52:24 -0700686func (p SoongConfigProperty) SelectKey() string {
687 // p.value being conditions_default can happen with or without a desired os. When not using
688 // an os, we want to emit literally just //conditions:default in the select statement, but
689 // when using an os, we want to emit namespace__name__conditions_default__os, so that
690 // the branch is only taken if the variable is not set, and we're on the desired os.
691 // ConfigurationAxis#SelectKey will map the conditions_default result of this function to
692 // //conditions:default.
693 if p.value == bazel.ConditionsDefaultConfigKey && p.os == "" {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000694 return bazel.ConditionsDefaultConfigKey
695 }
696
Cole Faust150f9a52023-04-26 10:52:24 -0700697 parts := []string{p.namespace, p.name}
698 if p.value != "" && p.value != bazel.ConditionsDefaultSelectKey {
699 parts = append(parts, p.value)
700 }
701 if p.os != "" {
702 parts = append(parts, p.os)
Jingwen Chen25825ca2021-11-15 12:28:43 +0000703 }
Sam Delmerico0e33c9d2022-01-07 20:39:21 +0000704
Cole Faust150f9a52023-04-26 10:52:24 -0700705 // e.g. acme__feature1, android__board__soc_a, my_namespace__my_variables__my_value__my_os
706 return strings.ToLower(strings.Join(parts, "__"))
Jingwen Chen25825ca2021-11-15 12:28:43 +0000707}
708
709// ProductConfigProperties is a map of maps to group property values according
710// their property name and the product config variable they're set under.
711//
712// The outer map key is the name of the property, like "cflags".
713//
714// The inner map key is a ProductConfigProperty, which is a struct of product
715// variable name, namespace, and the "full configuration" of the product
716// variable.
717//
718// e.g. product variable name: board, namespace: acme, full config: vendor_chip_foo
719//
720// The value of the map is the interface{} representing the value of the
721// property, like ["-DDEFINES"] for cflags.
Cole Faust150f9a52023-04-26 10:52:24 -0700722type ProductConfigProperties map[string]map[ProductConfigOrSoongConfigProperty]interface{}
Liz Kammera060c452021-03-24 10:14:47 -0400723
724// ProductVariableProperties returns a ProductConfigProperties containing only the properties which
Cole Faust912bc882023-03-08 12:29:50 -0800725// have been set for the given module.
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400726func ProductVariableProperties(ctx ArchVariantContext, module Module) (ProductConfigProperties, []error) {
727 var errs []error
Liz Kammera060c452021-03-24 10:14:47 -0400728 moduleBase := module.base()
729
730 productConfigProperties := ProductConfigProperties{}
731
Jingwen Chena47f28d2021-11-02 16:43:57 +0000732 if moduleBase.variableProperties != nil {
733 productVariablesProperty := proptools.FieldNameForProperty("product_variables")
Cole Fausted940002023-08-15 11:59:24 -0700734 if moduleBase.ArchSpecific() {
735 for /* axis */ _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
736 for config, props := range configToProps {
737 variableValues := reflect.ValueOf(props).Elem().FieldByName(productVariablesProperty)
738 productConfigProperties.AddProductConfigProperties(variableValues, config)
739 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000740 }
Cole Fausted940002023-08-15 11:59:24 -0700741 } else {
742 variableValues := reflect.ValueOf(moduleBase.variableProperties).Elem().FieldByName(productVariablesProperty)
743 productConfigProperties.AddProductConfigProperties(variableValues, "")
Jingwen Chena47f28d2021-11-02 16:43:57 +0000744 }
Liz Kammera060c452021-03-24 10:14:47 -0400745 }
746
Jingwen Chena47f28d2021-11-02 16:43:57 +0000747 if m, ok := module.(Bazelable); ok && m.namespacedVariableProps() != nil {
Jingwen Chen84817de2021-11-17 10:57:35 +0000748 for namespace, namespacedVariableProps := range m.namespacedVariableProps() {
749 for _, namespacedVariableProp := range namespacedVariableProps {
Cole Faust150f9a52023-04-26 10:52:24 -0700750 variableValues := reflect.ValueOf(namespacedVariableProp).Elem().FieldByName(soongconfig.SoongConfigProperty)
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400751 err := productConfigProperties.AddSoongConfigProperties(namespace, variableValues)
752 if err != nil {
753 errs = append(errs, err)
754 }
Jingwen Chen84817de2021-11-17 10:57:35 +0000755 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400756 }
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400757 }
758
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400759 return productConfigProperties, errs
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400760}
761
Jingwen Chena47f28d2021-11-02 16:43:57 +0000762func (p *ProductConfigProperties) AddProductConfigProperty(
Cole Faust150f9a52023-04-26 10:52:24 -0700763 propertyName, productVariableName, arch string, propertyValue interface{}) {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000764
Jingwen Chen25825ca2021-11-15 12:28:43 +0000765 productConfigProp := ProductConfigProperty{
Cole Faust150f9a52023-04-26 10:52:24 -0700766 name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
767 arch: arch, // e.g. "", x86, arm64
Jingwen Chena47f28d2021-11-02 16:43:57 +0000768 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000769
Cole Faust150f9a52023-04-26 10:52:24 -0700770 p.AddEitherProperty(propertyName, productConfigProp, propertyValue)
771}
772
773func (p *ProductConfigProperties) AddSoongConfigProperty(
774 propertyName, namespace, variableName, value, os string, propertyValue interface{}) {
775
776 soongConfigProp := SoongConfigProperty{
777 namespace: namespace,
778 name: variableName, // e.g. size, feature1, feature2, FEATURE3, board
779 value: value,
780 os: os, // e.g. android, linux_x86
781 }
782
783 p.AddEitherProperty(propertyName, soongConfigProp, propertyValue)
784}
785
786func (p *ProductConfigProperties) AddEitherProperty(
787 propertyName string, key ProductConfigOrSoongConfigProperty, propertyValue interface{}) {
788 if (*p)[propertyName] == nil {
789 (*p)[propertyName] = make(map[ProductConfigOrSoongConfigProperty]interface{})
790 }
791
792 if existing, ok := (*p)[propertyName][key]; ok {
Jingwen Chen84817de2021-11-17 10:57:35 +0000793 switch dst := existing.(type) {
794 case []string:
Cole Faust150f9a52023-04-26 10:52:24 -0700795 src, ok := propertyValue.([]string)
796 if !ok {
797 panic("Conflicting types")
Jingwen Chen84817de2021-11-17 10:57:35 +0000798 }
Cole Faust150f9a52023-04-26 10:52:24 -0700799 dst = append(dst, src...)
800 (*p)[propertyName][key] = dst
Jingwen Chen84817de2021-11-17 10:57:35 +0000801 default:
Spandan Das7b26e0f2023-07-28 01:31:15 +0000802 if existing != propertyValue {
803 panic(fmt.Errorf("TODO: handle merging value %#v", existing))
804 }
Jingwen Chen84817de2021-11-17 10:57:35 +0000805 }
806 } else {
Cole Faust150f9a52023-04-26 10:52:24 -0700807 (*p)[propertyName][key] = propertyValue
Jingwen Chen84817de2021-11-17 10:57:35 +0000808 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000809}
810
811var (
812 conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
813)
814
815// maybeExtractConfigVarProp attempts to read this value as a config var struct
816// wrapped by interfaces and ptrs. If it's not the right type, the second return
817// value is false.
818func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
819 if v.Kind() == reflect.Interface {
820 // The conditions_default value can be either
821 // 1) an ptr to an interface of a struct (bool config variables and product variables)
822 // 2) an interface of 1) (config variables with nested structs, like string vars)
823 v = v.Elem()
824 }
825 if v.Kind() != reflect.Ptr {
826 return v, false
827 }
828 v = reflect.Indirect(v)
829 if v.Kind() == reflect.Interface {
830 // Extract the struct from the interface
831 v = v.Elem()
832 }
833
834 if !v.IsValid() {
835 return v, false
836 }
837
838 if v.Kind() != reflect.Struct {
839 return v, false
840 }
841 return v, true
842}
843
Cole Faust150f9a52023-04-26 10:52:24 -0700844func (productConfigProperties *ProductConfigProperties) AddProductConfigProperties(variableValues reflect.Value, arch string) {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000845 // Example of product_variables:
846 //
847 // product_variables: {
848 // malloc_not_svelte: {
849 // shared_libs: ["malloc_not_svelte_shared_lib"],
850 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
851 // exclude_static_libs: [
852 // "malloc_not_svelte_static_lib_excludes",
853 // "malloc_not_svelte_whole_static_lib_excludes",
854 // ],
855 // },
856 // },
Cole Faust150f9a52023-04-26 10:52:24 -0700857
Liz Kammera060c452021-03-24 10:14:47 -0400858 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000859 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
860 productVariableName := variableValues.Type().Field(i).Name
861
Liz Kammera060c452021-03-24 10:14:47 -0400862 variableValue := variableValues.Field(i)
863 // Check if any properties were set for the module
864 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000865 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400866 continue
867 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000868
Liz Kammera060c452021-03-24 10:14:47 -0400869 for j := 0; j < variableValue.NumField(); j++ {
870 property := variableValue.Field(j)
Liz Kammerf3963f82022-12-21 14:47:18 -0500871 // e.g. Asflags, Cflags, Enabled, etc.
872 propertyName := variableValue.Type().Field(j).Name
Cole Faust150f9a52023-04-26 10:52:24 -0700873 if property.Kind() != reflect.Interface {
874 productConfigProperties.AddProductConfigProperty(propertyName, productVariableName, arch, property.Interface())
875 }
876 }
877 }
878
879}
880
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400881func (productConfigProperties *ProductConfigProperties) AddSoongConfigProperties(namespace string, soongConfigVariablesStruct reflect.Value) error {
Cole Faust150f9a52023-04-26 10:52:24 -0700882 //
883 // Example of soong_config_variables:
884 //
885 // soong_config_variables: {
886 // feature1: {
887 // conditions_default: {
888 // ...
889 // },
890 // cflags: ...
891 // },
892 // feature2: {
893 // cflags: ...
894 // conditions_default: {
895 // ...
896 // },
897 // },
898 // board: {
899 // soc_a: {
900 // ...
901 // },
902 // soc_b: {
903 // ...
904 // },
905 // soc_c: {},
906 // conditions_default: {
907 // ...
908 // },
909 // },
910 // }
911 for i := 0; i < soongConfigVariablesStruct.NumField(); i++ {
912 // e.g. feature1, feature2, board
913 variableName := soongConfigVariablesStruct.Type().Field(i).Name
914 variableStruct := soongConfigVariablesStruct.Field(i)
915 // Check if any properties were set for the module
916 if variableStruct.IsZero() {
917 // e.g. feature1: {}
918 continue
919 }
920
921 // Unlike product variables, config variables require a few more
922 // indirections to extract the struct from the reflect.Value.
923 if v, ok := maybeExtractConfigVarProp(variableStruct); ok {
924 variableStruct = v
liulvping4d11d102023-05-24 10:27:44 +0800925 } else if !v.IsValid() {
926 // Skip invalid variables which may not used, else leads to panic
927 continue
Cole Faust150f9a52023-04-26 10:52:24 -0700928 }
929
930 for j := 0; j < variableStruct.NumField(); j++ {
931 propertyOrStruct := variableStruct.Field(j)
932 // propertyOrValueName can either be:
933 // - A property, like: Asflags, Cflags, Enabled, etc.
934 // - A soong config string variable's value, like soc_a, soc_b, soc_c in the example above
935 // - "conditions_default"
936 propertyOrValueName := variableStruct.Type().Field(j).Name
Liz Kammerf3963f82022-12-21 14:47:18 -0500937
Liz Kammera060c452021-03-24 10:14:47 -0400938 // If the property wasn't set, no need to pass it along
Cole Faust150f9a52023-04-26 10:52:24 -0700939 if propertyOrStruct.IsZero() {
Liz Kammera060c452021-03-24 10:14:47 -0400940 continue
941 }
942
Cole Faust150f9a52023-04-26 10:52:24 -0700943 if v, ok := maybeExtractConfigVarProp(propertyOrStruct); ok {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000944 // The field is a struct, which is used by:
945 // 1) soong_config_string_variables
946 //
947 // soc_a: {
948 // cflags: ...,
949 // }
950 //
951 // soc_b: {
952 // cflags: ...,
953 // }
954 //
955 // 2) conditions_default structs for all soong config variable types.
956 //
957 // conditions_default: {
958 // cflags: ...,
959 // static_libs: ...
960 // }
Cole Faust150f9a52023-04-26 10:52:24 -0700961 //
962 // This means that propertyOrValueName is either conditions_default, or a soong
963 // config string variable's value.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000964 field := v
Liz Kammerf3963f82022-12-21 14:47:18 -0500965 // Iterate over fields of this struct prop.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000966 for k := 0; k < field.NumField(); k++ {
Liz Kammerf3963f82022-12-21 14:47:18 -0500967 // For product variables, zero values are irrelevant; however, for soong config variables,
968 // empty values are relevant because there can also be a conditions default which is not
969 // applied for empty variables.
970 if field.Field(k).IsZero() && namespace == "" {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000971 continue
972 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000973
Cole Faust150f9a52023-04-26 10:52:24 -0700974 propertyName := field.Type().Field(k).Name
975 if propertyName == "Target" {
976 productConfigProperties.AddSoongConfigPropertiesFromTargetStruct(namespace, variableName, proptools.PropertyNameForField(propertyOrValueName), field.Field(k))
977 } else if propertyName == "Arch" || propertyName == "Multilib" {
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400978 return fmt.Errorf("Arch/Multilib are not currently supported in soong config variable structs")
Cole Faust150f9a52023-04-26 10:52:24 -0700979 } else {
980 productConfigProperties.AddSoongConfigProperty(propertyName, namespace, variableName, proptools.PropertyNameForField(propertyOrValueName), "", field.Field(k).Interface())
981 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000982 }
Cole Faust150f9a52023-04-26 10:52:24 -0700983 } else if propertyOrStruct.Kind() != reflect.Interface {
Jingwen Chen58ff6802021-11-17 12:14:41 +0000984 // If not an interface, then this is not a conditions_default or
Cole Faust150f9a52023-04-26 10:52:24 -0700985 // a struct prop. That is, this is a bool/value config variable.
986 if propertyOrValueName == "Target" {
987 productConfigProperties.AddSoongConfigPropertiesFromTargetStruct(namespace, variableName, "", propertyOrStruct)
988 } else if propertyOrValueName == "Arch" || propertyOrValueName == "Multilib" {
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400989 return fmt.Errorf("Arch/Multilib are not currently supported in soong config variable structs")
Cole Faust150f9a52023-04-26 10:52:24 -0700990 } else {
991 productConfigProperties.AddSoongConfigProperty(propertyOrValueName, namespace, variableName, "", "", propertyOrStruct.Interface())
992 }
Liz Kammer47535c52021-06-02 16:02:22 -0400993 }
Liz Kammera060c452021-03-24 10:14:47 -0400994 }
995 }
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400996 return nil
Liz Kammera060c452021-03-24 10:14:47 -0400997}
998
Cole Faust150f9a52023-04-26 10:52:24 -0700999func (productConfigProperties *ProductConfigProperties) AddSoongConfigPropertiesFromTargetStruct(namespace, soongConfigVariableName string, soongConfigVariableValue string, targetStruct reflect.Value) {
1000 // targetStruct will be a struct with fields like "android", "host", "arm", "x86",
1001 // "android_arm", etc. The values of each of those fields will be a regular property struct.
1002 for i := 0; i < targetStruct.NumField(); i++ {
1003 targetFieldName := targetStruct.Type().Field(i).Name
1004 archOrOsSpecificStruct := targetStruct.Field(i)
1005 for j := 0; j < archOrOsSpecificStruct.NumField(); j++ {
1006 property := archOrOsSpecificStruct.Field(j)
1007 // e.g. Asflags, Cflags, Enabled, etc.
1008 propertyName := archOrOsSpecificStruct.Type().Field(j).Name
Jingwen Chen84817de2021-11-17 10:57:35 +00001009
Cole Faust150f9a52023-04-26 10:52:24 -07001010 if targetFieldName == "Android" {
1011 productConfigProperties.AddSoongConfigProperty(propertyName, namespace, soongConfigVariableName, soongConfigVariableValue, "android", property.Interface())
1012 } else if targetFieldName == "Host" {
1013 for _, os := range osTypeList {
1014 if os.Class == Host {
1015 productConfigProperties.AddSoongConfigProperty(propertyName, namespace, soongConfigVariableName, soongConfigVariableValue, os.Name, property.Interface())
1016 }
1017 }
Spandan Das474d43b2023-07-27 23:01:06 +00001018 } else if !archOrOsSpecificStruct.IsZero() {
Cole Faust150f9a52023-04-26 10:52:24 -07001019 // One problem with supporting additional fields is that if multiple branches of
1020 // "target" overlap, we don't want them to be in the same select statement (aka
1021 // configuration axis). "android" and "host" are disjoint, so it's ok that we only
1022 // have 2 axes right now. (soongConfigVariables and soongConfigVariablesPlusOs)
1023 panic("TODO: support other target types in soong config variable structs: " + targetFieldName)
1024 }
1025 }
1026 }
Jingwen Chen84817de2021-11-17 10:57:35 +00001027}
1028
Colin Crosseabaedd2020-02-06 17:01:55 -08001029func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001030 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -07001031 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -07001032 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001033 return
1034 }
1035
1036 // TODO: depend on config variable, create variants, propagate variants up tree
1037 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -07001038
1039 if a.variableProperties == nil {
1040 return
1041 }
1042
1043 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001044
Colin Cross0cec3122021-01-20 11:27:32 -08001045 productVariables := reflect.ValueOf(mctx.Config().productVariables)
1046
Colin Cross7f64b6d2015-07-09 13:57:48 -07001047 for i := 0; i < variableValues.NumField(); i++ {
1048 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -07001049 name := variableValues.Type().Field(i).Name
1050 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001051
Colin Crossa6bc19e2015-09-16 13:53:42 -07001052 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -08001053 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -07001054 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
1055 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -07001056 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001057
1058 val = val.Elem()
1059
1060 // For bools, check that the value is true
1061 if val.Kind() == reflect.Bool && val.Bool() == false {
1062 continue
1063 }
1064
1065 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -08001066 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -07001067 continue
1068 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001069 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -07001070 }
1071}
1072
Colin Cross4157e882019-06-06 16:57:04 -07001073func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -07001074 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
1075
Colin Cross7e0eaf12017-05-05 16:16:24 -07001076 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001077
Usta851a3272022-01-05 23:42:33 -05001078 err := proptools.AppendMatchingProperties(m.GetProperties(),
Colin Cross06a931b2015-10-28 17:23:31 -07001079 productVariablePropertyValue.Addr().Interface(), nil)
1080 if err != nil {
1081 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1082 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1083 } else {
1084 panic(err)
1085 }
1086 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001087}
1088
Colin Cross7e0eaf12017-05-05 16:16:24 -07001089func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
1090 productVariablePropertyValue reflect.Value, i int, err error) {
1091
1092 field := productVariablePropertyValue.Type().Field(i).Name
1093 property := prefix + "." + proptools.PropertyNameForField(field)
1094 ctx.PropertyErrorf(property, "%s", err)
1095}
1096
1097func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
1098 productVariablePropertyValue reflect.Value, variableValue interface{}) {
1099
Colin Cross7f64b6d2015-07-09 13:57:48 -07001100 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
1101 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -08001102 kind := propertyValue.Kind()
1103 if kind == reflect.Ptr {
1104 if propertyValue.IsNil() {
1105 continue
1106 }
1107 propertyValue = propertyValue.Elem()
1108 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001109 switch propertyValue.Kind() {
1110 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001111 err := printfIntoProperty(propertyValue, variableValue)
1112 if err != nil {
1113 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1114 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001115 case reflect.Slice:
1116 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001117 err := printfIntoProperty(propertyValue.Index(j), variableValue)
1118 if err != nil {
1119 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1120 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001121 }
Colin Crossdd0dbe62015-12-02 15:24:38 -08001122 case reflect.Bool:
1123 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001124 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001125 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001126 default:
1127 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
1128 }
1129 }
1130}
1131
Colin Cross7e0eaf12017-05-05 16:16:24 -07001132func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001133 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -07001134
1135 count := strings.Count(s, "%")
1136 if count == 0 {
1137 return nil
1138 }
1139
1140 if count > 1 {
1141 return fmt.Errorf("product variable properties only support a single '%%'")
1142 }
1143
Colin Cross7f64b6d2015-07-09 13:57:48 -07001144 if strings.Contains(s, "%d") {
1145 switch v := variableValue.(type) {
1146 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001147 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001148 case bool:
1149 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001150 variableValue = 1
1151 } else {
1152 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -07001153 }
1154 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001155 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001156 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001157 } else if strings.Contains(s, "%s") {
1158 switch variableValue.(type) {
1159 case string:
1160 // Nothing
1161 default:
1162 return fmt.Errorf("unsupported type %T for %%s", variableValue)
1163 }
1164 } else {
1165 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001166 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001167
1168 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
1169
1170 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -07001171}
Colin Cross18c46802019-09-24 22:19:02 -07001172
1173var variablePropTypeMap OncePer
1174
1175// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
1176// reflect.Types of each property struct. The result can be used as a key in a map.
1177func sliceToTypeArray(s []interface{}) interface{} {
1178 // Create an array using reflection whose length is the length of the input slice
1179 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
1180 for i, e := range s {
1181 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
1182 }
1183 return ret.Interface()
1184}
1185
Colin Crosseabaedd2020-02-06 17:01:55 -08001186func initProductVariableModule(m Module) {
1187 base := m.base()
1188
1189 // Allow tests to override the default product variables
1190 if base.variableProperties == nil {
1191 base.variableProperties = defaultProductVariables
1192 }
1193 // Filter the product variables properties to the ones that exist on this module
1194 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
1195 if base.variableProperties != nil {
1196 m.AddProperties(base.variableProperties)
1197 }
1198}
1199
Colin Cross18c46802019-09-24 22:19:02 -07001200// createVariableProperties takes the list of property structs for a module and returns a property struct that
1201// contains the product variable properties that exist in the property structs, or nil if there are none. It
1202// caches the result.
1203func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
1204 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
1205 key := sliceToTypeArray(moduleTypeProps)
1206
1207 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
1208 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
1209 // Compute the filtered property struct type.
1210 return createVariablePropertiesType(moduleTypeProps, productVariables)
1211 }).(reflect.Type)
1212
1213 if typ == nil {
1214 return nil
1215 }
1216
1217 // Create a new pointer to a filtered property struct.
1218 return reflect.New(typ).Interface()
1219}
1220
1221// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1222// a list of property structs.
1223func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1224 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1225 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1226 // Filter function, returns true if the field should be in the resulting struct
1227 if prefix == "" {
1228 // Keep the top level Product_variables field
1229 return true, field
1230 }
1231 _, rest := splitPrefix(prefix)
1232 if rest == "" {
1233 // Keep the 2nd level field (i.e. Product_variables.Eng)
1234 return true, field
1235 }
1236
1237 // Strip off the first 2 levels of the prefix
1238 _, prefix = splitPrefix(rest)
1239
1240 for _, p := range moduleTypeProps {
1241 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1242 // Keep any fields that exist in one of the property structs
1243 return true, field
1244 }
1245 }
1246
1247 return false, field
1248 })
1249 return typ
1250}
1251
1252func splitPrefix(prefix string) (first, rest string) {
1253 index := strings.IndexByte(prefix, '.')
1254 if index == -1 {
1255 return prefix, ""
1256 }
1257 return prefix[:index], prefix[index+1:]
1258}
1259
1260func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1261 if t.Kind() != reflect.Struct {
1262 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1263 }
1264
1265 if prefix != "" {
1266 split := strings.SplitN(prefix, ".", 2)
1267 firstPrefix := split[0]
1268 rest := ""
1269 if len(split) > 1 {
1270 rest = split[1]
1271 }
1272 f, exists := t.FieldByName(firstPrefix)
1273 if !exists {
1274 return false
1275 }
1276 ft := f.Type
1277 if ft.Kind() == reflect.Ptr {
1278 ft = ft.Elem()
1279 }
1280 if ft.Kind() != reflect.Struct {
1281 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1282 }
1283 return fieldExistsByNameRecursive(ft, rest, name)
1284 } else {
1285 _, exists := t.FieldByName(name)
1286 return exists
1287 }
1288}