blob: 3eec4390570812a3b2dfab7e18f68e58cac75e2d [file] [log] [blame]
Cole Faustb85d1a12022-11-08 18:14:01 -08001package bp2build
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8)
9
10func 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(`
51package(default_visibility=[
52 "@soong_injection//product_config_platforms:__subpackages__",
53 "@//build/bazel/product_config:__subpackages__",
54])
55load(":soong.variables.bzl", _soong_variables = "variables")
56load("@//build/bazel/product_config:utils.bzl", "android_product")
57
58android_product(
59 name = "{PRODUCT}-{VARIANT}",
60 soong_variables = _soong_variables,
61)
62`)),
63 newFile(
64 "product_config_platforms",
65 "BUILD.bazel",
66 productReplacer.Replace(`
67package(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.
75alias(
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
82alias(
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",
88 }),
89)
90`)),
91 newFile(
92 "product_config_platforms",
93 "common.bazelrc",
94 productReplacer.Replace(`
95build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
96
97build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
98build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
99build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
Cole Faustc167d0f2023-01-20 16:58:45 -0800100build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
101build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800102`)),
103 newFile(
104 "product_config_platforms",
105 "linux.bazelrc",
106 productReplacer.Replace(`
107build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
108`)),
109 newFile(
110 "product_config_platforms",
111 "darwin.bazelrc",
112 productReplacer.Replace(`
113build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
114`)),
115 newFile(
116 "product_config_platforms",
117 "platform_mappings",
118 productReplacer.Replace(`
119flags:
120 --cpu=k8
121 @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
122`)),
123 }
124
125 return result, nil
126}