• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package 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:android_product.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		newFile(
73			"product_config_platforms",
74			"product_labels.bzl",
75			productReplacer.Replace(`
76# This file keeps a list of all the products in the android source tree, because they're
77# discovered as part of a preprocessing step before bazel runs.
78# TODO: When we start generating the platforms for more than just the
79# currently lunched product, they should all be listed here
80product_labels = [
81  "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"
82]
83`)),
84		newFile(
85			"product_config_platforms",
86			"common.bazelrc",
87			productReplacer.Replace(`
88build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
89
90build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
91build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
92build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
93build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
94build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
95`)),
96		newFile(
97			"product_config_platforms",
98			"linux.bazelrc",
99			productReplacer.Replace(`
100build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
101`)),
102		newFile(
103			"product_config_platforms",
104			"darwin.bazelrc",
105			productReplacer.Replace(`
106build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
107`)),
108		newFile(
109			"product_config_platforms",
110			"platform_mappings",
111			productReplacer.Replace(`
112flags:
113  --cpu=k8
114    @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
115`)),
116	}
117
118	return result, nil
119}
120