blob: 20b268eab1220dd4ab015d2f9fc08dadede59a52 [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 Kim67e5add2021-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 Kim67e5add2021-03-17 18:05:33 +0900413
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700414 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
415
Inseob Kim67e5add2021-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 {
Jingwen Chen84817de2021-11-17 10:57:35 +0000643 for namespace, namespacedVariableProps := range m.namespacedVariableProps() {
644 for _, namespacedVariableProp := range namespacedVariableProps {
645 productVariableValues(
646 soongconfig.SoongConfigProperty,
647 namespacedVariableProp,
648 namespace,
649 "",
650 &productConfigProperties)
651 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400652 }
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400653 }
654
655 return productConfigProperties
656}
657
Jingwen Chena47f28d2021-11-02 16:43:57 +0000658func (p *ProductConfigProperties) AddProductConfigProperty(
659 propertyName, namespace, productVariableName, config string, property interface{}) {
660 if (*p)[propertyName] == nil {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000661 (*p)[propertyName] = make(map[ProductConfigProperty]interface{})
Jingwen Chena47f28d2021-11-02 16:43:57 +0000662 }
663
Jingwen Chen25825ca2021-11-15 12:28:43 +0000664 productConfigProp := ProductConfigProperty{
665 Namespace: namespace, // e.g. acme, android
666 Name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
667 FullConfig: config, // e.g. size, feature1-x86, size__conditions_default
Jingwen Chena47f28d2021-11-02 16:43:57 +0000668 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000669
Jingwen Chen84817de2021-11-17 10:57:35 +0000670 if existing, ok := (*p)[propertyName][productConfigProp]; ok && namespace != "" {
671 switch dst := existing.(type) {
672 case []string:
673 if src, ok := property.([]string); ok {
674 dst = append(dst, src...)
675 (*p)[propertyName][productConfigProp] = dst
676 }
677 default:
678 // TODO(jingwen): Add support for more types.
679 }
680 } else {
681 (*p)[propertyName][productConfigProp] = property
682 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000683}
684
685var (
686 conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
687)
688
689// maybeExtractConfigVarProp attempts to read this value as a config var struct
690// wrapped by interfaces and ptrs. If it's not the right type, the second return
691// value is false.
692func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
693 if v.Kind() == reflect.Interface {
694 // The conditions_default value can be either
695 // 1) an ptr to an interface of a struct (bool config variables and product variables)
696 // 2) an interface of 1) (config variables with nested structs, like string vars)
697 v = v.Elem()
698 }
699 if v.Kind() != reflect.Ptr {
700 return v, false
701 }
702 v = reflect.Indirect(v)
703 if v.Kind() == reflect.Interface {
704 // Extract the struct from the interface
705 v = v.Elem()
706 }
707
708 if !v.IsValid() {
709 return v, false
710 }
711
712 if v.Kind() != reflect.Struct {
713 return v, false
714 }
715 return v, true
716}
717
Jingwen Chen84817de2021-11-17 10:57:35 +0000718func (productConfigProperties *ProductConfigProperties) AddProductConfigProperties(namespace, suffix string, variableValues reflect.Value) {
719 // variableValues can either be a product_variables or
720 // soong_config_variables struct.
721 //
Jingwen Chena47f28d2021-11-02 16:43:57 +0000722 // Example of product_variables:
723 //
724 // product_variables: {
725 // malloc_not_svelte: {
726 // shared_libs: ["malloc_not_svelte_shared_lib"],
727 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
728 // exclude_static_libs: [
729 // "malloc_not_svelte_static_lib_excludes",
730 // "malloc_not_svelte_whole_static_lib_excludes",
731 // ],
732 // },
733 // },
734 //
735 // Example of soong_config_variables:
736 //
737 // soong_config_variables: {
738 // feature1: {
739 // conditions_default: {
740 // ...
741 // },
742 // cflags: ...
743 // },
744 // feature2: {
745 // cflags: ...
746 // conditions_default: {
747 // ...
748 // },
749 // },
750 // board: {
751 // soc_a: {
752 // ...
753 // },
754 // soc_a: {
755 // ...
756 // },
757 // soc_c: {},
758 // conditions_default: {
759 // ...
760 // },
761 // },
762 // }
Liz Kammera060c452021-03-24 10:14:47 -0400763 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000764 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
765 productVariableName := variableValues.Type().Field(i).Name
766
Liz Kammera060c452021-03-24 10:14:47 -0400767 variableValue := variableValues.Field(i)
768 // Check if any properties were set for the module
769 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000770 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400771 continue
772 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000773
774 // Unlike product variables, config variables require a few more
775 // indirections to extract the struct from the reflect.Value.
776 if v, ok := maybeExtractConfigVarProp(variableValue); ok {
777 variableValue = v
778 }
779
Liz Kammera060c452021-03-24 10:14:47 -0400780 for j := 0; j < variableValue.NumField(); j++ {
781 property := variableValue.Field(j)
782 // If the property wasn't set, no need to pass it along
783 if property.IsZero() {
784 continue
785 }
786
787 // e.g. Asflags, Cflags, Enabled, etc.
788 propertyName := variableValue.Type().Field(j).Name
Jingwen Chena47f28d2021-11-02 16:43:57 +0000789
790 if v, ok := maybeExtractConfigVarProp(property); ok {
791 // The field is a struct, which is used by:
792 // 1) soong_config_string_variables
793 //
794 // soc_a: {
795 // cflags: ...,
796 // }
797 //
798 // soc_b: {
799 // cflags: ...,
800 // }
801 //
802 // 2) conditions_default structs for all soong config variable types.
803 //
804 // conditions_default: {
805 // cflags: ...,
806 // static_libs: ...
807 // }
808 field := v
809 for k := 0; k < field.NumField(); k++ {
810 // Iterate over fields of this struct prop.
811 if field.Field(k).IsZero() {
812 continue
813 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000814 // config can also be "conditions_default".
815 config := proptools.PropertyNameForField(propertyName)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000816 actualPropertyName := field.Type().Field(k).Name
817
818 productConfigProperties.AddProductConfigProperty(
819 actualPropertyName, // e.g. cflags, static_libs
820 namespace, // e.g. acme, android
821 productVariableName, // e.g. size, feature1, FEATURE2, board
822 config,
823 field.Field(k).Interface(), // e.g. ["-DDEFAULT"], ["foo", "bar"]
824 )
825 }
826 } else {
827 // Not a conditions_default or a struct prop, i.e. regular
828 // product variables, or not a string-typed config var.
829 config := productVariableName + suffix
Jingwen Chena47f28d2021-11-02 16:43:57 +0000830 productConfigProperties.AddProductConfigProperty(
831 propertyName,
832 namespace,
833 productVariableName,
834 config,
835 property.Interface(),
836 )
Liz Kammer47535c52021-06-02 16:02:22 -0400837 }
Liz Kammera060c452021-03-24 10:14:47 -0400838 }
839 }
Liz Kammera060c452021-03-24 10:14:47 -0400840}
841
Jingwen Chen84817de2021-11-17 10:57:35 +0000842// productVariableValues uses reflection to convert a property struct for
843// product_variables and soong_config_variables to structs that can be generated
844// as select statements.
845func productVariableValues(
846 fieldName string, variableProps interface{}, namespace, suffix string, productConfigProperties *ProductConfigProperties) {
847 if suffix != "" {
848 suffix = "-" + suffix
849 }
850
851 // variableValues represent the product_variables or soong_config_variables struct.
852 variableValues := reflect.ValueOf(variableProps).Elem().FieldByName(fieldName)
853 productConfigProperties.AddProductConfigProperties(namespace, suffix, variableValues)
854}
855
Colin Crosseabaedd2020-02-06 17:01:55 -0800856func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700857 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700858 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700859 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700860 return
861 }
862
863 // TODO: depend on config variable, create variants, propagate variants up tree
864 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700865
866 if a.variableProperties == nil {
867 return
868 }
869
870 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700871
Colin Cross0cec3122021-01-20 11:27:32 -0800872 productVariables := reflect.ValueOf(mctx.Config().productVariables)
873
Colin Cross7f64b6d2015-07-09 13:57:48 -0700874 for i := 0; i < variableValues.NumField(); i++ {
875 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700876 name := variableValues.Type().Field(i).Name
877 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700878
Colin Crossa6bc19e2015-09-16 13:53:42 -0700879 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -0800880 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700881 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
882 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700883 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700884
885 val = val.Elem()
886
887 // For bools, check that the value is true
888 if val.Kind() == reflect.Bool && val.Bool() == false {
889 continue
890 }
891
892 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -0800893 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700894 continue
895 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700896 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700897 }
898}
899
Colin Cross4157e882019-06-06 16:57:04 -0700900func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700901 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
902
Colin Cross7e0eaf12017-05-05 16:16:24 -0700903 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700904
Colin Cross4157e882019-06-06 16:57:04 -0700905 err := proptools.AppendMatchingProperties(m.generalProperties,
Colin Cross06a931b2015-10-28 17:23:31 -0700906 productVariablePropertyValue.Addr().Interface(), nil)
907 if err != nil {
908 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
909 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
910 } else {
911 panic(err)
912 }
913 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700914}
915
Colin Cross7e0eaf12017-05-05 16:16:24 -0700916func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
917 productVariablePropertyValue reflect.Value, i int, err error) {
918
919 field := productVariablePropertyValue.Type().Field(i).Name
920 property := prefix + "." + proptools.PropertyNameForField(field)
921 ctx.PropertyErrorf(property, "%s", err)
922}
923
924func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
925 productVariablePropertyValue reflect.Value, variableValue interface{}) {
926
Colin Cross7f64b6d2015-07-09 13:57:48 -0700927 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
928 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800929 kind := propertyValue.Kind()
930 if kind == reflect.Ptr {
931 if propertyValue.IsNil() {
932 continue
933 }
934 propertyValue = propertyValue.Elem()
935 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700936 switch propertyValue.Kind() {
937 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700938 err := printfIntoProperty(propertyValue, variableValue)
939 if err != nil {
940 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
941 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700942 case reflect.Slice:
943 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700944 err := printfIntoProperty(propertyValue.Index(j), variableValue)
945 if err != nil {
946 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
947 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700948 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800949 case reflect.Bool:
950 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700951 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700952 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700953 default:
954 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
955 }
956 }
957}
958
Colin Cross7e0eaf12017-05-05 16:16:24 -0700959func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700960 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700961
962 count := strings.Count(s, "%")
963 if count == 0 {
964 return nil
965 }
966
967 if count > 1 {
968 return fmt.Errorf("product variable properties only support a single '%%'")
969 }
970
Colin Cross7f64b6d2015-07-09 13:57:48 -0700971 if strings.Contains(s, "%d") {
972 switch v := variableValue.(type) {
973 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700974 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700975 case bool:
976 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700977 variableValue = 1
978 } else {
979 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700980 }
981 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700982 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700983 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700984 } else if strings.Contains(s, "%s") {
985 switch variableValue.(type) {
986 case string:
987 // Nothing
988 default:
989 return fmt.Errorf("unsupported type %T for %%s", variableValue)
990 }
991 } else {
992 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700993 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700994
995 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
996
997 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700998}
Colin Cross18c46802019-09-24 22:19:02 -0700999
1000var variablePropTypeMap OncePer
1001
1002// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
1003// reflect.Types of each property struct. The result can be used as a key in a map.
1004func sliceToTypeArray(s []interface{}) interface{} {
1005 // Create an array using reflection whose length is the length of the input slice
1006 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
1007 for i, e := range s {
1008 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
1009 }
1010 return ret.Interface()
1011}
1012
Colin Crosseabaedd2020-02-06 17:01:55 -08001013func initProductVariableModule(m Module) {
1014 base := m.base()
1015
1016 // Allow tests to override the default product variables
1017 if base.variableProperties == nil {
1018 base.variableProperties = defaultProductVariables
1019 }
1020 // Filter the product variables properties to the ones that exist on this module
1021 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
1022 if base.variableProperties != nil {
1023 m.AddProperties(base.variableProperties)
1024 }
1025}
1026
Colin Cross18c46802019-09-24 22:19:02 -07001027// createVariableProperties takes the list of property structs for a module and returns a property struct that
1028// contains the product variable properties that exist in the property structs, or nil if there are none. It
1029// caches the result.
1030func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
1031 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
1032 key := sliceToTypeArray(moduleTypeProps)
1033
1034 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
1035 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
1036 // Compute the filtered property struct type.
1037 return createVariablePropertiesType(moduleTypeProps, productVariables)
1038 }).(reflect.Type)
1039
1040 if typ == nil {
1041 return nil
1042 }
1043
1044 // Create a new pointer to a filtered property struct.
1045 return reflect.New(typ).Interface()
1046}
1047
1048// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1049// a list of property structs.
1050func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1051 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1052 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1053 // Filter function, returns true if the field should be in the resulting struct
1054 if prefix == "" {
1055 // Keep the top level Product_variables field
1056 return true, field
1057 }
1058 _, rest := splitPrefix(prefix)
1059 if rest == "" {
1060 // Keep the 2nd level field (i.e. Product_variables.Eng)
1061 return true, field
1062 }
1063
1064 // Strip off the first 2 levels of the prefix
1065 _, prefix = splitPrefix(rest)
1066
1067 for _, p := range moduleTypeProps {
1068 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1069 // Keep any fields that exist in one of the property structs
1070 return true, field
1071 }
1072 }
1073
1074 return false, field
1075 })
1076 return typ
1077}
1078
1079func splitPrefix(prefix string) (first, rest string) {
1080 index := strings.IndexByte(prefix, '.')
1081 if index == -1 {
1082 return prefix, ""
1083 }
1084 return prefix[:index], prefix[index+1:]
1085}
1086
1087func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1088 if t.Kind() != reflect.Struct {
1089 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1090 }
1091
1092 if prefix != "" {
1093 split := strings.SplitN(prefix, ".", 2)
1094 firstPrefix := split[0]
1095 rest := ""
1096 if len(split) > 1 {
1097 rest = split[1]
1098 }
1099 f, exists := t.FieldByName(firstPrefix)
1100 if !exists {
1101 return false
1102 }
1103 ft := f.Type
1104 if ft.Kind() == reflect.Ptr {
1105 ft = ft.Elem()
1106 }
1107 if ft.Kind() != reflect.Struct {
1108 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1109 }
1110 return fieldExistsByNameRecursive(ft, rest, name)
1111 } else {
1112 _, exists := t.FieldByName(name)
1113 return exists
1114 }
1115}