blob: 2adccccfd247fa34cdc658b0c0d7940eb4db93a3 [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
Yu Liueebb2592023-10-12 20:31:27 -070032const releaseAconfigValueSetsName = "release_aconfig_value_sets"
33
Cole Fauste136dda2023-09-27 14:10:33 -070034func (l *bazelLabel) Less(other *bazelLabel) bool {
35 if l.repo < other.repo {
36 return true
37 }
38 if l.repo > other.repo {
39 return false
40 }
41 if l.pkg < other.pkg {
42 return true
43 }
44 if l.pkg > other.pkg {
45 return false
46 }
47 return l.target < other.target
48}
49
Cole Faustcb193ec2023-09-20 16:01:18 -070050func (l *bazelLabel) String() string {
51 return fmt.Sprintf("@%s//%s:%s", l.repo, l.pkg, l.target)
52}
53
Cole Faust6054cdf2023-09-12 10:07:07 -070054func createProductConfigFiles(
Cole Faust946d02c2023-08-03 16:08:09 -070055 ctx *CodegenContext,
Cole Faust6054cdf2023-09-12 10:07:07 -070056 metrics CodegenMetrics) (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 Faustb85d1a12022-11-08 18:14:01 -080071 productVariablesFileName := cfg.ProductVariablesFileName
72 if !strings.HasPrefix(productVariablesFileName, "/") {
73 productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
74 }
Cole Faustf8231dd2023-04-21 17:37:11 -070075 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080076 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070077 return res, err
Cole Faustf8231dd2023-04-21 17:37:11 -070078 }
79 productVariables := android.ProductVariables{}
80 err = json.Unmarshal(productVariablesBytes, &productVariables)
81 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070082 return res, err
Cole Faustb85d1a12022-11-08 18:14:01 -080083 }
84
Cole Faustf3cf34e2023-09-20 17:02:40 -070085 currentProductFolder := fmt.Sprintf("build/bazel/products/%s", targetProduct)
Cole Faustcb193ec2023-09-20 16:01:18 -070086 if len(productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory) > 0 {
87 currentProductFolder = fmt.Sprintf("%s%s", productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory, targetProduct)
Cole Faustb4cb0c82023-09-14 15:16:58 -070088 }
Cole Faustb85d1a12022-11-08 18:14:01 -080089
90 productReplacer := strings.NewReplacer(
91 "{PRODUCT}", targetProduct,
92 "{VARIANT}", targetBuildVariant,
93 "{PRODUCT_FOLDER}", currentProductFolder)
94
Cole Faust946d02c2023-08-03 16:08:09 -070095 productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
96 if err != nil {
Cole Faust6054cdf2023-09-12 10:07:07 -070097 return res, err
Cole Faust946d02c2023-08-03 16:08:09 -070098 }
99 productsForTesting := android.SortedKeys(productsForTestingMap)
100 for i := range productsForTesting {
101 productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i])
102 }
103
Cole Faustcb193ec2023-09-20 16:01:18 -0700104 productLabelsToVariables := make(map[bazelLabel]*android.ProductVariables)
105 productLabelsToVariables[bazelLabel{
106 repo: "",
107 pkg: currentProductFolder,
108 target: targetProduct,
109 }] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -0700110 for product, productVariablesStarlark := range productsForTestingMap {
111 productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
112 if err != nil {
113 return res, err
114 }
Cole Faustcb193ec2023-09-20 16:01:18 -0700115 productLabelsToVariables[bazelLabel{
116 repo: "",
117 pkg: "build/bazel/tests/products",
118 target: product,
119 }] = &productVariables
Cole Faust6054cdf2023-09-12 10:07:07 -0700120 }
121
Cole Faustb4cb0c82023-09-14 15:16:58 -0700122 res.bp2buildTargets = make(map[string]BazelTargets)
123 res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{
Cole Faustf3cf34e2023-09-20 17:02:40 -0700124 name: productReplacer.Replace("{PRODUCT}"),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700125 packageName: currentProductFolder,
126 content: productReplacer.Replace(`android_product(
Cole Faustf3cf34e2023-09-20 17:02:40 -0700127 name = "{PRODUCT}",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700128 soong_variables = _soong_variables,
129)`),
130 ruleClass: "android_product",
131 loads: []BazelLoad{
132 {
133 file: ":soong.variables.bzl",
134 symbols: []BazelLoadSymbol{{
135 symbol: "variables",
136 alias: "_soong_variables",
137 }},
138 },
139 {
140 file: "//build/bazel/product_config:android_product.bzl",
141 symbols: []BazelLoadSymbol{{symbol: "android_product"}},
142 },
143 },
144 })
145 createTargets(productLabelsToVariables, res.bp2buildTargets)
Cole Faust6054cdf2023-09-12 10:07:07 -0700146
147 platformMappingContent, err := platformMappingContent(
148 productLabelsToVariables,
149 ctx.Config().Bp2buildSoongConfigDefinitions,
150 metrics.convertedModulePathMap)
151 if err != nil {
152 return res, err
153 }
154
155 res.injectionFiles = []BazelFile{
Cole Faustb85d1a12022-11-08 18:14:01 -0800156 newFile(
Cole Faustb85d1a12022-11-08 18:14:01 -0800157 "product_config_platforms",
158 "BUILD.bazel",
159 productReplacer.Replace(`
160package(default_visibility = [
161 "@//build/bazel/product_config:__subpackages__",
162 "@soong_injection//product_config_platforms:__subpackages__",
163])
Jingwen Chen583ab212023-05-30 09:45:23 +0000164
Cole Faustb4cb0c82023-09-14 15:16:58 -0700165load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
Jingwen Chen583ab212023-05-30 09:45:23 +0000166load("@//build/bazel/product_config:android_product.bzl", "android_product")
167
Cole Faust319abae2023-06-06 15:12:49 -0700168# Bazel will qualify its outputs by the platform name. When switching between products, this
169# means that soong-built files that depend on bazel-built files will suddenly get different
170# dependency files, because the path changes, and they will be rebuilt. In order to avoid this
171# extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
172# are always under the same path.
Jingwen Chen583ab212023-05-30 09:45:23 +0000173android_product(
Cole Faustf3cf34e2023-09-20 17:02:40 -0700174 name = "mixed_builds_product",
Jingwen Chen583ab212023-05-30 09:45:23 +0000175 soong_variables = _soong_variables,
Cole Faustbc65a3f2023-08-01 16:38:55 +0000176 extra_constraints = ["@//build/bazel/platforms:mixed_builds"],
Jingwen Chen583ab212023-05-30 09:45:23 +0000177)
Cole Faust117bb742023-03-29 14:46:20 -0700178`)),
179 newFile(
180 "product_config_platforms",
181 "product_labels.bzl",
182 productReplacer.Replace(`
183# This file keeps a list of all the products in the android source tree, because they're
184# discovered as part of a preprocessing step before bazel runs.
185# TODO: When we start generating the platforms for more than just the
186# currently lunched product, they should all be listed here
187product_labels = [
Cole Faustf3cf34e2023-09-20 17:02:40 -0700188 "@soong_injection//product_config_platforms:mixed_builds_product",
189 "@//{PRODUCT_FOLDER}:{PRODUCT}",
Cole Faust946d02c2023-08-03 16:08:09 -0700190`)+strings.Join(productsForTesting, "\n")+"\n]\n"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800191 newFile(
192 "product_config_platforms",
193 "common.bazelrc",
194 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700195build --platform_mappings=platform_mappings
Cole Faustf3cf34e2023-09-20 17:02:40 -0700196build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
197build --//build/bazel/product_config:target_build_variant={VARIANT}
Cole Faustb85d1a12022-11-08 18:14:01 -0800198
Cole Faustf3cf34e2023-09-20 17:02:40 -0700199build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}
200build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86
201build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
202build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_bionic_x86_64
203build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86
204build:linux_musl_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800205`)),
206 newFile(
207 "product_config_platforms",
208 "linux.bazelrc",
209 productReplacer.Replace(`
Cole Faustf3cf34e2023-09-20 17:02:40 -0700210build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800211`)),
212 newFile(
213 "product_config_platforms",
214 "darwin.bazelrc",
215 productReplacer.Replace(`
Cole Faustf3cf34e2023-09-20 17:02:40 -0700216build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800217`)),
218 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700219 res.bp2buildFiles = []BazelFile{
Cole Faustf8231dd2023-04-21 17:37:11 -0700220 newFile(
221 "",
222 "platform_mappings",
223 platformMappingContent),
Cole Faustb4cb0c82023-09-14 15:16:58 -0700224 newFile(
225 currentProductFolder,
226 "soong.variables.bzl",
227 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustf8231dd2023-04-21 17:37:11 -0700228 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700229
230 return res, nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700231}
Cole Faustb85d1a12022-11-08 18:14:01 -0800232
Cole Faust946d02c2023-08-03 16:08:09 -0700233func platformMappingContent(
Cole Faustcb193ec2023-09-20 16:01:18 -0700234 productLabelToVariables map[bazelLabel]*android.ProductVariables,
Cole Faust946d02c2023-08-03 16:08:09 -0700235 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
236 convertedModulePathMap map[string]string) (string, error) {
Cole Faustf055db62023-07-24 15:17:03 -0700237 var result strings.Builder
Cole Faust946d02c2023-08-03 16:08:09 -0700238
239 mergedConvertedModulePathMap := make(map[string]string)
240 for k, v := range convertedModulePathMap {
241 mergedConvertedModulePathMap[k] = v
242 }
243 additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages")
244 if err != nil {
245 return "", err
246 }
247 for k, v := range additionalModuleNamesToPackages {
248 mergedConvertedModulePathMap[k] = v
249 }
250
Cole Fauste136dda2023-09-27 14:10:33 -0700251 productLabels := make([]bazelLabel, 0, len(productLabelToVariables))
252 for k := range productLabelToVariables {
253 productLabels = append(productLabels, k)
254 }
255 sort.Slice(productLabels, func(i, j int) bool {
256 return productLabels[i].Less(&productLabels[j])
257 })
Cole Faustf055db62023-07-24 15:17:03 -0700258 result.WriteString("platforms:\n")
Cole Fauste136dda2023-09-27 14:10:33 -0700259 for _, productLabel := range productLabels {
260 platformMappingSingleProduct(productLabel, productLabelToVariables[productLabel], soongConfigDefinitions, mergedConvertedModulePathMap, &result)
Cole Faustf8231dd2023-04-21 17:37:11 -0700261 }
Cole Faustf055db62023-07-24 15:17:03 -0700262 return result.String(), nil
Cole Faustf8231dd2023-04-21 17:37:11 -0700263}
264
Cole Faust88c8efb2023-07-18 11:05:16 -0700265var bazelPlatformSuffixes = []string{
266 "",
267 "_darwin_arm64",
268 "_darwin_x86_64",
269 "_linux_bionic_arm64",
270 "_linux_bionic_x86_64",
271 "_linux_musl_x86",
272 "_linux_musl_x86_64",
273 "_linux_x86",
274 "_linux_x86_64",
275 "_windows_x86",
276 "_windows_x86_64",
277}
278
Cole Faust946d02c2023-08-03 16:08:09 -0700279func platformMappingSingleProduct(
Cole Faustcb193ec2023-09-20 16:01:18 -0700280 label bazelLabel,
Cole Faust946d02c2023-08-03 16:08:09 -0700281 productVariables *android.ProductVariables,
282 soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions,
283 convertedModulePathMap map[string]string,
284 result *strings.Builder) {
Cole Faustf055db62023-07-24 15:17:03 -0700285
Cole Faust95c5cf82023-08-03 13:49:27 -0700286 platform_sdk_version := -1
287 if productVariables.Platform_sdk_version != nil {
288 platform_sdk_version = *productVariables.Platform_sdk_version
289 }
290
Cole Faust946d02c2023-08-03 16:08:09 -0700291 defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup"
292 if proptools.String(productVariables.DefaultAppCertificate) != "" {
Cole Faust6054cdf2023-09-12 10:07:07 -0700293 defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory"
Cole Faust946d02c2023-08-03 16:08:09 -0700294 }
295
Romain Jobredeaux9c06ef32023-08-21 18:05:29 -0400296 // TODO: b/301598690 - commas can't be escaped in a string-list passed in a platform mapping,
297 // so commas are switched for ":" here, and must be back-substituted into commas
298 // wherever the AAPTCharacteristics product config variable is used.
299 AAPTConfig := []string{}
300 for _, conf := range productVariables.AAPTConfig {
301 AAPTConfig = append(AAPTConfig, strings.Replace(conf, ",", ":", -1))
302 }
303
Cole Faustf055db62023-07-24 15:17:03 -0700304 for _, suffix := range bazelPlatformSuffixes {
305 result.WriteString(" ")
Cole Faustcb193ec2023-09-20 16:01:18 -0700306 result.WriteString(label.String())
Cole Faustf055db62023-07-24 15:17:03 -0700307 result.WriteString(suffix)
308 result.WriteString("\n")
Romain Jobredeaux9c06ef32023-08-21 18:05:29 -0400309 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_characteristics=%s\n", proptools.String(productVariables.AAPTCharacteristics)))
310 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_config=%s\n", strings.Join(AAPTConfig, ",")))
311 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_preferred_config=%s\n", proptools.String(productVariables.AAPTPreferredConfig)))
Cole Faustf055db62023-07-24 15:17:03 -0700312 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 -0700313 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
Cole Faustf055db62023-07-24 15:17:03 -0700314 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 -0700315 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
316 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 -0700317 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages))
Cole Faustf055db62023-07-24 15:17:03 -0700318 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
319 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
Cole Faustf055db62023-07-24 15:17:03 -0700320 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
321 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
322 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
323 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
Cole Faust946d02c2023-08-03 16:08:09 -0700324 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup))
Cole Faustf055db62023-07-24 15:17:03 -0700325 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
326 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
327 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
Juan Yescas01065602023-08-09 08:34:37 -0700328 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 -0700329 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
Cole Faustcb193ec2023-09-20 16:01:18 -0700330 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label.String()))
Cole Faustf055db62023-07-24 15:17:03 -0700331 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
Cole Faust87c0c332023-07-31 12:10:12 -0700332 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 -0700333 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
334 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
335 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 -0700336 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ",")))
337 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ",")))
338 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 -0700339 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 -0700340 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
Romain Jobredeaux3132f842023-09-15 10:06:16 -0400341 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 -0700342 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_security_patch=%s\n", proptools.String(productVariables.Platform_security_patch)))
343 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 -0700344 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
345 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
346 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
Yu Liu2cc802a2023-09-05 17:19:45 -0700347 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission))
Yu Liueebb2592023-10-12 20:31:27 -0700348 releaseAconfigValueSets := "//build/bazel/product_config:empty_aconfig_value_sets"
Yu Liu2cc802a2023-09-05 17:19:45 -0700349 if len(productVariables.ReleaseAconfigValueSets) > 0 {
Yu Liueebb2592023-10-12 20:31:27 -0700350 releaseAconfigValueSets = "@//" + label.pkg + ":" + releaseAconfigValueSetsName + "_" + label.target
Yu Liu2cc802a2023-09-05 17:19:45 -0700351 }
Yu Liueebb2592023-10-12 20:31:27 -0700352 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", releaseAconfigValueSets))
Yu Liu2cc802a2023-09-05 17:19:45 -0700353 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion))
Cole Faust95c5cf82023-08-03 13:49:27 -0700354 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version))
Cole Faust87c0c332023-07-31 12:10:12 -0700355 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
Cole Faust87c0c332023-07-31 12:10:12 -0700356 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 -0700357 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
Cole Faust87c0c332023-07-31 12:10:12 -0700358 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
Cole Faustf055db62023-07-24 15:17:03 -0700359 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
360 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 -0700361
362 for _, override := range productVariables.CertificateOverrides {
363 parts := strings.SplitN(override, ":", 2)
364 if apexPath, ok := convertedModulePathMap[parts[0]]; ok {
365 if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok {
366 result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1]))
367 }
368 }
369 }
370
Cole Fauste136dda2023-09-27 14:10:33 -0700371 for _, namespace := range android.SortedKeys(productVariables.VendorVars) {
372 for _, variable := range android.SortedKeys(productVariables.VendorVars[namespace]) {
373 value := productVariables.VendorVars[namespace][variable]
Cole Faust87c0c332023-07-31 12:10:12 -0700374 key := namespace + "__" + variable
375 _, hasBool := soongConfigDefinitions.BoolVars[key]
376 _, hasString := soongConfigDefinitions.StringVars[key]
377 _, hasValue := soongConfigDefinitions.ValueVars[key]
378 if !hasBool && !hasString && !hasValue {
379 // Not all soong config variables are defined in Android.bp files. For example,
380 // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
381 // way, that causes them to be present in the soong.variables file but not
382 // defined in an Android.bp file. There's also nothing stopping you from setting
383 // a variable in make that doesn't exist in soong. We only generate build
384 // settings for the ones that exist in soong, so skip all others.
385 continue
386 }
387 if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
388 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))
389 }
390 if hasBool {
391 // Logic copied from soongConfig.Bool()
392 value = strings.ToLower(value)
393 if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
394 value = "true"
395 } else {
396 value = "false"
397 }
398 }
399 result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
400 }
401 }
Cole Faustf055db62023-07-24 15:17:03 -0700402 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700403}
404
405func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
Cole Faustf8231dd2023-04-21 17:37:11 -0700406 result := android.ProductVariables{}
Cole Faustf055db62023-07-24 15:17:03 -0700407 productVarsReflect := reflect.ValueOf(&result).Elem()
408 for i := 0; i < productVarsReflect.NumField(); i++ {
409 field := productVarsReflect.Field(i)
410 fieldType := productVarsReflect.Type().Field(i)
411 name := fieldType.Name
Cole Faust87c0c332023-07-31 12:10:12 -0700412 if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
413 name == "RecoverySnapshotModules" {
Cole Faustf055db62023-07-24 15:17:03 -0700414 // These variables have more complicated types, and we don't need them right now
415 continue
416 }
417 if _, ok := in[name]; ok {
Cole Faust87c0c332023-07-31 12:10:12 -0700418 if name == "VendorVars" {
419 vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
420 if err != nil {
421 return result, err
422 }
423 field.Set(reflect.ValueOf(vendorVars))
424 continue
425 }
Cole Faustf055db62023-07-24 15:17:03 -0700426 switch field.Type().Kind() {
427 case reflect.Bool:
428 val, err := starlark_import.Unmarshal[bool](in[name])
429 if err != nil {
430 return result, err
431 }
432 field.SetBool(val)
433 case reflect.String:
434 val, err := starlark_import.Unmarshal[string](in[name])
435 if err != nil {
436 return result, err
437 }
438 field.SetString(val)
439 case reflect.Slice:
440 if field.Type().Elem().Kind() != reflect.String {
441 return result, fmt.Errorf("slices of types other than strings are unimplemented")
442 }
443 val, err := starlark_import.UnmarshalReflect(in[name], field.Type())
444 if err != nil {
445 return result, err
446 }
447 field.Set(val)
448 case reflect.Pointer:
449 switch field.Type().Elem().Kind() {
450 case reflect.Bool:
451 val, err := starlark_import.UnmarshalNoneable[bool](in[name])
452 if err != nil {
453 return result, err
454 }
455 field.Set(reflect.ValueOf(val))
456 case reflect.String:
457 val, err := starlark_import.UnmarshalNoneable[string](in[name])
458 if err != nil {
459 return result, err
460 }
461 field.Set(reflect.ValueOf(val))
462 case reflect.Int:
463 val, err := starlark_import.UnmarshalNoneable[int](in[name])
464 if err != nil {
465 return result, err
466 }
467 field.Set(reflect.ValueOf(val))
468 default:
469 return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String())
470 }
471 default:
472 return result, fmt.Errorf("unimplemented type: %s", field.Type().String())
473 }
474 }
Cole Faust88c8efb2023-07-18 11:05:16 -0700475 }
Cole Faustf055db62023-07-24 15:17:03 -0700476
Cole Faust87c0c332023-07-31 12:10:12 -0700477 result.Native_coverage = proptools.BoolPtr(
478 proptools.Bool(result.GcovCoverage) ||
479 proptools.Bool(result.ClangCoverage))
480
Cole Faustb85d1a12022-11-08 18:14:01 -0800481 return result, nil
482}
Cole Faust6054cdf2023-09-12 10:07:07 -0700483
Cole Faustcb193ec2023-09-20 16:01:18 -0700484func createTargets(productLabelsToVariables map[bazelLabel]*android.ProductVariables, 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 {
489 createSystemPartition(label, &variables.PartitionVarsForBazelMigrationOnlyDoNotUse, res)
490 }
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 Faustcb193ec2023-09-20 16:01:18 -0700584func createSystemPartition(platformLabel bazelLabel, variables *android.PartitionVariables, targets map[string]BazelTargets) {
585 if !variables.PartitionQualifiedVariables["system"].BuildingImage {
586 return
587 }
Cole Faustb5055392023-09-27 13:44:40 -0700588 qualifiedVariables := variables.PartitionQualifiedVariables["system"]
Cole Faustcb193ec2023-09-20 16:01:18 -0700589
590 imageProps := generateImagePropDictionary(variables, "system")
591 imageProps["skip_fsck"] = "true"
592
593 var properties strings.Builder
594 for _, prop := range android.SortedKeys(imageProps) {
595 properties.WriteString(prop)
596 properties.WriteRune('=')
597 properties.WriteString(imageProps[prop])
598 properties.WriteRune('\n')
599 }
600
Cole Faustb5055392023-09-27 13:44:40 -0700601 var extraProperties strings.Builder
602 if variables.BoardAvbEnable {
603 extraProperties.WriteString(" avb_enable = True,\n")
604 extraProperties.WriteString(fmt.Sprintf(" avb_add_hashtree_footer_args = %q,\n", qualifiedVariables.BoardAvbAddHashtreeFooterArgs))
605 keypath := qualifiedVariables.BoardAvbKeyPath
606 if keypath != "" {
607 extraProperties.WriteString(fmt.Sprintf(" avb_key = \"//%s:%s\",\n", filepath.Dir(keypath), filepath.Base(keypath)+"_filegroup"))
608 extraProperties.WriteString(fmt.Sprintf(" avb_algorithm = %q,\n", qualifiedVariables.BoardAvbAlgorithm))
609 extraProperties.WriteString(fmt.Sprintf(" avb_rollback_index = %s,\n", qualifiedVariables.BoardAvbRollbackIndex))
610 extraProperties.WriteString(fmt.Sprintf(" avb_rollback_index_location = %s,\n", qualifiedVariables.BoardAvbRollbackIndexLocation))
611 }
612 }
613
Cole Faustcb193ec2023-09-20 16:01:18 -0700614 targets[platformLabel.pkg] = append(targets[platformLabel.pkg], BazelTarget{
615 name: "system_image",
616 packageName: platformLabel.pkg,
617 content: fmt.Sprintf(`partition(
618 name = "system_image",
619 base_staging_dir = "//build/bazel/bazel_sandwich:system_staging_dir",
620 base_staging_dir_file_list = "//build/bazel/bazel_sandwich:system_staging_dir_file_list",
621 root_dir = "//build/bazel/bazel_sandwich:root_staging_dir",
Cole Faustb5055392023-09-27 13:44:40 -0700622 selinux_file_contexts = "//build/bazel/bazel_sandwich:selinux_file_contexts",
Cole Faustcb193ec2023-09-20 16:01:18 -0700623 image_properties = """
624%s
625""",
Cole Faustb5055392023-09-27 13:44:40 -0700626%s
Cole Faustcb193ec2023-09-20 16:01:18 -0700627 type = "system",
Cole Faustb5055392023-09-27 13:44:40 -0700628)`, properties.String(), extraProperties.String()),
Cole Faustcb193ec2023-09-20 16:01:18 -0700629 ruleClass: "partition",
630 loads: []BazelLoad{{
631 file: "//build/bazel/rules/partitions:partition.bzl",
632 symbols: []BazelLoadSymbol{{
633 symbol: "partition",
634 }},
635 }},
636 }, BazelTarget{
637 name: "system_image_test",
638 packageName: platformLabel.pkg,
639 content: `partition_diff_test(
640 name = "system_image_test",
641 partition1 = "//build/bazel/bazel_sandwich:make_system_image",
642 partition2 = ":system_image",
643)`,
644 ruleClass: "partition_diff_test",
645 loads: []BazelLoad{{
646 file: "//build/bazel/rules/partitions/diff:partition_diff.bzl",
647 symbols: []BazelLoadSymbol{{
648 symbol: "partition_diff_test",
649 }},
650 }},
651 }, BazelTarget{
652 name: "run_system_image_test",
653 packageName: platformLabel.pkg,
654 content: `run_test_in_build(
655 name = "run_system_image_test",
656 test = ":system_image_test",
657)`,
658 ruleClass: "run_test_in_build",
659 loads: []BazelLoad{{
660 file: "//build/bazel/bazel_sandwich:run_test_in_build.bzl",
661 symbols: []BazelLoadSymbol{{
662 symbol: "run_test_in_build",
663 }},
664 }},
665 })
666}
667
668var allPartitionTypes = []string{
669 "system",
670 "vendor",
671 "cache",
672 "userdata",
673 "product",
674 "system_ext",
675 "oem",
676 "odm",
677 "vendor_dlkm",
678 "odm_dlkm",
679 "system_dlkm",
680}
681
682// An equivalent of make's generate-image-prop-dictionary function
683func generateImagePropDictionary(variables *android.PartitionVariables, partitionType string) map[string]string {
684 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
685 if !ok {
686 panic("Unknown partitionType: " + partitionType)
687 }
688 ret := map[string]string{}
689 if partitionType == "system" {
690 if len(variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize) > 0 {
691 ret["system_other_size"] = variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize
692 }
693 if len(partitionQualifiedVariables.ProductHeadroom) > 0 {
694 ret["system_headroom"] = partitionQualifiedVariables.ProductHeadroom
695 }
696 addCommonRoFlagsToImageProps(variables, partitionType, ret)
697 }
698 // TODO: other partition-specific logic
699 if variables.TargetUserimagesUseExt2 {
700 ret["fs_type"] = "ext2"
701 } else if variables.TargetUserimagesUseExt3 {
702 ret["fs_type"] = "ext3"
703 } else if variables.TargetUserimagesUseExt4 {
704 ret["fs_type"] = "ext4"
705 }
706
707 if !variables.TargetUserimagesSparseExtDisabled {
708 ret["extfs_sparse_flag"] = "-s"
709 }
710 if !variables.TargetUserimagesSparseErofsDisabled {
711 ret["erofs_sparse_flag"] = "-s"
712 }
713 if !variables.TargetUserimagesSparseSquashfsDisabled {
714 ret["squashfs_sparse_flag"] = "-s"
715 }
716 if !variables.TargetUserimagesSparseF2fsDisabled {
717 ret["f2fs_sparse_flag"] = "-S"
718 }
719 erofsCompressor := variables.BoardErofsCompressor
720 if len(erofsCompressor) == 0 && hasErofsPartition(variables) {
721 if len(variables.BoardErofsUseLegacyCompression) > 0 {
722 erofsCompressor = "lz4"
723 } else {
724 erofsCompressor = "lz4hc,9"
725 }
726 }
727 if len(erofsCompressor) > 0 {
728 ret["erofs_default_compressor"] = erofsCompressor
729 }
730 if len(variables.BoardErofsCompressorHints) > 0 {
731 ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints
732 }
733 if len(variables.BoardErofsCompressorHints) > 0 {
734 ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints
735 }
736 if len(variables.BoardErofsPclusterSize) > 0 {
737 ret["erofs_pcluster_size"] = variables.BoardErofsPclusterSize
738 }
739 if len(variables.BoardErofsShareDupBlocks) > 0 {
740 ret["erofs_share_dup_blocks"] = variables.BoardErofsShareDupBlocks
741 }
742 if len(variables.BoardErofsUseLegacyCompression) > 0 {
743 ret["erofs_use_legacy_compression"] = variables.BoardErofsUseLegacyCompression
744 }
745 if len(variables.BoardExt4ShareDupBlocks) > 0 {
746 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
747 }
748 if len(variables.BoardFlashLogicalBlockSize) > 0 {
749 ret["flash_logical_block_size"] = variables.BoardFlashLogicalBlockSize
750 }
751 if len(variables.BoardFlashEraseBlockSize) > 0 {
752 ret["flash_erase_block_size"] = variables.BoardFlashEraseBlockSize
753 }
754 if len(variables.BoardExt4ShareDupBlocks) > 0 {
755 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
756 }
757 if len(variables.BoardExt4ShareDupBlocks) > 0 {
758 ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks
759 }
760 for _, partitionType := range allPartitionTypes {
761 if qualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]; ok && len(qualifiedVariables.ProductVerityPartition) > 0 {
762 ret[partitionType+"_verity_block_device"] = qualifiedVariables.ProductVerityPartition
763 }
764 }
765 // TODO: Vboot
766 // TODO: AVB
767 if variables.BoardUsesRecoveryAsBoot {
768 ret["recovery_as_boot"] = "true"
769 }
770 if variables.BoardBuildGkiBootImageWithoutRamdisk {
771 ret["gki_boot_image_without_ramdisk"] = "true"
772 }
773 if variables.ProductUseDynamicPartitionSize {
774 ret["use_dynamic_partition_size"] = "true"
775 }
776 if variables.CopyImagesForTargetFilesZip {
777 ret["use_fixed_timestamp"] = "true"
778 }
779 return ret
780}
781
782// Soong equivalent of make's add-common-ro-flags-to-image-props
783func addCommonRoFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) {
784 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
785 if !ok {
786 panic("Unknown partitionType: " + partitionType)
787 }
788 if len(partitionQualifiedVariables.BoardErofsCompressor) > 0 {
789 ret[partitionType+"_erofs_compressor"] = partitionQualifiedVariables.BoardErofsCompressor
790 }
791 if len(partitionQualifiedVariables.BoardErofsCompressHints) > 0 {
792 ret[partitionType+"_erofs_compress_hints"] = partitionQualifiedVariables.BoardErofsCompressHints
793 }
794 if len(partitionQualifiedVariables.BoardErofsPclusterSize) > 0 {
795 ret[partitionType+"_erofs_pcluster_size"] = partitionQualifiedVariables.BoardErofsPclusterSize
796 }
797 if len(partitionQualifiedVariables.BoardExtfsRsvPct) > 0 {
798 ret[partitionType+"_extfs_rsv_pct"] = partitionQualifiedVariables.BoardExtfsRsvPct
799 }
800 if len(partitionQualifiedVariables.BoardF2fsSloadCompressFlags) > 0 {
801 ret[partitionType+"_f2fs_sldc_flags"] = partitionQualifiedVariables.BoardF2fsSloadCompressFlags
802 }
803 if len(partitionQualifiedVariables.BoardFileSystemCompress) > 0 {
804 ret[partitionType+"_f2fs_compress"] = partitionQualifiedVariables.BoardFileSystemCompress
805 }
806 if len(partitionQualifiedVariables.BoardFileSystemType) > 0 {
807 ret[partitionType+"_fs_type"] = partitionQualifiedVariables.BoardFileSystemType
808 }
809 if len(partitionQualifiedVariables.BoardJournalSize) > 0 {
810 ret[partitionType+"_journal_size"] = partitionQualifiedVariables.BoardJournalSize
811 }
812 if len(partitionQualifiedVariables.BoardPartitionReservedSize) > 0 {
813 ret[partitionType+"_reserved_size"] = partitionQualifiedVariables.BoardPartitionReservedSize
814 }
815 if len(partitionQualifiedVariables.BoardPartitionSize) > 0 {
816 ret[partitionType+"_size"] = partitionQualifiedVariables.BoardPartitionSize
817 }
818 if len(partitionQualifiedVariables.BoardSquashfsBlockSize) > 0 {
819 ret[partitionType+"_squashfs_block_size"] = partitionQualifiedVariables.BoardSquashfsBlockSize
820 }
821 if len(partitionQualifiedVariables.BoardSquashfsCompressor) > 0 {
822 ret[partitionType+"_squashfs_compressor"] = partitionQualifiedVariables.BoardSquashfsCompressor
823 }
824 if len(partitionQualifiedVariables.BoardSquashfsCompressorOpt) > 0 {
825 ret[partitionType+"_squashfs_compressor_opt"] = partitionQualifiedVariables.BoardSquashfsCompressorOpt
826 }
827 if len(partitionQualifiedVariables.BoardSquashfsDisable4kAlign) > 0 {
828 ret[partitionType+"_squashfs_disable_4k_align"] = partitionQualifiedVariables.BoardSquashfsDisable4kAlign
829 }
830 if len(partitionQualifiedVariables.BoardPartitionSize) == 0 && len(partitionQualifiedVariables.BoardPartitionReservedSize) == 0 && len(partitionQualifiedVariables.ProductHeadroom) == 0 {
831 ret[partitionType+"_disable_sparse"] = "true"
832 }
833 addCommonFlagsToImageProps(variables, partitionType, ret)
834}
835
836func hasErofsPartition(variables *android.PartitionVariables) bool {
837 return variables.PartitionQualifiedVariables["product"].BoardFileSystemType == "erofs" ||
838 variables.PartitionQualifiedVariables["system_ext"].BoardFileSystemType == "erofs" ||
839 variables.PartitionQualifiedVariables["odm"].BoardFileSystemType == "erofs" ||
840 variables.PartitionQualifiedVariables["vendor"].BoardFileSystemType == "erofs" ||
841 variables.PartitionQualifiedVariables["system"].BoardFileSystemType == "erofs" ||
842 variables.PartitionQualifiedVariables["vendor_dlkm"].BoardFileSystemType == "erofs" ||
843 variables.PartitionQualifiedVariables["odm_dlkm"].BoardFileSystemType == "erofs" ||
844 variables.PartitionQualifiedVariables["system_dlkm"].BoardFileSystemType == "erofs"
845}
846
847// Soong equivalent of make's add-common-flags-to-image-props
848func addCommonFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) {
849 // The selinux_fc will be handled separately
850 partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]
851 if !ok {
852 panic("Unknown partitionType: " + partitionType)
853 }
854 ret["building_"+partitionType+"_image"] = boolToMakeString(partitionQualifiedVariables.BuildingImage)
855}
856
857func boolToMakeString(b bool) string {
858 if b {
859 return "true"
860 }
861 return ""
862}