blob: df2204591b5598d98c58ce11601e017acd001f5e [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 (
Cole Faustf2aab5e2025-02-11 13:32:51 -080018 "slices"
19 "sync"
20
Colin Cross0875c522017-11-28 17:34:01 -080021 "github.com/google/blueprint"
Colin Cross0875c522017-11-28 17:34:01 -080022)
23
24// SingletonContext
25type SingletonContext interface {
Colin Cross3c0a83d2023-12-12 14:13:26 -080026 blueprintSingletonContext() blueprint.SingletonContext
27
Colin Crossaabf6792017-11-29 00:27:14 -080028 Config() Config
Colin Cross65494b92019-02-07 14:25:51 -080029 DeviceConfig() DeviceConfig
Colin Cross0875c522017-11-28 17:34:01 -080030
31 ModuleName(module blueprint.Module) string
32 ModuleDir(module blueprint.Module) string
33 ModuleSubDir(module blueprint.Module) string
34 ModuleType(module blueprint.Module) string
35 BlueprintFile(module blueprint.Module) string
36
Bob Badoureef4c1c2022-05-16 12:20:04 -070037 // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer` enforcing visibility rules.
38 // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context.
39 ModuleVariantsFromName(referer Module, name string) []Module
40
Yu Liu663e4502024-08-12 18:23:59 +000041 otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Crossd27e7b82020-07-02 11:38:17 -070042
Colin Cross0875c522017-11-28 17:34:01 -080043 ModuleErrorf(module blueprint.Module, format string, args ...interface{})
44 Errorf(format string, args ...interface{})
45 Failed() bool
46
47 Variable(pctx PackageContext, name, value string)
Colin Cross59014392017-12-12 11:05:06 -080048 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Cross0875c522017-11-28 17:34:01 -080049 Build(pctx PackageContext, params BuildParams)
Colin Crossc3d87d32020-06-04 13:25:17 -070050
51 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
52 // phony rules or real files. Phony can be called on the same name multiple times to add
53 // additional dependencies.
54 Phony(name string, deps ...Path)
55
Colin Cross0875c522017-11-28 17:34:01 -080056 RequireNinjaVersion(major, minor, micro int)
57
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020058 // SetOutDir sets the value of the top-level "builddir" Ninja variable
Colin Cross0875c522017-11-28 17:34:01 -080059 // that controls where Ninja stores its build log files. This value can be
60 // set at most one time for a single build, later calls are ignored.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020061 SetOutDir(pctx PackageContext, value string)
Colin Cross0875c522017-11-28 17:34:01 -080062
63 // Eval takes a string with embedded ninja variables, and returns a string
64 // with all of the variables recursively expanded. Any variables references
65 // are expanded in the scope of the PackageContext.
66 Eval(pctx PackageContext, ninjaStr string) (string, error)
67
Colin Cross2465c3d2018-09-28 10:19:18 -070068 VisitAllModulesBlueprint(visit func(blueprint.Module))
Colin Cross0875c522017-11-28 17:34:01 -080069 VisitAllModules(visit func(Module))
Yu Liuec7043d2024-11-05 18:22:20 +000070 VisitAllModuleProxies(visit func(proxy ModuleProxy))
Colin Cross0875c522017-11-28 17:34:01 -080071 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Mitch Phillips3a7a31b2019-10-16 15:00:12 -070072
73 VisitDirectDeps(module Module, visit func(Module))
74 VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module))
75
Colin Cross6b753602018-06-21 13:03:07 -070076 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080077 VisitDepsDepthFirst(module Module, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -070078 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080079 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
80 visit func(Module))
81
82 VisitAllModuleVariants(module Module, visit func(Module))
83
Yu Liuec7043d2024-11-05 18:22:20 +000084 VisitAllModuleVariantProxies(module Module, visit func(proxy ModuleProxy))
85
Colin Cross0875c522017-11-28 17:34:01 -080086 PrimaryModule(module Module) Module
Yu Liu88ea9ff2024-11-07 19:19:42 +000087 IsFinalModule(module Module) bool
Colin Cross0875c522017-11-28 17:34:01 -080088
89 AddNinjaFileDeps(deps ...string)
90
91 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
92 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
93 // builder whenever a file matching the pattern as added or removed, without rerunning if a
94 // file that does not match the pattern is added to a searched directory.
95 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Cole Faust02987bd2024-03-21 17:58:43 -070096
97 // OtherModulePropertyErrorf reports an error on the line number of the given property of the given module
98 OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{})
Cole Faust4e2bf9f2024-09-11 13:26:20 -070099
100 // HasMutatorFinished returns true if the given mutator has finished running.
101 // It will panic if given an invalid mutator name.
102 HasMutatorFinished(mutatorName string) bool
Cole Faustf2aab5e2025-02-11 13:32:51 -0800103
104 // DistForGoals creates a rule to copy one or more Paths to the artifacts
105 // directory on the build server when any of the specified goals are built.
106 DistForGoal(goal string, paths ...Path)
107
108 // DistForGoalWithFilename creates a rule to copy a Path to the artifacts
109 // directory on the build server with the given filename when the specified
110 // goal is built.
111 DistForGoalWithFilename(goal string, path Path, filename string)
112
113 // DistForGoals creates a rule to copy one or more Paths to the artifacts
114 // directory on the build server when any of the specified goals are built.
115 DistForGoals(goals []string, paths ...Path)
116
117 // DistForGoalsWithFilename creates a rule to copy a Path to the artifacts
118 // directory on the build server with the given filename when any of the
119 // specified goals are built.
120 DistForGoalsWithFilename(goals []string, path Path, filename string)
Colin Cross0875c522017-11-28 17:34:01 -0800121}
122
123type singletonAdaptor struct {
124 Singleton
Colin Cross4c83e5c2019-02-25 14:54:28 -0800125
126 buildParams []BuildParams
127 ruleParams map[blueprint.Rule]blueprint.RuleParams
Colin Cross0875c522017-11-28 17:34:01 -0800128}
129
Colin Cross4c83e5c2019-02-25 14:54:28 -0800130var _ testBuildProvider = (*singletonAdaptor)(nil)
131
132func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) {
133 sctx := &singletonContextAdaptor{SingletonContext: ctx}
134 if sctx.Config().captureBuild {
135 sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams)
136 }
137
138 s.Singleton.GenerateBuildActions(sctx)
139
140 s.buildParams = sctx.buildParams
141 s.ruleParams = sctx.ruleParams
Cole Faustf2aab5e2025-02-11 13:32:51 -0800142
143 if len(sctx.dists) > 0 {
144 dists := getSingletonDists(sctx.Config())
145 dists.lock.Lock()
146 defer dists.lock.Unlock()
147 dists.dists = append(dists.dists, sctx.dists...)
148 }
Colin Cross4c83e5c2019-02-25 14:54:28 -0800149}
150
151func (s *singletonAdaptor) BuildParamsForTests() []BuildParams {
152 return s.buildParams
153}
154
155func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
156 return s.ruleParams
Colin Cross0875c522017-11-28 17:34:01 -0800157}
158
Cole Faustf2aab5e2025-02-11 13:32:51 -0800159var singletonDistsKey = NewOnceKey("singletonDistsKey")
160
161type singletonDistsAndLock struct {
162 dists []dist
163 lock sync.Mutex
164}
165
166func getSingletonDists(config Config) *singletonDistsAndLock {
167 return config.Once(singletonDistsKey, func() interface{} {
168 return &singletonDistsAndLock{}
169 }).(*singletonDistsAndLock)
170}
171
Colin Cross0875c522017-11-28 17:34:01 -0800172type Singleton interface {
173 GenerateBuildActions(SingletonContext)
174}
175
176type singletonContextAdaptor struct {
177 blueprint.SingletonContext
Colin Cross4c83e5c2019-02-25 14:54:28 -0800178
179 buildParams []BuildParams
180 ruleParams map[blueprint.Rule]blueprint.RuleParams
Cole Faustf2aab5e2025-02-11 13:32:51 -0800181 dists []dist
Colin Cross0875c522017-11-28 17:34:01 -0800182}
183
Colin Cross3c0a83d2023-12-12 14:13:26 -0800184func (s *singletonContextAdaptor) blueprintSingletonContext() blueprint.SingletonContext {
185 return s.SingletonContext
186}
187
Colin Cross4c83e5c2019-02-25 14:54:28 -0800188func (s *singletonContextAdaptor) Config() Config {
Colin Crossaabf6792017-11-29 00:27:14 -0800189 return s.SingletonContext.Config().(Config)
190}
191
Colin Cross4c83e5c2019-02-25 14:54:28 -0800192func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800193 return DeviceConfig{s.Config().deviceConfig}
194}
195
Colin Cross4c83e5c2019-02-25 14:54:28 -0800196func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) {
Colin Cross0875c522017-11-28 17:34:01 -0800197 s.SingletonContext.Variable(pctx.PackageContext, name, value)
198}
199
Colin Cross4c83e5c2019-02-25 14:54:28 -0800200func (s *singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule {
Ramy Medhat944839a2020-03-31 22:14:52 -0400201 if s.Config().UseRemoteBuild() {
202 if params.Pool == nil {
203 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
204 // jobs to the local parallelism value
205 params.Pool = localPool
206 } else if params.Pool == remotePool {
207 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
208 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
209 // parallelism.
210 params.Pool = nil
211 }
Colin Cross2e2dbc22019-09-25 13:31:46 -0700212 }
Colin Cross4c83e5c2019-02-25 14:54:28 -0800213 rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...)
214 if s.Config().captureBuild {
215 s.ruleParams[rule] = params
216 }
217 return rule
Colin Cross0875c522017-11-28 17:34:01 -0800218}
219
Colin Cross4c83e5c2019-02-25 14:54:28 -0800220func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) {
221 if s.Config().captureBuild {
222 s.buildParams = append(s.buildParams, params)
223 }
Colin Cross0875c522017-11-28 17:34:01 -0800224 bparams := convertBuildParams(params)
225 s.SingletonContext.Build(pctx.PackageContext, bparams)
Colin Cross0875c522017-11-28 17:34:01 -0800226}
227
Colin Crossc3d87d32020-06-04 13:25:17 -0700228func (s *singletonContextAdaptor) Phony(name string, deps ...Path) {
Yu Liu54513622024-08-19 20:00:32 +0000229 addSingletonPhony(s.Config(), name, deps...)
Colin Crossc3d87d32020-06-04 13:25:17 -0700230}
231
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200232func (s *singletonContextAdaptor) SetOutDir(pctx PackageContext, value string) {
233 s.SingletonContext.SetOutDir(pctx.PackageContext, value)
Colin Cross0875c522017-11-28 17:34:01 -0800234}
235
Colin Cross4c83e5c2019-02-25 14:54:28 -0800236func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) {
Colin Cross0875c522017-11-28 17:34:01 -0800237 return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr)
238}
239
240// visitAdaptor wraps a visit function that takes an android.Module parameter into
Yu Liuec7043d2024-11-05 18:22:20 +0000241// a function that takes a blueprint.Module parameter and only calls the visit function if the
Colin Cross0875c522017-11-28 17:34:01 -0800242// blueprint.Module is an android.Module.
243func visitAdaptor(visit func(Module)) func(blueprint.Module) {
244 return func(module blueprint.Module) {
245 if aModule, ok := module.(Module); ok {
246 visit(aModule)
247 }
248 }
249}
250
Yu Liuec7043d2024-11-05 18:22:20 +0000251// visitProxyAdaptor wraps a visit function that takes an android.ModuleProxy parameter into
252// a function that takes a blueprint.ModuleProxy parameter.
253func visitProxyAdaptor(visit func(proxy ModuleProxy)) func(proxy blueprint.ModuleProxy) {
254 return func(module blueprint.ModuleProxy) {
255 visit(ModuleProxy{
256 module: module,
257 })
258 }
259}
260
Colin Cross0875c522017-11-28 17:34:01 -0800261// predAdaptor wraps a pred function that takes an android.Module parameter
262// into a function that takes an blueprint.Module parameter and only calls the visit function if the
263// blueprint.Module is an android.Module, otherwise returns false.
264func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool {
265 return func(module blueprint.Module) bool {
266 if aModule, ok := module.(Module); ok {
267 return pred(aModule)
268 } else {
269 return false
270 }
271 }
272}
273
Colin Cross4c83e5c2019-02-25 14:54:28 -0800274func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
Colin Cross2465c3d2018-09-28 10:19:18 -0700275 s.SingletonContext.VisitAllModules(visit)
276}
277
Colin Cross4c83e5c2019-02-25 14:54:28 -0800278func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800279 s.SingletonContext.VisitAllModules(visitAdaptor(visit))
280}
281
Yu Liuec7043d2024-11-05 18:22:20 +0000282func (s *singletonContextAdaptor) VisitAllModuleProxies(visit func(proxy ModuleProxy)) {
283 s.SingletonContext.VisitAllModuleProxies(visitProxyAdaptor(visit))
284}
285
Colin Cross4c83e5c2019-02-25 14:54:28 -0800286func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800287 s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit))
288}
289
Mitch Phillips3a7a31b2019-10-16 15:00:12 -0700290func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) {
291 s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit))
292}
293
294func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) {
295 s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit))
296}
297
Colin Cross4c83e5c2019-02-25 14:54:28 -0800298func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800299 s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit))
300}
301
Colin Cross4c83e5c2019-02-25 14:54:28 -0800302func (s *singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800303 s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit))
304}
305
Colin Cross4c83e5c2019-02-25 14:54:28 -0800306func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800307 s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit))
308}
309
Yu Liuec7043d2024-11-05 18:22:20 +0000310func (s *singletonContextAdaptor) VisitAllModuleVariantProxies(module Module, visit func(proxy ModuleProxy)) {
311 s.SingletonContext.VisitAllModuleVariantProxies(module, visitProxyAdaptor(visit))
312}
313
Colin Cross4c83e5c2019-02-25 14:54:28 -0800314func (s *singletonContextAdaptor) PrimaryModule(module Module) Module {
Colin Cross0875c522017-11-28 17:34:01 -0800315 return s.SingletonContext.PrimaryModule(module).(Module)
316}
317
Yu Liu88ea9ff2024-11-07 19:19:42 +0000318func (s *singletonContextAdaptor) IsFinalModule(module Module) bool {
319 return s.SingletonContext.IsFinalModule(module)
Colin Cross0875c522017-11-28 17:34:01 -0800320}
Bob Badoureef4c1c2022-05-16 12:20:04 -0700321
322func (s *singletonContextAdaptor) ModuleVariantsFromName(referer Module, name string) []Module {
Cole Faust894bb3b2024-02-07 11:28:26 -0800323 // get module reference for visibility enforcement
Cole Faust9a24d902024-03-18 15:38:12 -0700324 qualified := createVisibilityModuleReference(s.ModuleName(referer), s.ModuleDir(referer), referer)
Bob Badoureef4c1c2022-05-16 12:20:04 -0700325
326 modules := s.SingletonContext.ModuleVariantsFromName(referer, name)
327 result := make([]Module, 0, len(modules))
328 for _, m := range modules {
329 if module, ok := m.(Module); ok {
330 // enforce visibility
331 depName := s.ModuleName(module)
332 depDir := s.ModuleDir(module)
333 depQualified := qualifiedModuleName{depDir, depName}
334 // Targets are always visible to other targets in their own package.
Cole Faust894bb3b2024-02-07 11:28:26 -0800335 if depQualified.pkg != qualified.name.pkg {
Bob Badoureef4c1c2022-05-16 12:20:04 -0700336 rule := effectiveVisibilityRules(s.Config(), depQualified)
337 if !rule.matches(qualified) {
Bob Badoura5ea2472022-05-20 16:37:26 -0700338 s.ModuleErrorf(referer, "module %q references %q which is not visible to this module\nYou may need to add %q to its visibility",
339 referer.Name(), depQualified, "//"+s.ModuleDir(referer))
Bob Badoureef4c1c2022-05-16 12:20:04 -0700340 continue
341 }
342 }
343 result = append(result, module)
344 }
345 }
346 return result
347}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800348
Yu Liu663e4502024-08-12 18:23:59 +0000349func (s *singletonContextAdaptor) otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross3c0a83d2023-12-12 14:13:26 -0800350 return s.SingletonContext.ModuleProvider(module, provider)
351}
Cole Faust02987bd2024-03-21 17:58:43 -0700352
353func (s *singletonContextAdaptor) OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{}) {
Cole Fausta963b942024-04-11 17:43:00 -0700354 s.blueprintSingletonContext().OtherModulePropertyErrorf(module, property, format, args...)
Cole Faust02987bd2024-03-21 17:58:43 -0700355}
Cole Faust4e2bf9f2024-09-11 13:26:20 -0700356
357func (s *singletonContextAdaptor) HasMutatorFinished(mutatorName string) bool {
358 return s.blueprintSingletonContext().HasMutatorFinished(mutatorName)
359}
Cole Faustf2aab5e2025-02-11 13:32:51 -0800360func (s *singletonContextAdaptor) DistForGoal(goal string, paths ...Path) {
361 s.DistForGoals([]string{goal}, paths...)
362}
363
364func (s *singletonContextAdaptor) DistForGoalWithFilename(goal string, path Path, filename string) {
365 s.DistForGoalsWithFilename([]string{goal}, path, filename)
366}
367
368func (s *singletonContextAdaptor) DistForGoals(goals []string, paths ...Path) {
369 var copies distCopies
370 for _, path := range paths {
371 copies = append(copies, distCopy{
372 from: path,
373 dest: path.Base(),
374 })
375 }
376 s.dists = append(s.dists, dist{
377 goals: slices.Clone(goals),
378 paths: copies,
379 })
380}
381
382func (s *singletonContextAdaptor) DistForGoalsWithFilename(goals []string, path Path, filename string) {
383 s.dists = append(s.dists, dist{
384 goals: slices.Clone(goals),
385 paths: distCopies{{from: path, dest: filename}},
386 })
387}