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 |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 121 | |
| 122 | AddVariantDependencies([]Variant, ...string) |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 125 | type ModuleContext interface { |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 126 | BaseModuleContext |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 127 | |
| 128 | OtherModuleName(m Module) string |
| 129 | OtherModuleErrorf(m Module, fmt string, args ...interface{}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 130 | |
Colin Cross | c7ffa30 | 2015-02-10 11:24:52 -0800 | [diff] [blame] | 131 | VisitDirectDeps(visit func(Module)) |
| 132 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 133 | VisitDepsDepthFirst(visit func(Module)) |
| 134 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 135 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 136 | ModuleSubDir() string |
| 137 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 138 | Variable(pctx *PackageContext, name, value string) |
| 139 | Rule(pctx *PackageContext, name string, params RuleParams, argNames ...string) Rule |
| 140 | Build(pctx *PackageContext, params BuildParams) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 141 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 142 | AddNinjaFileDeps(deps ...string) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 143 | |
| 144 | PrimaryModule() Module |
Colin Cross | 80ad04d | 2015-01-06 16:19:59 -0800 | [diff] [blame] | 145 | FinalModule() Module |
| 146 | VisitAllModuleVariants(visit func(Module)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 149 | var _ BaseModuleContext = (*baseModuleContext)(nil) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 150 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 151 | type baseModuleContext struct { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 152 | context *Context |
| 153 | config interface{} |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 154 | module *moduleInfo |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 155 | errs []error |
| 156 | } |
| 157 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 158 | func (d *baseModuleContext) ModuleName() string { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 159 | return d.module.properties.Name |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 162 | func (d *baseModuleContext) ContainsProperty(name string) bool { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 163 | _, ok := d.module.propertyPos[name] |
David Allison | 701fbad | 2014-10-29 14:51:13 -0700 | [diff] [blame] | 164 | return ok |
| 165 | } |
| 166 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 167 | func (d *baseModuleContext) ModuleDir() string { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 168 | return filepath.Dir(d.module.relBlueprintsFile) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 171 | func (d *baseModuleContext) Config() interface{} { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 172 | return d.config |
| 173 | } |
| 174 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 175 | func (d *baseModuleContext) Errorf(pos scanner.Position, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 176 | 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 Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 184 | func (d *baseModuleContext) ModuleErrorf(format string, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 185 | args ...interface{}) { |
| 186 | |
| 187 | d.errs = append(d.errs, &Error{ |
| 188 | Err: fmt.Errorf(format, args...), |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 189 | Pos: d.module.pos, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 190 | }) |
| 191 | } |
| 192 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 193 | func (d *baseModuleContext) PropertyErrorf(property, format string, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 194 | args ...interface{}) { |
| 195 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 196 | pos, ok := d.module.propertyPos[property] |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 197 | 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 Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 207 | func (d *baseModuleContext) Failed() bool { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 208 | return len(d.errs) > 0 |
| 209 | } |
| 210 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 211 | var _ ModuleContext = (*moduleContext)(nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 212 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 213 | type moduleContext struct { |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 214 | baseModuleContext |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 215 | scope *localScope |
| 216 | ninjaFileDeps []string |
| 217 | actionDefs localBuildActions |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 220 | func (m *moduleContext) OtherModuleName(logicModule Module) string { |
| 221 | module := m.context.moduleInfo[logicModule] |
| 222 | return module.properties.Name |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 225 | func (m *moduleContext) OtherModuleErrorf(logicModule Module, format string, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 226 | args ...interface{}) { |
| 227 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 228 | module := m.context.moduleInfo[logicModule] |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 229 | m.errs = append(m.errs, &Error{ |
| 230 | Err: fmt.Errorf(format, args...), |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 231 | Pos: module.pos, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 232 | }) |
| 233 | } |
| 234 | |
Colin Cross | c7ffa30 | 2015-02-10 11:24:52 -0800 | [diff] [blame] | 235 | func (m *moduleContext) VisitDirectDeps(visit func(Module)) { |
| 236 | m.context.visitDirectDeps(m.module, visit) |
| 237 | } |
| 238 | |
| 239 | func (m *moduleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 240 | m.context.visitDirectDepsIf(m.module, pred, visit) |
| 241 | } |
| 242 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 243 | func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) { |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 244 | m.context.visitDepsDepthFirst(m.module, visit) |
| 245 | } |
| 246 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 247 | func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 248 | visit func(Module)) { |
| 249 | |
| 250 | m.context.visitDepsDepthFirstIf(m.module, pred, visit) |
| 251 | } |
| 252 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 253 | func (m *moduleContext) ModuleSubDir() string { |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 254 | return m.module.variantName |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 257 | func (m *moduleContext) Variable(pctx *PackageContext, name, value string) { |
| 258 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 259 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 260 | 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 Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 268 | func (m *moduleContext) Rule(pctx *PackageContext, name string, |
| 269 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 270 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 271 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 272 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 273 | r, err := m.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 274 | if err != nil { |
| 275 | panic(err) |
| 276 | } |
| 277 | |
| 278 | m.actionDefs.rules = append(m.actionDefs.rules, r) |
| 279 | |
| 280 | return r |
| 281 | } |
| 282 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 283 | func (m *moduleContext) Build(pctx *PackageContext, params BuildParams) { |
| 284 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 285 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 286 | def, err := parseBuildParams(m.scope, ¶ms) |
| 287 | if err != nil { |
| 288 | panic(err) |
| 289 | } |
| 290 | |
| 291 | m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def) |
| 292 | } |
| 293 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 294 | func (m *moduleContext) AddNinjaFileDeps(deps ...string) { |
| 295 | m.ninjaFileDeps = append(m.ninjaFileDeps, deps...) |
| 296 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 297 | |
| 298 | func (m *moduleContext) PrimaryModule() Module { |
Colin Cross | 80ad04d | 2015-01-06 16:19:59 -0800 | [diff] [blame] | 299 | return m.module.group.modules[0].logicModule |
| 300 | } |
| 301 | |
| 302 | func (m *moduleContext) FinalModule() Module { |
| 303 | return m.module.group.modules[len(m.module.group.modules)-1].logicModule |
| 304 | } |
| 305 | |
| 306 | func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) { |
| 307 | for _, module := range m.module.group.modules { |
| 308 | visit(module.logicModule) |
| 309 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 313 | // DynamicDependerModuleContext |
| 314 | // |
| 315 | |
| 316 | type dynamicDependerModuleContext struct { |
| 317 | baseModuleContext |
| 318 | |
| 319 | module *moduleInfo |
| 320 | } |
| 321 | |
| 322 | func (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 Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 332 | // MutatorContext |
| 333 | // |
| 334 | |
| 335 | type mutatorContext struct { |
| 336 | baseModuleContext |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 337 | name string |
| 338 | dependenciesModified bool |
| 339 | } |
| 340 | |
| 341 | type baseMutatorContext interface { |
| 342 | BaseModuleContext |
| 343 | |
| 344 | Module() Module |
| 345 | } |
| 346 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 347 | type EarlyMutatorContext interface { |
| 348 | baseMutatorContext |
| 349 | |
| 350 | CreateVariants(...string) []Module |
| 351 | CreateLocalVariants(...string) []Module |
| 352 | } |
| 353 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 354 | type 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 | |
| 363 | type 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. |
| 380 | type TopDownMutator func(mctx TopDownMutatorContext) |
| 381 | type BottomUpMutator func(mctx BottomUpMutatorContext) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 382 | type EarlyMutator func(mctx EarlyMutatorContext) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 383 | |
| 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. |
| 395 | func (mctx *mutatorContext) CreateVariants(variantNames ...string) []Module { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 396 | 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. |
| 406 | func (mctx *mutatorContext) CreateLocalVariants(variantNames ...string) []Module { |
| 407 | return mctx.createVariants(variantNames, true) |
| 408 | } |
| 409 | |
| 410 | func (mctx *mutatorContext) createVariants(variantNames []string, local bool) []Module { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 411 | ret := []Module{} |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 412 | modules, errs := mctx.context.createVariants(mctx.module, mctx.name, variantNames) |
| 413 | if len(errs) > 0 { |
| 414 | mctx.errs = append(mctx.errs, errs...) |
| 415 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 416 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 417 | for i, module := range modules { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 418 | ret = append(ret, module.logicModule) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 419 | if !local { |
| 420 | module.dependencyVariants[mctx.name] = variantNames[i] |
| 421 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 422 | } |
| 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. |
| 433 | func (mctx *mutatorContext) SetDependencyVariant(variantName string) { |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 434 | mctx.context.convertDepsToVariant(mctx.module, mctx.name, variantName) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | func (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. |
| 445 | func (mctx *mutatorContext) AddDependency(module Module, depName string) { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame^] | 446 | errs := mctx.context.addDependency(mctx.context.moduleInfo[module], depName) |
| 447 | if len(errs) > 0 { |
| 448 | mctx.errs = append(mctx.errs, errs...) |
| 449 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 450 | mctx.dependenciesModified = true |
| 451 | } |
| 452 | |
| 453 | func (mctx *mutatorContext) VisitDirectDeps(visit func(Module)) { |
| 454 | mctx.context.visitDirectDeps(mctx.module, visit) |
| 455 | } |
| 456 | |
| 457 | func (mctx *mutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 458 | mctx.context.visitDirectDepsIf(mctx.module, pred, visit) |
| 459 | } |
| 460 | |
| 461 | func (mctx *mutatorContext) VisitDepsDepthFirst(visit func(Module)) { |
| 462 | mctx.context.visitDepsDepthFirst(mctx.module, visit) |
| 463 | } |
| 464 | |
| 465 | func (mctx *mutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
| 466 | visit func(Module)) { |
| 467 | |
| 468 | mctx.context.visitDepsDepthFirstIf(mctx.module, pred, visit) |
| 469 | } |