blob: 5edcdb721e685c1a5d7c1f5d3fb67f2ae49cf128 [file] [log] [blame]
Cole Faustb85d1a12022-11-08 18:14:01 -08001package bp2build
2
3import (
Cole Faustf8231dd2023-04-21 17:37:11 -07004 "encoding/json"
Cole Faustb85d1a12022-11-08 18:14:01 -08005 "fmt"
6 "os"
7 "path/filepath"
Cole Faustf055db62023-07-24 15:17:03 -07008 "reflect"
Cole Faustb85d1a12022-11-08 18:14:01 -08009 "strings"
Cole Faustf8231dd2023-04-21 17:37:11 -070010
Yu Liub6a15da2023-08-31 14:14:01 -070011 "android/soong/android"
12 "android/soong/android/soongconfig"
13 "android/soong/starlark_import"
14
Cole Faustf8231dd2023-04-21 17:37:11 -070015 "github.com/google/blueprint/proptools"
16 "go.starlark.net/starlark"
Cole Faustb85d1a12022-11-08 18:14:01 -080017)
18
19func CreateProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070020 ctx *CodegenContext,
21 metrics CodegenMetrics) ([]BazelFile, []BazelFile, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080022 cfg := &ctx.config
23 targetProduct := "unknown"
24 if cfg.HasDeviceProduct() {
25 targetProduct = cfg.DeviceProduct()
26 }
27 targetBuildVariant := "user"
28 if cfg.Eng() {
29 targetBuildVariant = "eng"
30 } else if cfg.Debuggable() {
31 targetBuildVariant = "userdebug"
32 }
33
34 productVariablesFileName := cfg.ProductVariablesFileName
35 if !strings.HasPrefix(productVariablesFileName, "/") {
36 productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
37 }
Cole Faustf8231dd2023-04-21 17:37:11 -070038 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080039 if err != nil {
Cole Faustf8231dd2023-04-21 17:37:11 -070040 return nil, nil, err
41 }
42 productVariables := android.ProductVariables{}
43 err = json.Unmarshal(productVariablesBytes, &productVariables)
44 if err != nil {
45 return nil, nil, err
Cole Faustb85d1a12022-11-08 18:14:01 -080046 }
47
48 // TODO(b/249685973): the name is product_config_platforms because product_config
49 // was already used for other files. Deduplicate them.
50 currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant)
51
52 productReplacer := strings.NewReplacer(
53 "{PRODUCT}", targetProduct,
54 "{VARIANT}", targetBuildVariant,
55 "{PRODUCT_FOLDER}", currentProductFolder)
56
Cole Faust946d02c2023-08-03 16:08:09 -070057 platformMappingContent, err := platformMappingContent(
58 productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"),
59 &productVariables,
60 ctx.Config().Bp2buildSoongConfigDefinitions,
61 metrics.convertedModulePathMap)
Cole Faustf8231dd2023-04-21 17:37:11 -070062 if err != nil {
63 return nil, nil, err
64 }
65
Cole Faust946d02c2023-08-03 16:08:09 -070066 productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
67 if err != nil {
68 return nil, nil, err
69 }
70 productsForTesting := android.SortedKeys(productsForTestingMap)
71 for i := range productsForTesting {
72 productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i])
73 }
74
Cole Faustf8231dd2023-04-21 17:37:11 -070075 injectionDirFiles := []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -080076 newFile(
77 currentProductFolder,
78 "soong.variables.bzl",
Cole Faustf8231dd2023-04-21 17:37:11 -070079 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustb85d1a12022-11-08 18:14:01 -080080 newFile(
81 currentProductFolder,
82 "BUILD",
83 productReplacer.Replace(`
84package(default_visibility=[
85 "@soong_injection//product_config_platforms:__subpackages__",
86 "@//build/bazel/product_config:__subpackages__",
87])
88load(":soong.variables.bzl", _soong_variables = "variables")
Cole Faustbd249822023-03-24 16:03:43 -070089load("@//build/bazel/product_config:android_product.bzl", "android_product")
Cole Faustb85d1a12022-11-08 18:14:01 -080090
91android_product(
92 name = "{PRODUCT}-{VARIANT}",
93 soong_variables = _soong_variables,
94)
95`)),
96 newFile(
97 "product_config_platforms",
98 "BUILD.bazel",
99 productReplacer.Replace(`
100package(default_visibility = [
101 "@//build/bazel/product_config:__subpackages__",
102 "@soong_injection//product_config_platforms:__subpackages__",
103])
Jingwen Chen583ab212023-05-30 09:45:23 +0000104
105load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
106load("@//build/bazel/product_config:android_product.bzl", "android_product")
107
Cole Faust319abae2023-06-06 15:12:49 -0700108# Bazel will qualify its outputs by the platform name. When switching between products, this
109# means that soong-built files that depend on bazel-built files will suddenly get different
110# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
111# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
112# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000113android_product(
Cole Faust319abae2023-06-06 15:12:49 -0700114 name = "mixed_builds_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +0000115 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000116 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000117)
Cole Faust117bb742023-03-29 14:46:20 -0700118`)),
119 newFile(
120 "product_config_platforms",
121 "product_labels.bzl",
122 productReplacer.Replace(`
123# This file keeps a list of all the products in the android source tree, because they're
124# discovered as part of a preprocessing step before bazel runs.
125# TODO: When we start generating the platforms for more than just the
126# currently lunched product, they should all be listed here
127product_labels = [
Cole Faust319abae2023-06-06 15:12:49 -0700128 "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700129 "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}",
130`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800131 newFile(
132 "product_config_platforms",
133 "common.bazelrc",
134 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700135build --platform_mappings=platform_mappings
Cole Faust319abae2023-06-06 15:12:49 -0700136build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800137
Cole Faust319abae2023-06-06 15:12:49 -0700138build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
Cole Faustd1acaa42023-08-03 17:04:03 -0700139build:linux_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86
Cole Faust319abae2023-06-06 15:12:49 -0700140build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
141build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
142build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
143build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800144`)),
145 newFile(
146 "product_config_platforms",
147 "linux.bazelrc",
148 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700149build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800150`)),
151 newFile(
152 "product_config_platforms",
153 "darwin.bazelrc",
154 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700155build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800156`)),
157 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700158 bp2buildDirFiles := []BazelFile{
159 newFile(
160 "",
161 "platform_mappings",
162 platformMappingContent),
163 }
164 return injectionDirFiles, bp2buildDirFiles, nil
165}
Cole Faustb85d1a12022-11-08 18:14:01 -0800166
Cole Faust946d02c2023-08-03 16:08:09 -0700167func platformMappingContent(
168 mainProductLabel string,
169 mainProductVariables *android.ProductVariables,
170 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
171 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700172 productsForTesting, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
173 if err != nil {
174 return "", err
175 }
Cole Faustf055db62023-07-24 15:17:03 -0700176 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700177
178 mergedConvertedModulePathMap := make(map[string]string)
179 for k, v := range convertedModulePathMap {
180 mergedConvertedModulePathMap[k] = v
181 }
182 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
183 if err != nil {
184 return "", err
185 }
186 for k, v := range additionalModuleNamesToPackages {
187 mergedConvertedModulePathMap[k] = v
188 }
189
Cole Faustf055db62023-07-24 15:17:03 -0700190 result.WriteString("platforms:\n")
Cole Faust946d02c2023-08-03 16:08:09 -0700191 platformMappingSingleProduct(mainProductLabel, mainProductVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700192 for product, productVariablesStarlark := range productsForTesting {
193 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
194 if err != nil {
195 return "", err
196 }
Cole Faust946d02c2023-08-03 16:08:09 -0700197 platformMappingSingleProduct("@//build/bazel/tests/products:"+product, &productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700198 }
Cole Faustf055db62023-07-24 15:17:03 -0700199 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700200}
201
Cole Faust88c8efb2023-07-18 11:05:16 -0700202var bazelPlatformSuffixes = []string{
203 "",
204 "_darwin_arm64",
205 "_darwin_x86_64",
206 "_linux_bionic_arm64",
207 "_linux_bionic_x86_64",
208 "_linux_musl_x86",
209 "_linux_musl_x86_64",
210 "_linux_x86",
211 "_linux_x86_64",
212 "_windows_x86",
213 "_windows_x86_64",
214}
215
Cole Faust946d02c2023-08-03 16:08:09 -0700216func platformMappingSingleProduct(
217 label string,
218 productVariables *android.ProductVariables,
219 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
220 convertedModulePathMap map[string]string,
221 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700222 targetBuildVariant := "user"
223 if proptools.Bool(productVariables.Eng) {
224 targetBuildVariant = "eng"
225 } else if proptools.Bool(productVariables.Debuggable) {
226 targetBuildVariant = "userdebug"
Cole Faustf8231dd2023-04-21 17:37:11 -0700227 }
Cole Faustf055db62023-07-24 15:17:03 -0700228
Cole Faust95c5cf82023-08-03 13:49:27 -0700229 platform_sdk_version := -1
230 if productVariables.Platform_sdk_version != nil {
231 platform_sdk_version = *productVariables.Platform_sdk_version
232 }
233
Cole Faust946d02c2023-08-03 16:08:09 -0700234 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
235 if proptools.String(productVariables.DefaultAppCertificate) != "" {
236 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":android_certificate_directory"
237 }
238
Cole Faustf055db62023-07-24 15:17:03 -0700239 for _, suffix := range bazelPlatformSuffixes {
240 result.WriteString(" ")
241 result.WriteString(label)
242 result.WriteString(suffix)
243 result.WriteString("\n")
244 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:always_use_prebuilt_sdks=%t\n", proptools.Bool(productVariables.Always_use_prebuilt_sdks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700245 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700246 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:apex_global_min_sdk_version_override=%s\n", proptools.String(productVariables.ApexGlobalMinSdkVersionOverride)))
Cole Faust87c0c332023-07-31 12:10:12 -0700247 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
248 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_from_text_stub=%t\n", proptools.Bool(productVariables.Build_from_text_stub)))
Cole Faustf055db62023-07-24 15:17:03 -0700249 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
250 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700251 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
252 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
253 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
Cole Faust87c0c332023-07-31 12:10:12 -0700254 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:debuggable=%t\n", proptools.Bool(productVariables.Debuggable)))
Cole Faustf055db62023-07-24 15:17:03 -0700255 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700256 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700257 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
258 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
259 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700260 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_page_size_agnostic=%t\n", proptools.Bool(productVariables.DevicePageSizeAgnostic)))
Cole Faustf055db62023-07-24 15:17:03 -0700261 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
262 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700263 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
264 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:eng=%t\n", proptools.Bool(productVariables.Eng)))
265 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
266 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
267 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_zero_contents=%t\n", proptools.Bool(productVariables.Malloc_zero_contents)))
Yu Liub6a15da2023-08-31 14:14:01 -0700268 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
269 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
270 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_sync_include_paths=%s\n", strings.Join(productVariables.MemtagHeapSyncIncludePaths, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700271 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:manifest_package_name_overrides=%s\n", strings.Join(productVariables.ManifestPackageNameOverrides, ",")))
Cole Faust87c0c332023-07-31 12:10:12 -0700272 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Cole Faustf055db62023-07-24 15:17:03 -0700273 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
274 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
275 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Cole Faust95c5cf82023-08-03 13:49:27 -0700276 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700277 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faustf055db62023-07-24 15:17:03 -0700278 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:target_build_variant=%s\n", targetBuildVariant))
Cole Faust87c0c332023-07-31 12:10:12 -0700279 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:treble_linker_namespaces=%t\n", proptools.Bool(productVariables.Treble_linker_namespaces)))
Cole Faustf055db62023-07-24 15:17:03 -0700280 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700281 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700282 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
283 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build_apps=%s\n", strings.Join(productVariables.Unbundled_build_apps, ",")))
Cole Faust946d02c2023-08-03 16:08:09 -0700284
285 for _, override := range productVariables.CertificateOverrides {
286 parts := strings.SplitN(override, ":", 2)
287 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
288 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
289 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
290 }
291 }
292 }
293
Cole Faust87c0c332023-07-31 12:10:12 -0700294 for namespace, namespaceContents := range productVariables.VendorVars {
295 for variable, value := range namespaceContents {
296 key := namespace + "__" + variable
297 _, hasBool := soongConfigDefinitions.BoolVars[key]
298 _, hasString := soongConfigDefinitions.StringVars[key]
299 _, hasValue := soongConfigDefinitions.ValueVars[key]
300 if !hasBool && !hasString && !hasValue {
301 // Not all soong config variables are defined in Android.bp files. For example,
302 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
303 // way, that causes them to be present in the soong.variables file but not
304 // defined in an Android.bp file. There's also nothing stopping you from setting
305 // a variable in make that doesn't exist in soong. We only generate build
306 // settings for the ones that exist in soong, so skip all others.
307 continue
308 }
309 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
310 panic(fmt.Sprintf("Soong config variable %s:%s appears to be of multiple types. bool? %t, string? %t, value? %t", namespace, variable, hasBool, hasString, hasValue))
311 }
312 if hasBool {
313 // Logic copied from soongConfig.Bool()
314 value = strings.ToLower(value)
315 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
316 value = "true"
317 } else {
318 value = "false"
319 }
320 }
321 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
322 }
323 }
Cole Faustf055db62023-07-24 15:17:03 -0700324 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700325}
326
327func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700328 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700329 productVarsReflect := reflect.ValueOf(&result).Elem()
330 for i := 0; i < productVarsReflect.NumField(); i++ {
331 field := productVarsReflect.Field(i)
332 fieldType := productVarsReflect.Type().Field(i)
333 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700334 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
335 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700336 // These variables have more complicated types, and we don't need them right now
337 continue
338 }
339 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700340 if name == "VendorVars" {
341 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
342 if err != nil {
343 return result, err
344 }
345 field.Set(reflect.ValueOf(vendorVars))
346 continue
347 }
Cole Faustf055db62023-07-24 15:17:03 -0700348 switch field.Type().Kind() {
349 case reflect.Bool:
350 val, err := starlark_import.Unmarshal[bool](in[name])
351 if err != nil {
352 return result, err
353 }
354 field.SetBool(val)
355 case reflect.String:
356 val, err := starlark_import.Unmarshal[string](in[name])
357 if err != nil {
358 return result, err
359 }
360 field.SetString(val)
361 case reflect.Slice:
362 if field.Type().Elem().Kind() != reflect.String {
363 return result, fmt.Errorf("slices of types other than strings are unimplemented")
364 }
365 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
366 if err != nil {
367 return result, err
368 }
369 field.Set(val)
370 case reflect.Pointer:
371 switch field.Type().Elem().Kind() {
372 case reflect.Bool:
373 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
374 if err != nil {
375 return result, err
376 }
377 field.Set(reflect.ValueOf(val))
378 case reflect.String:
379 val, err := starlark_import.UnmarshalNoneable[string](in[name])
380 if err != nil {
381 return result, err
382 }
383 field.Set(reflect.ValueOf(val))
384 case reflect.Int:
385 val, err := starlark_import.UnmarshalNoneable[int](in[name])
386 if err != nil {
387 return result, err
388 }
389 field.Set(reflect.ValueOf(val))
390 default:
391 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
392 }
393 default:
394 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
395 }
396 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700397 }
Cole Faustf055db62023-07-24 15:17:03 -0700398
Cole Faust87c0c332023-07-31 12:10:12 -0700399 result.Native_coverage = proptools.BoolPtr(
400 proptools.Bool(result.GcovCoverage) ||
401 proptools.Bool(result.ClangCoverage))
402
Cole Faustb85d1a12022-11-08 18:14:01 -0800403 return result, nil
404}