blob: 3d9cae06946a9d0d85e555698264e83d384a9f22 [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
Cole Faustb4cb0c82023-09-14 15:16:58 -070056 currentProductFolder := fmt.Sprintf("build/bazel/products/%s-%s", targetProduct, targetBuildVariant)
57 if len(productVariables.PartitionVars.ProductDirectory) > 0 {
58 currentProductFolder = fmt.Sprintf("%s%s-%s", productVariables.PartitionVars.ProductDirectory, targetProduct, targetBuildVariant)
59 }
Cole Faustb85d1a12022-11-08 18:14:01 -080060
61 productReplacer := strings.NewReplacer(
62 "{PRODUCT}", targetProduct,
63 "{VARIANT}", targetBuildVariant,
64 "{PRODUCT_FOLDER}", currentProductFolder)
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 {
Cole Faust6054cdf2023-09-12 10:07:07 -070068 return res, err
Cole Faust946d02c2023-08-03 16:08:09 -070069 }
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 Faust6054cdf2023-09-12 10:07:07 -070075 productLabelsToVariables := make(map[string]*android.ProductVariables)
Cole Faustb4cb0c82023-09-14 15:16:58 -070076 productLabelsToVariables[productReplacer.Replace("@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}")] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -070077 for product, productVariablesStarlark := range productsForTestingMap {
78 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
79 if err != nil {
80 return res, err
81 }
82 productLabelsToVariables["@//build/bazel/tests/products:"+product] = &productVariables
83 }
84
Cole Faustb4cb0c82023-09-14 15:16:58 -070085 res.bp2buildTargets = make(map[string]BazelTargets)
86 res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{
87 name: productReplacer.Replace("{PRODUCT}-{VARIANT}"),
88 packageName: currentProductFolder,
89 content: productReplacer.Replace(`android_product(
90 name = "{PRODUCT}-{VARIANT}",
91 soong_variables = _soong_variables,
92)`),
93 ruleClass: "android_product",
94 loads: []BazelLoad{
95 {
96 file: ":soong.variables.bzl",
97 symbols: []BazelLoadSymbol{{
98 symbol: "variables",
99 alias: "_soong_variables",
100 }},
101 },
102 {
103 file: "//build/bazel/product_config:android_product.bzl",
104 symbols: []BazelLoadSymbol{{symbol: "android_product"}},
105 },
106 },
107 })
108 createTargets(productLabelsToVariables, res.bp2buildTargets)
Cole Faust6054cdf2023-09-12 10:07:07 -0700109
110 platformMappingContent, err := platformMappingContent(
111 productLabelsToVariables,
112 ctx.Config().Bp2buildSoongConfigDefinitions,
113 metrics.convertedModulePathMap)
114 if err != nil {
115 return res, err
116 }
117
118 res.injectionFiles = []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -0800119 newFile(
Cole Faustb85d1a12022-11-08 18:14:01 -0800120 "product_config_platforms",
121 "BUILD.bazel",
122 productReplacer.Replace(`
123package(default_visibility = [
124 "@//build/bazel/product_config:__subpackages__",
125 "@soong_injection//product_config_platforms:__subpackages__",
126])
Jingwen Chen583ab212023-05-30 09:45:23 +0000127
Cole Faustb4cb0c82023-09-14 15:16:58 -0700128load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
Jingwen Chen583ab212023-05-30 09:45:23 +0000129load("@//build/bazel/product_config:android_product.bzl", "android_product")
130
Cole Faust319abae2023-06-06 15:12:49 -0700131# Bazel will qualify its outputs by the platform name. When switching between products, this
132# means that soong-built files that depend on bazel-built files will suddenly get different
133# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
134# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
135# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000136android_product(
Cole Faust319abae2023-06-06 15:12:49 -0700137 name = "mixed_builds_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +0000138 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000139 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000140)
Cole Faust117bb742023-03-29 14:46:20 -0700141`)),
142 newFile(
143 "product_config_platforms",
144 "product_labels.bzl",
145 productReplacer.Replace(`
146# This file keeps a list of all the products in the android source tree, because they're
147# discovered as part of a preprocessing step before bazel runs.
148# TODO: When we start generating the platforms for more than just the
149# currently lunched product, they should all be listed here
150product_labels = [
Cole Faust319abae2023-06-06 15:12:49 -0700151 "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700152 "@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700153`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800154 newFile(
155 "product_config_platforms",
156 "common.bazelrc",
157 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700158build --platform_mappings=platform_mappings
Cole Faustb4cb0c82023-09-14 15:16:58 -0700159build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800160
Cole Faustb4cb0c82023-09-14 15:16:58 -0700161build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
162build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86
163build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
164build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
165build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
166build:linux_musl_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800167`)),
168 newFile(
169 "product_config_platforms",
170 "linux.bazelrc",
171 productReplacer.Replace(`
Cole Faustb4cb0c82023-09-14 15:16:58 -0700172build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800173`)),
174 newFile(
175 "product_config_platforms",
176 "darwin.bazelrc",
177 productReplacer.Replace(`
Cole Faustb4cb0c82023-09-14 15:16:58 -0700178build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800179`)),
180 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700181 res.bp2buildFiles = []BazelFile{
Cole Faustf8231dd2023-04-21 17:37:11 -0700182 newFile(
183 "",
184 "platform_mappings",
185 platformMappingContent),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700186 newFile(
187 currentProductFolder,
188 "soong.variables.bzl",
189 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustf8231dd2023-04-21 17:37:11 -0700190 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700191
192 return res, nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700193}
Cole Faustb85d1a12022-11-08 18:14:01 -0800194
Cole Faust946d02c2023-08-03 16:08:09 -0700195func platformMappingContent(
Cole Faust6054cdf2023-09-12 10:07:07 -0700196 productLabelToVariables map[string]*android.ProductVariables,
Cole Faust946d02c2023-08-03 16:08:09 -0700197 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
198 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf055db62023-07-24 15:17:03 -0700199 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700200
201 mergedConvertedModulePathMap := make(map[string]string)
202 for k, v := range convertedModulePathMap {
203 mergedConvertedModulePathMap[k] = v
204 }
205 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
206 if err != nil {
207 return "", err
208 }
209 for k, v := range additionalModuleNamesToPackages {
210 mergedConvertedModulePathMap[k] = v
211 }
212
Cole Faustf055db62023-07-24 15:17:03 -0700213 result.WriteString("platforms:\n")
Cole Faust6054cdf2023-09-12 10:07:07 -0700214 for productLabel, productVariables := range productLabelToVariables {
215 platformMappingSingleProduct(productLabel, productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700216 }
Cole Faustf055db62023-07-24 15:17:03 -0700217 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700218}
219
Cole Faust88c8efb2023-07-18 11:05:16 -0700220var bazelPlatformSuffixes = []string{
221 "",
222 "_darwin_arm64",
223 "_darwin_x86_64",
224 "_linux_bionic_arm64",
225 "_linux_bionic_x86_64",
226 "_linux_musl_x86",
227 "_linux_musl_x86_64",
228 "_linux_x86",
229 "_linux_x86_64",
230 "_windows_x86",
231 "_windows_x86_64",
232}
233
Cole Faust946d02c2023-08-03 16:08:09 -0700234func platformMappingSingleProduct(
235 label string,
236 productVariables *android.ProductVariables,
237 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
238 convertedModulePathMap map[string]string,
239 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700240 targetBuildVariant := "user"
241 if proptools.Bool(productVariables.Eng) {
242 targetBuildVariant = "eng"
243 } else if proptools.Bool(productVariables.Debuggable) {
244 targetBuildVariant = "userdebug"
Cole Faustf8231dd2023-04-21 17:37:11 -0700245 }
Cole Faustf055db62023-07-24 15:17:03 -0700246
Cole Faust95c5cf82023-08-03 13:49:27 -0700247 platform_sdk_version := -1
248 if productVariables.Platform_sdk_version != nil {
249 platform_sdk_version = *productVariables.Platform_sdk_version
250 }
251
Cole Faust946d02c2023-08-03 16:08:09 -0700252 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
253 if proptools.String(productVariables.DefaultAppCertificate) != "" {
Cole Faust6054cdf2023-09-12 10:07:07 -0700254 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory"
Cole Faust946d02c2023-08-03 16:08:09 -0700255 }
256
Cole Faustf055db62023-07-24 15:17:03 -0700257 for _, suffix := range bazelPlatformSuffixes {
258 result.WriteString(" ")
259 result.WriteString(label)
260 result.WriteString(suffix)
261 result.WriteString("\n")
262 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 -0700263 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700264 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 -0700265 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
266 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 -0700267 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages))
Cole Faustf055db62023-07-24 15:17:03 -0700268 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
269 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700270 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
271 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
272 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
Cole Faust87c0c332023-07-31 12:10:12 -0700273 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:debuggable=%t\n", proptools.Bool(productVariables.Debuggable)))
Cole Faustf055db62023-07-24 15:17:03 -0700274 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700275 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700276 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
277 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
278 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700279 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 -0700280 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
Cole Faust48ce1372023-09-05 16:07:49 -0700281 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label))
Cole Faustf055db62023-07-24 15:17:03 -0700282 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700283 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
284 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:eng=%t\n", proptools.Bool(productVariables.Eng)))
285 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
286 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
287 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 -0700288 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
289 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
290 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 -0700291 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 -0700292 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Cole Faustf055db62023-07-24 15:17:03 -0700293 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
294 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
295 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Yu Liu2cc802a2023-09-05 17:19:45 -0700296 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission))
297 // Empty string can't be used as label_flag on the bazel side
298 if len(productVariables.ReleaseAconfigValueSets) > 0 {
299 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", productVariables.ReleaseAconfigValueSets))
300 }
301 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion))
Cole Faust95c5cf82023-08-03 13:49:27 -0700302 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700303 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faustf055db62023-07-24 15:17:03 -0700304 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:target_build_variant=%s\n", targetBuildVariant))
Cole Faust87c0c332023-07-31 12:10:12 -0700305 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 -0700306 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700307 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700308 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
309 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 -0700310
311 for _, override := range productVariables.CertificateOverrides {
312 parts := strings.SplitN(override, ":", 2)
313 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
314 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
315 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
316 }
317 }
318 }
319
Cole Faust87c0c332023-07-31 12:10:12 -0700320 for namespace, namespaceContents := range productVariables.VendorVars {
321 for variable, value := range namespaceContents {
322 key := namespace + "__" + variable
323 _, hasBool := soongConfigDefinitions.BoolVars[key]
324 _, hasString := soongConfigDefinitions.StringVars[key]
325 _, hasValue := soongConfigDefinitions.ValueVars[key]
326 if !hasBool && !hasString && !hasValue {
327 // Not all soong config variables are defined in Android.bp files. For example,
328 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
329 // way, that causes them to be present in the soong.variables file but not
330 // defined in an Android.bp file. There's also nothing stopping you from setting
331 // a variable in make that doesn't exist in soong. We only generate build
332 // settings for the ones that exist in soong, so skip all others.
333 continue
334 }
335 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
336 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))
337 }
338 if hasBool {
339 // Logic copied from soongConfig.Bool()
340 value = strings.ToLower(value)
341 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
342 value = "true"
343 } else {
344 value = "false"
345 }
346 }
347 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
348 }
349 }
Cole Faustf055db62023-07-24 15:17:03 -0700350 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700351}
352
353func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700354 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700355 productVarsReflect := reflect.ValueOf(&result).Elem()
356 for i := 0; i < productVarsReflect.NumField(); i++ {
357 field := productVarsReflect.Field(i)
358 fieldType := productVarsReflect.Type().Field(i)
359 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700360 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
361 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700362 // These variables have more complicated types, and we don't need them right now
363 continue
364 }
365 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700366 if name == "VendorVars" {
367 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
368 if err != nil {
369 return result, err
370 }
371 field.Set(reflect.ValueOf(vendorVars))
372 continue
373 }
Cole Faustf055db62023-07-24 15:17:03 -0700374 switch field.Type().Kind() {
375 case reflect.Bool:
376 val, err := starlark_import.Unmarshal[bool](in[name])
377 if err != nil {
378 return result, err
379 }
380 field.SetBool(val)
381 case reflect.String:
382 val, err := starlark_import.Unmarshal[string](in[name])
383 if err != nil {
384 return result, err
385 }
386 field.SetString(val)
387 case reflect.Slice:
388 if field.Type().Elem().Kind() != reflect.String {
389 return result, fmt.Errorf("slices of types other than strings are unimplemented")
390 }
391 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
392 if err != nil {
393 return result, err
394 }
395 field.Set(val)
396 case reflect.Pointer:
397 switch field.Type().Elem().Kind() {
398 case reflect.Bool:
399 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
400 if err != nil {
401 return result, err
402 }
403 field.Set(reflect.ValueOf(val))
404 case reflect.String:
405 val, err := starlark_import.UnmarshalNoneable[string](in[name])
406 if err != nil {
407 return result, err
408 }
409 field.Set(reflect.ValueOf(val))
410 case reflect.Int:
411 val, err := starlark_import.UnmarshalNoneable[int](in[name])
412 if err != nil {
413 return result, err
414 }
415 field.Set(reflect.ValueOf(val))
416 default:
417 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
418 }
419 default:
420 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
421 }
422 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700423 }
Cole Faustf055db62023-07-24 15:17:03 -0700424
Cole Faust87c0c332023-07-31 12:10:12 -0700425 result.Native_coverage = proptools.BoolPtr(
426 proptools.Bool(result.GcovCoverage) ||
427 proptools.Bool(result.ClangCoverage))
428
Cole Faustb85d1a12022-11-08 18:14:01 -0800429 return result, nil
430}
Cole Faust6054cdf2023-09-12 10:07:07 -0700431
Cole Faustb4cb0c82023-09-14 15:16:58 -0700432func createTargets(productLabelsToVariables map[string]*android.ProductVariables, res map[string]BazelTargets) {
433 createGeneratedAndroidCertificateDirectories(productLabelsToVariables, res)
434}
435
436func createGeneratedAndroidCertificateDirectories(productLabelsToVariables map[string]*android.ProductVariables, targets map[string]BazelTargets) {
Cole Faust6054cdf2023-09-12 10:07:07 -0700437 var allDefaultAppCertificateDirs []string
438 for _, productVariables := range productLabelsToVariables {
439 if proptools.String(productVariables.DefaultAppCertificate) != "" {
440 d := filepath.Dir(proptools.String(productVariables.DefaultAppCertificate))
441 if !android.InList(d, allDefaultAppCertificateDirs) {
442 allDefaultAppCertificateDirs = append(allDefaultAppCertificateDirs, d)
443 }
444 }
445 }
446 for _, dir := range allDefaultAppCertificateDirs {
Cole Faustb4cb0c82023-09-14 15:16:58 -0700447 content := `filegroup(
448 name = "generated_android_certificate_directory",
449 srcs = glob([
Cole Faust6054cdf2023-09-12 10:07:07 -0700450 "*.pk8",
451 "*.pem",
452 "*.avbpubkey",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700453 ]),
454 visibility = ["//visibility:public"],
455)`
456 targets[dir] = append(targets[dir], BazelTarget{
Cole Faust6054cdf2023-09-12 10:07:07 -0700457 name: "generated_android_certificate_directory",
458 packageName: dir,
459 content: content,
460 ruleClass: "filegroup",
461 })
462 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700463}