xref: /aosp_15_r20/build/soong/android/product_config.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18	"github.com/google/blueprint/proptools"
19)
20
21func init() {
22	ctx := InitRegistrationContext
23	ctx.RegisterModuleType("product_config", productConfigFactory)
24}
25
26type productConfigModule struct {
27	ModuleBase
28}
29
30func (p *productConfigModule) GenerateAndroidBuildActions(ctx ModuleContext) {
31	if ctx.ModuleName() != "product_config" || ctx.ModuleDir() != "build/soong" {
32		ctx.ModuleErrorf("There can only be one product_config module in build/soong")
33		return
34	}
35	outputFilePath := PathForModuleOut(ctx, p.Name()+".json")
36
37	// DeviceProduct can be null so calling ctx.Config().DeviceProduct() may cause null dereference
38	targetProduct := proptools.String(ctx.Config().config.productVariables.DeviceProduct)
39	if targetProduct != "" {
40		targetProduct += "."
41	}
42
43	coverageSuffix := ctx.Config().CoverageSuffix()
44	soongVariablesPath := PathForOutput(ctx, "soong."+targetProduct+coverageSuffix+"variables")
45	extraVariablesPath := PathForOutput(ctx, "soong."+targetProduct+coverageSuffix+"extra.variables")
46
47	rule := NewRuleBuilder(pctx, ctx)
48	rule.Command().BuiltTool("merge_json").
49		Output(outputFilePath).
50		Input(soongVariablesPath).
51		Input(extraVariablesPath).
52		rule.Build("product_config.json", "building product_config.json")
53
54	ctx.SetOutputFiles(Paths{outputFilePath}, "")
55}
56
57// product_config module exports product variables and extra variables as a JSON file.
58func productConfigFactory() Module {
59	module := &productConfigModule{}
60	InitAndroidModule(module)
61	return module
62}
63