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 build_flags
16
17import (
18	"fmt"
19
20	"android/soong/android"
21)
22
23const (
24	outJsonFileName = "build_flags.json"
25)
26
27func init() {
28	registerBuildFlagsModuleType(android.InitRegistrationContext)
29}
30
31func registerBuildFlagsModuleType(ctx android.RegistrationContext) {
32	ctx.RegisterModuleType("build_flags_json", buildFlagsFactory)
33}
34
35type buildFlags struct {
36	android.ModuleBase
37
38	outputPath android.OutputPath
39}
40
41func buildFlagsFactory() android.Module {
42	module := &buildFlags{}
43	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
44	return module
45}
46
47func (m *buildFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
48	// Read the build_flags_<partition>.json file generated by soong
49	// 'release-config' command.
50	srcPath := android.PathForOutput(ctx, "release-config", fmt.Sprintf("build_flags_%s.json", m.PartitionTag(ctx.DeviceConfig())))
51	m.outputPath = android.PathForModuleOut(ctx, outJsonFileName).OutputPath
52
53	// The 'release-config' command is called for every build, and generates the
54	// build_flags_<partition>.json file.
55	// Update the output file only if the source file is changed.
56	ctx.Build(pctx, android.BuildParams{
57		Rule:   android.CpIfChanged,
58		Input:  srcPath,
59		Output: m.outputPath,
60	})
61
62	installPath := android.PathForModuleInstall(ctx, "etc")
63	ctx.InstallFile(installPath, outJsonFileName, m.outputPath)
64}
65
66func (m *buildFlags) AndroidMkEntries() []android.AndroidMkEntries {
67	return []android.AndroidMkEntries{android.AndroidMkEntries{
68		Class:      "ETC",
69		OutputFile: android.OptionalPathForPath(m.outputPath),
70	}}
71}
72