blob: 89cd59ef28a8099f30ccadc04a561f5bcae23060 [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 (
Jingwen Chena47f28d2021-11-02 16:43:57 +000018 "android/soong/android/soongconfig"
19 "android/soong/bazel"
Colin Cross7f64b6d2015-07-09 13:57:48 -070020 "fmt"
21 "reflect"
Dan Willemsen490fd492015-11-24 17:53:15 -080022 "runtime"
Colin Cross7f64b6d2015-07-09 13:57:48 -070023 "strings"
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
Dan Willemsen97cae012016-03-01 14:08:42 -080066 Malloc_not_svelte struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080067 Cflags []string `android:"arch_variant"`
68 Shared_libs []string `android:"arch_variant"`
69 Whole_static_libs []string `android:"arch_variant"`
70 Exclude_static_libs []string `android:"arch_variant"`
Christopher Ferrisdea66172021-09-24 00:07:53 -070071 Srcs []string `android:"arch_variant"`
72 Header_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070073 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070074
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070075 Malloc_zero_contents struct {
76 Cflags []string `android:"arch_variant"`
77 } `android:"arch_variant"`
78
79 Malloc_pattern_fill_contents struct {
80 Cflags []string `android:"arch_variant"`
81 } `android:"arch_variant"`
82
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070083 Safestack struct {
84 Cflags []string `android:"arch_variant"`
85 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070086
Dan Willemsen1be35382016-07-25 17:42:18 -070087 Binder32bit struct {
88 Cflags []string
89 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070090
Colin Cross95436422017-05-04 13:57:05 -070091 Override_rs_driver struct {
92 Cflags []string
93 }
94
Steven Morelandc2b9f062017-12-13 14:06:20 -080095 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
96 // enabled.
97 Treble_linker_namespaces struct {
98 Cflags []string
99 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800100 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
101 Enforce_vintf_manifest struct {
102 Cflags []string
103 }
104
Colin Cross6bc59ef2016-12-08 09:46:35 -0800105 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
106 // debugging features that don't significantly impact runtime behavior. userdebug builds
107 // are used for dogfooding and performance testing, and should be as similar to user builds
108 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700109 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700110 Cflags []string
111 Cppflags []string
112 Init_rc []string
113 Required []string
114 Host_required []string
115 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700116 Strip struct {
117 All *bool
118 Keep_symbols *bool
119 Keep_symbols_and_debug_frame *bool
120 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700121 Static_libs []string
122 Whole_static_libs []string
123 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900124
125 Cmdline []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700126 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800127
128 // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging
129 // features.
130 Eng struct {
131 Cflags []string
132 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700133 Lto struct {
134 Never *bool
135 }
Colin Cross3273cc22018-10-26 23:58:42 -0700136 Sanitize struct {
137 Address *bool
138 }
Felka Changf13642e2019-06-21 20:58:27 +0800139 Optimize struct {
140 Enabled *bool
141 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800142 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700143
144 Pdk struct {
Colin Cross99967a72017-10-17 13:55:02 -0700145 Enabled *bool `android:"arch_variant"`
146 } `android:"arch_variant"`
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700147
148 Uml struct {
149 Cppflags []string
150 }
Risanea004dd2017-11-27 18:14:46 +0900151
152 Arc struct {
Satoshi Niwa54f48262020-12-18 09:53:26 +0900153 Cflags []string `android:"arch_variant"`
154 Exclude_srcs []string `android:"arch_variant"`
Jasmine Chenacace422021-03-05 00:56:40 +0800155 Header_libs []string `android:"arch_variant"`
Satoshi Niwa54f48262020-12-18 09:53:26 +0900156 Include_dirs []string `android:"arch_variant"`
157 Shared_libs []string `android:"arch_variant"`
158 Static_libs []string `android:"arch_variant"`
159 Srcs []string `android:"arch_variant"`
160 Whole_static_libs []string `android:"arch_variant"`
161 } `android:"arch_variant"`
Roland Levillain2879d412019-08-13 15:00:18 +0100162
163 Flatten_apex struct {
164 Enabled *bool
165 }
Peter Collingbournec66baf72019-10-29 11:00:15 -0700166
Colin Crossd9a121b2020-01-27 13:26:42 -0800167 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800168 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800169 Srcs []string `android:"arch_variant"`
170 Exclude_srcs []string `android:"arch_variant"`
171 } `android:"arch_variant"`
Colin Cross83163192015-12-01 16:31:16 -0800172 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700173}
174
Colin Cross6961a492020-02-06 16:57:45 -0800175var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700176
Colin Cross485e5722015-08-27 13:28:01 -0700177type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700178 // Suffix to add to generated Makefiles
179 Make_suffix *string `json:",omitempty"`
180
Colin Cross2a2e0db2020-02-21 16:55:46 -0800181 BuildId *string `json:",omitempty"`
182 BuildNumberFile *string `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800183
Colin Cross092c9da2019-04-02 22:56:43 -0700184 Platform_version_name *string `json:",omitempty"`
185 Platform_sdk_version *int `json:",omitempty"`
186 Platform_sdk_codename *string `json:",omitempty"`
Yuntao Xu402e9b02021-08-09 15:44:44 -0700187 Platform_sdk_version_or_codename *string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700188 Platform_sdk_final *bool `json:",omitempty"`
Anton Hanssonb021bf82021-08-27 17:35:40 +0100189 Platform_sdk_extension_version *int `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700190 Platform_version_active_codenames []string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700191 Platform_vndk_version *string `json:",omitempty"`
192 Platform_systemsdk_versions []string `json:",omitempty"`
193 Platform_security_patch *string `json:",omitempty"`
194 Platform_preview_sdk_version *string `json:",omitempty"`
195 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
196 Platform_base_os *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700197
Jeongik Cha219141c2020-08-06 23:00:37 +0900198 DeviceName *string `json:",omitempty"`
199 DeviceArch *string `json:",omitempty"`
200 DeviceArchVariant *string `json:",omitempty"`
201 DeviceCpuVariant *string `json:",omitempty"`
202 DeviceAbi []string `json:",omitempty"`
203 DeviceVndkVersion *string `json:",omitempty"`
204 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
205 DeviceSystemSdkVersions []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700206
Jose Galmes6f843bc2020-12-11 13:36:29 -0800207 RecoverySnapshotVersion *string `json:",omitempty"`
208
Colin Crossa74ca042019-01-31 14:31:51 -0800209 DeviceSecondaryArch *string `json:",omitempty"`
210 DeviceSecondaryArchVariant *string `json:",omitempty"`
211 DeviceSecondaryCpuVariant *string `json:",omitempty"`
212 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700213
dimitry8d6dde82019-07-11 10:23:53 +0200214 NativeBridgeArch *string `json:",omitempty"`
215 NativeBridgeArchVariant *string `json:",omitempty"`
216 NativeBridgeCpuVariant *string `json:",omitempty"`
217 NativeBridgeAbi []string `json:",omitempty"`
218 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100219
dimitry8d6dde82019-07-11 10:23:53 +0200220 NativeBridgeSecondaryArch *string `json:",omitempty"`
221 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
222 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
223 NativeBridgeSecondaryAbi []string `json:",omitempty"`
224 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100225
Colin Cross4225f652015-09-17 14:33:42 -0700226 HostArch *string `json:",omitempty"`
227 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000228 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800229
230 CrossHost *string `json:",omitempty"`
231 CrossHostArch *string `json:",omitempty"`
232 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800233
Jeongik Chacee5ba92021-02-19 12:11:51 +0900234 DeviceResourceOverlays []string `json:",omitempty"`
235 ProductResourceOverlays []string `json:",omitempty"`
236 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800237 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700238
Colin Crossa74ca042019-01-31 14:31:51 -0800239 AAPTCharacteristics *string `json:",omitempty"`
240 AAPTConfig []string `json:",omitempty"`
241 AAPTPreferredConfig *string `json:",omitempty"`
242 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700243
Colin Cross61ae0b72017-12-01 17:16:02 -0800244 DefaultAppCertificate *string `json:",omitempty"`
245
Colin Crossface4e42017-10-30 17:32:15 -0700246 AppsDefaultVersionName *string `json:",omitempty"`
247
Jeongik Cha816a23a2020-07-08 01:09:23 +0900248 Allow_missing_dependencies *bool `json:",omitempty"`
249 Unbundled_build *bool `json:",omitempty"`
250 Unbundled_build_apps *bool `json:",omitempty"`
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900251 Unbundled_build_image *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900252 Always_use_prebuilt_sdks *bool `json:",omitempty"`
Paul Duffin9a89a2a2020-10-28 19:20:06 +0000253 Skip_boot_jars_check *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900254 Malloc_not_svelte *bool `json:",omitempty"`
255 Malloc_zero_contents *bool `json:",omitempty"`
256 Malloc_pattern_fill_contents *bool `json:",omitempty"`
257 Safestack *bool `json:",omitempty"`
258 HostStaticBinaries *bool `json:",omitempty"`
259 Binder32bit *bool `json:",omitempty"`
260 UseGoma *bool `json:",omitempty"`
261 UseRBE *bool `json:",omitempty"`
262 UseRBEJAVAC *bool `json:",omitempty"`
263 UseRBER8 *bool `json:",omitempty"`
264 UseRBED8 *bool `json:",omitempty"`
265 Debuggable *bool `json:",omitempty"`
266 Eng *bool `json:",omitempty"`
267 Treble_linker_namespaces *bool `json:",omitempty"`
268 Enforce_vintf_manifest *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900269 Uml *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900270 Arc *bool `json:",omitempty"`
271 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800272
Logan Chien4fcea3d2018-11-20 11:59:08 +0800273 Check_elf_files *bool `json:",omitempty"`
274
Colin Cross5a0dcd52018-10-05 14:20:06 -0700275 UncompressPrivAppDex *bool `json:",omitempty"`
276 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800277
satayevd604b212021-07-21 14:23:52 +0100278 BootJars ConfiguredJarList `json:",omitempty"`
279 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000280
Colin Crossa74ca042019-01-31 14:31:51 -0800281 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700282
Colin Crossa74ca042019-01-31 14:31:51 -0800283 EnableCFI *bool `json:",omitempty"`
284 CFIExcludePaths []string `json:",omitempty"`
285 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700286
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800287 DisableScudo *bool `json:",omitempty"`
288
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800289 MemtagHeapExcludePaths []string `json:",omitempty"`
290 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
291 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
292
Justin Yund5f6c822019-06-25 16:47:17 +0900293 VendorPath *string `json:",omitempty"`
294 OdmPath *string `json:",omitempty"`
295 ProductPath *string `json:",omitempty"`
296 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800297
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700298 ClangTidy *bool `json:",omitempty"`
299 TidyChecks *string `json:",omitempty"`
300
Jooyung Han03302ee2020-04-08 09:22:26 +0900301 SamplingPGO *bool `json:",omitempty"`
Yi Kongceb5b762020-03-20 15:22:27 +0800302
Roland Levillainada12702020-06-09 13:07:36 +0100303 JavaCoveragePaths []string `json:",omitempty"`
304 JavaCoverageExcludePaths []string `json:",omitempty"`
305
Colin Cross1a6acd42020-06-16 17:51:46 -0700306 GcovCoverage *bool `json:",omitempty"`
Roland Levillain4f5297b2020-06-09 12:44:06 +0100307 ClangCoverage *bool `json:",omitempty"`
308 NativeCoveragePaths []string `json:",omitempty"`
309 NativeCoverageExcludePaths []string `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800310
Colin Cross1a6acd42020-06-16 17:51:46 -0700311 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400312 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700313
Colin Cross23ae82a2016-11-02 14:34:39 -0700314 SanitizeHost []string `json:",omitempty"`
315 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700316 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700317 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800318
319 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800320
321 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700322
323 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900324
325 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900326
327 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800328
329 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800330
331 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700332
Inseob Kimae553032019-05-14 18:52:49 +0900333 VndkUseCoreVariant *bool `json:",omitempty"`
334 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800335
Inseob Kim7cf14652021-01-06 23:06:52 +0900336 DirectedVendorSnapshot bool `json:",omitempty"`
337 VendorSnapshotModules map[string]bool `json:",omitempty"`
338
Jose Galmes4c6895e2021-02-09 07:44:30 -0800339 DirectedRecoverySnapshot bool `json:",omitempty"`
340 RecoverySnapshotModules map[string]bool `json:",omitempty"`
341
Justin DeMartino383bfb32021-02-24 10:49:43 -0800342 VendorSnapshotDirsIncluded []string `json:",omitempty"`
343 VendorSnapshotDirsExcluded []string `json:",omitempty"`
344 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
345 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000346 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800347
Tri Vof544fe32018-05-20 14:34:48 -0700348 BoardVendorSepolicyDirs []string `json:",omitempty"`
349 BoardOdmSepolicyDirs []string `json:",omitempty"`
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900350 BoardReqdMaskPolicy []string `json:",omitempty"`
Felixa20a8752020-05-17 18:28:35 +0200351 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
352 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900353 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700354
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900355 BoardSepolicyVers *string `json:",omitempty"`
356 PlatformSepolicyVersion *string `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000357 TotSepolicyVersion *string `json:",omitempty"`
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900358
Dan Willemsen0fe78662018-03-26 12:41:18 -0700359 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700360
Dan Albertd05ba002021-04-13 15:55:47 -0700361 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900362
Jiyong Park4da07972021-01-05 21:01:11 +0900363 Flatten_apex *bool `json:",omitempty"`
364 ForceApexSymlinkOptimization *bool `json:",omitempty"`
365 CompressedApex *bool `json:",omitempty"`
366 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800367
368 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900369
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900370 WithDexpreopt bool `json:",omitempty"`
371
Jiyong Park7f67f482019-01-05 12:57:48 +0900372 ManifestPackageNameOverrides []string `json:",omitempty"`
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800373 CertificateOverrides []string `json:",omitempty"`
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800374 PackageNameOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900375
376 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700377 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800378
Colin Crossf24a22a2019-01-31 14:12:44 -0800379 ProductHiddenAPIStubs []string `json:",omitempty"`
380 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
381 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700382
Inseob Kim0866b002019-04-15 20:21:29 +0900383 ProductPublicSepolicyDirs []string `json:",omitempty"`
384 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900385
Justin Yun5f7f7e82019-11-18 19:52:14 +0900386 ProductVndkVersion *string `json:",omitempty"`
387
Dan Willemsen54879d12019-04-18 10:08:46 -0700388 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700389
390 MissingUsesLibraries []string `json:",omitempty"`
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900391
392 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900393
JaeMan Parkff715562020-10-19 17:25:58 +0900394 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
395 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
396
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900397 InstallExtraFlattenedApexes *bool `json:",omitempty"`
Yifan Hong82db7352020-01-21 16:12:26 -0800398
399 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700400
Yifan Hong42bef8d2020-08-05 14:36:09 -0700401 BoardKernelBinaries []string `json:",omitempty"`
402 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700403
404 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800405
406 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900407
408 ShippingApiLevel *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900409
Inseob Kim67e5add192021-03-17 18:05:33 +0900410 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
411 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900412 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900413
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700414 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
415
Inseob Kim67e5add192021-03-17 18:05:33 +0900416 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
417
418 SelinuxIgnoreNeverallows bool `json:",omitempty"`
419
420 SepolicySplit bool `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000421
422 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
423 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900424
425 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700426}
427
428func boolPtr(v bool) *bool {
429 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700430}
431
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700432func intPtr(v int) *int {
433 return &v
434}
435
Colin Cross4225f652015-09-17 14:33:42 -0700436func stringPtr(v string) *string {
437 return &v
438}
439
Colin Cross27385972015-09-18 10:57:10 -0700440func (v *productVariables) SetDefaultConfig() {
441 *v = productVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800442 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700443
Jiyong Park84d8a7e2020-09-25 13:44:51 +0900444 Platform_version_name: stringPtr("S"),
445 Platform_sdk_version: intPtr(30),
446 Platform_sdk_codename: stringPtr("S"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700447 Platform_sdk_final: boolPtr(false),
Jiyong Park84d8a7e2020-09-25 13:44:51 +0900448 Platform_version_active_codenames: []string{"S"},
449 Platform_vndk_version: stringPtr("S"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800450
Colin Cross4225f652015-09-17 14:33:42 -0700451 HostArch: stringPtr("x86_64"),
452 HostSecondaryArch: stringPtr("x86"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800453 DeviceName: stringPtr("generic_arm64"),
Colin Cross4225f652015-09-17 14:33:42 -0700454 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700455 DeviceArchVariant: stringPtr("armv8-a"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800456 DeviceCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800457 DeviceAbi: []string{"arm64-v8a"},
Colin Cross4225f652015-09-17 14:33:42 -0700458 DeviceSecondaryArch: stringPtr("arm"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800459 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
460 DeviceSecondaryCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800461 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
Colin Crossface4e42017-10-30 17:32:15 -0700462
Colin Crossa74ca042019-01-31 14:31:51 -0800463 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700464 AAPTPreferredConfig: stringPtr("xhdpi"),
465 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800466 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700467
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700468 Malloc_not_svelte: boolPtr(true),
Steven Morelandd1342012020-07-30 20:38:49 +0000469 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700470 Malloc_pattern_fill_contents: boolPtr(false),
471 Safestack: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100472
satayevd604b212021-07-21 14:23:52 +0100473 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
474 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700475 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800476
477 if runtime.GOOS == "linux" {
478 v.CrossHost = stringPtr("windows")
479 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800480 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800481 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700482}
483
Liz Kammera060c452021-03-24 10:14:47 -0400484// ProductConfigContext requires the access to the Module to get product config properties.
485type ProductConfigContext interface {
486 Module() Module
487}
488
489// ProductConfigProperty contains the information for a single property (may be a struct) paired
490// with the appropriate ProductConfigVariable.
491type ProductConfigProperty struct {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000492 // The name of the product variable, e.g. "safestack", "malloc_not_svelte",
493 // "board"
Jingwen Chen25825ca2021-11-15 12:28:43 +0000494 Name string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000495
496 // Namespace of the variable, if this is a soong_config_module_type variable
Jingwen Chen25825ca2021-11-15 12:28:43 +0000497 // e.g. "acme", "ANDROID", "vendor_name"
498 Namespace string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000499
500 // Unique configuration to identify this product config property (i.e. a
501 // primary key), as just using the product variable name is not sufficient.
502 //
503 // For product variables, this is the product variable name + optional
504 // archvariant information. e.g.
505 //
506 // product_variables: {
507 // foo: {
508 // cflags: ["-Dfoo"],
509 // },
510 // },
511 //
512 // FullConfig would be "foo".
513 //
514 // target: {
515 // android: {
516 // product_variables: {
517 // foo: {
518 // cflags: ["-Dfoo-android"],
519 // },
520 // },
521 // },
522 // },
523 //
524 // FullConfig would be "foo-android".
525 //
526 // For soong config variables, this is the namespace + product variable name
527 // + value of the variable, if applicable. The value can also be
528 // conditions_default.
529 //
530 // e.g.
531 //
532 // soong_config_variables: {
533 // feature1: {
534 // conditions_default: {
535 // cflags: ["-DDEFAULT1"],
536 // },
537 // cflags: ["-DFEATURE1"],
538 // },
539 // }
540 //
541 // where feature1 is created in the "acme" namespace, so FullConfig would be
542 // "acme__feature1" and "acme__feature1__conditions_default".
543 //
544 // e.g.
545 //
546 // soong_config_variables: {
547 // board: {
548 // soc_a: {
549 // cflags: ["-DSOC_A"],
550 // },
551 // soc_b: {
552 // cflags: ["-DSOC_B"],
553 // },
554 // soc_c: {},
555 // conditions_default: {
556 // cflags: ["-DSOC_DEFAULT"]
557 // },
558 // },
559 // }
560 //
561 // where board is created in the "acme" namespace, so FullConfig would be
562 // "acme__board__soc_a", "acme__board__soc_b", and
563 // "acme__board__conditions_default"
564 FullConfig string
Liz Kammera060c452021-03-24 10:14:47 -0400565}
566
Jingwen Chena47f28d2021-11-02 16:43:57 +0000567func (p *ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
568 if p.Namespace == "" {
569 return bazel.ProductVariableConfigurationAxis(p.FullConfig)
570 } else {
571 // Soong config variables can be uniquely identified by the namespace
572 // (e.g. acme, android) and the product variable name (e.g. board, size)
Jingwen Chen25825ca2021-11-15 12:28:43 +0000573 return bazel.ProductVariableConfigurationAxis(p.Namespace + "__" + p.Name)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000574 }
575}
576
Jingwen Chen25825ca2021-11-15 12:28:43 +0000577// SelectKey returns the literal string that represents this variable in a BUILD
578// select statement.
579func (p *ProductConfigProperty) SelectKey() string {
580 if p.Namespace == "" {
581 return strings.ToLower(p.FullConfig)
582 }
583
584 if p.FullConfig == bazel.ConditionsDefaultConfigKey {
585 return bazel.ConditionsDefaultConfigKey
586 }
587
588 value := p.FullConfig
589 if value == p.Name {
590 value = "enabled"
591 }
592 // e.g. acme__feature1__enabled, android__board__soc_a
593 return strings.ToLower(strings.Join([]string{p.Namespace, p.Name, value}, "__"))
594}
595
596// ProductConfigProperties is a map of maps to group property values according
597// their property name and the product config variable they're set under.
598//
599// The outer map key is the name of the property, like "cflags".
600//
601// The inner map key is a ProductConfigProperty, which is a struct of product
602// variable name, namespace, and the "full configuration" of the product
603// variable.
604//
605// e.g. product variable name: board, namespace: acme, full config: vendor_chip_foo
606//
607// The value of the map is the interface{} representing the value of the
608// property, like ["-DDEFINES"] for cflags.
609type ProductConfigProperties map[string]map[ProductConfigProperty]interface{}
Liz Kammera060c452021-03-24 10:14:47 -0400610
611// ProductVariableProperties returns a ProductConfigProperties containing only the properties which
612// have been set for the module in the given context.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000613func ProductVariableProperties(ctx BazelConversionPathContext) ProductConfigProperties {
Liz Kammera060c452021-03-24 10:14:47 -0400614 module := ctx.Module()
615 moduleBase := module.base()
616
617 productConfigProperties := ProductConfigProperties{}
618
Jingwen Chena47f28d2021-11-02 16:43:57 +0000619 if moduleBase.variableProperties != nil {
620 productVariablesProperty := proptools.FieldNameForProperty("product_variables")
621 productVariableValues(
622 productVariablesProperty,
623 moduleBase.variableProperties,
624 "",
625 "",
626 &productConfigProperties)
627
628 for _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
629 for config, props := range configToProps {
630 // GetArchVariantProperties is creating an instance of the requested type
631 // and productVariablesValues expects an interface, so no need to cast
632 productVariableValues(
633 productVariablesProperty,
634 props,
635 "",
636 config,
637 &productConfigProperties)
638 }
639 }
Liz Kammera060c452021-03-24 10:14:47 -0400640 }
641
Jingwen Chena47f28d2021-11-02 16:43:57 +0000642 if m, ok := module.(Bazelable); ok && m.namespacedVariableProps() != nil {
643 for namespace, namespacedVariableProp := range m.namespacedVariableProps() {
644 productVariableValues(
645 soongconfig.SoongConfigProperty,
646 namespacedVariableProp,
647 namespace,
648 "",
649 &productConfigProperties)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400650 }
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400651 }
652
653 return productConfigProperties
654}
655
Jingwen Chena47f28d2021-11-02 16:43:57 +0000656func (p *ProductConfigProperties) AddProductConfigProperty(
657 propertyName, namespace, productVariableName, config string, property interface{}) {
658 if (*p)[propertyName] == nil {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000659 (*p)[propertyName] = make(map[ProductConfigProperty]interface{})
Jingwen Chena47f28d2021-11-02 16:43:57 +0000660 }
661
Jingwen Chen25825ca2021-11-15 12:28:43 +0000662 productConfigProp := ProductConfigProperty{
663 Namespace: namespace, // e.g. acme, android
664 Name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
665 FullConfig: config, // e.g. size, feature1-x86, size__conditions_default
Jingwen Chena47f28d2021-11-02 16:43:57 +0000666 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000667
668 (*p)[propertyName][productConfigProp] = property
Jingwen Chena47f28d2021-11-02 16:43:57 +0000669}
670
671var (
672 conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
673)
674
675// maybeExtractConfigVarProp attempts to read this value as a config var struct
676// wrapped by interfaces and ptrs. If it's not the right type, the second return
677// value is false.
678func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
679 if v.Kind() == reflect.Interface {
680 // The conditions_default value can be either
681 // 1) an ptr to an interface of a struct (bool config variables and product variables)
682 // 2) an interface of 1) (config variables with nested structs, like string vars)
683 v = v.Elem()
684 }
685 if v.Kind() != reflect.Ptr {
686 return v, false
687 }
688 v = reflect.Indirect(v)
689 if v.Kind() == reflect.Interface {
690 // Extract the struct from the interface
691 v = v.Elem()
692 }
693
694 if !v.IsValid() {
695 return v, false
696 }
697
698 if v.Kind() != reflect.Struct {
699 return v, false
700 }
701 return v, true
702}
703
704// productVariableValues uses reflection to convert a property struct for
705// product_variables and soong_config_variables to structs that can be generated
706// as select statements.
707func productVariableValues(
708 fieldName string, variableProps interface{}, namespace, suffix string, productConfigProperties *ProductConfigProperties) {
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400709 if suffix != "" {
710 suffix = "-" + suffix
711 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000712
713 // variableValues represent the product_variables or soong_config_variables
714 // struct.
715 variableValues := reflect.ValueOf(variableProps).Elem().FieldByName(fieldName)
716
717 // Example of product_variables:
718 //
719 // product_variables: {
720 // malloc_not_svelte: {
721 // shared_libs: ["malloc_not_svelte_shared_lib"],
722 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
723 // exclude_static_libs: [
724 // "malloc_not_svelte_static_lib_excludes",
725 // "malloc_not_svelte_whole_static_lib_excludes",
726 // ],
727 // },
728 // },
729 //
730 // Example of soong_config_variables:
731 //
732 // soong_config_variables: {
733 // feature1: {
734 // conditions_default: {
735 // ...
736 // },
737 // cflags: ...
738 // },
739 // feature2: {
740 // cflags: ...
741 // conditions_default: {
742 // ...
743 // },
744 // },
745 // board: {
746 // soc_a: {
747 // ...
748 // },
749 // soc_a: {
750 // ...
751 // },
752 // soc_c: {},
753 // conditions_default: {
754 // ...
755 // },
756 // },
757 // }
Liz Kammera060c452021-03-24 10:14:47 -0400758 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000759 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
760 productVariableName := variableValues.Type().Field(i).Name
761
Liz Kammera060c452021-03-24 10:14:47 -0400762 variableValue := variableValues.Field(i)
763 // Check if any properties were set for the module
764 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000765 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400766 continue
767 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000768
769 // Unlike product variables, config variables require a few more
770 // indirections to extract the struct from the reflect.Value.
771 if v, ok := maybeExtractConfigVarProp(variableValue); ok {
772 variableValue = v
773 }
774
Liz Kammera060c452021-03-24 10:14:47 -0400775 for j := 0; j < variableValue.NumField(); j++ {
776 property := variableValue.Field(j)
777 // If the property wasn't set, no need to pass it along
778 if property.IsZero() {
779 continue
780 }
781
782 // e.g. Asflags, Cflags, Enabled, etc.
783 propertyName := variableValue.Type().Field(j).Name
Jingwen Chena47f28d2021-11-02 16:43:57 +0000784
785 if v, ok := maybeExtractConfigVarProp(property); ok {
786 // The field is a struct, which is used by:
787 // 1) soong_config_string_variables
788 //
789 // soc_a: {
790 // cflags: ...,
791 // }
792 //
793 // soc_b: {
794 // cflags: ...,
795 // }
796 //
797 // 2) conditions_default structs for all soong config variable types.
798 //
799 // conditions_default: {
800 // cflags: ...,
801 // static_libs: ...
802 // }
803 field := v
804 for k := 0; k < field.NumField(); k++ {
805 // Iterate over fields of this struct prop.
806 if field.Field(k).IsZero() {
807 continue
808 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000809 // config can also be "conditions_default".
810 config := proptools.PropertyNameForField(propertyName)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000811 actualPropertyName := field.Type().Field(k).Name
812
813 productConfigProperties.AddProductConfigProperty(
814 actualPropertyName, // e.g. cflags, static_libs
815 namespace, // e.g. acme, android
816 productVariableName, // e.g. size, feature1, FEATURE2, board
817 config,
818 field.Field(k).Interface(), // e.g. ["-DDEFAULT"], ["foo", "bar"]
819 )
820 }
821 } else {
822 // Not a conditions_default or a struct prop, i.e. regular
823 // product variables, or not a string-typed config var.
824 config := productVariableName + suffix
Jingwen Chena47f28d2021-11-02 16:43:57 +0000825 productConfigProperties.AddProductConfigProperty(
826 propertyName,
827 namespace,
828 productVariableName,
829 config,
830 property.Interface(),
831 )
Liz Kammer47535c52021-06-02 16:02:22 -0400832 }
Liz Kammera060c452021-03-24 10:14:47 -0400833 }
834 }
Liz Kammera060c452021-03-24 10:14:47 -0400835}
836
Colin Crosseabaedd2020-02-06 17:01:55 -0800837func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700838 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700839 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700840 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700841 return
842 }
843
844 // TODO: depend on config variable, create variants, propagate variants up tree
845 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700846
847 if a.variableProperties == nil {
848 return
849 }
850
851 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700852
Colin Cross0cec3122021-01-20 11:27:32 -0800853 productVariables := reflect.ValueOf(mctx.Config().productVariables)
854
Colin Cross7f64b6d2015-07-09 13:57:48 -0700855 for i := 0; i < variableValues.NumField(); i++ {
856 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700857 name := variableValues.Type().Field(i).Name
858 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700859
Colin Crossa6bc19e2015-09-16 13:53:42 -0700860 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -0800861 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700862 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
863 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700864 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700865
866 val = val.Elem()
867
868 // For bools, check that the value is true
869 if val.Kind() == reflect.Bool && val.Bool() == false {
870 continue
871 }
872
873 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -0800874 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700875 continue
876 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700877 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700878 }
879}
880
Colin Cross4157e882019-06-06 16:57:04 -0700881func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700882 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
883
Colin Cross7e0eaf12017-05-05 16:16:24 -0700884 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700885
Colin Cross4157e882019-06-06 16:57:04 -0700886 err := proptools.AppendMatchingProperties(m.generalProperties,
Colin Cross06a931b2015-10-28 17:23:31 -0700887 productVariablePropertyValue.Addr().Interface(), nil)
888 if err != nil {
889 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
890 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
891 } else {
892 panic(err)
893 }
894 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700895}
896
Colin Cross7e0eaf12017-05-05 16:16:24 -0700897func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
898 productVariablePropertyValue reflect.Value, i int, err error) {
899
900 field := productVariablePropertyValue.Type().Field(i).Name
901 property := prefix + "." + proptools.PropertyNameForField(field)
902 ctx.PropertyErrorf(property, "%s", err)
903}
904
905func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
906 productVariablePropertyValue reflect.Value, variableValue interface{}) {
907
Colin Cross7f64b6d2015-07-09 13:57:48 -0700908 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
909 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800910 kind := propertyValue.Kind()
911 if kind == reflect.Ptr {
912 if propertyValue.IsNil() {
913 continue
914 }
915 propertyValue = propertyValue.Elem()
916 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700917 switch propertyValue.Kind() {
918 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700919 err := printfIntoProperty(propertyValue, variableValue)
920 if err != nil {
921 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
922 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700923 case reflect.Slice:
924 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700925 err := printfIntoProperty(propertyValue.Index(j), variableValue)
926 if err != nil {
927 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
928 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700929 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800930 case reflect.Bool:
931 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700932 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700933 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700934 default:
935 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
936 }
937 }
938}
939
Colin Cross7e0eaf12017-05-05 16:16:24 -0700940func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700941 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700942
943 count := strings.Count(s, "%")
944 if count == 0 {
945 return nil
946 }
947
948 if count > 1 {
949 return fmt.Errorf("product variable properties only support a single '%%'")
950 }
951
Colin Cross7f64b6d2015-07-09 13:57:48 -0700952 if strings.Contains(s, "%d") {
953 switch v := variableValue.(type) {
954 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700955 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700956 case bool:
957 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700958 variableValue = 1
959 } else {
960 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700961 }
962 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700963 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700964 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700965 } else if strings.Contains(s, "%s") {
966 switch variableValue.(type) {
967 case string:
968 // Nothing
969 default:
970 return fmt.Errorf("unsupported type %T for %%s", variableValue)
971 }
972 } else {
973 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700974 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700975
976 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
977
978 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700979}
Colin Cross18c46802019-09-24 22:19:02 -0700980
981var variablePropTypeMap OncePer
982
983// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
984// reflect.Types of each property struct. The result can be used as a key in a map.
985func sliceToTypeArray(s []interface{}) interface{} {
986 // Create an array using reflection whose length is the length of the input slice
987 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
988 for i, e := range s {
989 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
990 }
991 return ret.Interface()
992}
993
Colin Crosseabaedd2020-02-06 17:01:55 -0800994func initProductVariableModule(m Module) {
995 base := m.base()
996
997 // Allow tests to override the default product variables
998 if base.variableProperties == nil {
999 base.variableProperties = defaultProductVariables
1000 }
1001 // Filter the product variables properties to the ones that exist on this module
1002 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
1003 if base.variableProperties != nil {
1004 m.AddProperties(base.variableProperties)
1005 }
1006}
1007
Colin Cross18c46802019-09-24 22:19:02 -07001008// createVariableProperties takes the list of property structs for a module and returns a property struct that
1009// contains the product variable properties that exist in the property structs, or nil if there are none. It
1010// caches the result.
1011func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
1012 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
1013 key := sliceToTypeArray(moduleTypeProps)
1014
1015 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
1016 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
1017 // Compute the filtered property struct type.
1018 return createVariablePropertiesType(moduleTypeProps, productVariables)
1019 }).(reflect.Type)
1020
1021 if typ == nil {
1022 return nil
1023 }
1024
1025 // Create a new pointer to a filtered property struct.
1026 return reflect.New(typ).Interface()
1027}
1028
1029// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1030// a list of property structs.
1031func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1032 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1033 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1034 // Filter function, returns true if the field should be in the resulting struct
1035 if prefix == "" {
1036 // Keep the top level Product_variables field
1037 return true, field
1038 }
1039 _, rest := splitPrefix(prefix)
1040 if rest == "" {
1041 // Keep the 2nd level field (i.e. Product_variables.Eng)
1042 return true, field
1043 }
1044
1045 // Strip off the first 2 levels of the prefix
1046 _, prefix = splitPrefix(rest)
1047
1048 for _, p := range moduleTypeProps {
1049 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1050 // Keep any fields that exist in one of the property structs
1051 return true, field
1052 }
1053 }
1054
1055 return false, field
1056 })
1057 return typ
1058}
1059
1060func splitPrefix(prefix string) (first, rest string) {
1061 index := strings.IndexByte(prefix, '.')
1062 if index == -1 {
1063 return prefix, ""
1064 }
1065 return prefix[:index], prefix[index+1:]
1066}
1067
1068func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1069 if t.Kind() != reflect.Struct {
1070 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1071 }
1072
1073 if prefix != "" {
1074 split := strings.SplitN(prefix, ".", 2)
1075 firstPrefix := split[0]
1076 rest := ""
1077 if len(split) > 1 {
1078 rest = split[1]
1079 }
1080 f, exists := t.FieldByName(firstPrefix)
1081 if !exists {
1082 return false
1083 }
1084 ft := f.Type
1085 if ft.Kind() == reflect.Ptr {
1086 ft = ft.Elem()
1087 }
1088 if ft.Kind() != reflect.Struct {
1089 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1090 }
1091 return fieldExistsByNameRecursive(ft, rest, name)
1092 } else {
1093 _, exists := t.FieldByName(name)
1094 return exists
1095 }
1096}