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