blob: 2f9e9cc56f0f3112a3e51134d5628176c84c940e [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 Fauste136dda2023-09-27 14:10:33 -07009 "sort"
Cole Faustb85d1a12022-11-08 18:14:01 -080010 "strings"
Cole Faustf8231dd2023-04-21 17:37:11 -070011
Yu Liub6a15da2023-08-31 14:14:01 -070012 "android/soong/android"
13 "android/soong/android/soongconfig"
14 "android/soong/starlark_import"
15
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
Cole Faust6054cdf2023-09-12 10:07:07 -070020type createProductConfigFilesResult struct {
21 injectionFiles []BazelFile
22 bp2buildFiles []BazelFile
23 bp2buildTargets map[string]BazelTargets
24}
25
Cole Faustcb193ec2023-09-20 16:01:18 -070026type bazelLabel struct {
27 repo string
28 pkg string
29 target string
30}
31
Cole Fauste136dda2023-09-27 14:10:33 -070032func (l *bazelLabel) Less(other *bazelLabel) bool {
33 if l.repo < other.repo {
34 return true
35 }
36 if l.repo > other.repo {
37 return false
38 }
39 if l.pkg < other.pkg {
40 return true
41 }
42 if l.pkg > other.pkg {
43 return false
44 }
45 return l.target < other.target
46}
47
Cole Faustcb193ec2023-09-20 16:01:18 -070048func (l *bazelLabel) String() string {
49 return fmt.Sprintf("@%s//%s:%s", l.repo, l.pkg, l.target)
50}
51
Cole Faust6054cdf2023-09-12 10:07:07 -070052func createProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070053 ctx *CodegenContext,
Cole Faust6054cdf2023-09-12 10:07:07 -070054 metrics CodegenMetrics) (createProductConfigFilesResult, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080055 cfg := &ctx.config
56 targetProduct := "unknown"
57 if cfg.HasDeviceProduct() {
58 targetProduct = cfg.DeviceProduct()
59 }
60 targetBuildVariant := "user"
61 if cfg.Eng() {
62 targetBuildVariant = "eng"
63 } else if cfg.Debuggable() {
64 targetBuildVariant = "userdebug"
65 }
66
Cole Faust6054cdf2023-09-12 10:07:07 -070067 var res createProductConfigFilesResult
68
Cole Faustb85d1a12022-11-08 18:14:01 -080069 productVariablesFileName := cfg.ProductVariablesFileName
70 if !strings.HasPrefix(productVariablesFileName, "/") {
71 productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
72 }
Cole Faustf8231dd2023-04-21 17:37:11 -070073 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080074 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070075 return res, err
Cole Faustf8231dd2023-04-21 17:37:11 -070076 }
77 productVariables := android.ProductVariables{}
78 err = json.Unmarshal(productVariablesBytes, &productVariables)
79 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070080 return res, err
Cole Faustb85d1a12022-11-08 18:14:01 -080081 }
82
Cole Faustf3cf34e2023-09-20 17:02:40 -070083 currentProductFolder := fmt.Sprintf("build/bazel/products/%s", targetProduct)
Cole Faustcb193ec2023-09-20 16:01:18 -070084 if len(productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory) > 0 {
85 currentProductFolder = fmt.Sprintf("%s%s", productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory, targetProduct)
Cole Faustb4cb0c82023-09-14 15:16:58 -070086 }
Cole Faustb85d1a12022-11-08 18:14:01 -080087
88 productReplacer := strings.NewReplacer(
89 "{PRODUCT}", targetProduct,
90 "{VARIANT}", targetBuildVariant,
91 "{PRODUCT_FOLDER}", currentProductFolder)
92
Cole Faust946d02c2023-08-03 16:08:09 -070093 productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
94 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070095 return res, err
Cole Faust946d02c2023-08-03 16:08:09 -070096 }
97 productsForTesting := android.SortedKeys(productsForTestingMap)
98 for i := range productsForTesting {
99 productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i])
100 }
101
Cole Faustcb193ec2023-09-20 16:01:18 -0700102 productLabelsToVariables := make(map[bazelLabel]*android.ProductVariables)
103 productLabelsToVariables[bazelLabel{
104 repo: "",
105 pkg: currentProductFolder,
106 target: targetProduct,
107 }] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -0700108 for product, productVariablesStarlark := range productsForTestingMap {
109 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
110 if err != nil {
111 return res, err
112 }
Cole Faustcb193ec2023-09-20 16:01:18 -0700113 productLabelsToVariables[bazelLabel{
114 repo: "",
115 pkg: "build/bazel/tests/products",
116 target: product,
117 }] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -0700118 }
119
Cole Faustb4cb0c82023-09-14 15:16:58 -0700120 res.bp2buildTargets = make(map[string]BazelTargets)
121 res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{
Cole Faustf3cf34e2023-09-20 17:02:40 -0700122 name: productReplacer.Replace("{PRODUCT}"),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700123 packageName: currentProductFolder,
124 content: productReplacer.Replace(`android_product(
Cole Faustf3cf34e2023-09-20 17:02:40 -0700125 name = "{PRODUCT}",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700126 soong_variables = _soong_variables,
127)`),
128 ruleClass: "android_product",
129 loads: []BazelLoad{
130 {
131 file: ":soong.variables.bzl",
132 symbols: []BazelLoadSymbol{{
133 symbol: "variables",
134 alias: "_soong_variables",
135 }},
136 },
137 {
138 file: "//build/bazel/product_config:android_product.bzl",
139 symbols: []BazelLoadSymbol{{symbol: "android_product"}},
140 },
141 },
142 })
143 createTargets(productLabelsToVariables, res.bp2buildTargets)
Cole Faust6054cdf2023-09-12 10:07:07 -0700144
145 platformMappingContent, err := platformMappingContent(
146 productLabelsToVariables,
147 ctx.Config().Bp2buildSoongConfigDefinitions,
148 metrics.convertedModulePathMap)
149 if err != nil {
150 return res, err
151 }
152
153 res.injectionFiles = []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -0800154 newFile(
Cole Faustb85d1a12022-11-08 18:14:01 -0800155 "product_config_platforms",
156 "BUILD.bazel",
157 productReplacer.Replace(`
158package(default_visibility = [
159 "@//build/bazel/product_config:__subpackages__",
160 "@soong_injection//product_config_platforms:__subpackages__",
161])
Jingwen Chen583ab212023-05-30 09:45:23 +0000162
Cole Faustb4cb0c82023-09-14 15:16:58 -0700163load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
Jingwen Chen583ab212023-05-30 09:45:23 +0000164load("@//build/bazel/product_config:android_product.bzl", "android_product")
165
Cole Faust319abae2023-06-06 15:12:49 -0700166# Bazel will qualify its outputs by the platform name. When switching between products, this
167# means that soong-built files that depend on bazel-built files will suddenly get different
168# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
169# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
170# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000171android_product(
Cole Faustf3cf34e2023-09-20 17:02:40 -0700172 name = "mixed_builds_product",
Jingwen Chen583ab212023-05-30 09:45:23 +0000173 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000174 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000175)
Cole Faust117bb742023-03-29 14:46:20 -0700176`)),
177 newFile(
178 "product_config_platforms",
179 "product_labels.bzl",
180 productReplacer.Replace(`
181# This file keeps a list of all the products in the android source tree, because they're
182# discovered as part of a preprocessing step before bazel runs.
183# TODO: When we start generating the platforms for more than just the
184# currently lunched product, they should all be listed here
185product_labels = [
Cole Faustf3cf34e2023-09-20 17:02:40 -0700186 "@soong_injection//product_config_platforms:mixed_builds_product",
187 "@//{PRODUCT_FOLDER}:{PRODUCT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700188`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800189 newFile(
190 "product_config_platforms",
191 "common.bazelrc",
192 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700193build --platform_mappings=platform_mappings
Cole Faustf3cf34e2023-09-20 17:02:40 -0700194build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
195build --//build/bazel/product_config:target_build_variant={VARIANT}
Cole Faustb85d1a12022-11-08 18:14:01 -0800196
Cole Faustf3cf34e2023-09-20 17:02:40 -0700197build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}
198build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86
199build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
200build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_bionic_x86_64
201build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86
202build:linux_musl_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800203`)),
204 newFile(
205 "product_config_platforms",
206 "linux.bazelrc",
207 productReplacer.Replace(`
Cole Faustf3cf34e2023-09-20 17:02:40 -0700208build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800209`)),
210 newFile(
211 "product_config_platforms",
212 "darwin.bazelrc",
213 productReplacer.Replace(`
Cole Faustf3cf34e2023-09-20 17:02:40 -0700214build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800215`)),
216 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700217 res.bp2buildFiles = []BazelFile{
Cole Faustf8231dd2023-04-21 17:37:11 -0700218 newFile(
219 "",
220 "platform_mappings",
221 platformMappingContent),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700222 newFile(
223 currentProductFolder,
224 "soong.variables.bzl",
225 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustf8231dd2023-04-21 17:37:11 -0700226 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700227
228 return res, nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700229}
Cole Faustb85d1a12022-11-08 18:14:01 -0800230
Cole Faust946d02c2023-08-03 16:08:09 -0700231func platformMappingContent(
Cole Faustcb193ec2023-09-20 16:01:18 -0700232 productLabelToVariables map[bazelLabel]*android.ProductVariables,
Cole Faust946d02c2023-08-03 16:08:09 -0700233 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
234 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf055db62023-07-24 15:17:03 -0700235 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700236
237 mergedConvertedModulePathMap := make(map[string]string)
238 for k, v := range convertedModulePathMap {
239 mergedConvertedModulePathMap[k] = v
240 }
241 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
242 if err != nil {
243 return "", err
244 }
245 for k, v := range additionalModuleNamesToPackages {
246 mergedConvertedModulePathMap[k] = v
247 }
248
Cole Fauste136dda2023-09-27 14:10:33 -0700249 productLabels := make([]bazelLabel, 0, len(productLabelToVariables))
250 for k := range productLabelToVariables {
251 productLabels = append(productLabels, k)
252 }
253 sort.Slice(productLabels, func(i, j int) bool {
254 return productLabels[i].Less(&productLabels[j])
255 })
Cole Faustf055db62023-07-24 15:17:03 -0700256 result.WriteString("platforms:\n")
Cole Fauste136dda2023-09-27 14:10:33 -0700257 for _, productLabel := range productLabels {
258 platformMappingSingleProduct(productLabel, productLabelToVariables[productLabel], soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700259 }
Cole Faustf055db62023-07-24 15:17:03 -0700260 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700261}
262
Cole Faust88c8efb2023-07-18 11:05:16 -0700263var bazelPlatformSuffixes = []string{
264 "",
265 "_darwin_arm64",
266 "_darwin_x86_64",
267 "_linux_bionic_arm64",
268 "_linux_bionic_x86_64",
269 "_linux_musl_x86",
270 "_linux_musl_x86_64",
271 "_linux_x86",
272 "_linux_x86_64",
273 "_windows_x86",
274 "_windows_x86_64",
275}
276
Cole Faust946d02c2023-08-03 16:08:09 -0700277func platformMappingSingleProduct(
Cole Faustcb193ec2023-09-20 16:01:18 -0700278 label bazelLabel,
Cole Faust946d02c2023-08-03 16:08:09 -0700279 productVariables *android.ProductVariables,
280 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
281 convertedModulePathMap map[string]string,
282 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700283
Cole Faust95c5cf82023-08-03 13:49:27 -0700284 platform_sdk_version := -1
285 if productVariables.Platform_sdk_version != nil {
286 platform_sdk_version = *productVariables.Platform_sdk_version
287 }
288
Cole Faust946d02c2023-08-03 16:08:09 -0700289 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
290 if proptools.String(productVariables.DefaultAppCertificate) != "" {
Cole Faust6054cdf2023-09-12 10:07:07 -0700291 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory"
Cole Faust946d02c2023-08-03 16:08:09 -0700292 }
293
Cole Faustf055db62023-07-24 15:17:03 -0700294 for _, suffix := range bazelPlatformSuffixes {
295 result.WriteString(" ")
Cole Faustcb193ec2023-09-20 16:01:18 -0700296 result.WriteString(label.String())
Cole Faustf055db62023-07-24 15:17:03 -0700297 result.WriteString(suffix)
298 result.WriteString("\n")
299 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 -0700300 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700301 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 -0700302 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
303 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 -0700304 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages))
Cole Faustf055db62023-07-24 15:17:03 -0700305 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
306 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700307 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
308 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
309 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
310 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700311 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700312 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
313 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
314 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700315 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 -0700316 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
Cole Faustcb193ec2023-09-20 16:01:18 -0700317 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label.String()))
Cole Faustf055db62023-07-24 15:17:03 -0700318 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700319 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
Cole Faust87c0c332023-07-31 12:10:12 -0700320 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
321 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
322 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 -0700323 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
324 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
325 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 -0700326 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 -0700327 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Romain Jobredeaux3132f842023-09-15 10:06:16 -0400328 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 -0700329 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
330 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
331 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Yu Liu2cc802a2023-09-05 17:19:45 -0700332 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission))
333 // Empty string can't be used as label_flag on the bazel side
334 if len(productVariables.ReleaseAconfigValueSets) > 0 {
335 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", productVariables.ReleaseAconfigValueSets))
336 }
337 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion))
Cole Faust95c5cf82023-08-03 13:49:27 -0700338 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700339 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faust87c0c332023-07-31 12:10:12 -0700340 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 -0700341 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700342 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700343 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
344 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 -0700345
346 for _, override := range productVariables.CertificateOverrides {
347 parts := strings.SplitN(override, ":", 2)
348 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
349 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
350 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
351 }
352 }
353 }
354
Cole Fauste136dda2023-09-27 14:10:33 -0700355 for _, namespace := range android.SortedKeys(productVariables.VendorVars) {
356 for _, variable := range android.SortedKeys(productVariables.VendorVars[namespace]) {
357 value := productVariables.VendorVars[namespace][variable]
Cole Faust87c0c332023-07-31 12:10:12 -0700358 key := namespace + "__" + variable
359 _, hasBool := soongConfigDefinitions.BoolVars[key]
360 _, hasString := soongConfigDefinitions.StringVars[key]
361 _, hasValue := soongConfigDefinitions.ValueVars[key]
362 if !hasBool && !hasString && !hasValue {
363 // Not all soong config variables are defined in Android.bp files. For example,
364 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
365 // way, that causes them to be present in the soong.variables file but not
366 // defined in an Android.bp file. There's also nothing stopping you from setting
367 // a variable in make that doesn't exist in soong. We only generate build
368 // settings for the ones that exist in soong, so skip all others.
369 continue
370 }
371 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
372 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))
373 }
374 if hasBool {
375 // Logic copied from soongConfig.Bool()
376 value = strings.ToLower(value)
377 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
378 value = "true"
379 } else {
380 value = "false"
381 }
382 }
383 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
384 }
385 }
Cole Faustf055db62023-07-24 15:17:03 -0700386 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700387}
388
389func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700390 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700391 productVarsReflect := reflect.ValueOf(&result).Elem()
392 for i := 0; i < productVarsReflect.NumField(); i++ {
393 field := productVarsReflect.Field(i)
394 fieldType := productVarsReflect.Type().Field(i)
395 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700396 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
397 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700398 // These variables have more complicated types, and we don't need them right now
399 continue
400 }
401 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700402 if name == "VendorVars" {
403 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
404 if err != nil {
405 return result, err
406 }
407 field.Set(reflect.ValueOf(vendorVars))
408 continue
409 }
Cole Faustf055db62023-07-24 15:17:03 -0700410 switch field.Type().Kind() {
411 case reflect.Bool:
412 val, err := starlark_import.Unmarshal[bool](in[name])
413 if err != nil {
414 return result, err
415 }
416 field.SetBool(val)
417 case reflect.String:
418 val, err := starlark_import.Unmarshal[string](in[name])
419 if err != nil {
420 return result, err
421 }
422 field.SetString(val)
423 case reflect.Slice:
424 if field.Type().Elem().Kind() != reflect.String {
425 return result, fmt.Errorf("slices of types other than strings are unimplemented")
426 }
427 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
428 if err != nil {
429 return result, err
430 }
431 field.Set(val)
432 case reflect.Pointer:
433 switch field.Type().Elem().Kind() {
434 case reflect.Bool:
435 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
436 if err != nil {
437 return result, err
438 }
439 field.Set(reflect.ValueOf(val))
440 case reflect.String:
441 val, err := starlark_import.UnmarshalNoneable[string](in[name])
442 if err != nil {
443 return result, err
444 }
445 field.Set(reflect.ValueOf(val))
446 case reflect.Int:
447 val, err := starlark_import.UnmarshalNoneable[int](in[name])
448 if err != nil {
449 return result, err
450 }
451 field.Set(reflect.ValueOf(val))
452 default:
453 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
454 }
455 default:
456 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
457 }
458 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700459 }
Cole Faustf055db62023-07-24 15:17:03 -0700460
Cole Faust87c0c332023-07-31 12:10:12 -0700461 result.Native_coverage = proptools.BoolPtr(
462 proptools.Bool(result.GcovCoverage) ||
463 proptools.Bool(result.ClangCoverage))
464
Cole Faustb85d1a12022-11-08 18:14:01 -0800465 return result, nil
466}
Cole Faust6054cdf2023-09-12 10:07:07 -0700467
Cole Faustcb193ec2023-09-20 16:01:18 -0700468func createTargets(productLabelsToVariables map[bazelLabel]*android.ProductVariables, res map[string]BazelTargets) {
Cole Faustb4cb0c82023-09-14 15:16:58 -0700469 createGeneratedAndroidCertificateDirectories(productLabelsToVariables, res)
Cole Faustcb193ec2023-09-20 16:01:18 -0700470 for label, variables := range productLabelsToVariables {
471 createSystemPartition(label, &variables.PartitionVarsForBazelMigrationOnlyDoNotUse, res)
472 }
Cole Faustb4cb0c82023-09-14 15:16:58 -0700473}
474
Cole Faustcb193ec2023-09-20 16:01:18 -0700475func createGeneratedAndroidCertificateDirectories(productLabelsToVariables map[bazelLabel]*android.ProductVariables, targets map[string]BazelTargets) {
Cole Faust6054cdf2023-09-12 10:07:07 -0700476 var allDefaultAppCertificateDirs []string
477 for _, productVariables := range productLabelsToVariables {
478 if proptools.String(productVariables.DefaultAppCertificate) != "" {
479 d := filepath.Dir(proptools.String(productVariables.DefaultAppCertificate))
480 if !android.InList(d, allDefaultAppCertificateDirs) {
481 allDefaultAppCertificateDirs = append(allDefaultAppCertificateDirs, d)
482 }
483 }
484 }
485 for _, dir := range allDefaultAppCertificateDirs {
Cole Faustb4cb0c82023-09-14 15:16:58 -0700486 content := `filegroup(
487 name = "generated_android_certificate_directory",
488 srcs = glob([
Cole Faust6054cdf2023-09-12 10:07:07 -0700489 "*.pk8",
490 "*.pem",
491 "*.avbpubkey",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700492 ]),
493 visibility = ["//visibility:public"],
494)`
495 targets[dir] = append(targets[dir], BazelTarget{
Cole Faust6054cdf2023-09-12 10:07:07 -0700496 name: "generated_android_certificate_directory",
497 packageName: dir,
498 content: content,
499 ruleClass: "filegroup",
500 })
501 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700502}
Cole Faustcb193ec2023-09-20 16:01:18 -0700503
504func createSystemPartition(platformLabel bazelLabel, variables *android.PartitionVariables, targets map[string]BazelTargets) {
505 if !variables.PartitionQualifiedVariables["system"].BuildingImage {
506 return
507 }
508
509 imageProps := generateImagePropDictionary(variables, "system")
510 imageProps["skip_fsck"] = "true"
511
512 var properties strings.Builder
513 for _, prop := range android.SortedKeys(imageProps) {
514 properties.WriteString(prop)
515 properties.WriteRune('=')
516 properties.WriteString(imageProps[prop])
517 properties.WriteRune('\n')
518 }
519
520 targets[platformLabel.pkg] = append(targets[platformLabel.pkg], BazelTarget{
521 name: "system_image",
522 packageName: platformLabel.pkg,
523 content: fmt.Sprintf(`partition(
524 name = "system_image",
525 base_staging_dir = "//build/bazel/bazel_sandwich:system_staging_dir",
526 base_staging_dir_file_list = "//build/bazel/bazel_sandwich:system_staging_dir_file_list",
527 root_dir = "//build/bazel/bazel_sandwich:root_staging_dir",
528 image_properties = """
529%s
530""",
531 type = "system",
532)`, properties.String()),
533 ruleClass: "partition",
534 loads: []BazelLoad{{
535 file: "//build/bazel/rules/partitions:partition.bzl",
536 symbols: []BazelLoadSymbol{{
537 symbol: "partition",
538 }},
539 }},
540 }, BazelTarget{
541 name: "system_image_test",
542 packageName: platformLabel.pkg,
543 content: `partition_diff_test(
544 name = "system_image_test",
545 partition1 = "//build/bazel/bazel_sandwich:make_system_image",
546 partition2 = ":system_image",
547)`,
548 ruleClass: "partition_diff_test",
549 loads: []BazelLoad{{
550 file: "//build/bazel/rules/partitions/diff:partition_diff.bzl",
551 symbols: []BazelLoadSymbol{{
552 symbol: "partition_diff_test",
553 }},
554 }},
555 }, BazelTarget{
556 name: "run_system_image_test",
557 packageName: platformLabel.pkg,
558 content: `run_test_in_build(
559 name = "run_system_image_test",
560 test = ":system_image_test",
561)`,
562 ruleClass: "run_test_in_build",
563 loads: []BazelLoad{{
564 file: "//build/bazel/bazel_sandwich:run_test_in_build.bzl",
565 symbols: []BazelLoadSymbol{{
566 symbol: "run_test_in_build",
567 }},
568 }},
569 })
570}
571
572var allPartitionTypes = []string{
573 "system",
574 "vendor",
575 "cache",
576 "userdata",
577 "product",
578 "system_ext",
579 "oem",
580 "odm",
581 "vendor_dlkm",
582 "odm_dlkm",
583 "system_dlkm",
584}
585
586// An equivalent of make's generate-image-prop-dictionary function
587func generateImagePropDictionary(variables *android.PartitionVariables, partitionType string) map[string]string {
588 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
589 if !ok {
590 panic("Unknown partitionType: " + partitionType)
591 }
592 ret := map[string]string{}
593 if partitionType == "system" {
594 if len(variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize) > 0 {
595 ret["system_other_size"] = variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize
596 }
597 if len(partitionQualifiedVariables.ProductHeadroom) > 0 {
598 ret["system_headroom"] = partitionQualifiedVariables.ProductHeadroom
599 }
600 addCommonRoFlagsToImageProps(variables, partitionType, ret)
601 }
602 // TODO: other partition-specific logic
603 if variables.TargetUserimagesUseExt2 {
604 ret["fs_type"] = "ext2"
605 } else if variables.TargetUserimagesUseExt3 {
606 ret["fs_type"] = "ext3"
607 } else if variables.TargetUserimagesUseExt4 {
608 ret["fs_type"] = "ext4"
609 }
610
611 if !variables.TargetUserimagesSparseExtDisabled {
612 ret["extfs_sparse_flag"] = "-s"
613 }
614 if !variables.TargetUserimagesSparseErofsDisabled {
615 ret["erofs_sparse_flag"] = "-s"
616 }
617 if !variables.TargetUserimagesSparseSquashfsDisabled {
618 ret["squashfs_sparse_flag"] = "-s"
619 }
620 if !variables.TargetUserimagesSparseF2fsDisabled {
621 ret["f2fs_sparse_flag"] = "-S"
622 }
623 erofsCompressor := variables.BoardErofsCompressor
624 if len(erofsCompressor) == 0 && hasErofsPartition(variables) {
625 if len(variables.BoardErofsUseLegacyCompression) > 0 {
626 erofsCompressor = "lz4"
627 } else {
628 erofsCompressor = "lz4hc,9"
629 }
630 }
631 if len(erofsCompressor) > 0 {
632 ret["erofs_default_compressor"] = erofsCompressor
633 }
634 if len(variables.BoardErofsCompressorHints) > 0 {
635 ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints
636 }
637 if len(variables.BoardErofsCompressorHints) > 0 {
638 ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints
639 }
640 if len(variables.BoardErofsPclusterSize) > 0 {
641 ret["erofs_pcluster_size"] = variables.BoardErofsPclusterSize
642 }
643 if len(variables.BoardErofsShareDupBlocks) > 0 {
644 ret["erofs_share_dup_blocks"] = variables.BoardErofsShareDupBlocks
645 }
646 if len(variables.BoardErofsUseLegacyCompression) > 0 {
647 ret["erofs_use_legacy_compression"] = variables.BoardErofsUseLegacyCompression
648 }
649 if len(variables.BoardExt4ShareDupBlocks) > 0 {
650 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
651 }
652 if len(variables.BoardFlashLogicalBlockSize) > 0 {
653 ret["flash_logical_block_size"] = variables.BoardFlashLogicalBlockSize
654 }
655 if len(variables.BoardFlashEraseBlockSize) > 0 {
656 ret["flash_erase_block_size"] = variables.BoardFlashEraseBlockSize
657 }
658 if len(variables.BoardExt4ShareDupBlocks) > 0 {
659 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
660 }
661 if len(variables.BoardExt4ShareDupBlocks) > 0 {
662 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
663 }
664 for _, partitionType := range allPartitionTypes {
665 if qualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]; ok && len(qualifiedVariables.ProductVerityPartition) > 0 {
666 ret[partitionType+"_verity_block_device"] = qualifiedVariables.ProductVerityPartition
667 }
668 }
669 // TODO: Vboot
670 // TODO: AVB
671 if variables.BoardUsesRecoveryAsBoot {
672 ret["recovery_as_boot"] = "true"
673 }
674 if variables.BoardBuildGkiBootImageWithoutRamdisk {
675 ret["gki_boot_image_without_ramdisk"] = "true"
676 }
677 if variables.ProductUseDynamicPartitionSize {
678 ret["use_dynamic_partition_size"] = "true"
679 }
680 if variables.CopyImagesForTargetFilesZip {
681 ret["use_fixed_timestamp"] = "true"
682 }
683 return ret
684}
685
686// Soong equivalent of make's add-common-ro-flags-to-image-props
687func addCommonRoFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) {
688 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
689 if !ok {
690 panic("Unknown partitionType: " + partitionType)
691 }
692 if len(partitionQualifiedVariables.BoardErofsCompressor) > 0 {
693 ret[partitionType+"_erofs_compressor"] = partitionQualifiedVariables.BoardErofsCompressor
694 }
695 if len(partitionQualifiedVariables.BoardErofsCompressHints) > 0 {
696 ret[partitionType+"_erofs_compress_hints"] = partitionQualifiedVariables.BoardErofsCompressHints
697 }
698 if len(partitionQualifiedVariables.BoardErofsPclusterSize) > 0 {
699 ret[partitionType+"_erofs_pcluster_size"] = partitionQualifiedVariables.BoardErofsPclusterSize
700 }
701 if len(partitionQualifiedVariables.BoardExtfsRsvPct) > 0 {
702 ret[partitionType+"_extfs_rsv_pct"] = partitionQualifiedVariables.BoardExtfsRsvPct
703 }
704 if len(partitionQualifiedVariables.BoardF2fsSloadCompressFlags) > 0 {
705 ret[partitionType+"_f2fs_sldc_flags"] = partitionQualifiedVariables.BoardF2fsSloadCompressFlags
706 }
707 if len(partitionQualifiedVariables.BoardFileSystemCompress) > 0 {
708 ret[partitionType+"_f2fs_compress"] = partitionQualifiedVariables.BoardFileSystemCompress
709 }
710 if len(partitionQualifiedVariables.BoardFileSystemType) > 0 {
711 ret[partitionType+"_fs_type"] = partitionQualifiedVariables.BoardFileSystemType
712 }
713 if len(partitionQualifiedVariables.BoardJournalSize) > 0 {
714 ret[partitionType+"_journal_size"] = partitionQualifiedVariables.BoardJournalSize
715 }
716 if len(partitionQualifiedVariables.BoardPartitionReservedSize) > 0 {
717 ret[partitionType+"_reserved_size"] = partitionQualifiedVariables.BoardPartitionReservedSize
718 }
719 if len(partitionQualifiedVariables.BoardPartitionSize) > 0 {
720 ret[partitionType+"_size"] = partitionQualifiedVariables.BoardPartitionSize
721 }
722 if len(partitionQualifiedVariables.BoardSquashfsBlockSize) > 0 {
723 ret[partitionType+"_squashfs_block_size"] = partitionQualifiedVariables.BoardSquashfsBlockSize
724 }
725 if len(partitionQualifiedVariables.BoardSquashfsCompressor) > 0 {
726 ret[partitionType+"_squashfs_compressor"] = partitionQualifiedVariables.BoardSquashfsCompressor
727 }
728 if len(partitionQualifiedVariables.BoardSquashfsCompressorOpt) > 0 {
729 ret[partitionType+"_squashfs_compressor_opt"] = partitionQualifiedVariables.BoardSquashfsCompressorOpt
730 }
731 if len(partitionQualifiedVariables.BoardSquashfsDisable4kAlign) > 0 {
732 ret[partitionType+"_squashfs_disable_4k_align"] = partitionQualifiedVariables.BoardSquashfsDisable4kAlign
733 }
734 if len(partitionQualifiedVariables.BoardPartitionSize) == 0 && len(partitionQualifiedVariables.BoardPartitionReservedSize) == 0 && len(partitionQualifiedVariables.ProductHeadroom) == 0 {
735 ret[partitionType+"_disable_sparse"] = "true"
736 }
737 addCommonFlagsToImageProps(variables, partitionType, ret)
738}
739
740func hasErofsPartition(variables *android.PartitionVariables) bool {
741 return variables.PartitionQualifiedVariables["product"].BoardFileSystemType == "erofs" ||
742 variables.PartitionQualifiedVariables["system_ext"].BoardFileSystemType == "erofs" ||
743 variables.PartitionQualifiedVariables["odm"].BoardFileSystemType == "erofs" ||
744 variables.PartitionQualifiedVariables["vendor"].BoardFileSystemType == "erofs" ||
745 variables.PartitionQualifiedVariables["system"].BoardFileSystemType == "erofs" ||
746 variables.PartitionQualifiedVariables["vendor_dlkm"].BoardFileSystemType == "erofs" ||
747 variables.PartitionQualifiedVariables["odm_dlkm"].BoardFileSystemType == "erofs" ||
748 variables.PartitionQualifiedVariables["system_dlkm"].BoardFileSystemType == "erofs"
749}
750
751// Soong equivalent of make's add-common-flags-to-image-props
752func addCommonFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) {
753 // The selinux_fc will be handled separately
754 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
755 if !ok {
756 panic("Unknown partitionType: " + partitionType)
757 }
758 ret["building_"+partitionType+"_image"] = boolToMakeString(partitionQualifiedVariables.BuildingImage)
759}
760
761func boolToMakeString(b bool) string {
762 if b {
763 return "true"
764 }
765 return ""
766}