blob: 7af26d2578701ec694be0d883612c0ea5a42ccbf [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
Colin Cross763b6f12015-10-29 15:32:56 -070037// specified in the Blueprints file, dynamically added by implementing the
38// (deprecated) DynamicDependerModule interface, or dynamically added by a
39// BottomUpMutator) using the ModuleContext passed to GenerateBuildActions.
40// This ModuleContext is also used to create Ninja build actions and to report
41// errors to the user.
Jamie Gennisb9e87f62014-09-24 20:28:11 -070042//
43// In addition to implementing the GenerateBuildActions method, a Module should
44// implement methods that provide dependant modules and singletons information
45// they need to generate their build actions. These methods will only be called
46// after GenerateBuildActions is called because the Context calls
47// GenerateBuildActions in dependency-order (and singletons are invoked after
48// all the Modules). The set of methods a Module supports will determine how
49// dependant Modules interact with it.
50//
51// For example, consider a Module that is responsible for generating a library
52// that other modules can link against. The library Module might implement the
53// following interface:
54//
55// type LibraryProducer interface {
56// LibraryFileName() string
57// }
58//
59// func IsLibraryProducer(module blueprint.Module) {
60// _, ok := module.(LibraryProducer)
61// return ok
62// }
63//
64// A binary-producing Module that depends on the library Module could then do:
65//
66// func (m *myBinaryModule) GenerateBuildActions(ctx blueprint.ModuleContext) {
67// ...
68// var libraryFiles []string
69// ctx.VisitDepsDepthFirstIf(IsLibraryProducer,
70// func(module blueprint.Module) {
71// libProducer := module.(LibraryProducer)
72// libraryFiles = append(libraryFiles, libProducer.LibraryFileName())
73// })
74// ...
75// }
76//
77// to build the list of library file names that should be included in its link
78// command.
Colin Cross691a60d2015-01-07 18:08:56 -080079//
80// GenerateBuildActions may be called from multiple threads. It is guaranteed to
81// be called after it has finished being called on all dependencies and on all
82// variants of that appear earlier in the ModuleContext.VisitAllModuleVariants list.
83// Any accesses to global variables or to Module objects that are not dependencies
84// or variants of the current Module must be synchronized by the implementation of
85// GenerateBuildActions.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070086type Module interface {
Jamie Gennisb9e87f62014-09-24 20:28:11 -070087 // GenerateBuildActions is called by the Context that created the Module
88 // during its generate phase. This call should generate all Ninja build
89 // actions (rules, pools, and build statements) needed to build the module.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070090 GenerateBuildActions(ModuleContext)
91}
92
Jamie Gennisb9e87f62014-09-24 20:28:11 -070093// A DynamicDependerModule is a Module that may add dependencies that do not
94// appear in its "deps" property. Any Module that implements this interface
95// will have its DynamicDependencies method called by the Context that created
96// it during generate phase.
Colin Cross763b6f12015-10-29 15:32:56 -070097//
98// Deprecated, use a BottomUpMutator instead
Jamie Gennisb9e87f62014-09-24 20:28:11 -070099type DynamicDependerModule interface {
100 Module
101
102 // DynamicDependencies is called by the Context that created the
103 // DynamicDependerModule during its generate phase. This call should return
104 // the list of module names that the DynamicDependerModule depends on
105 // dynamically. Module names that already appear in the "deps" property may
106 // but do not need to be included in the returned list.
107 DynamicDependencies(DynamicDependerModuleContext) []string
108}
109
Colin Crossbe1a9a12014-12-18 11:05:45 -0800110type BaseModuleContext interface {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700111 ModuleName() string
112 ModuleDir() string
Jamie Gennis6eb4d242014-06-11 18:31:16 -0700113 Config() interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700114
David Allison701fbad2014-10-29 14:51:13 -0700115 ContainsProperty(name string) bool
Jamie Gennis6a40c192014-07-02 16:40:31 -0700116 Errorf(pos scanner.Position, fmt string, args ...interface{})
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700117 ModuleErrorf(fmt string, args ...interface{})
118 PropertyErrorf(property, fmt string, args ...interface{})
Jamie Gennis6a40c192014-07-02 16:40:31 -0700119 Failed() bool
Colin Cross763b6f12015-10-29 15:32:56 -0700120
121 moduleInfo() *moduleInfo
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700122}
123
Colin Cross763b6f12015-10-29 15:32:56 -0700124type DynamicDependerModuleContext BottomUpMutatorContext
Colin Crossbe1a9a12014-12-18 11:05:45 -0800125
Colin Cross1455a0f2014-12-17 13:23:56 -0800126type ModuleContext interface {
Colin Crossbe1a9a12014-12-18 11:05:45 -0800127 BaseModuleContext
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700128
129 OtherModuleName(m Module) string
130 OtherModuleErrorf(m Module, fmt string, args ...interface{})
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700131
Colin Crossc7ffa302015-02-10 11:24:52 -0800132 VisitDirectDeps(visit func(Module))
133 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800134 VisitDepsDepthFirst(visit func(Module))
135 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
Yuchen Wu222e2452015-10-06 14:03:27 -0700136 WalkDeps(visit func(Module, Module) bool)
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800137
Colin Crossc9028482014-12-18 16:28:54 -0800138 ModuleSubDir() string
139
Jamie Gennis2fb20952014-10-03 02:49:58 -0700140 Variable(pctx *PackageContext, name, value string)
141 Rule(pctx *PackageContext, name string, params RuleParams, argNames ...string) Rule
142 Build(pctx *PackageContext, params BuildParams)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700143
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700144 AddNinjaFileDeps(deps ...string)
Colin Crossc9028482014-12-18 16:28:54 -0800145
146 PrimaryModule() Module
Colin Cross80ad04d2015-01-06 16:19:59 -0800147 FinalModule() Module
148 VisitAllModuleVariants(visit func(Module))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700149}
150
Colin Crossbe1a9a12014-12-18 11:05:45 -0800151var _ BaseModuleContext = (*baseModuleContext)(nil)
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700152
Colin Crossbe1a9a12014-12-18 11:05:45 -0800153type baseModuleContext struct {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700154 context *Context
155 config interface{}
Colin Crossed342d92015-03-11 00:57:25 -0700156 module *moduleInfo
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700157 errs []error
158}
159
Colin Cross763b6f12015-10-29 15:32:56 -0700160func (d *baseModuleContext) moduleInfo() *moduleInfo {
161 return d.module
162}
163
Colin Crossbe1a9a12014-12-18 11:05:45 -0800164func (d *baseModuleContext) ModuleName() string {
Colin Crossed342d92015-03-11 00:57:25 -0700165 return d.module.properties.Name
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700166}
167
Colin Crossbe1a9a12014-12-18 11:05:45 -0800168func (d *baseModuleContext) ContainsProperty(name string) bool {
Colin Crossed342d92015-03-11 00:57:25 -0700169 _, ok := d.module.propertyPos[name]
David Allison701fbad2014-10-29 14:51:13 -0700170 return ok
171}
172
Colin Crossbe1a9a12014-12-18 11:05:45 -0800173func (d *baseModuleContext) ModuleDir() string {
Colin Crossed342d92015-03-11 00:57:25 -0700174 return filepath.Dir(d.module.relBlueprintsFile)
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700175}
176
Colin Crossbe1a9a12014-12-18 11:05:45 -0800177func (d *baseModuleContext) Config() interface{} {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700178 return d.config
179}
180
Colin Crossbe1a9a12014-12-18 11:05:45 -0800181func (d *baseModuleContext) Errorf(pos scanner.Position,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700182 format string, args ...interface{}) {
183
184 d.errs = append(d.errs, &Error{
185 Err: fmt.Errorf(format, args...),
186 Pos: pos,
187 })
188}
189
Colin Crossbe1a9a12014-12-18 11:05:45 -0800190func (d *baseModuleContext) ModuleErrorf(format string,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700191 args ...interface{}) {
192
193 d.errs = append(d.errs, &Error{
194 Err: fmt.Errorf(format, args...),
Colin Crossed342d92015-03-11 00:57:25 -0700195 Pos: d.module.pos,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700196 })
197}
198
Colin Crossbe1a9a12014-12-18 11:05:45 -0800199func (d *baseModuleContext) PropertyErrorf(property, format string,
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700200 args ...interface{}) {
201
Colin Crossed342d92015-03-11 00:57:25 -0700202 pos, ok := d.module.propertyPos[property]
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700203 if !ok {
204 panic(fmt.Errorf("property %q was not set for this module", property))
205 }
206
207 d.errs = append(d.errs, &Error{
208 Err: fmt.Errorf(format, args...),
209 Pos: pos,
210 })
211}
212
Colin Crossbe1a9a12014-12-18 11:05:45 -0800213func (d *baseModuleContext) Failed() bool {
Jamie Gennisb9e87f62014-09-24 20:28:11 -0700214 return len(d.errs) > 0
215}
216
Colin Cross1455a0f2014-12-17 13:23:56 -0800217var _ ModuleContext = (*moduleContext)(nil)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700218
Colin Cross1455a0f2014-12-17 13:23:56 -0800219type moduleContext struct {
Colin Crossbe1a9a12014-12-18 11:05:45 -0800220 baseModuleContext
Colin Cross1455a0f2014-12-17 13:23:56 -0800221 scope *localScope
222 ninjaFileDeps []string
223 actionDefs localBuildActions
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700224}
225
Colin Crossed342d92015-03-11 00:57:25 -0700226func (m *moduleContext) OtherModuleName(logicModule Module) string {
227 module := m.context.moduleInfo[logicModule]
228 return module.properties.Name
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700229}
230
Colin Crossed342d92015-03-11 00:57:25 -0700231func (m *moduleContext) OtherModuleErrorf(logicModule Module, format string,
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700232 args ...interface{}) {
233
Colin Crossed342d92015-03-11 00:57:25 -0700234 module := m.context.moduleInfo[logicModule]
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700235 m.errs = append(m.errs, &Error{
236 Err: fmt.Errorf(format, args...),
Colin Crossed342d92015-03-11 00:57:25 -0700237 Pos: module.pos,
Jamie Gennisd4c53d82014-06-22 17:02:55 -0700238 })
239}
240
Colin Crossc7ffa302015-02-10 11:24:52 -0800241func (m *moduleContext) VisitDirectDeps(visit func(Module)) {
242 m.context.visitDirectDeps(m.module, visit)
243}
244
245func (m *moduleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
246 m.context.visitDirectDepsIf(m.module, pred, visit)
247}
248
Colin Cross1455a0f2014-12-17 13:23:56 -0800249func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) {
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800250 m.context.visitDepsDepthFirst(m.module, visit)
251}
252
Colin Cross1455a0f2014-12-17 13:23:56 -0800253func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool,
Colin Crossb2e7b5d2014-11-11 14:18:53 -0800254 visit func(Module)) {
255
256 m.context.visitDepsDepthFirstIf(m.module, pred, visit)
257}
258
Yuchen Wu222e2452015-10-06 14:03:27 -0700259func (m *moduleContext) WalkDeps(visit func(Module, Module) bool) {
260 m.context.walkDeps(m.module, visit)
261}
262
Colin Crossc9028482014-12-18 16:28:54 -0800263func (m *moduleContext) ModuleSubDir() string {
Colin Crosse7daa222015-03-11 14:35:41 -0700264 return m.module.variantName
Colin Crossc9028482014-12-18 16:28:54 -0800265}
266
Jamie Gennis2fb20952014-10-03 02:49:58 -0700267func (m *moduleContext) Variable(pctx *PackageContext, name, value string) {
268 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700269
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700270 v, err := m.scope.AddLocalVariable(name, value)
271 if err != nil {
272 panic(err)
273 }
274
275 m.actionDefs.variables = append(m.actionDefs.variables, v)
276}
277
Jamie Gennis2fb20952014-10-03 02:49:58 -0700278func (m *moduleContext) Rule(pctx *PackageContext, name string,
279 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700280
Jamie Gennis2fb20952014-10-03 02:49:58 -0700281 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700282
Jamie Genniscbc6f862014-06-05 20:00:22 -0700283 r, err := m.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700284 if err != nil {
285 panic(err)
286 }
287
288 m.actionDefs.rules = append(m.actionDefs.rules, r)
289
290 return r
291}
292
Jamie Gennis2fb20952014-10-03 02:49:58 -0700293func (m *moduleContext) Build(pctx *PackageContext, params BuildParams) {
294 m.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700295
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700296 def, err := parseBuildParams(m.scope, &params)
297 if err != nil {
298 panic(err)
299 }
300
301 m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def)
302}
303
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700304func (m *moduleContext) AddNinjaFileDeps(deps ...string) {
305 m.ninjaFileDeps = append(m.ninjaFileDeps, deps...)
306}
Colin Crossc9028482014-12-18 16:28:54 -0800307
308func (m *moduleContext) PrimaryModule() Module {
Colin Cross80ad04d2015-01-06 16:19:59 -0800309 return m.module.group.modules[0].logicModule
310}
311
312func (m *moduleContext) FinalModule() Module {
313 return m.module.group.modules[len(m.module.group.modules)-1].logicModule
314}
315
316func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) {
317 for _, module := range m.module.group.modules {
318 visit(module.logicModule)
319 }
Colin Crossc9028482014-12-18 16:28:54 -0800320}
321
322//
323// MutatorContext
324//
325
326type mutatorContext struct {
327 baseModuleContext
Dan Willemsenfdeb7242015-07-24 16:53:27 -0700328 name string
Colin Crossc9028482014-12-18 16:28:54 -0800329}
330
331type baseMutatorContext interface {
332 BaseModuleContext
333
334 Module() Module
335}
336
Colin Cross65569e42015-03-10 20:08:19 -0700337type EarlyMutatorContext interface {
338 baseMutatorContext
339
Colin Crossf5e34b92015-03-13 16:02:36 -0700340 CreateVariations(...string) []Module
341 CreateLocalVariations(...string) []Module
Colin Cross65569e42015-03-10 20:08:19 -0700342}
343
Colin Crossc9028482014-12-18 16:28:54 -0800344type TopDownMutatorContext interface {
345 baseMutatorContext
346
347 VisitDirectDeps(visit func(Module))
348 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
349 VisitDepsDepthFirst(visit func(Module))
350 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
Yuchen Wu222e2452015-10-06 14:03:27 -0700351 WalkDeps(visit func(Module, Module) bool)
Colin Crossc9028482014-12-18 16:28:54 -0800352}
353
354type BottomUpMutatorContext interface {
355 baseMutatorContext
356
Colin Cross763b6f12015-10-29 15:32:56 -0700357 AddDependency(module Module, name ...string)
Dan Willemsenfdeb7242015-07-24 16:53:27 -0700358 AddReverseDependency(module Module, name string)
Colin Crossf5e34b92015-03-13 16:02:36 -0700359 CreateVariations(...string) []Module
Jamie Gennis6a5825e2015-05-19 11:26:11 -0700360 CreateLocalVariations(...string) []Module
Colin Crossf5e34b92015-03-13 16:02:36 -0700361 SetDependencyVariation(string)
Colin Cross763b6f12015-10-29 15:32:56 -0700362 AddVariationDependencies([]Variation, ...string)
363 AddFarVariationDependencies([]Variation, ...string)
Colin Crossc9028482014-12-18 16:28:54 -0800364}
365
366// A Mutator function is called for each Module, and can use
Colin Crossf5e34b92015-03-13 16:02:36 -0700367// MutatorContext.CreateVariations to split a Module into multiple Modules,
Colin Crossc9028482014-12-18 16:28:54 -0800368// modifying properties on the new modules to differentiate them. It is called
369// after parsing all Blueprint files, but before generating any build rules,
370// and is always called on dependencies before being called on the depending module.
371//
372// The Mutator function should only modify members of properties structs, and not
373// members of the module struct itself, to ensure the modified values are copied
374// if a second Mutator chooses to split the module a second time.
375type TopDownMutator func(mctx TopDownMutatorContext)
376type BottomUpMutator func(mctx BottomUpMutatorContext)
Colin Cross65569e42015-03-10 20:08:19 -0700377type EarlyMutator func(mctx EarlyMutatorContext)
Colin Crossc9028482014-12-18 16:28:54 -0800378
Colin Crossf5e34b92015-03-13 16:02:36 -0700379// Split a module into mulitple variants, one for each name in the variationNames
380// parameter. It returns a list of new modules in the same order as the variationNames
Colin Crossc9028482014-12-18 16:28:54 -0800381// list.
382//
383// If any of the dependencies of the module being operated on were already split
Colin Crossf5e34b92015-03-13 16:02:36 -0700384// by calling CreateVariations with the same name, the dependency will automatically
Colin Crossc9028482014-12-18 16:28:54 -0800385// be updated to point the matching variant.
386//
387// If a module is split, and then a module depending on the first module is not split
388// when the Mutator is later called on it, the dependency of the depending module will
389// automatically be updated to point to the first variant.
Colin Crossf5e34b92015-03-13 16:02:36 -0700390func (mctx *mutatorContext) CreateVariations(variationNames ...string) []Module {
391 return mctx.createVariations(variationNames, false)
Colin Cross65569e42015-03-10 20:08:19 -0700392}
393
394// Split a module into mulitple variants, one for each name in the variantNames
395// parameter. It returns a list of new modules in the same order as the variantNames
396// list.
397//
Colin Crossf5e34b92015-03-13 16:02:36 -0700398// Local variations do not affect automatic dependency resolution - dependencies added
Colin Cross65569e42015-03-10 20:08:19 -0700399// to the split module via deps or DynamicDependerModule must exactly match a variant
Colin Crossf5e34b92015-03-13 16:02:36 -0700400// that contains all the non-local variations.
401func (mctx *mutatorContext) CreateLocalVariations(variationNames ...string) []Module {
402 return mctx.createVariations(variationNames, true)
Colin Cross65569e42015-03-10 20:08:19 -0700403}
404
Colin Crossf5e34b92015-03-13 16:02:36 -0700405func (mctx *mutatorContext) createVariations(variationNames []string, local bool) []Module {
Colin Crossc9028482014-12-18 16:28:54 -0800406 ret := []Module{}
Colin Crossf5e34b92015-03-13 16:02:36 -0700407 modules, errs := mctx.context.createVariations(mctx.module, mctx.name, variationNames)
Colin Cross174ae052015-03-03 17:37:03 -0800408 if len(errs) > 0 {
409 mctx.errs = append(mctx.errs, errs...)
410 }
Colin Crossc9028482014-12-18 16:28:54 -0800411
Colin Cross65569e42015-03-10 20:08:19 -0700412 for i, module := range modules {
Colin Crossc9028482014-12-18 16:28:54 -0800413 ret = append(ret, module.logicModule)
Colin Cross65569e42015-03-10 20:08:19 -0700414 if !local {
Colin Crossf5e34b92015-03-13 16:02:36 -0700415 module.dependencyVariant[mctx.name] = variationNames[i]
Colin Cross65569e42015-03-10 20:08:19 -0700416 }
Colin Crossc9028482014-12-18 16:28:54 -0800417 }
418
Colin Crossf5e34b92015-03-13 16:02:36 -0700419 if len(ret) != len(variationNames) {
Colin Crossc9028482014-12-18 16:28:54 -0800420 panic("oops!")
421 }
422
423 return ret
424}
425
Colin Crossf5e34b92015-03-13 16:02:36 -0700426// Set all dangling dependencies on the current module to point to the variation
Colin Crossc9028482014-12-18 16:28:54 -0800427// with given name.
Colin Crossf5e34b92015-03-13 16:02:36 -0700428func (mctx *mutatorContext) SetDependencyVariation(variationName string) {
429 mctx.context.convertDepsToVariation(mctx.module, mctx.name, variationName)
Colin Crossc9028482014-12-18 16:28:54 -0800430}
431
432func (mctx *mutatorContext) Module() Module {
433 return mctx.module.logicModule
434}
435
Dan Willemsenfdeb7242015-07-24 16:53:27 -0700436// Add a dependency to the given module.
Colin Crossc9028482014-12-18 16:28:54 -0800437// Does not affect the ordering of the current mutator pass, but will be ordered
438// correctly for all future mutator passes.
Colin Cross763b6f12015-10-29 15:32:56 -0700439func (mctx *mutatorContext) AddDependency(module Module, deps ...string) {
440 for _, dep := range deps {
441 errs := mctx.context.addDependency(mctx.context.moduleInfo[module], dep)
442 if len(errs) > 0 {
443 mctx.errs = append(mctx.errs, errs...)
444 }
Colin Cross65569e42015-03-10 20:08:19 -0700445 }
Dan Willemsenfdeb7242015-07-24 16:53:27 -0700446}
447
448// Add a dependency from the destination to the given module.
449// Does not affect the ordering of the current mutator pass, but will be ordered
450// correctly for all future mutator passes.
451func (mctx *mutatorContext) AddReverseDependency(module Module, destName string) {
Colin Cross763b6f12015-10-29 15:32:56 -0700452 errs := mctx.context.addReverseDependency(mctx.module, destName)
Dan Willemsenfdeb7242015-07-24 16:53:27 -0700453 if len(errs) > 0 {
454 mctx.errs = append(mctx.errs, errs...)
455 }
Colin Crossc9028482014-12-18 16:28:54 -0800456}
457
Colin Cross763b6f12015-10-29 15:32:56 -0700458// AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
459// argument to select which variant of the dependency to use. A variant of the dependency must
460// exist that matches the all of the non-local variations of the current module, plus the variations
461// argument.
462func (mctx *mutatorContext) AddVariationDependencies(variations []Variation,
463 deps ...string) {
464
465 for _, dep := range deps {
466 errs := mctx.context.addVariationDependency(mctx.module, variations, dep, false)
467 if len(errs) > 0 {
468 mctx.errs = append(mctx.errs, errs...)
469 }
470 }
471}
472
473// AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
474// variations argument to select which variant of the dependency to use. A variant of the
475// dependency must exist that matches the variations argument, but may also have other variations.
476// For any unspecified variation the first variant will be used.
477//
478// Unlike AddVariationDependencies, the variations of the current module are ignored - the
479// depdendency only needs to match the supplied variations.
480func (mctx *mutatorContext) AddFarVariationDependencies(variations []Variation,
481 deps ...string) {
482
483 for _, dep := range deps {
484 errs := mctx.context.addVariationDependency(mctx.module, variations, dep, true)
485 if len(errs) > 0 {
486 mctx.errs = append(mctx.errs, errs...)
487 }
488 }
489}
490
Colin Crossc9028482014-12-18 16:28:54 -0800491func (mctx *mutatorContext) VisitDirectDeps(visit func(Module)) {
492 mctx.context.visitDirectDeps(mctx.module, visit)
493}
494
495func (mctx *mutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
496 mctx.context.visitDirectDepsIf(mctx.module, pred, visit)
497}
498
499func (mctx *mutatorContext) VisitDepsDepthFirst(visit func(Module)) {
500 mctx.context.visitDepsDepthFirst(mctx.module, visit)
501}
502
503func (mctx *mutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool,
504 visit func(Module)) {
505
506 mctx.context.visitDepsDepthFirstIf(mctx.module, pred, visit)
507}
Yuchen Wu222e2452015-10-06 14:03:27 -0700508
509func (mctx *mutatorContext) WalkDeps(visit func(Module, Module) bool) {
510 mctx.context.walkDeps(mctx.module, visit)
511}