blob: 1ba3dc865003da6b881862a6c97c3ba6d2d8d9df [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"
19 "path/filepath"
Jamie Gennis6a40c192014-07-02 16:40:31 -070020 "text/scanner"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070021)
22
Jamie Gennisb9e87f62014-09-24 20:28:11 -070023// A Module handles generating all of the Ninja build actions needed to build a
Colin Crossc9028482014-12-18 16:28:54 -080024// single module based on properties defined in a Blueprints file. Module
25// objects are initially created during the parse phase of a Context using one
26// of the registered module types (and the associated ModuleFactory function).
27// The Module's properties struct is automatically filled in with the property
28// values specified in the Blueprints file (see Context.RegisterModuleType for more
Jamie Gennisb9e87f62014-09-24 20:28:11 -070029// information on this).
30//
Colin Crossc9028482014-12-18 16:28:54 -080031// A Module can be split into multiple Modules by a Mutator. All existing
32// properties set on the module will be duplicated to the new Module, and then
33// modified as necessary by the Mutator.
34//
Jamie Gennisb9e87f62014-09-24 20:28:11 -070035// The Module implementation can access the build configuration as well as any
36// modules on which on which it depends (as defined by the "deps" property
37// specified in the Blueprints file or dynamically added by implementing the
38// DynamicDependerModule interface) using the ModuleContext passed to
39// GenerateBuildActions. This ModuleContext is also used to create Ninja build
40// actions and to report errors to the user.
41//
42// In addition to implementing the GenerateBuildActions method, a Module should
43// implement methods that provide dependant modules and singletons information
44// they need to generate their build actions. These methods will only be called
45// after GenerateBuildActions is called because the Context calls
46// GenerateBuildActions in dependency-order (and singletons are invoked after
47// all the Modules). The set of methods a Module supports will determine how
48// dependant Modules interact with it.
49//
50// For example, consider a Module that is responsible for generating a library
51// that other modules can link against. The library Module might implement the
52// following interface:
53//
54// type LibraryProducer interface {
55// LibraryFileName() string
56// }
57//
58// func IsLibraryProducer(module blueprint.Module) {
59// _, ok := module.(LibraryProducer)
60// return ok
61// }
62//
63// A binary-producing Module that depends on the library Module could then do:
64//
65// func (m *myBinaryModule) GenerateBuildActions(ctx blueprint.ModuleContext) {
66// ...
67// var libraryFiles []string
68// ctx.VisitDepsDepthFirstIf(IsLibraryProducer,
69// func(module blueprint.Module) {
70// libProducer := module.(LibraryProducer)
71// libraryFiles = append(libraryFiles, libProducer.LibraryFileName())
72// })
73// ...
74// }
75//
76// to build the list of library file names that should be included in its link
77// command.
Colin Cross691a60d2015-01-07 18:08:56 -080078//
79// GenerateBuildActions may be called from multiple threads. It is guaranteed to
80// be called after it has finished being called on all dependencies and on all
81// variants of that appear earlier in the ModuleContext.VisitAllModuleVariants list.
82// Any accesses to global variables or to Module objects that are not dependencies
83// or variants of the current Module must be synchronized by the implementation of
84// GenerateBuildActions.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070085type Module interface {
Jamie Gennisb9e87f62014-09-24 20:28:11 -070086 // GenerateBuildActions is called by the Context that created the Module
87 // during its generate phase. This call should generate all Ninja build
88 // actions (rules, pools, and build statements) needed to build the module.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070089 GenerateBuildActions(ModuleContext)
90}
91
Jamie Gennisb9e87f62014-09-24 20:28:11 -070092// A DynamicDependerModule is a Module that may add dependencies that do not
93// appear in its "deps" property. Any Module that implements this interface
94// will have its DynamicDependencies method called by the Context that created
95// it during generate phase.
96type DynamicDependerModule interface {
97 Module
98
99 // DynamicDependencies is called by the Context that created the
100 // DynamicDependerModule during its generate phase. This call should return
101 // the list of module names that the DynamicDependerModule depends on
102 // dynamically. Module names that already appear in the "deps" property may
103 // but do not need to be included in the returned list.
104 DynamicDependencies(DynamicDependerModuleContext) []string
105}
106
Colin Crossbe1a9a12014-12-18 11:05:45 -0800107type BaseModuleContext interface {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700108 ModuleName() string
109 ModuleDir() string
Jamie Gennis6eb4d242014-06-11 18:31:16 -0700110 Config() interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700111
David Allison701fbad2014-10-29 14:51:13 -0700112 ContainsProperty(name string) bool
Jamie Gennis6a40c192014-07-02 16:40:31 -0700113 Errorf(pos scanner.Position, fmt string, args ...interface{})
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700114 ModuleErrorf(fmt string, args ...interface{})
115 PropertyErrorf(property, fmt string, args ...interface{})
Jamie Gennis6a40c192014-07-02 16:40:31 -0700116 Failed() bool
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700117}
118
Colin Crossbe1a9a12014-12-18 11:05:45 -0800119type DynamicDependerModuleContext interface {
120 BaseModuleContext
Colin Cross65569e42015-03-10 20:08:19 -0700121
122 AddVariantDependencies([]Variant, ...string)
Colin Crossbe1a9a12014-12-18 11:05:45 -0800123}
124
Colin Cross1455a0f2014-12-17 13:23:56 -0800125type ModuleContext interface {
Colin Crossbe1a9a12014-12-18 11:05:45 -0800126 BaseModuleContext
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700127
128 OtherModuleName(m Module) string
129 OtherModuleErrorf(m Module, fmt string, args ...interface{})
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700130
Colin Crossc7ffa302015-02-10 11:24:52 -0800131 VisitDirectDeps(visit func(Module))
132 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800133 VisitDepsDepthFirst(visit func(Module))
134 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800135
Colin Crossc9028482014-12-18 16:28:54 -0800136 ModuleSubDir() string
137
Jamie Gennis2fb20952014-10-03 02:49:58 -0700138 Variable(pctx *PackageContext, name, value string)
139 Rule(pctx *PackageContext, name string, params RuleParams, argNames ...string) Rule
140 Build(pctx *PackageContext, params BuildParams)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700141
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700142 AddNinjaFileDeps(deps ...string)
Colin Crossc9028482014-12-18 16:28:54 -0800143
144 PrimaryModule() Module
Colin Cross80ad04d2015-01-06 16:19:59 -0800145 FinalModule() Module
146 VisitAllModuleVariants(visit func(Module))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700147}
148
Colin Crossbe1a9a12014-12-18 11:05:45 -0800149var _ BaseModuleContext = (*baseModuleContext)(nil)
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700150
Colin Crossbe1a9a12014-12-18 11:05:45 -0800151type baseModuleContext struct {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700152 context *Context
153 config interface{}
Colin Crossed342d92015-03-11 00:57:25 -0700154 module *moduleInfo
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700155 errs []error
156}
157
Colin Crossbe1a9a12014-12-18 11:05:45 -0800158func (d *baseModuleContext) ModuleName() string {
Colin Crossed342d92015-03-11 00:57:25 -0700159 return d.module.properties.Name
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700160}
161
Colin Crossbe1a9a12014-12-18 11:05:45 -0800162func (d *baseModuleContext) ContainsProperty(name string) bool {
Colin Crossed342d92015-03-11 00:57:25 -0700163 _, ok := d.module.propertyPos[name]
David Allison701fbad2014-10-29 14:51:13 -0700164 return ok
165}
166
Colin Crossbe1a9a12014-12-18 11:05:45 -0800167func (d *baseModuleContext) ModuleDir() string {
Colin Crossed342d92015-03-11 00:57:25 -0700168 return filepath.Dir(d.module.relBlueprintsFile)
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700169}
170
Colin Crossbe1a9a12014-12-18 11:05:45 -0800171func (d *baseModuleContext) Config() interface{} {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700172 return d.config
173}
174
Colin Crossbe1a9a12014-12-18 11:05:45 -0800175func (d *baseModuleContext) Errorf(pos scanner.Position,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700176 format string, args ...interface{}) {
177
178 d.errs = append(d.errs, &Error{
179 Err: fmt.Errorf(format, args...),
180 Pos: pos,
181 })
182}
183
Colin Crossbe1a9a12014-12-18 11:05:45 -0800184func (d *baseModuleContext) ModuleErrorf(format string,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700185 args ...interface{}) {
186
187 d.errs = append(d.errs, &Error{
188 Err: fmt.Errorf(format, args...),
Colin Crossed342d92015-03-11 00:57:25 -0700189 Pos: d.module.pos,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700190 })
191}
192
Colin Crossbe1a9a12014-12-18 11:05:45 -0800193func (d *baseModuleContext) PropertyErrorf(property, format string,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700194 args ...interface{}) {
195
Colin Crossed342d92015-03-11 00:57:25 -0700196 pos, ok := d.module.propertyPos[property]
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700197 if !ok {
198 panic(fmt.Errorf("property %q was not set for this module", property))
199 }
200
201 d.errs = append(d.errs, &Error{
202 Err: fmt.Errorf(format, args...),
203 Pos: pos,
204 })
205}
206
Colin Crossbe1a9a12014-12-18 11:05:45 -0800207func (d *baseModuleContext) Failed() bool {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700208 return len(d.errs) > 0
209}
210
Colin Cross1455a0f2014-12-17 13:23:56 -0800211var _ ModuleContext = (*moduleContext)(nil)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700212
Colin Cross1455a0f2014-12-17 13:23:56 -0800213type moduleContext struct {
Colin Crossbe1a9a12014-12-18 11:05:45 -0800214 baseModuleContext
Colin Cross1455a0f2014-12-17 13:23:56 -0800215 scope *localScope
216 ninjaFileDeps []string
217 actionDefs localBuildActions
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700218}
219
Colin Crossed342d92015-03-11 00:57:25 -0700220func (m *moduleContext) OtherModuleName(logicModule Module) string {
221 module := m.context.moduleInfo[logicModule]
222 return module.properties.Name
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700223}
224
Colin Crossed342d92015-03-11 00:57:25 -0700225func (m *moduleContext) OtherModuleErrorf(logicModule Module, format string,
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700226 args ...interface{}) {
227
Colin Crossed342d92015-03-11 00:57:25 -0700228 module := m.context.moduleInfo[logicModule]
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700229 m.errs = append(m.errs, &Error{
230 Err: fmt.Errorf(format, args...),
Colin Crossed342d92015-03-11 00:57:25 -0700231 Pos: module.pos,
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700232 })
233}
234
Colin Crossc7ffa302015-02-10 11:24:52 -0800235func (m *moduleContext) VisitDirectDeps(visit func(Module)) {
236 m.context.visitDirectDeps(m.module, visit)
237}
238
239func (m *moduleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
240 m.context.visitDirectDepsIf(m.module, pred, visit)
241}
242
Colin Cross1455a0f2014-12-17 13:23:56 -0800243func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) {
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800244 m.context.visitDepsDepthFirst(m.module, visit)
245}
246
Colin Cross1455a0f2014-12-17 13:23:56 -0800247func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool,
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800248 visit func(Module)) {
249
250 m.context.visitDepsDepthFirstIf(m.module, pred, visit)
251}
252
Colin Crossc9028482014-12-18 16:28:54 -0800253func (m *moduleContext) ModuleSubDir() string {
Colin Crosse7daa222015-03-11 14:35:41 -0700254 return m.module.variantName
Colin Crossc9028482014-12-18 16:28:54 -0800255}
256
Jamie Gennis2fb20952014-10-03 02:49:58 -0700257func (m *moduleContext) Variable(pctx *PackageContext, name, value string) {
258 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700259
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700260 v, err := m.scope.AddLocalVariable(name, value)
261 if err != nil {
262 panic(err)
263 }
264
265 m.actionDefs.variables = append(m.actionDefs.variables, v)
266}
267
Jamie Gennis2fb20952014-10-03 02:49:58 -0700268func (m *moduleContext) Rule(pctx *PackageContext, name string,
269 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700270
Jamie Gennis2fb20952014-10-03 02:49:58 -0700271 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700272
Jamie Genniscbc6f862014-06-05 20:00:22 -0700273 r, err := m.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700274 if err != nil {
275 panic(err)
276 }
277
278 m.actionDefs.rules = append(m.actionDefs.rules, r)
279
280 return r
281}
282
Jamie Gennis2fb20952014-10-03 02:49:58 -0700283func (m *moduleContext) Build(pctx *PackageContext, params BuildParams) {
284 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700285
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700286 def, err := parseBuildParams(m.scope, &params)
287 if err != nil {
288 panic(err)
289 }
290
291 m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def)
292}
293
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700294func (m *moduleContext) AddNinjaFileDeps(deps ...string) {
295 m.ninjaFileDeps = append(m.ninjaFileDeps, deps...)
296}
Colin Crossc9028482014-12-18 16:28:54 -0800297
298func (m *moduleContext) PrimaryModule() Module {
Colin Cross80ad04d2015-01-06 16:19:59 -0800299 return m.module.group.modules[0].logicModule
300}
301
302func (m *moduleContext) FinalModule() Module {
303 return m.module.group.modules[len(m.module.group.modules)-1].logicModule
304}
305
306func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) {
307 for _, module := range m.module.group.modules {
308 visit(module.logicModule)
309 }
Colin Crossc9028482014-12-18 16:28:54 -0800310}
311
312//
Colin Cross65569e42015-03-10 20:08:19 -0700313// DynamicDependerModuleContext
314//
315
316type dynamicDependerModuleContext struct {
317 baseModuleContext
318
319 module *moduleInfo
320}
321
322func (mctx *dynamicDependerModuleContext) AddVariantDependencies(variant []Variant, deps ...string) {
323 for _, dep := range deps {
324 errs := mctx.context.addVariantDependency(mctx.module, variant, dep)
325 if len(errs) > 0 {
326 mctx.errs = append(mctx.errs, errs...)
327 }
328 }
329}
330
331//
Colin Crossc9028482014-12-18 16:28:54 -0800332// MutatorContext
333//
334
335type mutatorContext struct {
336 baseModuleContext
Colin Crossc9028482014-12-18 16:28:54 -0800337 name string
338 dependenciesModified bool
339}
340
341type baseMutatorContext interface {
342 BaseModuleContext
343
344 Module() Module
345}
346
Colin Cross65569e42015-03-10 20:08:19 -0700347type EarlyMutatorContext interface {
348 baseMutatorContext
349
350 CreateVariants(...string) []Module
351 CreateLocalVariants(...string) []Module
352}
353
Colin Crossc9028482014-12-18 16:28:54 -0800354type TopDownMutatorContext interface {
355 baseMutatorContext
356
357 VisitDirectDeps(visit func(Module))
358 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
359 VisitDepsDepthFirst(visit func(Module))
360 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
361}
362
363type BottomUpMutatorContext interface {
364 baseMutatorContext
365
366 AddDependency(module Module, name string)
367 CreateVariants(...string) []Module
368 SetDependencyVariant(string)
369}
370
371// A Mutator function is called for each Module, and can use
372// MutatorContext.CreateSubVariants to split a Module into multiple Modules,
373// modifying properties on the new modules to differentiate them. It is called
374// after parsing all Blueprint files, but before generating any build rules,
375// and is always called on dependencies before being called on the depending module.
376//
377// The Mutator function should only modify members of properties structs, and not
378// members of the module struct itself, to ensure the modified values are copied
379// if a second Mutator chooses to split the module a second time.
380type TopDownMutator func(mctx TopDownMutatorContext)
381type BottomUpMutator func(mctx BottomUpMutatorContext)
Colin Cross65569e42015-03-10 20:08:19 -0700382type EarlyMutator func(mctx EarlyMutatorContext)
Colin Crossc9028482014-12-18 16:28:54 -0800383
384// Split a module into mulitple variants, one for each name in the variantNames
385// parameter. It returns a list of new modules in the same order as the variantNames
386// list.
387//
388// If any of the dependencies of the module being operated on were already split
389// by calling CreateVariants with the same name, the dependency will automatically
390// be updated to point the matching variant.
391//
392// If a module is split, and then a module depending on the first module is not split
393// when the Mutator is later called on it, the dependency of the depending module will
394// automatically be updated to point to the first variant.
395func (mctx *mutatorContext) CreateVariants(variantNames ...string) []Module {
Colin Cross65569e42015-03-10 20:08:19 -0700396 return mctx.createVariants(variantNames, false)
397}
398
399// Split a module into mulitple variants, one for each name in the variantNames
400// parameter. It returns a list of new modules in the same order as the variantNames
401// list.
402//
403// Local variants do not affect automatic dependency resolution - dependencies added
404// to the split module via deps or DynamicDependerModule must exactly match a variant
405// that contains all the non-local variants.
406func (mctx *mutatorContext) CreateLocalVariants(variantNames ...string) []Module {
407 return mctx.createVariants(variantNames, true)
408}
409
410func (mctx *mutatorContext) createVariants(variantNames []string, local bool) []Module {
Colin Crossc9028482014-12-18 16:28:54 -0800411 ret := []Module{}
Colin Cross174ae052015-03-03 17:37:03 -0800412 modules, errs := mctx.context.createVariants(mctx.module, mctx.name, variantNames)
413 if len(errs) > 0 {
414 mctx.errs = append(mctx.errs, errs...)
415 }
Colin Crossc9028482014-12-18 16:28:54 -0800416
Colin Cross65569e42015-03-10 20:08:19 -0700417 for i, module := range modules {
Colin Crossc9028482014-12-18 16:28:54 -0800418 ret = append(ret, module.logicModule)
Colin Cross65569e42015-03-10 20:08:19 -0700419 if !local {
420 module.dependencyVariants[mctx.name] = variantNames[i]
421 }
Colin Crossc9028482014-12-18 16:28:54 -0800422 }
423
424 if len(ret) != len(variantNames) {
425 panic("oops!")
426 }
427
428 return ret
429}
430
431// Set all dangling dependencies on the current module to point to the variant
432// with given name.
433func (mctx *mutatorContext) SetDependencyVariant(variantName string) {
Colin Crosse7daa222015-03-11 14:35:41 -0700434 mctx.context.convertDepsToVariant(mctx.module, mctx.name, variantName)
Colin Crossc9028482014-12-18 16:28:54 -0800435}
436
437func (mctx *mutatorContext) Module() Module {
438 return mctx.module.logicModule
439}
440
441// Add a dependency to the given module. The depender can be a specific variant
442// of a module, but the dependee must be a module that only has a single variant.
443// Does not affect the ordering of the current mutator pass, but will be ordered
444// correctly for all future mutator passes.
445func (mctx *mutatorContext) AddDependency(module Module, depName string) {
Colin Cross65569e42015-03-10 20:08:19 -0700446 errs := mctx.context.addDependency(mctx.context.moduleInfo[module], depName)
447 if len(errs) > 0 {
448 mctx.errs = append(mctx.errs, errs...)
449 }
Colin Crossc9028482014-12-18 16:28:54 -0800450 mctx.dependenciesModified = true
451}
452
453func (mctx *mutatorContext) VisitDirectDeps(visit func(Module)) {
454 mctx.context.visitDirectDeps(mctx.module, visit)
455}
456
457func (mctx *mutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
458 mctx.context.visitDirectDepsIf(mctx.module, pred, visit)
459}
460
461func (mctx *mutatorContext) VisitDepsDepthFirst(visit func(Module)) {
462 mctx.context.visitDepsDepthFirst(mctx.module, visit)
463}
464
465func (mctx *mutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool,
466 visit func(Module)) {
467
468 mctx.context.visitDepsDepthFirstIf(mctx.module, pred, visit)
469}