blob: 6ad58c3f253e8927fba38e9529b04a1fd11abf7a [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"
Liz Kammera060c452021-03-24 10:14:47 -0400494 ProductConfigVariable string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000495
496 // Namespace of the variable, if this is a soong_config_module_type variable
497 // e.g. "acme", "ANDROID", "vendor_nae"
498 Namespace string // for soong config variables
499
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
565
566 // The actual property value: list, bool, string..
567 Property interface{}
Liz Kammera060c452021-03-24 10:14:47 -0400568}
569
Jingwen Chena47f28d2021-11-02 16:43:57 +0000570func (p *ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
571 if p.Namespace == "" {
572 return bazel.ProductVariableConfigurationAxis(p.FullConfig)
573 } else {
574 // Soong config variables can be uniquely identified by the namespace
575 // (e.g. acme, android) and the product variable name (e.g. board, size)
576 return bazel.ProductVariableConfigurationAxis(p.Namespace + "__" + p.ProductConfigVariable)
577 }
578}
579
580// ProductConfigProperties is a map of property name to a slice of
581// ProductConfigProperty such that all product variable-specific versions of a
582// property are easily accessed together
Liz Kammer47535c52021-06-02 16:02:22 -0400583type ProductConfigProperties map[string]map[string]ProductConfigProperty
Liz Kammera060c452021-03-24 10:14:47 -0400584
585// ProductVariableProperties returns a ProductConfigProperties containing only the properties which
586// have been set for the module in the given context.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000587func ProductVariableProperties(ctx BazelConversionPathContext) ProductConfigProperties {
Liz Kammera060c452021-03-24 10:14:47 -0400588 module := ctx.Module()
589 moduleBase := module.base()
590
591 productConfigProperties := ProductConfigProperties{}
592
Jingwen Chena47f28d2021-11-02 16:43:57 +0000593 if moduleBase.variableProperties != nil {
594 productVariablesProperty := proptools.FieldNameForProperty("product_variables")
595 productVariableValues(
596 productVariablesProperty,
597 moduleBase.variableProperties,
598 "",
599 "",
600 &productConfigProperties)
601
602 for _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
603 for config, props := range configToProps {
604 // GetArchVariantProperties is creating an instance of the requested type
605 // and productVariablesValues expects an interface, so no need to cast
606 productVariableValues(
607 productVariablesProperty,
608 props,
609 "",
610 config,
611 &productConfigProperties)
612 }
613 }
Liz Kammera060c452021-03-24 10:14:47 -0400614 }
615
Jingwen Chena47f28d2021-11-02 16:43:57 +0000616 if m, ok := module.(Bazelable); ok && m.namespacedVariableProps() != nil {
617 for namespace, namespacedVariableProp := range m.namespacedVariableProps() {
618 productVariableValues(
619 soongconfig.SoongConfigProperty,
620 namespacedVariableProp,
621 namespace,
622 "",
623 &productConfigProperties)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400624 }
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400625 }
626
627 return productConfigProperties
628}
629
Jingwen Chena47f28d2021-11-02 16:43:57 +0000630func (p *ProductConfigProperties) AddProductConfigProperty(
631 propertyName, namespace, productVariableName, config string, property interface{}) {
632 if (*p)[propertyName] == nil {
633 (*p)[propertyName] = make(map[string]ProductConfigProperty)
634 }
635
636 // Normalize config to be all lowercase. It's the "primary key" of this
637 // unique property value. This can be the conditions_default value of the
638 // product variable as well.
639 config = strings.ToLower(config)
640 (*p)[propertyName][config] = ProductConfigProperty{
641 Namespace: namespace, // e.g. acme, android
642 ProductConfigVariable: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
643 FullConfig: config, // e.g. size, feature1-x86, size__conditions_default
644 Property: property, // e.g. ["-O3"]
645 }
646}
647
648var (
649 conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
650)
651
652// maybeExtractConfigVarProp attempts to read this value as a config var struct
653// wrapped by interfaces and ptrs. If it's not the right type, the second return
654// value is false.
655func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
656 if v.Kind() == reflect.Interface {
657 // The conditions_default value can be either
658 // 1) an ptr to an interface of a struct (bool config variables and product variables)
659 // 2) an interface of 1) (config variables with nested structs, like string vars)
660 v = v.Elem()
661 }
662 if v.Kind() != reflect.Ptr {
663 return v, false
664 }
665 v = reflect.Indirect(v)
666 if v.Kind() == reflect.Interface {
667 // Extract the struct from the interface
668 v = v.Elem()
669 }
670
671 if !v.IsValid() {
672 return v, false
673 }
674
675 if v.Kind() != reflect.Struct {
676 return v, false
677 }
678 return v, true
679}
680
681// productVariableValues uses reflection to convert a property struct for
682// product_variables and soong_config_variables to structs that can be generated
683// as select statements.
684func productVariableValues(
685 fieldName string, variableProps interface{}, namespace, suffix string, productConfigProperties *ProductConfigProperties) {
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400686 if suffix != "" {
687 suffix = "-" + suffix
688 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000689
690 // variableValues represent the product_variables or soong_config_variables
691 // struct.
692 variableValues := reflect.ValueOf(variableProps).Elem().FieldByName(fieldName)
693
694 // Example of product_variables:
695 //
696 // product_variables: {
697 // malloc_not_svelte: {
698 // shared_libs: ["malloc_not_svelte_shared_lib"],
699 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
700 // exclude_static_libs: [
701 // "malloc_not_svelte_static_lib_excludes",
702 // "malloc_not_svelte_whole_static_lib_excludes",
703 // ],
704 // },
705 // },
706 //
707 // Example of soong_config_variables:
708 //
709 // soong_config_variables: {
710 // feature1: {
711 // conditions_default: {
712 // ...
713 // },
714 // cflags: ...
715 // },
716 // feature2: {
717 // cflags: ...
718 // conditions_default: {
719 // ...
720 // },
721 // },
722 // board: {
723 // soc_a: {
724 // ...
725 // },
726 // soc_a: {
727 // ...
728 // },
729 // soc_c: {},
730 // conditions_default: {
731 // ...
732 // },
733 // },
734 // }
Liz Kammera060c452021-03-24 10:14:47 -0400735 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000736 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
737 productVariableName := variableValues.Type().Field(i).Name
738
Liz Kammera060c452021-03-24 10:14:47 -0400739 variableValue := variableValues.Field(i)
740 // Check if any properties were set for the module
741 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000742 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400743 continue
744 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000745
746 // Unlike product variables, config variables require a few more
747 // indirections to extract the struct from the reflect.Value.
748 if v, ok := maybeExtractConfigVarProp(variableValue); ok {
749 variableValue = v
750 }
751
Liz Kammera060c452021-03-24 10:14:47 -0400752 for j := 0; j < variableValue.NumField(); j++ {
753 property := variableValue.Field(j)
754 // If the property wasn't set, no need to pass it along
755 if property.IsZero() {
756 continue
757 }
758
759 // e.g. Asflags, Cflags, Enabled, etc.
760 propertyName := variableValue.Type().Field(j).Name
Jingwen Chena47f28d2021-11-02 16:43:57 +0000761
762 if v, ok := maybeExtractConfigVarProp(property); ok {
763 // The field is a struct, which is used by:
764 // 1) soong_config_string_variables
765 //
766 // soc_a: {
767 // cflags: ...,
768 // }
769 //
770 // soc_b: {
771 // cflags: ...,
772 // }
773 //
774 // 2) conditions_default structs for all soong config variable types.
775 //
776 // conditions_default: {
777 // cflags: ...,
778 // static_libs: ...
779 // }
780 field := v
781 for k := 0; k < field.NumField(); k++ {
782 // Iterate over fields of this struct prop.
783 if field.Field(k).IsZero() {
784 continue
785 }
786 productVariableValue := proptools.PropertyNameForField(propertyName)
787 config := strings.Join([]string{namespace, productVariableName, productVariableValue}, "__")
788 actualPropertyName := field.Type().Field(k).Name
789
790 productConfigProperties.AddProductConfigProperty(
791 actualPropertyName, // e.g. cflags, static_libs
792 namespace, // e.g. acme, android
793 productVariableName, // e.g. size, feature1, FEATURE2, board
794 config,
795 field.Field(k).Interface(), // e.g. ["-DDEFAULT"], ["foo", "bar"]
796 )
797 }
798 } else {
799 // Not a conditions_default or a struct prop, i.e. regular
800 // product variables, or not a string-typed config var.
801 config := productVariableName + suffix
802 if namespace != "" {
803 config = namespace + "__" + config
804 }
805 productConfigProperties.AddProductConfigProperty(
806 propertyName,
807 namespace,
808 productVariableName,
809 config,
810 property.Interface(),
811 )
Liz Kammer47535c52021-06-02 16:02:22 -0400812 }
Liz Kammera060c452021-03-24 10:14:47 -0400813 }
814 }
Liz Kammera060c452021-03-24 10:14:47 -0400815}
816
Colin Crosseabaedd2020-02-06 17:01:55 -0800817func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700818 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700819 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700820 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700821 return
822 }
823
824 // TODO: depend on config variable, create variants, propagate variants up tree
825 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700826
827 if a.variableProperties == nil {
828 return
829 }
830
831 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700832
Colin Cross0cec3122021-01-20 11:27:32 -0800833 productVariables := reflect.ValueOf(mctx.Config().productVariables)
834
Colin Cross7f64b6d2015-07-09 13:57:48 -0700835 for i := 0; i < variableValues.NumField(); i++ {
836 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700837 name := variableValues.Type().Field(i).Name
838 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700839
Colin Crossa6bc19e2015-09-16 13:53:42 -0700840 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -0800841 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700842 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
843 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700844 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700845
846 val = val.Elem()
847
848 // For bools, check that the value is true
849 if val.Kind() == reflect.Bool && val.Bool() == false {
850 continue
851 }
852
853 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -0800854 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700855 continue
856 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700857 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700858 }
859}
860
Colin Cross4157e882019-06-06 16:57:04 -0700861func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700862 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
863
Colin Cross7e0eaf12017-05-05 16:16:24 -0700864 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700865
Colin Cross4157e882019-06-06 16:57:04 -0700866 err := proptools.AppendMatchingProperties(m.generalProperties,
Colin Cross06a931b2015-10-28 17:23:31 -0700867 productVariablePropertyValue.Addr().Interface(), nil)
868 if err != nil {
869 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
870 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
871 } else {
872 panic(err)
873 }
874 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700875}
876
Colin Cross7e0eaf12017-05-05 16:16:24 -0700877func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
878 productVariablePropertyValue reflect.Value, i int, err error) {
879
880 field := productVariablePropertyValue.Type().Field(i).Name
881 property := prefix + "." + proptools.PropertyNameForField(field)
882 ctx.PropertyErrorf(property, "%s", err)
883}
884
885func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
886 productVariablePropertyValue reflect.Value, variableValue interface{}) {
887
Colin Cross7f64b6d2015-07-09 13:57:48 -0700888 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
889 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800890 kind := propertyValue.Kind()
891 if kind == reflect.Ptr {
892 if propertyValue.IsNil() {
893 continue
894 }
895 propertyValue = propertyValue.Elem()
896 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700897 switch propertyValue.Kind() {
898 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700899 err := printfIntoProperty(propertyValue, variableValue)
900 if err != nil {
901 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
902 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700903 case reflect.Slice:
904 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700905 err := printfIntoProperty(propertyValue.Index(j), variableValue)
906 if err != nil {
907 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
908 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700909 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800910 case reflect.Bool:
911 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700912 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700913 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700914 default:
915 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
916 }
917 }
918}
919
Colin Cross7e0eaf12017-05-05 16:16:24 -0700920func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700921 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700922
923 count := strings.Count(s, "%")
924 if count == 0 {
925 return nil
926 }
927
928 if count > 1 {
929 return fmt.Errorf("product variable properties only support a single '%%'")
930 }
931
Colin Cross7f64b6d2015-07-09 13:57:48 -0700932 if strings.Contains(s, "%d") {
933 switch v := variableValue.(type) {
934 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700935 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700936 case bool:
937 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700938 variableValue = 1
939 } else {
940 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700941 }
942 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700943 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700944 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700945 } else if strings.Contains(s, "%s") {
946 switch variableValue.(type) {
947 case string:
948 // Nothing
949 default:
950 return fmt.Errorf("unsupported type %T for %%s", variableValue)
951 }
952 } else {
953 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700954 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700955
956 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
957
958 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700959}
Colin Cross18c46802019-09-24 22:19:02 -0700960
961var variablePropTypeMap OncePer
962
963// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
964// reflect.Types of each property struct. The result can be used as a key in a map.
965func sliceToTypeArray(s []interface{}) interface{} {
966 // Create an array using reflection whose length is the length of the input slice
967 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
968 for i, e := range s {
969 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
970 }
971 return ret.Interface()
972}
973
Colin Crosseabaedd2020-02-06 17:01:55 -0800974func initProductVariableModule(m Module) {
975 base := m.base()
976
977 // Allow tests to override the default product variables
978 if base.variableProperties == nil {
979 base.variableProperties = defaultProductVariables
980 }
981 // Filter the product variables properties to the ones that exist on this module
982 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
983 if base.variableProperties != nil {
984 m.AddProperties(base.variableProperties)
985 }
986}
987
Colin Cross18c46802019-09-24 22:19:02 -0700988// createVariableProperties takes the list of property structs for a module and returns a property struct that
989// contains the product variable properties that exist in the property structs, or nil if there are none. It
990// caches the result.
991func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
992 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
993 key := sliceToTypeArray(moduleTypeProps)
994
995 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
996 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
997 // Compute the filtered property struct type.
998 return createVariablePropertiesType(moduleTypeProps, productVariables)
999 }).(reflect.Type)
1000
1001 if typ == nil {
1002 return nil
1003 }
1004
1005 // Create a new pointer to a filtered property struct.
1006 return reflect.New(typ).Interface()
1007}
1008
1009// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1010// a list of property structs.
1011func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1012 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1013 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1014 // Filter function, returns true if the field should be in the resulting struct
1015 if prefix == "" {
1016 // Keep the top level Product_variables field
1017 return true, field
1018 }
1019 _, rest := splitPrefix(prefix)
1020 if rest == "" {
1021 // Keep the 2nd level field (i.e. Product_variables.Eng)
1022 return true, field
1023 }
1024
1025 // Strip off the first 2 levels of the prefix
1026 _, prefix = splitPrefix(rest)
1027
1028 for _, p := range moduleTypeProps {
1029 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1030 // Keep any fields that exist in one of the property structs
1031 return true, field
1032 }
1033 }
1034
1035 return false, field
1036 })
1037 return typ
1038}
1039
1040func splitPrefix(prefix string) (first, rest string) {
1041 index := strings.IndexByte(prefix, '.')
1042 if index == -1 {
1043 return prefix, ""
1044 }
1045 return prefix[:index], prefix[index+1:]
1046}
1047
1048func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1049 if t.Kind() != reflect.Struct {
1050 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1051 }
1052
1053 if prefix != "" {
1054 split := strings.SplitN(prefix, ".", 2)
1055 firstPrefix := split[0]
1056 rest := ""
1057 if len(split) > 1 {
1058 rest = split[1]
1059 }
1060 f, exists := t.FieldByName(firstPrefix)
1061 if !exists {
1062 return false
1063 }
1064 ft := f.Type
1065 if ft.Kind() == reflect.Ptr {
1066 ft = ft.Elem()
1067 }
1068 if ft.Kind() != reflect.Struct {
1069 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1070 }
1071 return fieldExistsByNameRecursive(ft, rest, name)
1072 } else {
1073 _, exists := t.FieldByName(name)
1074 return exists
1075 }
1076}