blob: 7c6cf4fd68e105a09da97f9871fb60f3198915fe [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 Crossaabf6792017-11-29 00:27:14 -080023 Config() Config
Colin Cross65494b92019-02-07 14:25:51 -080024 DeviceConfig() DeviceConfig
Colin Cross0875c522017-11-28 17:34:01 -080025
26 ModuleName(module blueprint.Module) string
27 ModuleDir(module blueprint.Module) string
28 ModuleSubDir(module blueprint.Module) string
29 ModuleType(module blueprint.Module) string
30 BlueprintFile(module blueprint.Module) string
31
Bob Badoureef4c1c2022-05-16 12:20:04 -070032 // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer` enforcing visibility rules.
33 // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context.
34 ModuleVariantsFromName(referer Module, name string) []Module
35
Colin Crossd27e7b82020-07-02 11:38:17 -070036 // ModuleProvider returns the value, if any, for the provider for a module. If the value for the
37 // provider was not set it returns the zero value of the type of the provider, which means the
38 // return value can always be type-asserted to the type of the provider. The return value should
39 // always be considered read-only. It panics if called before the appropriate mutator or
40 // GenerateBuildActions pass for the provider on the module.
41 ModuleProvider(module blueprint.Module, provider blueprint.ProviderKey) interface{}
42
43 // ModuleHasProvider returns true if the provider for the given module has been set.
44 ModuleHasProvider(module blueprint.Module, provider blueprint.ProviderKey) bool
45
Colin Cross0875c522017-11-28 17:34:01 -080046 ModuleErrorf(module blueprint.Module, format string, args ...interface{})
47 Errorf(format string, args ...interface{})
48 Failed() bool
49
50 Variable(pctx PackageContext, name, value string)
Colin Cross59014392017-12-12 11:05:06 -080051 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Cross0875c522017-11-28 17:34:01 -080052 Build(pctx PackageContext, params BuildParams)
Colin Crossc3d87d32020-06-04 13:25:17 -070053
54 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
55 // phony rules or real files. Phony can be called on the same name multiple times to add
56 // additional dependencies.
57 Phony(name string, deps ...Path)
58
Colin Cross0875c522017-11-28 17:34:01 -080059 RequireNinjaVersion(major, minor, micro int)
60
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020061 // SetOutDir sets the value of the top-level "builddir" Ninja variable
Colin Cross0875c522017-11-28 17:34:01 -080062 // that controls where Ninja stores its build log files. This value can be
63 // set at most one time for a single build, later calls are ignored.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020064 SetOutDir(pctx PackageContext, value string)
Colin Cross0875c522017-11-28 17:34:01 -080065
66 // Eval takes a string with embedded ninja variables, and returns a string
67 // with all of the variables recursively expanded. Any variables references
68 // are expanded in the scope of the PackageContext.
69 Eval(pctx PackageContext, ninjaStr string) (string, error)
70
Colin Cross2465c3d2018-09-28 10:19:18 -070071 VisitAllModulesBlueprint(visit func(blueprint.Module))
Colin Cross0875c522017-11-28 17:34:01 -080072 VisitAllModules(visit func(Module))
73 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Mitch Phillips3a7a31b2019-10-16 15:00:12 -070074
75 VisitDirectDeps(module Module, visit func(Module))
76 VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module))
77
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 VisitDepsDepthFirst(module Module, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -070080 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080081 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
82 visit func(Module))
83
84 VisitAllModuleVariants(module Module, visit func(Module))
85
86 PrimaryModule(module Module) Module
87 FinalModule(module Module) Module
88
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)
Colin Cross0875c522017-11-28 17:34:01 -080096}
97
98type singletonAdaptor struct {
99 Singleton
Colin Cross4c83e5c2019-02-25 14:54:28 -0800100
101 buildParams []BuildParams
102 ruleParams map[blueprint.Rule]blueprint.RuleParams
Colin Cross0875c522017-11-28 17:34:01 -0800103}
104
Colin Cross4c83e5c2019-02-25 14:54:28 -0800105var _ testBuildProvider = (*singletonAdaptor)(nil)
106
107func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) {
108 sctx := &singletonContextAdaptor{SingletonContext: ctx}
109 if sctx.Config().captureBuild {
110 sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams)
111 }
112
113 s.Singleton.GenerateBuildActions(sctx)
114
115 s.buildParams = sctx.buildParams
116 s.ruleParams = sctx.ruleParams
117}
118
119func (s *singletonAdaptor) BuildParamsForTests() []BuildParams {
120 return s.buildParams
121}
122
123func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
124 return s.ruleParams
Colin Cross0875c522017-11-28 17:34:01 -0800125}
126
127type Singleton interface {
128 GenerateBuildActions(SingletonContext)
129}
130
131type singletonContextAdaptor struct {
132 blueprint.SingletonContext
Colin Cross4c83e5c2019-02-25 14:54:28 -0800133
134 buildParams []BuildParams
135 ruleParams map[blueprint.Rule]blueprint.RuleParams
Colin Cross0875c522017-11-28 17:34:01 -0800136}
137
Colin Cross4c83e5c2019-02-25 14:54:28 -0800138func (s *singletonContextAdaptor) Config() Config {
Colin Crossaabf6792017-11-29 00:27:14 -0800139 return s.SingletonContext.Config().(Config)
140}
141
Colin Cross4c83e5c2019-02-25 14:54:28 -0800142func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800143 return DeviceConfig{s.Config().deviceConfig}
144}
145
Colin Cross4c83e5c2019-02-25 14:54:28 -0800146func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) {
Colin Cross0875c522017-11-28 17:34:01 -0800147 s.SingletonContext.Variable(pctx.PackageContext, name, value)
148}
149
Colin Cross4c83e5c2019-02-25 14:54:28 -0800150func (s *singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule {
Ramy Medhat944839a2020-03-31 22:14:52 -0400151 if s.Config().UseRemoteBuild() {
152 if params.Pool == nil {
153 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
154 // jobs to the local parallelism value
155 params.Pool = localPool
156 } else if params.Pool == remotePool {
157 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
158 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
159 // parallelism.
160 params.Pool = nil
161 }
Colin Cross2e2dbc22019-09-25 13:31:46 -0700162 }
Colin Cross4c83e5c2019-02-25 14:54:28 -0800163 rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...)
164 if s.Config().captureBuild {
165 s.ruleParams[rule] = params
166 }
167 return rule
Colin Cross0875c522017-11-28 17:34:01 -0800168}
169
Colin Cross4c83e5c2019-02-25 14:54:28 -0800170func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) {
171 if s.Config().captureBuild {
172 s.buildParams = append(s.buildParams, params)
173 }
Colin Cross0875c522017-11-28 17:34:01 -0800174 bparams := convertBuildParams(params)
Jingwen Chence679d22020-09-23 04:30:02 +0000175 err := validateBuildParams(bparams)
176 if err != nil {
177 s.Errorf("%s: build parameter validation failed: %s", s.Name(), err.Error())
178 }
Colin Cross0875c522017-11-28 17:34:01 -0800179 s.SingletonContext.Build(pctx.PackageContext, bparams)
180
181}
182
Colin Crossc3d87d32020-06-04 13:25:17 -0700183func (s *singletonContextAdaptor) Phony(name string, deps ...Path) {
184 addPhony(s.Config(), name, deps...)
185}
186
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200187func (s *singletonContextAdaptor) SetOutDir(pctx PackageContext, value string) {
188 s.SingletonContext.SetOutDir(pctx.PackageContext, value)
Colin Cross0875c522017-11-28 17:34:01 -0800189}
190
Colin Cross4c83e5c2019-02-25 14:54:28 -0800191func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) {
Colin Cross0875c522017-11-28 17:34:01 -0800192 return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr)
193}
194
195// visitAdaptor wraps a visit function that takes an android.Module parameter into
196// a function that takes an blueprint.Module parameter and only calls the visit function if the
197// blueprint.Module is an android.Module.
198func visitAdaptor(visit func(Module)) func(blueprint.Module) {
199 return func(module blueprint.Module) {
200 if aModule, ok := module.(Module); ok {
201 visit(aModule)
202 }
203 }
204}
205
206// predAdaptor wraps a pred function that takes an android.Module parameter
207// into a function that takes an blueprint.Module parameter and only calls the visit function if the
208// blueprint.Module is an android.Module, otherwise returns false.
209func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool {
210 return func(module blueprint.Module) bool {
211 if aModule, ok := module.(Module); ok {
212 return pred(aModule)
213 } else {
214 return false
215 }
216 }
217}
218
Colin Cross4c83e5c2019-02-25 14:54:28 -0800219func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
Colin Cross2465c3d2018-09-28 10:19:18 -0700220 s.SingletonContext.VisitAllModules(visit)
221}
222
Colin Cross4c83e5c2019-02-25 14:54:28 -0800223func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800224 s.SingletonContext.VisitAllModules(visitAdaptor(visit))
225}
226
Colin Cross4c83e5c2019-02-25 14:54:28 -0800227func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800228 s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit))
229}
230
Mitch Phillips3a7a31b2019-10-16 15:00:12 -0700231func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) {
232 s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit))
233}
234
235func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) {
236 s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit))
237}
238
Colin Cross4c83e5c2019-02-25 14:54:28 -0800239func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800240 s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit))
241}
242
Colin Cross4c83e5c2019-02-25 14:54:28 -0800243func (s *singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800244 s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit))
245}
246
Colin Cross4c83e5c2019-02-25 14:54:28 -0800247func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) {
Colin Cross0875c522017-11-28 17:34:01 -0800248 s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit))
249}
250
Colin Cross4c83e5c2019-02-25 14:54:28 -0800251func (s *singletonContextAdaptor) PrimaryModule(module Module) Module {
Colin Cross0875c522017-11-28 17:34:01 -0800252 return s.SingletonContext.PrimaryModule(module).(Module)
253}
254
Colin Cross4c83e5c2019-02-25 14:54:28 -0800255func (s *singletonContextAdaptor) FinalModule(module Module) Module {
Colin Cross0875c522017-11-28 17:34:01 -0800256 return s.SingletonContext.FinalModule(module).(Module)
257}
Bob Badoureef4c1c2022-05-16 12:20:04 -0700258
259func (s *singletonContextAdaptor) ModuleVariantsFromName(referer Module, name string) []Module {
260 // get qualified module name for visibility enforcement
261 qualified := createQualifiedModuleName(s.ModuleName(referer), s.ModuleDir(referer))
262
263 modules := s.SingletonContext.ModuleVariantsFromName(referer, name)
264 result := make([]Module, 0, len(modules))
265 for _, m := range modules {
266 if module, ok := m.(Module); ok {
267 // enforce visibility
268 depName := s.ModuleName(module)
269 depDir := s.ModuleDir(module)
270 depQualified := qualifiedModuleName{depDir, depName}
271 // Targets are always visible to other targets in their own package.
272 if depQualified.pkg != qualified.pkg {
273 rule := effectiveVisibilityRules(s.Config(), depQualified)
274 if !rule.matches(qualified) {
Bob Badoura5ea2472022-05-20 16:37:26 -0700275 s.ModuleErrorf(referer, "module %q references %q which is not visible to this module\nYou may need to add %q to its visibility",
276 referer.Name(), depQualified, "//"+s.ModuleDir(referer))
Bob Badoureef4c1c2022-05-16 12:20:04 -0700277 continue
278 }
279 }
280 result = append(result, module)
281 }
282 }
283 return result
284}