blob: e8c2ef7e04da6585a6728e14a507e010ef2946fc [file] [log] [blame]
Cole Faustb85d1a12022-11-08 18:14:01 -08001package bp2build
2
3import (
Cole Faustf8231dd2023-04-21 17:37:11 -07004 "android/soong/android"
Cole Faust87c0c332023-07-31 12:10:12 -07005 "android/soong/android/soongconfig"
Cole Faustf8231dd2023-04-21 17:37:11 -07006 "android/soong/starlark_import"
7 "encoding/json"
Cole Faustb85d1a12022-11-08 18:14:01 -08008 "fmt"
9 "os"
10 "path/filepath"
Cole Faustf055db62023-07-24 15:17:03 -070011 "reflect"
Cole Faustb85d1a12022-11-08 18:14:01 -080012 "strings"
Cole Faustf8231dd2023-04-21 17:37:11 -070013
14 "github.com/google/blueprint/proptools"
15 "go.starlark.net/starlark"
Cole Faustb85d1a12022-11-08 18:14:01 -080016)
17
18func CreateProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070019 ctx *CodegenContext,
20 metrics CodegenMetrics) ([]BazelFile, []BazelFile, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080021 cfg := &ctx.config
22 targetProduct := "unknown"
23 if cfg.HasDeviceProduct() {
24 targetProduct = cfg.DeviceProduct()
25 }
26 targetBuildVariant := "user"
27 if cfg.Eng() {
28 targetBuildVariant = "eng"
29 } else if cfg.Debuggable() {
30 targetBuildVariant = "userdebug"
31 }
32
33 productVariablesFileName := cfg.ProductVariablesFileName
34 if !strings.HasPrefix(productVariablesFileName, "/") {
35 productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
36 }
Cole Faustf8231dd2023-04-21 17:37:11 -070037 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080038 if err != nil {
Cole Faustf8231dd2023-04-21 17:37:11 -070039 return nil, nil, err
40 }
41 productVariables := android.ProductVariables{}
42 err = json.Unmarshal(productVariablesBytes, &productVariables)
43 if err != nil {
44 return nil, nil, err
Cole Faustb85d1a12022-11-08 18:14:01 -080045 }
46
47 // TODO(b/249685973): the name is product_config_platforms because product_config
48 // was already used for other files. Deduplicate them.
49 currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant)
50
51 productReplacer := strings.NewReplacer(
52 "{PRODUCT}", targetProduct,
53 "{VARIANT}", targetBuildVariant,
54 "{PRODUCT_FOLDER}", currentProductFolder)
55
Cole Faust946d02c2023-08-03 16:08:09 -070056 platformMappingContent, err := platformMappingContent(
57 productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"),
58 &productVariables,
59 ctx.Config().Bp2buildSoongConfigDefinitions,
60 metrics.convertedModulePathMap)
Cole Faustf8231dd2023-04-21 17:37:11 -070061 if err != nil {
62 return nil, nil, err
63 }
64
Cole Faust946d02c2023-08-03 16:08:09 -070065 productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
66 if err != nil {
67 return nil, nil, err
68 }
69 productsForTesting := android.SortedKeys(productsForTestingMap)
70 for i := range productsForTesting {
71 productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i])
72 }
73
Cole Faustf8231dd2023-04-21 17:37:11 -070074 injectionDirFiles := []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -080075 newFile(
76 currentProductFolder,
77 "soong.variables.bzl",
Cole Faustf8231dd2023-04-21 17:37:11 -070078 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustb85d1a12022-11-08 18:14:01 -080079 newFile(
80 currentProductFolder,
81 "BUILD",
82 productReplacer.Replace(`
83package(default_visibility=[
84 "@soong_injection//product_config_platforms:__subpackages__",
85 "@//build/bazel/product_config:__subpackages__",
86])
87load(":soong.variables.bzl", _soong_variables = "variables")
Cole Faustbd249822023-03-24 16:03:43 -070088load("@//build/bazel/product_config:android_product.bzl", "android_product")
Cole Faustb85d1a12022-11-08 18:14:01 -080089
90android_product(
91 name = "{PRODUCT}-{VARIANT}",
92 soong_variables = _soong_variables,
93)
94`)),
95 newFile(
96 "product_config_platforms",
97 "BUILD.bazel",
98 productReplacer.Replace(`
99package(default_visibility = [
100 "@//build/bazel/product_config:__subpackages__",
101 "@soong_injection//product_config_platforms:__subpackages__",
102])
Jingwen Chen583ab212023-05-30 09:45:23 +0000103
104load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
105load("@//build/bazel/product_config:android_product.bzl", "android_product")
106
Cole Faust319abae2023-06-06 15:12:49 -0700107# Bazel will qualify its outputs by the platform name. When switching between products, this
108# means that soong-built files that depend on bazel-built files will suddenly get different
109# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
110# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
111# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000112android_product(
Cole Faust319abae2023-06-06 15:12:49 -0700113 name = "mixed_builds_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +0000114 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000115 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000116)
Cole Faust117bb742023-03-29 14:46:20 -0700117`)),
118 newFile(
119 "product_config_platforms",
120 "product_labels.bzl",
121 productReplacer.Replace(`
122# This file keeps a list of all the products in the android source tree, because they're
123# discovered as part of a preprocessing step before bazel runs.
124# TODO: When we start generating the platforms for more than just the
125# currently lunched product, they should all be listed here
126product_labels = [
Cole Faust319abae2023-06-06 15:12:49 -0700127 "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700128 "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}",
129`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800130 newFile(
131 "product_config_platforms",
132 "common.bazelrc",
133 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700134build --platform_mappings=platform_mappings
Cole Faust319abae2023-06-06 15:12:49 -0700135build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800136
Cole Faust319abae2023-06-06 15:12:49 -0700137build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
Cole Faustd1acaa42023-08-03 17:04:03 -0700138build:linux_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86
Cole Faust319abae2023-06-06 15:12:49 -0700139build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
140build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
141build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
142build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800143`)),
144 newFile(
145 "product_config_platforms",
146 "linux.bazelrc",
147 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700148build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800149`)),
150 newFile(
151 "product_config_platforms",
152 "darwin.bazelrc",
153 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700154build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800155`)),
156 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700157 bp2buildDirFiles := []BazelFile{
158 newFile(
159 "",
160 "platform_mappings",
161 platformMappingContent),
162 }
163 return injectionDirFiles, bp2buildDirFiles, nil
164}
Cole Faustb85d1a12022-11-08 18:14:01 -0800165
Cole Faust946d02c2023-08-03 16:08:09 -0700166func platformMappingContent(
167 mainProductLabel string,
168 mainProductVariables *android.ProductVariables,
169 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
170 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700171 productsForTesting, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
172 if err != nil {
173 return "", err
174 }
Cole Faustf055db62023-07-24 15:17:03 -0700175 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700176
177 mergedConvertedModulePathMap := make(map[string]string)
178 for k, v := range convertedModulePathMap {
179 mergedConvertedModulePathMap[k] = v
180 }
181 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
182 if err != nil {
183 return "", err
184 }
185 for k, v := range additionalModuleNamesToPackages {
186 mergedConvertedModulePathMap[k] = v
187 }
188
Cole Faustf055db62023-07-24 15:17:03 -0700189 result.WriteString("platforms:\n")
Cole Faust946d02c2023-08-03 16:08:09 -0700190 platformMappingSingleProduct(mainProductLabel, mainProductVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700191 for product, productVariablesStarlark := range productsForTesting {
192 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
193 if err != nil {
194 return "", err
195 }
Cole Faust946d02c2023-08-03 16:08:09 -0700196 platformMappingSingleProduct("@//build/bazel/tests/products:"+product, &productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700197 }
Cole Faustf055db62023-07-24 15:17:03 -0700198 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700199}
200
Cole Faust88c8efb2023-07-18 11:05:16 -0700201var bazelPlatformSuffixes = []string{
202 "",
203 "_darwin_arm64",
204 "_darwin_x86_64",
205 "_linux_bionic_arm64",
206 "_linux_bionic_x86_64",
207 "_linux_musl_x86",
208 "_linux_musl_x86_64",
209 "_linux_x86",
210 "_linux_x86_64",
211 "_windows_x86",
212 "_windows_x86_64",
213}
214
Cole Faust946d02c2023-08-03 16:08:09 -0700215func platformMappingSingleProduct(
216 label string,
217 productVariables *android.ProductVariables,
218 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
219 convertedModulePathMap map[string]string,
220 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700221 targetBuildVariant := "user"
222 if proptools.Bool(productVariables.Eng) {
223 targetBuildVariant = "eng"
224 } else if proptools.Bool(productVariables.Debuggable) {
225 targetBuildVariant = "userdebug"
Cole Faustf8231dd2023-04-21 17:37:11 -0700226 }
Cole Faustf055db62023-07-24 15:17:03 -0700227
Cole Faust95c5cf82023-08-03 13:49:27 -0700228 platform_sdk_version := -1
229 if productVariables.Platform_sdk_version != nil {
230 platform_sdk_version = *productVariables.Platform_sdk_version
231 }
232
Cole Faust946d02c2023-08-03 16:08:09 -0700233 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
234 if proptools.String(productVariables.DefaultAppCertificate) != "" {
235 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":android_certificate_directory"
236 }
237
Cole Faustf055db62023-07-24 15:17:03 -0700238 for _, suffix := range bazelPlatformSuffixes {
239 result.WriteString(" ")
240 result.WriteString(label)
241 result.WriteString(suffix)
242 result.WriteString("\n")
243 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 -0700244 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700245 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 -0700246 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
247 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 -0700248 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
249 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700250 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
251 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
252 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
Cole Faust87c0c332023-07-31 12:10:12 -0700253 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:debuggable=%t\n", proptools.Bool(productVariables.Debuggable)))
Cole Faustf055db62023-07-24 15:17:03 -0700254 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700255 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700256 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
257 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
258 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700259 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 -0700260 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
261 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700262 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
263 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:eng=%t\n", proptools.Bool(productVariables.Eng)))
264 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
265 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
266 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_zero_contents=%t\n", proptools.Bool(productVariables.Malloc_zero_contents)))
Cole Faustf055db62023-07-24 15:17:03 -0700267 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 -0700268 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Cole Faustf055db62023-07-24 15:17:03 -0700269 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
270 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
271 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Cole Faust95c5cf82023-08-03 13:49:27 -0700272 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700273 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faustf055db62023-07-24 15:17:03 -0700274 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:target_build_variant=%s\n", targetBuildVariant))
Cole Faust87c0c332023-07-31 12:10:12 -0700275 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 -0700276 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700277 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700278 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
279 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 -0700280
281 for _, override := range productVariables.CertificateOverrides {
282 parts := strings.SplitN(override, ":", 2)
283 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
284 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
285 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
286 }
287 }
288 }
289
Cole Faust87c0c332023-07-31 12:10:12 -0700290 for namespace, namespaceContents := range productVariables.VendorVars {
291 for variable, value := range namespaceContents {
292 key := namespace + "__" + variable
293 _, hasBool := soongConfigDefinitions.BoolVars[key]
294 _, hasString := soongConfigDefinitions.StringVars[key]
295 _, hasValue := soongConfigDefinitions.ValueVars[key]
296 if !hasBool && !hasString && !hasValue {
297 // Not all soong config variables are defined in Android.bp files. For example,
298 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
299 // way, that causes them to be present in the soong.variables file but not
300 // defined in an Android.bp file. There's also nothing stopping you from setting
301 // a variable in make that doesn't exist in soong. We only generate build
302 // settings for the ones that exist in soong, so skip all others.
303 continue
304 }
305 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
306 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))
307 }
308 if hasBool {
309 // Logic copied from soongConfig.Bool()
310 value = strings.ToLower(value)
311 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
312 value = "true"
313 } else {
314 value = "false"
315 }
316 }
317 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
318 }
319 }
Cole Faustf055db62023-07-24 15:17:03 -0700320 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700321}
322
323func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700324 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700325 productVarsReflect := reflect.ValueOf(&result).Elem()
326 for i := 0; i < productVarsReflect.NumField(); i++ {
327 field := productVarsReflect.Field(i)
328 fieldType := productVarsReflect.Type().Field(i)
329 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700330 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
331 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700332 // These variables have more complicated types, and we don't need them right now
333 continue
334 }
335 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700336 if name == "VendorVars" {
337 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
338 if err != nil {
339 return result, err
340 }
341 field.Set(reflect.ValueOf(vendorVars))
342 continue
343 }
Cole Faustf055db62023-07-24 15:17:03 -0700344 switch field.Type().Kind() {
345 case reflect.Bool:
346 val, err := starlark_import.Unmarshal[bool](in[name])
347 if err != nil {
348 return result, err
349 }
350 field.SetBool(val)
351 case reflect.String:
352 val, err := starlark_import.Unmarshal[string](in[name])
353 if err != nil {
354 return result, err
355 }
356 field.SetString(val)
357 case reflect.Slice:
358 if field.Type().Elem().Kind() != reflect.String {
359 return result, fmt.Errorf("slices of types other than strings are unimplemented")
360 }
361 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
362 if err != nil {
363 return result, err
364 }
365 field.Set(val)
366 case reflect.Pointer:
367 switch field.Type().Elem().Kind() {
368 case reflect.Bool:
369 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
370 if err != nil {
371 return result, err
372 }
373 field.Set(reflect.ValueOf(val))
374 case reflect.String:
375 val, err := starlark_import.UnmarshalNoneable[string](in[name])
376 if err != nil {
377 return result, err
378 }
379 field.Set(reflect.ValueOf(val))
380 case reflect.Int:
381 val, err := starlark_import.UnmarshalNoneable[int](in[name])
382 if err != nil {
383 return result, err
384 }
385 field.Set(reflect.ValueOf(val))
386 default:
387 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
388 }
389 default:
390 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
391 }
392 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700393 }
Cole Faustf055db62023-07-24 15:17:03 -0700394
Cole Faust87c0c332023-07-31 12:10:12 -0700395 result.Native_coverage = proptools.BoolPtr(
396 proptools.Bool(result.GcovCoverage) ||
397 proptools.Bool(result.ClangCoverage))
398
Cole Faustb85d1a12022-11-08 18:14:01 -0800399 return result, nil
400}