blob: 309824fc50fd0666ae8b0cb4bf0bd2515ebb5f86 [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
Colin Cross83163192015-12-01 16:31:16 -080058 // unbundled_build is a catch-all property to annotate modules that don't build in one or
59 // more unbundled branches, usually due to dependencies missing from the manifest.
60 Unbundled_build struct {
Cole Fausta963b942024-04-11 17:43:00 -070061 Enabled proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Colin Cross83163192015-12-01 16:31:16 -080062 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080063
Jeongik Chaf08e59c2023-02-09 10:47:59 +090064 // similar to `Unbundled_build`, but `Always_use_prebuilt_sdks` means that it uses prebuilt
65 // sdk specifically.
66 Always_use_prebuilt_sdks struct {
Cole Fausta963b942024-04-11 17:43:00 -070067 Enabled proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Jeongik Chaf08e59c2023-02-09 10:47:59 +090068 } `android:"arch_variant"`
69
Dan Willemsen97cae012016-03-01 14:08:42 -080070 Malloc_not_svelte struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080071 Cflags []string `android:"arch_variant"`
72 Shared_libs []string `android:"arch_variant"`
73 Whole_static_libs []string `android:"arch_variant"`
Florian Mayerca2854e2024-01-16 16:42:34 -080074 Static_libs []string `android:"arch_variant"`
Christopher Ferrisc71193a2019-12-16 09:55:10 -080075 Exclude_static_libs []string `android:"arch_variant"`
Christopher Ferrisdea66172021-09-24 00:07:53 -070076 Srcs []string `android:"arch_variant"`
77 Header_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070078 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070079
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070080 Malloc_zero_contents struct {
81 Cflags []string `android:"arch_variant"`
82 } `android:"arch_variant"`
83
84 Malloc_pattern_fill_contents struct {
85 Cflags []string `android:"arch_variant"`
86 } `android:"arch_variant"`
87
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070088 Safestack struct {
89 Cflags []string `android:"arch_variant"`
90 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070091
Dan Willemsen1be35382016-07-25 17:42:18 -070092 Binder32bit struct {
93 Cflags []string
94 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070095
Colin Cross95436422017-05-04 13:57:05 -070096 Override_rs_driver struct {
97 Cflags []string
98 }
99
Steven Morelandc2b9f062017-12-13 14:06:20 -0800100 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
101 // enabled.
102 Treble_linker_namespaces struct {
103 Cflags []string
104 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800105 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
106 Enforce_vintf_manifest struct {
107 Cflags []string
108 }
109
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000110 Build_from_text_stub struct {
111 Static_libs []string
112 Exclude_static_libs []string
113 }
114
Colin Cross6bc59ef2016-12-08 09:46:35 -0800115 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
116 // debugging features that don't significantly impact runtime behavior. userdebug builds
117 // are used for dogfooding and performance testing, and should be as similar to user builds
118 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700119 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700120 Cflags []string
121 Cppflags []string
122 Init_rc []string
123 Required []string
124 Host_required []string
125 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700126 Strip struct {
127 All *bool
128 Keep_symbols *bool
129 Keep_symbols_and_debug_frame *bool
130 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700131 Static_libs []string
132 Whole_static_libs []string
133 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900134
135 Cmdline []string
Inseob Kimf5ed3002022-01-07 19:17:29 +0900136
Jay Aliomer2599d1d2021-09-23 15:49:54 -0400137 Srcs []string
138 Exclude_srcs []string
Cole Fausted940002023-08-15 11:59:24 -0700139 Cmd *string
Jiyong Park62532d72024-04-12 16:16:28 +0900140
141 Deps []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700142 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800143
Usta Shresthac725f472022-01-11 02:44:21 -0500144 // eng is true for -eng builds, and can be used to turn on additional heavyweight debugging
Colin Cross6bc59ef2016-12-08 09:46:35 -0800145 // features.
146 Eng struct {
147 Cflags []string
148 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700149 Lto struct {
150 Never *bool
151 }
Colin Cross3273cc22018-10-26 23:58:42 -0700152 Sanitize struct {
153 Address *bool
154 }
Felka Changf13642e2019-06-21 20:58:27 +0800155 Optimize struct {
156 Enabled *bool
157 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800158 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700159
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700160 Uml struct {
161 Cppflags []string
162 }
Risanea004dd2017-11-27 18:14:46 +0900163
164 Arc struct {
Satoshi Niwa54f48262020-12-18 09:53:26 +0900165 Cflags []string `android:"arch_variant"`
166 Exclude_srcs []string `android:"arch_variant"`
Jasmine Chenacace422021-03-05 00:56:40 +0800167 Header_libs []string `android:"arch_variant"`
Satoshi Niwa54f48262020-12-18 09:53:26 +0900168 Include_dirs []string `android:"arch_variant"`
169 Shared_libs []string `android:"arch_variant"`
170 Static_libs []string `android:"arch_variant"`
171 Srcs []string `android:"arch_variant"`
172 Whole_static_libs []string `android:"arch_variant"`
173 } `android:"arch_variant"`
Roland Levillain2879d412019-08-13 15:00:18 +0100174
Colin Crossd9a121b2020-01-27 13:26:42 -0800175 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800176 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800177 Srcs []string `android:"arch_variant"`
178 Exclude_srcs []string `android:"arch_variant"`
179 } `android:"arch_variant"`
Devin Moorec381e102023-07-06 22:03:58 +0000180
181 // release_aidl_use_unfrozen is "true" when a device can
182 // use the unfrozen versions of AIDL interfaces.
183 Release_aidl_use_unfrozen struct {
184 Cflags []string
185 Cmd *string
186 }
Colin Cross83163192015-12-01 16:31:16 -0800187 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700188}
189
Colin Cross6961a492020-02-06 16:57:45 -0800190var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700191
Cole Faustf8231dd2023-04-21 17:37:11 -0700192type ProductVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700193 // Suffix to add to generated Makefiles
194 Make_suffix *string `json:",omitempty"`
195
Inseob Kim8fa54da2024-03-19 16:48:59 +0900196 BuildId *string `json:",omitempty"`
197 BuildNumberFile *string `json:",omitempty"`
198 BuildHostnameFile *string `json:",omitempty"`
199 BuildThumbprintFile *string `json:",omitempty"`
200 DisplayBuildNumber *bool `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800201
Linus Tufvessonba270c52024-04-12 13:06:22 +0200202 Platform_display_version_name *string `json:",omitempty"`
203 Platform_version_name *string `json:",omitempty"`
204 Platform_sdk_version *int `json:",omitempty"`
205 Platform_sdk_codename *string `json:",omitempty"`
206 Platform_sdk_version_or_codename *string `json:",omitempty"`
207 Platform_sdk_final *bool `json:",omitempty"`
208 Platform_sdk_extension_version *int `json:",omitempty"`
209 Platform_base_sdk_extension_version *int `json:",omitempty"`
210 Platform_version_active_codenames []string `json:",omitempty"`
211 Platform_version_all_preview_codenames []string `json:",omitempty"`
212 Platform_systemsdk_versions []string `json:",omitempty"`
213 Platform_security_patch *string `json:",omitempty"`
214 Platform_preview_sdk_version *string `json:",omitempty"`
215 Platform_base_os *string `json:",omitempty"`
216 Platform_version_last_stable *string `json:",omitempty"`
217 Platform_version_known_codenames *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700218
Jeongik Cha219141c2020-08-06 23:00:37 +0900219 DeviceName *string `json:",omitempty"`
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000220 DeviceProduct *string `json:",omitempty"`
Jeongik Cha219141c2020-08-06 23:00:37 +0900221 DeviceArch *string `json:",omitempty"`
222 DeviceArchVariant *string `json:",omitempty"`
223 DeviceCpuVariant *string `json:",omitempty"`
224 DeviceAbi []string `json:",omitempty"`
225 DeviceVndkVersion *string `json:",omitempty"`
226 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
227 DeviceSystemSdkVersions []string `json:",omitempty"`
Juan Yescas05d4d902023-04-07 10:35:35 -0700228 DeviceMaxPageSizeSupported *string `json:",omitempty"`
Vilas Bhatb3d2d222023-12-04 22:51:20 +0000229 DeviceNoBionicPageSizeMacro *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700230
Justin Yun41cbb5e2023-11-29 17:58:16 +0900231 VendorApiLevel *string `json:",omitempty"`
232
Jose Galmes6f843bc2020-12-11 13:36:29 -0800233 RecoverySnapshotVersion *string `json:",omitempty"`
234
Colin Crossa74ca042019-01-31 14:31:51 -0800235 DeviceSecondaryArch *string `json:",omitempty"`
236 DeviceSecondaryArchVariant *string `json:",omitempty"`
237 DeviceSecondaryCpuVariant *string `json:",omitempty"`
238 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700239
dimitry8d6dde82019-07-11 10:23:53 +0200240 NativeBridgeArch *string `json:",omitempty"`
241 NativeBridgeArchVariant *string `json:",omitempty"`
242 NativeBridgeCpuVariant *string `json:",omitempty"`
243 NativeBridgeAbi []string `json:",omitempty"`
244 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100245
dimitry8d6dde82019-07-11 10:23:53 +0200246 NativeBridgeSecondaryArch *string `json:",omitempty"`
247 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
248 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
249 NativeBridgeSecondaryAbi []string `json:",omitempty"`
250 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100251
Colin Cross4225f652015-09-17 14:33:42 -0700252 HostArch *string `json:",omitempty"`
253 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000254 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800255
256 CrossHost *string `json:",omitempty"`
257 CrossHostArch *string `json:",omitempty"`
258 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800259
Jeongik Chacee5ba92021-02-19 12:11:51 +0900260 DeviceResourceOverlays []string `json:",omitempty"`
261 ProductResourceOverlays []string `json:",omitempty"`
262 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800263 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700264
Colin Crossa74ca042019-01-31 14:31:51 -0800265 AAPTCharacteristics *string `json:",omitempty"`
266 AAPTConfig []string `json:",omitempty"`
267 AAPTPreferredConfig *string `json:",omitempty"`
268 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700269
Inseob Kim80fa7982022-08-12 21:36:25 +0900270 DefaultAppCertificate *string `json:",omitempty"`
271 MainlineSepolicyDevCertificates *string `json:",omitempty"`
Colin Cross61ae0b72017-12-01 17:16:02 -0800272
Colin Crossface4e42017-10-30 17:32:15 -0700273 AppsDefaultVersionName *string `json:",omitempty"`
274
Cole Faust701ca252021-11-23 19:02:08 -0800275 Allow_missing_dependencies *bool `json:",omitempty"`
276 Unbundled_build *bool `json:",omitempty"`
277 Unbundled_build_apps []string `json:",omitempty"`
278 Unbundled_build_image *bool `json:",omitempty"`
279 Always_use_prebuilt_sdks *bool `json:",omitempty"`
280 Skip_boot_jars_check *bool `json:",omitempty"`
281 Malloc_not_svelte *bool `json:",omitempty"`
282 Malloc_zero_contents *bool `json:",omitempty"`
283 Malloc_pattern_fill_contents *bool `json:",omitempty"`
284 Safestack *bool `json:",omitempty"`
285 HostStaticBinaries *bool `json:",omitempty"`
286 Binder32bit *bool `json:",omitempty"`
287 UseGoma *bool `json:",omitempty"`
288 UseRBE *bool `json:",omitempty"`
289 UseRBEJAVAC *bool `json:",omitempty"`
290 UseRBER8 *bool `json:",omitempty"`
291 UseRBED8 *bool `json:",omitempty"`
292 Debuggable *bool `json:",omitempty"`
293 Eng *bool `json:",omitempty"`
294 Treble_linker_namespaces *bool `json:",omitempty"`
295 Enforce_vintf_manifest *bool `json:",omitempty"`
296 Uml *bool `json:",omitempty"`
297 Arc *bool `json:",omitempty"`
298 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000299 Build_from_text_stub *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800300
Inseob Kim8fa54da2024-03-19 16:48:59 +0900301 BuildType *string `json:",omitempty"`
302
Logan Chien4fcea3d2018-11-20 11:59:08 +0800303 Check_elf_files *bool `json:",omitempty"`
304
Colin Cross5a0dcd52018-10-05 14:20:06 -0700305 UncompressPrivAppDex *bool `json:",omitempty"`
306 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800307
satayevd604b212021-07-21 14:23:52 +0100308 BootJars ConfiguredJarList `json:",omitempty"`
309 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000310
Colin Crossa74ca042019-01-31 14:31:51 -0800311 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700312
Colin Crossa74ca042019-01-31 14:31:51 -0800313 EnableCFI *bool `json:",omitempty"`
314 CFIExcludePaths []string `json:",omitempty"`
315 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700316
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800317 DisableScudo *bool `json:",omitempty"`
318
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800319 MemtagHeapExcludePaths []string `json:",omitempty"`
320 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
321 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
322
Hang Lua98aab92023-03-17 13:17:22 +0800323 HWASanIncludePaths []string `json:",omitempty"`
Tomislav Novakf734f002023-08-22 10:51:44 -0700324 HWASanExcludePaths []string `json:",omitempty"`
Hang Lua98aab92023-03-17 13:17:22 +0800325
Justin Yund5f6c822019-06-25 16:47:17 +0900326 VendorPath *string `json:",omitempty"`
327 OdmPath *string `json:",omitempty"`
328 ProductPath *string `json:",omitempty"`
329 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800330
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700331 ClangTidy *bool `json:",omitempty"`
332 TidyChecks *string `json:",omitempty"`
333
Roland Levillainada12702020-06-09 13:07:36 +0100334 JavaCoveragePaths []string `json:",omitempty"`
335 JavaCoverageExcludePaths []string `json:",omitempty"`
336
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800337 GcovCoverage *bool `json:",omitempty"`
338 ClangCoverage *bool `json:",omitempty"`
339 NativeCoveragePaths []string `json:",omitempty"`
340 NativeCoverageExcludePaths []string `json:",omitempty"`
341 ClangCoverageContinuousMode *bool `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800342
Colin Cross1a6acd42020-06-16 17:51:46 -0700343 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400344 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700345
Colin Cross23ae82a2016-11-02 14:34:39 -0700346 SanitizeHost []string `json:",omitempty"`
347 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700348 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700349 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800350
351 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800352
353 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700354
355 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900356
357 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900358
359 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800360
361 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800362
Vinh Tran7a8362c2022-11-01 15:33:51 -0400363 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700364
Inseob Kimae553032019-05-14 18:52:49 +0900365 VndkUseCoreVariant *bool `json:",omitempty"`
366 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800367
Inseob Kim7cf14652021-01-06 23:06:52 +0900368 DirectedVendorSnapshot bool `json:",omitempty"`
369 VendorSnapshotModules map[string]bool `json:",omitempty"`
370
Jose Galmes4c6895e2021-02-09 07:44:30 -0800371 DirectedRecoverySnapshot bool `json:",omitempty"`
372 RecoverySnapshotModules map[string]bool `json:",omitempty"`
373
Justin DeMartino383bfb32021-02-24 10:49:43 -0800374 VendorSnapshotDirsIncluded []string `json:",omitempty"`
375 VendorSnapshotDirsExcluded []string `json:",omitempty"`
376 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
377 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000378 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800379
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900380 MultitreeUpdateMeta bool `json:",omitempty"`
381
Inseob Kim6077b232023-08-31 16:49:59 +0900382 BoardVendorSepolicyDirs []string `json:",omitempty"`
383 BoardOdmSepolicyDirs []string `json:",omitempty"`
384 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
385 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
386 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700387
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900388 BoardSepolicyVers *string `json:",omitempty"`
389 PlatformSepolicyVersion *string `json:",omitempty"`
390
Inseob Kim1a0afcc2022-02-14 23:10:51 +0900391 SystemExtSepolicyPrebuiltApiDir *string `json:",omitempty"`
392 ProductSepolicyPrebuiltApiDir *string `json:",omitempty"`
393
Inseob Kim843f6642022-01-07 09:11:23 +0900394 PlatformSepolicyCompatVersions []string `json:",omitempty"`
395
Dan Willemsen0fe78662018-03-26 12:41:18 -0700396 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700397
Dan Albertd05ba002021-04-13 15:55:47 -0700398 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900399
Dennis Shene2ed70c2023-01-11 14:15:43 +0000400 TrimmedApex *bool `json:",omitempty"`
Jiyong Park4da07972021-01-05 21:01:11 +0900401 ForceApexSymlinkOptimization *bool `json:",omitempty"`
402 CompressedApex *bool `json:",omitempty"`
403 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800404
405 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900406
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900407 WithDexpreopt bool `json:",omitempty"`
408
Jiakai Zhang4d90da22023-07-12 16:51:48 +0100409 ManifestPackageNameOverrides []string `json:",omitempty"`
410 CertificateOverrides []string `json:",omitempty"`
411 PackageNameOverrides []string `json:",omitempty"`
412 ConfiguredJarLocationOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900413
Albert Martineefabcf2022-03-21 20:11:16 +0000414 ApexGlobalMinSdkVersionOverride *string `json:",omitempty"`
415
Jeongik Chac9464142019-01-07 12:07:27 +0900416 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700417 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800418
Colin Crossf24a22a2019-01-31 14:12:44 -0800419 ProductHiddenAPIStubs []string `json:",omitempty"`
420 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
421 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700422
Inseob Kim0866b002019-04-15 20:21:29 +0900423 ProductPublicSepolicyDirs []string `json:",omitempty"`
424 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900425
Dan Willemsen54879d12019-04-18 10:08:46 -0700426 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700427
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900428 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900429
JaeMan Parkff715562020-10-19 17:25:58 +0900430 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
431 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
432
Yifan Hong82db7352020-01-21 16:12:26 -0800433 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700434
Yifan Hong42bef8d2020-08-05 14:36:09 -0700435 BoardKernelBinaries []string `json:",omitempty"`
436 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700437
438 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800439
440 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900441
442 ShippingApiLevel *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900443
Cole Faustded79602023-09-05 17:48:11 -0700444 BuildBrokenPluginValidation []string `json:",omitempty"`
445 BuildBrokenClangAsFlags bool `json:",omitempty"`
446 BuildBrokenClangCFlags bool `json:",omitempty"`
447 BuildBrokenClangProperty bool `json:",omitempty"`
448 GenruleSandboxing *bool `json:",omitempty"`
449 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
450 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
451 BuildBrokenUsesSoongPython2Modules bool `json:",omitempty"`
452 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
453 BuildBrokenIncorrectPartitionImages bool `json:",omitempty"`
454 BuildBrokenInputDirModules []string `json:",omitempty"`
Jiyong Park3bb99242024-01-04 23:21:26 +0000455 BuildBrokenDontCheckSystemSdk bool `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900456
Jiakai Zhang48203e32023-06-02 23:42:21 +0100457 BuildWarningBadOptionalUsesLibsAllowlist []string `json:",omitempty"`
458
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700459 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
460
Inseob Kim67e5add192021-03-17 18:05:33 +0900461 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
462
463 SelinuxIgnoreNeverallows bool `json:",omitempty"`
464
Devin Moorec381e102023-07-06 22:03:58 +0000465 Release_aidl_use_unfrozen *bool `json:",omitempty"`
466
Inseob Kima10ef272021-09-15 03:04:53 +0000467 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
468 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900469
470 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Christopher Ferris98f10222022-07-13 23:16:52 -0700471
472 IgnorePrefer32OnDevice bool `json:",omitempty"`
Spandan Dasc5763832022-11-08 18:42:16 +0000473
Sam Delmerico98a73292023-02-21 11:50:29 -0500474 IncludeTags []string `json:",omitempty"`
475 SourceRootDirs []string `json:",omitempty"`
Vinh Tran44cb78c2023-03-09 22:07:19 -0500476
477 AfdoProfiles []string `json:",omitempty"`
Wei Lic642d682023-05-04 09:06:06 -0700478
Inseob Kim8fa54da2024-03-19 16:48:59 +0900479 ProductManufacturer string `json:",omitempty"`
480 ProductBrand string `json:",omitempty"`
Joe Onoratofee845a2023-05-09 08:14:14 -0700481
Yu Liueebb2592023-10-12 20:31:27 -0700482 ReleaseVersion string `json:",omitempty"`
483 ReleaseAconfigValueSets []string `json:",omitempty"`
Kiyoung Kime623c582023-07-06 16:43:44 +0900484
Zhi Dou3f65a412023-08-10 21:47:40 +0000485 ReleaseAconfigFlagDefaultPermission string `json:",omitempty"`
486
Alyssa Ketpreechasawat34ab8792023-10-06 07:01:22 +0000487 ReleaseDefaultModuleBuildFromSource *bool `json:",omitempty"`
488
Kiyoung Kime623c582023-07-06 16:43:44 +0900489 KeepVndk *bool `json:",omitempty"`
Inseob Kim5bac3b62023-08-25 21:29:46 +0900490
491 CheckVendorSeappViolations *bool `json:",omitempty"`
Cole Faustb4cb0c82023-09-14 15:16:58 -0700492
Inseob Kime4e85d52023-10-25 23:53:58 +0900493 BuildFlags map[string]string `json:",omitempty"`
Jihoon Kangc1b04d62023-11-16 19:07:04 +0000494
495 BuildFromSourceStub *bool `json:",omitempty"`
Spandan Das8ab28dd2024-02-17 03:31:45 +0000496
Spandan Das0d24ade2024-03-08 04:20:15 +0000497 BuildIgnoreApexContributionContents *bool `json:",omitempty"`
Jihoon Kangb36fc542024-02-22 19:35:26 +0000498
499 HiddenapiExportableStubs *bool `json:",omitempty"`
Jihoon Kang35349462024-02-22 19:52:46 +0000500
501 ExportRuntimeApis *bool `json:",omitempty"`
Yu Liu67a28422024-03-05 00:36:31 +0000502
503 AconfigContainerValidation string `json:",omitempty"`
Inseob Kim8fa54da2024-03-19 16:48:59 +0900504
505 ProductLocales []string `json:",omitempty"`
506
507 ProductDefaultWifiChannels []string `json:",omitempty"`
508
509 BoardUseVbmetaDigestInFingerprint *bool `json:",omitempty"`
510
511 OemProperties []string `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700512}
513
Cole Faust11edf552023-10-13 11:32:14 -0700514type PartitionQualifiedVariablesType struct {
515 BuildingImage bool `json:",omitempty"`
516 BoardErofsCompressor string `json:",omitempty"`
517 BoardErofsCompressHints string `json:",omitempty"`
518 BoardErofsPclusterSize string `json:",omitempty"`
519 BoardExtfsInodeCount string `json:",omitempty"`
520 BoardExtfsRsvPct string `json:",omitempty"`
521 BoardF2fsSloadCompressFlags string `json:",omitempty"`
522 BoardFileSystemCompress string `json:",omitempty"`
523 BoardFileSystemType string `json:",omitempty"`
524 BoardJournalSize string `json:",omitempty"`
525 BoardPartitionReservedSize string `json:",omitempty"`
526 BoardPartitionSize string `json:",omitempty"`
527 BoardSquashfsBlockSize string `json:",omitempty"`
528 BoardSquashfsCompressor string `json:",omitempty"`
529 BoardSquashfsCompressorOpt string `json:",omitempty"`
530 BoardSquashfsDisable4kAlign string `json:",omitempty"`
531 ProductBaseFsPath string `json:",omitempty"`
532 ProductHeadroom string `json:",omitempty"`
533 ProductVerityPartition string `json:",omitempty"`
534
535 BoardAvbAddHashtreeFooterArgs string `json:",omitempty"`
536 BoardAvbKeyPath string `json:",omitempty"`
537 BoardAvbAlgorithm string `json:",omitempty"`
538 BoardAvbRollbackIndex string `json:",omitempty"`
539 BoardAvbRollbackIndexLocation string `json:",omitempty"`
540}
541
Cole Faustcb193ec2023-09-20 16:01:18 -0700542type PartitionVariables struct {
543 ProductDirectory string `json:",omitempty"`
Cole Faust11edf552023-10-13 11:32:14 -0700544 PartitionQualifiedVariables map[string]PartitionQualifiedVariablesType
545 TargetUserimagesUseExt2 bool `json:",omitempty"`
546 TargetUserimagesUseExt3 bool `json:",omitempty"`
547 TargetUserimagesUseExt4 bool `json:",omitempty"`
Cole Faustcb193ec2023-09-20 16:01:18 -0700548
549 TargetUserimagesSparseExtDisabled bool `json:",omitempty"`
550 TargetUserimagesSparseErofsDisabled bool `json:",omitempty"`
551 TargetUserimagesSparseSquashfsDisabled bool `json:",omitempty"`
552 TargetUserimagesSparseF2fsDisabled bool `json:",omitempty"`
553
Yi-Yo Chiang939fe1a2023-11-24 14:58:50 +0800554 BoardErofsCompressor string `json:",omitempty"`
555 BoardErofsCompressorHints string `json:",omitempty"`
556 BoardErofsPclusterSize string `json:",omitempty"`
557 BoardErofsShareDupBlocks string `json:",omitempty"`
558 BoardErofsUseLegacyCompression string `json:",omitempty"`
559 BoardExt4ShareDupBlocks string `json:",omitempty"`
560 BoardFlashLogicalBlockSize string `json:",omitempty"`
561 BoardFlashEraseBlockSize string `json:",omitempty"`
562 BoardUsesRecoveryAsBoot bool `json:",omitempty"`
563 ProductUseDynamicPartitionSize bool `json:",omitempty"`
564 CopyImagesForTargetFilesZip bool `json:",omitempty"`
Cole Faustb5055392023-09-27 13:44:40 -0700565
566 BoardAvbEnable bool `json:",omitempty"`
Cole Faust11edf552023-10-13 11:32:14 -0700567
568 ProductPackages []string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700569}
570
571func boolPtr(v bool) *bool {
572 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700573}
574
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700575func intPtr(v int) *int {
576 return &v
577}
578
Colin Cross4225f652015-09-17 14:33:42 -0700579func stringPtr(v string) *string {
580 return &v
581}
582
Cole Faustf8231dd2023-04-21 17:37:11 -0700583func (v *ProductVariables) SetDefaultConfig() {
584 *v = ProductVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800585 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700586
Dan Albert8c7a9942023-03-27 20:34:01 +0000587 Platform_version_name: stringPtr("S"),
588 Platform_base_sdk_extension_version: intPtr(30),
589 Platform_sdk_version: intPtr(30),
590 Platform_sdk_codename: stringPtr("S"),
591 Platform_sdk_final: boolPtr(false),
592 Platform_version_active_codenames: []string{"S"},
593 Platform_version_all_preview_codenames: []string{"S"},
Dan Willemsencbf9e822017-11-13 14:34:56 -0800594
Vilas Bhatb3d2d222023-12-04 22:51:20 +0000595 HostArch: stringPtr("x86_64"),
596 HostSecondaryArch: stringPtr("x86"),
597 DeviceName: stringPtr("generic_arm64"),
598 DeviceProduct: stringPtr("aosp_arm-eng"),
599 DeviceArch: stringPtr("arm64"),
600 DeviceArchVariant: stringPtr("armv8-a"),
601 DeviceCpuVariant: stringPtr("generic"),
602 DeviceAbi: []string{"arm64-v8a"},
603 DeviceSecondaryArch: stringPtr("arm"),
604 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
605 DeviceSecondaryCpuVariant: stringPtr("generic"),
606 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
607 DeviceMaxPageSizeSupported: stringPtr("4096"),
608 DeviceNoBionicPageSizeMacro: boolPtr(false),
Colin Crossface4e42017-10-30 17:32:15 -0700609
Colin Crossa74ca042019-01-31 14:31:51 -0800610 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700611 AAPTPreferredConfig: stringPtr("xhdpi"),
612 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800613 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700614
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700615 Malloc_not_svelte: boolPtr(true),
Steven Morelandd1342012020-07-30 20:38:49 +0000616 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700617 Malloc_pattern_fill_contents: boolPtr(false),
618 Safestack: boolPtr(false),
Dennis Shene2ed70c2023-01-11 14:15:43 +0000619 TrimmedApex: boolPtr(false),
Jihoon Kangcbcad7c2023-06-01 22:31:52 +0000620 Build_from_text_stub: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100621
satayevd604b212021-07-21 14:23:52 +0100622 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
623 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700624 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800625
626 if runtime.GOOS == "linux" {
627 v.CrossHost = stringPtr("windows")
628 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800629 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800630 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700631}
632
Joe Onorato3fefc232023-12-04 17:35:15 +0000633func (this *ProductVariables) GetBuildFlagBool(flag string) bool {
634 val, ok := this.BuildFlags[flag]
635 if !ok {
636 return false
637 }
638 return val == "true"
639}
640
Colin Crosseabaedd2020-02-06 17:01:55 -0800641func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700642 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700643 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700644 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700645 return
646 }
647
648 // TODO: depend on config variable, create variants, propagate variants up tree
649 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700650
651 if a.variableProperties == nil {
652 return
653 }
654
655 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700656
Colin Cross0cec3122021-01-20 11:27:32 -0800657 productVariables := reflect.ValueOf(mctx.Config().productVariables)
658
Colin Cross7f64b6d2015-07-09 13:57:48 -0700659 for i := 0; i < variableValues.NumField(); i++ {
660 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700661 name := variableValues.Type().Field(i).Name
662 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700663
Colin Crossa6bc19e2015-09-16 13:53:42 -0700664 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -0800665 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700666 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
667 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700668 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700669
670 val = val.Elem()
671
672 // For bools, check that the value is true
673 if val.Kind() == reflect.Bool && val.Bool() == false {
674 continue
675 }
676
677 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -0800678 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700679 continue
680 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700681 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700682 }
683}
684
Colin Cross4157e882019-06-06 16:57:04 -0700685func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700686 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
687
Colin Cross7e0eaf12017-05-05 16:16:24 -0700688 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700689
Usta851a3272022-01-05 23:42:33 -0500690 err := proptools.AppendMatchingProperties(m.GetProperties(),
Colin Cross06a931b2015-10-28 17:23:31 -0700691 productVariablePropertyValue.Addr().Interface(), nil)
692 if err != nil {
693 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
694 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
695 } else {
696 panic(err)
697 }
698 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700699}
700
Colin Cross7e0eaf12017-05-05 16:16:24 -0700701func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
702 productVariablePropertyValue reflect.Value, i int, err error) {
703
704 field := productVariablePropertyValue.Type().Field(i).Name
705 property := prefix + "." + proptools.PropertyNameForField(field)
706 ctx.PropertyErrorf(property, "%s", err)
707}
708
709func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
710 productVariablePropertyValue reflect.Value, variableValue interface{}) {
711
Colin Cross7f64b6d2015-07-09 13:57:48 -0700712 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
713 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800714 kind := propertyValue.Kind()
715 if kind == reflect.Ptr {
716 if propertyValue.IsNil() {
717 continue
718 }
719 propertyValue = propertyValue.Elem()
720 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700721 switch propertyValue.Kind() {
722 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700723 err := printfIntoProperty(propertyValue, variableValue)
724 if err != nil {
725 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
726 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700727 case reflect.Slice:
728 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700729 err := printfIntoProperty(propertyValue.Index(j), variableValue)
730 if err != nil {
731 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
732 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700733 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800734 case reflect.Bool:
735 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700736 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700737 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700738 default:
739 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
740 }
741 }
742}
743
Colin Cross7e0eaf12017-05-05 16:16:24 -0700744func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700745 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700746
747 count := strings.Count(s, "%")
748 if count == 0 {
749 return nil
750 }
751
752 if count > 1 {
753 return fmt.Errorf("product variable properties only support a single '%%'")
754 }
755
Colin Cross7f64b6d2015-07-09 13:57:48 -0700756 if strings.Contains(s, "%d") {
757 switch v := variableValue.(type) {
758 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700759 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700760 case bool:
761 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700762 variableValue = 1
763 } else {
764 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700765 }
766 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700767 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700768 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700769 } else if strings.Contains(s, "%s") {
770 switch variableValue.(type) {
771 case string:
772 // Nothing
773 default:
774 return fmt.Errorf("unsupported type %T for %%s", variableValue)
775 }
776 } else {
777 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700778 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700779
780 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
781
782 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700783}
Colin Cross18c46802019-09-24 22:19:02 -0700784
785var variablePropTypeMap OncePer
786
787// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
788// reflect.Types of each property struct. The result can be used as a key in a map.
789func sliceToTypeArray(s []interface{}) interface{} {
790 // Create an array using reflection whose length is the length of the input slice
791 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
792 for i, e := range s {
793 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
794 }
795 return ret.Interface()
796}
797
Colin Crosseabaedd2020-02-06 17:01:55 -0800798func initProductVariableModule(m Module) {
799 base := m.base()
800
801 // Allow tests to override the default product variables
802 if base.variableProperties == nil {
803 base.variableProperties = defaultProductVariables
804 }
805 // Filter the product variables properties to the ones that exist on this module
806 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
807 if base.variableProperties != nil {
808 m.AddProperties(base.variableProperties)
809 }
810}
811
Colin Cross18c46802019-09-24 22:19:02 -0700812// createVariableProperties takes the list of property structs for a module and returns a property struct that
813// contains the product variable properties that exist in the property structs, or nil if there are none. It
814// caches the result.
815func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
816 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
817 key := sliceToTypeArray(moduleTypeProps)
818
819 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
820 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
821 // Compute the filtered property struct type.
822 return createVariablePropertiesType(moduleTypeProps, productVariables)
823 }).(reflect.Type)
824
825 if typ == nil {
826 return nil
827 }
828
829 // Create a new pointer to a filtered property struct.
830 return reflect.New(typ).Interface()
831}
832
833// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
834// a list of property structs.
835func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
836 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
837 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
838 // Filter function, returns true if the field should be in the resulting struct
839 if prefix == "" {
840 // Keep the top level Product_variables field
841 return true, field
842 }
843 _, rest := splitPrefix(prefix)
844 if rest == "" {
845 // Keep the 2nd level field (i.e. Product_variables.Eng)
846 return true, field
847 }
848
849 // Strip off the first 2 levels of the prefix
850 _, prefix = splitPrefix(rest)
851
852 for _, p := range moduleTypeProps {
853 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
854 // Keep any fields that exist in one of the property structs
855 return true, field
856 }
857 }
858
859 return false, field
860 })
861 return typ
862}
863
864func splitPrefix(prefix string) (first, rest string) {
865 index := strings.IndexByte(prefix, '.')
866 if index == -1 {
867 return prefix, ""
868 }
869 return prefix[:index], prefix[index+1:]
870}
871
872func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
873 if t.Kind() != reflect.Struct {
874 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
875 }
876
877 if prefix != "" {
878 split := strings.SplitN(prefix, ".", 2)
879 firstPrefix := split[0]
880 rest := ""
881 if len(split) > 1 {
882 rest = split[1]
883 }
884 f, exists := t.FieldByName(firstPrefix)
885 if !exists {
886 return false
887 }
888 ft := f.Type
889 if ft.Kind() == reflect.Ptr {
890 ft = ft.Elem()
891 }
892 if ft.Kind() != reflect.Struct {
893 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
894 }
895 return fieldExistsByNameRecursive(ft, rest, name)
896 } else {
897 _, exists := t.FieldByName(name)
898 return exists
899 }
900}