Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | func CreateProductConfigFiles( |
| 11 | ctx *CodegenContext) ([]BazelFile, error) { |
| 12 | cfg := &ctx.config |
| 13 | targetProduct := "unknown" |
| 14 | if cfg.HasDeviceProduct() { |
| 15 | targetProduct = cfg.DeviceProduct() |
| 16 | } |
| 17 | targetBuildVariant := "user" |
| 18 | if cfg.Eng() { |
| 19 | targetBuildVariant = "eng" |
| 20 | } else if cfg.Debuggable() { |
| 21 | targetBuildVariant = "userdebug" |
| 22 | } |
| 23 | |
| 24 | productVariablesFileName := cfg.ProductVariablesFileName |
| 25 | if !strings.HasPrefix(productVariablesFileName, "/") { |
| 26 | productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName) |
| 27 | } |
| 28 | bytes, err := os.ReadFile(productVariablesFileName) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | |
| 33 | // TODO(b/249685973): the name is product_config_platforms because product_config |
| 34 | // was already used for other files. Deduplicate them. |
| 35 | currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant) |
| 36 | |
| 37 | productReplacer := strings.NewReplacer( |
| 38 | "{PRODUCT}", targetProduct, |
| 39 | "{VARIANT}", targetBuildVariant, |
| 40 | "{PRODUCT_FOLDER}", currentProductFolder) |
| 41 | |
| 42 | result := []BazelFile{ |
| 43 | newFile( |
| 44 | currentProductFolder, |
| 45 | "soong.variables.bzl", |
| 46 | `variables = json.decode("""`+strings.ReplaceAll(string(bytes), "\\", "\\\\")+`""")`), |
| 47 | newFile( |
| 48 | currentProductFolder, |
| 49 | "BUILD", |
| 50 | productReplacer.Replace(` |
| 51 | package(default_visibility=[ |
| 52 | "@soong_injection//product_config_platforms:__subpackages__", |
| 53 | "@//build/bazel/product_config:__subpackages__", |
| 54 | ]) |
| 55 | load(":soong.variables.bzl", _soong_variables = "variables") |
Cole Faust | bd24982 | 2023-03-24 16:03:43 -0700 | [diff] [blame] | 56 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 57 | |
| 58 | android_product( |
| 59 | name = "{PRODUCT}-{VARIANT}", |
| 60 | soong_variables = _soong_variables, |
| 61 | ) |
| 62 | `)), |
| 63 | newFile( |
| 64 | "product_config_platforms", |
| 65 | "BUILD.bazel", |
| 66 | productReplacer.Replace(` |
| 67 | package(default_visibility = [ |
| 68 | "@//build/bazel/product_config:__subpackages__", |
| 69 | "@soong_injection//product_config_platforms:__subpackages__", |
| 70 | ]) |
| 71 | |
| 72 | # TODO(b/249685973): Remove this. It was only added for a platform_mappings file, |
| 73 | # which can possibly be replaced with autogenerating the platform_mappings file, |
| 74 | # or removing that file entirely. |
| 75 | alias( |
| 76 | name = "current_android_platform", |
| 77 | # TODO: When we start generating the platforms for more than just the |
| 78 | # currently lunched, product, turn this into a select with an arm for each product. |
| 79 | actual = "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}", |
| 80 | ) |
| 81 | |
| 82 | alias( |
| 83 | name = "product_vars", |
| 84 | actual = select({ |
| 85 | # TODO: When we start generating the platforms for more than just the |
| 86 | # currently lunched, product, this select should have an arm for each product. |
| 87 | "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_constraint_value": "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_product_vars", |
Cole Faust | ebc01a3 | 2023-03-27 16:08:03 -0700 | [diff] [blame] | 88 | "@soong_injection//product_config_platforms/products/aosp_arm_for_testing:aosp_arm_for_testing_constraint_value": "@soong_injection//product_config_platforms/products/aosp_arm_for_testing:aosp_arm_for_testing_product_vars", |
| 89 | "@soong_injection//product_config_platforms/products/aosp_arm64_for_testing:aosp_arm64_for_testing_constraint_value": "@soong_injection//product_config_platforms/products/aosp_arm64_for_testing:aosp_arm64_for_testing_product_vars", |
| 90 | "@soong_injection//product_config_platforms/products/aosp_x86_for_testing:aosp_x86_for_testing_constraint_value": "@soong_injection//product_config_platforms/products/aosp_x86_for_testing:aosp_x86_for_testing_product_vars", |
| 91 | "@soong_injection//product_config_platforms/products/aosp_x86_64_for_testing:aosp_x86_64_for_testing_constraint_value": "@soong_injection//product_config_platforms/products/aosp_x86_64_for_testing:aosp_x86_64_for_testing_product_vars", |
| 92 | "@soong_injection//product_config_platforms/products/aosp_arm64_for_testing_no_compression:aosp_arm64_for_testing_no_compression_constraint_value": "@soong_injection//product_config_platforms/products/aosp_arm64_for_testing_no_compression:aosp_arm64_for_testing_no_compression_product_vars", |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 93 | }), |
| 94 | ) |
| 95 | `)), |
| 96 | newFile( |
| 97 | "product_config_platforms", |
| 98 | "common.bazelrc", |
| 99 | productReplacer.Replace(` |
| 100 | build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64 |
| 101 | |
| 102 | build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT} |
| 103 | build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64 |
| 104 | build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64 |
Cole Faust | c167d0f | 2023-01-20 16:58:45 -0800 | [diff] [blame] | 105 | build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86 |
| 106 | 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] | 107 | `)), |
| 108 | newFile( |
| 109 | "product_config_platforms", |
| 110 | "linux.bazelrc", |
| 111 | productReplacer.Replace(` |
| 112 | build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64 |
| 113 | `)), |
| 114 | newFile( |
| 115 | "product_config_platforms", |
| 116 | "darwin.bazelrc", |
| 117 | productReplacer.Replace(` |
| 118 | build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64 |
| 119 | `)), |
| 120 | newFile( |
| 121 | "product_config_platforms", |
| 122 | "platform_mappings", |
| 123 | productReplacer.Replace(` |
| 124 | flags: |
| 125 | --cpu=k8 |
| 126 | @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT} |
| 127 | `)), |
| 128 | } |
| 129 | |
Cole Faust | ebc01a3 | 2023-03-27 16:08:03 -0700 | [diff] [blame] | 130 | // Add some products for testing |
| 131 | for _, arch := range []string{"arm", "arm64", "x86", "x86_64"} { |
| 132 | result = append(result, newFile( |
| 133 | fmt.Sprintf("product_config_platforms/products/aosp_%s_for_testing", arch), |
| 134 | "BUILD", |
| 135 | fmt.Sprintf(` |
| 136 | package(default_visibility=[ |
| 137 | "@soong_injection//product_config_platforms:__subpackages__", |
| 138 | "@//build/bazel/product_config:__subpackages__", |
| 139 | ]) |
| 140 | load("@//build/bazel/tests/products:aosp_%s.variables.bzl", _soong_variables = "variables") |
| 141 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
| 142 | |
| 143 | android_product( |
| 144 | name = "aosp_%s_for_testing", |
| 145 | soong_variables = _soong_variables, |
| 146 | ) |
| 147 | `, arch, arch))) |
| 148 | } |
| 149 | result = append(result, newFile( |
| 150 | "product_config_platforms/products/aosp_arm64_for_testing_no_compression", |
| 151 | "BUILD", |
| 152 | ` |
| 153 | package(default_visibility=[ |
| 154 | "@soong_injection//product_config_platforms:__subpackages__", |
| 155 | "@//build/bazel/product_config:__subpackages__", |
| 156 | ]) |
| 157 | load("@bazel_skylib//lib:dicts.bzl", "dicts") |
| 158 | load("@//build/bazel/tests/products:aosp_arm64.variables.bzl", _soong_variables = "variables") |
| 159 | load("@//build/bazel/product_config:android_product.bzl", "android_product") |
| 160 | |
| 161 | android_product( |
| 162 | name = "aosp_arm64_for_testing_no_compression", |
| 163 | soong_variables = dicts.add(_soong_variables, {"CompressedApex": False}), |
| 164 | ) |
| 165 | `)) |
| 166 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 167 | return result, nil |
| 168 | } |