blob: fe828d3e320f5204a874191093babc40bb0075c7 [file] [log] [blame]
Colin Cross7f64b6d2015-07-09 13:57:48 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross7f64b6d2015-07-09 13:57:48 -070016
17import (
Jingwen Chena47f28d2021-11-02 16:43:57 +000018 "android/soong/android/soongconfig"
19 "android/soong/bazel"
Colin Cross7f64b6d2015-07-09 13:57:48 -070020 "fmt"
21 "reflect"
Dan Willemsen490fd492015-11-24 17:53:15 -080022 "runtime"
Colin Cross7f64b6d2015-07-09 13:57:48 -070023 "strings"
24
Colin Cross7f64b6d2015-07-09 13:57:48 -070025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
Paul Duffine7a055c2021-03-07 15:46:33 +000029 registerVariableBuildComponents(InitRegistrationContext)
30}
31
32func registerVariableBuildComponents(ctx RegistrationContext) {
33 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
Colin Crosseabaedd2020-02-06 17:01:55 -080034 ctx.BottomUp("variable", VariableMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070035 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070036}
37
Paul Duffine7a055c2021-03-07 15:46:33 +000038var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
39
Colin Cross7f64b6d2015-07-09 13:57:48 -070040type variableProperties struct {
41 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070042 Platform_sdk_version struct {
43 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070044 Cflags []string
Dan Willemsen9fe14102021-07-13 21:52:04 -070045 Cmd *string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070046 }
Colin Cross83163192015-12-01 16:31:16 -080047
Yuntao Xu402e9b02021-08-09 15:44:44 -070048 Platform_sdk_version_or_codename struct {
49 Java_resource_dirs []string
50 }
51
Anton Hanssonb021bf82021-08-27 17:35:40 +010052 Platform_sdk_extension_version struct {
53 Cmd *string
54 }
55
Dan Willemsen9fe14102021-07-13 21:52:04 -070056 Platform_version_name struct {
57 Base_dir *string
58 }
59
Colin Cross83163192015-12-01 16:31:16 -080060 // unbundled_build is a catch-all property to annotate modules that don't build in one or
61 // more unbundled branches, usually due to dependencies missing from the manifest.
62 Unbundled_build struct {
63 Enabled *bool `android:"arch_variant"`
64 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080065
Dan Willemsen97cae012016-03-01 14:08:42 -080066 Malloc_not_svelte struct {
Christopher Ferrisc71193a2019-12-16 09:55:10 -080067 Cflags []string `android:"arch_variant"`
68 Shared_libs []string `android:"arch_variant"`
69 Whole_static_libs []string `android:"arch_variant"`
70 Exclude_static_libs []string `android:"arch_variant"`
Christopher Ferrisdea66172021-09-24 00:07:53 -070071 Srcs []string `android:"arch_variant"`
72 Header_libs []string `android:"arch_variant"`
Sasha Smundak61ebf8d2018-08-31 16:56:05 -070073 } `android:"arch_variant"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070074
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -070075 Malloc_zero_contents struct {
76 Cflags []string `android:"arch_variant"`
77 } `android:"arch_variant"`
78
79 Malloc_pattern_fill_contents struct {
80 Cflags []string `android:"arch_variant"`
81 } `android:"arch_variant"`
82
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070083 Safestack struct {
84 Cflags []string `android:"arch_variant"`
85 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070086
Dan Willemsen1be35382016-07-25 17:42:18 -070087 Binder32bit struct {
88 Cflags []string
89 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070090
Colin Cross95436422017-05-04 13:57:05 -070091 Override_rs_driver struct {
92 Cflags []string
93 }
94
Steven Morelandc2b9f062017-12-13 14:06:20 -080095 // treble_linker_namespaces is true when the system/vendor linker namespace separation is
96 // enabled.
97 Treble_linker_namespaces struct {
98 Cflags []string
99 }
Steven Morelandc2b9f062017-12-13 14:06:20 -0800100 // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
101 Enforce_vintf_manifest struct {
102 Cflags []string
103 }
104
Colin Cross6bc59ef2016-12-08 09:46:35 -0800105 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
106 // debugging features that don't significantly impact runtime behavior. userdebug builds
107 // are used for dogfooding and performance testing, and should be as similar to user builds
108 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700109 Debuggable struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -0700110 Cflags []string
111 Cppflags []string
112 Init_rc []string
113 Required []string
114 Host_required []string
115 Target_required []string
Philip Cuadra328e0bf2020-10-19 22:01:29 -0700116 Strip struct {
117 All *bool
118 Keep_symbols *bool
119 Keep_symbols_and_debug_frame *bool
120 }
Yifan Hong8f323ae2021-06-23 20:01:13 -0700121 Static_libs []string
122 Whole_static_libs []string
123 Shared_libs []string
Jiyong Park16e77a92021-08-30 18:43:19 +0900124
125 Cmdline []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700126 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800127
128 // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging
129 // features.
130 Eng struct {
131 Cflags []string
132 Cppflags []string
John Reck1d2b7ee2018-07-11 11:01:59 -0700133 Lto struct {
134 Never *bool
135 }
Colin Cross3273cc22018-10-26 23:58:42 -0700136 Sanitize struct {
137 Address *bool
138 }
Felka Changf13642e2019-06-21 20:58:27 +0800139 Optimize struct {
140 Enabled *bool
141 }
Colin Cross6bc59ef2016-12-08 09:46:35 -0800142 }
Colin Crossb2123aa2017-05-05 13:37:11 -0700143
144 Pdk struct {
Colin Cross99967a72017-10-17 13:55:02 -0700145 Enabled *bool `android:"arch_variant"`
146 } `android:"arch_variant"`
Dmitry Shmidtdba54192017-08-23 14:58:37 -0700147
148 Uml struct {
149 Cppflags []string
150 }
Risanea004dd2017-11-27 18:14:46 +0900151
152 Arc struct {
Satoshi Niwa54f48262020-12-18 09:53:26 +0900153 Cflags []string `android:"arch_variant"`
154 Exclude_srcs []string `android:"arch_variant"`
Jasmine Chenacace422021-03-05 00:56:40 +0800155 Header_libs []string `android:"arch_variant"`
Satoshi Niwa54f48262020-12-18 09:53:26 +0900156 Include_dirs []string `android:"arch_variant"`
157 Shared_libs []string `android:"arch_variant"`
158 Static_libs []string `android:"arch_variant"`
159 Srcs []string `android:"arch_variant"`
160 Whole_static_libs []string `android:"arch_variant"`
161 } `android:"arch_variant"`
Roland Levillain2879d412019-08-13 15:00:18 +0100162
163 Flatten_apex struct {
164 Enabled *bool
165 }
Peter Collingbournec66baf72019-10-29 11:00:15 -0700166
Colin Crossd9a121b2020-01-27 13:26:42 -0800167 Native_coverage struct {
Colin Cross2b10ba02020-02-05 16:23:21 -0800168 Src *string `android:"arch_variant"`
Colin Crossd9a121b2020-01-27 13:26:42 -0800169 Srcs []string `android:"arch_variant"`
170 Exclude_srcs []string `android:"arch_variant"`
171 } `android:"arch_variant"`
Colin Cross83163192015-12-01 16:31:16 -0800172 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700173}
174
Colin Cross6961a492020-02-06 16:57:45 -0800175var defaultProductVariables interface{} = variableProperties{}
Colin Cross7f64b6d2015-07-09 13:57:48 -0700176
Colin Cross485e5722015-08-27 13:28:01 -0700177type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700178 // Suffix to add to generated Makefiles
179 Make_suffix *string `json:",omitempty"`
180
Colin Cross2a2e0db2020-02-21 16:55:46 -0800181 BuildId *string `json:",omitempty"`
182 BuildNumberFile *string `json:",omitempty"`
Nan Zhang581fd212018-01-10 16:06:12 -0800183
Colin Cross092c9da2019-04-02 22:56:43 -0700184 Platform_version_name *string `json:",omitempty"`
185 Platform_sdk_version *int `json:",omitempty"`
186 Platform_sdk_codename *string `json:",omitempty"`
Yuntao Xu402e9b02021-08-09 15:44:44 -0700187 Platform_sdk_version_or_codename *string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700188 Platform_sdk_final *bool `json:",omitempty"`
Anton Hanssonb021bf82021-08-27 17:35:40 +0100189 Platform_sdk_extension_version *int `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700190 Platform_version_active_codenames []string `json:",omitempty"`
Colin Cross092c9da2019-04-02 22:56:43 -0700191 Platform_vndk_version *string `json:",omitempty"`
192 Platform_systemsdk_versions []string `json:",omitempty"`
193 Platform_security_patch *string `json:",omitempty"`
194 Platform_preview_sdk_version *string `json:",omitempty"`
195 Platform_min_supported_target_sdk_version *string `json:",omitempty"`
196 Platform_base_os *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700197
Jeongik Cha219141c2020-08-06 23:00:37 +0900198 DeviceName *string `json:",omitempty"`
199 DeviceArch *string `json:",omitempty"`
200 DeviceArchVariant *string `json:",omitempty"`
201 DeviceCpuVariant *string `json:",omitempty"`
202 DeviceAbi []string `json:",omitempty"`
203 DeviceVndkVersion *string `json:",omitempty"`
204 DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
205 DeviceSystemSdkVersions []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700206
Jose Galmes6f843bc2020-12-11 13:36:29 -0800207 RecoverySnapshotVersion *string `json:",omitempty"`
208
Colin Crossa74ca042019-01-31 14:31:51 -0800209 DeviceSecondaryArch *string `json:",omitempty"`
210 DeviceSecondaryArchVariant *string `json:",omitempty"`
211 DeviceSecondaryCpuVariant *string `json:",omitempty"`
212 DeviceSecondaryAbi []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700213
dimitry8d6dde82019-07-11 10:23:53 +0200214 NativeBridgeArch *string `json:",omitempty"`
215 NativeBridgeArchVariant *string `json:",omitempty"`
216 NativeBridgeCpuVariant *string `json:",omitempty"`
217 NativeBridgeAbi []string `json:",omitempty"`
218 NativeBridgeRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100219
dimitry8d6dde82019-07-11 10:23:53 +0200220 NativeBridgeSecondaryArch *string `json:",omitempty"`
221 NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
222 NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
223 NativeBridgeSecondaryAbi []string `json:",omitempty"`
224 NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
dimitry1f33e402019-03-26 12:39:31 +0100225
Colin Cross4225f652015-09-17 14:33:42 -0700226 HostArch *string `json:",omitempty"`
227 HostSecondaryArch *string `json:",omitempty"`
Colin Cross528d67e2021-07-23 22:23:07 +0000228 HostMusl *bool `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800229
230 CrossHost *string `json:",omitempty"`
231 CrossHostArch *string `json:",omitempty"`
232 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800233
Jeongik Chacee5ba92021-02-19 12:11:51 +0900234 DeviceResourceOverlays []string `json:",omitempty"`
235 ProductResourceOverlays []string `json:",omitempty"`
236 EnforceRROTargets []string `json:",omitempty"`
Colin Crossa74ca042019-01-31 14:31:51 -0800237 EnforceRROExcludedOverlays []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700238
Colin Crossa74ca042019-01-31 14:31:51 -0800239 AAPTCharacteristics *string `json:",omitempty"`
240 AAPTConfig []string `json:",omitempty"`
241 AAPTPreferredConfig *string `json:",omitempty"`
242 AAPTPrebuiltDPI []string `json:",omitempty"`
Colin Crossface4e42017-10-30 17:32:15 -0700243
Colin Cross61ae0b72017-12-01 17:16:02 -0800244 DefaultAppCertificate *string `json:",omitempty"`
245
Colin Crossface4e42017-10-30 17:32:15 -0700246 AppsDefaultVersionName *string `json:",omitempty"`
247
Jeongik Cha816a23a2020-07-08 01:09:23 +0900248 Allow_missing_dependencies *bool `json:",omitempty"`
249 Unbundled_build *bool `json:",omitempty"`
250 Unbundled_build_apps *bool `json:",omitempty"`
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900251 Unbundled_build_image *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900252 Always_use_prebuilt_sdks *bool `json:",omitempty"`
Paul Duffin9a89a2a2020-10-28 19:20:06 +0000253 Skip_boot_jars_check *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900254 Malloc_not_svelte *bool `json:",omitempty"`
255 Malloc_zero_contents *bool `json:",omitempty"`
256 Malloc_pattern_fill_contents *bool `json:",omitempty"`
257 Safestack *bool `json:",omitempty"`
258 HostStaticBinaries *bool `json:",omitempty"`
259 Binder32bit *bool `json:",omitempty"`
260 UseGoma *bool `json:",omitempty"`
261 UseRBE *bool `json:",omitempty"`
262 UseRBEJAVAC *bool `json:",omitempty"`
263 UseRBER8 *bool `json:",omitempty"`
264 UseRBED8 *bool `json:",omitempty"`
265 Debuggable *bool `json:",omitempty"`
266 Eng *bool `json:",omitempty"`
267 Treble_linker_namespaces *bool `json:",omitempty"`
268 Enforce_vintf_manifest *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900269 Uml *bool `json:",omitempty"`
Jeongik Cha816a23a2020-07-08 01:09:23 +0900270 Arc *bool `json:",omitempty"`
271 MinimizeJavaDebugInfo *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800272
Logan Chien4fcea3d2018-11-20 11:59:08 +0800273 Check_elf_files *bool `json:",omitempty"`
274
Colin Cross5a0dcd52018-10-05 14:20:06 -0700275 UncompressPrivAppDex *bool `json:",omitempty"`
276 ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800277
satayevd604b212021-07-21 14:23:52 +0100278 BootJars ConfiguredJarList `json:",omitempty"`
279 ApexBootJars ConfiguredJarList `json:",omitempty"`
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000280
Colin Crossa74ca042019-01-31 14:31:51 -0800281 IntegerOverflowExcludePaths []string `json:",omitempty"`
Ivan Lozano5f595532017-07-13 14:46:05 -0700282
Colin Crossa74ca042019-01-31 14:31:51 -0800283 EnableCFI *bool `json:",omitempty"`
284 CFIExcludePaths []string `json:",omitempty"`
285 CFIIncludePaths []string `json:",omitempty"`
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700286
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800287 DisableScudo *bool `json:",omitempty"`
288
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800289 MemtagHeapExcludePaths []string `json:",omitempty"`
290 MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
291 MemtagHeapSyncIncludePaths []string `json:",omitempty"`
292
Justin Yund5f6c822019-06-25 16:47:17 +0900293 VendorPath *string `json:",omitempty"`
294 OdmPath *string `json:",omitempty"`
295 ProductPath *string `json:",omitempty"`
296 SystemExtPath *string `json:",omitempty"`
Dan Willemsen4353bc42016-12-05 17:16:02 -0800297
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700298 ClangTidy *bool `json:",omitempty"`
299 TidyChecks *string `json:",omitempty"`
300
Jooyung Han03302ee2020-04-08 09:22:26 +0900301 SamplingPGO *bool `json:",omitempty"`
Yi Kongceb5b762020-03-20 15:22:27 +0800302
Roland Levillainada12702020-06-09 13:07:36 +0100303 JavaCoveragePaths []string `json:",omitempty"`
304 JavaCoverageExcludePaths []string `json:",omitempty"`
305
Colin Cross1a6acd42020-06-16 17:51:46 -0700306 GcovCoverage *bool `json:",omitempty"`
Roland Levillain4f5297b2020-06-09 12:44:06 +0100307 ClangCoverage *bool `json:",omitempty"`
308 NativeCoveragePaths []string `json:",omitempty"`
309 NativeCoverageExcludePaths []string `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800310
Colin Cross1a6acd42020-06-16 17:51:46 -0700311 // Set by NewConfig
Liz Kammer09f947d2021-05-12 14:51:49 -0400312 Native_coverage *bool `json:",omitempty"`
Colin Cross1a6acd42020-06-16 17:51:46 -0700313
Colin Cross23ae82a2016-11-02 14:34:39 -0700314 SanitizeHost []string `json:",omitempty"`
315 SanitizeDevice []string `json:",omitempty"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700316 SanitizeDeviceDiag []string `json:",omitempty"`
Colin Cross23ae82a2016-11-02 14:34:39 -0700317 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800318
319 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800320
321 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700322
323 Override_rs_driver *string `json:",omitempty"`
Jiyong Parkd773eb32017-07-03 13:18:12 +0900324
325 DeviceKernelHeaders []string `json:",omitempty"`
Justin Yun71549282017-11-17 12:10:28 +0900326
327 ExtraVndkVersions []string `json:",omitempty"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800328
329 NamespacesToExport []string `json:",omitempty"`
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800330
331 PgoAdditionalProfileDirs []string `json:",omitempty"`
Dan Willemsen0fe78662018-03-26 12:41:18 -0700332
Inseob Kimae553032019-05-14 18:52:49 +0900333 VndkUseCoreVariant *bool `json:",omitempty"`
334 VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
Vic Yangefd249e2018-11-12 20:19:56 -0800335
Inseob Kim7cf14652021-01-06 23:06:52 +0900336 DirectedVendorSnapshot bool `json:",omitempty"`
337 VendorSnapshotModules map[string]bool `json:",omitempty"`
338
Jose Galmes4c6895e2021-02-09 07:44:30 -0800339 DirectedRecoverySnapshot bool `json:",omitempty"`
340 RecoverySnapshotModules map[string]bool `json:",omitempty"`
341
Justin DeMartino383bfb32021-02-24 10:49:43 -0800342 VendorSnapshotDirsIncluded []string `json:",omitempty"`
343 VendorSnapshotDirsExcluded []string `json:",omitempty"`
344 RecoverySnapshotDirsExcluded []string `json:",omitempty"`
345 RecoverySnapshotDirsIncluded []string `json:",omitempty"`
Rob Seymour925aa092021-08-10 20:42:03 +0000346 HostFakeSnapshotEnabled bool `json:",omitempty"`
Justin DeMartino383bfb32021-02-24 10:49:43 -0800347
Tri Vof544fe32018-05-20 14:34:48 -0700348 BoardVendorSepolicyDirs []string `json:",omitempty"`
349 BoardOdmSepolicyDirs []string `json:",omitempty"`
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900350 BoardReqdMaskPolicy []string `json:",omitempty"`
Felixa20a8752020-05-17 18:28:35 +0200351 SystemExtPublicSepolicyDirs []string `json:",omitempty"`
352 SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900353 BoardSepolicyM4Defs []string `json:",omitempty"`
Tri Vo35a51432018-03-25 20:00:00 -0700354
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900355 BoardSepolicyVers *string `json:",omitempty"`
356 PlatformSepolicyVersion *string `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000357 TotSepolicyVersion *string `json:",omitempty"`
Inseob Kim16ebd5a2020-12-09 23:08:17 +0900358
Dan Willemsen0fe78662018-03-26 12:41:18 -0700359 VendorVars map[string]map[string]string `json:",omitempty"`
Colin Cross395f2cf2018-10-24 16:10:32 -0700360
Dan Albertd05ba002021-04-13 15:55:47 -0700361 Ndk_abis *bool `json:",omitempty"`
Jiyong Park8fd61922018-11-08 02:50:25 +0900362
Jiyong Park4da07972021-01-05 21:01:11 +0900363 Flatten_apex *bool `json:",omitempty"`
364 ForceApexSymlinkOptimization *bool `json:",omitempty"`
365 CompressedApex *bool `json:",omitempty"`
366 Aml_abis *bool `json:",omitempty"`
Colin Cross43f08db2018-11-12 10:13:39 -0800367
368 DexpreoptGlobalConfig *string `json:",omitempty"`
Jiyong Park7f67f482019-01-05 12:57:48 +0900369
Inseob Kim7b85eeb2021-03-23 20:52:24 +0900370 WithDexpreopt bool `json:",omitempty"`
371
Jiyong Park7f67f482019-01-05 12:57:48 +0900372 ManifestPackageNameOverrides []string `json:",omitempty"`
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800373 CertificateOverrides []string `json:",omitempty"`
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800374 PackageNameOverrides []string `json:",omitempty"`
Jeongik Chac9464142019-01-07 12:07:27 +0900375
376 EnforceSystemCertificate *bool `json:",omitempty"`
Colin Cross440e0d02020-06-11 11:32:11 -0700377 EnforceSystemCertificateAllowList []string `json:",omitempty"`
Colin Cross8faf8fc2019-01-16 15:15:52 -0800378
Colin Crossf24a22a2019-01-31 14:12:44 -0800379 ProductHiddenAPIStubs []string `json:",omitempty"`
380 ProductHiddenAPIStubsSystem []string `json:",omitempty"`
381 ProductHiddenAPIStubsTest []string `json:",omitempty"`
Dan Willemsen71c74602019-04-10 12:27:35 -0700382
Inseob Kim0866b002019-04-15 20:21:29 +0900383 ProductPublicSepolicyDirs []string `json:",omitempty"`
384 ProductPrivateSepolicyDirs []string `json:",omitempty"`
Inseob Kim0866b002019-04-15 20:21:29 +0900385
Justin Yun5f7f7e82019-11-18 19:52:14 +0900386 ProductVndkVersion *string `json:",omitempty"`
387
Dan Willemsen54879d12019-04-18 10:08:46 -0700388 TargetFSConfigGen []string `json:",omitempty"`
Colin Cross50ddcc42019-05-16 12:28:22 -0700389
390 MissingUsesLibraries []string `json:",omitempty"`
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900391
392 EnforceProductPartitionInterface *bool `json:",omitempty"`
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900393
JaeMan Parkff715562020-10-19 17:25:58 +0900394 EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
395 InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
396
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900397 InstallExtraFlattenedApexes *bool `json:",omitempty"`
Yifan Hong82db7352020-01-21 16:12:26 -0800398
399 BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
Yifan Hong97365ee2020-07-29 09:51:57 -0700400
Yifan Hong42bef8d2020-08-05 14:36:09 -0700401 BoardKernelBinaries []string `json:",omitempty"`
402 BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
Yifan Hongdd8dacc2020-10-21 15:40:17 -0700403
404 BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
Bill Peckhambae47492021-01-08 09:34:44 -0800405
406 PrebuiltHiddenApiDir *string `json:",omitempty"`
Inseob Kim60c32f02020-12-21 22:53:05 +0900407
408 ShippingApiLevel *string `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900409
Inseob Kim67e5add192021-03-17 18:05:33 +0900410 BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
411 BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
Inseob Kim0cac7b42021-02-03 18:16:46 +0900412 BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
Inseob Kim67e5add192021-03-17 18:05:33 +0900413
Hridya Valsaraju5a5c7d52021-04-02 16:45:24 -0700414 BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
415
Inseob Kim67e5add192021-03-17 18:05:33 +0900416 RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
417
418 SelinuxIgnoreNeverallows bool `json:",omitempty"`
419
420 SepolicySplit bool `json:",omitempty"`
Inseob Kima10ef272021-09-15 03:04:53 +0000421
422 SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
423 SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
Jiyong Parkd163d4d2021-10-12 16:47:43 +0900424
425 GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700426}
427
428func boolPtr(v bool) *bool {
429 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700430}
431
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700432func intPtr(v int) *int {
433 return &v
434}
435
Colin Cross4225f652015-09-17 14:33:42 -0700436func stringPtr(v string) *string {
437 return &v
438}
439
Colin Cross27385972015-09-18 10:57:10 -0700440func (v *productVariables) SetDefaultConfig() {
441 *v = productVariables{
Colin Cross2a2e0db2020-02-21 16:55:46 -0800442 BuildNumberFile: stringPtr("build_number.txt"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700443
Jiyong Park84d8a7e2020-09-25 13:44:51 +0900444 Platform_version_name: stringPtr("S"),
445 Platform_sdk_version: intPtr(30),
446 Platform_sdk_codename: stringPtr("S"),
Dan Willemsenb6d171b2019-04-07 09:41:44 -0700447 Platform_sdk_final: boolPtr(false),
Jiyong Park84d8a7e2020-09-25 13:44:51 +0900448 Platform_version_active_codenames: []string{"S"},
449 Platform_vndk_version: stringPtr("S"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800450
Colin Cross4225f652015-09-17 14:33:42 -0700451 HostArch: stringPtr("x86_64"),
452 HostSecondaryArch: stringPtr("x86"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800453 DeviceName: stringPtr("generic_arm64"),
Colin Cross4225f652015-09-17 14:33:42 -0700454 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700455 DeviceArchVariant: stringPtr("armv8-a"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800456 DeviceCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800457 DeviceAbi: []string{"arm64-v8a"},
Colin Cross4225f652015-09-17 14:33:42 -0700458 DeviceSecondaryArch: stringPtr("arm"),
Dan Willemsencbf9e822017-11-13 14:34:56 -0800459 DeviceSecondaryArchVariant: stringPtr("armv8-a"),
460 DeviceSecondaryCpuVariant: stringPtr("generic"),
Colin Crossa74ca042019-01-31 14:31:51 -0800461 DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
Colin Crossface4e42017-10-30 17:32:15 -0700462
Colin Crossa74ca042019-01-31 14:31:51 -0800463 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700464 AAPTPreferredConfig: stringPtr("xhdpi"),
465 AAPTCharacteristics: stringPtr("nosdcard"),
Colin Crossa74ca042019-01-31 14:31:51 -0800466 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
Colin Crossface4e42017-10-30 17:32:15 -0700467
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700468 Malloc_not_svelte: boolPtr(true),
Steven Morelandd1342012020-07-30 20:38:49 +0000469 Malloc_zero_contents: boolPtr(true),
Evgenii Stepanov19ff3c92020-04-29 14:57:30 -0700470 Malloc_pattern_fill_contents: boolPtr(false),
471 Safestack: boolPtr(false),
Lukacs T. Berki720b3962021-03-17 13:34:30 +0100472
satayevd604b212021-07-21 14:23:52 +0100473 BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
474 ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
Colin Cross485e5722015-08-27 13:28:01 -0700475 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800476
477 if runtime.GOOS == "linux" {
478 v.CrossHost = stringPtr("windows")
479 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800480 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800481 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700482}
483
Liz Kammera060c452021-03-24 10:14:47 -0400484// ProductConfigContext requires the access to the Module to get product config properties.
485type ProductConfigContext interface {
486 Module() Module
487}
488
489// ProductConfigProperty contains the information for a single property (may be a struct) paired
490// with the appropriate ProductConfigVariable.
491type ProductConfigProperty struct {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000492 // The name of the product variable, e.g. "safestack", "malloc_not_svelte",
493 // "board"
Jingwen Chen25825ca2021-11-15 12:28:43 +0000494 Name string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000495
496 // Namespace of the variable, if this is a soong_config_module_type variable
Jingwen Chen25825ca2021-11-15 12:28:43 +0000497 // e.g. "acme", "ANDROID", "vendor_name"
498 Namespace string
Jingwen Chena47f28d2021-11-02 16:43:57 +0000499
500 // Unique configuration to identify this product config property (i.e. a
501 // primary key), as just using the product variable name is not sufficient.
502 //
503 // For product variables, this is the product variable name + optional
504 // archvariant information. e.g.
505 //
506 // product_variables: {
507 // foo: {
508 // cflags: ["-Dfoo"],
509 // },
510 // },
511 //
512 // FullConfig would be "foo".
513 //
514 // target: {
515 // android: {
516 // product_variables: {
517 // foo: {
518 // cflags: ["-Dfoo-android"],
519 // },
520 // },
521 // },
522 // },
523 //
524 // FullConfig would be "foo-android".
525 //
526 // For soong config variables, this is the namespace + product variable name
527 // + value of the variable, if applicable. The value can also be
528 // conditions_default.
529 //
530 // e.g.
531 //
532 // soong_config_variables: {
533 // feature1: {
534 // conditions_default: {
535 // cflags: ["-DDEFAULT1"],
536 // },
537 // cflags: ["-DFEATURE1"],
538 // },
539 // }
540 //
541 // where feature1 is created in the "acme" namespace, so FullConfig would be
542 // "acme__feature1" and "acme__feature1__conditions_default".
543 //
544 // e.g.
545 //
546 // soong_config_variables: {
547 // board: {
548 // soc_a: {
549 // cflags: ["-DSOC_A"],
550 // },
551 // soc_b: {
552 // cflags: ["-DSOC_B"],
553 // },
554 // soc_c: {},
555 // conditions_default: {
556 // cflags: ["-DSOC_DEFAULT"]
557 // },
558 // },
559 // }
560 //
561 // where board is created in the "acme" namespace, so FullConfig would be
562 // "acme__board__soc_a", "acme__board__soc_b", and
563 // "acme__board__conditions_default"
564 FullConfig string
Liz Kammera060c452021-03-24 10:14:47 -0400565}
566
Jingwen Chen58ff6802021-11-17 12:14:41 +0000567func (p *ProductConfigProperty) AlwaysEmit() bool {
568 return p.Namespace != ""
569}
570
Jingwen Chena47f28d2021-11-02 16:43:57 +0000571func (p *ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
572 if p.Namespace == "" {
573 return bazel.ProductVariableConfigurationAxis(p.FullConfig)
574 } else {
575 // Soong config variables can be uniquely identified by the namespace
576 // (e.g. acme, android) and the product variable name (e.g. board, size)
Jingwen Chen25825ca2021-11-15 12:28:43 +0000577 return bazel.ProductVariableConfigurationAxis(p.Namespace + "__" + p.Name)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000578 }
579}
580
Jingwen Chen25825ca2021-11-15 12:28:43 +0000581// SelectKey returns the literal string that represents this variable in a BUILD
582// select statement.
583func (p *ProductConfigProperty) SelectKey() string {
584 if p.Namespace == "" {
585 return strings.ToLower(p.FullConfig)
586 }
587
588 if p.FullConfig == bazel.ConditionsDefaultConfigKey {
589 return bazel.ConditionsDefaultConfigKey
590 }
591
592 value := p.FullConfig
593 if value == p.Name {
594 value = "enabled"
595 }
596 // e.g. acme__feature1__enabled, android__board__soc_a
597 return strings.ToLower(strings.Join([]string{p.Namespace, p.Name, value}, "__"))
598}
599
600// ProductConfigProperties is a map of maps to group property values according
601// their property name and the product config variable they're set under.
602//
603// The outer map key is the name of the property, like "cflags".
604//
605// The inner map key is a ProductConfigProperty, which is a struct of product
606// variable name, namespace, and the "full configuration" of the product
607// variable.
608//
609// e.g. product variable name: board, namespace: acme, full config: vendor_chip_foo
610//
611// The value of the map is the interface{} representing the value of the
612// property, like ["-DDEFINES"] for cflags.
613type ProductConfigProperties map[string]map[ProductConfigProperty]interface{}
Liz Kammera060c452021-03-24 10:14:47 -0400614
615// ProductVariableProperties returns a ProductConfigProperties containing only the properties which
616// have been set for the module in the given context.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000617func ProductVariableProperties(ctx BazelConversionPathContext) ProductConfigProperties {
Liz Kammera060c452021-03-24 10:14:47 -0400618 module := ctx.Module()
619 moduleBase := module.base()
620
621 productConfigProperties := ProductConfigProperties{}
622
Jingwen Chena47f28d2021-11-02 16:43:57 +0000623 if moduleBase.variableProperties != nil {
624 productVariablesProperty := proptools.FieldNameForProperty("product_variables")
625 productVariableValues(
626 productVariablesProperty,
627 moduleBase.variableProperties,
628 "",
629 "",
630 &productConfigProperties)
631
632 for _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
633 for config, props := range configToProps {
634 // GetArchVariantProperties is creating an instance of the requested type
635 // and productVariablesValues expects an interface, so no need to cast
636 productVariableValues(
637 productVariablesProperty,
638 props,
639 "",
640 config,
641 &productConfigProperties)
642 }
643 }
Liz Kammera060c452021-03-24 10:14:47 -0400644 }
645
Jingwen Chena47f28d2021-11-02 16:43:57 +0000646 if m, ok := module.(Bazelable); ok && m.namespacedVariableProps() != nil {
Jingwen Chen84817de2021-11-17 10:57:35 +0000647 for namespace, namespacedVariableProps := range m.namespacedVariableProps() {
648 for _, namespacedVariableProp := range namespacedVariableProps {
649 productVariableValues(
650 soongconfig.SoongConfigProperty,
651 namespacedVariableProp,
652 namespace,
653 "",
654 &productConfigProperties)
655 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400656 }
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400657 }
658
Jingwen Chen58ff6802021-11-17 12:14:41 +0000659 productConfigProperties.zeroValuesForNamespacedVariables()
660
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400661 return productConfigProperties
662}
663
Jingwen Chen58ff6802021-11-17 12:14:41 +0000664// zeroValuesForNamespacedVariables ensures that selects that contain __only__
665// conditions default values have zero values set for the other non-default
666// values for that select statement.
667//
668// If the ProductConfigProperties map contains these items, as parsed from the .bp file:
669//
670// library_linking_strategy: {
671// prefer_static: {
672// static_libs: [
673// "lib_a",
674// "lib_b",
675// ],
676// },
677// conditions_default: {
678// shared_libs: [
679// "lib_a",
680// "lib_b",
681// ],
682// },
683// },
684//
685// Static_libs {Library_linking_strategy ANDROID prefer_static} [lib_a lib_b]
686// Shared_libs {Library_linking_strategy ANDROID conditions_default} [lib_a lib_b]
687//
688// We need to add this:
689//
690// Shared_libs {Library_linking_strategy ANDROID prefer_static} []
691//
692// so that the following gets generated for the "dynamic_deps" attribute,
693// instead of putting lib_a and lib_b directly into dynamic_deps without a
694// select:
695//
696// dynamic_deps = select({
697// "//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
698// "//conditions:default": [
699// "//foo/bar:lib_a",
700// "//foo/bar:lib_b",
701// ],
702// }),
703func (props *ProductConfigProperties) zeroValuesForNamespacedVariables() {
704 // A map of product config properties to the zero values of their respective
705 // property value.
706 zeroValues := make(map[ProductConfigProperty]interface{})
707
708 // A map of prop names (e.g. cflags) to product config properties where the
709 // (prop name, ProductConfigProperty) tuple contains a non-conditions_default key.
710 //
711 // e.g.
712 //
713 // prefer_static: {
714 // static_libs: [
715 // "lib_a",
716 // "lib_b",
717 // ],
718 // },
719 // conditions_default: {
720 // shared_libs: [
721 // "lib_a",
722 // "lib_b",
723 // ],
724 // },
725 //
726 // The tuple of ("static_libs", prefer_static) would be in this map.
727 hasNonDefaultValue := make(map[string]map[ProductConfigProperty]bool)
728
729 // Iterate over all added soong config variables.
730 for propName, v := range *props {
731 for p, intf := range v {
732 if p.Namespace == "" {
733 // If there's no namespace, this isn't a soong config variable,
734 // i.e. this is a product variable. product variables have no
735 // conditions_defaults, so skip them.
736 continue
737 }
738 if p.FullConfig == bazel.ConditionsDefaultConfigKey {
739 // Skip conditions_defaults.
740 continue
741 }
742 if hasNonDefaultValue[propName] == nil {
743 hasNonDefaultValue[propName] = make(map[ProductConfigProperty]bool)
744 hasNonDefaultValue[propName][p] = false
745 }
746 // Create the zero value of the variable.
747 if _, exists := zeroValues[p]; !exists {
748 zeroValue := reflect.Zero(reflect.ValueOf(intf).Type()).Interface()
749 if zeroValue == nil {
750 panic(fmt.Errorf("Expected non-nil zero value for product/config variable %+v\n", intf))
751 }
752 zeroValues[p] = zeroValue
753 }
754 hasNonDefaultValue[propName][p] = true
755 }
756 }
757
758 for propName := range *props {
759 for p, zeroValue := range zeroValues {
760 // Ignore variables that already have a non-default value for that axis
761 if exists, _ := hasNonDefaultValue[propName][p]; !exists {
762 // fmt.Println(propName, p.Namespace, p.Name, p.FullConfig, zeroValue)
763 // Insert the zero value for this propname + product config value.
764 props.AddProductConfigProperty(
765 propName,
766 p.Namespace,
767 p.Name,
768 p.FullConfig,
769 zeroValue,
770 )
771 }
772 }
773 }
774}
775
Jingwen Chena47f28d2021-11-02 16:43:57 +0000776func (p *ProductConfigProperties) AddProductConfigProperty(
777 propertyName, namespace, productVariableName, config string, property interface{}) {
778 if (*p)[propertyName] == nil {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000779 (*p)[propertyName] = make(map[ProductConfigProperty]interface{})
Jingwen Chena47f28d2021-11-02 16:43:57 +0000780 }
781
Jingwen Chen25825ca2021-11-15 12:28:43 +0000782 productConfigProp := ProductConfigProperty{
783 Namespace: namespace, // e.g. acme, android
784 Name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
785 FullConfig: config, // e.g. size, feature1-x86, size__conditions_default
Jingwen Chena47f28d2021-11-02 16:43:57 +0000786 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000787
Jingwen Chen84817de2021-11-17 10:57:35 +0000788 if existing, ok := (*p)[propertyName][productConfigProp]; ok && namespace != "" {
789 switch dst := existing.(type) {
790 case []string:
791 if src, ok := property.([]string); ok {
792 dst = append(dst, src...)
793 (*p)[propertyName][productConfigProp] = dst
794 }
795 default:
Jingwen Chen58ff6802021-11-17 12:14:41 +0000796 panic(fmt.Errorf("TODO: handle merging value %s", existing))
Jingwen Chen84817de2021-11-17 10:57:35 +0000797 }
798 } else {
799 (*p)[propertyName][productConfigProp] = property
800 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000801}
802
803var (
804 conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
805)
806
807// maybeExtractConfigVarProp attempts to read this value as a config var struct
808// wrapped by interfaces and ptrs. If it's not the right type, the second return
809// value is false.
810func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
811 if v.Kind() == reflect.Interface {
812 // The conditions_default value can be either
813 // 1) an ptr to an interface of a struct (bool config variables and product variables)
814 // 2) an interface of 1) (config variables with nested structs, like string vars)
815 v = v.Elem()
816 }
817 if v.Kind() != reflect.Ptr {
818 return v, false
819 }
820 v = reflect.Indirect(v)
821 if v.Kind() == reflect.Interface {
822 // Extract the struct from the interface
823 v = v.Elem()
824 }
825
826 if !v.IsValid() {
827 return v, false
828 }
829
830 if v.Kind() != reflect.Struct {
831 return v, false
832 }
833 return v, true
834}
835
Jingwen Chen84817de2021-11-17 10:57:35 +0000836func (productConfigProperties *ProductConfigProperties) AddProductConfigProperties(namespace, suffix string, variableValues reflect.Value) {
837 // variableValues can either be a product_variables or
838 // soong_config_variables struct.
839 //
Jingwen Chena47f28d2021-11-02 16:43:57 +0000840 // Example of product_variables:
841 //
842 // product_variables: {
843 // malloc_not_svelte: {
844 // shared_libs: ["malloc_not_svelte_shared_lib"],
845 // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
846 // exclude_static_libs: [
847 // "malloc_not_svelte_static_lib_excludes",
848 // "malloc_not_svelte_whole_static_lib_excludes",
849 // ],
850 // },
851 // },
852 //
853 // Example of soong_config_variables:
854 //
855 // soong_config_variables: {
856 // feature1: {
857 // conditions_default: {
858 // ...
859 // },
860 // cflags: ...
861 // },
862 // feature2: {
863 // cflags: ...
864 // conditions_default: {
865 // ...
866 // },
867 // },
868 // board: {
869 // soc_a: {
870 // ...
871 // },
872 // soc_a: {
873 // ...
874 // },
875 // soc_c: {},
876 // conditions_default: {
877 // ...
878 // },
879 // },
880 // }
Liz Kammera060c452021-03-24 10:14:47 -0400881 for i := 0; i < variableValues.NumField(); i++ {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000882 // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
883 productVariableName := variableValues.Type().Field(i).Name
884
Liz Kammera060c452021-03-24 10:14:47 -0400885 variableValue := variableValues.Field(i)
886 // Check if any properties were set for the module
887 if variableValue.IsZero() {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000888 // e.g. feature1: {}, malloc_not_svelte: {}
Liz Kammera060c452021-03-24 10:14:47 -0400889 continue
890 }
Jingwen Chena47f28d2021-11-02 16:43:57 +0000891
892 // Unlike product variables, config variables require a few more
893 // indirections to extract the struct from the reflect.Value.
894 if v, ok := maybeExtractConfigVarProp(variableValue); ok {
895 variableValue = v
896 }
897
Liz Kammera060c452021-03-24 10:14:47 -0400898 for j := 0; j < variableValue.NumField(); j++ {
899 property := variableValue.Field(j)
900 // If the property wasn't set, no need to pass it along
901 if property.IsZero() {
902 continue
903 }
904
905 // e.g. Asflags, Cflags, Enabled, etc.
906 propertyName := variableValue.Type().Field(j).Name
Jingwen Chena47f28d2021-11-02 16:43:57 +0000907
908 if v, ok := maybeExtractConfigVarProp(property); ok {
909 // The field is a struct, which is used by:
910 // 1) soong_config_string_variables
911 //
912 // soc_a: {
913 // cflags: ...,
914 // }
915 //
916 // soc_b: {
917 // cflags: ...,
918 // }
919 //
920 // 2) conditions_default structs for all soong config variable types.
921 //
922 // conditions_default: {
923 // cflags: ...,
924 // static_libs: ...
925 // }
926 field := v
927 for k := 0; k < field.NumField(); k++ {
928 // Iterate over fields of this struct prop.
929 if field.Field(k).IsZero() {
930 continue
931 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000932 // config can also be "conditions_default".
933 config := proptools.PropertyNameForField(propertyName)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000934 actualPropertyName := field.Type().Field(k).Name
935
936 productConfigProperties.AddProductConfigProperty(
937 actualPropertyName, // e.g. cflags, static_libs
938 namespace, // e.g. acme, android
939 productVariableName, // e.g. size, feature1, FEATURE2, board
940 config,
941 field.Field(k).Interface(), // e.g. ["-DDEFAULT"], ["foo", "bar"]
942 )
943 }
Jingwen Chen58ff6802021-11-17 12:14:41 +0000944 } else if property.Kind() != reflect.Interface {
945 // If not an interface, then this is not a conditions_default or
946 // a struct prop. That is, this is a regular product variable,
947 // or a bool/value config variable.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000948 config := productVariableName + suffix
Jingwen Chena47f28d2021-11-02 16:43:57 +0000949 productConfigProperties.AddProductConfigProperty(
950 propertyName,
951 namespace,
952 productVariableName,
953 config,
954 property.Interface(),
955 )
Liz Kammer47535c52021-06-02 16:02:22 -0400956 }
Liz Kammera060c452021-03-24 10:14:47 -0400957 }
958 }
Liz Kammera060c452021-03-24 10:14:47 -0400959}
960
Jingwen Chen84817de2021-11-17 10:57:35 +0000961// productVariableValues uses reflection to convert a property struct for
962// product_variables and soong_config_variables to structs that can be generated
963// as select statements.
964func productVariableValues(
965 fieldName string, variableProps interface{}, namespace, suffix string, productConfigProperties *ProductConfigProperties) {
966 if suffix != "" {
967 suffix = "-" + suffix
968 }
969
970 // variableValues represent the product_variables or soong_config_variables struct.
971 variableValues := reflect.ValueOf(variableProps).Elem().FieldByName(fieldName)
972 productConfigProperties.AddProductConfigProperties(namespace, suffix, variableValues)
973}
974
Colin Crosseabaedd2020-02-06 17:01:55 -0800975func VariableMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700976 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700977 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700978 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700979 return
980 }
981
982 // TODO: depend on config variable, create variants, propagate variants up tree
983 a := module.base()
Colin Cross18c46802019-09-24 22:19:02 -0700984
985 if a.variableProperties == nil {
986 return
987 }
988
989 variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700990
Colin Cross0cec3122021-01-20 11:27:32 -0800991 productVariables := reflect.ValueOf(mctx.Config().productVariables)
992
Colin Cross7f64b6d2015-07-09 13:57:48 -0700993 for i := 0; i < variableValues.NumField(); i++ {
994 variableValue := variableValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700995 name := variableValues.Type().Field(i).Name
996 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700997
Colin Crossa6bc19e2015-09-16 13:53:42 -0700998 // Check that the variable was set for the product
Colin Cross0cec3122021-01-20 11:27:32 -0800999 val := productVariables.FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -07001000 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
1001 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -07001002 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001003
1004 val = val.Elem()
1005
1006 // For bools, check that the value is true
1007 if val.Kind() == reflect.Bool && val.Bool() == false {
1008 continue
1009 }
1010
1011 // Check if any properties were set for the module
Colin Cross6961a492020-02-06 16:57:45 -08001012 if variableValue.IsZero() {
Colin Crossa6bc19e2015-09-16 13:53:42 -07001013 continue
1014 }
Colin Crossa6bc19e2015-09-16 13:53:42 -07001015 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -07001016 }
1017}
1018
Colin Cross4157e882019-06-06 16:57:04 -07001019func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -07001020 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
1021
Colin Cross7e0eaf12017-05-05 16:16:24 -07001022 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001023
Colin Cross4157e882019-06-06 16:57:04 -07001024 err := proptools.AppendMatchingProperties(m.generalProperties,
Colin Cross06a931b2015-10-28 17:23:31 -07001025 productVariablePropertyValue.Addr().Interface(), nil)
1026 if err != nil {
1027 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1028 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1029 } else {
1030 panic(err)
1031 }
1032 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001033}
1034
Colin Cross7e0eaf12017-05-05 16:16:24 -07001035func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
1036 productVariablePropertyValue reflect.Value, i int, err error) {
1037
1038 field := productVariablePropertyValue.Type().Field(i).Name
1039 property := prefix + "." + proptools.PropertyNameForField(field)
1040 ctx.PropertyErrorf(property, "%s", err)
1041}
1042
1043func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
1044 productVariablePropertyValue reflect.Value, variableValue interface{}) {
1045
Colin Cross7f64b6d2015-07-09 13:57:48 -07001046 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
1047 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -08001048 kind := propertyValue.Kind()
1049 if kind == reflect.Ptr {
1050 if propertyValue.IsNil() {
1051 continue
1052 }
1053 propertyValue = propertyValue.Elem()
1054 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001055 switch propertyValue.Kind() {
1056 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001057 err := printfIntoProperty(propertyValue, variableValue)
1058 if err != nil {
1059 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1060 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001061 case reflect.Slice:
1062 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001063 err := printfIntoProperty(propertyValue.Index(j), variableValue)
1064 if err != nil {
1065 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
1066 }
Colin Cross7f64b6d2015-07-09 13:57:48 -07001067 }
Colin Crossdd0dbe62015-12-02 15:24:38 -08001068 case reflect.Bool:
1069 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001070 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001071 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001072 default:
1073 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
1074 }
1075 }
1076}
1077
Colin Cross7e0eaf12017-05-05 16:16:24 -07001078func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -07001079 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -07001080
1081 count := strings.Count(s, "%")
1082 if count == 0 {
1083 return nil
1084 }
1085
1086 if count > 1 {
1087 return fmt.Errorf("product variable properties only support a single '%%'")
1088 }
1089
Colin Cross7f64b6d2015-07-09 13:57:48 -07001090 if strings.Contains(s, "%d") {
1091 switch v := variableValue.(type) {
1092 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001093 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -07001094 case bool:
1095 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -07001096 variableValue = 1
1097 } else {
1098 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -07001099 }
1100 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -07001101 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -07001102 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001103 } else if strings.Contains(s, "%s") {
1104 switch variableValue.(type) {
1105 case string:
1106 // Nothing
1107 default:
1108 return fmt.Errorf("unsupported type %T for %%s", variableValue)
1109 }
1110 } else {
1111 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -07001112 }
Colin Cross7e0eaf12017-05-05 16:16:24 -07001113
1114 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
1115
1116 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -07001117}
Colin Cross18c46802019-09-24 22:19:02 -07001118
1119var variablePropTypeMap OncePer
1120
1121// sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
1122// reflect.Types of each property struct. The result can be used as a key in a map.
1123func sliceToTypeArray(s []interface{}) interface{} {
1124 // Create an array using reflection whose length is the length of the input slice
1125 ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
1126 for i, e := range s {
1127 ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
1128 }
1129 return ret.Interface()
1130}
1131
Colin Crosseabaedd2020-02-06 17:01:55 -08001132func initProductVariableModule(m Module) {
1133 base := m.base()
1134
1135 // Allow tests to override the default product variables
1136 if base.variableProperties == nil {
1137 base.variableProperties = defaultProductVariables
1138 }
1139 // Filter the product variables properties to the ones that exist on this module
1140 base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
1141 if base.variableProperties != nil {
1142 m.AddProperties(base.variableProperties)
1143 }
1144}
1145
Colin Cross18c46802019-09-24 22:19:02 -07001146// createVariableProperties takes the list of property structs for a module and returns a property struct that
1147// contains the product variable properties that exist in the property structs, or nil if there are none. It
1148// caches the result.
1149func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
1150 // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
1151 key := sliceToTypeArray(moduleTypeProps)
1152
1153 // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
1154 typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
1155 // Compute the filtered property struct type.
1156 return createVariablePropertiesType(moduleTypeProps, productVariables)
1157 }).(reflect.Type)
1158
1159 if typ == nil {
1160 return nil
1161 }
1162
1163 // Create a new pointer to a filtered property struct.
1164 return reflect.New(typ).Interface()
1165}
1166
1167// createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
1168// a list of property structs.
1169func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
1170 typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
1171 func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1172 // Filter function, returns true if the field should be in the resulting struct
1173 if prefix == "" {
1174 // Keep the top level Product_variables field
1175 return true, field
1176 }
1177 _, rest := splitPrefix(prefix)
1178 if rest == "" {
1179 // Keep the 2nd level field (i.e. Product_variables.Eng)
1180 return true, field
1181 }
1182
1183 // Strip off the first 2 levels of the prefix
1184 _, prefix = splitPrefix(rest)
1185
1186 for _, p := range moduleTypeProps {
1187 if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
1188 // Keep any fields that exist in one of the property structs
1189 return true, field
1190 }
1191 }
1192
1193 return false, field
1194 })
1195 return typ
1196}
1197
1198func splitPrefix(prefix string) (first, rest string) {
1199 index := strings.IndexByte(prefix, '.')
1200 if index == -1 {
1201 return prefix, ""
1202 }
1203 return prefix[:index], prefix[index+1:]
1204}
1205
1206func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
1207 if t.Kind() != reflect.Struct {
1208 panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
1209 }
1210
1211 if prefix != "" {
1212 split := strings.SplitN(prefix, ".", 2)
1213 firstPrefix := split[0]
1214 rest := ""
1215 if len(split) > 1 {
1216 rest = split[1]
1217 }
1218 f, exists := t.FieldByName(firstPrefix)
1219 if !exists {
1220 return false
1221 }
1222 ft := f.Type
1223 if ft.Kind() == reflect.Ptr {
1224 ft = ft.Elem()
1225 }
1226 if ft.Kind() != reflect.Struct {
1227 panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
1228 }
1229 return fieldExistsByNameRecursive(ft, rest, name)
1230 } else {
1231 _, exists := t.FieldByName(name)
1232 return exists
1233 }
1234}