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 |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 37 | // 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 Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 42 | // |
| 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 Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 79 | // |
| 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 86 | type Module interface { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 87 | // 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 90 | GenerateBuildActions(ModuleContext) |
| 91 | } |
| 92 | |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 93 | // 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 Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 97 | // |
| 98 | // Deprecated, use a BottomUpMutator instead |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 99 | type 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 Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 110 | type BaseModuleContext interface { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 111 | ModuleName() string |
| 112 | ModuleDir() string |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 113 | Config() interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 114 | |
David Allison | 701fbad | 2014-10-29 14:51:13 -0700 | [diff] [blame] | 115 | ContainsProperty(name string) bool |
Jamie Gennis | 6a40c19 | 2014-07-02 16:40:31 -0700 | [diff] [blame] | 116 | Errorf(pos scanner.Position, fmt string, args ...interface{}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 117 | ModuleErrorf(fmt string, args ...interface{}) |
| 118 | PropertyErrorf(property, fmt string, args ...interface{}) |
Jamie Gennis | 6a40c19 | 2014-07-02 16:40:31 -0700 | [diff] [blame] | 119 | Failed() bool |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 120 | |
| 121 | moduleInfo() *moduleInfo |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 124 | type DynamicDependerModuleContext BottomUpMutatorContext |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 125 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 126 | type ModuleContext interface { |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 127 | BaseModuleContext |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 128 | |
| 129 | OtherModuleName(m Module) string |
| 130 | OtherModuleErrorf(m Module, fmt string, args ...interface{}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 131 | |
Colin Cross | c7ffa30 | 2015-02-10 11:24:52 -0800 | [diff] [blame] | 132 | VisitDirectDeps(visit func(Module)) |
| 133 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 134 | VisitDepsDepthFirst(visit func(Module)) |
| 135 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) |
Yuchen Wu | 222e245 | 2015-10-06 14:03:27 -0700 | [diff] [blame] | 136 | WalkDeps(visit func(Module, Module) bool) |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 137 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 138 | ModuleSubDir() string |
| 139 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 140 | Variable(pctx PackageContext, name, value string) |
| 141 | Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule |
| 142 | Build(pctx PackageContext, params BuildParams) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 143 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 144 | AddNinjaFileDeps(deps ...string) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 145 | |
| 146 | PrimaryModule() Module |
Colin Cross | 80ad04d | 2015-01-06 16:19:59 -0800 | [diff] [blame] | 147 | FinalModule() Module |
| 148 | VisitAllModuleVariants(visit func(Module)) |
Colin Cross | 036a1df | 2015-12-17 15:49:30 -0800 | [diff] [blame^] | 149 | |
| 150 | GetMissingDependencies() []string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 153 | var _ BaseModuleContext = (*baseModuleContext)(nil) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 154 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 155 | type baseModuleContext struct { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 156 | context *Context |
| 157 | config interface{} |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 158 | module *moduleInfo |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 159 | errs []error |
| 160 | } |
| 161 | |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 162 | func (d *baseModuleContext) moduleInfo() *moduleInfo { |
| 163 | return d.module |
| 164 | } |
| 165 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 166 | func (d *baseModuleContext) ModuleName() string { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 167 | return d.module.properties.Name |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 170 | func (d *baseModuleContext) ContainsProperty(name string) bool { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 171 | _, ok := d.module.propertyPos[name] |
David Allison | 701fbad | 2014-10-29 14:51:13 -0700 | [diff] [blame] | 172 | return ok |
| 173 | } |
| 174 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 175 | func (d *baseModuleContext) ModuleDir() string { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 176 | return filepath.Dir(d.module.relBlueprintsFile) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 179 | func (d *baseModuleContext) Config() interface{} { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 180 | return d.config |
| 181 | } |
| 182 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 183 | func (d *baseModuleContext) Errorf(pos scanner.Position, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 184 | format string, args ...interface{}) { |
| 185 | |
| 186 | d.errs = append(d.errs, &Error{ |
| 187 | Err: fmt.Errorf(format, args...), |
| 188 | Pos: pos, |
| 189 | }) |
| 190 | } |
| 191 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 192 | func (d *baseModuleContext) ModuleErrorf(format string, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 193 | args ...interface{}) { |
| 194 | |
| 195 | d.errs = append(d.errs, &Error{ |
| 196 | Err: fmt.Errorf(format, args...), |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 197 | Pos: d.module.pos, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 198 | }) |
| 199 | } |
| 200 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 201 | func (d *baseModuleContext) PropertyErrorf(property, format string, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 202 | args ...interface{}) { |
| 203 | |
Colin Cross | a2ca92c | 2015-11-02 16:10:23 -0800 | [diff] [blame] | 204 | pos := d.module.propertyPos[property] |
| 205 | |
| 206 | if !pos.IsValid() { |
| 207 | pos = d.module.pos |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Colin Cross | a2ca92c | 2015-11-02 16:10:23 -0800 | [diff] [blame] | 210 | format = property + ": " + format |
| 211 | |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 212 | d.errs = append(d.errs, &Error{ |
| 213 | Err: fmt.Errorf(format, args...), |
| 214 | Pos: pos, |
| 215 | }) |
| 216 | } |
| 217 | |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 218 | func (d *baseModuleContext) Failed() bool { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 219 | return len(d.errs) > 0 |
| 220 | } |
| 221 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 222 | var _ ModuleContext = (*moduleContext)(nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 223 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 224 | type moduleContext struct { |
Colin Cross | be1a9a1 | 2014-12-18 11:05:45 -0800 | [diff] [blame] | 225 | baseModuleContext |
Colin Cross | 036a1df | 2015-12-17 15:49:30 -0800 | [diff] [blame^] | 226 | scope *localScope |
| 227 | ninjaFileDeps []string |
| 228 | actionDefs localBuildActions |
| 229 | handledMissingDeps bool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 232 | func (m *moduleContext) OtherModuleName(logicModule Module) string { |
| 233 | module := m.context.moduleInfo[logicModule] |
| 234 | return module.properties.Name |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 237 | func (m *moduleContext) OtherModuleErrorf(logicModule Module, format string, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 238 | args ...interface{}) { |
| 239 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 240 | module := m.context.moduleInfo[logicModule] |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 241 | m.errs = append(m.errs, &Error{ |
| 242 | Err: fmt.Errorf(format, args...), |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 243 | Pos: module.pos, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 244 | }) |
| 245 | } |
| 246 | |
Colin Cross | c7ffa30 | 2015-02-10 11:24:52 -0800 | [diff] [blame] | 247 | func (m *moduleContext) VisitDirectDeps(visit func(Module)) { |
| 248 | m.context.visitDirectDeps(m.module, visit) |
| 249 | } |
| 250 | |
| 251 | func (m *moduleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 252 | m.context.visitDirectDepsIf(m.module, pred, visit) |
| 253 | } |
| 254 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 255 | func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) { |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 256 | m.context.visitDepsDepthFirst(m.module, visit) |
| 257 | } |
| 258 | |
Colin Cross | 1455a0f | 2014-12-17 13:23:56 -0800 | [diff] [blame] | 259 | func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
Colin Cross | b2e7b5d | 2014-11-11 14:18:53 -0800 | [diff] [blame] | 260 | visit func(Module)) { |
| 261 | |
| 262 | m.context.visitDepsDepthFirstIf(m.module, pred, visit) |
| 263 | } |
| 264 | |
Yuchen Wu | 222e245 | 2015-10-06 14:03:27 -0700 | [diff] [blame] | 265 | func (m *moduleContext) WalkDeps(visit func(Module, Module) bool) { |
| 266 | m.context.walkDeps(m.module, visit) |
| 267 | } |
| 268 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 269 | func (m *moduleContext) ModuleSubDir() string { |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 270 | return m.module.variantName |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 273 | func (m *moduleContext) Variable(pctx PackageContext, name, value string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 274 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 275 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 276 | v, err := m.scope.AddLocalVariable(name, value) |
| 277 | if err != nil { |
| 278 | panic(err) |
| 279 | } |
| 280 | |
| 281 | m.actionDefs.variables = append(m.actionDefs.variables, v) |
| 282 | } |
| 283 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 284 | func (m *moduleContext) Rule(pctx PackageContext, name string, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 285 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 286 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 287 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 288 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 289 | r, err := m.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 290 | if err != nil { |
| 291 | panic(err) |
| 292 | } |
| 293 | |
| 294 | m.actionDefs.rules = append(m.actionDefs.rules, r) |
| 295 | |
| 296 | return r |
| 297 | } |
| 298 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 299 | func (m *moduleContext) Build(pctx PackageContext, params BuildParams) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 300 | m.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 301 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 302 | def, err := parseBuildParams(m.scope, ¶ms) |
| 303 | if err != nil { |
| 304 | panic(err) |
| 305 | } |
| 306 | |
| 307 | m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def) |
| 308 | } |
| 309 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 310 | func (m *moduleContext) AddNinjaFileDeps(deps ...string) { |
| 311 | m.ninjaFileDeps = append(m.ninjaFileDeps, deps...) |
| 312 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 313 | |
| 314 | func (m *moduleContext) PrimaryModule() Module { |
Colin Cross | 80ad04d | 2015-01-06 16:19:59 -0800 | [diff] [blame] | 315 | return m.module.group.modules[0].logicModule |
| 316 | } |
| 317 | |
| 318 | func (m *moduleContext) FinalModule() Module { |
| 319 | return m.module.group.modules[len(m.module.group.modules)-1].logicModule |
| 320 | } |
| 321 | |
| 322 | func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) { |
| 323 | for _, module := range m.module.group.modules { |
| 324 | visit(module.logicModule) |
| 325 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Colin Cross | 036a1df | 2015-12-17 15:49:30 -0800 | [diff] [blame^] | 328 | func (m *moduleContext) GetMissingDependencies() []string { |
| 329 | m.handledMissingDeps = true |
| 330 | return m.module.missingDeps |
| 331 | } |
| 332 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 333 | // |
| 334 | // MutatorContext |
| 335 | // |
| 336 | |
| 337 | type mutatorContext struct { |
| 338 | baseModuleContext |
Colin Cross | 8d8a7af | 2015-11-03 16:41:29 -0800 | [diff] [blame] | 339 | name string |
| 340 | reverseDeps map[*moduleInfo][]*moduleInfo |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | type baseMutatorContext interface { |
| 344 | BaseModuleContext |
| 345 | |
| 346 | Module() Module |
| 347 | } |
| 348 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 349 | type EarlyMutatorContext interface { |
| 350 | baseMutatorContext |
| 351 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 352 | CreateVariations(...string) []Module |
| 353 | CreateLocalVariations(...string) []Module |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 356 | type TopDownMutatorContext interface { |
| 357 | baseMutatorContext |
| 358 | |
Colin Cross | 8d22233 | 2015-11-02 16:11:32 -0800 | [diff] [blame] | 359 | OtherModuleName(m Module) string |
| 360 | OtherModuleErrorf(m Module, fmt string, args ...interface{}) |
| 361 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 362 | VisitDirectDeps(visit func(Module)) |
| 363 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) |
| 364 | VisitDepsDepthFirst(visit func(Module)) |
| 365 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) |
Yuchen Wu | 222e245 | 2015-10-06 14:03:27 -0700 | [diff] [blame] | 366 | WalkDeps(visit func(Module, Module) bool) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | type BottomUpMutatorContext interface { |
| 370 | baseMutatorContext |
| 371 | |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 372 | AddDependency(module Module, name ...string) |
Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 373 | AddReverseDependency(module Module, name string) |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 374 | CreateVariations(...string) []Module |
Jamie Gennis | 6a5825e | 2015-05-19 11:26:11 -0700 | [diff] [blame] | 375 | CreateLocalVariations(...string) []Module |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 376 | SetDependencyVariation(string) |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 377 | AddVariationDependencies([]Variation, ...string) |
| 378 | AddFarVariationDependencies([]Variation, ...string) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // A Mutator function is called for each Module, and can use |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 382 | // MutatorContext.CreateVariations to split a Module into multiple Modules, |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 383 | // modifying properties on the new modules to differentiate them. It is called |
| 384 | // after parsing all Blueprint files, but before generating any build rules, |
| 385 | // and is always called on dependencies before being called on the depending module. |
| 386 | // |
| 387 | // The Mutator function should only modify members of properties structs, and not |
| 388 | // members of the module struct itself, to ensure the modified values are copied |
| 389 | // if a second Mutator chooses to split the module a second time. |
| 390 | type TopDownMutator func(mctx TopDownMutatorContext) |
| 391 | type BottomUpMutator func(mctx BottomUpMutatorContext) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 392 | type EarlyMutator func(mctx EarlyMutatorContext) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 393 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 394 | // Split a module into mulitple variants, one for each name in the variationNames |
| 395 | // parameter. It returns a list of new modules in the same order as the variationNames |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 396 | // list. |
| 397 | // |
| 398 | // If any of the dependencies of the module being operated on were already split |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 399 | // by calling CreateVariations with the same name, the dependency will automatically |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 400 | // be updated to point the matching variant. |
| 401 | // |
| 402 | // If a module is split, and then a module depending on the first module is not split |
| 403 | // when the Mutator is later called on it, the dependency of the depending module will |
| 404 | // automatically be updated to point to the first variant. |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 405 | func (mctx *mutatorContext) CreateVariations(variationNames ...string) []Module { |
| 406 | return mctx.createVariations(variationNames, false) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Split a module into mulitple variants, one for each name in the variantNames |
| 410 | // parameter. It returns a list of new modules in the same order as the variantNames |
| 411 | // list. |
| 412 | // |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 413 | // Local variations do not affect automatic dependency resolution - dependencies added |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 414 | // to the split module via deps or DynamicDependerModule must exactly match a variant |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 415 | // that contains all the non-local variations. |
| 416 | func (mctx *mutatorContext) CreateLocalVariations(variationNames ...string) []Module { |
| 417 | return mctx.createVariations(variationNames, true) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 420 | func (mctx *mutatorContext) createVariations(variationNames []string, local bool) []Module { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 421 | ret := []Module{} |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 422 | modules, errs := mctx.context.createVariations(mctx.module, mctx.name, variationNames) |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 423 | if len(errs) > 0 { |
| 424 | mctx.errs = append(mctx.errs, errs...) |
| 425 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 426 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 427 | for i, module := range modules { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 428 | ret = append(ret, module.logicModule) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 429 | if !local { |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 430 | module.dependencyVariant[mctx.name] = variationNames[i] |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 431 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 434 | if len(ret) != len(variationNames) { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 435 | panic("oops!") |
| 436 | } |
| 437 | |
| 438 | return ret |
| 439 | } |
| 440 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 441 | // Set all dangling dependencies on the current module to point to the variation |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 442 | // with given name. |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 443 | func (mctx *mutatorContext) SetDependencyVariation(variationName string) { |
| 444 | mctx.context.convertDepsToVariation(mctx.module, mctx.name, variationName) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | func (mctx *mutatorContext) Module() Module { |
| 448 | return mctx.module.logicModule |
| 449 | } |
| 450 | |
Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 451 | // Add a dependency to the given module. |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 452 | // Does not affect the ordering of the current mutator pass, but will be ordered |
| 453 | // correctly for all future mutator passes. |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 454 | func (mctx *mutatorContext) AddDependency(module Module, deps ...string) { |
| 455 | for _, dep := range deps { |
| 456 | errs := mctx.context.addDependency(mctx.context.moduleInfo[module], dep) |
| 457 | if len(errs) > 0 { |
| 458 | mctx.errs = append(mctx.errs, errs...) |
| 459 | } |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 460 | } |
Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Add a dependency from the destination to the given module. |
| 464 | // Does not affect the ordering of the current mutator pass, but will be ordered |
Colin Cross | 8d8a7af | 2015-11-03 16:41:29 -0800 | [diff] [blame] | 465 | // correctly for all future mutator passes. All reverse dependencies for a destination module are |
| 466 | // collected until the end of the mutator pass, sorted by name, and then appended to the destination |
| 467 | // module's dependency list. |
Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 468 | func (mctx *mutatorContext) AddReverseDependency(module Module, destName string) { |
Colin Cross | 8d8a7af | 2015-11-03 16:41:29 -0800 | [diff] [blame] | 469 | destModule, errs := mctx.context.findReverseDependency(mctx.context.moduleInfo[module], destName) |
Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 470 | if len(errs) > 0 { |
| 471 | mctx.errs = append(mctx.errs, errs...) |
Colin Cross | 8d8a7af | 2015-11-03 16:41:29 -0800 | [diff] [blame] | 472 | return |
Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 473 | } |
Colin Cross | 8d8a7af | 2015-11-03 16:41:29 -0800 | [diff] [blame] | 474 | |
| 475 | mctx.reverseDeps[destModule] = append(mctx.reverseDeps[destModule], |
| 476 | mctx.context.moduleInfo[module]) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 479 | // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations |
| 480 | // argument to select which variant of the dependency to use. A variant of the dependency must |
| 481 | // exist that matches the all of the non-local variations of the current module, plus the variations |
| 482 | // argument. |
| 483 | func (mctx *mutatorContext) AddVariationDependencies(variations []Variation, |
| 484 | deps ...string) { |
| 485 | |
| 486 | for _, dep := range deps { |
| 487 | errs := mctx.context.addVariationDependency(mctx.module, variations, dep, false) |
| 488 | if len(errs) > 0 { |
| 489 | mctx.errs = append(mctx.errs, errs...) |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the |
| 495 | // variations argument to select which variant of the dependency to use. A variant of the |
| 496 | // dependency must exist that matches the variations argument, but may also have other variations. |
| 497 | // For any unspecified variation the first variant will be used. |
| 498 | // |
| 499 | // Unlike AddVariationDependencies, the variations of the current module are ignored - the |
| 500 | // depdendency only needs to match the supplied variations. |
| 501 | func (mctx *mutatorContext) AddFarVariationDependencies(variations []Variation, |
| 502 | deps ...string) { |
| 503 | |
| 504 | for _, dep := range deps { |
| 505 | errs := mctx.context.addVariationDependency(mctx.module, variations, dep, true) |
| 506 | if len(errs) > 0 { |
| 507 | mctx.errs = append(mctx.errs, errs...) |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Colin Cross | 8d22233 | 2015-11-02 16:11:32 -0800 | [diff] [blame] | 512 | func (mctx *mutatorContext) OtherModuleName(logicModule Module) string { |
| 513 | module := mctx.context.moduleInfo[logicModule] |
| 514 | return module.properties.Name |
| 515 | } |
| 516 | |
| 517 | func (mctx *mutatorContext) OtherModuleErrorf(logicModule Module, format string, |
| 518 | args ...interface{}) { |
| 519 | |
| 520 | module := mctx.context.moduleInfo[logicModule] |
| 521 | mctx.errs = append(mctx.errs, &Error{ |
| 522 | Err: fmt.Errorf(format, args...), |
| 523 | Pos: module.pos, |
| 524 | }) |
| 525 | } |
| 526 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 527 | func (mctx *mutatorContext) VisitDirectDeps(visit func(Module)) { |
| 528 | mctx.context.visitDirectDeps(mctx.module, visit) |
| 529 | } |
| 530 | |
| 531 | func (mctx *mutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 532 | mctx.context.visitDirectDepsIf(mctx.module, pred, visit) |
| 533 | } |
| 534 | |
| 535 | func (mctx *mutatorContext) VisitDepsDepthFirst(visit func(Module)) { |
| 536 | mctx.context.visitDepsDepthFirst(mctx.module, visit) |
| 537 | } |
| 538 | |
| 539 | func (mctx *mutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
| 540 | visit func(Module)) { |
| 541 | |
| 542 | mctx.context.visitDepsDepthFirstIf(mctx.module, pred, visit) |
| 543 | } |
Yuchen Wu | 222e245 | 2015-10-06 14:03:27 -0700 | [diff] [blame] | 544 | |
| 545 | func (mctx *mutatorContext) WalkDeps(visit func(Module, Module) bool) { |
| 546 | mctx.context.walkDeps(mctx.module, visit) |
| 547 | } |