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" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 6 | "path/filepath" |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 7 | "reflect" |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 8 | "sort" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 9 | "strings" |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 10 | |
Yu Liu | b6a15da | 2023-08-31 14:14:01 -0700 | [diff] [blame] | 11 | "android/soong/android" |
| 12 | "android/soong/android/soongconfig" |
| 13 | "android/soong/starlark_import" |
| 14 | |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 15 | "github.com/google/blueprint/proptools" |
| 16 | "go.starlark.net/starlark" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 17 | ) |
| 18 | |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 19 | type createProductConfigFilesResult struct { |
| 20 | injectionFiles []BazelFile |
| 21 | bp2buildFiles []BazelFile |
| 22 | bp2buildTargets map[string]BazelTargets |
| 23 | } |
| 24 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 25 | type bazelLabel struct { |
| 26 | repo string |
| 27 | pkg string |
| 28 | target string |
| 29 | } |
| 30 | |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 31 | const releaseAconfigValueSetsName = "release_aconfig_value_sets" |
| 32 | |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 33 | func (l *bazelLabel) Less(other *bazelLabel) bool { |
| 34 | if l.repo < other.repo { |
| 35 | return true |
| 36 | } |
| 37 | if l.repo > other.repo { |
| 38 | return false |
| 39 | } |
| 40 | if l.pkg < other.pkg { |
| 41 | return true |
| 42 | } |
| 43 | if l.pkg > other.pkg { |
| 44 | return false |
| 45 | } |
| 46 | return l.target < other.target |
| 47 | } |
| 48 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 49 | func (l *bazelLabel) String() string { |
| 50 | return fmt.Sprintf("@%s//%s:%s", l.repo, l.pkg, l.target) |
| 51 | } |
| 52 | |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 53 | func createProductConfigFiles( |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 54 | ctx *CodegenContext, |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 55 | moduleNameToPartition map[string]string, |
| 56 | convertedModulePathMap map[string]string) (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 | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 71 | productVariables := ctx.Config().ProductVariables() |
| 72 | // TODO(b/306243251): For some reason, using the real value of native_coverage makes some select |
| 73 | // statements ambiguous |
| 74 | productVariables.Native_coverage = nil |
| 75 | productVariablesBytes, err := json.Marshal(productVariables) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 76 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 77 | return res, err |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 80 | currentProductFolder := fmt.Sprintf("build/bazel/products/%s", targetProduct) |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 81 | if len(productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory) > 0 { |
| 82 | currentProductFolder = fmt.Sprintf("%s%s", productVariables.PartitionVarsForBazelMigrationOnlyDoNotUse.ProductDirectory, targetProduct) |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 83 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 84 | |
| 85 | productReplacer := strings.NewReplacer( |
| 86 | "{PRODUCT}", targetProduct, |
| 87 | "{VARIANT}", targetBuildVariant, |
| 88 | "{PRODUCT_FOLDER}", currentProductFolder) |
| 89 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 90 | productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing") |
| 91 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 92 | return res, err |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 93 | } |
| 94 | productsForTesting := android.SortedKeys(productsForTestingMap) |
| 95 | for i := range productsForTesting { |
| 96 | productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i]) |
| 97 | } |
| 98 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 99 | productLabelsToVariables := make(map[bazelLabel]*android.ProductVariables) |
| 100 | productLabelsToVariables[bazelLabel{ |
| 101 | repo: "", |
| 102 | pkg: currentProductFolder, |
| 103 | target: targetProduct, |
| 104 | }] = &productVariables |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 105 | for product, productVariablesStarlark := range productsForTestingMap { |
| 106 | productVariables, err := starlarkMapToProductVariables(productVariablesStarlark) |
| 107 | if err != nil { |
| 108 | return res, err |
| 109 | } |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 110 | productLabelsToVariables[bazelLabel{ |
| 111 | repo: "", |
| 112 | pkg: "build/bazel/tests/products", |
| 113 | target: product, |
| 114 | }] = &productVariables |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 117 | res.bp2buildTargets = make(map[string]BazelTargets) |
| 118 | res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{ |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 119 | name: productReplacer.Replace("{PRODUCT}"), |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 120 | packageName: currentProductFolder, |
| 121 | content: productReplacer.Replace(`android_product( |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 122 | name = "{PRODUCT}", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 123 | soong_variables = _soong_variables, |
| 124 | )`), |
| 125 | ruleClass: "android_product", |
| 126 | loads: []BazelLoad{ |
| 127 | { |
| 128 | file: ":soong.variables.bzl", |
| 129 | symbols: []BazelLoadSymbol{{ |
| 130 | symbol: "variables", |
| 131 | alias: "_soong_variables", |
| 132 | }}, |
| 133 | }, |
| 134 | { |
| 135 | file: "//build/bazel/product_config:android_product.bzl", |
| 136 | symbols: []BazelLoadSymbol{{symbol: "android_product"}}, |
| 137 | }, |
| 138 | }, |
| 139 | }) |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 140 | createTargets(ctx, productLabelsToVariables, moduleNameToPartition, convertedModulePathMap, res.bp2buildTargets) |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 141 | |
| 142 | platformMappingContent, err := platformMappingContent( |
| 143 | productLabelsToVariables, |
| 144 | ctx.Config().Bp2buildSoongConfigDefinitions, |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 145 | convertedModulePathMap) |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 146 | if err != nil { |
| 147 | return res, err |
| 148 | } |
| 149 | |
| 150 | res.injectionFiles = []BazelFile{ |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 151 | newFile( |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 152 | "product_config_platforms", |
| 153 | "BUILD.bazel", |
| 154 | productReplacer.Replace(` |
| 155 | package(default_visibility = [ |
| 156 | "@//build/bazel/product_config:__subpackages__", |
| 157 | "@soong_injection//product_config_platforms:__subpackages__", |
| 158 | ]) |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 159 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 160 | load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables") |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 161 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
| 162 | |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 163 | # Bazel will qualify its outputs by the platform name. When switching between products, this |
| 164 | # means that soong-built files that depend on bazel-built files will suddenly get different |
| 165 | # dependency files, because the path changes, and they will be rebuilt. In order to avoid this |
| 166 | # extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts |
| 167 | # are always under the same path. |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 168 | android_product( |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 169 | name = "mixed_builds_product", |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 170 | soong_variables = _soong_variables, |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 171 | extra_constraints = ["@//build/bazel/platforms:mixed_builds"], |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 172 | ) |
Cole Faust | 117bb74 | 2023-03-29 14:46:20 -0700 | [diff] [blame] | 173 | `)), |
| 174 | newFile( |
| 175 | "product_config_platforms", |
| 176 | "product_labels.bzl", |
| 177 | productReplacer.Replace(` |
| 178 | # This file keeps a list of all the products in the android source tree, because they're |
| 179 | # discovered as part of a preprocessing step before bazel runs. |
| 180 | # TODO: When we start generating the platforms for more than just the |
| 181 | # currently lunched product, they should all be listed here |
| 182 | product_labels = [ |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 183 | "@soong_injection//product_config_platforms:mixed_builds_product", |
| 184 | "@//{PRODUCT_FOLDER}:{PRODUCT}", |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 185 | `)+strings.Join(productsForTesting, "\n")+"\n]\n"), |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 186 | newFile( |
| 187 | "product_config_platforms", |
| 188 | "common.bazelrc", |
| 189 | productReplacer.Replace(` |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 190 | build --platform_mappings=platform_mappings |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 191 | build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
| 192 | build --//build/bazel/product_config:target_build_variant={VARIANT} |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 193 | |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 194 | build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT} |
| 195 | build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86 |
| 196 | build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
| 197 | build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_bionic_x86_64 |
| 198 | build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86 |
| 199 | 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] | 200 | `)), |
| 201 | newFile( |
| 202 | "product_config_platforms", |
| 203 | "linux.bazelrc", |
| 204 | productReplacer.Replace(` |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 205 | build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 206 | `)), |
| 207 | newFile( |
| 208 | "product_config_platforms", |
| 209 | "darwin.bazelrc", |
| 210 | productReplacer.Replace(` |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 211 | build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_darwin_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 212 | `)), |
| 213 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 214 | res.bp2buildFiles = []BazelFile{ |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 215 | newFile( |
| 216 | "", |
| 217 | "platform_mappings", |
| 218 | platformMappingContent), |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 219 | newFile( |
| 220 | currentProductFolder, |
| 221 | "soong.variables.bzl", |
| 222 | `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`), |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 223 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 224 | |
| 225 | return res, nil |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 226 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 227 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 228 | func platformMappingContent( |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 229 | productLabelToVariables map[bazelLabel]*android.ProductVariables, |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 230 | soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, |
| 231 | convertedModulePathMap map[string]string) (string, error) { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 232 | var result strings.Builder |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 233 | |
| 234 | mergedConvertedModulePathMap := make(map[string]string) |
| 235 | for k, v := range convertedModulePathMap { |
| 236 | mergedConvertedModulePathMap[k] = v |
| 237 | } |
| 238 | additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages") |
| 239 | if err != nil { |
| 240 | return "", err |
| 241 | } |
| 242 | for k, v := range additionalModuleNamesToPackages { |
| 243 | mergedConvertedModulePathMap[k] = v |
| 244 | } |
| 245 | |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 246 | productLabels := make([]bazelLabel, 0, len(productLabelToVariables)) |
| 247 | for k := range productLabelToVariables { |
| 248 | productLabels = append(productLabels, k) |
| 249 | } |
| 250 | sort.Slice(productLabels, func(i, j int) bool { |
| 251 | return productLabels[i].Less(&productLabels[j]) |
| 252 | }) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 253 | result.WriteString("platforms:\n") |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 254 | for _, productLabel := range productLabels { |
| 255 | platformMappingSingleProduct(productLabel, productLabelToVariables[productLabel], soongConfigDefinitions, mergedConvertedModulePathMap, &result) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 256 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 257 | return result.String(), nil |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 260 | var bazelPlatformSuffixes = []string{ |
| 261 | "", |
| 262 | "_darwin_arm64", |
| 263 | "_darwin_x86_64", |
| 264 | "_linux_bionic_arm64", |
| 265 | "_linux_bionic_x86_64", |
| 266 | "_linux_musl_x86", |
| 267 | "_linux_musl_x86_64", |
| 268 | "_linux_x86", |
| 269 | "_linux_x86_64", |
| 270 | "_windows_x86", |
| 271 | "_windows_x86_64", |
| 272 | } |
| 273 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 274 | func platformMappingSingleProduct( |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 275 | label bazelLabel, |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 276 | productVariables *android.ProductVariables, |
| 277 | soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, |
| 278 | convertedModulePathMap map[string]string, |
| 279 | result *strings.Builder) { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 280 | |
Cole Faust | 95c5cf8 | 2023-08-03 13:49:27 -0700 | [diff] [blame] | 281 | platform_sdk_version := -1 |
| 282 | if productVariables.Platform_sdk_version != nil { |
| 283 | platform_sdk_version = *productVariables.Platform_sdk_version |
| 284 | } |
| 285 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 286 | defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup" |
| 287 | if proptools.String(productVariables.DefaultAppCertificate) != "" { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 288 | defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory" |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 291 | // TODO: b/301598690 - commas can't be escaped in a string-list passed in a platform mapping, |
| 292 | // so commas are switched for ":" here, and must be back-substituted into commas |
| 293 | // wherever the AAPTCharacteristics product config variable is used. |
| 294 | AAPTConfig := []string{} |
| 295 | for _, conf := range productVariables.AAPTConfig { |
| 296 | AAPTConfig = append(AAPTConfig, strings.Replace(conf, ",", ":", -1)) |
| 297 | } |
| 298 | |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 299 | for _, suffix := range bazelPlatformSuffixes { |
| 300 | result.WriteString(" ") |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 301 | result.WriteString(label.String()) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 302 | result.WriteString(suffix) |
| 303 | result.WriteString("\n") |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 304 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_characteristics=%s\n", proptools.String(productVariables.AAPTCharacteristics))) |
| 305 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_config=%s\n", strings.Join(AAPTConfig, ","))) |
| 306 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:aapt_preferred_config=%s\n", proptools.String(productVariables.AAPTPreferredConfig))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 307 | 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] | 308 | 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] | 309 | 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] | 310 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit))) |
| 311 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_from_text_stub=%t\n", proptools.Bool(productVariables.Build_from_text_stub))) |
Cole Faust | ded7960 | 2023-09-05 17:48:11 -0700 | [diff] [blame] | 312 | 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] | 313 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId))) |
| 314 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ","))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 315 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ","))) |
| 316 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ","))) |
| 317 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex))) |
| 318 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate))) |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 319 | 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] | 320 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ","))) |
| 321 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported))) |
| 322 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName))) |
Juan Yescas | 0106560 | 2023-08-09 08:34:37 -0700 | [diff] [blame] | 323 | 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] | 324 | 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] | 325 | 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] | 326 | 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] | 327 | 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] | 328 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte))) |
| 329 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents))) |
| 330 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_zero_contents=%t\n", proptools.Bool(productVariables.Malloc_zero_contents))) |
Yu Liu | b6a15da | 2023-08-31 14:14:01 -0700 | [diff] [blame] | 331 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ","))) |
| 332 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ","))) |
| 333 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_sync_include_paths=%s\n", strings.Join(productVariables.MemtagHeapSyncIncludePaths, ","))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 334 | 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] | 335 | 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] | 336 | 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] | 337 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_security_patch=%s\n", proptools.String(productVariables.Platform_security_patch))) |
| 338 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_last_stable=%s\n", proptools.String(productVariables.Platform_version_last_stable))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 339 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name))) |
| 340 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand)) |
| 341 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer)) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 342 | 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] | 343 | releaseAconfigValueSets := "//build/bazel/product_config:empty_aconfig_value_sets" |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 344 | if len(productVariables.ReleaseAconfigValueSets) > 0 { |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 345 | releaseAconfigValueSets = "@//" + label.pkg + ":" + releaseAconfigValueSetsName + "_" + label.target |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 346 | } |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 347 | 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] | 348 | 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] | 349 | 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] | 350 | 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] | 351 | 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] | 352 | 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] | 353 | 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] | 354 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build))) |
| 355 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build_apps=%s\n", strings.Join(productVariables.Unbundled_build_apps, ","))) |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 356 | |
| 357 | for _, override := range productVariables.CertificateOverrides { |
| 358 | parts := strings.SplitN(override, ":", 2) |
| 359 | if apexPath, ok := convertedModulePathMap[parts[0]]; ok { |
| 360 | if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok { |
| 361 | result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1])) |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Cole Faust | e136dda | 2023-09-27 14:10:33 -0700 | [diff] [blame] | 366 | for _, namespace := range android.SortedKeys(productVariables.VendorVars) { |
| 367 | for _, variable := range android.SortedKeys(productVariables.VendorVars[namespace]) { |
| 368 | value := productVariables.VendorVars[namespace][variable] |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 369 | key := namespace + "__" + variable |
| 370 | _, hasBool := soongConfigDefinitions.BoolVars[key] |
| 371 | _, hasString := soongConfigDefinitions.StringVars[key] |
| 372 | _, hasValue := soongConfigDefinitions.ValueVars[key] |
| 373 | if !hasBool && !hasString && !hasValue { |
| 374 | // Not all soong config variables are defined in Android.bp files. For example, |
| 375 | // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard |
| 376 | // way, that causes them to be present in the soong.variables file but not |
| 377 | // defined in an Android.bp file. There's also nothing stopping you from setting |
| 378 | // a variable in make that doesn't exist in soong. We only generate build |
| 379 | // settings for the ones that exist in soong, so skip all others. |
| 380 | continue |
| 381 | } |
| 382 | if hasBool && hasString || hasBool && hasValue || hasString && hasValue { |
| 383 | panic(fmt.Sprintf("Soong config variable %s:%s appears to be of multiple types. bool? %t, string? %t, value? %t", namespace, variable, hasBool, hasString, hasValue)) |
| 384 | } |
| 385 | if hasBool { |
| 386 | // Logic copied from soongConfig.Bool() |
| 387 | value = strings.ToLower(value) |
| 388 | if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" { |
| 389 | value = "true" |
| 390 | } else { |
| 391 | value = "false" |
| 392 | } |
| 393 | } |
| 394 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value)) |
| 395 | } |
| 396 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 397 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) { |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 401 | result := android.ProductVariables{} |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 402 | productVarsReflect := reflect.ValueOf(&result).Elem() |
| 403 | for i := 0; i < productVarsReflect.NumField(); i++ { |
| 404 | field := productVarsReflect.Field(i) |
| 405 | fieldType := productVarsReflect.Type().Field(i) |
| 406 | name := fieldType.Name |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 407 | if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" || |
| 408 | name == "RecoverySnapshotModules" { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 409 | // These variables have more complicated types, and we don't need them right now |
| 410 | continue |
| 411 | } |
| 412 | if _, ok := in[name]; ok { |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 413 | if name == "VendorVars" { |
| 414 | vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name]) |
| 415 | if err != nil { |
| 416 | return result, err |
| 417 | } |
| 418 | field.Set(reflect.ValueOf(vendorVars)) |
| 419 | continue |
| 420 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 421 | switch field.Type().Kind() { |
| 422 | case reflect.Bool: |
| 423 | val, err := starlark_import.Unmarshal[bool](in[name]) |
| 424 | if err != nil { |
| 425 | return result, err |
| 426 | } |
| 427 | field.SetBool(val) |
| 428 | case reflect.String: |
| 429 | val, err := starlark_import.Unmarshal[string](in[name]) |
| 430 | if err != nil { |
| 431 | return result, err |
| 432 | } |
| 433 | field.SetString(val) |
| 434 | case reflect.Slice: |
| 435 | if field.Type().Elem().Kind() != reflect.String { |
| 436 | return result, fmt.Errorf("slices of types other than strings are unimplemented") |
| 437 | } |
| 438 | val, err := starlark_import.UnmarshalReflect(in[name], field.Type()) |
| 439 | if err != nil { |
| 440 | return result, err |
| 441 | } |
| 442 | field.Set(val) |
| 443 | case reflect.Pointer: |
| 444 | switch field.Type().Elem().Kind() { |
| 445 | case reflect.Bool: |
| 446 | val, err := starlark_import.UnmarshalNoneable[bool](in[name]) |
| 447 | if err != nil { |
| 448 | return result, err |
| 449 | } |
| 450 | field.Set(reflect.ValueOf(val)) |
| 451 | case reflect.String: |
| 452 | val, err := starlark_import.UnmarshalNoneable[string](in[name]) |
| 453 | if err != nil { |
| 454 | return result, err |
| 455 | } |
| 456 | field.Set(reflect.ValueOf(val)) |
| 457 | case reflect.Int: |
| 458 | val, err := starlark_import.UnmarshalNoneable[int](in[name]) |
| 459 | if err != nil { |
| 460 | return result, err |
| 461 | } |
| 462 | field.Set(reflect.ValueOf(val)) |
| 463 | default: |
| 464 | return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String()) |
| 465 | } |
| 466 | default: |
| 467 | return result, fmt.Errorf("unimplemented type: %s", field.Type().String()) |
| 468 | } |
| 469 | } |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 470 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 471 | |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 472 | result.Native_coverage = proptools.BoolPtr( |
| 473 | proptools.Bool(result.GcovCoverage) || |
| 474 | proptools.Bool(result.ClangCoverage)) |
| 475 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 476 | return result, nil |
| 477 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 478 | |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 479 | func createTargets( |
| 480 | ctx *CodegenContext, |
| 481 | productLabelsToVariables map[bazelLabel]*android.ProductVariables, |
| 482 | moduleNameToPartition map[string]string, |
| 483 | convertedModulePathMap map[string]string, |
| 484 | res map[string]BazelTargets) { |
Cole 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 { |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 489 | createSystemPartition(ctx, label, &variables.PartitionVarsForBazelMigrationOnlyDoNotUse, moduleNameToPartition, convertedModulePathMap, res) |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 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 | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 584 | func createSystemPartition( |
| 585 | ctx *CodegenContext, |
| 586 | platformLabel bazelLabel, |
| 587 | variables *android.PartitionVariables, |
| 588 | moduleNameToPartition map[string]string, |
| 589 | convertedModulePathMap map[string]string, |
| 590 | targets map[string]BazelTargets) { |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 591 | if !variables.PartitionQualifiedVariables["system"].BuildingImage { |
| 592 | return |
| 593 | } |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 594 | qualifiedVariables := variables.PartitionQualifiedVariables["system"] |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 595 | |
| 596 | imageProps := generateImagePropDictionary(variables, "system") |
| 597 | imageProps["skip_fsck"] = "true" |
| 598 | |
| 599 | var properties strings.Builder |
| 600 | for _, prop := range android.SortedKeys(imageProps) { |
| 601 | properties.WriteString(prop) |
| 602 | properties.WriteRune('=') |
| 603 | properties.WriteString(imageProps[prop]) |
| 604 | properties.WriteRune('\n') |
| 605 | } |
| 606 | |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 607 | var extraProperties strings.Builder |
| 608 | if variables.BoardAvbEnable { |
| 609 | extraProperties.WriteString(" avb_enable = True,\n") |
| 610 | extraProperties.WriteString(fmt.Sprintf(" avb_add_hashtree_footer_args = %q,\n", qualifiedVariables.BoardAvbAddHashtreeFooterArgs)) |
| 611 | keypath := qualifiedVariables.BoardAvbKeyPath |
| 612 | if keypath != "" { |
| 613 | extraProperties.WriteString(fmt.Sprintf(" avb_key = \"//%s:%s\",\n", filepath.Dir(keypath), filepath.Base(keypath)+"_filegroup")) |
| 614 | extraProperties.WriteString(fmt.Sprintf(" avb_algorithm = %q,\n", qualifiedVariables.BoardAvbAlgorithm)) |
| 615 | extraProperties.WriteString(fmt.Sprintf(" avb_rollback_index = %s,\n", qualifiedVariables.BoardAvbRollbackIndex)) |
| 616 | extraProperties.WriteString(fmt.Sprintf(" avb_rollback_index_location = %s,\n", qualifiedVariables.BoardAvbRollbackIndexLocation)) |
| 617 | } |
| 618 | } |
| 619 | |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 620 | var deps []string |
| 621 | for _, mod := range variables.ProductPackages { |
| 622 | if path, ok := convertedModulePathMap[mod]; ok && ctx.Config().BazelContext.IsModuleNameAllowed(mod, false) { |
| 623 | if partition, ok := moduleNameToPartition[mod]; ok && partition == "system" { |
| 624 | if path == "//." { |
| 625 | path = "//" |
| 626 | } |
| 627 | deps = append(deps, fmt.Sprintf(" \"%s:%s\",\n", path, mod)) |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | if len(deps) > 0 { |
| 632 | sort.Strings(deps) |
| 633 | extraProperties.WriteString(" deps = [\n") |
| 634 | for _, dep := range deps { |
| 635 | extraProperties.WriteString(dep) |
| 636 | } |
| 637 | extraProperties.WriteString(" ],\n") |
| 638 | } |
| 639 | |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 640 | targets[platformLabel.pkg] = append(targets[platformLabel.pkg], BazelTarget{ |
| 641 | name: "system_image", |
| 642 | packageName: platformLabel.pkg, |
| 643 | content: fmt.Sprintf(`partition( |
| 644 | name = "system_image", |
| 645 | base_staging_dir = "//build/bazel/bazel_sandwich:system_staging_dir", |
| 646 | base_staging_dir_file_list = "//build/bazel/bazel_sandwich:system_staging_dir_file_list", |
| 647 | root_dir = "//build/bazel/bazel_sandwich:root_staging_dir", |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 648 | selinux_file_contexts = "//build/bazel/bazel_sandwich:selinux_file_contexts", |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 649 | image_properties = """ |
| 650 | %s |
| 651 | """, |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 652 | %s |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 653 | type = "system", |
Cole Faust | b505539 | 2023-09-27 13:44:40 -0700 | [diff] [blame] | 654 | )`, properties.String(), extraProperties.String()), |
Cole Faust | cb193ec | 2023-09-20 16:01:18 -0700 | [diff] [blame] | 655 | ruleClass: "partition", |
| 656 | loads: []BazelLoad{{ |
| 657 | file: "//build/bazel/rules/partitions:partition.bzl", |
| 658 | symbols: []BazelLoadSymbol{{ |
| 659 | symbol: "partition", |
| 660 | }}, |
| 661 | }}, |
| 662 | }, BazelTarget{ |
| 663 | name: "system_image_test", |
| 664 | packageName: platformLabel.pkg, |
| 665 | content: `partition_diff_test( |
| 666 | name = "system_image_test", |
| 667 | partition1 = "//build/bazel/bazel_sandwich:make_system_image", |
| 668 | partition2 = ":system_image", |
| 669 | )`, |
| 670 | ruleClass: "partition_diff_test", |
| 671 | loads: []BazelLoad{{ |
| 672 | file: "//build/bazel/rules/partitions/diff:partition_diff.bzl", |
| 673 | symbols: []BazelLoadSymbol{{ |
| 674 | symbol: "partition_diff_test", |
| 675 | }}, |
| 676 | }}, |
| 677 | }, BazelTarget{ |
| 678 | name: "run_system_image_test", |
| 679 | packageName: platformLabel.pkg, |
| 680 | content: `run_test_in_build( |
| 681 | name = "run_system_image_test", |
| 682 | test = ":system_image_test", |
| 683 | )`, |
| 684 | ruleClass: "run_test_in_build", |
| 685 | loads: []BazelLoad{{ |
| 686 | file: "//build/bazel/bazel_sandwich:run_test_in_build.bzl", |
| 687 | symbols: []BazelLoadSymbol{{ |
| 688 | symbol: "run_test_in_build", |
| 689 | }}, |
| 690 | }}, |
| 691 | }) |
| 692 | } |
| 693 | |
| 694 | var allPartitionTypes = []string{ |
| 695 | "system", |
| 696 | "vendor", |
| 697 | "cache", |
| 698 | "userdata", |
| 699 | "product", |
| 700 | "system_ext", |
| 701 | "oem", |
| 702 | "odm", |
| 703 | "vendor_dlkm", |
| 704 | "odm_dlkm", |
| 705 | "system_dlkm", |
| 706 | } |
| 707 | |
| 708 | // An equivalent of make's generate-image-prop-dictionary function |
| 709 | func generateImagePropDictionary(variables *android.PartitionVariables, partitionType string) map[string]string { |
| 710 | partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType] |
| 711 | if !ok { |
| 712 | panic("Unknown partitionType: " + partitionType) |
| 713 | } |
| 714 | ret := map[string]string{} |
| 715 | if partitionType == "system" { |
| 716 | if len(variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize) > 0 { |
| 717 | ret["system_other_size"] = variables.PartitionQualifiedVariables["system_other"].BoardPartitionSize |
| 718 | } |
| 719 | if len(partitionQualifiedVariables.ProductHeadroom) > 0 { |
| 720 | ret["system_headroom"] = partitionQualifiedVariables.ProductHeadroom |
| 721 | } |
| 722 | addCommonRoFlagsToImageProps(variables, partitionType, ret) |
| 723 | } |
| 724 | // TODO: other partition-specific logic |
| 725 | if variables.TargetUserimagesUseExt2 { |
| 726 | ret["fs_type"] = "ext2" |
| 727 | } else if variables.TargetUserimagesUseExt3 { |
| 728 | ret["fs_type"] = "ext3" |
| 729 | } else if variables.TargetUserimagesUseExt4 { |
| 730 | ret["fs_type"] = "ext4" |
| 731 | } |
| 732 | |
| 733 | if !variables.TargetUserimagesSparseExtDisabled { |
| 734 | ret["extfs_sparse_flag"] = "-s" |
| 735 | } |
| 736 | if !variables.TargetUserimagesSparseErofsDisabled { |
| 737 | ret["erofs_sparse_flag"] = "-s" |
| 738 | } |
| 739 | if !variables.TargetUserimagesSparseSquashfsDisabled { |
| 740 | ret["squashfs_sparse_flag"] = "-s" |
| 741 | } |
| 742 | if !variables.TargetUserimagesSparseF2fsDisabled { |
| 743 | ret["f2fs_sparse_flag"] = "-S" |
| 744 | } |
| 745 | erofsCompressor := variables.BoardErofsCompressor |
| 746 | if len(erofsCompressor) == 0 && hasErofsPartition(variables) { |
| 747 | if len(variables.BoardErofsUseLegacyCompression) > 0 { |
| 748 | erofsCompressor = "lz4" |
| 749 | } else { |
| 750 | erofsCompressor = "lz4hc,9" |
| 751 | } |
| 752 | } |
| 753 | if len(erofsCompressor) > 0 { |
| 754 | ret["erofs_default_compressor"] = erofsCompressor |
| 755 | } |
| 756 | if len(variables.BoardErofsCompressorHints) > 0 { |
| 757 | ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints |
| 758 | } |
| 759 | if len(variables.BoardErofsCompressorHints) > 0 { |
| 760 | ret["erofs_default_compress_hints"] = variables.BoardErofsCompressorHints |
| 761 | } |
| 762 | if len(variables.BoardErofsPclusterSize) > 0 { |
| 763 | ret["erofs_pcluster_size"] = variables.BoardErofsPclusterSize |
| 764 | } |
| 765 | if len(variables.BoardErofsShareDupBlocks) > 0 { |
| 766 | ret["erofs_share_dup_blocks"] = variables.BoardErofsShareDupBlocks |
| 767 | } |
| 768 | if len(variables.BoardErofsUseLegacyCompression) > 0 { |
| 769 | ret["erofs_use_legacy_compression"] = variables.BoardErofsUseLegacyCompression |
| 770 | } |
| 771 | if len(variables.BoardExt4ShareDupBlocks) > 0 { |
| 772 | ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks |
| 773 | } |
| 774 | if len(variables.BoardFlashLogicalBlockSize) > 0 { |
| 775 | ret["flash_logical_block_size"] = variables.BoardFlashLogicalBlockSize |
| 776 | } |
| 777 | if len(variables.BoardFlashEraseBlockSize) > 0 { |
| 778 | ret["flash_erase_block_size"] = variables.BoardFlashEraseBlockSize |
| 779 | } |
| 780 | if len(variables.BoardExt4ShareDupBlocks) > 0 { |
| 781 | ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks |
| 782 | } |
| 783 | if len(variables.BoardExt4ShareDupBlocks) > 0 { |
| 784 | ret["ext4_share_dup_blocks"] = variables.BoardExt4ShareDupBlocks |
| 785 | } |
| 786 | for _, partitionType := range allPartitionTypes { |
| 787 | if qualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType]; ok && len(qualifiedVariables.ProductVerityPartition) > 0 { |
| 788 | ret[partitionType+"_verity_block_device"] = qualifiedVariables.ProductVerityPartition |
| 789 | } |
| 790 | } |
| 791 | // TODO: Vboot |
| 792 | // TODO: AVB |
| 793 | if variables.BoardUsesRecoveryAsBoot { |
| 794 | ret["recovery_as_boot"] = "true" |
| 795 | } |
| 796 | if variables.BoardBuildGkiBootImageWithoutRamdisk { |
| 797 | ret["gki_boot_image_without_ramdisk"] = "true" |
| 798 | } |
| 799 | if variables.ProductUseDynamicPartitionSize { |
| 800 | ret["use_dynamic_partition_size"] = "true" |
| 801 | } |
| 802 | if variables.CopyImagesForTargetFilesZip { |
| 803 | ret["use_fixed_timestamp"] = "true" |
| 804 | } |
| 805 | return ret |
| 806 | } |
| 807 | |
| 808 | // Soong equivalent of make's add-common-ro-flags-to-image-props |
| 809 | func addCommonRoFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) { |
| 810 | partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType] |
| 811 | if !ok { |
| 812 | panic("Unknown partitionType: " + partitionType) |
| 813 | } |
| 814 | if len(partitionQualifiedVariables.BoardErofsCompressor) > 0 { |
| 815 | ret[partitionType+"_erofs_compressor"] = partitionQualifiedVariables.BoardErofsCompressor |
| 816 | } |
| 817 | if len(partitionQualifiedVariables.BoardErofsCompressHints) > 0 { |
| 818 | ret[partitionType+"_erofs_compress_hints"] = partitionQualifiedVariables.BoardErofsCompressHints |
| 819 | } |
| 820 | if len(partitionQualifiedVariables.BoardErofsPclusterSize) > 0 { |
| 821 | ret[partitionType+"_erofs_pcluster_size"] = partitionQualifiedVariables.BoardErofsPclusterSize |
| 822 | } |
| 823 | if len(partitionQualifiedVariables.BoardExtfsRsvPct) > 0 { |
| 824 | ret[partitionType+"_extfs_rsv_pct"] = partitionQualifiedVariables.BoardExtfsRsvPct |
| 825 | } |
| 826 | if len(partitionQualifiedVariables.BoardF2fsSloadCompressFlags) > 0 { |
| 827 | ret[partitionType+"_f2fs_sldc_flags"] = partitionQualifiedVariables.BoardF2fsSloadCompressFlags |
| 828 | } |
| 829 | if len(partitionQualifiedVariables.BoardFileSystemCompress) > 0 { |
| 830 | ret[partitionType+"_f2fs_compress"] = partitionQualifiedVariables.BoardFileSystemCompress |
| 831 | } |
| 832 | if len(partitionQualifiedVariables.BoardFileSystemType) > 0 { |
| 833 | ret[partitionType+"_fs_type"] = partitionQualifiedVariables.BoardFileSystemType |
| 834 | } |
| 835 | if len(partitionQualifiedVariables.BoardJournalSize) > 0 { |
| 836 | ret[partitionType+"_journal_size"] = partitionQualifiedVariables.BoardJournalSize |
| 837 | } |
| 838 | if len(partitionQualifiedVariables.BoardPartitionReservedSize) > 0 { |
| 839 | ret[partitionType+"_reserved_size"] = partitionQualifiedVariables.BoardPartitionReservedSize |
| 840 | } |
| 841 | if len(partitionQualifiedVariables.BoardPartitionSize) > 0 { |
| 842 | ret[partitionType+"_size"] = partitionQualifiedVariables.BoardPartitionSize |
| 843 | } |
| 844 | if len(partitionQualifiedVariables.BoardSquashfsBlockSize) > 0 { |
| 845 | ret[partitionType+"_squashfs_block_size"] = partitionQualifiedVariables.BoardSquashfsBlockSize |
| 846 | } |
| 847 | if len(partitionQualifiedVariables.BoardSquashfsCompressor) > 0 { |
| 848 | ret[partitionType+"_squashfs_compressor"] = partitionQualifiedVariables.BoardSquashfsCompressor |
| 849 | } |
| 850 | if len(partitionQualifiedVariables.BoardSquashfsCompressorOpt) > 0 { |
| 851 | ret[partitionType+"_squashfs_compressor_opt"] = partitionQualifiedVariables.BoardSquashfsCompressorOpt |
| 852 | } |
| 853 | if len(partitionQualifiedVariables.BoardSquashfsDisable4kAlign) > 0 { |
| 854 | ret[partitionType+"_squashfs_disable_4k_align"] = partitionQualifiedVariables.BoardSquashfsDisable4kAlign |
| 855 | } |
| 856 | if len(partitionQualifiedVariables.BoardPartitionSize) == 0 && len(partitionQualifiedVariables.BoardPartitionReservedSize) == 0 && len(partitionQualifiedVariables.ProductHeadroom) == 0 { |
| 857 | ret[partitionType+"_disable_sparse"] = "true" |
| 858 | } |
| 859 | addCommonFlagsToImageProps(variables, partitionType, ret) |
| 860 | } |
| 861 | |
| 862 | func hasErofsPartition(variables *android.PartitionVariables) bool { |
| 863 | return variables.PartitionQualifiedVariables["product"].BoardFileSystemType == "erofs" || |
| 864 | variables.PartitionQualifiedVariables["system_ext"].BoardFileSystemType == "erofs" || |
| 865 | variables.PartitionQualifiedVariables["odm"].BoardFileSystemType == "erofs" || |
| 866 | variables.PartitionQualifiedVariables["vendor"].BoardFileSystemType == "erofs" || |
| 867 | variables.PartitionQualifiedVariables["system"].BoardFileSystemType == "erofs" || |
| 868 | variables.PartitionQualifiedVariables["vendor_dlkm"].BoardFileSystemType == "erofs" || |
| 869 | variables.PartitionQualifiedVariables["odm_dlkm"].BoardFileSystemType == "erofs" || |
| 870 | variables.PartitionQualifiedVariables["system_dlkm"].BoardFileSystemType == "erofs" |
| 871 | } |
| 872 | |
| 873 | // Soong equivalent of make's add-common-flags-to-image-props |
| 874 | func addCommonFlagsToImageProps(variables *android.PartitionVariables, partitionType string, ret map[string]string) { |
| 875 | // The selinux_fc will be handled separately |
| 876 | partitionQualifiedVariables, ok := variables.PartitionQualifiedVariables[partitionType] |
| 877 | if !ok { |
| 878 | panic("Unknown partitionType: " + partitionType) |
| 879 | } |
| 880 | ret["building_"+partitionType+"_image"] = boolToMakeString(partitionQualifiedVariables.BuildingImage) |
| 881 | } |
| 882 | |
| 883 | func boolToMakeString(b bool) string { |
| 884 | if b { |
| 885 | return "true" |
| 886 | } |
| 887 | return "" |
| 888 | } |