blob: a331439f6fed71e31a6f1bb22c8a285a7b641489 [file] [log] [blame]
Colin Cross7f64b6d2015-07-09 13:57:48 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross7f64b6d2015-07-09 13:57:48 -070016
17import (
18 "fmt"
19 "reflect"
Dan Willemsen490fd492015-11-24 17:53:15 -080020 "runtime"
Colin Cross7f64b6d2015-07-09 13:57:48 -070021 "strings"
22
Colin Cross7f64b6d2015-07-09 13:57:48 -070023 "github.com/google/blueprint/proptools"
24)
25
26func init() {
Paul Duffine7a055c2021-03-07 15:46:33 +000027 registerVariableBuildComponents(InitRegistrationContext)
28}
29
30func registerVariableBuildComponents(ctx RegistrationContext) {
31 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
Colin Crosseabaedd2020-02-06 17:01:55 -080032 ctx.BottomUp("variable", VariableMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070033 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070034}
35
Paul Duffine7a055c2021-03-07 15:46:33 +000036var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
37
Colin Cross7f64b6d2015-07-09 13:57:48 -070038type variableProperties struct {
39 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070040 Platform_sdk_version struct {
41 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070042 Cflags []string
Dan Willemsen9fe14102021-07-13 21:52:04 -070043 Cmd *string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070044 }
Colin Cross83163192015-12-01 16:31:16 -080045
Yuntao Xu402e9b02021-08-09 15:44:44 -070046 Platform_sdk_version_or_codename struct {
47 Java_resource_dirs []string
48 }
49
Anton Hanssonb021bf82021-08-27 17:35:40 +010050 Platform_sdk_extension_version struct {
51 Cmd *string
52 }
53
Dan Willemsen9fe14102021-07-13 21:52:04 -070054 Platform_version_name struct {
55 Base_dir *string
56 }
57
Steven Morelanda48df2b2024-06-03 22:29:38 +000058 Shipping_api_level struct {
59 Cflags []string
60 }
61
Colin Cross83163192015-12-01 16:31:16 -080062 // unbundled_build is a catch-all property to annotate modules that don't build in one or
63 // more unbundled branches, usually due to dependencies missing from the manifest.
64 Unbundled_build struct {
Cole Fausta963b942024-04-11 17:43:00 -070065 Enabled proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Colin Cross83163192015-12-01 16:31:16 -080066 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080067
Jeongik Chaf08e59c2023-02-09 10:47:59 +090068 // similar to `Unbundled_build`, but `Always_use_prebuilt_sdks` means that it uses prebuilt
69 // sdk specifically.
70 Always_use_prebuilt_sdks struct {
Cole Fausta963b942024-04-11 17:43:00 -070071 Enabled proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Jeongik Chaf08e59c2023-02-09 10:47:59 +090072 } `android:"arch_variant"`
73
Christopher Ferrisa5a6b9c2024-04-23 17:17:49 -070074 Malloc_low_memory struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080075 Cflags []string `android:"arch_variant"`
76 Shared_libs []string `android:"arch_variant"`
77 Whole_static_libs []string `android:"arch_variant"`
Florian Mayerca2854e2024-01-16 16:42:34 -080078 Static_libs []string `android:"arch_variant"`
Christopher Ferrisc71193a2019-12-16 09:55:10 -080079 Exclude_static_libs []string `android:"arch_variant"`
Christopher Ferrisdea66172021-09-24 00:07:53 -070080 Srcs []string `android:"arch_variant"`
81 Header_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070082 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070083
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070084 Malloc_zero_contents struct {
85 Cflags []string `android:"arch_variant"`
86 } `android:"arch_variant"`
87
88 Malloc_pattern_fill_contents struct {
89 Cflags []string `android:"arch_variant"`
90 } `android:"arch_variant"`
91
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070092 Safestack struct {
93 Cflags []string `android:"arch_variant"`
94 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070095
Dan Willemsen1be35382016-07-25 17:42:18 -070096 Binder32bit struct {
97 Cflags []string
98 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070099
Colin Cross95436422017-05-04 13:57:05 -0700100 Override_rs_driver struct {
101 Cflags []string
102 }
103
Steven Morelandc2b9f062017-12-13 14:06:20 -0800104 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
105 // enabled.
106 Treble_linker_namespaces struct {
107 Cflags []string
108 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800109 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
110 Enforce_vintf_manifest struct {
111 Cflags []string
112 }
113
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000114 Build_from_text_stub struct {
115 Static_libs []string
116 Exclude_static_libs []string
117 }
118
Colin Cross6bc59ef2016-12-08 09:46:35 -0800119 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
120 // debugging features that don't significantly impact runtime behavior. userdebug builds
121 // are used for dogfooding and performance testing, and should be as similar to user builds
122 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700123 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700124 Cflags []string
125 Cppflags []string
126 Init_rc []string
127 Required []string
128 Host_required []string
129 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700130 Strip struct {
131 All *bool
132 Keep_symbols *bool
133 Keep_symbols_and_debug_frame *bool
134 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700135 Static_libs []string
136 Whole_static_libs []string
137 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900138
139 Cmdline []string
Inseob Kimf5ed3002022-01-07 19:17:29 +0900140
Jay Aliomer2599d1d2021-09-23 15:49:54 -0400141 Srcs []string
142 Exclude_srcs []string
Cole Fausted940002023-08-15 11:59:24 -0700143 Cmd *string
Jiyong Park62532d72024-04-12 16:16:28 +0900144
145 Deps []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700146 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800147
Usta Shresthac725f472022-01-11 02:44:21 -0500148 // eng is true for -eng builds, and can be used to turn on additional heavyweight debugging
Colin Cross6bc59ef2016-12-08 09:46:35 -0800149 // features.
150 Eng struct {
151 Cflags []string
152 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700153 Lto struct {
154 Never *bool
155 }
Colin Cross3273cc22018-10-26 23:58:42 -0700156 Sanitize struct {
157 Address *bool
158 }
Felka Changf13642e2019-06-21 20:58:27 +0800159 Optimize struct {
160 Enabled *bool
161 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800162 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700163
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700164 Uml struct {
165 Cppflags []string
166 }
Risanea004dd2017-11-27 18:14:46 +0900167
168 Arc struct {
Satoshi Niwa54f48262020-12-18 09:53:26 +0900169 Cflags []string `android:"arch_variant"`
170 Exclude_srcs []string `android:"arch_variant"`
Jasmine Chenacace422021-03-05 00:56:40 +0800171 Header_libs []string `android:"arch_variant"`
Satoshi Niwa54f48262020-12-18 09:53:26 +0900172 Include_dirs []string `android:"arch_variant"`
173 Shared_libs []string `android:"arch_variant"`
174 Static_libs []string `android:"arch_variant"`
175 Srcs []string `android:"arch_variant"`
176 Whole_static_libs []string `android:"arch_variant"`
177 } `android:"arch_variant"`
Roland Levillain2879d412019-08-13 15:00:18 +0100178
Colin Crossd9a121b2020-01-27 13:26:42 -0800179 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800180 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800181 Srcs []string `android:"arch_variant"`
182 Exclude_srcs []string `android:"arch_variant"`
183 } `android:"arch_variant"`
Devin Moorec381e102023-07-06 22:03:58 +0000184
185 // release_aidl_use_unfrozen is "true" when a device can
186 // use the unfrozen versions of AIDL interfaces.
187 Release_aidl_use_unfrozen struct {
Devin Moore570d72b2024-05-24 10:07:55 +0000188 Cflags []string
189 Cmd *string
yangbill585564c2024-05-23 10:50:56 +0000190 Required []string
Devin Moore570d72b2024-05-24 10:07:55 +0000191 Vintf_fragments []string
Devin Moorec381e102023-07-06 22:03:58 +0000192 }
Colin Cross83163192015-12-01 16:31:16 -0800193 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700194}
195
Colin Cross6961a492020-02-06 16:57:45 -0800196var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700197
Cole Faustf8231dd2023-04-21 17:37:11 -0700198type ProductVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700199 // Suffix to add to generated Makefiles
200 Make_suffix *string `json:",omitempty"`
201
Inseob Kim2da72af2024-06-18 11:09:12 +0900202 BuildId *string `json:",omitempty"`
203 BuildFingerprintFile *string `json:",omitempty"`
204 BuildNumberFile *string `json:",omitempty"`
205 BuildHostnameFile *string `json:",omitempty"`
206 BuildThumbprintFile *string `json:",omitempty"`
207 DisplayBuildNumber *bool `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800208
Linus Tufvessonba270c52024-04-12 13:06:22 +0200209 Platform_display_version_name *string `json:",omitempty"`
210 Platform_version_name *string `json:",omitempty"`
211 Platform_sdk_version *int `json:",omitempty"`
212 Platform_sdk_codename *string `json:",omitempty"`
213 Platform_sdk_version_or_codename *string `json:",omitempty"`
214 Platform_sdk_final *bool `json:",omitempty"`
215 Platform_sdk_extension_version *int `json:",omitempty"`
216 Platform_base_sdk_extension_version *int `json:",omitempty"`
217 Platform_version_active_codenames []string `json:",omitempty"`
218 Platform_version_all_preview_codenames []string `json:",omitempty"`
219 Platform_systemsdk_versions []string `json:",omitempty"`
220 Platform_security_patch *string `json:",omitempty"`
221 Platform_preview_sdk_version *string `json:",omitempty"`
222 Platform_base_os *string `json:",omitempty"`
223 Platform_version_last_stable *string `json:",omitempty"`
224 Platform_version_known_codenames *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700225
Jeongik Cha219141c2020-08-06 23:00:37 +0900226 DeviceName *string `json:",omitempty"`
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000227 DeviceProduct *string `json:",omitempty"`
Jeongik Cha219141c2020-08-06 23:00:37 +0900228 DeviceArch *string `json:",omitempty"`
229 DeviceArchVariant *string `json:",omitempty"`
230 DeviceCpuVariant *string `json:",omitempty"`
231 DeviceAbi []string `json:",omitempty"`
232 DeviceVndkVersion *string `json:",omitempty"`
233 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
234 DeviceSystemSdkVersions []string `json:",omitempty"`
Juan Yescas05d4d902023-04-07 10:35:35 -0700235 DeviceMaxPageSizeSupported *string `json:",omitempty"`
Vilas Bhatb3d2d222023-12-04 22:51:20 +0000236 DeviceNoBionicPageSizeMacro *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700237
Justin Yun41cbb5e2023-11-29 17:58:16 +0900238 VendorApiLevel *string `json:",omitempty"`
239
Jose Galmes6f843bc2020-12-11 13:36:29 -0800240 RecoverySnapshotVersion *string `json:",omitempty"`
241
Colin Crossa74ca042019-01-31 14:31:51 -0800242 DeviceSecondaryArch *string `json:",omitempty"`
243 DeviceSecondaryArchVariant *string `json:",omitempty"`
244 DeviceSecondaryCpuVariant *string `json:",omitempty"`
245 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700246
dimitry8d6dde82019-07-11 10:23:53 +0200247 NativeBridgeArch *string `json:",omitempty"`
248 NativeBridgeArchVariant *string `json:",omitempty"`
249 NativeBridgeCpuVariant *string `json:",omitempty"`
250 NativeBridgeAbi []string `json:",omitempty"`
251 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100252
dimitry8d6dde82019-07-11 10:23:53 +0200253 NativeBridgeSecondaryArch *string `json:",omitempty"`
254 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
255 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
256 NativeBridgeSecondaryAbi []string `json:",omitempty"`
257 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100258
Colin Cross4225f652015-09-17 14:33:42 -0700259 HostArch *string `json:",omitempty"`
260 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000261 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800262
263 CrossHost *string `json:",omitempty"`
264 CrossHostArch *string `json:",omitempty"`
265 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800266
Jeongik Chacee5ba92021-02-19 12:11:51 +0900267 DeviceResourceOverlays []string `json:",omitempty"`
268 ProductResourceOverlays []string `json:",omitempty"`
269 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800270 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700271
Colin Crossa74ca042019-01-31 14:31:51 -0800272 AAPTCharacteristics *string `json:",omitempty"`
273 AAPTConfig []string `json:",omitempty"`
274 AAPTPreferredConfig *string `json:",omitempty"`
275 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700276
Inseob Kim80fa7982022-08-12 21:36:25 +0900277 DefaultAppCertificate *string `json:",omitempty"`
278 MainlineSepolicyDevCertificates *string `json:",omitempty"`
Colin Cross61ae0b72017-12-01 17:16:02 -0800279
Colin Crossface4e42017-10-30 17:32:15 -0700280 AppsDefaultVersionName *string `json:",omitempty"`
281
Cole Faust701ca252021-11-23 19:02:08 -0800282 Allow_missing_dependencies *bool `json:",omitempty"`
283 Unbundled_build *bool `json:",omitempty"`
284 Unbundled_build_apps []string `json:",omitempty"`
285 Unbundled_build_image *bool `json:",omitempty"`
286 Always_use_prebuilt_sdks *bool `json:",omitempty"`
287 Skip_boot_jars_check *bool `json:",omitempty"`
Christopher Ferrisa5a6b9c2024-04-23 17:17:49 -0700288 Malloc_low_memory *bool `json:",omitempty"`
Cole Faust701ca252021-11-23 19:02:08 -0800289 Malloc_zero_contents *bool `json:",omitempty"`
290 Malloc_pattern_fill_contents *bool `json:",omitempty"`
291 Safestack *bool `json:",omitempty"`
292 HostStaticBinaries *bool `json:",omitempty"`
293 Binder32bit *bool `json:",omitempty"`
294 UseGoma *bool `json:",omitempty"`
295 UseRBE *bool `json:",omitempty"`
296 UseRBEJAVAC *bool `json:",omitempty"`
297 UseRBER8 *bool `json:",omitempty"`
298 UseRBED8 *bool `json:",omitempty"`
299 Debuggable *bool `json:",omitempty"`
300 Eng *bool `json:",omitempty"`
301 Treble_linker_namespaces *bool `json:",omitempty"`
302 Enforce_vintf_manifest *bool `json:",omitempty"`
303 Uml *bool `json:",omitempty"`
304 Arc *bool `json:",omitempty"`
305 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000306 Build_from_text_stub *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800307
Inseob Kim8fa54da2024-03-19 16:48:59 +0900308 BuildType *string `json:",omitempty"`
309
Logan Chien4fcea3d2018-11-20 11:59:08 +0800310 Check_elf_files *bool `json:",omitempty"`
311
Colin Cross5a0dcd52018-10-05 14:20:06 -0700312 UncompressPrivAppDex *bool `json:",omitempty"`
313 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800314
satayevd604b212021-07-21 14:23:52 +0100315 BootJars ConfiguredJarList `json:",omitempty"`
316 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000317
Colin Crossa74ca042019-01-31 14:31:51 -0800318 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700319
Colin Crossa74ca042019-01-31 14:31:51 -0800320 EnableCFI *bool `json:",omitempty"`
321 CFIExcludePaths []string `json:",omitempty"`
322 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700323
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800324 DisableScudo *bool `json:",omitempty"`
325
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800326 MemtagHeapExcludePaths []string `json:",omitempty"`
327 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
328 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
329
Hang Lua98aab92023-03-17 13:17:22 +0800330 HWASanIncludePaths []string `json:",omitempty"`
Tomislav Novakf734f002023-08-22 10:51:44 -0700331 HWASanExcludePaths []string `json:",omitempty"`
Hang Lua98aab92023-03-17 13:17:22 +0800332
Justin Yund5f6c822019-06-25 16:47:17 +0900333 VendorPath *string `json:",omitempty"`
334 OdmPath *string `json:",omitempty"`
335 ProductPath *string `json:",omitempty"`
336 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800337
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700338 ClangTidy *bool `json:",omitempty"`
339 TidyChecks *string `json:",omitempty"`
340
Roland Levillainada12702020-06-09 13:07:36 +0100341 JavaCoveragePaths []string `json:",omitempty"`
342 JavaCoverageExcludePaths []string `json:",omitempty"`
343
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800344 GcovCoverage *bool `json:",omitempty"`
345 ClangCoverage *bool `json:",omitempty"`
346 NativeCoveragePaths []string `json:",omitempty"`
347 NativeCoverageExcludePaths []string `json:",omitempty"`
348 ClangCoverageContinuousMode *bool `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800349
Colin Cross1a6acd42020-06-16 17:51:46 -0700350 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400351 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700352
Colin Cross23ae82a2016-11-02 14:34:39 -0700353 SanitizeHost []string `json:",omitempty"`
354 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700355 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700356 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800357
358 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800359
360 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700361
362 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900363
364 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900365
366 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800367
368 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800369
Vinh Tran7a8362c2022-11-01 15:33:51 -0400370 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700371
Inseob Kimae553032019-05-14 18:52:49 +0900372 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800373
Inseob Kim7cf14652021-01-06 23:06:52 +0900374 DirectedVendorSnapshot bool `json:",omitempty"`
375 VendorSnapshotModules map[string]bool `json:",omitempty"`
376
Jose Galmes4c6895e2021-02-09 07:44:30 -0800377 DirectedRecoverySnapshot bool `json:",omitempty"`
378 RecoverySnapshotModules map[string]bool `json:",omitempty"`
379
Justin DeMartino383bfb32021-02-24 10:49:43 -0800380 VendorSnapshotDirsIncluded []string `json:",omitempty"`
381 VendorSnapshotDirsExcluded []string `json:",omitempty"`
382 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
383 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000384 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800385
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900386 MultitreeUpdateMeta bool `json:",omitempty"`
387
Inseob Kim6077b232023-08-31 16:49:59 +0900388 BoardVendorSepolicyDirs []string `json:",omitempty"`
389 BoardOdmSepolicyDirs []string `json:",omitempty"`
390 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
391 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
392 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700393
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900394 BoardSepolicyVers *string `json:",omitempty"`
395 PlatformSepolicyVersion *string `json:",omitempty"`
396
Inseob Kim1a0afcc2022-02-14 23:10:51 +0900397 SystemExtSepolicyPrebuiltApiDir *string `json:",omitempty"`
398 ProductSepolicyPrebuiltApiDir *string `json:",omitempty"`
399
Inseob Kim843f6642022-01-07 09:11:23 +0900400 PlatformSepolicyCompatVersions []string `json:",omitempty"`
401
Cole Faust46f6e2f2024-06-20 12:57:43 -0700402 VendorVars map[string]map[string]string `json:",omitempty"`
403 VendorVarTypes map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700404
Dan Albertd05ba002021-04-13 15:55:47 -0700405 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900406
Dennis Shene2ed70c2023-01-11 14:15:43 +0000407 TrimmedApex *bool `json:",omitempty"`
Jiyong Park4da07972021-01-05 21:01:11 +0900408 ForceApexSymlinkOptimization *bool `json:",omitempty"`
409 CompressedApex *bool `json:",omitempty"`
410 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800411
412 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900413
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900414 WithDexpreopt bool `json:",omitempty"`
415
Jiakai Zhang4d90da22023-07-12 16:51:48 +0100416 ManifestPackageNameOverrides []string `json:",omitempty"`
417 CertificateOverrides []string `json:",omitempty"`
418 PackageNameOverrides []string `json:",omitempty"`
419 ConfiguredJarLocationOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900420
Albert Martineefabcf2022-03-21 20:11:16 +0000421 ApexGlobalMinSdkVersionOverride *string `json:",omitempty"`
422
Jeongik Chac9464142019-01-07 12:07:27 +0900423 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700424 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800425
Colin Crossf24a22a2019-01-31 14:12:44 -0800426 ProductHiddenAPIStubs []string `json:",omitempty"`
427 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
428 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700429
Inseob Kim0866b002019-04-15 20:21:29 +0900430 ProductPublicSepolicyDirs []string `json:",omitempty"`
431 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900432
Dan Willemsen54879d12019-04-18 10:08:46 -0700433 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700434
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900435 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900436
JaeMan Parkff715562020-10-19 17:25:58 +0900437 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
438 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
439
Yifan Hong82db7352020-01-21 16:12:26 -0800440 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700441
Yifan Hong42bef8d2020-08-05 14:36:09 -0700442 BoardKernelBinaries []string `json:",omitempty"`
443 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700444
445 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800446
447 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900448
Steven Morelanda48df2b2024-06-03 22:29:38 +0000449 Shipping_api_level *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900450
Cole Faustded79602023-09-05 17:48:11 -0700451 BuildBrokenPluginValidation []string `json:",omitempty"`
452 BuildBrokenClangAsFlags bool `json:",omitempty"`
453 BuildBrokenClangCFlags bool `json:",omitempty"`
454 BuildBrokenClangProperty bool `json:",omitempty"`
455 GenruleSandboxing *bool `json:",omitempty"`
456 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
457 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
458 BuildBrokenUsesSoongPython2Modules bool `json:",omitempty"`
459 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
460 BuildBrokenIncorrectPartitionImages bool `json:",omitempty"`
461 BuildBrokenInputDirModules []string `json:",omitempty"`
Jiyong Park3bb99242024-01-04 23:21:26 +0000462 BuildBrokenDontCheckSystemSdk bool `json:",omitempty"`
Inseob Kim2da72af2024-06-18 11:09:12 +0900463 BuildBrokenDupSysprop bool `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900464
Jiakai Zhang48203e32023-06-02 23:42:21 +0100465 BuildWarningBadOptionalUsesLibsAllowlist []string `json:",omitempty"`
466
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700467 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
468
Inseob Kim67e5add192021-03-17 18:05:33 +0900469 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
470
471 SelinuxIgnoreNeverallows bool `json:",omitempty"`
472
Devin Moorec381e102023-07-06 22:03:58 +0000473 Release_aidl_use_unfrozen *bool `json:",omitempty"`
474
Inseob Kima10ef272021-09-15 03:04:53 +0000475 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
476 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900477
478 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Christopher Ferris98f10222022-07-13 23:16:52 -0700479
480 IgnorePrefer32OnDevice bool `json:",omitempty"`
Spandan Dasc5763832022-11-08 18:42:16 +0000481
Sam Delmerico98a73292023-02-21 11:50:29 -0500482 SourceRootDirs []string `json:",omitempty"`
Vinh Tran44cb78c2023-03-09 22:07:19 -0500483
484 AfdoProfiles []string `json:",omitempty"`
Wei Lic642d682023-05-04 09:06:06 -0700485
Inseob Kim8fa54da2024-03-19 16:48:59 +0900486 ProductManufacturer string `json:",omitempty"`
487 ProductBrand string `json:",omitempty"`
Joe Onoratofee845a2023-05-09 08:14:14 -0700488
Yu Liueebb2592023-10-12 20:31:27 -0700489 ReleaseVersion string `json:",omitempty"`
490 ReleaseAconfigValueSets []string `json:",omitempty"`
Kiyoung Kime623c582023-07-06 16:43:44 +0900491
Zhi Dou3f65a412023-08-10 21:47:40 +0000492 ReleaseAconfigFlagDefaultPermission string `json:",omitempty"`
493
Alyssa Ketpreechasawat34ab8792023-10-06 07:01:22 +0000494 ReleaseDefaultModuleBuildFromSource *bool `json:",omitempty"`
495
Inseob Kim5bac3b62023-08-25 21:29:46 +0900496 CheckVendorSeappViolations *bool `json:",omitempty"`
Cole Faustb4cb0c82023-09-14 15:16:58 -0700497
Inseob Kime4e85d52023-10-25 23:53:58 +0900498 BuildFlags map[string]string `json:",omitempty"`
Jihoon Kangc1b04d62023-11-16 19:07:04 +0000499
Cole Faust751a4a52024-05-21 16:51:59 -0700500 BuildFlagTypes map[string]string `json:",omitempty"`
501
Jihoon Kangc1b04d62023-11-16 19:07:04 +0000502 BuildFromSourceStub *bool `json:",omitempty"`
Spandan Das8ab28dd2024-02-17 03:31:45 +0000503
Spandan Das0d24ade2024-03-08 04:20:15 +0000504 BuildIgnoreApexContributionContents *bool `json:",omitempty"`
Jihoon Kangb36fc542024-02-22 19:35:26 +0000505
506 HiddenapiExportableStubs *bool `json:",omitempty"`
Jihoon Kang35349462024-02-22 19:52:46 +0000507
508 ExportRuntimeApis *bool `json:",omitempty"`
Yu Liu67a28422024-03-05 00:36:31 +0000509
510 AconfigContainerValidation string `json:",omitempty"`
Inseob Kim8fa54da2024-03-19 16:48:59 +0900511
512 ProductLocales []string `json:",omitempty"`
513
514 ProductDefaultWifiChannels []string `json:",omitempty"`
515
516 BoardUseVbmetaDigestInFingerprint *bool `json:",omitempty"`
517
518 OemProperties []string `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700519}
520
Cole Faust11edf552023-10-13 11:32:14 -0700521type PartitionQualifiedVariablesType struct {
522 BuildingImage bool `json:",omitempty"`
523 BoardErofsCompressor string `json:",omitempty"`
524 BoardErofsCompressHints string `json:",omitempty"`
525 BoardErofsPclusterSize string `json:",omitempty"`
526 BoardExtfsInodeCount string `json:",omitempty"`
527 BoardExtfsRsvPct string `json:",omitempty"`
528 BoardF2fsSloadCompressFlags string `json:",omitempty"`
529 BoardFileSystemCompress string `json:",omitempty"`
530 BoardFileSystemType string `json:",omitempty"`
531 BoardJournalSize string `json:",omitempty"`
532 BoardPartitionReservedSize string `json:",omitempty"`
533 BoardPartitionSize string `json:",omitempty"`
534 BoardSquashfsBlockSize string `json:",omitempty"`
535 BoardSquashfsCompressor string `json:",omitempty"`
536 BoardSquashfsCompressorOpt string `json:",omitempty"`
537 BoardSquashfsDisable4kAlign string `json:",omitempty"`
538 ProductBaseFsPath string `json:",omitempty"`
539 ProductHeadroom string `json:",omitempty"`
540 ProductVerityPartition string `json:",omitempty"`
541
542 BoardAvbAddHashtreeFooterArgs string `json:",omitempty"`
543 BoardAvbKeyPath string `json:",omitempty"`
544 BoardAvbAlgorithm string `json:",omitempty"`
545 BoardAvbRollbackIndex string `json:",omitempty"`
546 BoardAvbRollbackIndexLocation string `json:",omitempty"`
547}
548
Cole Faustcb193ec2023-09-20 16:01:18 -0700549type PartitionVariables struct {
550 ProductDirectory string `json:",omitempty"`
Cole Faust11edf552023-10-13 11:32:14 -0700551 PartitionQualifiedVariables map[string]PartitionQualifiedVariablesType
552 TargetUserimagesUseExt2 bool `json:",omitempty"`
553 TargetUserimagesUseExt3 bool `json:",omitempty"`
554 TargetUserimagesUseExt4 bool `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700555
556 TargetUserimagesSparseExtDisabled bool `json:",omitempty"`
557 TargetUserimagesSparseErofsDisabled bool `json:",omitempty"`
558 TargetUserimagesSparseSquashfsDisabled bool `json:",omitempty"`
559 TargetUserimagesSparseF2fsDisabled bool `json:",omitempty"`
560
Yi-Yo Chiang939fe1a2023-11-24 14:58:50 +0800561 BoardErofsCompressor string `json:",omitempty"`
562 BoardErofsCompressorHints string `json:",omitempty"`
563 BoardErofsPclusterSize string `json:",omitempty"`
564 BoardErofsShareDupBlocks string `json:",omitempty"`
565 BoardErofsUseLegacyCompression string `json:",omitempty"`
566 BoardExt4ShareDupBlocks string `json:",omitempty"`
567 BoardFlashLogicalBlockSize string `json:",omitempty"`
568 BoardFlashEraseBlockSize string `json:",omitempty"`
569 BoardUsesRecoveryAsBoot bool `json:",omitempty"`
570 ProductUseDynamicPartitionSize bool `json:",omitempty"`
571 CopyImagesForTargetFilesZip bool `json:",omitempty"`
Cole Faustb5055392023-09-27 13:44:40 -0700572
573 BoardAvbEnable bool `json:",omitempty"`
Cole Faust11edf552023-10-13 11:32:14 -0700574
575 ProductPackages []string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700576}
577
578func boolPtr(v bool) *bool {
579 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700580}
581
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700582func intPtr(v int) *int {
583 return &v
584}
585
Colin Cross4225f652015-09-17 14:33:42 -0700586func stringPtr(v string) *string {
587 return &v
588}
589
Cole Faustf8231dd2023-04-21 17:37:11 -0700590func (v *ProductVariables) SetDefaultConfig() {
591 *v = ProductVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800592 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700593
Dan Albert8c7a9942023-03-27 20:34:01 +0000594 Platform_version_name: stringPtr("S"),
595 Platform_base_sdk_extension_version: intPtr(30),
596 Platform_sdk_version: intPtr(30),
597 Platform_sdk_codename: stringPtr("S"),
598 Platform_sdk_final: boolPtr(false),
599 Platform_version_active_codenames: []string{"S"},
600 Platform_version_all_preview_codenames: []string{"S"},
Dan Willemsencbf9e822017-11-13 14:34:56 -0800601
Vilas Bhatb3d2d222023-12-04 22:51:20 +0000602 HostArch: stringPtr("x86_64"),
603 HostSecondaryArch: stringPtr("x86"),
604 DeviceName: stringPtr("generic_arm64"),
605 DeviceProduct: stringPtr("aosp_arm-eng"),
606 DeviceArch: stringPtr("arm64"),
607 DeviceArchVariant: stringPtr("armv8-a"),
608 DeviceCpuVariant: stringPtr("generic"),
609 DeviceAbi: []string{"arm64-v8a"},
610 DeviceSecondaryArch: stringPtr("arm"),
611 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
612 DeviceSecondaryCpuVariant: stringPtr("generic"),
613 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
614 DeviceMaxPageSizeSupported: stringPtr("4096"),
615 DeviceNoBionicPageSizeMacro: boolPtr(false),
Colin Crossface4e42017-10-30 17:32:15 -0700616
Colin Crossa74ca042019-01-31 14:31:51 -0800617 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700618 AAPTPreferredConfig: stringPtr("xhdpi"),
619 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800620 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700621
Christopher Ferrisa5a6b9c2024-04-23 17:17:49 -0700622 Malloc_low_memory: boolPtr(false),
Steven Morelandd1342012020-07-30 20:38:49 +0000623 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700624 Malloc_pattern_fill_contents: boolPtr(false),
625 Safestack: boolPtr(false),
Dennis Shene2ed70c2023-01-11 14:15:43 +0000626 TrimmedApex: boolPtr(false),
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000627 Build_from_text_stub: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100628
satayevd604b212021-07-21 14:23:52 +0100629 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
630 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700631 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800632
633 if runtime.GOOS == "linux" {
634 v.CrossHost = stringPtr("windows")
635 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800636 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800637 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700638}
639
Joe Onorato3fefc232023-12-04 17:35:15 +0000640func (this *ProductVariables) GetBuildFlagBool(flag string) bool {
641 val, ok := this.BuildFlags[flag]
642 if !ok {
643 return false
644 }
645 return val == "true"
646}
647
Colin Crosseabaedd2020-02-06 17:01:55 -0800648func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700649 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700650 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700651 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700652 return
653 }
654
655 // TODO: depend on config variable, create variants, propagate variants up tree
656 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700657
658 if a.variableProperties == nil {
659 return
660 }
661
662 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700663
Colin Cross0cec3122021-01-20 11:27:32 -0800664 productVariables := reflect.ValueOf(mctx.Config().productVariables)
665
Colin Cross7f64b6d2015-07-09 13:57:48 -0700666 for i := 0; i < variableValues.NumField(); i++ {
667 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700668 name := variableValues.Type().Field(i).Name
669 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700670
Colin Crossa6bc19e2015-09-16 13:53:42 -0700671 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -0800672 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700673 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
674 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700675 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700676
677 val = val.Elem()
678
679 // For bools, check that the value is true
680 if val.Kind() == reflect.Bool && val.Bool() == false {
681 continue
682 }
683
684 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -0800685 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700686 continue
687 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700688 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700689 }
690}
691
Colin Cross4157e882019-06-06 16:57:04 -0700692func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700693 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
694
Colin Cross7e0eaf12017-05-05 16:16:24 -0700695 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700696
Usta851a3272022-01-05 23:42:33 -0500697 err := proptools.AppendMatchingProperties(m.GetProperties(),
Colin Cross06a931b2015-10-28 17:23:31 -0700698 productVariablePropertyValue.Addr().Interface(), nil)
699 if err != nil {
700 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
701 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
702 } else {
703 panic(err)
704 }
705 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700706}
707
Colin Cross7e0eaf12017-05-05 16:16:24 -0700708func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
709 productVariablePropertyValue reflect.Value, i int, err error) {
710
711 field := productVariablePropertyValue.Type().Field(i).Name
712 property := prefix + "." + proptools.PropertyNameForField(field)
713 ctx.PropertyErrorf(property, "%s", err)
714}
715
716func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
717 productVariablePropertyValue reflect.Value, variableValue interface{}) {
718
Colin Cross7f64b6d2015-07-09 13:57:48 -0700719 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
720 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800721 kind := propertyValue.Kind()
722 if kind == reflect.Ptr {
723 if propertyValue.IsNil() {
724 continue
725 }
726 propertyValue = propertyValue.Elem()
727 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700728 switch propertyValue.Kind() {
729 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700730 err := printfIntoProperty(propertyValue, variableValue)
731 if err != nil {
732 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
733 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700734 case reflect.Slice:
735 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700736 err := printfIntoProperty(propertyValue.Index(j), variableValue)
737 if err != nil {
738 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
739 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700740 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800741 case reflect.Bool:
742 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700743 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700744 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700745 default:
746 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
747 }
748 }
749}
750
Colin Cross7e0eaf12017-05-05 16:16:24 -0700751func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700752 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700753
754 count := strings.Count(s, "%")
755 if count == 0 {
756 return nil
757 }
758
759 if count > 1 {
760 return fmt.Errorf("product variable properties only support a single '%%'")
761 }
762
Colin Cross7f64b6d2015-07-09 13:57:48 -0700763 if strings.Contains(s, "%d") {
764 switch v := variableValue.(type) {
765 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700766 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700767 case bool:
768 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700769 variableValue = 1
770 } else {
771 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700772 }
773 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700774 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700775 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700776 } else if strings.Contains(s, "%s") {
777 switch variableValue.(type) {
778 case string:
779 // Nothing
780 default:
781 return fmt.Errorf("unsupported type %T for %%s", variableValue)
782 }
783 } else {
784 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700785 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700786
787 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
788
789 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700790}
Colin Cross18c46802019-09-24 22:19:02 -0700791
792var variablePropTypeMap OncePer
793
794// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
795// reflect.Types of each property struct. The result can be used as a key in a map.
796func sliceToTypeArray(s []interface{}) interface{} {
797 // Create an array using reflection whose length is the length of the input slice
798 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
799 for i, e := range s {
800 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
801 }
802 return ret.Interface()
803}
804
Colin Crosseabaedd2020-02-06 17:01:55 -0800805func initProductVariableModule(m Module) {
806 base := m.base()
807
808 // Allow tests to override the default product variables
809 if base.variableProperties == nil {
810 base.variableProperties = defaultProductVariables
811 }
812 // Filter the product variables properties to the ones that exist on this module
813 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
814 if base.variableProperties != nil {
815 m.AddProperties(base.variableProperties)
816 }
817}
818
Colin Cross18c46802019-09-24 22:19:02 -0700819// createVariableProperties takes the list of property structs for a module and returns a property struct that
820// contains the product variable properties that exist in the property structs, or nil if there are none. It
821// caches the result.
822func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
823 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
824 key := sliceToTypeArray(moduleTypeProps)
825
826 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
827 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
828 // Compute the filtered property struct type.
829 return createVariablePropertiesType(moduleTypeProps, productVariables)
830 }).(reflect.Type)
831
832 if typ == nil {
833 return nil
834 }
835
836 // Create a new pointer to a filtered property struct.
837 return reflect.New(typ).Interface()
838}
839
840// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
841// a list of property structs.
842func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
843 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
844 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
845 // Filter function, returns true if the field should be in the resulting struct
846 if prefix == "" {
847 // Keep the top level Product_variables field
848 return true, field
849 }
850 _, rest := splitPrefix(prefix)
851 if rest == "" {
852 // Keep the 2nd level field (i.e. Product_variables.Eng)
853 return true, field
854 }
855
856 // Strip off the first 2 levels of the prefix
857 _, prefix = splitPrefix(rest)
858
859 for _, p := range moduleTypeProps {
860 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
861 // Keep any fields that exist in one of the property structs
862 return true, field
863 }
864 }
865
866 return false, field
867 })
868 return typ
869}
870
871func splitPrefix(prefix string) (first, rest string) {
872 index := strings.IndexByte(prefix, '.')
873 if index == -1 {
874 return prefix, ""
875 }
876 return prefix[:index], prefix[index+1:]
877}
878
879func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
880 if t.Kind() != reflect.Struct {
881 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
882 }
883
884 if prefix != "" {
885 split := strings.SplitN(prefix, ".", 2)
886 firstPrefix := split[0]
887 rest := ""
888 if len(split) > 1 {
889 rest = split[1]
890 }
891 f, exists := t.FieldByName(firstPrefix)
892 if !exists {
893 return false
894 }
895 ft := f.Type
896 if ft.Kind() == reflect.Ptr {
897 ft = ft.Elem()
898 }
899 if ft.Kind() != reflect.Struct {
900 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
901 }
902 return fieldExistsByNameRecursive(ft, rest, name)
903 } else {
904 _, exists := t.FieldByName(name)
905 return exists
906 }
907}