blob: 5f939967ca4aeb20a9f67c4fede85fbc94be82c2 [file] [log] [blame]
Colin Cross0875c522017-11-28 17:34:01 -08001// Copyright 2017 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 android
16
17import (
18 "github.com/google/blueprint"
Colin Cross0875c522017-11-28 17:34:01 -080019)
20
21// SingletonContext
22type SingletonContext interface {
Colin Cross3c0a83d2023-12-12 14:13:26 -080023 blueprintSingletonContext() blueprint.SingletonContext
24
Colin Crossaabf6792017-11-29 00:27:14 -080025 Config() Config
Colin Cross65494b92019-02-07 14:25:51 -080026 DeviceConfig() DeviceConfig
Colin Cross0875c522017-11-28 17:34:01 -080027
28 ModuleName(module blueprint.Module) string
29 ModuleDir(module blueprint.Module) string
30 ModuleSubDir(module blueprint.Module) string
31 ModuleType(module blueprint.Module) string
32 BlueprintFile(module blueprint.Module) string
33
Bob Badoureef4c1c2022-05-16 12:20:04 -070034 // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer` enforcing visibility rules.
35 // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context.
36 ModuleVariantsFromName(referer Module, name string) []Module
37
Colin Cross3c0a83d2023-12-12 14:13:26 -080038 moduleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Crossd27e7b82020-07-02 11:38:17 -070039
Colin Cross0875c522017-11-28 17:34:01 -080040 ModuleErrorf(module blueprint.Module, format string, args ...interface{})
41 Errorf(format string, args ...interface{})
42 Failed() bool
43
44 Variable(pctx PackageContext, name, value string)
Colin Cross59014392017-12-12 11:05:06 -080045 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Cross0875c522017-11-28 17:34:01 -080046 Build(pctx PackageContext, params BuildParams)
Colin Crossc3d87d32020-06-04 13:25:17 -070047
48 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
49 // phony rules or real files. Phony can be called on the same name multiple times to add
50 // additional dependencies.
51 Phony(name string, deps ...Path)
52
Colin Cross0875c522017-11-28 17:34:01 -080053 RequireNinjaVersion(major, minor, micro int)
54
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020055 // SetOutDir sets the value of the top-level "builddir" Ninja variable
Colin Cross0875c522017-11-28 17:34:01 -080056 // that controls where Ninja stores its build log files. This value can be
57 // set at most one time for a single build, later calls are ignored.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020058 SetOutDir(pctx PackageContext, value string)
Colin Cross0875c522017-11-28 17:34:01 -080059
60 // Eval takes a string with embedded ninja variables, and returns a string
61 // with all of the variables recursively expanded. Any variables references
62 // are expanded in the scope of the PackageContext.
63 Eval(pctx PackageContext, ninjaStr string) (string, error)
64
Colin Cross2465c3d2018-09-28 10:19:18 -070065 VisitAllModulesBlueprint(visit func(blueprint.Module))
Colin Cross0875c522017-11-28 17:34:01 -080066 VisitAllModules(visit func(Module))
67 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Mitch Phillips3a7a31b2019-10-16 15:00:12 -070068
69 VisitDirectDeps(module Module, visit func(Module))
70 VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module))
71
Colin Cross6b753602018-06-21 13:03:07 -070072 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080073 VisitDepsDepthFirst(module Module, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -070074 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080075 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
76 visit func(Module))
77
78 VisitAllModuleVariants(module Module, visit func(Module))
79
80 PrimaryModule(module Module) Module
81 FinalModule(module Module) Module
82
83 AddNinjaFileDeps(deps ...string)
84
85 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
86 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
87 // builder whenever a file matching the pattern as added or removed, without rerunning if a
88 // file that does not match the pattern is added to a searched directory.
89 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Cross0875c522017-11-28 17:34:01 -080090}
91
92type singletonAdaptor struct {
93 Singleton
Colin Cross4c83e5c2019-02-25 14:54:28 -080094
95 buildParams []BuildParams
96 ruleParams map[blueprint.Rule]blueprint.RuleParams
Colin Cross0875c522017-11-28 17:34:01 -080097}
98
Colin Cross4c83e5c2019-02-25 14:54:28 -080099var _ testBuildProvider = (*singletonAdaptor)(nil)
100
101func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) {
102 sctx := &singletonContextAdaptor{SingletonContext: ctx}
103 if sctx.Config().captureBuild {
104 sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams)
105 }
106
107 s.Singleton.GenerateBuildActions(sctx)
108
109 s.buildParams = sctx.buildParams
110 s.ruleParams = sctx.ruleParams
111}
112
113func (s *singletonAdaptor) BuildParamsForTests() []BuildParams {
114 return s.buildParams
115}
116
117func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
118 return s.ruleParams
Colin Cross0875c522017-11-28 17:34:01 -0800119}
120
121type Singleton interface {
122 GenerateBuildActions(SingletonContext)
123}
124
125type singletonContextAdaptor struct {
126 blueprint.SingletonContext
Colin Cross4c83e5c2019-02-25 14:54:28 -0800127
128 buildParams []BuildParams
129 ruleParams map[blueprint.Rule]blueprint.RuleParams
Colin Cross0875c522017-11-28 17:34:01 -0800130}
131
Colin Cross3c0a83d2023-12-12 14:13:26 -0800132func (s *singletonContextAdaptor) blueprintSingletonContext() blueprint.SingletonContext {
133 return s.SingletonContext
134}
135
Colin Cross4c83e5c2019-02-25 14:54:28 -0800136func (s *singletonContextAdaptor) Config() Config {
Colin Crossaabf6792017-11-29 00:27:14 -0800137 return s.SingletonContext.Config().(Config)
138}
139
Colin Cross4c83e5c2019-02-25 14:54:28 -0800140func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800141 return DeviceConfig{s.Config().deviceConfig}
142}
143
Colin Cross4c83e5c2019-02-25 14:54:28 -0800144func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) {
Colin Cross0875c522017-11-28 17:34:01 -0800145 s.SingletonContext.Variable(pctx.PackageContext, name, value)
146}
147
Colin Cross4c83e5c2019-02-25 14:54:28 -0800148func (s *singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule {
Ramy Medhat944839a2020-03-31 22:14:52 -0400149 if s.Config().UseRemoteBuild() {
150 if params.Pool == nil {
151 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
152 // jobs to the local parallelism value
153 params.Pool = localPool
154 } else if params.Pool == remotePool {
155 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
156 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
157 // parallelism.
158 params.Pool = nil
159 }
Colin Cross2e2dbc22019-09-25 13:31:46 -0700160 }
Colin Cross4c83e5c2019-02-25 14:54:28 -0800161 rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...)
162 if s.Config().captureBuild {
163 s.ruleParams[rule] = params
164 }
165 return rule
Colin Cross0875c522017-11-28 17:34:01 -0800166}
167
Colin Cross4c83e5c2019-02-25 14:54:28 -0800168func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) {
169 if s.Config().captureBuild {
170 s.buildParams = append(s.buildParams, params)
171 }
Colin Cross0875c522017-11-28 17:34:01 -0800172 bparams := convertBuildParams(params)
173 s.SingletonContext.Build(pctx.PackageContext, bparams)
Colin Cross0875c522017-11-28 17:34:01 -0800174}
175
Colin Crossc3d87d32020-06-04 13:25:17 -0700176func (s *singletonContextAdaptor) Phony(name string, deps ...Path) {
177 addPhony(s.Config(), name, deps...)
178}
179
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200180func (s *singletonContextAdaptor) SetOutDir(pctx PackageContext, value string) {
181 s.SingletonContext.SetOutDir(pctx.PackageContext, value)
Colin Cross0875c522017-11-28 17:34:01 -0800182}
183
Colin Cross4c83e5c2019-02-25 14:54:28 -0800184func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) {
Colin Cross0875c522017-11-28 17:34:01 -0800185 return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr)
186}
187
188// visitAdaptor wraps a visit function that takes an android.Module parameter into
189// a function that takes an blueprint.Module parameter and only calls the visit function if the
190// blueprint.Module is an android.Module.
191func visitAdaptor(visit func(Module)) func(blueprint.Module) {
192 return func(module blueprint.Module) {
193 if aModule, ok := module.(Module); ok {
194 visit(aModule)
195 }
196 }
197}
198
199// predAdaptor wraps a pred function that takes an android.Module parameter
200// into a function that takes an blueprint.Module parameter and only calls the visit function if the
201// blueprint.Module is an android.Module, otherwise returns false.
202func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool {
203 return func(module blueprint.Module) bool {
204 if aModule, ok := module.(Module); ok {
205 return pred(aModule)
206 } else {
207 return false
208 }
209 }
210}
211
Colin Cross4c83e5c2019-02-25 14:54:28 -0800212func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
Colin Cross2465c3d2018-09-28 10:19:18 -0700213 s.SingletonContext.VisitAllModules(visit)
214}
215
Colin Cross4c83e5c2019-02-25 14:54:28 -0800216func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800217 s.SingletonContext.VisitAllModules(visitAdaptor(visit))
218}
219
Colin Cross4c83e5c2019-02-25 14:54:28 -0800220func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800221 s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit))
222}
223
Mitch Phillips3a7a31b2019-10-16 15:00:12 -0700224func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) {
225 s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit))
226}
227
228func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) {
229 s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit))
230}
231
Colin Cross4c83e5c2019-02-25 14:54:28 -0800232func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800233 s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit))
234}
235
Colin Cross4c83e5c2019-02-25 14:54:28 -0800236func (s *singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800237 s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit))
238}
239
Colin Cross4c83e5c2019-02-25 14:54:28 -0800240func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800241 s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit))
242}
243
Colin Cross4c83e5c2019-02-25 14:54:28 -0800244func (s *singletonContextAdaptor) PrimaryModule(module Module) Module {
Colin Cross0875c522017-11-28 17:34:01 -0800245 return s.SingletonContext.PrimaryModule(module).(Module)
246}
247
Colin Cross4c83e5c2019-02-25 14:54:28 -0800248func (s *singletonContextAdaptor) FinalModule(module Module) Module {
Colin Cross0875c522017-11-28 17:34:01 -0800249 return s.SingletonContext.FinalModule(module).(Module)
250}
Bob Badoureef4c1c2022-05-16 12:20:04 -0700251
252func (s *singletonContextAdaptor) ModuleVariantsFromName(referer Module, name string) []Module {
Cole Faust894bb3b2024-02-07 11:28:26 -0800253 // get module reference for visibility enforcement
Cole Faust9a24d902024-03-18 15:38:12 -0700254 qualified := createVisibilityModuleReference(s.ModuleName(referer), s.ModuleDir(referer), referer)
Bob Badoureef4c1c2022-05-16 12:20:04 -0700255
256 modules := s.SingletonContext.ModuleVariantsFromName(referer, name)
257 result := make([]Module, 0, len(modules))
258 for _, m := range modules {
259 if module, ok := m.(Module); ok {
260 // enforce visibility
261 depName := s.ModuleName(module)
262 depDir := s.ModuleDir(module)
263 depQualified := qualifiedModuleName{depDir, depName}
264 // Targets are always visible to other targets in their own package.
Cole Faust894bb3b2024-02-07 11:28:26 -0800265 if depQualified.pkg != qualified.name.pkg {
Bob Badoureef4c1c2022-05-16 12:20:04 -0700266 rule := effectiveVisibilityRules(s.Config(), depQualified)
267 if !rule.matches(qualified) {
Bob Badoura5ea2472022-05-20 16:37:26 -0700268 s.ModuleErrorf(referer, "module %q references %q which is not visible to this module\nYou may need to add %q to its visibility",
269 referer.Name(), depQualified, "//"+s.ModuleDir(referer))
Bob Badoureef4c1c2022-05-16 12:20:04 -0700270 continue
271 }
272 }
273 result = append(result, module)
274 }
275 }
276 return result
277}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800278
Colin Cross3c0a83d2023-12-12 14:13:26 -0800279func (s *singletonContextAdaptor) moduleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
280 return s.SingletonContext.ModuleProvider(module, provider)
281}