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 | 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 | |
| 25 | func createProductConfigFiles( |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 26 | ctx *CodegenContext, |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 27 | metrics CodegenMetrics) (createProductConfigFilesResult, error) { |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 28 | cfg := &ctx.config |
| 29 | targetProduct := "unknown" |
| 30 | if cfg.HasDeviceProduct() { |
| 31 | targetProduct = cfg.DeviceProduct() |
| 32 | } |
| 33 | targetBuildVariant := "user" |
| 34 | if cfg.Eng() { |
| 35 | targetBuildVariant = "eng" |
| 36 | } else if cfg.Debuggable() { |
| 37 | targetBuildVariant = "userdebug" |
| 38 | } |
| 39 | |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 40 | var res createProductConfigFilesResult |
| 41 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 42 | productVariablesFileName := cfg.ProductVariablesFileName |
| 43 | if !strings.HasPrefix(productVariablesFileName, "/") { |
| 44 | productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName) |
| 45 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 46 | productVariablesBytes, err := os.ReadFile(productVariablesFileName) |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 47 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 48 | return res, err |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 49 | } |
| 50 | productVariables := android.ProductVariables{} |
| 51 | err = json.Unmarshal(productVariablesBytes, &productVariables) |
| 52 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 53 | return res, err |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 56 | currentProductFolder := fmt.Sprintf("build/bazel/products/%s", targetProduct) |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 57 | if len(productVariables.PartitionVars.ProductDirectory) > 0 { |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 58 | currentProductFolder = fmt.Sprintf("%s%s", productVariables.PartitionVars.ProductDirectory, targetProduct) |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 59 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 60 | |
| 61 | productReplacer := strings.NewReplacer( |
| 62 | "{PRODUCT}", targetProduct, |
| 63 | "{VARIANT}", targetBuildVariant, |
| 64 | "{PRODUCT_FOLDER}", currentProductFolder) |
| 65 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 66 | productsForTestingMap, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing") |
| 67 | if err != nil { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 68 | return res, err |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 69 | } |
| 70 | productsForTesting := android.SortedKeys(productsForTestingMap) |
| 71 | for i := range productsForTesting { |
| 72 | productsForTesting[i] = fmt.Sprintf(" \"@//build/bazel/tests/products:%s\",", productsForTesting[i]) |
| 73 | } |
| 74 | |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 75 | productLabelsToVariables := make(map[string]*android.ProductVariables) |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 76 | productLabelsToVariables[productReplacer.Replace("@//{PRODUCT_FOLDER}:{PRODUCT}")] = &productVariables |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 77 | for product, productVariablesStarlark := range productsForTestingMap { |
| 78 | productVariables, err := starlarkMapToProductVariables(productVariablesStarlark) |
| 79 | if err != nil { |
| 80 | return res, err |
| 81 | } |
| 82 | productLabelsToVariables["@//build/bazel/tests/products:"+product] = &productVariables |
| 83 | } |
| 84 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 85 | res.bp2buildTargets = make(map[string]BazelTargets) |
| 86 | res.bp2buildTargets[currentProductFolder] = append(res.bp2buildTargets[currentProductFolder], BazelTarget{ |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 87 | name: productReplacer.Replace("{PRODUCT}"), |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 88 | packageName: currentProductFolder, |
| 89 | content: productReplacer.Replace(`android_product( |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 90 | name = "{PRODUCT}", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 91 | soong_variables = _soong_variables, |
| 92 | )`), |
| 93 | ruleClass: "android_product", |
| 94 | loads: []BazelLoad{ |
| 95 | { |
| 96 | file: ":soong.variables.bzl", |
| 97 | symbols: []BazelLoadSymbol{{ |
| 98 | symbol: "variables", |
| 99 | alias: "_soong_variables", |
| 100 | }}, |
| 101 | }, |
| 102 | { |
| 103 | file: "//build/bazel/product_config:android_product.bzl", |
| 104 | symbols: []BazelLoadSymbol{{symbol: "android_product"}}, |
| 105 | }, |
| 106 | }, |
| 107 | }) |
| 108 | createTargets(productLabelsToVariables, res.bp2buildTargets) |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 109 | |
| 110 | platformMappingContent, err := platformMappingContent( |
| 111 | productLabelsToVariables, |
| 112 | ctx.Config().Bp2buildSoongConfigDefinitions, |
| 113 | metrics.convertedModulePathMap) |
| 114 | if err != nil { |
| 115 | return res, err |
| 116 | } |
| 117 | |
| 118 | res.injectionFiles = []BazelFile{ |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 119 | newFile( |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 120 | "product_config_platforms", |
| 121 | "BUILD.bazel", |
| 122 | productReplacer.Replace(` |
| 123 | package(default_visibility = [ |
| 124 | "@//build/bazel/product_config:__subpackages__", |
| 125 | "@soong_injection//product_config_platforms:__subpackages__", |
| 126 | ]) |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 127 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 128 | load("@//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables") |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 129 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
| 130 | |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 131 | # Bazel will qualify its outputs by the platform name. When switching between products, this |
| 132 | # means that soong-built files that depend on bazel-built files will suddenly get different |
| 133 | # dependency files, because the path changes, and they will be rebuilt. In order to avoid this |
| 134 | # extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts |
| 135 | # are always under the same path. |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 136 | android_product( |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 137 | name = "mixed_builds_product", |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 138 | soong_variables = _soong_variables, |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 139 | extra_constraints = ["@//build/bazel/platforms:mixed_builds"], |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 140 | ) |
Cole Faust | 117bb74 | 2023-03-29 14:46:20 -0700 | [diff] [blame] | 141 | `)), |
| 142 | newFile( |
| 143 | "product_config_platforms", |
| 144 | "product_labels.bzl", |
| 145 | productReplacer.Replace(` |
| 146 | # This file keeps a list of all the products in the android source tree, because they're |
| 147 | # discovered as part of a preprocessing step before bazel runs. |
| 148 | # TODO: When we start generating the platforms for more than just the |
| 149 | # currently lunched product, they should all be listed here |
| 150 | product_labels = [ |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 151 | "@soong_injection//product_config_platforms:mixed_builds_product", |
| 152 | "@//{PRODUCT_FOLDER}:{PRODUCT}", |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 153 | `)+strings.Join(productsForTesting, "\n")+"\n]\n"), |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 154 | newFile( |
| 155 | "product_config_platforms", |
| 156 | "common.bazelrc", |
| 157 | productReplacer.Replace(` |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 158 | build --platform_mappings=platform_mappings |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 159 | build --platforms @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
| 160 | build --//build/bazel/product_config:target_build_variant={VARIANT} |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 161 | |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 162 | build:android --platforms=@//{PRODUCT_FOLDER}:{PRODUCT} |
| 163 | build:linux_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86 |
| 164 | build:linux_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
| 165 | build:linux_bionic_x86_64 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_bionic_x86_64 |
| 166 | build:linux_musl_x86 --platforms=@//{PRODUCT_FOLDER}:{PRODUCT}_linux_musl_x86 |
| 167 | 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] | 168 | `)), |
| 169 | newFile( |
| 170 | "product_config_platforms", |
| 171 | "linux.bazelrc", |
| 172 | productReplacer.Replace(` |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 173 | build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_linux_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 174 | `)), |
| 175 | newFile( |
| 176 | "product_config_platforms", |
| 177 | "darwin.bazelrc", |
| 178 | productReplacer.Replace(` |
Cole Faust | f3cf34e | 2023-09-20 17:02:40 -0700 | [diff] [blame] | 179 | build --host_platform @//{PRODUCT_FOLDER}:{PRODUCT}_darwin_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 180 | `)), |
| 181 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 182 | res.bp2buildFiles = []BazelFile{ |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 183 | newFile( |
| 184 | "", |
| 185 | "platform_mappings", |
| 186 | platformMappingContent), |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 187 | newFile( |
| 188 | currentProductFolder, |
| 189 | "soong.variables.bzl", |
| 190 | `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`), |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 191 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 192 | |
| 193 | return res, nil |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 194 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 195 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 196 | func platformMappingContent( |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 197 | productLabelToVariables map[string]*android.ProductVariables, |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 198 | soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, |
| 199 | convertedModulePathMap map[string]string) (string, error) { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 200 | var result strings.Builder |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 201 | |
| 202 | mergedConvertedModulePathMap := make(map[string]string) |
| 203 | for k, v := range convertedModulePathMap { |
| 204 | mergedConvertedModulePathMap[k] = v |
| 205 | } |
| 206 | additionalModuleNamesToPackages, err := starlark_import.GetStarlarkValue[map[string]string]("additional_module_names_to_packages") |
| 207 | if err != nil { |
| 208 | return "", err |
| 209 | } |
| 210 | for k, v := range additionalModuleNamesToPackages { |
| 211 | mergedConvertedModulePathMap[k] = v |
| 212 | } |
| 213 | |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 214 | result.WriteString("platforms:\n") |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 215 | for productLabel, productVariables := range productLabelToVariables { |
| 216 | platformMappingSingleProduct(productLabel, productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 217 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 218 | return result.String(), nil |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 221 | var bazelPlatformSuffixes = []string{ |
| 222 | "", |
| 223 | "_darwin_arm64", |
| 224 | "_darwin_x86_64", |
| 225 | "_linux_bionic_arm64", |
| 226 | "_linux_bionic_x86_64", |
| 227 | "_linux_musl_x86", |
| 228 | "_linux_musl_x86_64", |
| 229 | "_linux_x86", |
| 230 | "_linux_x86_64", |
| 231 | "_windows_x86", |
| 232 | "_windows_x86_64", |
| 233 | } |
| 234 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 235 | func platformMappingSingleProduct( |
| 236 | label string, |
| 237 | productVariables *android.ProductVariables, |
| 238 | soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, |
| 239 | convertedModulePathMap map[string]string, |
| 240 | result *strings.Builder) { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 241 | |
Cole Faust | 95c5cf8 | 2023-08-03 13:49:27 -0700 | [diff] [blame] | 242 | platform_sdk_version := -1 |
| 243 | if productVariables.Platform_sdk_version != nil { |
| 244 | platform_sdk_version = *productVariables.Platform_sdk_version |
| 245 | } |
| 246 | |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 247 | defaultAppCertificateFilegroup := "//build/bazel/utils:empty_filegroup" |
| 248 | if proptools.String(productVariables.DefaultAppCertificate) != "" { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 249 | defaultAppCertificateFilegroup = "@//" + filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) + ":generated_android_certificate_directory" |
Cole Faust | 946d02c | 2023-08-03 16:08:09 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 252 | for _, suffix := range bazelPlatformSuffixes { |
| 253 | result.WriteString(" ") |
| 254 | result.WriteString(label) |
| 255 | result.WriteString(suffix) |
| 256 | result.WriteString("\n") |
| 257 | 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] | 258 | 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] | 259 | 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] | 260 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit))) |
| 261 | 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] | 262 | 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] | 263 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId))) |
| 264 | 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] | 265 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ","))) |
| 266 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ","))) |
| 267 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex))) |
| 268 | 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] | 269 | 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] | 270 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ","))) |
| 271 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported))) |
| 272 | 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] | 273 | 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] | 274 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct))) |
Cole Faust | 48ce137 | 2023-09-05 16:07:49 -0700 | [diff] [blame] | 275 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_platform=%s\n", label)) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 276 | 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] | 277 | 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] | 278 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte))) |
| 279 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents))) |
| 280 | 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] | 281 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_exclude_paths=%s\n", strings.Join(productVariables.MemtagHeapExcludePaths, ","))) |
| 282 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:memtag_heap_async_include_paths=%s\n", strings.Join(productVariables.MemtagHeapAsyncIncludePaths, ","))) |
| 283 | 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] | 284 | 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] | 285 | 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] | 286 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_final=%t\n", proptools.Bool(productVariables.Platform_sdk_final))) |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 287 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name))) |
| 288 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand)) |
| 289 | 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] | 290 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission)) |
| 291 | // Empty string can't be used as label_flag on the bazel side |
| 292 | if len(productVariables.ReleaseAconfigValueSets) > 0 { |
| 293 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", productVariables.ReleaseAconfigValueSets)) |
| 294 | } |
| 295 | 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] | 296 | 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] | 297 | 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] | 298 | 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] | 299 | 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] | 300 | 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] | 301 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build))) |
| 302 | 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] | 303 | |
| 304 | for _, override := range productVariables.CertificateOverrides { |
| 305 | parts := strings.SplitN(override, ":", 2) |
| 306 | if apexPath, ok := convertedModulePathMap[parts[0]]; ok { |
| 307 | if overrideCertPath, ok := convertedModulePathMap[parts[1]]; ok { |
| 308 | result.WriteString(fmt.Sprintf(" --%s:%s_certificate_override=%s:%s\n", apexPath, parts[0], overrideCertPath, parts[1])) |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 313 | for namespace, namespaceContents := range productVariables.VendorVars { |
| 314 | for variable, value := range namespaceContents { |
| 315 | key := namespace + "__" + variable |
| 316 | _, hasBool := soongConfigDefinitions.BoolVars[key] |
| 317 | _, hasString := soongConfigDefinitions.StringVars[key] |
| 318 | _, hasValue := soongConfigDefinitions.ValueVars[key] |
| 319 | if !hasBool && !hasString && !hasValue { |
| 320 | // Not all soong config variables are defined in Android.bp files. For example, |
| 321 | // prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard |
| 322 | // way, that causes them to be present in the soong.variables file but not |
| 323 | // defined in an Android.bp file. There's also nothing stopping you from setting |
| 324 | // a variable in make that doesn't exist in soong. We only generate build |
| 325 | // settings for the ones that exist in soong, so skip all others. |
| 326 | continue |
| 327 | } |
| 328 | if hasBool && hasString || hasBool && hasValue || hasString && hasValue { |
| 329 | 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)) |
| 330 | } |
| 331 | if hasBool { |
| 332 | // Logic copied from soongConfig.Bool() |
| 333 | value = strings.ToLower(value) |
| 334 | if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" { |
| 335 | value = "true" |
| 336 | } else { |
| 337 | value = "false" |
| 338 | } |
| 339 | } |
| 340 | result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value)) |
| 341 | } |
| 342 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 343 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) { |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 347 | result := android.ProductVariables{} |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 348 | productVarsReflect := reflect.ValueOf(&result).Elem() |
| 349 | for i := 0; i < productVarsReflect.NumField(); i++ { |
| 350 | field := productVarsReflect.Field(i) |
| 351 | fieldType := productVarsReflect.Type().Field(i) |
| 352 | name := fieldType.Name |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 353 | if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" || |
| 354 | name == "RecoverySnapshotModules" { |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 355 | // These variables have more complicated types, and we don't need them right now |
| 356 | continue |
| 357 | } |
| 358 | if _, ok := in[name]; ok { |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 359 | if name == "VendorVars" { |
| 360 | vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name]) |
| 361 | if err != nil { |
| 362 | return result, err |
| 363 | } |
| 364 | field.Set(reflect.ValueOf(vendorVars)) |
| 365 | continue |
| 366 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 367 | switch field.Type().Kind() { |
| 368 | case reflect.Bool: |
| 369 | val, err := starlark_import.Unmarshal[bool](in[name]) |
| 370 | if err != nil { |
| 371 | return result, err |
| 372 | } |
| 373 | field.SetBool(val) |
| 374 | case reflect.String: |
| 375 | val, err := starlark_import.Unmarshal[string](in[name]) |
| 376 | if err != nil { |
| 377 | return result, err |
| 378 | } |
| 379 | field.SetString(val) |
| 380 | case reflect.Slice: |
| 381 | if field.Type().Elem().Kind() != reflect.String { |
| 382 | return result, fmt.Errorf("slices of types other than strings are unimplemented") |
| 383 | } |
| 384 | val, err := starlark_import.UnmarshalReflect(in[name], field.Type()) |
| 385 | if err != nil { |
| 386 | return result, err |
| 387 | } |
| 388 | field.Set(val) |
| 389 | case reflect.Pointer: |
| 390 | switch field.Type().Elem().Kind() { |
| 391 | case reflect.Bool: |
| 392 | val, err := starlark_import.UnmarshalNoneable[bool](in[name]) |
| 393 | if err != nil { |
| 394 | return result, err |
| 395 | } |
| 396 | field.Set(reflect.ValueOf(val)) |
| 397 | case reflect.String: |
| 398 | val, err := starlark_import.UnmarshalNoneable[string](in[name]) |
| 399 | if err != nil { |
| 400 | return result, err |
| 401 | } |
| 402 | field.Set(reflect.ValueOf(val)) |
| 403 | case reflect.Int: |
| 404 | val, err := starlark_import.UnmarshalNoneable[int](in[name]) |
| 405 | if err != nil { |
| 406 | return result, err |
| 407 | } |
| 408 | field.Set(reflect.ValueOf(val)) |
| 409 | default: |
| 410 | return result, fmt.Errorf("pointers of types other than strings/bools are unimplemented: %s", field.Type().Elem().Kind().String()) |
| 411 | } |
| 412 | default: |
| 413 | return result, fmt.Errorf("unimplemented type: %s", field.Type().String()) |
| 414 | } |
| 415 | } |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 416 | } |
Cole Faust | f055db6 | 2023-07-24 15:17:03 -0700 | [diff] [blame] | 417 | |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 418 | result.Native_coverage = proptools.BoolPtr( |
| 419 | proptools.Bool(result.GcovCoverage) || |
| 420 | proptools.Bool(result.ClangCoverage)) |
| 421 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 422 | return result, nil |
| 423 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 424 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 425 | func createTargets(productLabelsToVariables map[string]*android.ProductVariables, res map[string]BazelTargets) { |
| 426 | createGeneratedAndroidCertificateDirectories(productLabelsToVariables, res) |
| 427 | } |
| 428 | |
| 429 | func createGeneratedAndroidCertificateDirectories(productLabelsToVariables map[string]*android.ProductVariables, targets map[string]BazelTargets) { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 430 | var allDefaultAppCertificateDirs []string |
| 431 | for _, productVariables := range productLabelsToVariables { |
| 432 | if proptools.String(productVariables.DefaultAppCertificate) != "" { |
| 433 | d := filepath.Dir(proptools.String(productVariables.DefaultAppCertificate)) |
| 434 | if !android.InList(d, allDefaultAppCertificateDirs) { |
| 435 | allDefaultAppCertificateDirs = append(allDefaultAppCertificateDirs, d) |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | for _, dir := range allDefaultAppCertificateDirs { |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 440 | content := `filegroup( |
| 441 | name = "generated_android_certificate_directory", |
| 442 | srcs = glob([ |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 443 | "*.pk8", |
| 444 | "*.pem", |
| 445 | "*.avbpubkey", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 446 | ]), |
| 447 | visibility = ["//visibility:public"], |
| 448 | )` |
| 449 | targets[dir] = append(targets[dir], BazelTarget{ |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 450 | name: "generated_android_certificate_directory", |
| 451 | packageName: dir, |
| 452 | content: content, |
| 453 | ruleClass: "filegroup", |
| 454 | }) |
| 455 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 456 | } |