blob: ac80d03fbf5c9a0e46b5f6541970fb269431e08b [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
121}
122
Colin Cross1455a0f2014-12-17 13:23:56 -0800123type ModuleContext interface {
Colin Crossbe1a9a12014-12-18 11:05:45 -0800124 BaseModuleContext
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700125
126 OtherModuleName(m Module) string
127 OtherModuleErrorf(m Module, fmt string, args ...interface{})
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700128
Colin Crossc7ffa302015-02-10 11:24:52 -0800129 VisitDirectDeps(visit func(Module))
130 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800131 VisitDepsDepthFirst(visit func(Module))
132 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800133
Colin Crossc9028482014-12-18 16:28:54 -0800134 ModuleSubDir() string
135
Jamie Gennis2fb20952014-10-03 02:49:58 -0700136 Variable(pctx *PackageContext, name, value string)
137 Rule(pctx *PackageContext, name string, params RuleParams, argNames ...string) Rule
138 Build(pctx *PackageContext, params BuildParams)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700139
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700140 AddNinjaFileDeps(deps ...string)
Colin Crossc9028482014-12-18 16:28:54 -0800141
142 PrimaryModule() Module
Colin Cross80ad04d2015-01-06 16:19:59 -0800143 FinalModule() Module
144 VisitAllModuleVariants(visit func(Module))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700145}
146
Colin Crossbe1a9a12014-12-18 11:05:45 -0800147var _ BaseModuleContext = (*baseModuleContext)(nil)
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700148
Colin Crossbe1a9a12014-12-18 11:05:45 -0800149type baseModuleContext struct {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700150 context *Context
151 config interface{}
Colin Crossed342d92015-03-11 00:57:25 -0700152 module *moduleInfo
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700153 errs []error
154}
155
Colin Crossbe1a9a12014-12-18 11:05:45 -0800156func (d *baseModuleContext) ModuleName() string {
Colin Crossed342d92015-03-11 00:57:25 -0700157 return d.module.properties.Name
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700158}
159
Colin Crossbe1a9a12014-12-18 11:05:45 -0800160func (d *baseModuleContext) ContainsProperty(name string) bool {
Colin Crossed342d92015-03-11 00:57:25 -0700161 _, ok := d.module.propertyPos[name]
David Allison701fbad2014-10-29 14:51:13 -0700162 return ok
163}
164
Colin Crossbe1a9a12014-12-18 11:05:45 -0800165func (d *baseModuleContext) ModuleDir() string {
Colin Crossed342d92015-03-11 00:57:25 -0700166 return filepath.Dir(d.module.relBlueprintsFile)
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700167}
168
Colin Crossbe1a9a12014-12-18 11:05:45 -0800169func (d *baseModuleContext) Config() interface{} {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700170 return d.config
171}
172
Colin Crossbe1a9a12014-12-18 11:05:45 -0800173func (d *baseModuleContext) Errorf(pos scanner.Position,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700174 format string, args ...interface{}) {
175
176 d.errs = append(d.errs, &Error{
177 Err: fmt.Errorf(format, args...),
178 Pos: pos,
179 })
180}
181
Colin Crossbe1a9a12014-12-18 11:05:45 -0800182func (d *baseModuleContext) ModuleErrorf(format string,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700183 args ...interface{}) {
184
185 d.errs = append(d.errs, &Error{
186 Err: fmt.Errorf(format, args...),
Colin Crossed342d92015-03-11 00:57:25 -0700187 Pos: d.module.pos,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700188 })
189}
190
Colin Crossbe1a9a12014-12-18 11:05:45 -0800191func (d *baseModuleContext) PropertyErrorf(property, format string,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700192 args ...interface{}) {
193
Colin Crossed342d92015-03-11 00:57:25 -0700194 pos, ok := d.module.propertyPos[property]
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700195 if !ok {
196 panic(fmt.Errorf("property %q was not set for this module", property))
197 }
198
199 d.errs = append(d.errs, &Error{
200 Err: fmt.Errorf(format, args...),
201 Pos: pos,
202 })
203}
204
Colin Crossbe1a9a12014-12-18 11:05:45 -0800205func (d *baseModuleContext) Failed() bool {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700206 return len(d.errs) > 0
207}
208
Colin Cross1455a0f2014-12-17 13:23:56 -0800209var _ ModuleContext = (*moduleContext)(nil)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700210
Colin Cross1455a0f2014-12-17 13:23:56 -0800211type moduleContext struct {
Colin Crossbe1a9a12014-12-18 11:05:45 -0800212 baseModuleContext
Colin Cross1455a0f2014-12-17 13:23:56 -0800213 scope *localScope
214 ninjaFileDeps []string
215 actionDefs localBuildActions
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700216}
217
Colin Crossed342d92015-03-11 00:57:25 -0700218func (m *moduleContext) OtherModuleName(logicModule Module) string {
219 module := m.context.moduleInfo[logicModule]
220 return module.properties.Name
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700221}
222
Colin Crossed342d92015-03-11 00:57:25 -0700223func (m *moduleContext) OtherModuleErrorf(logicModule Module, format string,
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700224 args ...interface{}) {
225
Colin Crossed342d92015-03-11 00:57:25 -0700226 module := m.context.moduleInfo[logicModule]
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700227 m.errs = append(m.errs, &Error{
228 Err: fmt.Errorf(format, args...),
Colin Crossed342d92015-03-11 00:57:25 -0700229 Pos: module.pos,
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700230 })
231}
232
Colin Crossc7ffa302015-02-10 11:24:52 -0800233func (m *moduleContext) VisitDirectDeps(visit func(Module)) {
234 m.context.visitDirectDeps(m.module, visit)
235}
236
237func (m *moduleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
238 m.context.visitDirectDepsIf(m.module, pred, visit)
239}
240
Colin Cross1455a0f2014-12-17 13:23:56 -0800241func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) {
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800242 m.context.visitDepsDepthFirst(m.module, visit)
243}
244
Colin Cross1455a0f2014-12-17 13:23:56 -0800245func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool,
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800246 visit func(Module)) {
247
248 m.context.visitDepsDepthFirstIf(m.module, pred, visit)
249}
250
Colin Crossc9028482014-12-18 16:28:54 -0800251func (m *moduleContext) ModuleSubDir() string {
252 return m.module.subName()
253}
254
Jamie Gennis2fb20952014-10-03 02:49:58 -0700255func (m *moduleContext) Variable(pctx *PackageContext, name, value string) {
256 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700257
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700258 v, err := m.scope.AddLocalVariable(name, value)
259 if err != nil {
260 panic(err)
261 }
262
263 m.actionDefs.variables = append(m.actionDefs.variables, v)
264}
265
Jamie Gennis2fb20952014-10-03 02:49:58 -0700266func (m *moduleContext) Rule(pctx *PackageContext, name string,
267 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700268
Jamie Gennis2fb20952014-10-03 02:49:58 -0700269 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700270
Jamie Genniscbc6f862014-06-05 20:00:22 -0700271 r, err := m.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700272 if err != nil {
273 panic(err)
274 }
275
276 m.actionDefs.rules = append(m.actionDefs.rules, r)
277
278 return r
279}
280
Jamie Gennis2fb20952014-10-03 02:49:58 -0700281func (m *moduleContext) Build(pctx *PackageContext, params BuildParams) {
282 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700283
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700284 def, err := parseBuildParams(m.scope, &params)
285 if err != nil {
286 panic(err)
287 }
288
289 m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def)
290}
291
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700292func (m *moduleContext) AddNinjaFileDeps(deps ...string) {
293 m.ninjaFileDeps = append(m.ninjaFileDeps, deps...)
294}
Colin Crossc9028482014-12-18 16:28:54 -0800295
296func (m *moduleContext) PrimaryModule() Module {
Colin Cross80ad04d2015-01-06 16:19:59 -0800297 return m.module.group.modules[0].logicModule
298}
299
300func (m *moduleContext) FinalModule() Module {
301 return m.module.group.modules[len(m.module.group.modules)-1].logicModule
302}
303
304func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) {
305 for _, module := range m.module.group.modules {
306 visit(module.logicModule)
307 }
Colin Crossc9028482014-12-18 16:28:54 -0800308}
309
310//
311// MutatorContext
312//
313
314type mutatorContext struct {
315 baseModuleContext
Colin Crossc9028482014-12-18 16:28:54 -0800316 name string
317 dependenciesModified bool
318}
319
320type baseMutatorContext interface {
321 BaseModuleContext
322
323 Module() Module
324}
325
326type TopDownMutatorContext interface {
327 baseMutatorContext
328
329 VisitDirectDeps(visit func(Module))
330 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
331 VisitDepsDepthFirst(visit func(Module))
332 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
333}
334
335type BottomUpMutatorContext interface {
336 baseMutatorContext
337
338 AddDependency(module Module, name string)
339 CreateVariants(...string) []Module
340 SetDependencyVariant(string)
341}
342
343// A Mutator function is called for each Module, and can use
344// MutatorContext.CreateSubVariants to split a Module into multiple Modules,
345// modifying properties on the new modules to differentiate them. It is called
346// after parsing all Blueprint files, but before generating any build rules,
347// and is always called on dependencies before being called on the depending module.
348//
349// The Mutator function should only modify members of properties structs, and not
350// members of the module struct itself, to ensure the modified values are copied
351// if a second Mutator chooses to split the module a second time.
352type TopDownMutator func(mctx TopDownMutatorContext)
353type BottomUpMutator func(mctx BottomUpMutatorContext)
354
355// Split a module into mulitple variants, one for each name in the variantNames
356// parameter. It returns a list of new modules in the same order as the variantNames
357// list.
358//
359// If any of the dependencies of the module being operated on were already split
360// by calling CreateVariants with the same name, the dependency will automatically
361// be updated to point the matching variant.
362//
363// If a module is split, and then a module depending on the first module is not split
364// when the Mutator is later called on it, the dependency of the depending module will
365// automatically be updated to point to the first variant.
366func (mctx *mutatorContext) CreateVariants(variantNames ...string) []Module {
367 ret := []Module{}
Colin Cross174ae052015-03-03 17:37:03 -0800368 modules, errs := mctx.context.createVariants(mctx.module, mctx.name, variantNames)
369 if len(errs) > 0 {
370 mctx.errs = append(mctx.errs, errs...)
371 }
Colin Crossc9028482014-12-18 16:28:54 -0800372
373 for _, module := range modules {
374 ret = append(ret, module.logicModule)
375 }
376
377 if len(ret) != len(variantNames) {
378 panic("oops!")
379 }
380
381 return ret
382}
383
384// Set all dangling dependencies on the current module to point to the variant
385// with given name.
386func (mctx *mutatorContext) SetDependencyVariant(variantName string) {
387 subName := subName{
388 mutatorName: mctx.name,
389 variantName: variantName,
390 }
391 mctx.context.convertDepsToVariant(mctx.module, subName)
392}
393
394func (mctx *mutatorContext) Module() Module {
395 return mctx.module.logicModule
396}
397
398// Add a dependency to the given module. The depender can be a specific variant
399// of a module, but the dependee must be a module that only has a single variant.
400// Does not affect the ordering of the current mutator pass, but will be ordered
401// correctly for all future mutator passes.
402func (mctx *mutatorContext) AddDependency(module Module, depName string) {
403 mctx.context.addDependency(mctx.context.moduleInfo[module], depName)
404 mctx.dependenciesModified = true
405}
406
407func (mctx *mutatorContext) VisitDirectDeps(visit func(Module)) {
408 mctx.context.visitDirectDeps(mctx.module, visit)
409}
410
411func (mctx *mutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
412 mctx.context.visitDirectDepsIf(mctx.module, pred, visit)
413}
414
415func (mctx *mutatorContext) VisitDepsDepthFirst(visit func(Module)) {
416 mctx.context.visitDepsDepthFirst(mctx.module, visit)
417}
418
419func (mctx *mutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool,
420 visit func(Module)) {
421
422 mctx.context.visitDepsDepthFirstIf(mctx.module, pred, visit)
423}