blob: 25a0f315b8bc20482e2566802186d57b929b6356 [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
Cole Faust6054cdf2023-09-12 10:07:07 -070019type createProductConfigFilesResult struct {
20 injectionFiles []BazelFile
21 bp2buildFiles []BazelFile
22 bp2buildTargets map[string]BazelTargets
23}
24
25func createProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070026 ctx *CodegenContext,
Cole Faust6054cdf2023-09-12 10:07:07 -070027 metrics CodegenMetrics) (createProductConfigFilesResult, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080028 cfg := &ctx.config
29 targetProduct := "unknown"
30 if cfg.HasDeviceProduct() {
31 targetProduct = cfg.DeviceProduct()
32 }
33 targetBuildVariant := "user"
34 if cfg.Eng() {
35 targetBuildVariant = "eng"
36 } else if cfg.Debuggable() {
37 targetBuildVariant = "userdebug"
38 }
39
Cole Faust6054cdf2023-09-12 10:07:07 -070040 var res createProductConfigFilesResult
41
Cole Faustb85d1a12022-11-08 18:14:01 -080042 productVariablesFileName := cfg.ProductVariablesFileName
43 if !strings.HasPrefix(productVariablesFileName, "/") {
44 productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
45 }
Cole Faustf8231dd2023-04-21 17:37:11 -070046 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080047 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070048 return res, err
Cole Faustf8231dd2023-04-21 17:37:11 -070049 }
50 productVariables := android.ProductVariables{}
51 err = json.Unmarshal(productVariablesBytes, &productVariables)
52 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070053 return res, err
Cole Faustb85d1a12022-11-08 18:14:01 -080054 }
55
56 // TODO(b/249685973): the name is product_config_platforms because product_config
57 // was already used for other files. Deduplicate them.
58 currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant)
59
60 productReplacer := strings.NewReplacer(
61 "{PRODUCT}", targetProduct,
62 "{VARIANT}", targetBuildVariant,
63 "{PRODUCT_FOLDER}", currentProductFolder)
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 {
Cole Faust6054cdf2023-09-12 10:07:07 -070067 return res, err
Cole Faust946d02c2023-08-03 16:08:09 -070068 }
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 Faust6054cdf2023-09-12 10:07:07 -070074 productLabelsToVariables := make(map[string]*android.ProductVariables)
75 productLabelsToVariables[productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}")] = &productVariables
76 for product, productVariablesStarlark := range productsForTestingMap {
77 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
78 if err != nil {
79 return res, err
80 }
81 productLabelsToVariables["@//build/bazel/tests/products:"+product] = &productVariables
82 }
83
84 res.bp2buildTargets = createTargets(productLabelsToVariables)
85
86 platformMappingContent, err := platformMappingContent(
87 productLabelsToVariables,
88 ctx.Config().Bp2buildSoongConfigDefinitions,
89 metrics.convertedModulePathMap)
90 if err != nil {
91 return res, err
92 }
93
94 res.injectionFiles = []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -080095 newFile(
96 currentProductFolder,
97 "soong.variables.bzl",
Cole Faustf8231dd2023-04-21 17:37:11 -070098 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustb85d1a12022-11-08 18:14:01 -080099 newFile(
100 currentProductFolder,
101 "BUILD",
102 productReplacer.Replace(`
103package(default_visibility=[
104 "@soong_injection//product_config_platforms:__subpackages__",
105 "@//build/bazel/product_config:__subpackages__",
106])
107load(":soong.variables.bzl", _soong_variables = "variables")
Cole Faustbd249822023-03-24 16:03:43 -0700108load("@//build/bazel/product_config:android_product.bzl", "android_product")
Cole Faustb85d1a12022-11-08 18:14:01 -0800109
110android_product(
111 name = "{PRODUCT}-{VARIANT}",
112 soong_variables = _soong_variables,
113)
114`)),
115 newFile(
116 "product_config_platforms",
117 "BUILD.bazel",
118 productReplacer.Replace(`
119package(default_visibility = [
120 "@//build/bazel/product_config:__subpackages__",
121 "@soong_injection//product_config_platforms:__subpackages__",
122])
Jingwen Chen583ab212023-05-30 09:45:23 +0000123
124load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
125load("@//build/bazel/product_config:android_product.bzl", "android_product")
126
Cole Faust319abae2023-06-06 15:12:49 -0700127# Bazel will qualify its outputs by the platform name. When switching between products, this
128# means that soong-built files that depend on bazel-built files will suddenly get different
129# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
130# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
131# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000132android_product(
Cole Faust319abae2023-06-06 15:12:49 -0700133 name = "mixed_builds_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +0000134 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000135 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000136)
Cole Faust117bb742023-03-29 14:46:20 -0700137`)),
138 newFile(
139 "product_config_platforms",
140 "product_labels.bzl",
141 productReplacer.Replace(`
142# This file keeps a list of all the products in the android source tree, because they're
143# discovered as part of a preprocessing step before bazel runs.
144# TODO: When we start generating the platforms for more than just the
145# currently lunched product, they should all be listed here
146product_labels = [
Cole Faust319abae2023-06-06 15:12:49 -0700147 "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700148 "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}",
149`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800150 newFile(
151 "product_config_platforms",
152 "common.bazelrc",
153 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700154build --platform_mappings=platform_mappings
Cole Faust319abae2023-06-06 15:12:49 -0700155build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800156
Cole Faust319abae2023-06-06 15:12:49 -0700157build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
Cole Faustd1acaa42023-08-03 17:04:03 -0700158build:linux_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86
Cole Faust319abae2023-06-06 15:12:49 -0700159build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
160build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
161build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
162build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800163`)),
164 newFile(
165 "product_config_platforms",
166 "linux.bazelrc",
167 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700168build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800169`)),
170 newFile(
171 "product_config_platforms",
172 "darwin.bazelrc",
173 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700174build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800175`)),
176 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700177 res.bp2buildFiles = []BazelFile{
Cole Faustf8231dd2023-04-21 17:37:11 -0700178 newFile(
179 "",
180 "platform_mappings",
181 platformMappingContent),
182 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700183
184 return res, nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700185}
Cole Faustb85d1a12022-11-08 18:14:01 -0800186
Cole Faust946d02c2023-08-03 16:08:09 -0700187func platformMappingContent(
Cole Faust6054cdf2023-09-12 10:07:07 -0700188 productLabelToVariables map[string]*android.ProductVariables,
Cole Faust946d02c2023-08-03 16:08:09 -0700189 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
190 convertedModulePathMap map[string]string) (string, error) {
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 Faust6054cdf2023-09-12 10:07:07 -0700206 for productLabel, productVariables := range productLabelToVariables {
207 platformMappingSingleProduct(productLabel, productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700208 }
Cole Faustf055db62023-07-24 15:17:03 -0700209 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700210}
211
Cole Faust88c8efb2023-07-18 11:05:16 -0700212var bazelPlatformSuffixes = []string{
213 "",
214 "_darwin_arm64",
215 "_darwin_x86_64",
216 "_linux_bionic_arm64",
217 "_linux_bionic_x86_64",
218 "_linux_musl_x86",
219 "_linux_musl_x86_64",
220 "_linux_x86",
221 "_linux_x86_64",
222 "_windows_x86",
223 "_windows_x86_64",
224}
225
Cole Faust946d02c2023-08-03 16:08:09 -0700226func platformMappingSingleProduct(
227 label string,
228 productVariables *android.ProductVariables,
229 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
230 convertedModulePathMap map[string]string,
231 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700232 targetBuildVariant := "user"
233 if proptools.Bool(productVariables.Eng) {
234 targetBuildVariant = "eng"
235 } else if proptools.Bool(productVariables.Debuggable) {
236 targetBuildVariant = "userdebug"
Cole Faustf8231dd2023-04-21 17:37:11 -0700237 }
Cole Faustf055db62023-07-24 15:17:03 -0700238
Cole Faust95c5cf82023-08-03 13:49:27 -0700239 platform_sdk_version := -1
240 if productVariables.Platform_sdk_version != nil {
241 platform_sdk_version = *productVariables.Platform_sdk_version
242 }
243
Cole Faust946d02c2023-08-03 16:08:09 -0700244 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
245 if proptools.String(productVariables.DefaultAppCertificate) != "" {
Cole Faust6054cdf2023-09-12 10:07:07 -0700246 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory"
Cole Faust946d02c2023-08-03 16:08:09 -0700247 }
248
Cole Faustf055db62023-07-24 15:17:03 -0700249 for _, suffix := range bazelPlatformSuffixes {
250 result.WriteString(" ")
251 result.WriteString(label)
252 result.WriteString(suffix)
253 result.WriteString("\n")
254 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 -0700255 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700256 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 -0700257 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
258 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 -0700259 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages))
Cole Faustf055db62023-07-24 15:17:03 -0700260 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
261 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700262 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
263 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
264 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
Cole Faust87c0c332023-07-31 12:10:12 -0700265 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:debuggable=%t\n", proptools.Bool(productVariables.Debuggable)))
Cole Faustf055db62023-07-24 15:17:03 -0700266 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700267 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700268 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
269 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
270 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700271 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 -0700272 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
Cole Faust48ce1372023-09-05 16:07:49 -0700273 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label))
Cole Faustf055db62023-07-24 15:17:03 -0700274 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700275 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
276 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:eng=%t\n", proptools.Bool(productVariables.Eng)))
277 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
278 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
279 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 -0700280 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
281 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
282 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 -0700283 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 -0700284 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Romain Jobredeaux3132f842023-09-15 10:06:16 -0400285 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_final=%t\n", proptools.Bool(productVariables.Platform_sdk_final)))
Cole Faustf055db62023-07-24 15:17:03 -0700286 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
287 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
288 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Yu Liu2cc802a2023-09-05 17:19:45 -0700289 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission))
290 // Empty string can't be used as label_flag on the bazel side
291 if len(productVariables.ReleaseAconfigValueSets) > 0 {
292 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", productVariables.ReleaseAconfigValueSets))
293 }
294 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion))
Cole Faust95c5cf82023-08-03 13:49:27 -0700295 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700296 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faustf055db62023-07-24 15:17:03 -0700297 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:target_build_variant=%s\n", targetBuildVariant))
Cole Faust87c0c332023-07-31 12:10:12 -0700298 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 -0700299 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700300 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700301 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
302 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 -0700303
304 for _, override := range productVariables.CertificateOverrides {
305 parts := strings.SplitN(override, ":", 2)
306 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
307 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
308 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
309 }
310 }
311 }
312
Cole Faust87c0c332023-07-31 12:10:12 -0700313 for namespace, namespaceContents := range productVariables.VendorVars {
314 for variable, value := range namespaceContents {
315 key := namespace + "__" + variable
316 _, hasBool := soongConfigDefinitions.BoolVars[key]
317 _, hasString := soongConfigDefinitions.StringVars[key]
318 _, hasValue := soongConfigDefinitions.ValueVars[key]
319 if !hasBool && !hasString && !hasValue {
320 // Not all soong config variables are defined in Android.bp files. For example,
321 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
322 // way, that causes them to be present in the soong.variables file but not
323 // defined in an Android.bp file. There's also nothing stopping you from setting
324 // a variable in make that doesn't exist in soong. We only generate build
325 // settings for the ones that exist in soong, so skip all others.
326 continue
327 }
328 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
329 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))
330 }
331 if hasBool {
332 // Logic copied from soongConfig.Bool()
333 value = strings.ToLower(value)
334 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
335 value = "true"
336 } else {
337 value = "false"
338 }
339 }
340 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
341 }
342 }
Cole Faustf055db62023-07-24 15:17:03 -0700343 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700344}
345
346func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700347 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700348 productVarsReflect := reflect.ValueOf(&result).Elem()
349 for i := 0; i < productVarsReflect.NumField(); i++ {
350 field := productVarsReflect.Field(i)
351 fieldType := productVarsReflect.Type().Field(i)
352 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700353 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
354 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700355 // These variables have more complicated types, and we don't need them right now
356 continue
357 }
358 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700359 if name == "VendorVars" {
360 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
361 if err != nil {
362 return result, err
363 }
364 field.Set(reflect.ValueOf(vendorVars))
365 continue
366 }
Cole Faustf055db62023-07-24 15:17:03 -0700367 switch field.Type().Kind() {
368 case reflect.Bool:
369 val, err := starlark_import.Unmarshal[bool](in[name])
370 if err != nil {
371 return result, err
372 }
373 field.SetBool(val)
374 case reflect.String:
375 val, err := starlark_import.Unmarshal[string](in[name])
376 if err != nil {
377 return result, err
378 }
379 field.SetString(val)
380 case reflect.Slice:
381 if field.Type().Elem().Kind() != reflect.String {
382 return result, fmt.Errorf("slices of types other than strings are unimplemented")
383 }
384 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
385 if err != nil {
386 return result, err
387 }
388 field.Set(val)
389 case reflect.Pointer:
390 switch field.Type().Elem().Kind() {
391 case reflect.Bool:
392 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
393 if err != nil {
394 return result, err
395 }
396 field.Set(reflect.ValueOf(val))
397 case reflect.String:
398 val, err := starlark_import.UnmarshalNoneable[string](in[name])
399 if err != nil {
400 return result, err
401 }
402 field.Set(reflect.ValueOf(val))
403 case reflect.Int:
404 val, err := starlark_import.UnmarshalNoneable[int](in[name])
405 if err != nil {
406 return result, err
407 }
408 field.Set(reflect.ValueOf(val))
409 default:
410 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
411 }
412 default:
413 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
414 }
415 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700416 }
Cole Faustf055db62023-07-24 15:17:03 -0700417
Cole Faust87c0c332023-07-31 12:10:12 -0700418 result.Native_coverage = proptools.BoolPtr(
419 proptools.Bool(result.GcovCoverage) ||
420 proptools.Bool(result.ClangCoverage))
421
Cole Faustb85d1a12022-11-08 18:14:01 -0800422 return result, nil
423}
Cole Faust6054cdf2023-09-12 10:07:07 -0700424
425func createTargets(productLabelsToVariables map[string]*android.ProductVariables) map[string]BazelTargets {
426 res := make(map[string]BazelTargets)
427 var allDefaultAppCertificateDirs []string
428 for _, productVariables := range productLabelsToVariables {
429 if proptools.String(productVariables.DefaultAppCertificate) != "" {
430 d := filepath.Dir(proptools.String(productVariables.DefaultAppCertificate))
431 if !android.InList(d, allDefaultAppCertificateDirs) {
432 allDefaultAppCertificateDirs = append(allDefaultAppCertificateDirs, d)
433 }
434 }
435 }
436 for _, dir := range allDefaultAppCertificateDirs {
437 content := fmt.Sprintf(ruleTargetTemplate, "filegroup", "generated_android_certificate_directory", propsToAttributes(map[string]string{
438 "srcs": `glob([
439 "*.pk8",
440 "*.pem",
441 "*.avbpubkey",
442 ])`,
443 "visibility": `["//visibility:public"]`,
444 }))
445 res[dir] = append(res[dir], BazelTarget{
446 name: "generated_android_certificate_directory",
447 packageName: dir,
448 content: content,
449 ruleClass: "filegroup",
450 })
451 }
452 return res
453}