blob: 7f26bef3b7cbdec2014994ce8713c1253c0cb7e2 [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"
Cole Faustb85d1a12022-11-08 18:14:01 -08006 "path/filepath"
Cole Faustf055db62023-07-24 15:17:03 -07007 "reflect"
Cole Fauste136dda2023-09-27 14:10:33 -07008 "sort"
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
Cole Faustcb193ec2023-09-20 16:01:18 -070025type bazelLabel struct {
26 repo string
27 pkg string
28 target string
29}
30
Yu Liueebb2592023-10-12 20:31:27 -070031const releaseAconfigValueSetsName = "release_aconfig_value_sets"
32
Cole Fauste136dda2023-09-27 14:10:33 -070033func (l *bazelLabel) Less(other *bazelLabel) bool {
34 if l.repo < other.repo {
35 return true
36 }
37 if l.repo > other.repo {
38 return false
39 }
40 if l.pkg < other.pkg {
41 return true
42 }
43 if l.pkg > other.pkg {
44 return false
45 }
46 return l.target < other.target
47}
48
Cole Faustcb193ec2023-09-20 16:01:18 -070049func (l *bazelLabel) String() string {
50 return fmt.Sprintf("@%s//%s:%s", l.repo, l.pkg, l.target)
51}
52
Cole Faust6054cdf2023-09-12 10:07:07 -070053func createProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070054 ctx *CodegenContext,
Cole Faust11edf552023-10-13 11:32:14 -070055 moduleNameToPartition map[string]string,
56 convertedModulePathMap map[string]string) (createProductConfigFilesResult, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080057 cfg := &ctx.config
58 targetProduct := "unknown"
59 if cfg.HasDeviceProduct() {
60 targetProduct = cfg.DeviceProduct()
61 }
62 targetBuildVariant := "user"
63 if cfg.Eng() {
64 targetBuildVariant = "eng"
65 } else if cfg.Debuggable() {
66 targetBuildVariant = "userdebug"
67 }
68
Cole Faust6054cdf2023-09-12 10:07:07 -070069 var res createProductConfigFilesResult
70
Cole Faust11edf552023-10-13 11:32:14 -070071 productVariables := ctx.Config().ProductVariables()
72 // TODO(b/306243251): For some reason, using the real value of native_coverage makes some select
73 // statements ambiguous
74 productVariables.Native_coverage = nil
75 productVariablesBytes, err := json.Marshal(productVariables)
Cole Faustf8231dd2023-04-21 17:37:11 -070076 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070077 return res, err
Cole Faustb85d1a12022-11-08 18:14:01 -080078 }
79
Cole Faustf3cf34e2023-09-20 17:02:40 -070080 currentProductFolder := fmt.Sprintf("build/bazel/products/%s", targetProduct)
Cole Faustcb193ec2023-09-20 16:01:18 -070081 if len(productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory) > 0 {
82 currentProductFolder = fmt.Sprintf("%s%s", productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory, targetProduct)
Cole Faustb4cb0c82023-09-14 15:16:58 -070083 }
Cole Faustb85d1a12022-11-08 18:14:01 -080084
85 productReplacer := strings.NewReplacer(
86 "{PRODUCT}", targetProduct,
87 "{VARIANT}", targetBuildVariant,
88 "{PRODUCT_FOLDER}", currentProductFolder)
89
Cole Faust946d02c2023-08-03 16:08:09 -070090 productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
91 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070092 return res, err
Cole Faust946d02c2023-08-03 16:08:09 -070093 }
94 productsForTesting := android.SortedKeys(productsForTestingMap)
95 for i := range productsForTesting {
96 productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i])
97 }
98
Cole Faustcb193ec2023-09-20 16:01:18 -070099 productLabelsToVariables := make(map[bazelLabel]*android.ProductVariables)
100 productLabelsToVariables[bazelLabel{
101 repo: "",
102 pkg: currentProductFolder,
103 target: targetProduct,
104 }] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -0700105 for product, productVariablesStarlark := range productsForTestingMap {
106 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
107 if err != nil {
108 return res, err
109 }
Cole Faustcb193ec2023-09-20 16:01:18 -0700110 productLabelsToVariables[bazelLabel{
111 repo: "",
112 pkg: "build/bazel/tests/products",
113 target: product,
114 }] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -0700115 }
116
Cole Faustb4cb0c82023-09-14 15:16:58 -0700117 res.bp2buildTargets = make(map[string]BazelTargets)
118 res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{
Cole Faustf3cf34e2023-09-20 17:02:40 -0700119 name: productReplacer.Replace("{PRODUCT}"),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700120 packageName: currentProductFolder,
121 content: productReplacer.Replace(`android_product(
Cole Faustf3cf34e2023-09-20 17:02:40 -0700122 name = "{PRODUCT}",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700123 soong_variables = _soong_variables,
124)`),
125 ruleClass: "android_product",
126 loads: []BazelLoad{
127 {
128 file: ":soong.variables.bzl",
129 symbols: []BazelLoadSymbol{{
130 symbol: "variables",
131 alias: "_soong_variables",
132 }},
133 },
134 {
135 file: "//build/bazel/product_config:android_product.bzl",
136 symbols: []BazelLoadSymbol{{symbol: "android_product"}},
137 },
138 },
139 })
Cole Faust11edf552023-10-13 11:32:14 -0700140 createTargets(ctx, productLabelsToVariables, moduleNameToPartition, convertedModulePathMap, res.bp2buildTargets)
Cole Faust6054cdf2023-09-12 10:07:07 -0700141
142 platformMappingContent, err := platformMappingContent(
143 productLabelsToVariables,
144 ctx.Config().Bp2buildSoongConfigDefinitions,
Cole Faust11edf552023-10-13 11:32:14 -0700145 convertedModulePathMap)
Cole Faust6054cdf2023-09-12 10:07:07 -0700146 if err != nil {
147 return res, err
148 }
149
150 res.injectionFiles = []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -0800151 newFile(
Cole Faustb85d1a12022-11-08 18:14:01 -0800152 "product_config_platforms",
153 "BUILD.bazel",
154 productReplacer.Replace(`
155package(default_visibility = [
156 "@//build/bazel/product_config:__subpackages__",
157 "@soong_injection//product_config_platforms:__subpackages__",
158])
Jingwen Chen583ab212023-05-30 09:45:23 +0000159
Cole Faustb4cb0c82023-09-14 15:16:58 -0700160load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
Jingwen Chen583ab212023-05-30 09:45:23 +0000161load("@//build/bazel/product_config:android_product.bzl", "android_product")
162
Cole Faust319abae2023-06-06 15:12:49 -0700163# Bazel will qualify its outputs by the platform name. When switching between products, this
164# means that soong-built files that depend on bazel-built files will suddenly get different
165# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
166# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
167# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000168android_product(
Cole Faustf3cf34e2023-09-20 17:02:40 -0700169 name = "mixed_builds_product",
Jingwen Chen583ab212023-05-30 09:45:23 +0000170 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000171 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000172)
Cole Faust117bb742023-03-29 14:46:20 -0700173`)),
174 newFile(
175 "product_config_platforms",
176 "product_labels.bzl",
177 productReplacer.Replace(`
178# This file keeps a list of all the products in the android source tree, because they're
179# discovered as part of a preprocessing step before bazel runs.
180# TODO: When we start generating the platforms for more than just the
181# currently lunched product, they should all be listed here
182product_labels = [
Cole Faustf3cf34e2023-09-20 17:02:40 -0700183 "@soong_injection//product_config_platforms:mixed_builds_product",
184 "@//{PRODUCT_FOLDER}:{PRODUCT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700185`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800186 newFile(
187 "product_config_platforms",
188 "common.bazelrc",
189 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700190build --platform_mappings=platform_mappings
Cole Faustf3cf34e2023-09-20 17:02:40 -0700191build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
192build --//build/bazel/product_config:target_build_variant={VARIANT}
Cole Faustb85d1a12022-11-08 18:14:01 -0800193
Cole Faustf3cf34e2023-09-20 17:02:40 -0700194build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}
195build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86
196build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
197build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_bionic_x86_64
198build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86
199build:linux_musl_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800200`)),
201 newFile(
202 "product_config_platforms",
203 "linux.bazelrc",
204 productReplacer.Replace(`
Cole Faustf3cf34e2023-09-20 17:02:40 -0700205build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800206`)),
207 newFile(
208 "product_config_platforms",
209 "darwin.bazelrc",
210 productReplacer.Replace(`
Cole Faustf3cf34e2023-09-20 17:02:40 -0700211build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800212`)),
213 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700214 res.bp2buildFiles = []BazelFile{
Cole Faustf8231dd2023-04-21 17:37:11 -0700215 newFile(
216 "",
217 "platform_mappings",
218 platformMappingContent),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700219 newFile(
220 currentProductFolder,
221 "soong.variables.bzl",
222 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustf8231dd2023-04-21 17:37:11 -0700223 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700224
225 return res, nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700226}
Cole Faustb85d1a12022-11-08 18:14:01 -0800227
Cole Faust946d02c2023-08-03 16:08:09 -0700228func platformMappingContent(
Cole Faustcb193ec2023-09-20 16:01:18 -0700229 productLabelToVariables map[bazelLabel]*android.ProductVariables,
Cole Faust946d02c2023-08-03 16:08:09 -0700230 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
231 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf055db62023-07-24 15:17:03 -0700232 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700233
234 mergedConvertedModulePathMap := make(map[string]string)
235 for k, v := range convertedModulePathMap {
236 mergedConvertedModulePathMap[k] = v
237 }
238 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
239 if err != nil {
240 return "", err
241 }
242 for k, v := range additionalModuleNamesToPackages {
243 mergedConvertedModulePathMap[k] = v
244 }
245
Cole Fauste136dda2023-09-27 14:10:33 -0700246 productLabels := make([]bazelLabel, 0, len(productLabelToVariables))
247 for k := range productLabelToVariables {
248 productLabels = append(productLabels, k)
249 }
250 sort.Slice(productLabels, func(i, j int) bool {
251 return productLabels[i].Less(&productLabels[j])
252 })
Cole Faustf055db62023-07-24 15:17:03 -0700253 result.WriteString("platforms:\n")
Cole Fauste136dda2023-09-27 14:10:33 -0700254 for _, productLabel := range productLabels {
255 platformMappingSingleProduct(productLabel, productLabelToVariables[productLabel], soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700256 }
Cole Faustf055db62023-07-24 15:17:03 -0700257 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700258}
259
Cole Faust88c8efb2023-07-18 11:05:16 -0700260var bazelPlatformSuffixes = []string{
261 "",
262 "_darwin_arm64",
263 "_darwin_x86_64",
264 "_linux_bionic_arm64",
265 "_linux_bionic_x86_64",
266 "_linux_musl_x86",
267 "_linux_musl_x86_64",
268 "_linux_x86",
269 "_linux_x86_64",
270 "_windows_x86",
271 "_windows_x86_64",
272}
273
Cole Faust946d02c2023-08-03 16:08:09 -0700274func platformMappingSingleProduct(
Cole Faustcb193ec2023-09-20 16:01:18 -0700275 label bazelLabel,
Cole Faust946d02c2023-08-03 16:08:09 -0700276 productVariables *android.ProductVariables,
277 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
278 convertedModulePathMap map[string]string,
279 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700280
Cole Faust95c5cf82023-08-03 13:49:27 -0700281 platform_sdk_version := -1
282 if productVariables.Platform_sdk_version != nil {
283 platform_sdk_version = *productVariables.Platform_sdk_version
284 }
285
Cole Faust946d02c2023-08-03 16:08:09 -0700286 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
287 if proptools.String(productVariables.DefaultAppCertificate) != "" {
Cole Faust6054cdf2023-09-12 10:07:07 -0700288 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory"
Cole Faust946d02c2023-08-03 16:08:09 -0700289 }
290
Romain Jobredeaux9c06ef32023-08-21 18:05:29 -0400291 // TODO: b/301598690 - commas can't be escaped in a string-list passed in a platform mapping,
292 // so commas are switched for ":" here, and must be back-substituted into commas
293 // wherever the AAPTCharacteristics product config variable is used.
294 AAPTConfig := []string{}
295 for _, conf := range productVariables.AAPTConfig {
296 AAPTConfig = append(AAPTConfig, strings.Replace(conf, ",", ":", -1))
297 }
298
Cole Faustf055db62023-07-24 15:17:03 -0700299 for _, suffix := range bazelPlatformSuffixes {
300 result.WriteString(" ")
Cole Faustcb193ec2023-09-20 16:01:18 -0700301 result.WriteString(label.String())
Cole Faustf055db62023-07-24 15:17:03 -0700302 result.WriteString(suffix)
303 result.WriteString("\n")
Romain Jobredeaux9c06ef32023-08-21 18:05:29 -0400304 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_characteristics=%s\n", proptools.String(productVariables.AAPTCharacteristics)))
305 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_config=%s\n", strings.Join(AAPTConfig, ",")))
306 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_preferred_config=%s\n", proptools.String(productVariables.AAPTPreferredConfig)))
Cole Faustf055db62023-07-24 15:17:03 -0700307 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 -0700308 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700309 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 -0700310 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
311 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 -0700312 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages))
Cole Faustf055db62023-07-24 15:17:03 -0700313 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
314 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700315 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
316 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
317 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
318 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700319 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700320 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
321 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
322 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700323 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 -0700324 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
Cole Faustcb193ec2023-09-20 16:01:18 -0700325 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label.String()))
Cole Faustf055db62023-07-24 15:17:03 -0700326 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700327 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 -0700328 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
329 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
330 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 -0700331 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
332 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
333 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 -0700334 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 -0700335 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Romain Jobredeaux3132f842023-09-15 10:06:16 -0400336 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_final=%t\n", proptools.Bool(productVariables.Platform_sdk_final)))
Cole Faustb5055392023-09-27 13:44:40 -0700337 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_security_patch=%s\n", proptools.String(productVariables.Platform_security_patch)))
338 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_last_stable=%s\n", proptools.String(productVariables.Platform_version_last_stable)))
Cole Faustf055db62023-07-24 15:17:03 -0700339 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
340 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
341 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Yu Liu2cc802a2023-09-05 17:19:45 -0700342 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission))
Yu Liueebb2592023-10-12 20:31:27 -0700343 releaseAconfigValueSets := "//build/bazel/product_config:empty_aconfig_value_sets"
Yu Liu2cc802a2023-09-05 17:19:45 -0700344 if len(productVariables.ReleaseAconfigValueSets) > 0 {
Yu Liueebb2592023-10-12 20:31:27 -0700345 releaseAconfigValueSets = "@//" + label.pkg + ":" + releaseAconfigValueSetsName + "_" + label.target
Yu Liu2cc802a2023-09-05 17:19:45 -0700346 }
Yu Liueebb2592023-10-12 20:31:27 -0700347 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", releaseAconfigValueSets))
Yu Liu2cc802a2023-09-05 17:19:45 -0700348 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion))
Cole Faust95c5cf82023-08-03 13:49:27 -0700349 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700350 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faust87c0c332023-07-31 12:10:12 -0700351 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 -0700352 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700353 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700354 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
355 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 -0700356
357 for _, override := range productVariables.CertificateOverrides {
358 parts := strings.SplitN(override, ":", 2)
359 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
360 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
361 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
362 }
363 }
364 }
365
Cole Fauste136dda2023-09-27 14:10:33 -0700366 for _, namespace := range android.SortedKeys(productVariables.VendorVars) {
367 for _, variable := range android.SortedKeys(productVariables.VendorVars[namespace]) {
368 value := productVariables.VendorVars[namespace][variable]
Cole Faust87c0c332023-07-31 12:10:12 -0700369 key := namespace + "__" + variable
370 _, hasBool := soongConfigDefinitions.BoolVars[key]
371 _, hasString := soongConfigDefinitions.StringVars[key]
372 _, hasValue := soongConfigDefinitions.ValueVars[key]
373 if !hasBool && !hasString && !hasValue {
374 // Not all soong config variables are defined in Android.bp files. For example,
375 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
376 // way, that causes them to be present in the soong.variables file but not
377 // defined in an Android.bp file. There's also nothing stopping you from setting
378 // a variable in make that doesn't exist in soong. We only generate build
379 // settings for the ones that exist in soong, so skip all others.
380 continue
381 }
382 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
383 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))
384 }
385 if hasBool {
386 // Logic copied from soongConfig.Bool()
387 value = strings.ToLower(value)
388 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
389 value = "true"
390 } else {
391 value = "false"
392 }
393 }
394 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
395 }
396 }
Cole Faustf055db62023-07-24 15:17:03 -0700397 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700398}
399
400func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700401 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700402 productVarsReflect := reflect.ValueOf(&result).Elem()
403 for i := 0; i < productVarsReflect.NumField(); i++ {
404 field := productVarsReflect.Field(i)
405 fieldType := productVarsReflect.Type().Field(i)
406 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700407 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
408 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700409 // These variables have more complicated types, and we don't need them right now
410 continue
411 }
412 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700413 if name == "VendorVars" {
414 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
415 if err != nil {
416 return result, err
417 }
418 field.Set(reflect.ValueOf(vendorVars))
419 continue
420 }
Cole Faustf055db62023-07-24 15:17:03 -0700421 switch field.Type().Kind() {
422 case reflect.Bool:
423 val, err := starlark_import.Unmarshal[bool](in[name])
424 if err != nil {
425 return result, err
426 }
427 field.SetBool(val)
428 case reflect.String:
429 val, err := starlark_import.Unmarshal[string](in[name])
430 if err != nil {
431 return result, err
432 }
433 field.SetString(val)
434 case reflect.Slice:
435 if field.Type().Elem().Kind() != reflect.String {
436 return result, fmt.Errorf("slices of types other than strings are unimplemented")
437 }
438 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
439 if err != nil {
440 return result, err
441 }
442 field.Set(val)
443 case reflect.Pointer:
444 switch field.Type().Elem().Kind() {
445 case reflect.Bool:
446 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
447 if err != nil {
448 return result, err
449 }
450 field.Set(reflect.ValueOf(val))
451 case reflect.String:
452 val, err := starlark_import.UnmarshalNoneable[string](in[name])
453 if err != nil {
454 return result, err
455 }
456 field.Set(reflect.ValueOf(val))
457 case reflect.Int:
458 val, err := starlark_import.UnmarshalNoneable[int](in[name])
459 if err != nil {
460 return result, err
461 }
462 field.Set(reflect.ValueOf(val))
463 default:
464 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
465 }
466 default:
467 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
468 }
469 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700470 }
Cole Faustf055db62023-07-24 15:17:03 -0700471
Cole Faust87c0c332023-07-31 12:10:12 -0700472 result.Native_coverage = proptools.BoolPtr(
473 proptools.Bool(result.GcovCoverage) ||
474 proptools.Bool(result.ClangCoverage))
475
Cole Faustb85d1a12022-11-08 18:14:01 -0800476 return result, nil
477}
Cole Faust6054cdf2023-09-12 10:07:07 -0700478
Cole Faust11edf552023-10-13 11:32:14 -0700479func createTargets(
480 ctx *CodegenContext,
481 productLabelsToVariables map[bazelLabel]*android.ProductVariables,
482 moduleNameToPartition map[string]string,
483 convertedModulePathMap map[string]string,
484 res map[string]BazelTargets) {
Cole Faustb4cb0c82023-09-14 15:16:58 -0700485 createGeneratedAndroidCertificateDirectories(productLabelsToVariables, res)
Cole Faustb5055392023-09-27 13:44:40 -0700486 createAvbKeyFilegroups(productLabelsToVariables, res)
Yu Liueebb2592023-10-12 20:31:27 -0700487 createReleaseAconfigValueSetsFilegroup(productLabelsToVariables, res)
Cole Faustcb193ec2023-09-20 16:01:18 -0700488 for label, variables := range productLabelsToVariables {
Cole Faust11edf552023-10-13 11:32:14 -0700489 createSystemPartition(ctx, label, &variables.PartitionVarsForBazelMigrationOnlyDoNotUse, moduleNameToPartition, convertedModulePathMap, res)
Cole Faustcb193ec2023-09-20 16:01:18 -0700490 }
Cole Faustb4cb0c82023-09-14 15:16:58 -0700491}
492
Cole Faustcb193ec2023-09-20 16:01:18 -0700493func createGeneratedAndroidCertificateDirectories(productLabelsToVariables map[bazelLabel]*android.ProductVariables, targets map[string]BazelTargets) {
Cole Faust6054cdf2023-09-12 10:07:07 -0700494 var allDefaultAppCertificateDirs []string
495 for _, productVariables := range productLabelsToVariables {
496 if proptools.String(productVariables.DefaultAppCertificate) != "" {
497 d := filepath.Dir(proptools.String(productVariables.DefaultAppCertificate))
498 if !android.InList(d, allDefaultAppCertificateDirs) {
499 allDefaultAppCertificateDirs = append(allDefaultAppCertificateDirs, d)
500 }
501 }
502 }
503 for _, dir := range allDefaultAppCertificateDirs {
Cole Faustb4cb0c82023-09-14 15:16:58 -0700504 content := `filegroup(
505 name = "generated_android_certificate_directory",
506 srcs = glob([
Cole Faust6054cdf2023-09-12 10:07:07 -0700507 "*.pk8",
508 "*.pem",
509 "*.avbpubkey",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700510 ]),
511 visibility = ["//visibility:public"],
512)`
513 targets[dir] = append(targets[dir], BazelTarget{
Cole Faust6054cdf2023-09-12 10:07:07 -0700514 name: "generated_android_certificate_directory",
515 packageName: dir,
516 content: content,
517 ruleClass: "filegroup",
518 })
519 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700520}
Cole Faustcb193ec2023-09-20 16:01:18 -0700521
Yu Liueebb2592023-10-12 20:31:27 -0700522func createReleaseAconfigValueSetsFilegroup(productLabelsToVariables map[bazelLabel]*android.ProductVariables, targets map[string]BazelTargets) {
523 for label, productVariables := range productLabelsToVariables {
524 if len(productVariables.ReleaseAconfigValueSets) > 0 {
525 key := label.target
526 dir := label.pkg
527 var value_sets strings.Builder
528 for _, value_set := range productVariables.ReleaseAconfigValueSets {
529 value_sets.WriteString(" \"" + value_set + "\",\n")
530 }
531
532 name := releaseAconfigValueSetsName + "_" + key
533 content := "aconfig_value_sets(\n" +
534 " name = \"" + name + "\",\n" +
535 " value_sets = [\n" +
536 value_sets.String() +
537 " ],\n" +
538 " visibility = [\"//visibility:public\"],\n" +
539 ")"
540 targets[dir] = append(targets[dir], BazelTarget{
541 name: name,
542 packageName: dir,
543 content: content,
544 ruleClass: "aconfig_value_sets",
545 loads: []BazelLoad{{
546 file: "//build/bazel/rules/aconfig:aconfig_value_sets.bzl",
547 symbols: []BazelLoadSymbol{{
548 symbol: "aconfig_value_sets",
549 }},
550 }},
551 })
552 }
553 }
554}
555
Cole Faustb5055392023-09-27 13:44:40 -0700556func createAvbKeyFilegroups(productLabelsToVariables map[bazelLabel]*android.ProductVariables, targets map[string]BazelTargets) {
557 var allAvbKeys []string
558 for _, productVariables := range productLabelsToVariables {
559 for _, partitionVariables := range productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.PartitionQualifiedVariables {
560 if partitionVariables.BoardAvbKeyPath != "" {
561 if !android.InList(partitionVariables.BoardAvbKeyPath, allAvbKeys) {
562 allAvbKeys = append(allAvbKeys, partitionVariables.BoardAvbKeyPath)
563 }
564 }
565 }
566 }
567 for _, key := range allAvbKeys {
568 dir := filepath.Dir(key)
569 name := filepath.Base(key)
570 content := fmt.Sprintf(`filegroup(
571 name = "%s_filegroup",
572 srcs = ["%s"],
573 visibility = ["//visibility:public"],
574)`, name, name)
575 targets[dir] = append(targets[dir], BazelTarget{
576 name: name + "_filegroup",
577 packageName: dir,
578 content: content,
579 ruleClass: "filegroup",
580 })
581 }
582}
583
Cole Faust11edf552023-10-13 11:32:14 -0700584func createSystemPartition(
585 ctx *CodegenContext,
586 platformLabel bazelLabel,
587 variables *android.PartitionVariables,
588 moduleNameToPartition map[string]string,
589 convertedModulePathMap map[string]string,
590 targets map[string]BazelTargets) {
Cole Faustcb193ec2023-09-20 16:01:18 -0700591 if !variables.PartitionQualifiedVariables["system"].BuildingImage {
592 return
593 }
Cole Faustb5055392023-09-27 13:44:40 -0700594 qualifiedVariables := variables.PartitionQualifiedVariables["system"]
Cole Faustcb193ec2023-09-20 16:01:18 -0700595
596 imageProps := generateImagePropDictionary(variables, "system")
597 imageProps["skip_fsck"] = "true"
598
599 var properties strings.Builder
600 for _, prop := range android.SortedKeys(imageProps) {
601 properties.WriteString(prop)
602 properties.WriteRune('=')
603 properties.WriteString(imageProps[prop])
604 properties.WriteRune('\n')
605 }
606
Cole Faustb5055392023-09-27 13:44:40 -0700607 var extraProperties strings.Builder
608 if variables.BoardAvbEnable {
609 extraProperties.WriteString(" avb_enable = True,\n")
610 extraProperties.WriteString(fmt.Sprintf(" avb_add_hashtree_footer_args = %q,\n", qualifiedVariables.BoardAvbAddHashtreeFooterArgs))
611 keypath := qualifiedVariables.BoardAvbKeyPath
612 if keypath != "" {
613 extraProperties.WriteString(fmt.Sprintf(" avb_key = \"//%s:%s\",\n", filepath.Dir(keypath), filepath.Base(keypath)+"_filegroup"))
614 extraProperties.WriteString(fmt.Sprintf(" avb_algorithm = %q,\n", qualifiedVariables.BoardAvbAlgorithm))
615 extraProperties.WriteString(fmt.Sprintf(" avb_rollback_index = %s,\n", qualifiedVariables.BoardAvbRollbackIndex))
616 extraProperties.WriteString(fmt.Sprintf(" avb_rollback_index_location = %s,\n", qualifiedVariables.BoardAvbRollbackIndexLocation))
617 }
618 }
619
Cole Faust11edf552023-10-13 11:32:14 -0700620 var deps []string
621 for _, mod := range variables.ProductPackages {
622 if path, ok := convertedModulePathMap[mod]; ok && ctx.Config().BazelContext.IsModuleNameAllowed(mod, false) {
623 if partition, ok := moduleNameToPartition[mod]; ok && partition == "system" {
624 if path == "//." {
625 path = "//"
626 }
627 deps = append(deps, fmt.Sprintf(" \"%s:%s\",\n", path, mod))
628 }
629 }
630 }
631 if len(deps) > 0 {
632 sort.Strings(deps)
633 extraProperties.WriteString(" deps = [\n")
634 for _, dep := range deps {
635 extraProperties.WriteString(dep)
636 }
637 extraProperties.WriteString(" ],\n")
638 }
639
Cole Faustcb193ec2023-09-20 16:01:18 -0700640 targets[platformLabel.pkg] = append(targets[platformLabel.pkg], BazelTarget{
641 name: "system_image",
642 packageName: platformLabel.pkg,
643 content: fmt.Sprintf(`partition(
644 name = "system_image",
645 base_staging_dir = "//build/bazel/bazel_sandwich:system_staging_dir",
646 base_staging_dir_file_list = "//build/bazel/bazel_sandwich:system_staging_dir_file_list",
647 root_dir = "//build/bazel/bazel_sandwich:root_staging_dir",
Cole Faustb5055392023-09-27 13:44:40 -0700648 selinux_file_contexts = "//build/bazel/bazel_sandwich:selinux_file_contexts",
Cole Faustcb193ec2023-09-20 16:01:18 -0700649 image_properties = """
650%s
651""",
Cole Faustb5055392023-09-27 13:44:40 -0700652%s
Cole Faustcb193ec2023-09-20 16:01:18 -0700653 type = "system",
Cole Faustb5055392023-09-27 13:44:40 -0700654)`, properties.String(), extraProperties.String()),
Cole Faustcb193ec2023-09-20 16:01:18 -0700655 ruleClass: "partition",
656 loads: []BazelLoad{{
657 file: "//build/bazel/rules/partitions:partition.bzl",
658 symbols: []BazelLoadSymbol{{
659 symbol: "partition",
660 }},
661 }},
662 }, BazelTarget{
663 name: "system_image_test",
664 packageName: platformLabel.pkg,
665 content: `partition_diff_test(
666 name = "system_image_test",
667 partition1 = "//build/bazel/bazel_sandwich:make_system_image",
668 partition2 = ":system_image",
669)`,
670 ruleClass: "partition_diff_test",
671 loads: []BazelLoad{{
672 file: "//build/bazel/rules/partitions/diff:partition_diff.bzl",
673 symbols: []BazelLoadSymbol{{
674 symbol: "partition_diff_test",
675 }},
676 }},
677 }, BazelTarget{
678 name: "run_system_image_test",
679 packageName: platformLabel.pkg,
680 content: `run_test_in_build(
681 name = "run_system_image_test",
682 test = ":system_image_test",
683)`,
684 ruleClass: "run_test_in_build",
685 loads: []BazelLoad{{
686 file: "//build/bazel/bazel_sandwich:run_test_in_build.bzl",
687 symbols: []BazelLoadSymbol{{
688 symbol: "run_test_in_build",
689 }},
690 }},
691 })
692}
693
694var allPartitionTypes = []string{
695 "system",
696 "vendor",
697 "cache",
698 "userdata",
699 "product",
700 "system_ext",
701 "oem",
702 "odm",
703 "vendor_dlkm",
704 "odm_dlkm",
705 "system_dlkm",
706}
707
708// An equivalent of make's generate-image-prop-dictionary function
709func generateImagePropDictionary(variables *android.PartitionVariables, partitionType string) map[string]string {
710 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
711 if !ok {
712 panic("Unknown partitionType: " + partitionType)
713 }
714 ret := map[string]string{}
715 if partitionType == "system" {
716 if len(variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize) > 0 {
717 ret["system_other_size"] = variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize
718 }
719 if len(partitionQualifiedVariables.ProductHeadroom) > 0 {
720 ret["system_headroom"] = partitionQualifiedVariables.ProductHeadroom
721 }
722 addCommonRoFlagsToImageProps(variables, partitionType, ret)
723 }
724 // TODO: other partition-specific logic
725 if variables.TargetUserimagesUseExt2 {
726 ret["fs_type"] = "ext2"
727 } else if variables.TargetUserimagesUseExt3 {
728 ret["fs_type"] = "ext3"
729 } else if variables.TargetUserimagesUseExt4 {
730 ret["fs_type"] = "ext4"
731 }
732
733 if !variables.TargetUserimagesSparseExtDisabled {
734 ret["extfs_sparse_flag"] = "-s"
735 }
736 if !variables.TargetUserimagesSparseErofsDisabled {
737 ret["erofs_sparse_flag"] = "-s"
738 }
739 if !variables.TargetUserimagesSparseSquashfsDisabled {
740 ret["squashfs_sparse_flag"] = "-s"
741 }
742 if !variables.TargetUserimagesSparseF2fsDisabled {
743 ret["f2fs_sparse_flag"] = "-S"
744 }
745 erofsCompressor := variables.BoardErofsCompressor
746 if len(erofsCompressor) == 0 && hasErofsPartition(variables) {
747 if len(variables.BoardErofsUseLegacyCompression) > 0 {
748 erofsCompressor = "lz4"
749 } else {
750 erofsCompressor = "lz4hc,9"
751 }
752 }
753 if len(erofsCompressor) > 0 {
754 ret["erofs_default_compressor"] = erofsCompressor
755 }
756 if len(variables.BoardErofsCompressorHints) > 0 {
757 ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints
758 }
759 if len(variables.BoardErofsCompressorHints) > 0 {
760 ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints
761 }
762 if len(variables.BoardErofsPclusterSize) > 0 {
763 ret["erofs_pcluster_size"] = variables.BoardErofsPclusterSize
764 }
765 if len(variables.BoardErofsShareDupBlocks) > 0 {
766 ret["erofs_share_dup_blocks"] = variables.BoardErofsShareDupBlocks
767 }
768 if len(variables.BoardErofsUseLegacyCompression) > 0 {
769 ret["erofs_use_legacy_compression"] = variables.BoardErofsUseLegacyCompression
770 }
771 if len(variables.BoardExt4ShareDupBlocks) > 0 {
772 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
773 }
774 if len(variables.BoardFlashLogicalBlockSize) > 0 {
775 ret["flash_logical_block_size"] = variables.BoardFlashLogicalBlockSize
776 }
777 if len(variables.BoardFlashEraseBlockSize) > 0 {
778 ret["flash_erase_block_size"] = variables.BoardFlashEraseBlockSize
779 }
780 if len(variables.BoardExt4ShareDupBlocks) > 0 {
781 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
782 }
783 if len(variables.BoardExt4ShareDupBlocks) > 0 {
784 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
785 }
786 for _, partitionType := range allPartitionTypes {
787 if qualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]; ok && len(qualifiedVariables.ProductVerityPartition) > 0 {
788 ret[partitionType+"_verity_block_device"] = qualifiedVariables.ProductVerityPartition
789 }
790 }
791 // TODO: Vboot
792 // TODO: AVB
793 if variables.BoardUsesRecoveryAsBoot {
794 ret["recovery_as_boot"] = "true"
795 }
796 if variables.BoardBuildGkiBootImageWithoutRamdisk {
797 ret["gki_boot_image_without_ramdisk"] = "true"
798 }
799 if variables.ProductUseDynamicPartitionSize {
800 ret["use_dynamic_partition_size"] = "true"
801 }
802 if variables.CopyImagesForTargetFilesZip {
803 ret["use_fixed_timestamp"] = "true"
804 }
805 return ret
806}
807
808// Soong equivalent of make's add-common-ro-flags-to-image-props
809func addCommonRoFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) {
810 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
811 if !ok {
812 panic("Unknown partitionType: " + partitionType)
813 }
814 if len(partitionQualifiedVariables.BoardErofsCompressor) > 0 {
815 ret[partitionType+"_erofs_compressor"] = partitionQualifiedVariables.BoardErofsCompressor
816 }
817 if len(partitionQualifiedVariables.BoardErofsCompressHints) > 0 {
818 ret[partitionType+"_erofs_compress_hints"] = partitionQualifiedVariables.BoardErofsCompressHints
819 }
820 if len(partitionQualifiedVariables.BoardErofsPclusterSize) > 0 {
821 ret[partitionType+"_erofs_pcluster_size"] = partitionQualifiedVariables.BoardErofsPclusterSize
822 }
823 if len(partitionQualifiedVariables.BoardExtfsRsvPct) > 0 {
824 ret[partitionType+"_extfs_rsv_pct"] = partitionQualifiedVariables.BoardExtfsRsvPct
825 }
826 if len(partitionQualifiedVariables.BoardF2fsSloadCompressFlags) > 0 {
827 ret[partitionType+"_f2fs_sldc_flags"] = partitionQualifiedVariables.BoardF2fsSloadCompressFlags
828 }
829 if len(partitionQualifiedVariables.BoardFileSystemCompress) > 0 {
830 ret[partitionType+"_f2fs_compress"] = partitionQualifiedVariables.BoardFileSystemCompress
831 }
832 if len(partitionQualifiedVariables.BoardFileSystemType) > 0 {
833 ret[partitionType+"_fs_type"] = partitionQualifiedVariables.BoardFileSystemType
834 }
835 if len(partitionQualifiedVariables.BoardJournalSize) > 0 {
836 ret[partitionType+"_journal_size"] = partitionQualifiedVariables.BoardJournalSize
837 }
838 if len(partitionQualifiedVariables.BoardPartitionReservedSize) > 0 {
839 ret[partitionType+"_reserved_size"] = partitionQualifiedVariables.BoardPartitionReservedSize
840 }
841 if len(partitionQualifiedVariables.BoardPartitionSize) > 0 {
842 ret[partitionType+"_size"] = partitionQualifiedVariables.BoardPartitionSize
843 }
844 if len(partitionQualifiedVariables.BoardSquashfsBlockSize) > 0 {
845 ret[partitionType+"_squashfs_block_size"] = partitionQualifiedVariables.BoardSquashfsBlockSize
846 }
847 if len(partitionQualifiedVariables.BoardSquashfsCompressor) > 0 {
848 ret[partitionType+"_squashfs_compressor"] = partitionQualifiedVariables.BoardSquashfsCompressor
849 }
850 if len(partitionQualifiedVariables.BoardSquashfsCompressorOpt) > 0 {
851 ret[partitionType+"_squashfs_compressor_opt"] = partitionQualifiedVariables.BoardSquashfsCompressorOpt
852 }
853 if len(partitionQualifiedVariables.BoardSquashfsDisable4kAlign) > 0 {
854 ret[partitionType+"_squashfs_disable_4k_align"] = partitionQualifiedVariables.BoardSquashfsDisable4kAlign
855 }
856 if len(partitionQualifiedVariables.BoardPartitionSize) == 0 && len(partitionQualifiedVariables.BoardPartitionReservedSize) == 0 && len(partitionQualifiedVariables.ProductHeadroom) == 0 {
857 ret[partitionType+"_disable_sparse"] = "true"
858 }
859 addCommonFlagsToImageProps(variables, partitionType, ret)
860}
861
862func hasErofsPartition(variables *android.PartitionVariables) bool {
863 return variables.PartitionQualifiedVariables["product"].BoardFileSystemType == "erofs" ||
864 variables.PartitionQualifiedVariables["system_ext"].BoardFileSystemType == "erofs" ||
865 variables.PartitionQualifiedVariables["odm"].BoardFileSystemType == "erofs" ||
866 variables.PartitionQualifiedVariables["vendor"].BoardFileSystemType == "erofs" ||
867 variables.PartitionQualifiedVariables["system"].BoardFileSystemType == "erofs" ||
868 variables.PartitionQualifiedVariables["vendor_dlkm"].BoardFileSystemType == "erofs" ||
869 variables.PartitionQualifiedVariables["odm_dlkm"].BoardFileSystemType == "erofs" ||
870 variables.PartitionQualifiedVariables["system_dlkm"].BoardFileSystemType == "erofs"
871}
872
873// Soong equivalent of make's add-common-flags-to-image-props
874func addCommonFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) {
875 // The selinux_fc will be handled separately
876 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
877 if !ok {
878 panic("Unknown partitionType: " + partitionType)
879 }
880 ret["building_"+partitionType+"_image"] = boolToMakeString(partitionQualifiedVariables.BuildingImage)
881}
882
883func boolToMakeString(b bool) string {
884 if b {
885 return "true"
886 }
887 return ""
888}