blob: 3c0a24c8e39cc5bce8c8251ec7635a2dbdc401fd [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// Copyright 2014 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
Jamie Gennis1bc967e2014-05-27 16:34:41 -070015package blueprint
16
17import (
18 "fmt"
Colin Crossc5816202017-12-11 14:39:10 -080019
Colin Crossb519a7e2017-02-01 13:21:35 -080020 "github.com/google/blueprint/pathtools"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070021)
22
23type Singleton interface {
24 GenerateBuildActions(SingletonContext)
25}
26
27type SingletonContext interface {
Colin Cross7bcc2562019-05-20 14:51:55 -070028 // Config returns the config object that was passed to Context.PrepareBuildActions.
Jamie Gennis6eb4d242014-06-11 18:31:16 -070029 Config() interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070030
Colin Cross7bcc2562019-05-20 14:51:55 -070031 // Name returns the name of the current singleton passed to Context.RegisterSingletonType
Colin Cross9226d6c2019-02-25 18:07:44 -080032 Name() string
33
Colin Cross7bcc2562019-05-20 14:51:55 -070034 // ModuleName returns the name of the given Module. See BaseModuleContext.ModuleName for more information.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070035 ModuleName(module Module) string
Colin Cross7bcc2562019-05-20 14:51:55 -070036
37 // ModuleDir returns the directory of the given Module. See BaseModuleContext.ModuleDir for more information.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070038 ModuleDir(module Module) string
Colin Cross7bcc2562019-05-20 14:51:55 -070039
40 // ModuleSubDir returns the unique subdirectory name of the given Module. See ModuleContext.ModuleSubDir for
41 // more information.
Colin Cross8c602f72015-12-17 18:02:11 -080042 ModuleSubDir(module Module) string
Colin Cross7bcc2562019-05-20 14:51:55 -070043
44 // ModuleType returns the type of the given Module. See BaseModuleContext.ModuleType for more information.
Dan Willemsenc98e55b2016-07-25 15:51:50 -070045 ModuleType(module Module) string
Colin Cross7bcc2562019-05-20 14:51:55 -070046
47 // BlueprintFile returns the path of the Blueprint file that defined the given module.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070048 BlueprintFile(module Module) string
49
Colin Cross7bcc2562019-05-20 14:51:55 -070050 // ModuleErrorf reports an error at the line number of the module type in the module definition.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070051 ModuleErrorf(module Module, format string, args ...interface{})
Colin Cross7bcc2562019-05-20 14:51:55 -070052
53 // Errorf reports an error at the specified position of the module definition file.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070054 Errorf(format string, args ...interface{})
Colin Cross7bcc2562019-05-20 14:51:55 -070055
56 // Failed returns true if any errors have been reported. In most cases the singleton can continue with generating
57 // build rules after an error, allowing it to report additional errors in a single run, but in cases where the error
58 // has prevented the singleton from creating necessary data it can return early when Failed returns true.
Dan Willemsen265d92c2015-12-09 18:06:23 -080059 Failed() bool
Jamie Gennis1bc967e2014-05-27 16:34:41 -070060
Colin Cross7bcc2562019-05-20 14:51:55 -070061 // Variable creates a new ninja variable scoped to the singleton. It can be referenced by calls to Rule and Build
62 // in the same singleton.
Dan Willemsenaeffbf72015-11-25 15:29:32 -080063 Variable(pctx PackageContext, name, value string)
Colin Cross7bcc2562019-05-20 14:51:55 -070064
65 // Rule creates a new ninja rule scoped to the singleton. It can be referenced by calls to Build in the same
66 // singleton.
Dan Willemsenaeffbf72015-11-25 15:29:32 -080067 Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule
Colin Cross7bcc2562019-05-20 14:51:55 -070068
69 // Build creates a new ninja build statement.
Dan Willemsenaeffbf72015-11-25 15:29:32 -080070 Build(pctx PackageContext, params BuildParams)
Colin Cross7bcc2562019-05-20 14:51:55 -070071
72 // RequireNinjaVersion sets the generated ninja manifest to require at least the specified version of ninja.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070073 RequireNinjaVersion(major, minor, micro int)
74
Colin Crossa2599452015-11-18 16:01:01 -080075 // SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable
Jamie Gennis1bc967e2014-05-27 16:34:41 -070076 // that controls where Ninja stores its build log files. This value can be
Colin Crossa2599452015-11-18 16:01:01 -080077 // set at most one time for a single build, later calls are ignored.
Dan Willemsenaeffbf72015-11-25 15:29:32 -080078 SetNinjaBuildDir(pctx PackageContext, value string)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070079
Dan Willemsenab223a52018-07-05 21:56:59 -070080 // AddSubninja adds a ninja file to include with subninja. This should likely
81 // only ever be used inside bootstrap to handle glob rules.
82 AddSubninja(file string)
83
Dan Willemsen4bb62762016-01-14 15:42:54 -080084 // Eval takes a string with embedded ninja variables, and returns a string
85 // with all of the variables recursively expanded. Any variables references
86 // are expanded in the scope of the PackageContext.
87 Eval(pctx PackageContext, ninjaStr string) (string, error)
88
Colin Cross7bcc2562019-05-20 14:51:55 -070089 // VisitAllModules calls visit for each defined variant of each module in an unspecified order.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070090 VisitAllModules(visit func(Module))
Colin Cross7bcc2562019-05-20 14:51:55 -070091
92 // VisitAllModules calls pred for each defined variant of each module in an unspecified order, and if pred returns
93 // true calls visit.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070094 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Colin Cross7bcc2562019-05-20 14:51:55 -070095
Ulya Trafimovich811381a2019-09-20 12:01:01 +010096 // VisitDirectDeps calls visit for each direct dependency of the Module. If there are
97 // multiple direct dependencies on the same module visit will be called multiple times on
98 // that module and OtherModuleDependencyTag will return a different tag for each.
99 //
100 // The Module passed to the visit function should not be retained outside of the visit
101 // function, it may be invalidated by future mutators.
102 VisitDirectDeps(module Module, visit func(Module))
103
104 // VisitDirectDepsIf calls pred for each direct dependency of the Module, and if pred
105 // returns true calls visit. If there are multiple direct dependencies on the same module
106 // pred and visit will be called multiple times on that module and OtherModuleDependencyTag
107 // will return a different tag for each.
108 //
109 // The Module passed to the visit function should not be retained outside of the visit
110 // function, it may be invalidated by future mutators.
111 VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module))
112
Colin Cross7bcc2562019-05-20 14:51:55 -0700113 // VisitDepsDepthFirst calls visit for each transitive dependency, traversing the dependency tree in depth first
114 // order. visit will only be called once for any given module, even if there are multiple paths through the
115 // dependency tree to the module or multiple direct dependencies with different tags.
Jamie Gennise98b8a92014-06-17 10:24:24 -0700116 VisitDepsDepthFirst(module Module, visit func(Module))
Colin Cross7bcc2562019-05-20 14:51:55 -0700117
118 // VisitDepsDepthFirst calls pred for each transitive dependency, and if pred returns true calls visit, traversing
119 // the dependency tree in depth first order. visit will only be called once for any given module, even if there are
120 // multiple paths through the dependency tree to the module or multiple direct dependencies with different tags.
Jamie Gennise98b8a92014-06-17 10:24:24 -0700121 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
122 visit func(Module))
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700123
Colin Cross7bcc2562019-05-20 14:51:55 -0700124 // VisitAllModuleVariants calls visit for each variant of the given module.
Colin Cross24ad5872015-11-17 16:22:29 -0800125 VisitAllModuleVariants(module Module, visit func(Module))
126
Colin Cross7bcc2562019-05-20 14:51:55 -0700127 // PrimaryModule returns the first variant of the given module. This can be used to perform
128 // // singleton actions that are only done once for all variants of a module.
Colin Cross24ad5872015-11-17 16:22:29 -0800129 PrimaryModule(module Module) Module
Colin Cross7bcc2562019-05-20 14:51:55 -0700130
131 // FinalModule returns the last variant of the given module. This can be used to perform
132 // singleton actions that are only done once for all variants of a module.
Colin Cross24ad5872015-11-17 16:22:29 -0800133 FinalModule(module Module) Module
134
Colin Cross7bcc2562019-05-20 14:51:55 -0700135 // AddNinjaFileDeps adds dependencies on the specified files to the rule that creates the ninja manifest. The
136 // primary builder will be rerun whenever the specified files are modified.
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700137 AddNinjaFileDeps(deps ...string)
Colin Cross127d2ea2016-11-01 11:10:51 -0700138
Dan Willemsenb6c90232018-02-23 14:49:45 -0800139 // GlobWithDeps returns a list of files and directories that match the
140 // specified pattern but do not match any of the patterns in excludes.
141 // Any directories will have a '/' suffix. It also adds efficient
142 // dependencies to rerun the primary builder whenever a file matching
143 // the pattern as added or removed, without rerunning if a file that
144 // does not match the pattern is added to a searched directory.
Colin Cross127d2ea2016-11-01 11:10:51 -0700145 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossb519a7e2017-02-01 13:21:35 -0800146
Colin Cross7bcc2562019-05-20 14:51:55 -0700147 // Fs returns a pathtools.Filesystem that can be used to interact with files. Using the Filesystem interface allows
148 // the singleton to be used in build system tests that run against a mock filesystem.
Colin Crossb519a7e2017-02-01 13:21:35 -0800149 Fs() pathtools.FileSystem
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700150}
151
152var _ SingletonContext = (*singletonContext)(nil)
153
154type singletonContext struct {
Colin Cross9226d6c2019-02-25 18:07:44 -0800155 name string
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700156 context *Context
Jamie Gennis6eb4d242014-06-11 18:31:16 -0700157 config interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700158 scope *localScope
Dan Willemsen4bb62762016-01-14 15:42:54 -0800159 globals *liveTracker
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700160
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700161 ninjaFileDeps []string
162 errs []error
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700163
164 actionDefs localBuildActions
165}
166
Jamie Gennis6eb4d242014-06-11 18:31:16 -0700167func (s *singletonContext) Config() interface{} {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700168 return s.config
169}
170
Colin Cross9226d6c2019-02-25 18:07:44 -0800171func (s *singletonContext) Name() string {
172 return s.name
173}
174
Colin Crossbbfa51a2014-12-17 16:12:41 -0800175func (s *singletonContext) ModuleName(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700176 return s.context.ModuleName(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700177}
178
Colin Crossbbfa51a2014-12-17 16:12:41 -0800179func (s *singletonContext) ModuleDir(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700180 return s.context.ModuleDir(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700181}
182
Colin Cross8c602f72015-12-17 18:02:11 -0800183func (s *singletonContext) ModuleSubDir(logicModule Module) string {
184 return s.context.ModuleSubDir(logicModule)
185}
186
Dan Willemsenc98e55b2016-07-25 15:51:50 -0700187func (s *singletonContext) ModuleType(logicModule Module) string {
188 return s.context.ModuleType(logicModule)
189}
190
Colin Crossbbfa51a2014-12-17 16:12:41 -0800191func (s *singletonContext) BlueprintFile(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700192 return s.context.BlueprintFile(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700193}
194
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800195func (s *singletonContext) error(err error) {
196 if err != nil {
197 s.errs = append(s.errs, err)
198 }
199}
200
Colin Crossbbfa51a2014-12-17 16:12:41 -0800201func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700202 args ...interface{}) {
203
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800204 s.error(s.context.ModuleErrorf(logicModule, format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700205}
206
207func (s *singletonContext) Errorf(format string, args ...interface{}) {
208 // TODO: Make this not result in the error being printed as "internal error"
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800209 s.error(fmt.Errorf(format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700210}
211
Dan Willemsen265d92c2015-12-09 18:06:23 -0800212func (s *singletonContext) Failed() bool {
213 return len(s.errs) > 0
214}
215
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800216func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700217 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700218
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700219 v, err := s.scope.AddLocalVariable(name, value)
220 if err != nil {
221 panic(err)
222 }
223
224 s.actionDefs.variables = append(s.actionDefs.variables, v)
225}
226
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800227func (s *singletonContext) Rule(pctx PackageContext, name string,
Jamie Gennis2fb20952014-10-03 02:49:58 -0700228 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700229
Jamie Gennis2fb20952014-10-03 02:49:58 -0700230 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700231
Jamie Genniscbc6f862014-06-05 20:00:22 -0700232 r, err := s.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700233 if err != nil {
234 panic(err)
235 }
236
237 s.actionDefs.rules = append(s.actionDefs.rules, r)
238
239 return r
240}
241
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800242func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700243 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700244
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700245 def, err := parseBuildParams(s.scope, &params)
246 if err != nil {
247 panic(err)
248 }
249
250 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
251}
252
Dan Willemsen4bb62762016-01-14 15:42:54 -0800253func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
254 s.scope.ReparentTo(pctx)
255
256 ninjaStr, err := parseNinjaString(s.scope, str)
257 if err != nil {
258 return "", err
259 }
260
261 err = s.globals.addNinjaStringDeps(ninjaStr)
262 if err != nil {
263 return "", err
264 }
265
266 return ninjaStr.Eval(s.globals.variables)
267}
268
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700269func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
270 s.context.requireNinjaVersion(major, minor, micro)
271}
272
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800273func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700274 s.scope.ReparentTo(pctx)
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700275
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700276 ninjaValue, err := parseNinjaString(s.scope, value)
277 if err != nil {
278 panic(err)
279 }
280
Colin Crossa2599452015-11-18 16:01:01 -0800281 s.context.setNinjaBuildDir(ninjaValue)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700282}
283
Dan Willemsenab223a52018-07-05 21:56:59 -0700284func (s *singletonContext) AddSubninja(file string) {
285 s.context.subninjas = append(s.context.subninjas, file)
286}
287
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700288func (s *singletonContext) VisitAllModules(visit func(Module)) {
Colin Cross7f90d172018-09-19 13:37:29 -0700289 var visitingModule Module
290 defer func() {
291 if r := recover(); r != nil {
292 panic(newPanicErrorf(r, "VisitAllModules(%s) for module %s",
Colin Cross818af3b2019-03-25 15:04:09 -0700293 funcName(visit), s.context.moduleInfo[visitingModule]))
Colin Cross7f90d172018-09-19 13:37:29 -0700294 }
295 }()
296
297 s.context.VisitAllModules(func(m Module) {
298 visitingModule = m
299 visit(m)
300 })
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700301}
302
303func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
304 visit func(Module)) {
305
Colin Cross4572edd2015-05-13 14:36:24 -0700306 s.context.VisitAllModulesIf(pred, visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700307}
Jamie Gennise98b8a92014-06-17 10:24:24 -0700308
Ulya Trafimovich811381a2019-09-20 12:01:01 +0100309func (s *singletonContext) VisitDirectDeps(module Module, visit func(Module)) {
310 s.context.VisitDirectDeps(module, visit)
311}
312
313func (s *singletonContext) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) {
314 s.context.VisitDirectDepsIf(module, pred, visit)
315}
316
Jamie Gennise98b8a92014-06-17 10:24:24 -0700317func (s *singletonContext) VisitDepsDepthFirst(module Module,
318 visit func(Module)) {
319
Colin Cross4572edd2015-05-13 14:36:24 -0700320 s.context.VisitDepsDepthFirst(module, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700321}
322
323func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
324 pred func(Module) bool, visit func(Module)) {
325
Colin Cross4572edd2015-05-13 14:36:24 -0700326 s.context.VisitDepsDepthFirstIf(module, pred, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700327}
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700328
Colin Cross24ad5872015-11-17 16:22:29 -0800329func (s *singletonContext) PrimaryModule(module Module) Module {
330 return s.context.PrimaryModule(module)
331}
332
333func (s *singletonContext) FinalModule(module Module) Module {
334 return s.context.FinalModule(module)
335}
336
337func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
338 s.context.VisitAllModuleVariants(module, visit)
339}
340
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700341func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
342 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
343}
Colin Cross127d2ea2016-11-01 11:10:51 -0700344
345func (s *singletonContext) GlobWithDeps(pattern string,
346 excludes []string) ([]string, error) {
347 return s.context.glob(pattern, excludes)
348}
Colin Crossb519a7e2017-02-01 13:21:35 -0800349
350func (s *singletonContext) Fs() pathtools.FileSystem {
351 return s.context.fs
352}