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