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