Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
Jamie Gennis | 6a40c19 | 2014-07-02 16:40:31 -0700 | [diff] [blame] | 20 | "text/scanner" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 23 | // A Module handles generating all of the Ninja build actions needed to build a |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 24 | // 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 Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 29 | // information on this). |
| 30 | // |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 31 | // 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 Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 35 | // 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 Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 78 | // |
| 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 85 | type Module interface { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 86 | // 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 89 | GenerateBuildActions(ModuleContext) |
| 90 | } |
| 91 | |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 92 | // 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. |
| 96 | type 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 Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 107 | type BaseModuleContext interface { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 108 | ModuleName() string |
| 109 | ModuleDir() string |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 110 | Config() interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 111 | |
David Allison | 701fbad | 2014-10-29 14:51:13 -0700 | [diff] [blame] | 112 | ContainsProperty(name string) bool |
Jamie Gennis | 6a40c19 | 2014-07-02 16:40:31 -0700 | [diff] [blame] | 113 | Errorf(pos scanner.Position, fmt string, args ...interface{}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 114 | ModuleErrorf(fmt string, args ...interface{}) |
| 115 | PropertyErrorf(property, fmt string, args ...interface{}) |
Jamie Gennis | 6a40c19 | 2014-07-02 16:40:31 -0700 | [diff] [blame] | 116 | Failed() bool |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 119 | type DynamicDependerModuleContext interface { |
| 120 | BaseModuleContext |
| 121 | } |
| 122 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 123 | type ModuleContext interface { |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 124 | BaseModuleContext |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 125 | |
| 126 | OtherModuleName(m Module) string |
| 127 | OtherModuleErrorf(m Module, fmt string, args ...interface{}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 128 | |
Colin Cross | c7ffa30 | 2015-02-10 11:24:52 -0800 | [diff] [blame] | 129 | VisitDirectDeps(visit func(Module)) |
| 130 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 131 | VisitDepsDepthFirst(visit func(Module)) |
| 132 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 133 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 134 | ModuleSubDir() string |
| 135 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 136 | Variable(pctx *PackageContext, name, value string) |
| 137 | Rule(pctx *PackageContext, name string, params RuleParams, argNames ...string) Rule |
| 138 | Build(pctx *PackageContext, params BuildParams) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 139 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 140 | AddNinjaFileDeps(deps ...string) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 141 | |
| 142 | PrimaryModule() Module |
Colin Cross | 80ad04d | 2015-01-06 16:19:59 -0800 | [diff] [blame] | 143 | FinalModule() Module |
| 144 | VisitAllModuleVariants(visit func(Module)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 147 | var _ BaseModuleContext = (*baseModuleContext)(nil) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 148 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 149 | type baseModuleContext struct { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 150 | context *Context |
| 151 | config interface{} |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 152 | group *moduleGroup |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 153 | errs []error |
| 154 | } |
| 155 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 156 | func (d *baseModuleContext) ModuleName() string { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 157 | return d.group.properties.Name |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 160 | func (d *baseModuleContext) ContainsProperty(name string) bool { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 161 | _, ok := d.group.propertyPos[name] |
David Allison | 701fbad | 2014-10-29 14:51:13 -0700 | [diff] [blame] | 162 | return ok |
| 163 | } |
| 164 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 165 | func (d *baseModuleContext) ModuleDir() string { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 166 | return filepath.Dir(d.group.relBlueprintsFile) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 169 | func (d *baseModuleContext) Config() interface{} { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 170 | return d.config |
| 171 | } |
| 172 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 173 | func (d *baseModuleContext) Errorf(pos scanner.Position, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 174 | 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 Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 182 | func (d *baseModuleContext) ModuleErrorf(format string, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 183 | args ...interface{}) { |
| 184 | |
| 185 | d.errs = append(d.errs, &Error{ |
| 186 | Err: fmt.Errorf(format, args...), |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 187 | Pos: d.group.pos, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 188 | }) |
| 189 | } |
| 190 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 191 | func (d *baseModuleContext) PropertyErrorf(property, format string, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 192 | args ...interface{}) { |
| 193 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 194 | pos, ok := d.group.propertyPos[property] |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 195 | 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 Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 205 | func (d *baseModuleContext) Failed() bool { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 206 | return len(d.errs) > 0 |
| 207 | } |
| 208 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 209 | var _ ModuleContext = (*moduleContext)(nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 210 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 211 | type moduleContext struct { |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 212 | baseModuleContext |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 213 | module *moduleInfo |
| 214 | scope *localScope |
| 215 | ninjaFileDeps []string |
| 216 | actionDefs localBuildActions |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 219 | func (m *moduleContext) OtherModuleName(module Module) string { |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 220 | info := m.context.moduleInfo[module] |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 221 | return info.group.properties.Name |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 224 | func (m *moduleContext) OtherModuleErrorf(module Module, format string, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 225 | args ...interface{}) { |
| 226 | |
| 227 | info := m.context.moduleInfo[module] |
| 228 | m.errs = append(m.errs, &Error{ |
| 229 | Err: fmt.Errorf(format, args...), |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 230 | Pos: info.group.pos, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 231 | }) |
| 232 | } |
| 233 | |
Colin Cross | c7ffa30 | 2015-02-10 11:24:52 -0800 | [diff] [blame] | 234 | func (m *moduleContext) VisitDirectDeps(visit func(Module)) { |
| 235 | m.context.visitDirectDeps(m.module, visit) |
| 236 | } |
| 237 | |
| 238 | func (m *moduleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 239 | m.context.visitDirectDepsIf(m.module, pred, visit) |
| 240 | } |
| 241 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 242 | func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) { |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 243 | m.context.visitDepsDepthFirst(m.module, visit) |
| 244 | } |
| 245 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 246 | func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 247 | visit func(Module)) { |
| 248 | |
| 249 | m.context.visitDepsDepthFirstIf(m.module, pred, visit) |
| 250 | } |
| 251 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 252 | func (m *moduleContext) ModuleSubDir() string { |
| 253 | return m.module.subName() |
| 254 | } |
| 255 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 256 | func (m *moduleContext) Variable(pctx *PackageContext, name, value string) { |
| 257 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 258 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 259 | v, err := m.scope.AddLocalVariable(name, value) |
| 260 | if err != nil { |
| 261 | panic(err) |
| 262 | } |
| 263 | |
| 264 | m.actionDefs.variables = append(m.actionDefs.variables, v) |
| 265 | } |
| 266 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 267 | func (m *moduleContext) Rule(pctx *PackageContext, name string, |
| 268 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 269 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 270 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 271 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 272 | r, err := m.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 273 | if err != nil { |
| 274 | panic(err) |
| 275 | } |
| 276 | |
| 277 | m.actionDefs.rules = append(m.actionDefs.rules, r) |
| 278 | |
| 279 | return r |
| 280 | } |
| 281 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 282 | func (m *moduleContext) Build(pctx *PackageContext, params BuildParams) { |
| 283 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 284 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 285 | def, err := parseBuildParams(m.scope, ¶ms) |
| 286 | if err != nil { |
| 287 | panic(err) |
| 288 | } |
| 289 | |
| 290 | m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def) |
| 291 | } |
| 292 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 293 | func (m *moduleContext) AddNinjaFileDeps(deps ...string) { |
| 294 | m.ninjaFileDeps = append(m.ninjaFileDeps, deps...) |
| 295 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 296 | |
| 297 | func (m *moduleContext) PrimaryModule() Module { |
Colin Cross | 80ad04d | 2015-01-06 16:19:59 -0800 | [diff] [blame] | 298 | return m.module.group.modules[0].logicModule |
| 299 | } |
| 300 | |
| 301 | func (m *moduleContext) FinalModule() Module { |
| 302 | return m.module.group.modules[len(m.module.group.modules)-1].logicModule |
| 303 | } |
| 304 | |
| 305 | func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) { |
| 306 | for _, module := range m.module.group.modules { |
| 307 | visit(module.logicModule) |
| 308 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // |
| 312 | // MutatorContext |
| 313 | // |
| 314 | |
| 315 | type mutatorContext struct { |
| 316 | baseModuleContext |
| 317 | module *moduleInfo |
| 318 | name string |
| 319 | dependenciesModified bool |
| 320 | } |
| 321 | |
| 322 | type baseMutatorContext interface { |
| 323 | BaseModuleContext |
| 324 | |
| 325 | Module() Module |
| 326 | } |
| 327 | |
| 328 | type TopDownMutatorContext interface { |
| 329 | baseMutatorContext |
| 330 | |
| 331 | VisitDirectDeps(visit func(Module)) |
| 332 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) |
| 333 | VisitDepsDepthFirst(visit func(Module)) |
| 334 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) |
| 335 | } |
| 336 | |
| 337 | type BottomUpMutatorContext interface { |
| 338 | baseMutatorContext |
| 339 | |
| 340 | AddDependency(module Module, name string) |
| 341 | CreateVariants(...string) []Module |
| 342 | SetDependencyVariant(string) |
| 343 | } |
| 344 | |
| 345 | // A Mutator function is called for each Module, and can use |
| 346 | // MutatorContext.CreateSubVariants to split a Module into multiple Modules, |
| 347 | // modifying properties on the new modules to differentiate them. It is called |
| 348 | // after parsing all Blueprint files, but before generating any build rules, |
| 349 | // and is always called on dependencies before being called on the depending module. |
| 350 | // |
| 351 | // The Mutator function should only modify members of properties structs, and not |
| 352 | // members of the module struct itself, to ensure the modified values are copied |
| 353 | // if a second Mutator chooses to split the module a second time. |
| 354 | type TopDownMutator func(mctx TopDownMutatorContext) |
| 355 | type BottomUpMutator func(mctx BottomUpMutatorContext) |
| 356 | |
| 357 | // Split a module into mulitple variants, one for each name in the variantNames |
| 358 | // parameter. It returns a list of new modules in the same order as the variantNames |
| 359 | // list. |
| 360 | // |
| 361 | // If any of the dependencies of the module being operated on were already split |
| 362 | // by calling CreateVariants with the same name, the dependency will automatically |
| 363 | // be updated to point the matching variant. |
| 364 | // |
| 365 | // If a module is split, and then a module depending on the first module is not split |
| 366 | // when the Mutator is later called on it, the dependency of the depending module will |
| 367 | // automatically be updated to point to the first variant. |
| 368 | func (mctx *mutatorContext) CreateVariants(variantNames ...string) []Module { |
| 369 | ret := []Module{} |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 370 | modules, errs := mctx.context.createVariants(mctx.module, mctx.name, variantNames) |
| 371 | if len(errs) > 0 { |
| 372 | mctx.errs = append(mctx.errs, errs...) |
| 373 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 374 | |
| 375 | for _, module := range modules { |
| 376 | ret = append(ret, module.logicModule) |
| 377 | } |
| 378 | |
| 379 | if len(ret) != len(variantNames) { |
| 380 | panic("oops!") |
| 381 | } |
| 382 | |
| 383 | return ret |
| 384 | } |
| 385 | |
| 386 | // Set all dangling dependencies on the current module to point to the variant |
| 387 | // with given name. |
| 388 | func (mctx *mutatorContext) SetDependencyVariant(variantName string) { |
| 389 | subName := subName{ |
| 390 | mutatorName: mctx.name, |
| 391 | variantName: variantName, |
| 392 | } |
| 393 | mctx.context.convertDepsToVariant(mctx.module, subName) |
| 394 | } |
| 395 | |
| 396 | func (mctx *mutatorContext) Module() Module { |
| 397 | return mctx.module.logicModule |
| 398 | } |
| 399 | |
| 400 | // Add a dependency to the given module. The depender can be a specific variant |
| 401 | // of a module, but the dependee must be a module that only has a single variant. |
| 402 | // Does not affect the ordering of the current mutator pass, but will be ordered |
| 403 | // correctly for all future mutator passes. |
| 404 | func (mctx *mutatorContext) AddDependency(module Module, depName string) { |
| 405 | mctx.context.addDependency(mctx.context.moduleInfo[module], depName) |
| 406 | mctx.dependenciesModified = true |
| 407 | } |
| 408 | |
| 409 | func (mctx *mutatorContext) VisitDirectDeps(visit func(Module)) { |
| 410 | mctx.context.visitDirectDeps(mctx.module, visit) |
| 411 | } |
| 412 | |
| 413 | func (mctx *mutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 414 | mctx.context.visitDirectDepsIf(mctx.module, pred, visit) |
| 415 | } |
| 416 | |
| 417 | func (mctx *mutatorContext) VisitDepsDepthFirst(visit func(Module)) { |
| 418 | mctx.context.visitDepsDepthFirst(mctx.module, visit) |
| 419 | } |
| 420 | |
| 421 | func (mctx *mutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
| 422 | visit func(Module)) { |
| 423 | |
| 424 | mctx.context.visitDepsDepthFirstIf(mctx.module, pred, visit) |
| 425 | } |