xref: /aosp_15_r20/build/soong/aconfig/build_flags/build_flags_singleton.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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	flagsBinaryProtoPath   android.OutputPath
34	flagsTextProtoPath     android.OutputPath
35	configsBinaryProtoPath android.OutputPath
36	configsTextProtoPath   android.OutputPath
37}
38
39func (this *allBuildFlagDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
40	// Find all of the build_flag_declarations modules
41	var flagsFiles android.Paths
42	// Find all of the release_config_contribution modules
43	var contributionDirs android.Paths
44	ctx.VisitAllModules(func(module android.Module) {
45		decl, ok := android.OtherModuleProvider(ctx, module, BuildFlagDeclarationsProviderKey)
46		if ok {
47			flagsFiles = append(flagsFiles, decl.IntermediateCacheOutputPath)
48		}
49
50		contrib, ok := android.OtherModuleProvider(ctx, module, ReleaseConfigContributionsProviderKey)
51		if ok {
52			contributionDirs = append(contributionDirs, contrib.ContributionDir)
53		}
54	})
55
56	// Generate build action for build_flag (binary proto output)
57	this.flagsBinaryProtoPath = android.PathForIntermediates(ctx, "all_build_flag_declarations.pb")
58	ctx.Build(pctx, android.BuildParams{
59		Rule:        allDeclarationsRule,
60		Inputs:      flagsFiles,
61		Output:      this.flagsBinaryProtoPath,
62		Description: "all_build_flag_declarations",
63		Args: map[string]string{
64			"intermediates": android.JoinPathsWithPrefix(flagsFiles, "--intermediate "),
65		},
66	})
67	ctx.Phony("all_build_flag_declarations", this.flagsBinaryProtoPath)
68
69	// Generate build action for build_flag (text proto output)
70	this.flagsTextProtoPath = android.PathForIntermediates(ctx, "all_build_flag_declarations.textproto")
71	ctx.Build(pctx, android.BuildParams{
72		Rule:        allDeclarationsRuleTextProto,
73		Input:       this.flagsBinaryProtoPath,
74		Output:      this.flagsTextProtoPath,
75		Description: "all_build_flag_declarations_textproto",
76	})
77	ctx.Phony("all_build_flag_declarations_textproto", this.flagsTextProtoPath)
78
79	// Generate build action for release_configs (binary proto output)
80	this.configsBinaryProtoPath = android.PathForIntermediates(ctx, "all_release_config_contributions.pb")
81	ctx.Build(pctx, android.BuildParams{
82		Rule:        allReleaseConfigContributionsRule,
83		Inputs:      contributionDirs,
84		Output:      this.configsBinaryProtoPath,
85		Description: "all_release_config_contributions",
86		Args: map[string]string{
87			"dirs":   android.JoinPathsWithPrefix(contributionDirs, "--dir "),
88			"format": "pb",
89		},
90	})
91	ctx.Phony("all_release_config_contributions", this.configsBinaryProtoPath)
92
93	this.configsTextProtoPath = android.PathForIntermediates(ctx, "all_release_config_contributions.textproto")
94	ctx.Build(pctx, android.BuildParams{
95		Rule:        allReleaseConfigContributionsRule,
96		Inputs:      contributionDirs,
97		Output:      this.configsTextProtoPath,
98		Description: "all_release_config_contributions_textproto",
99		Args: map[string]string{
100			"dirs":   android.JoinPathsWithPrefix(contributionDirs, "--dir "),
101			"format": "textproto",
102		},
103	})
104	ctx.Phony("all_release_config_contributions_textproto", this.configsTextProtoPath)
105
106	// Add a simple target for ci/build_metadata to use.
107	ctx.Phony("release_config_metadata",
108		this.flagsBinaryProtoPath,
109		this.flagsTextProtoPath,
110		this.configsBinaryProtoPath,
111		this.configsTextProtoPath,
112	)
113}
114
115func (this *allBuildFlagDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) {
116	ctx.DistForGoal("droid", this.flagsBinaryProtoPath)
117	for _, goal := range []string{"docs", "droid", "sdk", "release_config_metadata"} {
118		ctx.DistForGoalWithFilename(goal, this.flagsBinaryProtoPath, "build_flags/all_flags.pb")
119		ctx.DistForGoalWithFilename(goal, this.flagsTextProtoPath, "build_flags/all_flags.textproto")
120		ctx.DistForGoalWithFilename(goal, this.configsBinaryProtoPath, "build_flags/all_release_config_contributions.pb")
121		ctx.DistForGoalWithFilename(goal, this.configsTextProtoPath, "build_flags/all_release_config_contributions.textproto")
122	}
123}
124