blob: c8067af05bf77c66d48ca7ce2edb2bbb9e9b4798 [file] [log] [blame]
Cole Faustb85d1a12022-11-08 18:14:01 -08001package bp2build
2
3import (
Cole Faustf8231dd2023-04-21 17:37:11 -07004 "android/soong/android"
5 "android/soong/starlark_import"
6 "encoding/json"
Cole Faustb85d1a12022-11-08 18:14:01 -08007 "fmt"
8 "os"
9 "path/filepath"
10 "strings"
Cole Faustf8231dd2023-04-21 17:37:11 -070011
12 "github.com/google/blueprint/proptools"
13 "go.starlark.net/starlark"
Cole Faustb85d1a12022-11-08 18:14:01 -080014)
15
16func CreateProductConfigFiles(
Cole Faustf8231dd2023-04-21 17:37:11 -070017 ctx *CodegenContext) ([]BazelFile, []BazelFile, error) {
Cole Faustb85d1a12022-11-08 18:14:01 -080018 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 Faustf8231dd2023-04-21 17:37:11 -070034 productVariablesBytes, err := os.ReadFile(productVariablesFileName)
Cole Faustb85d1a12022-11-08 18:14:01 -080035 if err != nil {
Cole Faustf8231dd2023-04-21 17:37:11 -070036 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 Faustb85d1a12022-11-08 18:14:01 -080042 }
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 Faustf8231dd2023-04-21 17:37:11 -070053 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 Faustb85d1a12022-11-08 18:14:01 -080059 newFile(
60 currentProductFolder,
61 "soong.variables.bzl",
Cole Faustf8231dd2023-04-21 17:37:11 -070062 `variables = json.decode("""`+strings.ReplaceAll(string(productVariablesBytes), "\\", "\\\\")+`""")`),
Cole Faustb85d1a12022-11-08 18:14:01 -080063 newFile(
64 currentProductFolder,
65 "BUILD",
66 productReplacer.Replace(`
67package(default_visibility=[
68 "@soong_injection//product_config_platforms:__subpackages__",
69 "@//build/bazel/product_config:__subpackages__",
70])
71load(":soong.variables.bzl", _soong_variables = "variables")
Cole Faustbd249822023-03-24 16:03:43 -070072load("@//build/bazel/product_config:android_product.bzl", "android_product")
Cole Faustb85d1a12022-11-08 18:14:01 -080073
74android_product(
75 name = "{PRODUCT}-{VARIANT}",
76 soong_variables = _soong_variables,
77)
78`)),
79 newFile(
80 "product_config_platforms",
81 "BUILD.bazel",
82 productReplacer.Replace(`
83package(default_visibility = [
84 "@//build/bazel/product_config:__subpackages__",
85 "@soong_injection//product_config_platforms:__subpackages__",
86])
Jingwen Chen583ab212023-05-30 09:45:23 +000087
88load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
89load("@//build/bazel/product_config:android_product.bzl", "android_product")
90
Cole Faust319abae2023-06-06 15:12:49 -070091# 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 Chen583ab212023-05-30 09:45:23 +000096android_product(
Cole Faust319abae2023-06-06 15:12:49 -070097 name = "mixed_builds_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +000098 soong_variables = _soong_variables,
99)
Cole Faust117bb742023-03-29 14:46:20 -0700100`)),
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
109product_labels = [
Cole Faust319abae2023-06-06 15:12:49 -0700110 "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
Cole Faust117bb742023-03-29 14:46:20 -0700111 "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"
112]
Cole Faustb85d1a12022-11-08 18:14:01 -0800113`)),
114 newFile(
115 "product_config_platforms",
116 "common.bazelrc",
117 productReplacer.Replace(`
Cole Faustf8231dd2023-04-21 17:37:11 -0700118build --platform_mappings=platform_mappings
Cole Faust319abae2023-06-06 15:12:49 -0700119build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800120
Cole Faust319abae2023-06-06 15:12:49 -0700121build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
122build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
123build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
124build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
125build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800126`)),
127 newFile(
128 "product_config_platforms",
129 "linux.bazelrc",
130 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700131build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800132`)),
133 newFile(
134 "product_config_platforms",
135 "darwin.bazelrc",
136 productReplacer.Replace(`
Cole Faust319abae2023-06-06 15:12:49 -0700137build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800138`)),
139 }
Cole Faustf8231dd2023-04-21 17:37:11 -0700140 bp2buildDirFiles := []BazelFile{
141 newFile(
142 "",
143 "platform_mappings",
144 platformMappingContent),
145 }
146 return injectionDirFiles, bp2buildDirFiles, nil
147}
Cole Faustb85d1a12022-11-08 18:14:01 -0800148
Cole Faustf8231dd2023-04-21 17:37:11 -0700149func 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 Faust88c8efb2023-07-18 11:05:16 -0700166var 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 Faustf8231dd2023-04-21 17:37:11 -0700180func 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 Faust88c8efb2023-07-18 11:05:16 -0700183 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 Jobredeauxcc1b6762023-07-20 10:51:26 -0400186 buildSettings += fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ","))
Cole Faustf8231dd2023-04-21 17:37:11 -0700187 result := ""
Cole Faust88c8efb2023-07-18 11:05:16 -0700188 for _, suffix := range bazelPlatformSuffixes {
189 result += " " + label + suffix + "\n" + buildSettings
Cole Faustf8231dd2023-04-21 17:37:11 -0700190 }
191 return result
192}
193
194func starlarkMapToProductVariables(in map[string]starlark.Value) (android.ProductVariables, error) {
195 var err error
196 result := android.ProductVariables{}
Cole Faust88c8efb2023-07-18 11:05:16 -0700197 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 Faustf8231dd2023-04-21 17:37:11 -0700210 if err != nil {
211 return result, err
212 }
Romain Jobredeauxcc1b6762023-07-20 10:51:26 -0400213 result.DeviceAbi, err = starlark_import.Unmarshal[[]string](in["DeviceAbi"])
214 if err != nil {
215 return result, err
216 }
Cole Faustb85d1a12022-11-08 18:14:01 -0800217 return result, nil
218}