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	"android/soong/android"
19)
20
21// A singleton module that collects all of the build flags declared in the
22// tree into a single combined file for export to the external flag setting
23// server (inside Google it's Gantry).
24//
25// Note that this is ALL build_declarations modules present in the tree, not just
26// ones that are relevant to the product currently being built, so that that infra
27// doesn't need to pull from multiple builds and merge them.
28func AllBuildFlagDeclarationsFactory() android.Singleton {
29	return &allBuildFlagDeclarationsSingleton{}
30}
31
32type allBuildFlagDeclarationsSingleton struct {
33	intermediateBinaryProtoPath android.OutputPath
34	intermediateTextProtoPath   android.OutputPath
35}
36
37func (this *allBuildFlagDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
38	// Find all of the build_flag_declarations modules
39	var intermediateFiles android.Paths
40	ctx.VisitAllModules(func(module android.Module) {
41		decl, ok := android.SingletonModuleProvider(ctx, module, BuildFlagDeclarationsProviderKey)
42		if !ok {
43			return
44		}
45		intermediateFiles = append(intermediateFiles, decl.IntermediateCacheOutputPath)
46	})
47
48	// Generate build action for build_flag (binary proto output)
49	this.intermediateBinaryProtoPath = android.PathForIntermediates(ctx, "all_build_flag_declarations.pb")
50	ctx.Build(pctx, android.BuildParams{
51		Rule:        allDeclarationsRule,
52		Inputs:      intermediateFiles,
53		Output:      this.intermediateBinaryProtoPath,
54		Description: "all_build_flag_declarations",
55		Args: map[string]string{
56			"intermediates": android.JoinPathsWithPrefix(intermediateFiles, "--intermediate "),
57		},
58	})
59	ctx.Phony("all_build_flag_declarations", this.intermediateBinaryProtoPath)
60
61	// Generate build action for build_flag (text proto output)
62	this.intermediateTextProtoPath = android.PathForIntermediates(ctx, "all_build_flag_declarations.textproto")
63	ctx.Build(pctx, android.BuildParams{
64		Rule:        allDeclarationsRuleTextProto,
65		Input:       this.intermediateBinaryProtoPath,
66		Output:      this.intermediateTextProtoPath,
67		Description: "all_build_flag_declarations_textproto",
68	})
69	ctx.Phony("all_build_flag_declarations_textproto", this.intermediateTextProtoPath)
70}
71
72func (this *allBuildFlagDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) {
73	ctx.DistForGoal("droid", this.intermediateBinaryProtoPath)
74	for _, goal := range []string{"docs", "droid", "sdk"} {
75		ctx.DistForGoalWithFilename(goal, this.intermediateBinaryProtoPath, "build_flags/all_flags.pb")
76		ctx.DistForGoalWithFilename(goal, this.intermediateTextProtoPath, "build_flags/all_flags.textproto")
77	}
78}
79