Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 4 | "encoding/json" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 8 | "reflect" |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 9 | "sort" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 10 | "strings" |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 11 | |
Yu Liu | b6a15da | 2023-08-31 14:14:01 -0700 | [diff] [blame] | 12 | "android/soong/android" |
| 13 | "android/soong/android/soongconfig" |
| 14 | "android/soong/starlark_import" |
| 15 | |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 16 | "github.com/google/blueprint/proptools" |
| 17 | "go.starlark.net/starlark" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 18 | ) |
| 19 | |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 20 | type createProductConfigFilesResult struct { |
| 21 | injectionFiles []BazelFile |
| 22 | bp2buildFiles []BazelFile |
| 23 | bp2buildTargets map[string]BazelTargets |
| 24 | } |
| 25 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 26 | type bazelLabel struct { |
| 27 | repo string |
| 28 | pkg string |
| 29 | target string |
| 30 | } |
| 31 | |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame^] | 32 | const releaseAconfigValueSetsName = "release_aconfig_value_sets" |
| 33 | |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 34 | func (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 Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 50 | func (l *bazelLabel) String() string { |
| 51 | return fmt.Sprintf("@%s//%s:%s", l.repo, l.pkg, l.target) |
| 52 | } |
| 53 | |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 54 | func createProductConfigFiles( |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 55 | ctx *CodegenContext, |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 56 | metrics CodegenMetrics) (createProductConfigFilesResult, error) { |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 57 | 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 Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 69 | var res createProductConfigFilesResult |
| 70 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 71 | productVariablesFileName := cfg.ProductVariablesFileName |
| 72 | if !strings.HasPrefix(productVariablesFileName, "/") { |
| 73 | productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName) |
| 74 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 75 | productVariablesBytes, err := os.ReadFile(productVariablesFileName) |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 76 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 77 | return res, err |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 78 | } |
| 79 | productVariables := android.ProductVariables{} |
| 80 | err = json.Unmarshal(productVariablesBytes, &productVariables) |
| 81 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 82 | return res, err |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 85 | currentProductFolder := fmt.Sprintf("build/bazel/products/%s", targetProduct) |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 86 | if len(productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory) > 0 { |
| 87 | currentProductFolder = fmt.Sprintf("%s%s", productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory, targetProduct) |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 88 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 89 | |
| 90 | productReplacer := strings.NewReplacer( |
| 91 | "{PRODUCT}", targetProduct, |
| 92 | "{VARIANT}", targetBuildVariant, |
| 93 | "{PRODUCT_FOLDER}", currentProductFolder) |
| 94 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 95 | productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing") |
| 96 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 97 | return res, err |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 98 | } |
| 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 Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 104 | productLabelsToVariables := make(map[bazelLabel]*android.ProductVariables) |
| 105 | productLabelsToVariables[bazelLabel{ |
| 106 | repo: "", |
| 107 | pkg: currentProductFolder, |
| 108 | target: targetProduct, |
| 109 | }] = &productVariables |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 110 | for product, productVariablesStarlark := range productsForTestingMap { |
| 111 | productVariables, err := starlarkMapToProductVariables(productVariablesStarlark) |
| 112 | if err != nil { |
| 113 | return res, err |
| 114 | } |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 115 | productLabelsToVariables[bazelLabel{ |
| 116 | repo: "", |
| 117 | pkg: "build/bazel/tests/products", |
| 118 | target: product, |
| 119 | }] = &productVariables |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 122 | res.bp2buildTargets = make(map[string]BazelTargets) |
| 123 | res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{ |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 124 | name: productReplacer.Replace("{PRODUCT}"), |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 125 | packageName: currentProductFolder, |
| 126 | content: productReplacer.Replace(`android_product( |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 127 | name = "{PRODUCT}", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 128 | 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 Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 146 | |
| 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 Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 156 | newFile( |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 157 | "product_config_platforms", |
| 158 | "BUILD.bazel", |
| 159 | productReplacer.Replace(` |
| 160 | package(default_visibility = [ |
| 161 | "@//build/bazel/product_config:__subpackages__", |
| 162 | "@soong_injection//product_config_platforms:__subpackages__", |
| 163 | ]) |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 164 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 165 | load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables") |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 166 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
| 167 | |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 168 | # 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 Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 173 | android_product( |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 174 | name = "mixed_builds_product", |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 175 | soong_variables = _soong_variables, |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 176 | extra_constraints = ["@//build/bazel/platforms:mixed_builds"], |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 177 | ) |
Cole Faust | 117bb74 | 2023-03-29 14:46:20 -0700 | [diff] [blame] | 178 | `)), |
| 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 |
| 187 | product_labels = [ |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 188 | "@soong_injection//product_config_platforms:mixed_builds_product", |
| 189 | "@//{PRODUCT_FOLDER}:{PRODUCT}", |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 190 | `)+strings.Join(productsForTesting, "\n")+"\n]\n"), |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 191 | newFile( |
| 192 | "product_config_platforms", |
| 193 | "common.bazelrc", |
| 194 | productReplacer.Replace(` |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 195 | build --platform_mappings=platform_mappings |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 196 | build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
| 197 | build --//build/bazel/product_config:target_build_variant={VARIANT} |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 198 | |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 199 | build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT} |
| 200 | build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86 |
| 201 | build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
| 202 | build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_bionic_x86_64 |
| 203 | build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86 |
| 204 | build:linux_musl_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 205 | `)), |
| 206 | newFile( |
| 207 | "product_config_platforms", |
| 208 | "linux.bazelrc", |
| 209 | productReplacer.Replace(` |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 210 | build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 211 | `)), |
| 212 | newFile( |
| 213 | "product_config_platforms", |
| 214 | "darwin.bazelrc", |
| 215 | productReplacer.Replace(` |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 216 | build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_darwin_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 217 | `)), |
| 218 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 219 | res.bp2buildFiles = []BazelFile{ |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 220 | newFile( |
| 221 | "", |
| 222 | "platform_mappings", |
| 223 | platformMappingContent), |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 224 | newFile( |
| 225 | currentProductFolder, |
| 226 | "soong.variables.bzl", |
| 227 | `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`), |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 228 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 229 | |
| 230 | return res, nil |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 231 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 232 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 233 | func platformMappingContent( |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 234 | productLabelToVariables map[bazelLabel]*android.ProductVariables, |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 235 | soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, |
| 236 | convertedModulePathMap map[string]string) (string, error) { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 237 | var result strings.Builder |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 238 | |
| 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 Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 251 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 258 | result.WriteString("platforms:\n") |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 259 | for _, productLabel := range productLabels { |
| 260 | platformMappingSingleProduct(productLabel, productLabelToVariables[productLabel], soongConfigDefinitions, mergedConvertedModulePathMap, &result) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 261 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 262 | return result.String(), nil |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 265 | var 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 Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 279 | func platformMappingSingleProduct( |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 280 | label bazelLabel, |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 281 | productVariables *android.ProductVariables, |
| 282 | soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, |
| 283 | convertedModulePathMap map[string]string, |
| 284 | result *strings.Builder) { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 285 | |
Cole Faust | 95c5cf8 | 2023-08-03 13:49:27 -0700 | [diff] [blame] | 286 | platform_sdk_version := -1 |
| 287 | if productVariables.Platform_sdk_version != nil { |
| 288 | platform_sdk_version = *productVariables.Platform_sdk_version |
| 289 | } |
| 290 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 291 | defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup" |
| 292 | if proptools.String(productVariables.DefaultAppCertificate) != "" { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 293 | defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory" |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 296 | // 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 304 | for _, suffix := range bazelPlatformSuffixes { |
| 305 | result.WriteString(" ") |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 306 | result.WriteString(label.String()) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 307 | result.WriteString(suffix) |
| 308 | result.WriteString("\n") |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 309 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 312 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:always_use_prebuilt_sdks=%t\n", proptools.Bool(productVariables.Always_use_prebuilt_sdks))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 313 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 314 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:apex_global_min_sdk_version_override=%s\n", proptools.String(productVariables.ApexGlobalMinSdkVersionOverride))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 315 | 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 Faust | ded7960 | 2023-09-05 17:48:11 -0700 | [diff] [blame] | 317 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_broken_incorrect_partition_images=%t\n", productVariables.BuildBrokenIncorrectPartitionImages)) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 318 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 320 | 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 Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 324 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate_filegroup=%s\n", defaultAppCertificateFilegroup)) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 325 | 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 Yescas | 0106560 | 2023-08-09 08:34:37 -0700 | [diff] [blame] | 328 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_page_size_agnostic=%t\n", proptools.Bool(productVariables.DevicePageSizeAgnostic))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 329 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct))) |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 330 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label.String())) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 331 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 332 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 333 | 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 Liu | b6a15da | 2023-08-31 14:14:01 -0700 | [diff] [blame] | 336 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 339 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:manifest_package_name_overrides=%s\n", strings.Join(productVariables.ManifestPackageNameOverrides, ","))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 340 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage))) |
Romain Jobredeaux | 3132f84 | 2023-09-15 10:06:16 -0400 | [diff] [blame] | 341 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_final=%t\n", proptools.Bool(productVariables.Platform_sdk_final))) |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 342 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 344 | 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 Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 347 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission)) |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame^] | 348 | releaseAconfigValueSets := "//build/bazel/product_config:empty_aconfig_value_sets" |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 349 | if len(productVariables.ReleaseAconfigValueSets) > 0 { |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame^] | 350 | releaseAconfigValueSets = "@//" + label.pkg + ":" + releaseAconfigValueSetsName + "_" + label.target |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 351 | } |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame^] | 352 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", releaseAconfigValueSets)) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 353 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion)) |
Cole Faust | 95c5cf8 | 2023-08-03 13:49:27 -0700 | [diff] [blame] | 354 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", platform_sdk_version)) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 355 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 356 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:treble_linker_namespaces=%t\n", proptools.Bool(productVariables.Treble_linker_namespaces))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 357 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks))) |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 358 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 359 | 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 Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 361 | |
| 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 Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 371 | for _, namespace := range android.SortedKeys(productVariables.VendorVars) { |
| 372 | for _, variable := range android.SortedKeys(productVariables.VendorVars[namespace]) { |
| 373 | value := productVariables.VendorVars[namespace][variable] |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 374 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 402 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) { |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 406 | result := android.ProductVariables{} |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 407 | 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 Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 412 | if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" || |
| 413 | name == "RecoverySnapshotModules" { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 414 | // These variables have more complicated types, and we don't need them right now |
| 415 | continue |
| 416 | } |
| 417 | if _, ok := in[name]; ok { |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 418 | 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 Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 426 | 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 Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 475 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 476 | |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 477 | result.Native_coverage = proptools.BoolPtr( |
| 478 | proptools.Bool(result.GcovCoverage) || |
| 479 | proptools.Bool(result.ClangCoverage)) |
| 480 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 481 | return result, nil |
| 482 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 483 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 484 | func createTargets(productLabelsToVariables map[bazelLabel]*android.ProductVariables, res map[string]BazelTargets) { |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 485 | createGeneratedAndroidCertificateDirectories(productLabelsToVariables, res) |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 486 | createAvbKeyFilegroups(productLabelsToVariables, res) |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame^] | 487 | createReleaseAconfigValueSetsFilegroup(productLabelsToVariables, res) |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 488 | for label, variables := range productLabelsToVariables { |
| 489 | createSystemPartition(label, &variables.PartitionVarsForBazelMigrationOnlyDoNotUse, res) |
| 490 | } |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 493 | func createGeneratedAndroidCertificateDirectories(productLabelsToVariables map[bazelLabel]*android.ProductVariables, targets map[string]BazelTargets) { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 494 | 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 Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 504 | content := `filegroup( |
| 505 | name = "generated_android_certificate_directory", |
| 506 | srcs = glob([ |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 507 | "*.pk8", |
| 508 | "*.pem", |
| 509 | "*.avbpubkey", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 510 | ]), |
| 511 | visibility = ["//visibility:public"], |
| 512 | )` |
| 513 | targets[dir] = append(targets[dir], BazelTarget{ |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 514 | name: "generated_android_certificate_directory", |
| 515 | packageName: dir, |
| 516 | content: content, |
| 517 | ruleClass: "filegroup", |
| 518 | }) |
| 519 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 520 | } |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 521 | |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame^] | 522 | func 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 Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 556 | func 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 Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 584 | func createSystemPartition(platformLabel bazelLabel, variables *android.PartitionVariables, targets map[string]BazelTargets) { |
| 585 | if !variables.PartitionQualifiedVariables["system"].BuildingImage { |
| 586 | return |
| 587 | } |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 588 | qualifiedVariables := variables.PartitionQualifiedVariables["system"] |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 589 | |
| 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 Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 601 | 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 Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 614 | 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 Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 622 | selinux_file_contexts = "//build/bazel/bazel_sandwich:selinux_file_contexts", |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 623 | image_properties = """ |
| 624 | %s |
| 625 | """, |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 626 | %s |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 627 | type = "system", |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 628 | )`, properties.String(), extraProperties.String()), |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 629 | 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 | |
| 668 | var 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 |
| 683 | func 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 |
| 783 | func 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 | |
| 836 | func 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 |
| 848 | func 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 | |
| 857 | func boolToMakeString(b bool) string { |
| 858 | if b { |
| 859 | return "true" |
| 860 | } |
| 861 | return "" |
| 862 | } |