blob: 3b407556fbc22ab37fbe1e34f9f87b46e21717bb [file] [log] [blame]
LaMont Jonesc6aef162024-05-20 10:37:25 -07001// 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 {
LaMont Jones735a80b2024-10-02 15:42:53 -070033 flagsBinaryProtoPath android.OutputPath
34 flagsTextProtoPath android.OutputPath
35 configsBinaryProtoPath android.OutputPath
36 configsTextProtoPath android.OutputPath
LaMont Jonesc6aef162024-05-20 10:37:25 -070037}
38
39func (this *allBuildFlagDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
40 // Find all of the build_flag_declarations modules
LaMont Jones735a80b2024-10-02 15:42:53 -070041 var flagsFiles android.Paths
42 // Find all of the release_config_contribution modules
43 var contributionDirs android.Paths
LaMont Jonesc6aef162024-05-20 10:37:25 -070044 ctx.VisitAllModules(func(module android.Module) {
Yu Liu663e4502024-08-12 18:23:59 +000045 decl, ok := android.OtherModuleProvider(ctx, module, BuildFlagDeclarationsProviderKey)
LaMont Jones735a80b2024-10-02 15:42:53 -070046 if ok {
47 flagsFiles = append(flagsFiles, decl.IntermediateCacheOutputPath)
LaMont Jonesc6aef162024-05-20 10:37:25 -070048 }
LaMont Jones735a80b2024-10-02 15:42:53 -070049
50 contrib, ok := android.OtherModuleProvider(ctx, module, ReleaseConfigContributionsProviderKey)
51 if ok {
52 contributionDirs = append(contributionDirs, contrib.ContributionDir)
53 }
LaMont Jonesc6aef162024-05-20 10:37:25 -070054 })
55
56 // Generate build action for build_flag (binary proto output)
LaMont Jones735a80b2024-10-02 15:42:53 -070057 this.flagsBinaryProtoPath = android.PathForIntermediates(ctx, "all_build_flag_declarations.pb")
LaMont Jonesc6aef162024-05-20 10:37:25 -070058 ctx.Build(pctx, android.BuildParams{
59 Rule: allDeclarationsRule,
LaMont Jones735a80b2024-10-02 15:42:53 -070060 Inputs: flagsFiles,
61 Output: this.flagsBinaryProtoPath,
LaMont Jonesc6aef162024-05-20 10:37:25 -070062 Description: "all_build_flag_declarations",
63 Args: map[string]string{
LaMont Jones735a80b2024-10-02 15:42:53 -070064 "intermediates": android.JoinPathsWithPrefix(flagsFiles, "--intermediate "),
LaMont Jonesc6aef162024-05-20 10:37:25 -070065 },
66 })
LaMont Jones735a80b2024-10-02 15:42:53 -070067 ctx.Phony("all_build_flag_declarations", this.flagsBinaryProtoPath)
LaMont Jonesc6aef162024-05-20 10:37:25 -070068
69 // Generate build action for build_flag (text proto output)
LaMont Jones735a80b2024-10-02 15:42:53 -070070 this.flagsTextProtoPath = android.PathForIntermediates(ctx, "all_build_flag_declarations.textproto")
LaMont Jonesc6aef162024-05-20 10:37:25 -070071 ctx.Build(pctx, android.BuildParams{
72 Rule: allDeclarationsRuleTextProto,
LaMont Jones735a80b2024-10-02 15:42:53 -070073 Input: this.flagsBinaryProtoPath,
74 Output: this.flagsTextProtoPath,
LaMont Jonesc6aef162024-05-20 10:37:25 -070075 Description: "all_build_flag_declarations_textproto",
76 })
LaMont Jones735a80b2024-10-02 15:42:53 -070077 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 )
LaMont Jonesc6aef162024-05-20 10:37:25 -0700113}
114
115func (this *allBuildFlagDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) {
LaMont Jones735a80b2024-10-02 15:42:53 -0700116 ctx.DistForGoal("droid", this.flagsBinaryProtoPath)
LaMont Jones1639ab52024-10-04 12:48:01 -0700117 for _, goal := range []string{"docs", "droid", "sdk", "release_config_metadata"} {
LaMont Jones735a80b2024-10-02 15:42:53 -0700118 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")
LaMont Jonesc6aef162024-05-20 10:37:25 -0700122 }
123}