blob: a40e377c9d781b57bc99d605340ebd1c36de2bfa [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
Simon Shields1a771b12017-08-23 12:05:33 +100023 "bliss/soong/android"
24
Colin Cross7f64b6d2015-07-09 13:57:48 -070025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
Colin Cross1e676be2016-10-12 14:38:15 -070029 PreDepsMutators(func(ctx RegisterMutatorsContext) {
30 ctx.BottomUp("variable", variableMutator).Parallel()
31 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070032}
33
34type variableProperties struct {
35 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070036 Platform_sdk_version struct {
37 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070038 Cflags []string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070039 }
Colin Cross83163192015-12-01 16:31:16 -080040
41 // unbundled_build is a catch-all property to annotate modules that don't build in one or
42 // more unbundled branches, usually due to dependencies missing from the manifest.
43 Unbundled_build struct {
44 Enabled *bool `android:"arch_variant"`
45 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080046
Dan Willemsen97cae012016-03-01 14:08:42 -080047 Malloc_not_svelte struct {
Christopher Ferris25bda7e2019-04-01 16:30:21 -070048 Cflags []string `android:"arch_variant"`
49 Shared_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070050 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070051
52 Safestack struct {
53 Cflags []string `android:"arch_variant"`
54 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070055
Dan Willemsen1be35382016-07-25 17:42:18 -070056 Binder32bit struct {
57 Cflags []string
58 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070059
Colin Cross95436422017-05-04 13:57:05 -070060 Override_rs_driver struct {
61 Cflags []string
62 }
63
Ari Hausman-Cohen7805d1d2018-06-21 11:11:03 -070064 // Product_is_iot is true for Android Things devices.
65 Product_is_iot struct {
Dan Willemsen377a12b2018-11-19 23:36:38 -080066 Cflags []string
67 Enabled bool
68 Exclude_srcs []string
69 Init_rc []string
70 Shared_libs []string
71 Srcs []string
72 Static_libs []string
Ari Hausman-Cohen7805d1d2018-06-21 11:11:03 -070073 }
74
Steven Morelandc2b9f062017-12-13 14:06:20 -080075 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
76 // enabled.
77 Treble_linker_namespaces struct {
78 Cflags []string
79 }
Steven Morelandc2b9f062017-12-13 14:06:20 -080080 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
81 Enforce_vintf_manifest struct {
82 Cflags []string
83 }
84
Colin Cross6bc59ef2016-12-08 09:46:35 -080085 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
86 // debugging features that don't significantly impact runtime behavior. userdebug builds
87 // are used for dogfooding and performance testing, and should be as similar to user builds
88 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -070089 Debuggable struct {
Dan Willemsen7b872832016-10-03 23:36:47 -070090 Cflags []string
91 Cppflags []string
Carmen Jackson6fe956b2017-04-14 14:38:47 -070092 Init_rc []string
Mark Salyzynaaf53532019-03-01 15:13:48 -080093 Required []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -070094 }
Colin Cross6bc59ef2016-12-08 09:46:35 -080095
96 // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging
97 // features.
98 Eng struct {
99 Cflags []string
100 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700101 Lto struct {
102 Never *bool
103 }
Colin Cross3273cc22018-10-26 23:58:42 -0700104 Sanitize struct {
105 Address *bool
106 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800107 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700108
109 Pdk struct {
Colin Cross99967a72017-10-17 13:55:02 -0700110 Enabled *bool `android:"arch_variant"`
111 } `android:"arch_variant"`
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700112
113 Uml struct {
114 Cppflags []string
115 }
Risanea004dd2017-11-27 18:14:46 +0900116
Rajeev Kumar423e2db2018-03-09 18:16:35 -0800117 Use_lmkd_stats_log struct {
118 Cflags []string
119 }
120
Risanea004dd2017-11-27 18:14:46 +0900121 Arc struct {
Hirokazu Honda89913352018-01-17 14:00:33 +0900122 Cflags []string
Risanea004dd2017-11-27 18:14:46 +0900123 Exclude_srcs []string
Hirokazu Honda89913352018-01-17 14:00:33 +0900124 Include_dirs []string
125 Shared_libs []string
Risanea004dd2017-11-27 18:14:46 +0900126 Static_libs []string
Hirokazu Honda89913352018-01-17 14:00:33 +0900127 Srcs []string
Risanea004dd2017-11-27 18:14:46 +0900128 }
Simon Shields1a771b12017-08-23 12:05:33 +1000129
130 // include Bliss variables
131 Bliss android.Product_variables
Colin Cross83163192015-12-01 16:31:16 -0800132 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700133}
134
135var zeroProductVariables variableProperties
136
Colin Cross485e5722015-08-27 13:28:01 -0700137type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700138 // Suffix to add to generated Makefiles
139 Make_suffix *string `json:",omitempty"`
140
Nan Zhang581fd212018-01-10 16:06:12 -0800141 BuildId *string `json:",omitempty"`
142 BuildNumberFromFile *string `json:",omitempty"`
143 DateFromFile *string `json:",omitempty"`
144
Colin Cross8e846482019-04-02 22:56:43 -0700145 Platform_version_name *string `json:",omitempty"`
146 Platform_sdk_version *int `json:",omitempty"`
147 Platform_sdk_codename *string `json:",omitempty"`
148 Platform_sdk_final *bool `json:",omitempty"`
149 Platform_version_active_codenames []string `json:",omitempty"`
150 Platform_version_future_codenames []string `json:",omitempty"`
151 Platform_vndk_version *string `json:",omitempty"`
152 Platform_systemsdk_versions []string `json:",omitempty"`
153 Platform_security_patch *string `json:",omitempty"`
154 Platform_preview_sdk_version *string `json:",omitempty"`
155 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
156 Platform_base_os *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700157
Colin Crossa74ca042019-01-31 14:31:51 -0800158 DeviceName *string `json:",omitempty"`
159 DeviceArch *string `json:",omitempty"`
160 DeviceArchVariant *string `json:",omitempty"`
161 DeviceCpuVariant *string `json:",omitempty"`
162 DeviceAbi []string `json:",omitempty"`
163 DeviceVndkVersion *string `json:",omitempty"`
164 DeviceSystemSdkVersions []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700165
Colin Crossa74ca042019-01-31 14:31:51 -0800166 DeviceSecondaryArch *string `json:",omitempty"`
167 DeviceSecondaryArchVariant *string `json:",omitempty"`
168 DeviceSecondaryCpuVariant *string `json:",omitempty"`
169 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700170
171 HostArch *string `json:",omitempty"`
172 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800173
174 CrossHost *string `json:",omitempty"`
175 CrossHostArch *string `json:",omitempty"`
176 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800177
Anton Hansson53c88442019-03-18 15:53:16 +0000178 DeviceResourceOverlays []string `json:",omitempty"`
179 ProductResourceOverlays []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800180 EnforceRROTargets []string `json:",omitempty"`
181 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700182
Colin Crossa74ca042019-01-31 14:31:51 -0800183 AAPTCharacteristics *string `json:",omitempty"`
184 AAPTConfig []string `json:",omitempty"`
185 AAPTPreferredConfig *string `json:",omitempty"`
186 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700187
Colin Cross61ae0b72017-12-01 17:16:02 -0800188 DefaultAppCertificate *string `json:",omitempty"`
189
Colin Crossface4e42017-10-30 17:32:15 -0700190 AppsDefaultVersionName *string `json:",omitempty"`
191
Colin Cross1f367bf2018-12-18 22:46:24 -0800192 Allow_missing_dependencies *bool `json:",omitempty"`
193 Unbundled_build *bool `json:",omitempty"`
194 Unbundled_build_sdks_from_source *bool `json:",omitempty"`
195 Malloc_not_svelte *bool `json:",omitempty"`
196 Safestack *bool `json:",omitempty"`
197 HostStaticBinaries *bool `json:",omitempty"`
198 Binder32bit *bool `json:",omitempty"`
199 UseGoma *bool `json:",omitempty"`
200 Debuggable *bool `json:",omitempty"`
201 Eng *bool `json:",omitempty"`
202 Treble_linker_namespaces *bool `json:",omitempty"`
203 Enforce_vintf_manifest *bool `json:",omitempty"`
204 Pdk *bool `json:",omitempty"`
205 Uml *bool `json:",omitempty"`
206 Use_lmkd_stats_log *bool `json:",omitempty"`
207 Arc *bool `json:",omitempty"`
208 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800209
Logan Chien4fcea3d2018-11-20 11:59:08 +0800210 Check_elf_files *bool `json:",omitempty"`
211
Colin Cross5a0dcd52018-10-05 14:20:06 -0700212 UncompressPrivAppDex *bool `json:",omitempty"`
213 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800214
Colin Cross9be41522019-02-20 10:40:13 -0800215 BootJars []string `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000216
Colin Crossa74ca042019-01-31 14:31:51 -0800217 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700218
Colin Crossa74ca042019-01-31 14:31:51 -0800219 EnableCFI *bool `json:",omitempty"`
220 CFIExcludePaths []string `json:",omitempty"`
221 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700222
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800223 DisableScudo *bool `json:",omitempty"`
224
Colin Crossa74ca042019-01-31 14:31:51 -0800225 EnableXOM *bool `json:",omitempty"`
226 XOMExcludePaths []string `json:",omitempty"`
Ivan Lozano074ec482018-11-21 08:59:37 -0800227
Dario Frenifd05a742018-05-29 13:28:54 +0100228 VendorPath *string `json:",omitempty"`
229 OdmPath *string `json:",omitempty"`
230 ProductPath *string `json:",omitempty"`
231 ProductServicesPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800232
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700233 ClangTidy *bool `json:",omitempty"`
234 TidyChecks *string `json:",omitempty"`
235
Colin Crossa74ca042019-01-31 14:31:51 -0800236 NativeCoverage *bool `json:",omitempty"`
237 CoveragePaths []string `json:",omitempty"`
238 CoverageExcludePaths []string `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800239
Colin Cross88da24e2018-10-01 15:30:23 -0700240 DevicePrefer32BitApps *bool `json:",omitempty"`
Colin Cross1e7d3702016-08-24 15:25:47 -0700241 DevicePrefer32BitExecutables *bool `json:",omitempty"`
242 HostPrefer32BitExecutables *bool `json:",omitempty"`
243
Colin Cross23ae82a2016-11-02 14:34:39 -0700244 SanitizeHost []string `json:",omitempty"`
245 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700246 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700247 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800248
249 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800250
251 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700252
253 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900254
Ari Hausman-Cohen7805d1d2018-06-21 11:11:03 -0700255 Product_is_iot *bool `json:",omitempty"`
256
Doug Horn21b94272019-01-16 12:06:11 -0800257 Fuchsia *bool `json:",omitempty"`
258
Jiyong Parkd773eb32017-07-03 13:18:12 +0900259 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900260
261 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800262
263 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800264
265 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700266
Vic Yangefd249e2018-11-12 20:19:56 -0800267 VndkUseCoreVariant *bool `json:",omitempty"`
268
Tri Vof544fe32018-05-20 14:34:48 -0700269 BoardVendorSepolicyDirs []string `json:",omitempty"`
270 BoardOdmSepolicyDirs []string `json:",omitempty"`
271 BoardPlatPublicSepolicyDirs []string `json:",omitempty"`
272 BoardPlatPrivateSepolicyDirs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700273
Dan Willemsen0fe78662018-03-26 12:41:18 -0700274 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700275
Dan Albert23d37e02018-11-28 08:30:10 -0800276 Ndk_abis *bool `json:",omitempty"`
277 Exclude_draft_ndk_apis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900278
279 FlattenApex *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800280
281 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900282
283 ManifestPackageNameOverrides []string `json:",omitempty"`
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800284 CertificateOverrides []string `json:",omitempty"`
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800285 PackageNameOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900286
287 EnforceSystemCertificate *bool `json:",omitempty"`
288 EnforceSystemCertificateWhitelist []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800289
Colin Crossf24a22a2019-01-31 14:12:44 -0800290 ProductHiddenAPIStubs []string `json:",omitempty"`
291 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
292 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen898efa12019-04-10 12:27:35 -0700293
Dan Willemsen6128b852019-04-18 10:08:46 -0700294 TargetFSConfigGen []string `json:",omitempty"`
Simon Shields1a771b12017-08-23 12:05:33 +1000295
296 Bliss android.ProductVariables
Colin Crossa6bc19e2015-09-16 13:53:42 -0700297}
298
299func boolPtr(v bool) *bool {
300 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700301}
302
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700303func intPtr(v int) *int {
304 return &v
305}
306
Colin Cross4225f652015-09-17 14:33:42 -0700307func stringPtr(v string) *string {
308 return &v
309}
310
Colin Cross27385972015-09-18 10:57:10 -0700311func (v *productVariables) SetDefaultConfig() {
312 *v = productVariables{
Dan Willemsencbf9e822017-11-13 14:34:56 -0800313 Platform_sdk_version: intPtr(26),
314 Platform_version_active_codenames: []string{"P"},
315 Platform_version_future_codenames: []string{"P"},
316
Colin Cross4225f652015-09-17 14:33:42 -0700317 HostArch: stringPtr("x86_64"),
318 HostSecondaryArch: stringPtr("x86"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800319 DeviceName: stringPtr("generic_arm64"),
Colin Cross4225f652015-09-17 14:33:42 -0700320 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700321 DeviceArchVariant: stringPtr("armv8-a"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800322 DeviceCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800323 DeviceAbi: []string{"arm64-v8a"},
Colin Cross4225f652015-09-17 14:33:42 -0700324 DeviceSecondaryArch: stringPtr("arm"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800325 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
326 DeviceSecondaryCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800327 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
Colin Crossface4e42017-10-30 17:32:15 -0700328
Colin Crossa74ca042019-01-31 14:31:51 -0800329 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700330 AAPTPreferredConfig: stringPtr("xhdpi"),
331 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800332 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700333
Dan Willemsencbf9e822017-11-13 14:34:56 -0800334 Malloc_not_svelte: boolPtr(true),
Colin Crossface4e42017-10-30 17:32:15 -0700335 Safestack: boolPtr(false),
Colin Cross485e5722015-08-27 13:28:01 -0700336 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800337
338 if runtime.GOOS == "linux" {
339 v.CrossHost = stringPtr("windows")
340 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800341 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800342 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700343}
344
Colin Cross635c3b02016-05-18 15:37:25 -0700345func variableMutator(mctx BottomUpMutatorContext) {
346 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700347 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700348 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700349 return
350 }
351
352 // TODO: depend on config variable, create variants, propagate variants up tree
353 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700354 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700355 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
Simon Shields947ce4a2017-09-04 21:09:10 +1000356 valStruct := reflect.ValueOf(mctx.Config().productVariables)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700357
Simon Shields947ce4a2017-09-04 21:09:10 +1000358 doVariableMutation(mctx, a, variableValues, zeroValues, valStruct)
359
360}
361
362func doVariableMutation(mctx BottomUpMutatorContext, a *ModuleBase, variableValues reflect.Value, zeroValues reflect.Value,
363 valStruct reflect.Value) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700364 for i := 0; i < variableValues.NumField(); i++ {
365 variableValue := variableValues.Field(i)
366 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700367 name := variableValues.Type().Field(i).Name
368 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700369
Colin Crossa6bc19e2015-09-16 13:53:42 -0700370 // Check that the variable was set for the product
Simon Shields947ce4a2017-09-04 21:09:10 +1000371 val := valStruct.FieldByName(name)
372 if val.IsValid() && val.Kind() == reflect.Struct {
373 doVariableMutation(mctx, a, variableValue, zeroValue, val)
374 continue
375 } else if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700376 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700377 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700378
379 val = val.Elem()
380
381 // For bools, check that the value is true
382 if val.Kind() == reflect.Bool && val.Bool() == false {
383 continue
384 }
385
386 // Check if any properties were set for the module
387 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
388 continue
389 }
390
391 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700392 }
393}
394
Colin Cross635c3b02016-05-18 15:37:25 -0700395func (a *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700396 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
397
Colin Cross7e0eaf12017-05-05 16:16:24 -0700398 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700399
Colin Cross06a931b2015-10-28 17:23:31 -0700400 err := proptools.AppendMatchingProperties(a.generalProperties,
401 productVariablePropertyValue.Addr().Interface(), nil)
402 if err != nil {
403 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
404 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
405 } else {
406 panic(err)
407 }
408 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700409}
410
Colin Cross7e0eaf12017-05-05 16:16:24 -0700411func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
412 productVariablePropertyValue reflect.Value, i int, err error) {
413
414 field := productVariablePropertyValue.Type().Field(i).Name
415 property := prefix + "." + proptools.PropertyNameForField(field)
416 ctx.PropertyErrorf(property, "%s", err)
417}
418
419func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
420 productVariablePropertyValue reflect.Value, variableValue interface{}) {
421
Colin Cross7f64b6d2015-07-09 13:57:48 -0700422 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
423 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800424 kind := propertyValue.Kind()
425 if kind == reflect.Ptr {
426 if propertyValue.IsNil() {
427 continue
428 }
429 propertyValue = propertyValue.Elem()
430 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700431 switch propertyValue.Kind() {
432 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700433 err := printfIntoProperty(propertyValue, variableValue)
434 if err != nil {
435 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
436 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700437 case reflect.Slice:
438 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700439 err := printfIntoProperty(propertyValue.Index(j), variableValue)
440 if err != nil {
441 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
442 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700443 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800444 case reflect.Bool:
445 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700446 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700447 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700448 default:
449 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
450 }
451 }
452}
453
Colin Cross7e0eaf12017-05-05 16:16:24 -0700454func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700455 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700456
457 count := strings.Count(s, "%")
458 if count == 0 {
459 return nil
460 }
461
462 if count > 1 {
463 return fmt.Errorf("product variable properties only support a single '%%'")
464 }
465
Colin Cross7f64b6d2015-07-09 13:57:48 -0700466 if strings.Contains(s, "%d") {
467 switch v := variableValue.(type) {
468 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700469 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700470 case bool:
471 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700472 variableValue = 1
473 } else {
474 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700475 }
476 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700477 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700478 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700479 } else if strings.Contains(s, "%s") {
480 switch variableValue.(type) {
481 case string:
482 // Nothing
483 default:
484 return fmt.Errorf("unsupported type %T for %%s", variableValue)
485 }
486 } else {
487 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700488 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700489
490 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
491
492 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700493}