blob: 20355d736bc7f9a0cb380d42af81c369b413f7a1 [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
Spandan Das3645a622023-09-07 22:32:25 +000015 "github.com/google/blueprint"
Cole Faustf8231dd2023-04-21 17:37:11 -070016 "github.com/google/blueprint/proptools"
17 "go.starlark.net/starlark"
Cole Faustb85d1a12022-11-08 18:14:01 -080018)
19
20func CreateProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070021 ctx *CodegenContext,
22 metrics CodegenMetrics) ([]BazelFile, []BazelFile, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080023 cfg := &ctx.config
24 targetProduct := "unknown"
25 if cfg.HasDeviceProduct() {
26 targetProduct = cfg.DeviceProduct()
27 }
28 targetBuildVariant := "user"
29 if cfg.Eng() {
30 targetBuildVariant = "eng"
31 } else if cfg.Debuggable() {
32 targetBuildVariant = "userdebug"
33 }
34
35 productVariablesFileName := cfg.ProductVariablesFileName
36 if !strings.HasPrefix(productVariablesFileName, "/") {
37 productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
38 }
Cole Faustf8231dd2023-04-21 17:37:11 -070039 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080040 if err != nil {
Cole Faustf8231dd2023-04-21 17:37:11 -070041 return nil, nil, err
42 }
43 productVariables := android.ProductVariables{}
44 err = json.Unmarshal(productVariablesBytes, &productVariables)
45 if err != nil {
46 return nil, nil, err
Cole Faustb85d1a12022-11-08 18:14:01 -080047 }
48
Spandan Das3645a622023-09-07 22:32:25 +000049 // Visit all modules to determine the list of ndk libraries
50 // This list will be used to add additional flags for cc stub generation
51 ndkLibsStringFormatted := []string{}
52 ctx.Context().VisitAllModules(func(m blueprint.Module) {
53 if ctx.Context().ModuleType(m) == "ndk_library" {
54 ndkLibsStringFormatted = append(ndkLibsStringFormatted, fmt.Sprintf(`"%s"`, m.Name())) // name will be `"libc.ndk"`
55 }
56 })
57
Cole Faustb85d1a12022-11-08 18:14:01 -080058 // TODO(b/249685973): the name is product_config_platforms because product_config
59 // was already used for other files. Deduplicate them.
60 currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant)
61
62 productReplacer := strings.NewReplacer(
63 "{PRODUCT}", targetProduct,
64 "{VARIANT}", targetBuildVariant,
65 "{PRODUCT_FOLDER}", currentProductFolder)
66
Cole Faust946d02c2023-08-03 16:08:09 -070067 platformMappingContent, err := platformMappingContent(
68 productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"),
69 &productVariables,
70 ctx.Config().Bp2buildSoongConfigDefinitions,
71 metrics.convertedModulePathMap)
Cole Faustf8231dd2023-04-21 17:37:11 -070072 if err != nil {
73 return nil, nil, err
74 }
75
Cole Faust946d02c2023-08-03 16:08:09 -070076 productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
77 if err != nil {
78 return nil, nil, err
79 }
80 productsForTesting := android.SortedKeys(productsForTestingMap)
81 for i := range productsForTesting {
82 productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i])
83 }
84
Cole Faustf8231dd2023-04-21 17:37:11 -070085 injectionDirFiles := []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -080086 newFile(
87 currentProductFolder,
88 "soong.variables.bzl",
Cole Faustf8231dd2023-04-21 17:37:11 -070089 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustb85d1a12022-11-08 18:14:01 -080090 newFile(
91 currentProductFolder,
92 "BUILD",
93 productReplacer.Replace(`
94package(default_visibility=[
95 "@soong_injection//product_config_platforms:__subpackages__",
96 "@//build/bazel/product_config:__subpackages__",
97])
98load(":soong.variables.bzl", _soong_variables = "variables")
Cole Faustbd249822023-03-24 16:03:43 -070099load("@//build/bazel/product_config:android_product.bzl", "android_product")
Cole Faustb85d1a12022-11-08 18:14:01 -0800100
101android_product(
102 name = "{PRODUCT}-{VARIANT}",
103 soong_variables = _soong_variables,
104)
105`)),
106 newFile(
107 "product_config_platforms",
108 "BUILD.bazel",
109 productReplacer.Replace(`
110package(default_visibility = [
111 "@//build/bazel/product_config:__subpackages__",
112 "@soong_injection//product_config_platforms:__subpackages__",
113])
Jingwen Chen583ab212023-05-30 09:45:23 +0000114
115load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
116load("@//build/bazel/product_config:android_product.bzl", "android_product")
117
Cole Faust319abae2023-06-06 15:12:49 -0700118# Bazel will qualify its outputs by the platform name. When switching between products, this
119# means that soong-built files that depend on bazel-built files will suddenly get different
120# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
121# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
122# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000123android_product(
Cole Faust319abae2023-06-06 15:12:49 -0700124 name = "mixed_builds_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +0000125 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000126 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000127)
Cole Faust117bb742023-03-29 14:46:20 -0700128`)),
129 newFile(
130 "product_config_platforms",
131 "product_labels.bzl",
132 productReplacer.Replace(`
133# This file keeps a list of all the products in the android source tree, because they're
134# discovered as part of a preprocessing step before bazel runs.
135# TODO: When we start generating the platforms for more than just the
136# currently lunched product, they should all be listed here
137product_labels = [
Cole Faust319abae2023-06-06 15:12:49 -0700138 "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700139 "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}",
140`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800141 newFile(
142 "product_config_platforms",
143 "common.bazelrc",
144 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700145build --platform_mappings=platform_mappings
Cole Faust319abae2023-06-06 15:12:49 -0700146build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800147
Cole Faust319abae2023-06-06 15:12:49 -0700148build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
Cole Faustd1acaa42023-08-03 17:04:03 -0700149build:linux_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86
Cole Faust319abae2023-06-06 15:12:49 -0700150build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
151build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
152build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
153build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800154`)),
155 newFile(
156 "product_config_platforms",
157 "linux.bazelrc",
158 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700159build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800160`)),
161 newFile(
162 "product_config_platforms",
163 "darwin.bazelrc",
164 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700165build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800166`)),
Spandan Das3645a622023-09-07 22:32:25 +0000167 newFile(
168 "cc_toolchain",
169 "ndk_libs.bzl",
170 fmt.Sprintf("ndk_libs = [%v]", strings.Join(ndkLibsStringFormatted, ", ")),
171 ),
Cole Faustb85d1a12022-11-08 18:14:01 -0800172 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700173 bp2buildDirFiles := []BazelFile{
174 newFile(
175 "",
176 "platform_mappings",
177 platformMappingContent),
178 }
179 return injectionDirFiles, bp2buildDirFiles, nil
180}
Cole Faustb85d1a12022-11-08 18:14:01 -0800181
Cole Faust946d02c2023-08-03 16:08:09 -0700182func platformMappingContent(
183 mainProductLabel string,
184 mainProductVariables *android.ProductVariables,
185 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
186 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700187 productsForTesting, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
188 if err != nil {
189 return "", err
190 }
Cole Faustf055db62023-07-24 15:17:03 -0700191 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700192
193 mergedConvertedModulePathMap := make(map[string]string)
194 for k, v := range convertedModulePathMap {
195 mergedConvertedModulePathMap[k] = v
196 }
197 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
198 if err != nil {
199 return "", err
200 }
201 for k, v := range additionalModuleNamesToPackages {
202 mergedConvertedModulePathMap[k] = v
203 }
204
Cole Faustf055db62023-07-24 15:17:03 -0700205 result.WriteString("platforms:\n")
Cole Faust946d02c2023-08-03 16:08:09 -0700206 platformMappingSingleProduct(mainProductLabel, mainProductVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700207 for product, productVariablesStarlark := range productsForTesting {
208 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
209 if err != nil {
210 return "", err
211 }
Cole Faust946d02c2023-08-03 16:08:09 -0700212 platformMappingSingleProduct("@//build/bazel/tests/products:"+product, &productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700213 }
Cole Faustf055db62023-07-24 15:17:03 -0700214 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700215}
216
Cole Faust88c8efb2023-07-18 11:05:16 -0700217var bazelPlatformSuffixes = []string{
218 "",
219 "_darwin_arm64",
220 "_darwin_x86_64",
221 "_linux_bionic_arm64",
222 "_linux_bionic_x86_64",
223 "_linux_musl_x86",
224 "_linux_musl_x86_64",
225 "_linux_x86",
226 "_linux_x86_64",
227 "_windows_x86",
228 "_windows_x86_64",
229}
230
Cole Faust946d02c2023-08-03 16:08:09 -0700231func platformMappingSingleProduct(
232 label string,
233 productVariables *android.ProductVariables,
234 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
235 convertedModulePathMap map[string]string,
236 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700237 targetBuildVariant := "user"
238 if proptools.Bool(productVariables.Eng) {
239 targetBuildVariant = "eng"
240 } else if proptools.Bool(productVariables.Debuggable) {
241 targetBuildVariant = "userdebug"
Cole Faustf8231dd2023-04-21 17:37:11 -0700242 }
Cole Faustf055db62023-07-24 15:17:03 -0700243
Cole Faust95c5cf82023-08-03 13:49:27 -0700244 platform_sdk_version := -1
245 if productVariables.Platform_sdk_version != nil {
246 platform_sdk_version = *productVariables.Platform_sdk_version
247 }
248
Cole Faust946d02c2023-08-03 16:08:09 -0700249 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
250 if proptools.String(productVariables.DefaultAppCertificate) != "" {
251 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":android_certificate_directory"
252 }
253
Cole Faustf055db62023-07-24 15:17:03 -0700254 for _, suffix := range bazelPlatformSuffixes {
255 result.WriteString(" ")
256 result.WriteString(label)
257 result.WriteString(suffix)
258 result.WriteString("\n")
259 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 -0700260 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700261 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 -0700262 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
263 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_from_text_stub=%t\n", proptools.Bool(productVariables.Build_from_text_stub)))
Cole Faustded79602023-09-05 17:48:11 -0700264 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages))
Cole Faustf055db62023-07-24 15:17:03 -0700265 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
266 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700267 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
268 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
269 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
Cole Faust87c0c332023-07-31 12:10:12 -0700270 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:debuggable=%t\n", proptools.Bool(productVariables.Debuggable)))
Cole Faustf055db62023-07-24 15:17:03 -0700271 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700272 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700273 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
274 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
275 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700276 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 -0700277 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
Cole Faust48ce1372023-09-05 16:07:49 -0700278 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label))
Cole Faustf055db62023-07-24 15:17:03 -0700279 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700280 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
281 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:eng=%t\n", proptools.Bool(productVariables.Eng)))
282 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
283 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
284 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 -0700285 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
286 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
287 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 -0700288 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 -0700289 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Cole Faustf055db62023-07-24 15:17:03 -0700290 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
291 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
292 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Cole Faust95c5cf82023-08-03 13:49:27 -0700293 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700294 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faustf055db62023-07-24 15:17:03 -0700295 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:target_build_variant=%s\n", targetBuildVariant))
Cole Faust87c0c332023-07-31 12:10:12 -0700296 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 -0700297 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700298 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700299 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
300 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 -0700301
302 for _, override := range productVariables.CertificateOverrides {
303 parts := strings.SplitN(override, ":", 2)
304 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
305 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
306 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
307 }
308 }
309 }
310
Cole Faust87c0c332023-07-31 12:10:12 -0700311 for namespace, namespaceContents := range productVariables.VendorVars {
312 for variable, value := range namespaceContents {
313 key := namespace + "__" + variable
314 _, hasBool := soongConfigDefinitions.BoolVars[key]
315 _, hasString := soongConfigDefinitions.StringVars[key]
316 _, hasValue := soongConfigDefinitions.ValueVars[key]
317 if !hasBool && !hasString && !hasValue {
318 // Not all soong config variables are defined in Android.bp files. For example,
319 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
320 // way, that causes them to be present in the soong.variables file but not
321 // defined in an Android.bp file. There's also nothing stopping you from setting
322 // a variable in make that doesn't exist in soong. We only generate build
323 // settings for the ones that exist in soong, so skip all others.
324 continue
325 }
326 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
327 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))
328 }
329 if hasBool {
330 // Logic copied from soongConfig.Bool()
331 value = strings.ToLower(value)
332 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
333 value = "true"
334 } else {
335 value = "false"
336 }
337 }
338 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
339 }
340 }
Cole Faustf055db62023-07-24 15:17:03 -0700341 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700342}
343
344func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700345 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700346 productVarsReflect := reflect.ValueOf(&result).Elem()
347 for i := 0; i < productVarsReflect.NumField(); i++ {
348 field := productVarsReflect.Field(i)
349 fieldType := productVarsReflect.Type().Field(i)
350 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700351 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
352 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700353 // These variables have more complicated types, and we don't need them right now
354 continue
355 }
356 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700357 if name == "VendorVars" {
358 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
359 if err != nil {
360 return result, err
361 }
362 field.Set(reflect.ValueOf(vendorVars))
363 continue
364 }
Cole Faustf055db62023-07-24 15:17:03 -0700365 switch field.Type().Kind() {
366 case reflect.Bool:
367 val, err := starlark_import.Unmarshal[bool](in[name])
368 if err != nil {
369 return result, err
370 }
371 field.SetBool(val)
372 case reflect.String:
373 val, err := starlark_import.Unmarshal[string](in[name])
374 if err != nil {
375 return result, err
376 }
377 field.SetString(val)
378 case reflect.Slice:
379 if field.Type().Elem().Kind() != reflect.String {
380 return result, fmt.Errorf("slices of types other than strings are unimplemented")
381 }
382 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
383 if err != nil {
384 return result, err
385 }
386 field.Set(val)
387 case reflect.Pointer:
388 switch field.Type().Elem().Kind() {
389 case reflect.Bool:
390 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
391 if err != nil {
392 return result, err
393 }
394 field.Set(reflect.ValueOf(val))
395 case reflect.String:
396 val, err := starlark_import.UnmarshalNoneable[string](in[name])
397 if err != nil {
398 return result, err
399 }
400 field.Set(reflect.ValueOf(val))
401 case reflect.Int:
402 val, err := starlark_import.UnmarshalNoneable[int](in[name])
403 if err != nil {
404 return result, err
405 }
406 field.Set(reflect.ValueOf(val))
407 default:
408 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
409 }
410 default:
411 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
412 }
413 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700414 }
Cole Faustf055db62023-07-24 15:17:03 -0700415
Cole Faust87c0c332023-07-31 12:10:12 -0700416 result.Native_coverage = proptools.BoolPtr(
417 proptools.Bool(result.GcovCoverage) ||
418 proptools.Bool(result.ClangCoverage))
419
Cole Faustb85d1a12022-11-08 18:14:01 -0800420 return result, nil
421}