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" |
| 5 | "android/soong/starlark_import" |
| 6 | "encoding/json" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 11 | |
| 12 | "github.com/google/blueprint/proptools" |
| 13 | "go.starlark.net/starlark" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | func CreateProductConfigFiles( |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 17 | ctx *CodegenContext) ([]BazelFile, []BazelFile, error) { |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 18 | cfg := &ctx.config |
| 19 | targetProduct := "unknown" |
| 20 | if cfg.HasDeviceProduct() { |
| 21 | targetProduct = cfg.DeviceProduct() |
| 22 | } |
| 23 | targetBuildVariant := "user" |
| 24 | if cfg.Eng() { |
| 25 | targetBuildVariant = "eng" |
| 26 | } else if cfg.Debuggable() { |
| 27 | targetBuildVariant = "userdebug" |
| 28 | } |
| 29 | |
| 30 | productVariablesFileName := cfg.ProductVariablesFileName |
| 31 | if !strings.HasPrefix(productVariablesFileName, "/") { |
| 32 | productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName) |
| 33 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 34 | productVariablesBytes, err := os.ReadFile(productVariablesFileName) |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 35 | if err != nil { |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 36 | return nil, nil, err |
| 37 | } |
| 38 | productVariables := android.ProductVariables{} |
| 39 | err = json.Unmarshal(productVariablesBytes, &productVariables) |
| 40 | if err != nil { |
| 41 | return nil, nil, err |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // TODO(b/249685973): the name is product_config_platforms because product_config |
| 45 | // was already used for other files. Deduplicate them. |
| 46 | currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant) |
| 47 | |
| 48 | productReplacer := strings.NewReplacer( |
| 49 | "{PRODUCT}", targetProduct, |
| 50 | "{VARIANT}", targetBuildVariant, |
| 51 | "{PRODUCT_FOLDER}", currentProductFolder) |
| 52 | |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 53 | platformMappingContent, err := platformMappingContent(productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"), &productVariables) |
| 54 | if err != nil { |
| 55 | return nil, nil, err |
| 56 | } |
| 57 | |
| 58 | injectionDirFiles := []BazelFile{ |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 59 | newFile( |
| 60 | currentProductFolder, |
| 61 | "soong.variables.bzl", |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 62 | `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`), |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 63 | newFile( |
| 64 | currentProductFolder, |
| 65 | "BUILD", |
| 66 | productReplacer.Replace(` |
| 67 | package(default_visibility=[ |
| 68 | "@soong_injection//product_config_platforms:__subpackages__", |
| 69 | "@//build/bazel/product_config:__subpackages__", |
| 70 | ]) |
| 71 | load(":soong.variables.bzl", _soong_variables = "variables") |
Cole Faust | bd24982 | 2023-03-24 16:03:43 -0700 | [diff] [blame] | 72 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 73 | |
| 74 | android_product( |
| 75 | name = "{PRODUCT}-{VARIANT}", |
| 76 | soong_variables = _soong_variables, |
| 77 | ) |
| 78 | `)), |
| 79 | newFile( |
| 80 | "product_config_platforms", |
| 81 | "BUILD.bazel", |
| 82 | productReplacer.Replace(` |
| 83 | package(default_visibility = [ |
| 84 | "@//build/bazel/product_config:__subpackages__", |
| 85 | "@soong_injection//product_config_platforms:__subpackages__", |
| 86 | ]) |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 87 | |
| 88 | load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables") |
| 89 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
| 90 | |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 91 | # Bazel will qualify its outputs by the platform name. When switching between products, this |
| 92 | # means that soong-built files that depend on bazel-built files will suddenly get different |
| 93 | # dependency files, because the path changes, and they will be rebuilt. In order to avoid this |
| 94 | # extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts |
| 95 | # are always under the same path. |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 96 | android_product( |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 97 | name = "mixed_builds_product-{VARIANT}", |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 98 | soong_variables = _soong_variables, |
| 99 | ) |
Cole Faust | 117bb74 | 2023-03-29 14:46:20 -0700 | [diff] [blame] | 100 | `)), |
| 101 | newFile( |
| 102 | "product_config_platforms", |
| 103 | "product_labels.bzl", |
| 104 | productReplacer.Replace(` |
| 105 | # This file keeps a list of all the products in the android source tree, because they're |
| 106 | # discovered as part of a preprocessing step before bazel runs. |
| 107 | # TODO: When we start generating the platforms for more than just the |
| 108 | # currently lunched product, they should all be listed here |
| 109 | product_labels = [ |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 110 | "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}", |
Cole Faust | 117bb74 | 2023-03-29 14:46:20 -0700 | [diff] [blame] | 111 | "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}" |
| 112 | ] |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 113 | `)), |
| 114 | newFile( |
| 115 | "product_config_platforms", |
| 116 | "common.bazelrc", |
| 117 | productReplacer.Replace(` |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 118 | build --platform_mappings=platform_mappings |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 119 | build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 120 | |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 121 | build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT} |
| 122 | build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64 |
| 123 | build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64 |
| 124 | build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86 |
| 125 | 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] | 126 | `)), |
| 127 | newFile( |
| 128 | "product_config_platforms", |
| 129 | "linux.bazelrc", |
| 130 | productReplacer.Replace(` |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 131 | build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 132 | `)), |
| 133 | newFile( |
| 134 | "product_config_platforms", |
| 135 | "darwin.bazelrc", |
| 136 | productReplacer.Replace(` |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 137 | build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64 |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 138 | `)), |
| 139 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 140 | bp2buildDirFiles := []BazelFile{ |
| 141 | newFile( |
| 142 | "", |
| 143 | "platform_mappings", |
| 144 | platformMappingContent), |
| 145 | } |
| 146 | return injectionDirFiles, bp2buildDirFiles, nil |
| 147 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 148 | |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 149 | func platformMappingContent(mainProductLabel string, mainProductVariables *android.ProductVariables) (string, error) { |
| 150 | productsForTesting, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing") |
| 151 | if err != nil { |
| 152 | return "", err |
| 153 | } |
| 154 | result := "platforms:\n" |
| 155 | result += platformMappingSingleProduct(mainProductLabel, mainProductVariables) |
| 156 | for product, productVariablesStarlark := range productsForTesting { |
| 157 | productVariables, err := starlarkMapToProductVariables(productVariablesStarlark) |
| 158 | if err != nil { |
| 159 | return "", err |
| 160 | } |
| 161 | result += platformMappingSingleProduct("@//build/bazel/tests/products:"+product, &productVariables) |
| 162 | } |
| 163 | return result, nil |
| 164 | } |
| 165 | |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 166 | var bazelPlatformSuffixes = []string{ |
| 167 | "", |
| 168 | "_darwin_arm64", |
| 169 | "_darwin_x86_64", |
| 170 | "_linux_bionic_arm64", |
| 171 | "_linux_bionic_x86_64", |
| 172 | "_linux_musl_x86", |
| 173 | "_linux_musl_x86_64", |
| 174 | "_linux_x86", |
| 175 | "_linux_x86_64", |
| 176 | "_windows_x86", |
| 177 | "_windows_x86_64", |
| 178 | } |
| 179 | |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 180 | func platformMappingSingleProduct(label string, productVariables *android.ProductVariables) string { |
| 181 | buildSettings := "" |
| 182 | buildSettings += fmt.Sprintf(" --//build/bazel/product_config:apex_global_min_sdk_version_override=%s\n", proptools.String(productVariables.ApexGlobalMinSdkVersionOverride)) |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 183 | buildSettings += fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")) |
| 184 | buildSettings += fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")) |
| 185 | buildSettings += fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)) |
Romain Jobredeaux | cc1b676 | 2023-07-20 10:51:26 -0400 | [diff] [blame] | 186 | buildSettings += fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 187 | result := "" |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 188 | for _, suffix := range bazelPlatformSuffixes { |
| 189 | result += " " + label + suffix + "\n" + buildSettings |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 190 | } |
| 191 | return result |
| 192 | } |
| 193 | |
| 194 | func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) { |
| 195 | var err error |
| 196 | result := android.ProductVariables{} |
Cole Faust | 88c8efb | 2023-07-18 11:05:16 -0700 | [diff] [blame] | 197 | result.ApexGlobalMinSdkVersionOverride, err = starlark_import.UnmarshalNoneable[string](in["ApexGlobalMinSdkVersionOverride"]) |
| 198 | if err != nil { |
| 199 | return result, err |
| 200 | } |
| 201 | result.CFIIncludePaths, err = starlark_import.Unmarshal[[]string](in["CFIIncludePaths"]) |
| 202 | if err != nil { |
| 203 | return result, err |
| 204 | } |
| 205 | result.CFIExcludePaths, err = starlark_import.Unmarshal[[]string](in["CFIExcludePaths"]) |
| 206 | if err != nil { |
| 207 | return result, err |
| 208 | } |
| 209 | result.EnableCFI, err = starlark_import.UnmarshalNoneable[bool](in["EnableCFI"]) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 210 | if err != nil { |
| 211 | return result, err |
| 212 | } |
Romain Jobredeaux | cc1b676 | 2023-07-20 10:51:26 -0400 | [diff] [blame] | 213 | result.DeviceAbi, err = starlark_import.Unmarshal[[]string](in["DeviceAbi"]) |
| 214 | if err != nil { |
| 215 | return result, err |
| 216 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 217 | return result, nil |
| 218 | } |