blob: 66d0cc5546b23eff62b9503c6b75ce35d6b92a12 [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")
Cole Faustbd249822023-03-24 16:03:43 -070056load("@//build/bazel/product_config:android_product.bzl", "android_product")
Cole Faustb85d1a12022-11-08 18:14:01 -080057
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])
Jingwen Chen583ab212023-05-30 09:45:23 +000071
72load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
73load("@//build/bazel/product_config:android_product.bzl", "android_product")
74
75android_product(
Matthias Männich08ccec72023-06-08 08:19:14 +000076 name = "current_product-{VARIANT}",
Jingwen Chen583ab212023-05-30 09:45:23 +000077 soong_variables = _soong_variables,
78)
Cole Faust117bb742023-03-29 14:46:20 -070079`)),
80 newFile(
81 "product_config_platforms",
82 "product_labels.bzl",
83 productReplacer.Replace(`
84# This file keeps a list of all the products in the android source tree, because they're
85# discovered as part of a preprocessing step before bazel runs.
86# TODO: When we start generating the platforms for more than just the
87# currently lunched product, they should all be listed here
88product_labels = [
Matthias Männich08ccec72023-06-08 08:19:14 +000089 "@soong_injection//product_config_platforms:current_product-{VARIANT}",
Cole Faust117bb742023-03-29 14:46:20 -070090 "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"
91]
Cole Faustb85d1a12022-11-08 18:14:01 -080092`)),
93 newFile(
94 "product_config_platforms",
95 "common.bazelrc",
96 productReplacer.Replace(`
Matthias Männich08ccec72023-06-08 08:19:14 +000097# current_product refers to the current TARGET_PRODUCT set, usually through
98# 'lunch' or 'banchan'. Every build will have a primary TARGET_PRODUCT, but
99# bazel supports using other products in tests or configuration transitions. The
100# other products can be found in
101# @soong_injection//product_config_platforms/products/...
102build --platforms @soong_injection//product_config_platforms:current_product-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800103
Matthias Männich08ccec72023-06-08 08:19:14 +0000104build:android --platforms=@soong_injection//product_config_platforms:current_product-{VARIANT}
105build:linux_x86_64 --platforms=@soong_injection//product_config_platforms:current_product-{VARIANT}_linux_x86_64
106build:linux_bionic_x86_64 --platforms=@soong_injection//product_config_platforms:current_product-{VARIANT}_linux_bionic_x86_64
107build:linux_musl_x86 --platforms=@soong_injection//product_config_platforms:current_product-{VARIANT}_linux_musl_x86
108build:linux_musl_x86_64 --platforms=@soong_injection//product_config_platforms:current_product-{VARIANT}_linux_musl_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800109`)),
110 newFile(
111 "product_config_platforms",
112 "linux.bazelrc",
113 productReplacer.Replace(`
Matthias Männich08ccec72023-06-08 08:19:14 +0000114build --host_platform @soong_injection//product_config_platforms:current_product-{VARIANT}_linux_x86_64
Cole Faustb85d1a12022-11-08 18:14:01 -0800115`)),
116 newFile(
117 "product_config_platforms",
118 "darwin.bazelrc",
119 productReplacer.Replace(`
Matthias Männich08ccec72023-06-08 08:19:14 +0000120build --host_platform product_config_platforms:current_product-{VARIANT}_darwin_x86_64
121`)),
122 newFile(
123 "product_config_platforms",
124 "platform_mappings",
125 productReplacer.Replace(`
126flags:
127 --cpu=k8
128 @soong_injection//product_config_platforms:current_product-{VARIANT}
Cole Faustb85d1a12022-11-08 18:14:01 -0800129`)),
130 }
131
Cole Faustb85d1a12022-11-08 18:14:01 -0800132 return result, nil
133}