blob: bcab46a12040b4bcd1dd45e2e6290692edb79af3 [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() {
Colin Cross1e676be2016-10-12 14:38:15 -070027 PreDepsMutators(func(ctx RegisterMutatorsContext) {
28 ctx.BottomUp("variable", variableMutator).Parallel()
29 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070030}
31
32type variableProperties struct {
33 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070034 Platform_sdk_version struct {
35 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070036 Cflags []string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070037 }
Colin Cross83163192015-12-01 16:31:16 -080038
39 // unbundled_build is a catch-all property to annotate modules that don't build in one or
40 // more unbundled branches, usually due to dependencies missing from the manifest.
41 Unbundled_build struct {
42 Enabled *bool `android:"arch_variant"`
43 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080044
Dan Willemsen97cae012016-03-01 14:08:42 -080045 Malloc_not_svelte struct {
Christopher Ferris134f8f62019-04-01 16:30:21 -070046 Cflags []string `android:"arch_variant"`
47 Shared_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070048 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070049
50 Safestack struct {
51 Cflags []string `android:"arch_variant"`
52 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070053
Dan Willemsen1be35382016-07-25 17:42:18 -070054 Binder32bit struct {
55 Cflags []string
56 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070057
Colin Cross95436422017-05-04 13:57:05 -070058 Override_rs_driver struct {
59 Cflags []string
60 }
61
Steven Morelandc2b9f062017-12-13 14:06:20 -080062 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
63 // enabled.
64 Treble_linker_namespaces struct {
65 Cflags []string
66 }
Steven Morelandc2b9f062017-12-13 14:06:20 -080067 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
68 Enforce_vintf_manifest struct {
69 Cflags []string
70 }
71
Colin Cross6bc59ef2016-12-08 09:46:35 -080072 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
73 // debugging features that don't significantly impact runtime behavior. userdebug builds
74 // are used for dogfooding and performance testing, and should be as similar to user builds
75 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -070076 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070077 Cflags []string
78 Cppflags []string
79 Init_rc []string
80 Required []string
81 Host_required []string
82 Target_required []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -070083 }
Colin Cross6bc59ef2016-12-08 09:46:35 -080084
85 // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging
86 // features.
87 Eng struct {
88 Cflags []string
89 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -070090 Lto struct {
91 Never *bool
92 }
Colin Cross3273cc22018-10-26 23:58:42 -070093 Sanitize struct {
94 Address *bool
95 }
Colin Cross6bc59ef2016-12-08 09:46:35 -080096 }
Colin Crossb2123aa2017-05-05 13:37:11 -070097
98 Pdk struct {
Colin Cross99967a72017-10-17 13:55:02 -070099 Enabled *bool `android:"arch_variant"`
100 } `android:"arch_variant"`
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700101
102 Uml struct {
103 Cppflags []string
104 }
Risanea004dd2017-11-27 18:14:46 +0900105
Rajeev Kumar423e2db2018-03-09 18:16:35 -0800106 Use_lmkd_stats_log struct {
107 Cflags []string
108 }
109
Risanea004dd2017-11-27 18:14:46 +0900110 Arc struct {
Hirokazu Honda89913352018-01-17 14:00:33 +0900111 Cflags []string
Risanea004dd2017-11-27 18:14:46 +0900112 Exclude_srcs []string
Hirokazu Honda89913352018-01-17 14:00:33 +0900113 Include_dirs []string
114 Shared_libs []string
Risanea004dd2017-11-27 18:14:46 +0900115 Static_libs []string
Hirokazu Honda89913352018-01-17 14:00:33 +0900116 Srcs []string
Risanea004dd2017-11-27 18:14:46 +0900117 }
Roland Levillain2879d412019-08-13 15:00:18 +0100118
119 Flatten_apex struct {
120 Enabled *bool
121 }
Peter Collingbournec66baf72019-10-29 11:00:15 -0700122
123 Experimental_mte struct {
124 Cflags []string `android:"arch_variant"`
125 } `android:"arch_variant"`
Colin Cross83163192015-12-01 16:31:16 -0800126 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700127}
128
Colin Cross18c46802019-09-24 22:19:02 -0700129var zeroProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700130
Colin Cross485e5722015-08-27 13:28:01 -0700131type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700132 // Suffix to add to generated Makefiles
133 Make_suffix *string `json:",omitempty"`
134
Nan Zhang581fd212018-01-10 16:06:12 -0800135 BuildId *string `json:",omitempty"`
136 BuildNumberFromFile *string `json:",omitempty"`
137 DateFromFile *string `json:",omitempty"`
138
Colin Cross092c9da2019-04-02 22:56:43 -0700139 Platform_version_name *string `json:",omitempty"`
140 Platform_sdk_version *int `json:",omitempty"`
141 Platform_sdk_codename *string `json:",omitempty"`
142 Platform_sdk_final *bool `json:",omitempty"`
143 Platform_version_active_codenames []string `json:",omitempty"`
144 Platform_version_future_codenames []string `json:",omitempty"`
145 Platform_vndk_version *string `json:",omitempty"`
146 Platform_systemsdk_versions []string `json:",omitempty"`
147 Platform_security_patch *string `json:",omitempty"`
148 Platform_preview_sdk_version *string `json:",omitempty"`
149 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
150 Platform_base_os *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700151
Colin Crossa74ca042019-01-31 14:31:51 -0800152 DeviceName *string `json:",omitempty"`
153 DeviceArch *string `json:",omitempty"`
154 DeviceArchVariant *string `json:",omitempty"`
155 DeviceCpuVariant *string `json:",omitempty"`
156 DeviceAbi []string `json:",omitempty"`
157 DeviceVndkVersion *string `json:",omitempty"`
158 DeviceSystemSdkVersions []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700159
Colin Crossa74ca042019-01-31 14:31:51 -0800160 DeviceSecondaryArch *string `json:",omitempty"`
161 DeviceSecondaryArchVariant *string `json:",omitempty"`
162 DeviceSecondaryCpuVariant *string `json:",omitempty"`
163 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700164
dimitry8d6dde82019-07-11 10:23:53 +0200165 NativeBridgeArch *string `json:",omitempty"`
166 NativeBridgeArchVariant *string `json:",omitempty"`
167 NativeBridgeCpuVariant *string `json:",omitempty"`
168 NativeBridgeAbi []string `json:",omitempty"`
169 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100170
dimitry8d6dde82019-07-11 10:23:53 +0200171 NativeBridgeSecondaryArch *string `json:",omitempty"`
172 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
173 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
174 NativeBridgeSecondaryAbi []string `json:",omitempty"`
175 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100176
Colin Cross4225f652015-09-17 14:33:42 -0700177 HostArch *string `json:",omitempty"`
178 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800179
180 CrossHost *string `json:",omitempty"`
181 CrossHostArch *string `json:",omitempty"`
182 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800183
Anton Hansson53c88442019-03-18 15:53:16 +0000184 DeviceResourceOverlays []string `json:",omitempty"`
185 ProductResourceOverlays []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800186 EnforceRROTargets []string `json:",omitempty"`
187 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700188
Colin Crossa74ca042019-01-31 14:31:51 -0800189 AAPTCharacteristics *string `json:",omitempty"`
190 AAPTConfig []string `json:",omitempty"`
191 AAPTPreferredConfig *string `json:",omitempty"`
192 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700193
Colin Cross61ae0b72017-12-01 17:16:02 -0800194 DefaultAppCertificate *string `json:",omitempty"`
195
Colin Crossface4e42017-10-30 17:32:15 -0700196 AppsDefaultVersionName *string `json:",omitempty"`
197
Colin Cross1f367bf2018-12-18 22:46:24 -0800198 Allow_missing_dependencies *bool `json:",omitempty"`
199 Unbundled_build *bool `json:",omitempty"`
200 Unbundled_build_sdks_from_source *bool `json:",omitempty"`
201 Malloc_not_svelte *bool `json:",omitempty"`
202 Safestack *bool `json:",omitempty"`
203 HostStaticBinaries *bool `json:",omitempty"`
204 Binder32bit *bool `json:",omitempty"`
205 UseGoma *bool `json:",omitempty"`
Ramy Medhatbbf25672019-07-17 12:30:04 +0000206 UseRBE *bool `json:",omitempty"`
Colin Cross1f367bf2018-12-18 22:46:24 -0800207 Debuggable *bool `json:",omitempty"`
208 Eng *bool `json:",omitempty"`
209 Treble_linker_namespaces *bool `json:",omitempty"`
210 Enforce_vintf_manifest *bool `json:",omitempty"`
211 Pdk *bool `json:",omitempty"`
212 Uml *bool `json:",omitempty"`
213 Use_lmkd_stats_log *bool `json:",omitempty"`
214 Arc *bool `json:",omitempty"`
215 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800216
Logan Chien4fcea3d2018-11-20 11:59:08 +0800217 Check_elf_files *bool `json:",omitempty"`
218
Colin Cross5a0dcd52018-10-05 14:20:06 -0700219 UncompressPrivAppDex *bool `json:",omitempty"`
220 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800221
Colin Cross9be41522019-02-20 10:40:13 -0800222 BootJars []string `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000223
Colin Crossa74ca042019-01-31 14:31:51 -0800224 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700225
Colin Crossa74ca042019-01-31 14:31:51 -0800226 EnableCFI *bool `json:",omitempty"`
227 CFIExcludePaths []string `json:",omitempty"`
228 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700229
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800230 DisableScudo *bool `json:",omitempty"`
231
Colin Crossa74ca042019-01-31 14:31:51 -0800232 EnableXOM *bool `json:",omitempty"`
233 XOMExcludePaths []string `json:",omitempty"`
Ivan Lozano074ec482018-11-21 08:59:37 -0800234
Peter Collingbournec66baf72019-10-29 11:00:15 -0700235 Experimental_mte *bool `json:",omitempty"`
236
Justin Yund5f6c822019-06-25 16:47:17 +0900237 VendorPath *string `json:",omitempty"`
238 OdmPath *string `json:",omitempty"`
239 ProductPath *string `json:",omitempty"`
240 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800241
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700242 ClangTidy *bool `json:",omitempty"`
243 TidyChecks *string `json:",omitempty"`
244
Colin Crossa74ca042019-01-31 14:31:51 -0800245 NativeCoverage *bool `json:",omitempty"`
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800246 ClangCoverage *bool `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800247 CoveragePaths []string `json:",omitempty"`
248 CoverageExcludePaths []string `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800249
Colin Cross88da24e2018-10-01 15:30:23 -0700250 DevicePrefer32BitApps *bool `json:",omitempty"`
Colin Cross1e7d3702016-08-24 15:25:47 -0700251 DevicePrefer32BitExecutables *bool `json:",omitempty"`
252 HostPrefer32BitExecutables *bool `json:",omitempty"`
253
Colin Cross23ae82a2016-11-02 14:34:39 -0700254 SanitizeHost []string `json:",omitempty"`
255 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700256 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700257 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800258
259 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800260
261 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700262
263 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900264
Doug Horn21b94272019-01-16 12:06:11 -0800265 Fuchsia *bool `json:",omitempty"`
266
Jiyong Parkd773eb32017-07-03 13:18:12 +0900267 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900268
269 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800270
271 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800272
273 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700274
Inseob Kimae553032019-05-14 18:52:49 +0900275 VndkUseCoreVariant *bool `json:",omitempty"`
276 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800277
Tri Vof544fe32018-05-20 14:34:48 -0700278 BoardVendorSepolicyDirs []string `json:",omitempty"`
279 BoardOdmSepolicyDirs []string `json:",omitempty"`
280 BoardPlatPublicSepolicyDirs []string `json:",omitempty"`
281 BoardPlatPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900282 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700283
Inseob Kim1f086e22019-05-09 13:29:15 +0900284 BoardVndkRuntimeDisable *bool `json:",omitempty"`
285
Dan Willemsen0fe78662018-03-26 12:41:18 -0700286 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700287
Dan Albert23d37e02018-11-28 08:30:10 -0800288 Ndk_abis *bool `json:",omitempty"`
289 Exclude_draft_ndk_apis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900290
Roland Levillaina3863212019-08-12 19:56:16 +0100291 Flatten_apex *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800292
293 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900294
295 ManifestPackageNameOverrides []string `json:",omitempty"`
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800296 CertificateOverrides []string `json:",omitempty"`
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800297 PackageNameOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900298
299 EnforceSystemCertificate *bool `json:",omitempty"`
300 EnforceSystemCertificateWhitelist []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800301
Colin Crossf24a22a2019-01-31 14:12:44 -0800302 ProductHiddenAPIStubs []string `json:",omitempty"`
303 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
304 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700305
Inseob Kim0866b002019-04-15 20:21:29 +0900306 ProductPublicSepolicyDirs []string `json:",omitempty"`
307 ProductPrivateSepolicyDirs []string `json:",omitempty"`
308 ProductCompatibleProperty *bool `json:",omitempty"`
309
Dan Willemsen54879d12019-04-18 10:08:46 -0700310 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700311
312 MissingUsesLibraries []string `json:",omitempty"`
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900313
314 EnforceProductPartitionInterface *bool `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700315}
316
317func boolPtr(v bool) *bool {
318 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700319}
320
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700321func intPtr(v int) *int {
322 return &v
323}
324
Colin Cross4225f652015-09-17 14:33:42 -0700325func stringPtr(v string) *string {
326 return &v
327}
328
Colin Cross27385972015-09-18 10:57:10 -0700329func (v *productVariables) SetDefaultConfig() {
330 *v = productVariables{
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700331 BuildNumberFromFile: stringPtr("123456789"),
332
333 Platform_version_name: stringPtr("Q"),
334 Platform_sdk_version: intPtr(28),
335 Platform_sdk_codename: stringPtr("Q"),
336 Platform_sdk_final: boolPtr(false),
337 Platform_version_active_codenames: []string{"Q"},
338 Platform_version_future_codenames: []string{"Q"},
339 Platform_vndk_version: stringPtr("Q"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800340
Colin Cross4225f652015-09-17 14:33:42 -0700341 HostArch: stringPtr("x86_64"),
342 HostSecondaryArch: stringPtr("x86"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800343 DeviceName: stringPtr("generic_arm64"),
Colin Cross4225f652015-09-17 14:33:42 -0700344 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700345 DeviceArchVariant: stringPtr("armv8-a"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800346 DeviceCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800347 DeviceAbi: []string{"arm64-v8a"},
Colin Cross4225f652015-09-17 14:33:42 -0700348 DeviceSecondaryArch: stringPtr("arm"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800349 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
350 DeviceSecondaryCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800351 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
Colin Crossface4e42017-10-30 17:32:15 -0700352
Colin Crossa74ca042019-01-31 14:31:51 -0800353 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700354 AAPTPreferredConfig: stringPtr("xhdpi"),
355 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800356 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700357
Dan Willemsencbf9e822017-11-13 14:34:56 -0800358 Malloc_not_svelte: boolPtr(true),
Colin Crossface4e42017-10-30 17:32:15 -0700359 Safestack: boolPtr(false),
Colin Cross485e5722015-08-27 13:28:01 -0700360 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800361
362 if runtime.GOOS == "linux" {
363 v.CrossHost = stringPtr("windows")
364 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800365 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800366 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700367}
368
Colin Cross635c3b02016-05-18 15:37:25 -0700369func variableMutator(mctx BottomUpMutatorContext) {
370 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700371 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700372 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700373 return
374 }
375
376 // TODO: depend on config variable, create variants, propagate variants up tree
377 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700378
379 if a.variableProperties == nil {
380 return
381 }
382
383 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
384 zeroValues := reflect.ValueOf(zeroProductVariables).FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700385
386 for i := 0; i < variableValues.NumField(); i++ {
387 variableValue := variableValues.Field(i)
388 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700389 name := variableValues.Type().Field(i).Name
390 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700391
Colin Crossa6bc19e2015-09-16 13:53:42 -0700392 // Check that the variable was set for the product
Dan Willemsen45133ac2018-03-09 21:22:06 -0800393 val := reflect.ValueOf(mctx.Config().productVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700394 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
395 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700396 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700397
398 val = val.Elem()
399
400 // For bools, check that the value is true
401 if val.Kind() == reflect.Bool && val.Bool() == false {
402 continue
403 }
404
405 // Check if any properties were set for the module
406 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
407 continue
408 }
409
410 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700411 }
412}
413
Colin Cross4157e882019-06-06 16:57:04 -0700414func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700415 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
416
Colin Cross7e0eaf12017-05-05 16:16:24 -0700417 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700418
Colin Cross4157e882019-06-06 16:57:04 -0700419 err := proptools.AppendMatchingProperties(m.generalProperties,
Colin Cross06a931b2015-10-28 17:23:31 -0700420 productVariablePropertyValue.Addr().Interface(), nil)
421 if err != nil {
422 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
423 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
424 } else {
425 panic(err)
426 }
427 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700428}
429
Colin Cross7e0eaf12017-05-05 16:16:24 -0700430func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
431 productVariablePropertyValue reflect.Value, i int, err error) {
432
433 field := productVariablePropertyValue.Type().Field(i).Name
434 property := prefix + "." + proptools.PropertyNameForField(field)
435 ctx.PropertyErrorf(property, "%s", err)
436}
437
438func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
439 productVariablePropertyValue reflect.Value, variableValue interface{}) {
440
Colin Cross7f64b6d2015-07-09 13:57:48 -0700441 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
442 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800443 kind := propertyValue.Kind()
444 if kind == reflect.Ptr {
445 if propertyValue.IsNil() {
446 continue
447 }
448 propertyValue = propertyValue.Elem()
449 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700450 switch propertyValue.Kind() {
451 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700452 err := printfIntoProperty(propertyValue, variableValue)
453 if err != nil {
454 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
455 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700456 case reflect.Slice:
457 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700458 err := printfIntoProperty(propertyValue.Index(j), variableValue)
459 if err != nil {
460 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
461 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700462 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800463 case reflect.Bool:
464 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700465 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700466 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700467 default:
468 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
469 }
470 }
471}
472
Colin Cross7e0eaf12017-05-05 16:16:24 -0700473func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700474 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700475
476 count := strings.Count(s, "%")
477 if count == 0 {
478 return nil
479 }
480
481 if count > 1 {
482 return fmt.Errorf("product variable properties only support a single '%%'")
483 }
484
Colin Cross7f64b6d2015-07-09 13:57:48 -0700485 if strings.Contains(s, "%d") {
486 switch v := variableValue.(type) {
487 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700488 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700489 case bool:
490 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700491 variableValue = 1
492 } else {
493 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700494 }
495 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700496 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700497 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700498 } else if strings.Contains(s, "%s") {
499 switch variableValue.(type) {
500 case string:
501 // Nothing
502 default:
503 return fmt.Errorf("unsupported type %T for %%s", variableValue)
504 }
505 } else {
506 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700507 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700508
509 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
510
511 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700512}
Colin Cross18c46802019-09-24 22:19:02 -0700513
514var variablePropTypeMap OncePer
515
516// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
517// reflect.Types of each property struct. The result can be used as a key in a map.
518func sliceToTypeArray(s []interface{}) interface{} {
519 // Create an array using reflection whose length is the length of the input slice
520 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
521 for i, e := range s {
522 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
523 }
524 return ret.Interface()
525}
526
527// createVariableProperties takes the list of property structs for a module and returns a property struct that
528// contains the product variable properties that exist in the property structs, or nil if there are none. It
529// caches the result.
530func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
531 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
532 key := sliceToTypeArray(moduleTypeProps)
533
534 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
535 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
536 // Compute the filtered property struct type.
537 return createVariablePropertiesType(moduleTypeProps, productVariables)
538 }).(reflect.Type)
539
540 if typ == nil {
541 return nil
542 }
543
544 // Create a new pointer to a filtered property struct.
545 return reflect.New(typ).Interface()
546}
547
548// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
549// a list of property structs.
550func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
551 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
552 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
553 // Filter function, returns true if the field should be in the resulting struct
554 if prefix == "" {
555 // Keep the top level Product_variables field
556 return true, field
557 }
558 _, rest := splitPrefix(prefix)
559 if rest == "" {
560 // Keep the 2nd level field (i.e. Product_variables.Eng)
561 return true, field
562 }
563
564 // Strip off the first 2 levels of the prefix
565 _, prefix = splitPrefix(rest)
566
567 for _, p := range moduleTypeProps {
568 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
569 // Keep any fields that exist in one of the property structs
570 return true, field
571 }
572 }
573
574 return false, field
575 })
576 return typ
577}
578
579func splitPrefix(prefix string) (first, rest string) {
580 index := strings.IndexByte(prefix, '.')
581 if index == -1 {
582 return prefix, ""
583 }
584 return prefix[:index], prefix[index+1:]
585}
586
587func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
588 if t.Kind() != reflect.Struct {
589 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
590 }
591
592 if prefix != "" {
593 split := strings.SplitN(prefix, ".", 2)
594 firstPrefix := split[0]
595 rest := ""
596 if len(split) > 1 {
597 rest = split[1]
598 }
599 f, exists := t.FieldByName(firstPrefix)
600 if !exists {
601 return false
602 }
603 ft := f.Type
604 if ft.Kind() == reflect.Ptr {
605 ft = ft.Elem()
606 }
607 if ft.Kind() != reflect.Struct {
608 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
609 }
610 return fieldExistsByNameRecursive(ft, rest, name)
611 } else {
612 _, exists := t.FieldByName(name)
613 return exists
614 }
615}