blob: cc58e06c760e3855958cd875cab52dd4b4d28df1 [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
96 // VisitDepsDepthFirst calls visit for each transitive dependency, traversing the dependency tree in depth first
97 // order. visit will only be called once for any given module, even if there are multiple paths through the
98 // dependency tree to the module or multiple direct dependencies with different tags.
Jamie Gennise98b8a92014-06-17 10:24:24 -070099 VisitDepsDepthFirst(module Module, visit func(Module))
Colin Cross7bcc2562019-05-20 14:51:55 -0700100
101 // VisitDepsDepthFirst calls pred for each transitive dependency, and if pred returns true calls visit, traversing
102 // the dependency tree in depth first order. visit will only be called once for any given module, even if there are
103 // multiple paths through the dependency tree to the module or multiple direct dependencies with different tags.
Jamie Gennise98b8a92014-06-17 10:24:24 -0700104 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
105 visit func(Module))
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700106
Colin Cross7bcc2562019-05-20 14:51:55 -0700107 // VisitAllModuleVariants calls visit for each variant of the given module.
Colin Cross24ad5872015-11-17 16:22:29 -0800108 VisitAllModuleVariants(module Module, visit func(Module))
109
Colin Cross7bcc2562019-05-20 14:51:55 -0700110 // PrimaryModule returns the first variant of the given module. This can be used to perform
111 // // singleton actions that are only done once for all variants of a module.
Colin Cross24ad5872015-11-17 16:22:29 -0800112 PrimaryModule(module Module) Module
Colin Cross7bcc2562019-05-20 14:51:55 -0700113
114 // FinalModule returns the last variant of the given module. This can be used to perform
115 // singleton actions that are only done once for all variants of a module.
Colin Cross24ad5872015-11-17 16:22:29 -0800116 FinalModule(module Module) Module
117
Colin Cross7bcc2562019-05-20 14:51:55 -0700118 // AddNinjaFileDeps adds dependencies on the specified files to the rule that creates the ninja manifest. The
119 // primary builder will be rerun whenever the specified files are modified.
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700120 AddNinjaFileDeps(deps ...string)
Colin Cross127d2ea2016-11-01 11:10:51 -0700121
Dan Willemsenb6c90232018-02-23 14:49:45 -0800122 // GlobWithDeps returns a list of files and directories that match the
123 // specified pattern but do not match any of the patterns in excludes.
124 // Any directories will have a '/' suffix. It also adds efficient
125 // dependencies to rerun the primary builder whenever a file matching
126 // the pattern as added or removed, without rerunning if a file that
127 // does not match the pattern is added to a searched directory.
Colin Cross127d2ea2016-11-01 11:10:51 -0700128 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossb519a7e2017-02-01 13:21:35 -0800129
Colin Cross7bcc2562019-05-20 14:51:55 -0700130 // Fs returns a pathtools.Filesystem that can be used to interact with files. Using the Filesystem interface allows
131 // the singleton to be used in build system tests that run against a mock filesystem.
Colin Crossb519a7e2017-02-01 13:21:35 -0800132 Fs() pathtools.FileSystem
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700133}
134
135var _ SingletonContext = (*singletonContext)(nil)
136
137type singletonContext struct {
Colin Cross9226d6c2019-02-25 18:07:44 -0800138 name string
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700139 context *Context
Jamie Gennis6eb4d242014-06-11 18:31:16 -0700140 config interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700141 scope *localScope
Dan Willemsen4bb62762016-01-14 15:42:54 -0800142 globals *liveTracker
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700143
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700144 ninjaFileDeps []string
145 errs []error
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700146
147 actionDefs localBuildActions
148}
149
Jamie Gennis6eb4d242014-06-11 18:31:16 -0700150func (s *singletonContext) Config() interface{} {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700151 return s.config
152}
153
Colin Cross9226d6c2019-02-25 18:07:44 -0800154func (s *singletonContext) Name() string {
155 return s.name
156}
157
Colin Crossbbfa51a2014-12-17 16:12:41 -0800158func (s *singletonContext) ModuleName(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700159 return s.context.ModuleName(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700160}
161
Colin Crossbbfa51a2014-12-17 16:12:41 -0800162func (s *singletonContext) ModuleDir(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700163 return s.context.ModuleDir(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700164}
165
Colin Cross8c602f72015-12-17 18:02:11 -0800166func (s *singletonContext) ModuleSubDir(logicModule Module) string {
167 return s.context.ModuleSubDir(logicModule)
168}
169
Dan Willemsenc98e55b2016-07-25 15:51:50 -0700170func (s *singletonContext) ModuleType(logicModule Module) string {
171 return s.context.ModuleType(logicModule)
172}
173
Colin Crossbbfa51a2014-12-17 16:12:41 -0800174func (s *singletonContext) BlueprintFile(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700175 return s.context.BlueprintFile(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700176}
177
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800178func (s *singletonContext) error(err error) {
179 if err != nil {
180 s.errs = append(s.errs, err)
181 }
182}
183
Colin Crossbbfa51a2014-12-17 16:12:41 -0800184func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700185 args ...interface{}) {
186
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800187 s.error(s.context.ModuleErrorf(logicModule, format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700188}
189
190func (s *singletonContext) Errorf(format string, args ...interface{}) {
191 // TODO: Make this not result in the error being printed as "internal error"
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800192 s.error(fmt.Errorf(format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700193}
194
Dan Willemsen265d92c2015-12-09 18:06:23 -0800195func (s *singletonContext) Failed() bool {
196 return len(s.errs) > 0
197}
198
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800199func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700200 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700201
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700202 v, err := s.scope.AddLocalVariable(name, value)
203 if err != nil {
204 panic(err)
205 }
206
207 s.actionDefs.variables = append(s.actionDefs.variables, v)
208}
209
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800210func (s *singletonContext) Rule(pctx PackageContext, name string,
Jamie Gennis2fb20952014-10-03 02:49:58 -0700211 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700212
Jamie Gennis2fb20952014-10-03 02:49:58 -0700213 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700214
Jamie Genniscbc6f862014-06-05 20:00:22 -0700215 r, err := s.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700216 if err != nil {
217 panic(err)
218 }
219
220 s.actionDefs.rules = append(s.actionDefs.rules, r)
221
222 return r
223}
224
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800225func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700226 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700227
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700228 def, err := parseBuildParams(s.scope, &params)
229 if err != nil {
230 panic(err)
231 }
232
233 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
234}
235
Dan Willemsen4bb62762016-01-14 15:42:54 -0800236func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
237 s.scope.ReparentTo(pctx)
238
239 ninjaStr, err := parseNinjaString(s.scope, str)
240 if err != nil {
241 return "", err
242 }
243
244 err = s.globals.addNinjaStringDeps(ninjaStr)
245 if err != nil {
246 return "", err
247 }
248
249 return ninjaStr.Eval(s.globals.variables)
250}
251
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700252func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
253 s.context.requireNinjaVersion(major, minor, micro)
254}
255
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800256func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700257 s.scope.ReparentTo(pctx)
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700258
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700259 ninjaValue, err := parseNinjaString(s.scope, value)
260 if err != nil {
261 panic(err)
262 }
263
Colin Crossa2599452015-11-18 16:01:01 -0800264 s.context.setNinjaBuildDir(ninjaValue)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700265}
266
Dan Willemsenab223a52018-07-05 21:56:59 -0700267func (s *singletonContext) AddSubninja(file string) {
268 s.context.subninjas = append(s.context.subninjas, file)
269}
270
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700271func (s *singletonContext) VisitAllModules(visit func(Module)) {
Colin Cross7f90d172018-09-19 13:37:29 -0700272 var visitingModule Module
273 defer func() {
274 if r := recover(); r != nil {
275 panic(newPanicErrorf(r, "VisitAllModules(%s) for module %s",
Colin Cross818af3b2019-03-25 15:04:09 -0700276 funcName(visit), s.context.moduleInfo[visitingModule]))
Colin Cross7f90d172018-09-19 13:37:29 -0700277 }
278 }()
279
280 s.context.VisitAllModules(func(m Module) {
281 visitingModule = m
282 visit(m)
283 })
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700284}
285
286func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
287 visit func(Module)) {
288
Colin Cross4572edd2015-05-13 14:36:24 -0700289 s.context.VisitAllModulesIf(pred, visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700290}
Jamie Gennise98b8a92014-06-17 10:24:24 -0700291
292func (s *singletonContext) VisitDepsDepthFirst(module Module,
293 visit func(Module)) {
294
Colin Cross4572edd2015-05-13 14:36:24 -0700295 s.context.VisitDepsDepthFirst(module, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700296}
297
298func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
299 pred func(Module) bool, visit func(Module)) {
300
Colin Cross4572edd2015-05-13 14:36:24 -0700301 s.context.VisitDepsDepthFirstIf(module, pred, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700302}
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700303
Colin Cross24ad5872015-11-17 16:22:29 -0800304func (s *singletonContext) PrimaryModule(module Module) Module {
305 return s.context.PrimaryModule(module)
306}
307
308func (s *singletonContext) FinalModule(module Module) Module {
309 return s.context.FinalModule(module)
310}
311
312func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
313 s.context.VisitAllModuleVariants(module, visit)
314}
315
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700316func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
317 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
318}
Colin Cross127d2ea2016-11-01 11:10:51 -0700319
320func (s *singletonContext) GlobWithDeps(pattern string,
321 excludes []string) ([]string, error) {
322 return s.context.glob(pattern, excludes)
323}
Colin Crossb519a7e2017-02-01 13:21:35 -0800324
325func (s *singletonContext) Fs() pathtools.FileSystem {
326 return s.context.fs
327}