1// Copyright 2023 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	"strings"
19
20	"android/soong/android"
21
22	"github.com/google/blueprint"
23)
24
25type BuildFlagDeclarationsProviderData struct {
26	IntermediateCacheOutputPath android.WritablePath
27	IntermediateDumpOutputPath  android.WritablePath
28}
29
30var BuildFlagDeclarationsProviderKey = blueprint.NewProvider[BuildFlagDeclarationsProviderData]()
31
32type DeclarationsModule struct {
33	android.ModuleBase
34	android.DefaultableModuleBase
35
36	// Properties for "aconfig_declarations"
37	properties struct {
38		// aconfig files, relative to this Android.bp file
39		Srcs []string `android:"path"`
40	}
41}
42
43func DeclarationsFactory() android.Module {
44	module := &DeclarationsModule{}
45
46	android.InitAndroidModule(module)
47	android.InitDefaultableModule(module)
48	module.AddProperties(&module.properties)
49
50	return module
51}
52
53func joinAndPrefix(prefix string, values []string) string {
54	var sb strings.Builder
55	for _, v := range values {
56		sb.WriteString(prefix)
57		sb.WriteString(v)
58	}
59	return sb.String()
60}
61
62func optionalVariable(prefix string, value string) string {
63	var sb strings.Builder
64	if value != "" {
65		sb.WriteString(prefix)
66		sb.WriteString(value)
67	}
68	return sb.String()
69}
70
71func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
72	// Intermediate format
73	declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
74	intermediateCacheFilePath := android.PathForModuleOut(ctx, "build_flag_intermediate.pb")
75	inputFiles := make([]android.Path, len(declarationFiles))
76	copy(inputFiles, declarationFiles)
77
78	// TODO(lamont): generate the rc_proto.FlagArtifacts message for the sources.
79	args := map[string]string{
80		"release_version": ctx.Config().ReleaseVersion(),
81		"declarations":    android.JoinPathsWithPrefix(declarationFiles, "--decl "),
82	}
83	ctx.Build(pctx, android.BuildParams{
84		Rule:        buildFlagRule,
85		Output:      intermediateCacheFilePath,
86		Inputs:      inputFiles,
87		Description: "build_flag_declarations",
88		Args:        args,
89	})
90
91	intermediateDumpFilePath := android.PathForModuleOut(ctx, "build_flag_intermediate.textproto")
92	ctx.Build(pctx, android.BuildParams{
93		Rule:        buildFlagTextRule,
94		Output:      intermediateDumpFilePath,
95		Input:       intermediateCacheFilePath,
96		Description: "build_flag_declarations_text",
97	})
98
99	android.SetProvider(ctx, BuildFlagDeclarationsProviderKey, BuildFlagDeclarationsProviderData{
100		IntermediateCacheOutputPath: intermediateCacheFilePath,
101		IntermediateDumpOutputPath:  intermediateDumpFilePath,
102	})
103}
104