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 ( |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 18 | "bytes" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "reflect" |
Romain Guy | 2852965 | 2014-08-12 17:50:11 -0700 | [diff] [blame] | 25 | "runtime" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 26 | "sort" |
Colin Cross | 6134a5c | 2015-02-10 11:26:26 -0800 | [diff] [blame] | 27 | "strconv" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 28 | "strings" |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 29 | "sync/atomic" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 30 | "text/scanner" |
| 31 | "text/template" |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 32 | |
| 33 | "github.com/google/blueprint/parser" |
| 34 | "github.com/google/blueprint/pathtools" |
| 35 | "github.com/google/blueprint/proptools" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | var ErrBuildActionsNotReady = errors.New("build actions are not ready") |
| 39 | |
| 40 | const maxErrors = 10 |
| 41 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 42 | // A Context contains all the state needed to parse a set of Blueprints files |
| 43 | // and generate a Ninja file. The process of generating a Ninja file proceeds |
| 44 | // through a series of four phases. Each phase corresponds with a some methods |
| 45 | // on the Context object |
| 46 | // |
| 47 | // Phase Methods |
| 48 | // ------------ ------------------------------------------- |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 49 | // 1. Registration RegisterModuleType, RegisterSingletonType |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 50 | // |
| 51 | // 2. Parse ParseBlueprintsFiles, Parse |
| 52 | // |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 53 | // 3. Generate ResolveDependencies, PrepareBuildActions |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 54 | // |
| 55 | // 4. Write WriteBuildFile |
| 56 | // |
| 57 | // The registration phase prepares the context to process Blueprints files |
| 58 | // containing various types of modules. The parse phase reads in one or more |
| 59 | // Blueprints files and validates their contents against the module types that |
| 60 | // have been registered. The generate phase then analyzes the parsed Blueprints |
| 61 | // contents to create an internal representation for the build actions that must |
| 62 | // be performed. This phase also performs validation of the module dependencies |
| 63 | // and property values defined in the parsed Blueprints files. Finally, the |
| 64 | // write phase generates the Ninja manifest text based on the generated build |
| 65 | // actions. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 66 | type Context struct { |
| 67 | // set at instantiation |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 68 | moduleFactories map[string]ModuleFactory |
| 69 | moduleGroups map[string]*moduleGroup |
| 70 | moduleInfo map[Module]*moduleInfo |
| 71 | modulesSorted []*moduleInfo |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 72 | singletonInfo []*singletonInfo |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 73 | mutatorInfo []*mutatorInfo |
| 74 | earlyMutatorInfo []*earlyMutatorInfo |
| 75 | variantMutatorNames []string |
| 76 | moduleNinjaNames map[string]*moduleGroup |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 77 | |
| 78 | dependenciesReady bool // set to true on a successful ResolveDependencies |
| 79 | buildActionsReady bool // set to true on a successful PrepareBuildActions |
| 80 | |
| 81 | // set by SetIgnoreUnknownModuleTypes |
| 82 | ignoreUnknownModuleTypes bool |
| 83 | |
| 84 | // set during PrepareBuildActions |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 85 | pkgNames map[*PackageContext]string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 86 | globalVariables map[Variable]*ninjaString |
| 87 | globalPools map[Pool]*poolDef |
| 88 | globalRules map[Rule]*ruleDef |
| 89 | |
| 90 | // set during PrepareBuildActions |
| 91 | buildDir *ninjaString // The builddir special Ninja variable |
| 92 | requiredNinjaMajor int // For the ninja_required_version variable |
| 93 | requiredNinjaMinor int // For the ninja_required_version variable |
| 94 | requiredNinjaMicro int // For the ninja_required_version variable |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 95 | |
| 96 | // set lazily by sortedModuleNames |
| 97 | cachedSortedModuleNames []string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 100 | // An Error describes a problem that was encountered that is related to a |
| 101 | // particular location in a Blueprints file. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 102 | type Error struct { |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 103 | Err error // the error that occurred |
| 104 | Pos scanner.Position // the relevant Blueprints file location |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | type localBuildActions struct { |
| 108 | variables []*localVariable |
| 109 | rules []*localRule |
| 110 | buildDefs []*buildDef |
| 111 | } |
| 112 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 113 | type moduleGroup struct { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 114 | name string |
| 115 | ninjaName string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 116 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 117 | modules []*moduleInfo |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 120 | type moduleInfo struct { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 121 | // set during Parse |
| 122 | typeName string |
| 123 | relBlueprintsFile string |
| 124 | pos scanner.Position |
| 125 | propertyPos map[string]scanner.Position |
| 126 | properties struct { |
| 127 | Name string |
| 128 | Deps []string |
| 129 | } |
| 130 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 131 | variantName string |
| 132 | variant variationMap |
| 133 | dependencyVariant variationMap |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 134 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 135 | logicModule Module |
| 136 | group *moduleGroup |
| 137 | moduleProperties []interface{} |
| 138 | |
| 139 | // set during ResolveDependencies |
| 140 | directDeps []*moduleInfo |
| 141 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 142 | // set during updateDependencies |
| 143 | reverseDeps []*moduleInfo |
| 144 | depsCount int |
| 145 | |
| 146 | // used by parallelVisitAllBottomUp |
| 147 | waitingCount int |
| 148 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 149 | // set during each runMutator |
| 150 | splitModules []*moduleInfo |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 151 | |
| 152 | // set during PrepareBuildActions |
| 153 | actionDefs localBuildActions |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 156 | // A Variation is a way that a variant of a module differs from other variants of the same module. |
| 157 | // For example, two variants of the same module might have Variation{"arch","arm"} and |
| 158 | // Variation{"arch","arm64"} |
| 159 | type Variation struct { |
| 160 | // Mutator is the axis on which this variation applies, i.e. "arch" or "link" |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 161 | Mutator string |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 162 | // Variation is the name of the variation on the axis, i.e. "arm" or "arm64" for arch, or |
| 163 | // "shared" or "static" for link. |
| 164 | Variation string |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 167 | // A variationMap stores a map of Mutator to Variation to specify a variant of a module. |
| 168 | type variationMap map[string]string |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 169 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 170 | func (vm variationMap) clone() variationMap { |
| 171 | newVm := make(variationMap) |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 172 | for k, v := range vm { |
| 173 | newVm[k] = v |
| 174 | } |
| 175 | |
| 176 | return newVm |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | 8948623 | 2015-05-08 11:14:54 -0700 | [diff] [blame] | 179 | // Compare this variationMap to another one. Returns true if the every entry in this map |
| 180 | // is either the same in the other map or doesn't exist in the other map. |
| 181 | func (vm variationMap) subset(other variationMap) bool { |
| 182 | for k, v1 := range vm { |
| 183 | if v2, ok := other[k]; ok && v1 != v2 { |
| 184 | return false |
| 185 | } |
| 186 | } |
| 187 | return true |
| 188 | } |
| 189 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 190 | func (vm variationMap) equal(other variationMap) bool { |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 191 | return reflect.DeepEqual(vm, other) |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 194 | type singletonInfo struct { |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 195 | // set during RegisterSingletonType |
| 196 | factory SingletonFactory |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 197 | singleton Singleton |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 198 | name string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 199 | |
| 200 | // set during PrepareBuildActions |
| 201 | actionDefs localBuildActions |
| 202 | } |
| 203 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 204 | type mutatorInfo struct { |
| 205 | // set during RegisterMutator |
Colin Cross | c0dbc55 | 2015-01-02 15:19:28 -0800 | [diff] [blame] | 206 | topDownMutator TopDownMutator |
| 207 | bottomUpMutator BottomUpMutator |
| 208 | name string |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 211 | type earlyMutatorInfo struct { |
| 212 | // set during RegisterEarlyMutator |
| 213 | mutator EarlyMutator |
| 214 | name string |
| 215 | } |
| 216 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 217 | func (e *Error) Error() string { |
| 218 | |
| 219 | return fmt.Sprintf("%s: %s", e.Pos, e.Err) |
| 220 | } |
| 221 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 222 | // NewContext creates a new Context object. The created context initially has |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 223 | // no module or singleton factories registered, so the RegisterModuleFactory and |
| 224 | // RegisterSingletonFactory methods must be called before it can do anything |
| 225 | // useful. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 226 | func NewContext() *Context { |
| 227 | return &Context{ |
Colin Cross | 6134a5c | 2015-02-10 11:26:26 -0800 | [diff] [blame] | 228 | moduleFactories: make(map[string]ModuleFactory), |
| 229 | moduleGroups: make(map[string]*moduleGroup), |
| 230 | moduleInfo: make(map[Module]*moduleInfo), |
Colin Cross | 6134a5c | 2015-02-10 11:26:26 -0800 | [diff] [blame] | 231 | moduleNinjaNames: make(map[string]*moduleGroup), |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 235 | // A ModuleFactory function creates a new Module object. See the |
| 236 | // Context.RegisterModuleType method for details about how a registered |
| 237 | // ModuleFactory is used by a Context. |
| 238 | type ModuleFactory func() (m Module, propertyStructs []interface{}) |
| 239 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 240 | // RegisterModuleType associates a module type name (which can appear in a |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 241 | // Blueprints file) with a Module factory function. When the given module type |
| 242 | // name is encountered in a Blueprints file during parsing, the Module factory |
| 243 | // is invoked to instantiate a new Module object to handle the build action |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 244 | // generation for the module. If a Mutator splits a module into multiple variants, |
| 245 | // the factory is invoked again to create a new Module for each variant. |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 246 | // |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 247 | // The module type names given here must be unique for the context. The factory |
| 248 | // function should be a named function so that its package and name can be |
| 249 | // included in the generated Ninja file for debugging purposes. |
| 250 | // |
| 251 | // The factory function returns two values. The first is the newly created |
| 252 | // Module object. The second is a slice of pointers to that Module object's |
| 253 | // properties structs. Each properties struct is examined when parsing a module |
| 254 | // definition of this type in a Blueprints file. Exported fields of the |
| 255 | // properties structs are automatically set to the property values specified in |
| 256 | // the Blueprints file. The properties struct field names determine the name of |
| 257 | // the Blueprints file properties that are used - the Blueprints property name |
| 258 | // matches that of the properties struct field name with the first letter |
| 259 | // converted to lower-case. |
| 260 | // |
| 261 | // The fields of the properties struct must be either []string, a string, or |
| 262 | // bool. The Context will panic if a Module gets instantiated with a properties |
| 263 | // struct containing a field that is not one these supported types. |
| 264 | // |
| 265 | // Any properties that appear in the Blueprints files that are not built-in |
| 266 | // module properties (such as "name" and "deps") and do not have a corresponding |
| 267 | // field in the returned module properties struct result in an error during the |
| 268 | // Context's parse phase. |
| 269 | // |
| 270 | // As an example, the follow code: |
| 271 | // |
| 272 | // type myModule struct { |
| 273 | // properties struct { |
| 274 | // Foo string |
| 275 | // Bar []string |
| 276 | // } |
| 277 | // } |
| 278 | // |
| 279 | // func NewMyModule() (blueprint.Module, []interface{}) { |
| 280 | // module := new(myModule) |
| 281 | // properties := &module.properties |
| 282 | // return module, []interface{}{properties} |
| 283 | // } |
| 284 | // |
| 285 | // func main() { |
| 286 | // ctx := blueprint.NewContext() |
| 287 | // ctx.RegisterModuleType("my_module", NewMyModule) |
| 288 | // // ... |
| 289 | // } |
| 290 | // |
| 291 | // would support parsing a module defined in a Blueprints file as follows: |
| 292 | // |
| 293 | // my_module { |
| 294 | // name: "myName", |
| 295 | // foo: "my foo string", |
| 296 | // bar: ["my", "bar", "strings"], |
| 297 | // } |
| 298 | // |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 299 | // The factory function may be called from multiple goroutines. Any accesses |
| 300 | // to global variables must be synchronized. |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 301 | func (c *Context) RegisterModuleType(name string, factory ModuleFactory) { |
| 302 | if _, present := c.moduleFactories[name]; present { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 303 | panic(errors.New("module type name is already registered")) |
| 304 | } |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 305 | c.moduleFactories[name] = factory |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 308 | // A SingletonFactory function creates a new Singleton object. See the |
| 309 | // Context.RegisterSingletonType method for details about how a registered |
| 310 | // SingletonFactory is used by a Context. |
| 311 | type SingletonFactory func() Singleton |
| 312 | |
| 313 | // RegisterSingletonType registers a singleton type that will be invoked to |
| 314 | // generate build actions. Each registered singleton type is instantiated and |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 315 | // and invoked exactly once as part of the generate phase. Each registered |
| 316 | // singleton is invoked in registration order. |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 317 | // |
| 318 | // The singleton type names given here must be unique for the context. The |
| 319 | // factory function should be a named function so that its package and name can |
| 320 | // be included in the generated Ninja file for debugging purposes. |
| 321 | func (c *Context) RegisterSingletonType(name string, factory SingletonFactory) { |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 322 | for _, s := range c.singletonInfo { |
| 323 | if s.name == name { |
| 324 | panic(errors.New("singleton name is already registered")) |
| 325 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 326 | } |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 327 | |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 328 | c.singletonInfo = append(c.singletonInfo, &singletonInfo{ |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 329 | factory: factory, |
| 330 | singleton: factory(), |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 331 | name: name, |
| 332 | }) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | func singletonPkgPath(singleton Singleton) string { |
| 336 | typ := reflect.TypeOf(singleton) |
| 337 | for typ.Kind() == reflect.Ptr { |
| 338 | typ = typ.Elem() |
| 339 | } |
| 340 | return typ.PkgPath() |
| 341 | } |
| 342 | |
| 343 | func singletonTypeName(singleton Singleton) string { |
| 344 | typ := reflect.TypeOf(singleton) |
| 345 | for typ.Kind() == reflect.Ptr { |
| 346 | typ = typ.Elem() |
| 347 | } |
| 348 | return typ.PkgPath() + "." + typ.Name() |
| 349 | } |
| 350 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 351 | // RegisterTopDownMutator registers a mutator that will be invoked to propagate |
| 352 | // dependency info top-down between Modules. Each registered mutator |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 353 | // is invoked in registration order (mixing TopDownMutators and BottomUpMutators) |
| 354 | // once per Module, and is invoked on a module before being invoked on any of its |
| 355 | // dependencies. |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 356 | // |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 357 | // The mutator type names given here must be unique to all top down mutators in |
| 358 | // the Context. |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 359 | func (c *Context) RegisterTopDownMutator(name string, mutator TopDownMutator) { |
| 360 | for _, m := range c.mutatorInfo { |
| 361 | if m.name == name && m.topDownMutator != nil { |
| 362 | panic(fmt.Errorf("mutator name %s is already registered", name)) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | c.mutatorInfo = append(c.mutatorInfo, &mutatorInfo{ |
| 367 | topDownMutator: mutator, |
Colin Cross | c0dbc55 | 2015-01-02 15:19:28 -0800 | [diff] [blame] | 368 | name: name, |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 369 | }) |
| 370 | } |
| 371 | |
| 372 | // RegisterBottomUpMutator registers a mutator that will be invoked to split |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 373 | // Modules into variants. Each registered mutator is invoked in registration |
| 374 | // order (mixing TopDownMutators and BottomUpMutators) once per Module, and is |
| 375 | // invoked on dependencies before being invoked on dependers. |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 376 | // |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 377 | // The mutator type names given here must be unique to all bottom up or early |
| 378 | // mutators in the Context. |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 379 | func (c *Context) RegisterBottomUpMutator(name string, mutator BottomUpMutator) { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 380 | for _, m := range c.variantMutatorNames { |
| 381 | if m == name { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 382 | panic(fmt.Errorf("mutator name %s is already registered", name)) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | c.mutatorInfo = append(c.mutatorInfo, &mutatorInfo{ |
| 387 | bottomUpMutator: mutator, |
Colin Cross | c0dbc55 | 2015-01-02 15:19:28 -0800 | [diff] [blame] | 388 | name: name, |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 389 | }) |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 390 | |
| 391 | c.variantMutatorNames = append(c.variantMutatorNames, name) |
| 392 | } |
| 393 | |
| 394 | // RegisterEarlyMutator registers a mutator that will be invoked to split |
| 395 | // Modules into multiple variant Modules before any dependencies have been |
| 396 | // created. Each registered mutator is invoked in registration order once |
| 397 | // per Module (including each variant from previous early mutators). Module |
| 398 | // order is unpredictable. |
| 399 | // |
| 400 | // In order for dependencies to be satisifed in a later pass, all dependencies |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 401 | // of a module either must have an identical variant or must have no variations. |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 402 | // |
| 403 | // The mutator type names given here must be unique to all bottom up or early |
| 404 | // mutators in the Context. |
| 405 | func (c *Context) RegisterEarlyMutator(name string, mutator EarlyMutator) { |
| 406 | for _, m := range c.variantMutatorNames { |
| 407 | if m == name { |
| 408 | panic(fmt.Errorf("mutator name %s is already registered", name)) |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | c.earlyMutatorInfo = append(c.earlyMutatorInfo, &earlyMutatorInfo{ |
| 413 | mutator: mutator, |
| 414 | name: name, |
| 415 | }) |
| 416 | |
| 417 | c.variantMutatorNames = append(c.variantMutatorNames, name) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 420 | // SetIgnoreUnknownModuleTypes sets the behavior of the context in the case |
| 421 | // where it encounters an unknown module type while parsing Blueprints files. By |
| 422 | // default, the context will report unknown module types as an error. If this |
| 423 | // method is called with ignoreUnknownModuleTypes set to true then the context |
| 424 | // will silently ignore unknown module types. |
| 425 | // |
| 426 | // This method should generally not be used. It exists to facilitate the |
| 427 | // bootstrapping process. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 428 | func (c *Context) SetIgnoreUnknownModuleTypes(ignoreUnknownModuleTypes bool) { |
| 429 | c.ignoreUnknownModuleTypes = ignoreUnknownModuleTypes |
| 430 | } |
| 431 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 432 | // Parse parses a single Blueprints file from r, creating Module objects for |
| 433 | // each of the module definitions encountered. If the Blueprints file contains |
| 434 | // an assignment to the "subdirs" variable, then the subdirectories listed are |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 435 | // searched for Blueprints files returned in the subBlueprints return value. |
| 436 | // If the Blueprints file contains an assignment to the "build" variable, then |
| 437 | // the file listed are returned in the subBlueprints return value. |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 438 | // |
| 439 | // rootDir specifies the path to the root directory of the source tree, while |
| 440 | // filename specifies the path to the Blueprints file. These paths are used for |
| 441 | // error reporting and for determining the module's directory. |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 442 | func (c *Context) parse(rootDir, filename string, r io.Reader, |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 443 | scope *parser.Scope) (file *parser.File, subBlueprints []stringAndScope, deps []string, |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 444 | errs []error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 445 | |
Jamie Gennis | ec70128 | 2014-06-12 20:06:31 -0700 | [diff] [blame] | 446 | relBlueprintsFile, err := filepath.Rel(rootDir, filename) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 447 | if err != nil { |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 448 | return nil, nil, nil, []error{err} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Colin Cross | c0dbc55 | 2015-01-02 15:19:28 -0800 | [diff] [blame] | 451 | scope = parser.NewScope(scope) |
| 452 | scope.Remove("subdirs") |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 453 | scope.Remove("build") |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 454 | file, errs = parser.ParseAndEval(filename, r, scope) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 455 | if len(errs) > 0 { |
| 456 | for i, err := range errs { |
| 457 | if parseErr, ok := err.(*parser.ParseError); ok { |
| 458 | err = &Error{ |
| 459 | Err: parseErr.Err, |
| 460 | Pos: parseErr.Pos, |
| 461 | } |
| 462 | errs[i] = err |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // If there were any parse errors don't bother trying to interpret the |
| 467 | // result. |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 468 | return nil, nil, nil, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 469 | } |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 470 | file.Name = relBlueprintsFile |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 471 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 472 | subdirs, subdirsPos, err := getStringListFromScope(scope, "subdirs") |
| 473 | if err != nil { |
| 474 | errs = append(errs, err) |
| 475 | } |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 476 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 477 | build, buildPos, err := getStringListFromScope(scope, "build") |
| 478 | if err != nil { |
| 479 | errs = append(errs, err) |
| 480 | } |
| 481 | |
Colin Cross | 2939422 | 2015-04-27 13:18:21 -0700 | [diff] [blame] | 482 | subBlueprintsName, _, err := getStringFromScope(scope, "subname") |
| 483 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 484 | blueprints, deps, newErrs := c.findSubdirBlueprints(filepath.Dir(filename), subdirs, build, |
Colin Cross | 2939422 | 2015-04-27 13:18:21 -0700 | [diff] [blame] | 485 | subBlueprintsName, subdirsPos, buildPos) |
Colin Cross | c0dbc55 | 2015-01-02 15:19:28 -0800 | [diff] [blame] | 486 | if len(newErrs) > 0 { |
| 487 | errs = append(errs, newErrs...) |
| 488 | } |
| 489 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 490 | subBlueprintsAndScope := make([]stringAndScope, len(blueprints)) |
| 491 | for i, b := range blueprints { |
| 492 | subBlueprintsAndScope[i] = stringAndScope{b, scope} |
| 493 | } |
| 494 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 495 | return file, subBlueprintsAndScope, deps, errs |
Colin Cross | c0dbc55 | 2015-01-02 15:19:28 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 498 | type stringAndScope struct { |
| 499 | string |
| 500 | *parser.Scope |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 503 | // ParseBlueprintsFiles parses a set of Blueprints files starting with the file |
| 504 | // at rootFile. When it encounters a Blueprints file with a set of subdirs |
| 505 | // listed it recursively parses any Blueprints files found in those |
| 506 | // subdirectories. |
| 507 | // |
| 508 | // If no errors are encountered while parsing the files, the list of paths on |
| 509 | // which the future output will depend is returned. This list will include both |
| 510 | // Blueprints file paths as well as directory paths for cases where wildcard |
| 511 | // subdirs are found. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 512 | func (c *Context) ParseBlueprintsFiles(rootFile string) (deps []string, |
| 513 | errs []error) { |
| 514 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 515 | c.dependenciesReady = false |
| 516 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 517 | moduleCh := make(chan *moduleInfo) |
| 518 | errsCh := make(chan []error) |
| 519 | doneCh := make(chan struct{}) |
| 520 | var numErrs uint32 |
| 521 | var numGoroutines int32 |
| 522 | |
| 523 | // handler must be reentrant |
| 524 | handler := func(file *parser.File) { |
| 525 | if atomic.LoadUint32(&numErrs) > maxErrors { |
| 526 | return |
| 527 | } |
| 528 | |
| 529 | atomic.AddInt32(&numGoroutines, 1) |
| 530 | go func() { |
| 531 | for _, def := range file.Defs { |
| 532 | var module *moduleInfo |
| 533 | var errs []error |
| 534 | switch def := def.(type) { |
| 535 | case *parser.Module: |
| 536 | module, errs = c.processModuleDef(def, file.Name) |
| 537 | case *parser.Assignment: |
| 538 | // Already handled via Scope object |
| 539 | default: |
| 540 | panic("unknown definition type") |
| 541 | } |
| 542 | |
| 543 | if len(errs) > 0 { |
| 544 | atomic.AddUint32(&numErrs, uint32(len(errs))) |
| 545 | errsCh <- errs |
| 546 | } else if module != nil { |
| 547 | moduleCh <- module |
| 548 | } |
| 549 | } |
| 550 | doneCh <- struct{}{} |
| 551 | }() |
| 552 | } |
| 553 | |
| 554 | atomic.AddInt32(&numGoroutines, 1) |
| 555 | go func() { |
| 556 | var errs []error |
| 557 | deps, errs = c.WalkBlueprintsFiles(rootFile, handler) |
| 558 | if len(errs) > 0 { |
| 559 | errsCh <- errs |
| 560 | } |
| 561 | doneCh <- struct{}{} |
| 562 | }() |
| 563 | |
| 564 | loop: |
| 565 | for { |
| 566 | select { |
| 567 | case newErrs := <-errsCh: |
| 568 | errs = append(errs, newErrs...) |
| 569 | case module := <-moduleCh: |
| 570 | newErrs := c.addModule(module) |
| 571 | if len(newErrs) > 0 { |
| 572 | errs = append(errs, newErrs...) |
| 573 | } |
| 574 | case <-doneCh: |
| 575 | n := atomic.AddInt32(&numGoroutines, -1) |
| 576 | if n == 0 { |
| 577 | break loop |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return deps, errs |
| 583 | } |
| 584 | |
| 585 | type FileHandler func(*parser.File) |
| 586 | |
| 587 | // Walk a set of Blueprints files starting with the file at rootFile, calling handler on each. |
| 588 | // When it encounters a Blueprints file with a set of subdirs listed it recursively parses any |
| 589 | // Blueprints files found in those subdirectories. handler will be called from a goroutine, so |
| 590 | // it must be reentrant. |
| 591 | // |
| 592 | // If no errors are encountered while parsing the files, the list of paths on |
| 593 | // which the future output will depend is returned. This list will include both |
| 594 | // Blueprints file paths as well as directory paths for cases where wildcard |
| 595 | // subdirs are found. |
| 596 | func (c *Context) WalkBlueprintsFiles(rootFile string, handler FileHandler) (deps []string, |
| 597 | errs []error) { |
| 598 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 599 | rootDir := filepath.Dir(rootFile) |
| 600 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 601 | blueprintsSet := make(map[string]bool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 602 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 603 | // Channels to receive data back from parseBlueprintsFile goroutines |
| 604 | blueprintsCh := make(chan stringAndScope) |
| 605 | errsCh := make(chan []error) |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 606 | fileCh := make(chan *parser.File) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 607 | depsCh := make(chan string) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 608 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 609 | // Channel to notify main loop that a parseBlueprintsFile goroutine has finished |
| 610 | doneCh := make(chan struct{}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 611 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 612 | // Number of outstanding goroutines to wait for |
| 613 | count := 0 |
| 614 | |
| 615 | startParseBlueprintsFile := func(filename string, scope *parser.Scope) { |
| 616 | count++ |
| 617 | go func() { |
| 618 | c.parseBlueprintsFile(filename, scope, rootDir, |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 619 | errsCh, fileCh, blueprintsCh, depsCh) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 620 | doneCh <- struct{}{} |
| 621 | }() |
| 622 | } |
| 623 | |
| 624 | tooManyErrors := false |
| 625 | |
| 626 | startParseBlueprintsFile(rootFile, nil) |
| 627 | |
| 628 | loop: |
| 629 | for { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 630 | if len(errs) > maxErrors { |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 631 | tooManyErrors = true |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 634 | select { |
| 635 | case newErrs := <-errsCh: |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 636 | errs = append(errs, newErrs...) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 637 | case dep := <-depsCh: |
| 638 | deps = append(deps, dep) |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 639 | case file := <-fileCh: |
| 640 | handler(file) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 641 | case blueprint := <-blueprintsCh: |
| 642 | if tooManyErrors { |
| 643 | continue |
| 644 | } |
| 645 | if blueprintsSet[blueprint.string] { |
| 646 | continue |
| 647 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 648 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 649 | blueprintsSet[blueprint.string] = true |
| 650 | startParseBlueprintsFile(blueprint.string, blueprint.Scope) |
| 651 | case <-doneCh: |
| 652 | count-- |
| 653 | if count == 0 { |
| 654 | break loop |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 659 | return |
| 660 | } |
| 661 | |
| 662 | // parseBlueprintFile parses a single Blueprints file, returning any errors through |
| 663 | // errsCh, any defined modules through modulesCh, any sub-Blueprints files through |
| 664 | // blueprintsCh, and any dependencies on Blueprints files or directories through |
| 665 | // depsCh. |
| 666 | func (c *Context) parseBlueprintsFile(filename string, scope *parser.Scope, rootDir string, |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 667 | errsCh chan<- []error, fileCh chan<- *parser.File, blueprintsCh chan<- stringAndScope, |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 668 | depsCh chan<- string) { |
| 669 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 670 | f, err := os.Open(filename) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 671 | if err != nil { |
| 672 | errsCh <- []error{err} |
| 673 | return |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 674 | } |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 675 | defer func() { |
| 676 | err = f.Close() |
| 677 | if err != nil { |
| 678 | errsCh <- []error{err} |
| 679 | } |
| 680 | }() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 681 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 682 | file, subBlueprints, deps, errs := c.parse(rootDir, filename, f, scope) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 683 | if len(errs) > 0 { |
| 684 | errsCh <- errs |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 685 | } else { |
| 686 | fileCh <- file |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 689 | for _, b := range subBlueprints { |
| 690 | blueprintsCh <- b |
| 691 | } |
| 692 | |
| 693 | for _, d := range deps { |
| 694 | depsCh <- d |
| 695 | } |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Colin Cross | 2939422 | 2015-04-27 13:18:21 -0700 | [diff] [blame] | 698 | func (c *Context) findSubdirBlueprints(dir string, subdirs, build []string, subBlueprintsName string, |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 699 | subdirsPos, buildPos scanner.Position) (blueprints, deps []string, errs []error) { |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 700 | |
| 701 | for _, subdir := range subdirs { |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 702 | globPattern := filepath.Join(dir, subdir) |
| 703 | matches, matchedDirs, err := pathtools.Glob(globPattern) |
| 704 | if err != nil { |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 705 | errs = append(errs, &Error{ |
| 706 | Err: fmt.Errorf("%q: %s", globPattern, err.Error()), |
| 707 | Pos: subdirsPos, |
| 708 | }) |
| 709 | continue |
| 710 | } |
| 711 | |
| 712 | if len(matches) == 0 { |
| 713 | errs = append(errs, &Error{ |
| 714 | Err: fmt.Errorf("%q: not found", globPattern), |
| 715 | Pos: subdirsPos, |
| 716 | }) |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 717 | } |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 718 | |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 719 | // Depend on all searched directories so we pick up future changes. |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 720 | deps = append(deps, matchedDirs...) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 721 | |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 722 | for _, foundSubdir := range matches { |
| 723 | fileInfo, subdirStatErr := os.Stat(foundSubdir) |
| 724 | if subdirStatErr != nil { |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 725 | errs = append(errs, subdirStatErr) |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 726 | continue |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 729 | // Skip files |
| 730 | if !fileInfo.IsDir() { |
| 731 | continue |
| 732 | } |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 733 | |
Colin Cross | 2939422 | 2015-04-27 13:18:21 -0700 | [diff] [blame] | 734 | var subBlueprints string |
| 735 | if subBlueprintsName != "" { |
| 736 | subBlueprints = filepath.Join(foundSubdir, subBlueprintsName) |
| 737 | _, err = os.Stat(subBlueprints) |
| 738 | } |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 739 | |
Colin Cross | 2939422 | 2015-04-27 13:18:21 -0700 | [diff] [blame] | 740 | if os.IsNotExist(err) || subBlueprints == "" { |
| 741 | subBlueprints = filepath.Join(foundSubdir, "Blueprints") |
| 742 | _, err = os.Stat(subBlueprints) |
| 743 | } |
| 744 | |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 745 | if os.IsNotExist(err) { |
| 746 | // There is no Blueprints file in this subdirectory. We |
| 747 | // need to add the directory to the list of dependencies |
| 748 | // so that if someone adds a Blueprints file in the |
| 749 | // future we'll pick it up. |
Jamie Gennis | 7ccc2c2 | 2015-07-06 13:11:15 -0700 | [diff] [blame] | 750 | deps = append(deps, foundSubdir) |
Michael Beardsworth | 1ec4453 | 2015-03-31 20:39:02 -0700 | [diff] [blame] | 751 | } else { |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 752 | deps = append(deps, subBlueprints) |
| 753 | blueprints = append(blueprints, subBlueprints) |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 754 | } |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 757 | |
| 758 | for _, file := range build { |
| 759 | globPattern := filepath.Join(dir, file) |
| 760 | matches, matchedDirs, err := pathtools.Glob(globPattern) |
| 761 | if err != nil { |
| 762 | errs = append(errs, &Error{ |
| 763 | Err: fmt.Errorf("%q: %s", globPattern, err.Error()), |
| 764 | Pos: buildPos, |
| 765 | }) |
| 766 | continue |
| 767 | } |
| 768 | |
| 769 | if len(matches) == 0 { |
| 770 | errs = append(errs, &Error{ |
| 771 | Err: fmt.Errorf("%q: not found", globPattern), |
| 772 | Pos: buildPos, |
| 773 | }) |
| 774 | } |
| 775 | |
| 776 | // Depend on all searched directories so we pick up future changes. |
| 777 | deps = append(deps, matchedDirs...) |
| 778 | |
| 779 | for _, foundBlueprints := range matches { |
| 780 | fileInfo, err := os.Stat(foundBlueprints) |
| 781 | if os.IsNotExist(err) { |
| 782 | errs = append(errs, &Error{ |
| 783 | Err: fmt.Errorf("%q not found", foundBlueprints), |
| 784 | }) |
| 785 | continue |
| 786 | } |
| 787 | |
| 788 | if fileInfo.IsDir() { |
| 789 | errs = append(errs, &Error{ |
| 790 | Err: fmt.Errorf("%q is a directory", foundBlueprints), |
| 791 | }) |
| 792 | continue |
| 793 | } |
| 794 | |
| 795 | blueprints = append(blueprints, foundBlueprints) |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | return blueprints, deps, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 802 | func getStringListFromScope(scope *parser.Scope, v string) ([]string, scanner.Position, error) { |
| 803 | if assignment, err := scope.Get(v); err == nil { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 804 | switch assignment.Value.Type { |
| 805 | case parser.List: |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 806 | ret := make([]string, 0, len(assignment.Value.ListValue)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 807 | |
| 808 | for _, value := range assignment.Value.ListValue { |
| 809 | if value.Type != parser.String { |
| 810 | // The parser should not produce this. |
| 811 | panic("non-string value found in list") |
| 812 | } |
| 813 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 814 | ret = append(ret, value.StringValue) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 817 | return ret, assignment.Pos, nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 818 | case parser.Bool, parser.String: |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 819 | return nil, scanner.Position{}, &Error{ |
| 820 | Err: fmt.Errorf("%q must be a list of strings", v), |
| 821 | Pos: assignment.Pos, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 822 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 823 | default: |
| 824 | panic(fmt.Errorf("unknown value type: %d", assignment.Value.Type)) |
| 825 | } |
| 826 | } |
| 827 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 828 | return nil, scanner.Position{}, nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Colin Cross | 2939422 | 2015-04-27 13:18:21 -0700 | [diff] [blame] | 831 | func getStringFromScope(scope *parser.Scope, v string) (string, scanner.Position, error) { |
| 832 | if assignment, err := scope.Get(v); err == nil { |
| 833 | switch assignment.Value.Type { |
| 834 | case parser.String: |
| 835 | return assignment.Value.StringValue, assignment.Pos, nil |
| 836 | case parser.Bool, parser.List: |
| 837 | return "", scanner.Position{}, &Error{ |
| 838 | Err: fmt.Errorf("%q must be a string", v), |
| 839 | Pos: assignment.Pos, |
| 840 | } |
| 841 | default: |
| 842 | panic(fmt.Errorf("unknown value type: %d", assignment.Value.Type)) |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return "", scanner.Position{}, nil |
| 847 | } |
| 848 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 849 | func (c *Context) createVariations(origModule *moduleInfo, mutatorName string, |
| 850 | variationNames []string) ([]*moduleInfo, []error) { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 851 | |
Colin Cross | f4d18a6 | 2015-03-18 17:43:15 -0700 | [diff] [blame] | 852 | if len(variationNames) == 0 { |
| 853 | panic(fmt.Errorf("mutator %q passed zero-length variation list for module %q", |
Jamie Gennis | 6cafc2c | 2015-03-20 22:39:29 -0400 | [diff] [blame] | 854 | mutatorName, origModule.properties.Name)) |
Colin Cross | f4d18a6 | 2015-03-18 17:43:15 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 857 | newModules := []*moduleInfo{} |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 858 | |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 859 | var errs []error |
| 860 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 861 | for i, variationName := range variationNames { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 862 | typeName := origModule.typeName |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 863 | factory, ok := c.moduleFactories[typeName] |
| 864 | if !ok { |
| 865 | panic(fmt.Sprintf("unrecognized module type %q during cloning", typeName)) |
| 866 | } |
| 867 | |
| 868 | var newLogicModule Module |
| 869 | var newProperties []interface{} |
| 870 | |
| 871 | if i == 0 { |
| 872 | // Reuse the existing module for the first new variant |
Colin Cross | 21e078a | 2015-03-16 10:57:54 -0700 | [diff] [blame] | 873 | // This both saves creating a new module, and causes the insertion in c.moduleInfo below |
| 874 | // with logicModule as the key to replace the original entry in c.moduleInfo |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 875 | newLogicModule = origModule.logicModule |
| 876 | newProperties = origModule.moduleProperties |
| 877 | } else { |
| 878 | props := []interface{}{ |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 879 | &origModule.properties, |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 880 | } |
| 881 | newLogicModule, newProperties = factory() |
| 882 | |
| 883 | newProperties = append(props, newProperties...) |
| 884 | |
| 885 | if len(newProperties) != len(origModule.moduleProperties) { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 886 | panic("mismatched properties array length in " + origModule.properties.Name) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | for i := range newProperties { |
| 890 | dst := reflect.ValueOf(newProperties[i]).Elem() |
| 891 | src := reflect.ValueOf(origModule.moduleProperties[i]).Elem() |
| 892 | |
| 893 | proptools.CopyProperties(dst, src) |
| 894 | } |
| 895 | } |
| 896 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 897 | newVariant := origModule.variant.clone() |
| 898 | newVariant[mutatorName] = variationName |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 899 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 900 | m := *origModule |
| 901 | newModule := &m |
| 902 | newModule.directDeps = append([]*moduleInfo(nil), origModule.directDeps...) |
| 903 | newModule.logicModule = newLogicModule |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 904 | newModule.variant = newVariant |
| 905 | newModule.dependencyVariant = origModule.dependencyVariant.clone() |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 906 | newModule.moduleProperties = newProperties |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 907 | |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 908 | if newModule.variantName == "" { |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 909 | newModule.variantName = variationName |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 910 | } else { |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 911 | newModule.variantName += "_" + variationName |
Colin Cross | e7daa22 | 2015-03-11 14:35:41 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 914 | newModules = append(newModules, newModule) |
Colin Cross | 21e078a | 2015-03-16 10:57:54 -0700 | [diff] [blame] | 915 | |
| 916 | // Insert the new variant into the global module map. If this is the first variant then |
| 917 | // it reuses logicModule from the original module, which causes this to replace the |
| 918 | // original module in the global module map. |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 919 | c.moduleInfo[newModule.logicModule] = newModule |
| 920 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 921 | newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName) |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 922 | if len(newErrs) > 0 { |
| 923 | errs = append(errs, newErrs...) |
| 924 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // Mark original variant as invalid. Modules that depend on this module will still |
| 928 | // depend on origModule, but we'll fix it when the mutator is called on them. |
| 929 | origModule.logicModule = nil |
| 930 | origModule.splitModules = newModules |
| 931 | |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 932 | return newModules, errs |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 935 | func (c *Context) convertDepsToVariation(module *moduleInfo, |
| 936 | mutatorName, variationName string) (errs []error) { |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 937 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 938 | for i, dep := range module.directDeps { |
| 939 | if dep.logicModule == nil { |
| 940 | var newDep *moduleInfo |
| 941 | for _, m := range dep.splitModules { |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 942 | if m.variant[mutatorName] == variationName { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 943 | newDep = m |
| 944 | break |
| 945 | } |
| 946 | } |
| 947 | if newDep == nil { |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 948 | errs = append(errs, &Error{ |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 949 | Err: fmt.Errorf("failed to find variation %q for module %q needed by %q", |
| 950 | variationName, dep.properties.Name, module.properties.Name), |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 951 | Pos: module.pos, |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 952 | }) |
| 953 | continue |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 954 | } |
| 955 | module.directDeps[i] = newDep |
| 956 | } |
| 957 | } |
Colin Cross | 174ae05 | 2015-03-03 17:37:03 -0800 | [diff] [blame] | 958 | |
| 959 | return errs |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 960 | } |
| 961 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 962 | func (c *Context) prettyPrintVariant(variant variationMap) string { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 963 | names := make([]string, 0, len(variant)) |
| 964 | for _, m := range c.variantMutatorNames { |
| 965 | if v, ok := variant[m]; ok { |
| 966 | names = append(names, m+":"+v) |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | return strings.Join(names, ", ") |
| 971 | } |
| 972 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 973 | func (c *Context) processModuleDef(moduleDef *parser.Module, |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 974 | relBlueprintsFile string) (*moduleInfo, []error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 975 | |
Colin Cross | d1facc1 | 2015-01-08 14:56:03 -0800 | [diff] [blame] | 976 | typeName := moduleDef.Type.Name |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 977 | factory, ok := c.moduleFactories[typeName] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 978 | if !ok { |
| 979 | if c.ignoreUnknownModuleTypes { |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 980 | return nil, nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 983 | return nil, []error{ |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 984 | &Error{ |
| 985 | Err: fmt.Errorf("unrecognized module type %q", typeName), |
Colin Cross | d1facc1 | 2015-01-08 14:56:03 -0800 | [diff] [blame] | 986 | Pos: moduleDef.Type.Pos, |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 987 | }, |
| 988 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 991 | logicModule, properties := factory() |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 992 | |
| 993 | module := &moduleInfo{ |
| 994 | logicModule: logicModule, |
Jamie Gennis | ec70128 | 2014-06-12 20:06:31 -0700 | [diff] [blame] | 995 | typeName: typeName, |
Jamie Gennis | ec70128 | 2014-06-12 20:06:31 -0700 | [diff] [blame] | 996 | relBlueprintsFile: relBlueprintsFile, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 999 | props := []interface{}{ |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1000 | &module.properties, |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 1001 | } |
| 1002 | properties = append(props, properties...) |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1003 | module.moduleProperties = properties |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 1004 | |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 1005 | propertyMap, errs := unpackProperties(moduleDef.Properties, properties...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1006 | if len(errs) > 0 { |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 1007 | return nil, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1010 | module.pos = moduleDef.Type.Pos |
| 1011 | module.propertyPos = make(map[string]scanner.Position) |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 1012 | for name, propertyDef := range propertyMap { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1013 | module.propertyPos[name] = propertyDef.Pos |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 1016 | return module, nil |
| 1017 | } |
| 1018 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 1019 | func (c *Context) addModule(module *moduleInfo) []error { |
| 1020 | name := module.properties.Name |
| 1021 | c.moduleInfo[module.logicModule] = module |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1022 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 1023 | if group, present := c.moduleGroups[name]; present { |
| 1024 | return []error{ |
| 1025 | &Error{ |
| 1026 | Err: fmt.Errorf("module %q already defined", name), |
| 1027 | Pos: module.pos, |
| 1028 | }, |
| 1029 | &Error{ |
| 1030 | Err: fmt.Errorf("<-- previous definition here"), |
| 1031 | Pos: group.modules[0].pos, |
| 1032 | }, |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1033 | } |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 1034 | } else { |
| 1035 | ninjaName := toNinjaName(module.properties.Name) |
| 1036 | |
| 1037 | // The sanitizing in toNinjaName can result in collisions, uniquify the name if it |
| 1038 | // already exists |
| 1039 | for i := 0; c.moduleNinjaNames[ninjaName] != nil; i++ { |
| 1040 | ninjaName = toNinjaName(module.properties.Name) + strconv.Itoa(i) |
| 1041 | } |
| 1042 | |
| 1043 | group := &moduleGroup{ |
| 1044 | name: module.properties.Name, |
| 1045 | ninjaName: ninjaName, |
| 1046 | modules: []*moduleInfo{module}, |
| 1047 | } |
| 1048 | module.group = group |
| 1049 | c.moduleGroups[name] = group |
| 1050 | c.moduleNinjaNames[ninjaName] = group |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Colin Cross | 23d7aa1 | 2015-06-30 16:05:22 -0700 | [diff] [blame] | 1053 | return nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 1056 | // ResolveDependencies checks that the dependencies specified by all of the |
| 1057 | // modules defined in the parsed Blueprints files are valid. This means that |
| 1058 | // the modules depended upon are defined and that no circular dependencies |
| 1059 | // exist. |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1060 | // |
| 1061 | // The config argument is made available to all of the DynamicDependerModule |
| 1062 | // objects via the Config method on the DynamicDependerModuleContext objects |
| 1063 | // passed to their DynamicDependencies method. |
| 1064 | func (c *Context) ResolveDependencies(config interface{}) []error { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 1065 | errs := c.runEarlyMutators(config) |
| 1066 | if len(errs) > 0 { |
| 1067 | return errs |
| 1068 | } |
| 1069 | |
| 1070 | errs = c.resolveDependencies(config) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1071 | if len(errs) > 0 { |
| 1072 | return errs |
| 1073 | } |
| 1074 | |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1075 | errs = c.updateDependencies() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1076 | if len(errs) > 0 { |
| 1077 | return errs |
| 1078 | } |
| 1079 | |
| 1080 | c.dependenciesReady = true |
| 1081 | return nil |
| 1082 | } |
| 1083 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1084 | // moduleDeps adds dependencies to a module. If the module implements the |
| 1085 | // DynamicDependerModule interface then this set consists of the union of those |
| 1086 | // module names listed in its "deps" property, those returned by its |
| 1087 | // DynamicDependencies method, and those added by calling AddDependencies or |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1088 | // AddVariationDependencies on DynamicDependencyModuleContext. Otherwise it |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1089 | // is simply those names listed in its "deps" property. |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1090 | func (c *Context) moduleDeps(module *moduleInfo, |
| 1091 | config interface{}) (errs []error) { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1092 | |
| 1093 | depNamesSet := make(map[string]bool) |
Colin Cross | a434b3f | 2015-01-13 10:59:52 -0800 | [diff] [blame] | 1094 | depNames := []string{} |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1095 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1096 | for _, depName := range module.properties.Deps { |
Colin Cross | a434b3f | 2015-01-13 10:59:52 -0800 | [diff] [blame] | 1097 | if !depNamesSet[depName] { |
| 1098 | depNamesSet[depName] = true |
| 1099 | depNames = append(depNames, depName) |
| 1100 | } |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1103 | dynamicDepender, ok := module.logicModule.(DynamicDependerModule) |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1104 | if ok { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1105 | ddmctx := &dynamicDependerModuleContext{ |
| 1106 | baseModuleContext: baseModuleContext{ |
| 1107 | context: c, |
| 1108 | config: config, |
| 1109 | module: module, |
| 1110 | }, |
| 1111 | module: module, |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | dynamicDeps := dynamicDepender.DynamicDependencies(ddmctx) |
| 1115 | |
| 1116 | if len(ddmctx.errs) > 0 { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1117 | return ddmctx.errs |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | for _, depName := range dynamicDeps { |
Colin Cross | a434b3f | 2015-01-13 10:59:52 -0800 | [diff] [blame] | 1121 | if !depNamesSet[depName] { |
| 1122 | depNamesSet[depName] = true |
| 1123 | depNames = append(depNames, depName) |
| 1124 | } |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1128 | for _, depName := range depNames { |
| 1129 | newErrs := c.addDependency(module, depName) |
| 1130 | if len(newErrs) > 0 { |
| 1131 | errs = append(errs, newErrs...) |
| 1132 | } |
| 1133 | } |
| 1134 | return errs |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1137 | // resolveDependencies populates the directDeps list for every module. In doing so it checks for |
| 1138 | // missing dependencies and self-dependant modules. |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1139 | func (c *Context) resolveDependencies(config interface{}) (errs []error) { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1140 | for _, group := range c.moduleGroups { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1141 | for _, module := range group.modules { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1142 | module.directDeps = make([]*moduleInfo, 0, len(module.properties.Deps)) |
| 1143 | |
| 1144 | newErrs := c.moduleDeps(module, config) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1145 | if len(newErrs) > 0 { |
| 1146 | errs = append(errs, newErrs...) |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1147 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | return |
| 1152 | } |
| 1153 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1154 | func (c *Context) addDependency(module *moduleInfo, depName string) []error { |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1155 | depsPos := module.propertyPos["deps"] |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1156 | |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1157 | if depName == module.properties.Name { |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1158 | return []error{&Error{ |
| 1159 | Err: fmt.Errorf("%q depends on itself", depName), |
| 1160 | Pos: depsPos, |
| 1161 | }} |
| 1162 | } |
| 1163 | |
| 1164 | depInfo, ok := c.moduleGroups[depName] |
| 1165 | if !ok { |
| 1166 | return []error{&Error{ |
| 1167 | Err: fmt.Errorf("%q depends on undefined module %q", |
Colin Cross | ed342d9 | 2015-03-11 00:57:25 -0700 | [diff] [blame] | 1168 | module.properties.Name, depName), |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1169 | Pos: depsPos, |
| 1170 | }} |
| 1171 | } |
| 1172 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1173 | for _, m := range module.directDeps { |
| 1174 | if m.group == depInfo { |
| 1175 | return nil |
| 1176 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1179 | if len(depInfo.modules) == 1 { |
| 1180 | module.directDeps = append(module.directDeps, depInfo.modules[0]) |
| 1181 | return nil |
| 1182 | } else { |
| 1183 | for _, m := range depInfo.modules { |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1184 | if m.variant.equal(module.dependencyVariant) { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1185 | module.directDeps = append(module.directDeps, m) |
| 1186 | return nil |
| 1187 | } |
| 1188 | } |
| 1189 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1190 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1191 | return []error{&Error{ |
| 1192 | Err: fmt.Errorf("dependency %q of %q missing variant %q", |
| 1193 | depInfo.modules[0].properties.Name, module.properties.Name, |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1194 | c.prettyPrintVariant(module.dependencyVariant)), |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1195 | Pos: depsPos, |
| 1196 | }} |
| 1197 | } |
| 1198 | |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1199 | func (c *Context) addVariationDependency(module *moduleInfo, variations []Variation, |
Colin Cross | 8948623 | 2015-05-08 11:14:54 -0700 | [diff] [blame] | 1200 | depName string, far bool) []error { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1201 | |
| 1202 | depsPos := module.propertyPos["deps"] |
| 1203 | |
| 1204 | depInfo, ok := c.moduleGroups[depName] |
| 1205 | if !ok { |
| 1206 | return []error{&Error{ |
| 1207 | Err: fmt.Errorf("%q depends on undefined module %q", |
| 1208 | module.properties.Name, depName), |
| 1209 | Pos: depsPos, |
| 1210 | }} |
| 1211 | } |
| 1212 | |
| 1213 | // We can't just append variant.Variant to module.dependencyVariants.variantName and |
| 1214 | // compare the strings because the result won't be in mutator registration order. |
| 1215 | // Create a new map instead, and then deep compare the maps. |
Colin Cross | 8948623 | 2015-05-08 11:14:54 -0700 | [diff] [blame] | 1216 | var newVariant variationMap |
| 1217 | if !far { |
| 1218 | newVariant = module.dependencyVariant.clone() |
| 1219 | } else { |
| 1220 | newVariant = make(variationMap) |
| 1221 | } |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1222 | for _, v := range variations { |
| 1223 | newVariant[v.Mutator] = v.Variation |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | for _, m := range depInfo.modules { |
Colin Cross | 8948623 | 2015-05-08 11:14:54 -0700 | [diff] [blame] | 1227 | var found bool |
| 1228 | if far { |
| 1229 | found = m.variant.subset(newVariant) |
| 1230 | } else { |
| 1231 | found = m.variant.equal(newVariant) |
| 1232 | } |
| 1233 | if found { |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1234 | // AddVariationDependency allows adding a dependency on itself, but only if |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1235 | // that module is earlier in the module list than this one, since we always |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1236 | // run GenerateBuildActions in order for the variants of a module |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1237 | if depInfo == module.group && beforeInModuleList(module, m, module.group.modules) { |
| 1238 | return []error{&Error{ |
| 1239 | Err: fmt.Errorf("%q depends on later version of itself", depName), |
| 1240 | Pos: depsPos, |
| 1241 | }} |
| 1242 | } |
| 1243 | module.directDeps = append(module.directDeps, m) |
| 1244 | return nil |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | return []error{&Error{ |
| 1249 | Err: fmt.Errorf("dependency %q of %q missing variant %q", |
| 1250 | depInfo.modules[0].properties.Name, module.properties.Name, |
Colin Cross | f5e34b9 | 2015-03-13 16:02:36 -0700 | [diff] [blame] | 1251 | c.prettyPrintVariant(newVariant)), |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1252 | Pos: depsPos, |
| 1253 | }} |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1256 | func (c *Context) parallelVisitAllBottomUp(visit func(group *moduleInfo) bool) { |
| 1257 | doneCh := make(chan *moduleInfo) |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1258 | count := 0 |
Colin Cross | 8900e9b | 2015-03-02 14:03:01 -0800 | [diff] [blame] | 1259 | cancel := false |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1260 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1261 | for _, module := range c.modulesSorted { |
| 1262 | module.waitingCount = module.depsCount |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1265 | visitOne := func(module *moduleInfo) { |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1266 | count++ |
| 1267 | go func() { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1268 | ret := visit(module) |
Colin Cross | 8900e9b | 2015-03-02 14:03:01 -0800 | [diff] [blame] | 1269 | if ret { |
| 1270 | cancel = true |
| 1271 | } |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1272 | doneCh <- module |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1273 | }() |
| 1274 | } |
| 1275 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1276 | for _, module := range c.modulesSorted { |
| 1277 | if module.waitingCount == 0 { |
| 1278 | visitOne(module) |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Colin Cross | 11e3b0d | 2015-02-04 10:41:00 -0800 | [diff] [blame] | 1282 | for count > 0 { |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1283 | select { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1284 | case doneModule := <-doneCh: |
Colin Cross | 8900e9b | 2015-03-02 14:03:01 -0800 | [diff] [blame] | 1285 | if !cancel { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1286 | for _, parent := range doneModule.reverseDeps { |
Colin Cross | 8900e9b | 2015-03-02 14:03:01 -0800 | [diff] [blame] | 1287 | parent.waitingCount-- |
| 1288 | if parent.waitingCount == 0 { |
| 1289 | visitOne(parent) |
| 1290 | } |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | count-- |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | // updateDependencies recursively walks the module dependency graph and updates |
| 1299 | // additional fields based on the dependencies. It builds a sorted list of modules |
| 1300 | // such that dependencies of a module always appear first, and populates reverse |
| 1301 | // dependency links and counts of total dependencies. It also reports errors when |
| 1302 | // it encounters dependency cycles. This should called after resolveDependencies, |
| 1303 | // as well as after any mutator pass has called addDependency |
| 1304 | func (c *Context) updateDependencies() (errs []error) { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1305 | visited := make(map[*moduleInfo]bool) // modules that were already checked |
| 1306 | checking := make(map[*moduleInfo]bool) // modules actively being checked |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1307 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1308 | sorted := make([]*moduleInfo, 0, len(c.moduleInfo)) |
Colin Cross | 573a2fd | 2014-12-17 14:16:51 -0800 | [diff] [blame] | 1309 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1310 | var check func(group *moduleInfo) []*moduleInfo |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1311 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1312 | cycleError := func(cycle []*moduleInfo) { |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1313 | // We are the "start" of the cycle, so we're responsible |
| 1314 | // for generating the errors. The cycle list is in |
| 1315 | // reverse order because all the 'check' calls append |
| 1316 | // their own module to the list. |
| 1317 | errs = append(errs, &Error{ |
| 1318 | Err: fmt.Errorf("encountered dependency cycle:"), |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1319 | Pos: cycle[len(cycle)-1].pos, |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1320 | }) |
| 1321 | |
| 1322 | // Iterate backwards through the cycle list. |
Colin Cross | 0e4607e | 2015-03-24 16:42:56 -0700 | [diff] [blame] | 1323 | curModule := cycle[0] |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1324 | for i := len(cycle) - 1; i >= 0; i-- { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1325 | nextModule := cycle[i] |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1326 | errs = append(errs, &Error{ |
| 1327 | Err: fmt.Errorf(" %q depends on %q", |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1328 | curModule.properties.Name, |
| 1329 | nextModule.properties.Name), |
| 1330 | Pos: curModule.propertyPos["deps"], |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1331 | }) |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1332 | curModule = nextModule |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1336 | check = func(module *moduleInfo) []*moduleInfo { |
| 1337 | visited[module] = true |
| 1338 | checking[module] = true |
| 1339 | defer delete(checking, module) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1340 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1341 | deps := make(map[*moduleInfo]bool) |
| 1342 | |
| 1343 | // Add an implicit dependency ordering on all earlier modules in the same module group |
| 1344 | for _, dep := range module.group.modules { |
| 1345 | if dep == module { |
| 1346 | break |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1347 | } |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1348 | deps[dep] = true |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1349 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1350 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1351 | for _, dep := range module.directDeps { |
| 1352 | deps[dep] = true |
| 1353 | } |
| 1354 | |
| 1355 | module.reverseDeps = []*moduleInfo{} |
| 1356 | module.depsCount = len(deps) |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1357 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1358 | for dep := range deps { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1359 | if checking[dep] { |
| 1360 | // This is a cycle. |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1361 | return []*moduleInfo{dep, module} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | if !visited[dep] { |
| 1365 | cycle := check(dep) |
| 1366 | if cycle != nil { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1367 | if cycle[0] == module { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1368 | // We are the "start" of the cycle, so we're responsible |
| 1369 | // for generating the errors. The cycle list is in |
| 1370 | // reverse order because all the 'check' calls append |
| 1371 | // their own module to the list. |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1372 | cycleError(cycle) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1373 | |
| 1374 | // We can continue processing this module's children to |
| 1375 | // find more cycles. Since all the modules that were |
| 1376 | // part of the found cycle were marked as visited we |
| 1377 | // won't run into that cycle again. |
| 1378 | } else { |
| 1379 | // We're not the "start" of the cycle, so we just append |
| 1380 | // our module to the list and return it. |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1381 | return append(cycle, module) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | } |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1385 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1386 | dep.reverseDeps = append(dep.reverseDeps, module) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1389 | sorted = append(sorted, module) |
Colin Cross | 573a2fd | 2014-12-17 14:16:51 -0800 | [diff] [blame] | 1390 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1391 | return nil |
| 1392 | } |
| 1393 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1394 | for _, module := range c.moduleInfo { |
| 1395 | if !visited[module] { |
| 1396 | cycle := check(module) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1397 | if cycle != nil { |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1398 | if cycle[len(cycle)-1] != module { |
Colin Cross | 10b54db | 2015-03-11 14:40:30 -0700 | [diff] [blame] | 1399 | panic("inconceivable!") |
| 1400 | } |
| 1401 | cycleError(cycle) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1406 | c.modulesSorted = sorted |
Colin Cross | 573a2fd | 2014-12-17 14:16:51 -0800 | [diff] [blame] | 1407 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1408 | return |
| 1409 | } |
| 1410 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 1411 | // PrepareBuildActions generates an internal representation of all the build |
| 1412 | // actions that need to be performed. This process involves invoking the |
| 1413 | // GenerateBuildActions method on each of the Module objects created during the |
| 1414 | // parse phase and then on each of the registered Singleton objects. |
| 1415 | // |
| 1416 | // If the ResolveDependencies method has not already been called it is called |
| 1417 | // automatically by this method. |
| 1418 | // |
| 1419 | // The config argument is made available to all of the Module and Singleton |
| 1420 | // objects via the Config method on the ModuleContext and SingletonContext |
| 1421 | // objects passed to GenerateBuildActions. It is also passed to the functions |
| 1422 | // specified via PoolFunc, RuleFunc, and VariableFunc so that they can compute |
| 1423 | // config-specific values. |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1424 | // |
| 1425 | // The returned deps is a list of the ninja files dependencies that were added |
| 1426 | // by the modules and singletons via the ModuleContext.AddNinjaFileDeps() and |
| 1427 | // SingletonContext.AddNinjaFileDeps() methods. |
| 1428 | func (c *Context) PrepareBuildActions(config interface{}) (deps []string, errs []error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1429 | c.buildActionsReady = false |
| 1430 | |
| 1431 | if !c.dependenciesReady { |
Jamie Gennis | b9e87f6 | 2014-09-24 20:28:11 -0700 | [diff] [blame] | 1432 | errs := c.ResolveDependencies(config) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1433 | if len(errs) > 0 { |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1434 | return nil, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1438 | errs = c.runMutators(config) |
| 1439 | if len(errs) > 0 { |
| 1440 | return nil, errs |
| 1441 | } |
| 1442 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1443 | liveGlobals := newLiveTracker(config) |
| 1444 | |
| 1445 | c.initSpecialVariables() |
| 1446 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1447 | depsModules, errs := c.generateModuleBuildActions(config, liveGlobals) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1448 | if len(errs) > 0 { |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1449 | return nil, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1452 | depsSingletons, errs := c.generateSingletonBuildActions(config, liveGlobals) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1453 | if len(errs) > 0 { |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1454 | return nil, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1457 | deps = append(depsModules, depsSingletons...) |
| 1458 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1459 | if c.buildDir != nil { |
| 1460 | liveGlobals.addNinjaStringDeps(c.buildDir) |
| 1461 | } |
| 1462 | |
| 1463 | pkgNames := c.makeUniquePackageNames(liveGlobals) |
| 1464 | |
| 1465 | // This will panic if it finds a problem since it's a programming error. |
| 1466 | c.checkForVariableReferenceCycles(liveGlobals.variables, pkgNames) |
| 1467 | |
| 1468 | c.pkgNames = pkgNames |
| 1469 | c.globalVariables = liveGlobals.variables |
| 1470 | c.globalPools = liveGlobals.pools |
| 1471 | c.globalRules = liveGlobals.rules |
| 1472 | |
| 1473 | c.buildActionsReady = true |
| 1474 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1475 | return deps, nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 1478 | func (c *Context) runEarlyMutators(config interface{}) (errs []error) { |
| 1479 | for _, mutator := range c.earlyMutatorInfo { |
| 1480 | for _, group := range c.moduleGroups { |
| 1481 | newModules := make([]*moduleInfo, 0, len(group.modules)) |
| 1482 | |
| 1483 | for _, module := range group.modules { |
| 1484 | mctx := &mutatorContext{ |
| 1485 | baseModuleContext: baseModuleContext{ |
| 1486 | context: c, |
| 1487 | config: config, |
| 1488 | module: module, |
| 1489 | }, |
| 1490 | name: mutator.name, |
| 1491 | } |
| 1492 | mutator.mutator(mctx) |
| 1493 | if len(mctx.errs) > 0 { |
| 1494 | errs = append(errs, mctx.errs...) |
| 1495 | return errs |
| 1496 | } |
| 1497 | |
| 1498 | if module.splitModules != nil { |
| 1499 | newModules = append(newModules, module.splitModules...) |
| 1500 | } else { |
| 1501 | newModules = append(newModules, module) |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | group.modules = newModules |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | return nil |
| 1510 | } |
| 1511 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1512 | func (c *Context) runMutators(config interface{}) (errs []error) { |
| 1513 | for _, mutator := range c.mutatorInfo { |
| 1514 | if mutator.topDownMutator != nil { |
| 1515 | errs = c.runTopDownMutator(config, mutator.name, mutator.topDownMutator) |
| 1516 | } else if mutator.bottomUpMutator != nil { |
| 1517 | errs = c.runBottomUpMutator(config, mutator.name, mutator.bottomUpMutator) |
| 1518 | } else { |
| 1519 | panic("no mutator set on " + mutator.name) |
| 1520 | } |
| 1521 | if len(errs) > 0 { |
| 1522 | return errs |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | return nil |
| 1527 | } |
| 1528 | |
| 1529 | func (c *Context) runTopDownMutator(config interface{}, |
| 1530 | name string, mutator TopDownMutator) (errs []error) { |
| 1531 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1532 | for i := 0; i < len(c.modulesSorted); i++ { |
| 1533 | module := c.modulesSorted[len(c.modulesSorted)-1-i] |
| 1534 | mctx := &mutatorContext{ |
| 1535 | baseModuleContext: baseModuleContext{ |
| 1536 | context: c, |
| 1537 | config: config, |
| 1538 | module: module, |
| 1539 | }, |
| 1540 | name: name, |
| 1541 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1542 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1543 | mutator(mctx) |
| 1544 | if len(mctx.errs) > 0 { |
| 1545 | errs = append(errs, mctx.errs...) |
| 1546 | return errs |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | return errs |
| 1551 | } |
| 1552 | |
| 1553 | func (c *Context) runBottomUpMutator(config interface{}, |
| 1554 | name string, mutator BottomUpMutator) (errs []error) { |
| 1555 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1556 | for _, module := range c.modulesSorted { |
| 1557 | newModules := make([]*moduleInfo, 0, 1) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1558 | |
Jamie Gennis | c798825 | 2015-04-14 23:28:10 -0400 | [diff] [blame] | 1559 | if module.splitModules != nil { |
| 1560 | panic("split module found in sorted module list") |
| 1561 | } |
| 1562 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1563 | mctx := &mutatorContext{ |
| 1564 | baseModuleContext: baseModuleContext{ |
| 1565 | context: c, |
| 1566 | config: config, |
| 1567 | module: module, |
| 1568 | }, |
| 1569 | name: name, |
| 1570 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1571 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1572 | mutator(mctx) |
| 1573 | if len(mctx.errs) > 0 { |
| 1574 | errs = append(errs, mctx.errs...) |
| 1575 | return errs |
| 1576 | } |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1577 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1578 | // Fix up any remaining dependencies on modules that were split into variants |
| 1579 | // by replacing them with the first variant |
| 1580 | for i, dep := range module.directDeps { |
| 1581 | if dep.logicModule == nil { |
| 1582 | module.directDeps[i] = dep.splitModules[0] |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1586 | if module.splitModules != nil { |
| 1587 | newModules = append(newModules, module.splitModules...) |
| 1588 | } else { |
| 1589 | newModules = append(newModules, module) |
| 1590 | } |
| 1591 | |
| 1592 | module.group.modules = spliceModules(module.group.modules, module, newModules) |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
Jamie Gennis | c798825 | 2015-04-14 23:28:10 -0400 | [diff] [blame] | 1595 | errs = c.updateDependencies() |
| 1596 | if len(errs) > 0 { |
| 1597 | return errs |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | return errs |
| 1601 | } |
| 1602 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1603 | func spliceModules(modules []*moduleInfo, origModule *moduleInfo, |
| 1604 | newModules []*moduleInfo) []*moduleInfo { |
| 1605 | for i, m := range modules { |
| 1606 | if m == origModule { |
| 1607 | return spliceModulesAtIndex(modules, i, newModules) |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | panic("failed to find original module to splice") |
| 1612 | } |
| 1613 | |
| 1614 | func spliceModulesAtIndex(modules []*moduleInfo, i int, newModules []*moduleInfo) []*moduleInfo { |
| 1615 | spliceSize := len(newModules) |
| 1616 | newLen := len(modules) + spliceSize - 1 |
| 1617 | var dest []*moduleInfo |
| 1618 | if cap(modules) >= len(modules)-1+len(newModules) { |
| 1619 | // We can fit the splice in the existing capacity, do everything in place |
| 1620 | dest = modules[:newLen] |
| 1621 | } else { |
| 1622 | dest = make([]*moduleInfo, newLen) |
| 1623 | copy(dest, modules[:i]) |
| 1624 | } |
| 1625 | |
| 1626 | // Move the end of the slice over by spliceSize-1 |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 1627 | copy(dest[i+spliceSize:], modules[i+1:]) |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1628 | |
| 1629 | // Copy the new modules into the slice |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 1630 | copy(dest[i:], newModules) |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1631 | |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 1632 | return dest |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1635 | func (c *Context) initSpecialVariables() { |
| 1636 | c.buildDir = nil |
| 1637 | c.requiredNinjaMajor = 1 |
Dan Willemsen | 21b6f37 | 2015-07-22 12:58:01 -0700 | [diff] [blame] | 1638 | c.requiredNinjaMinor = 6 |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1639 | c.requiredNinjaMicro = 0 |
| 1640 | } |
| 1641 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 1642 | func (c *Context) generateModuleBuildActions(config interface{}, |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1643 | liveGlobals *liveTracker) ([]string, []error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1644 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1645 | var deps []string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1646 | var errs []error |
| 1647 | |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1648 | cancelCh := make(chan struct{}) |
| 1649 | errsCh := make(chan []error) |
| 1650 | depsCh := make(chan []string) |
| 1651 | |
| 1652 | go func() { |
| 1653 | for { |
| 1654 | select { |
| 1655 | case <-cancelCh: |
| 1656 | close(cancelCh) |
| 1657 | return |
| 1658 | case newErrs := <-errsCh: |
| 1659 | errs = append(errs, newErrs...) |
| 1660 | case newDeps := <-depsCh: |
| 1661 | deps = append(deps, newDeps...) |
| 1662 | |
| 1663 | } |
| 1664 | } |
| 1665 | }() |
| 1666 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1667 | c.parallelVisitAllBottomUp(func(module *moduleInfo) bool { |
| 1668 | // The parent scope of the moduleContext's local scope gets overridden to be that of the |
| 1669 | // calling Go package on a per-call basis. Since the initial parent scope doesn't matter we |
| 1670 | // just set it to nil. |
| 1671 | prefix := moduleNamespacePrefix(module.group.ninjaName + "_" + module.variantName) |
| 1672 | scope := newLocalScope(nil, prefix) |
Colin Cross | 6134a5c | 2015-02-10 11:26:26 -0800 | [diff] [blame] | 1673 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1674 | mctx := &moduleContext{ |
| 1675 | baseModuleContext: baseModuleContext{ |
| 1676 | context: c, |
| 1677 | config: config, |
| 1678 | module: module, |
| 1679 | }, |
| 1680 | scope: scope, |
| 1681 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1682 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1683 | mctx.module.logicModule.GenerateBuildActions(mctx) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1684 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1685 | if len(mctx.errs) > 0 { |
| 1686 | errsCh <- mctx.errs |
| 1687 | return true |
| 1688 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1689 | |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1690 | depsCh <- mctx.ninjaFileDeps |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1691 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 1692 | newErrs := c.processLocalBuildActions(&module.actionDefs, |
Colin Cross | 7addea3 | 2015-03-11 15:43:52 -0700 | [diff] [blame] | 1693 | &mctx.actionDefs, liveGlobals) |
| 1694 | if len(newErrs) > 0 { |
| 1695 | errsCh <- newErrs |
| 1696 | return true |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1697 | } |
Colin Cross | 8900e9b | 2015-03-02 14:03:01 -0800 | [diff] [blame] | 1698 | return false |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 1699 | }) |
| 1700 | |
| 1701 | cancelCh <- struct{}{} |
| 1702 | <-cancelCh |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1703 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1704 | return deps, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 1707 | func (c *Context) generateSingletonBuildActions(config interface{}, |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1708 | liveGlobals *liveTracker) ([]string, []error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1709 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1710 | var deps []string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1711 | var errs []error |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1712 | |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 1713 | for _, info := range c.singletonInfo { |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 1714 | // The parent scope of the singletonContext's local scope gets overridden to be that of the |
| 1715 | // calling Go package on a per-call basis. Since the initial parent scope doesn't matter we |
| 1716 | // just set it to nil. |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 1717 | scope := newLocalScope(nil, singletonNamespacePrefix(info.name)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1718 | |
| 1719 | sctx := &singletonContext{ |
| 1720 | context: c, |
| 1721 | config: config, |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 1722 | scope: scope, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | info.singleton.GenerateBuildActions(sctx) |
| 1726 | |
| 1727 | if len(sctx.errs) > 0 { |
| 1728 | errs = append(errs, sctx.errs...) |
| 1729 | if len(errs) > maxErrors { |
| 1730 | break |
| 1731 | } |
| 1732 | continue |
| 1733 | } |
| 1734 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1735 | deps = append(deps, sctx.ninjaFileDeps...) |
| 1736 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1737 | newErrs := c.processLocalBuildActions(&info.actionDefs, |
| 1738 | &sctx.actionDefs, liveGlobals) |
| 1739 | errs = append(errs, newErrs...) |
| 1740 | if len(errs) > maxErrors { |
| 1741 | break |
| 1742 | } |
| 1743 | } |
| 1744 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 1745 | return deps, errs |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | func (c *Context) processLocalBuildActions(out, in *localBuildActions, |
| 1749 | liveGlobals *liveTracker) []error { |
| 1750 | |
| 1751 | var errs []error |
| 1752 | |
| 1753 | // First we go through and add everything referenced by the module's |
| 1754 | // buildDefs to the live globals set. This will end up adding the live |
| 1755 | // locals to the set as well, but we'll take them out after. |
| 1756 | for _, def := range in.buildDefs { |
| 1757 | err := liveGlobals.AddBuildDefDeps(def) |
| 1758 | if err != nil { |
| 1759 | errs = append(errs, err) |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | if len(errs) > 0 { |
| 1764 | return errs |
| 1765 | } |
| 1766 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1767 | out.buildDefs = append(out.buildDefs, in.buildDefs...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1768 | |
| 1769 | // We use the now-incorrect set of live "globals" to determine which local |
| 1770 | // definitions are live. As we go through copying those live locals to the |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1771 | // moduleGroup we remove them from the live globals set. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1772 | for _, v := range in.variables { |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 1773 | isLive := liveGlobals.RemoveVariableIfLive(v) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1774 | if isLive { |
| 1775 | out.variables = append(out.variables, v) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1779 | for _, r := range in.rules { |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 1780 | isLive := liveGlobals.RemoveRuleIfLive(r) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1781 | if isLive { |
| 1782 | out.rules = append(out.rules, r) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | return nil |
| 1787 | } |
| 1788 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1789 | func (c *Context) visitDepsDepthFirst(topModule *moduleInfo, visit func(Module)) { |
| 1790 | visited := make(map[*moduleInfo]bool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1791 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1792 | var walk func(module *moduleInfo) |
| 1793 | walk = func(module *moduleInfo) { |
| 1794 | visited[module] = true |
| 1795 | for _, moduleDep := range module.directDeps { |
| 1796 | if !visited[moduleDep] { |
| 1797 | walk(moduleDep) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1798 | } |
| 1799 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1800 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1801 | if module != topModule { |
| 1802 | visit(module.logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1803 | } |
| 1804 | } |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1805 | |
| 1806 | walk(topModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1809 | func (c *Context) visitDepsDepthFirstIf(topModule *moduleInfo, pred func(Module) bool, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1810 | visit func(Module)) { |
| 1811 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1812 | visited := make(map[*moduleInfo]bool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1813 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1814 | var walk func(module *moduleInfo) |
| 1815 | walk = func(module *moduleInfo) { |
| 1816 | visited[module] = true |
| 1817 | for _, moduleDep := range module.directDeps { |
| 1818 | if !visited[moduleDep] { |
| 1819 | walk(moduleDep) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1820 | } |
Jamie Gennis | 0bb5d8a | 2014-11-26 14:45:37 -0800 | [diff] [blame] | 1821 | } |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1822 | |
| 1823 | if module != topModule { |
| 1824 | if pred(module.logicModule) { |
| 1825 | visit(module.logicModule) |
| 1826 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1830 | walk(topModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Colin Cross | c902848 | 2014-12-18 16:28:54 -0800 | [diff] [blame] | 1833 | func (c *Context) visitDirectDeps(module *moduleInfo, visit func(Module)) { |
| 1834 | for _, dep := range module.directDeps { |
| 1835 | visit(dep.logicModule) |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | func (c *Context) visitDirectDepsIf(module *moduleInfo, pred func(Module) bool, |
| 1840 | visit func(Module)) { |
| 1841 | |
| 1842 | for _, dep := range module.directDeps { |
| 1843 | if pred(dep.logicModule) { |
| 1844 | visit(dep.logicModule) |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 1849 | func (c *Context) sortedModuleNames() []string { |
| 1850 | if c.cachedSortedModuleNames == nil { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1851 | c.cachedSortedModuleNames = make([]string, 0, len(c.moduleGroups)) |
| 1852 | for moduleName := range c.moduleGroups { |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 1853 | c.cachedSortedModuleNames = append(c.cachedSortedModuleNames, |
| 1854 | moduleName) |
| 1855 | } |
| 1856 | sort.Strings(c.cachedSortedModuleNames) |
| 1857 | } |
| 1858 | |
| 1859 | return c.cachedSortedModuleNames |
| 1860 | } |
| 1861 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1862 | func (c *Context) visitAllModules(visit func(Module)) { |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 1863 | for _, moduleName := range c.sortedModuleNames() { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1864 | group := c.moduleGroups[moduleName] |
| 1865 | for _, module := range group.modules { |
| 1866 | visit(module.logicModule) |
| 1867 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | func (c *Context) visitAllModulesIf(pred func(Module) bool, |
| 1872 | visit func(Module)) { |
| 1873 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 1874 | for _, moduleName := range c.sortedModuleNames() { |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 1875 | group := c.moduleGroups[moduleName] |
| 1876 | for _, module := range group.modules { |
| 1877 | if pred(module.logicModule) { |
| 1878 | visit(module.logicModule) |
| 1879 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | func (c *Context) requireNinjaVersion(major, minor, micro int) { |
| 1885 | if major != 1 { |
| 1886 | panic("ninja version with major version != 1 not supported") |
| 1887 | } |
| 1888 | if c.requiredNinjaMinor < minor { |
| 1889 | c.requiredNinjaMinor = minor |
| 1890 | c.requiredNinjaMicro = micro |
| 1891 | } |
| 1892 | if c.requiredNinjaMinor == minor && c.requiredNinjaMicro < micro { |
| 1893 | c.requiredNinjaMicro = micro |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | func (c *Context) setBuildDir(value *ninjaString) { |
| 1898 | if c.buildDir != nil { |
| 1899 | panic("buildDir set multiple times") |
| 1900 | } |
| 1901 | c.buildDir = value |
| 1902 | } |
| 1903 | |
| 1904 | func (c *Context) makeUniquePackageNames( |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1905 | liveGlobals *liveTracker) map[*PackageContext]string { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1906 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1907 | pkgs := make(map[string]*PackageContext) |
| 1908 | pkgNames := make(map[*PackageContext]string) |
| 1909 | longPkgNames := make(map[*PackageContext]bool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1910 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1911 | processPackage := func(pctx *PackageContext) { |
| 1912 | if pctx == nil { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1913 | // This is a built-in rule and has no package. |
| 1914 | return |
| 1915 | } |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1916 | if _, ok := pkgNames[pctx]; ok { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1917 | // We've already processed this package. |
| 1918 | return |
| 1919 | } |
| 1920 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1921 | otherPkg, present := pkgs[pctx.shortName] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1922 | if present { |
| 1923 | // Short name collision. Both this package and the one that's |
| 1924 | // already there need to use their full names. We leave the short |
| 1925 | // name in pkgNames for now so future collisions still get caught. |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1926 | longPkgNames[pctx] = true |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1927 | longPkgNames[otherPkg] = true |
| 1928 | } else { |
| 1929 | // No collision so far. Tentatively set the package's name to be |
| 1930 | // its short name. |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1931 | pkgNames[pctx] = pctx.shortName |
Colin Cross | 0d44125 | 2015-04-14 18:02:20 -0700 | [diff] [blame] | 1932 | pkgs[pctx.shortName] = pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | // We try to give all packages their short name, but when we get collisions |
| 1937 | // we need to use the full unique package name. |
| 1938 | for v, _ := range liveGlobals.variables { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1939 | processPackage(v.packageContext()) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1940 | } |
| 1941 | for p, _ := range liveGlobals.pools { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1942 | processPackage(p.packageContext()) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1943 | } |
| 1944 | for r, _ := range liveGlobals.rules { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1945 | processPackage(r.packageContext()) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | // Add the packages that had collisions using their full unique names. This |
| 1949 | // will overwrite any short names that were added in the previous step. |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1950 | for pctx := range longPkgNames { |
| 1951 | pkgNames[pctx] = pctx.fullName |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | return pkgNames |
| 1955 | } |
| 1956 | |
| 1957 | func (c *Context) checkForVariableReferenceCycles( |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 1958 | variables map[Variable]*ninjaString, pkgNames map[*PackageContext]string) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1959 | |
| 1960 | visited := make(map[Variable]bool) // variables that were already checked |
| 1961 | checking := make(map[Variable]bool) // variables actively being checked |
| 1962 | |
| 1963 | var check func(v Variable) []Variable |
| 1964 | |
| 1965 | check = func(v Variable) []Variable { |
| 1966 | visited[v] = true |
| 1967 | checking[v] = true |
| 1968 | defer delete(checking, v) |
| 1969 | |
| 1970 | value := variables[v] |
| 1971 | for _, dep := range value.variables { |
| 1972 | if checking[dep] { |
| 1973 | // This is a cycle. |
| 1974 | return []Variable{dep, v} |
| 1975 | } |
| 1976 | |
| 1977 | if !visited[dep] { |
| 1978 | cycle := check(dep) |
| 1979 | if cycle != nil { |
| 1980 | if cycle[0] == v { |
| 1981 | // We are the "start" of the cycle, so we're responsible |
| 1982 | // for generating the errors. The cycle list is in |
| 1983 | // reverse order because all the 'check' calls append |
| 1984 | // their own module to the list. |
| 1985 | msgs := []string{"detected variable reference cycle:"} |
| 1986 | |
| 1987 | // Iterate backwards through the cycle list. |
| 1988 | curName := v.fullName(pkgNames) |
| 1989 | curValue := value.Value(pkgNames) |
| 1990 | for i := len(cycle) - 1; i >= 0; i-- { |
| 1991 | next := cycle[i] |
| 1992 | nextName := next.fullName(pkgNames) |
| 1993 | nextValue := variables[next].Value(pkgNames) |
| 1994 | |
| 1995 | msgs = append(msgs, fmt.Sprintf( |
| 1996 | " %q depends on %q", curName, nextName)) |
| 1997 | msgs = append(msgs, fmt.Sprintf( |
| 1998 | " [%s = %s]", curName, curValue)) |
| 1999 | |
| 2000 | curName = nextName |
| 2001 | curValue = nextValue |
| 2002 | } |
| 2003 | |
| 2004 | // Variable reference cycles are a programming error, |
| 2005 | // not the fault of the Blueprint file authors. |
| 2006 | panic(strings.Join(msgs, "\n")) |
| 2007 | } else { |
| 2008 | // We're not the "start" of the cycle, so we just append |
| 2009 | // our module to the list and return it. |
| 2010 | return append(cycle, v) |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | return nil |
| 2017 | } |
| 2018 | |
| 2019 | for v := range variables { |
| 2020 | if !visited[v] { |
| 2021 | cycle := check(v) |
| 2022 | if cycle != nil { |
| 2023 | panic("inconceivable!") |
| 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | |
Jamie Gennis | af43556 | 2014-10-27 22:34:56 -0700 | [diff] [blame] | 2029 | // AllTargets returns a map all the build target names to the rule used to build |
| 2030 | // them. This is the same information that is output by running 'ninja -t |
| 2031 | // targets all'. If this is called before PrepareBuildActions successfully |
| 2032 | // completes then ErrbuildActionsNotReady is returned. |
| 2033 | func (c *Context) AllTargets() (map[string]string, error) { |
| 2034 | if !c.buildActionsReady { |
| 2035 | return nil, ErrBuildActionsNotReady |
| 2036 | } |
| 2037 | |
| 2038 | targets := map[string]string{} |
| 2039 | |
| 2040 | // Collect all the module build targets. |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2041 | for _, module := range c.moduleInfo { |
| 2042 | for _, buildDef := range module.actionDefs.buildDefs { |
Jamie Gennis | af43556 | 2014-10-27 22:34:56 -0700 | [diff] [blame] | 2043 | ruleName := buildDef.Rule.fullName(c.pkgNames) |
| 2044 | for _, output := range buildDef.Outputs { |
Christian Zander | 6e2b232 | 2014-11-21 15:12:08 -0800 | [diff] [blame] | 2045 | outputValue, err := output.Eval(c.globalVariables) |
| 2046 | if err != nil { |
| 2047 | return nil, err |
| 2048 | } |
Jamie Gennis | af43556 | 2014-10-27 22:34:56 -0700 | [diff] [blame] | 2049 | targets[outputValue] = ruleName |
| 2050 | } |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | // Collect all the singleton build targets. |
| 2055 | for _, info := range c.singletonInfo { |
| 2056 | for _, buildDef := range info.actionDefs.buildDefs { |
| 2057 | ruleName := buildDef.Rule.fullName(c.pkgNames) |
| 2058 | for _, output := range buildDef.Outputs { |
Christian Zander | 6e2b232 | 2014-11-21 15:12:08 -0800 | [diff] [blame] | 2059 | outputValue, err := output.Eval(c.globalVariables) |
| 2060 | if err != nil { |
Colin Cross | fea2b75 | 2014-12-30 16:05:02 -0800 | [diff] [blame] | 2061 | return nil, err |
Christian Zander | 6e2b232 | 2014-11-21 15:12:08 -0800 | [diff] [blame] | 2062 | } |
Jamie Gennis | af43556 | 2014-10-27 22:34:56 -0700 | [diff] [blame] | 2063 | targets[outputValue] = ruleName |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | return targets, nil |
| 2069 | } |
| 2070 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 2071 | // ModuleTypePropertyStructs returns a mapping from module type name to a list of pointers to |
| 2072 | // property structs returned by the factory for that module type. |
| 2073 | func (c *Context) ModuleTypePropertyStructs() map[string][]interface{} { |
| 2074 | ret := make(map[string][]interface{}) |
| 2075 | for moduleType, factory := range c.moduleFactories { |
| 2076 | _, ret[moduleType] = factory() |
| 2077 | } |
| 2078 | |
| 2079 | return ret |
| 2080 | } |
| 2081 | |
| 2082 | func (c *Context) ModuleName(logicModule Module) string { |
| 2083 | module := c.moduleInfo[logicModule] |
| 2084 | return module.properties.Name |
| 2085 | } |
| 2086 | |
| 2087 | func (c *Context) ModuleDir(logicModule Module) string { |
| 2088 | module := c.moduleInfo[logicModule] |
| 2089 | return filepath.Dir(module.relBlueprintsFile) |
| 2090 | } |
| 2091 | |
| 2092 | func (c *Context) BlueprintFile(logicModule Module) string { |
| 2093 | module := c.moduleInfo[logicModule] |
| 2094 | return module.relBlueprintsFile |
| 2095 | } |
| 2096 | |
| 2097 | func (c *Context) ModuleErrorf(logicModule Module, format string, |
| 2098 | args ...interface{}) error { |
| 2099 | |
| 2100 | module := c.moduleInfo[logicModule] |
| 2101 | return &Error{ |
| 2102 | Err: fmt.Errorf(format, args...), |
| 2103 | Pos: module.pos, |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | func (c *Context) VisitAllModules(visit func(Module)) { |
| 2108 | c.visitAllModules(visit) |
| 2109 | } |
| 2110 | |
| 2111 | func (c *Context) VisitAllModulesIf(pred func(Module) bool, |
| 2112 | visit func(Module)) { |
| 2113 | |
| 2114 | c.visitAllModulesIf(pred, visit) |
| 2115 | } |
| 2116 | |
| 2117 | func (c *Context) VisitDepsDepthFirst(module Module, |
| 2118 | visit func(Module)) { |
| 2119 | |
| 2120 | c.visitDepsDepthFirst(c.moduleInfo[module], visit) |
| 2121 | } |
| 2122 | |
| 2123 | func (c *Context) VisitDepsDepthFirstIf(module Module, |
| 2124 | pred func(Module) bool, visit func(Module)) { |
| 2125 | |
| 2126 | c.visitDepsDepthFirstIf(c.moduleInfo[module], pred, visit) |
| 2127 | } |
| 2128 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 2129 | // WriteBuildFile writes the Ninja manifeset text for the generated build |
| 2130 | // actions to w. If this is called before PrepareBuildActions successfully |
| 2131 | // completes then ErrBuildActionsNotReady is returned. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2132 | func (c *Context) WriteBuildFile(w io.Writer) error { |
| 2133 | if !c.buildActionsReady { |
| 2134 | return ErrBuildActionsNotReady |
| 2135 | } |
| 2136 | |
| 2137 | nw := newNinjaWriter(w) |
| 2138 | |
| 2139 | err := c.writeBuildFileHeader(nw) |
| 2140 | if err != nil { |
| 2141 | return err |
| 2142 | } |
| 2143 | |
| 2144 | err = c.writeNinjaRequiredVersion(nw) |
| 2145 | if err != nil { |
| 2146 | return err |
| 2147 | } |
| 2148 | |
| 2149 | // TODO: Group the globals by package. |
| 2150 | |
| 2151 | err = c.writeGlobalVariables(nw) |
| 2152 | if err != nil { |
| 2153 | return err |
| 2154 | } |
| 2155 | |
| 2156 | err = c.writeGlobalPools(nw) |
| 2157 | if err != nil { |
| 2158 | return err |
| 2159 | } |
| 2160 | |
| 2161 | err = c.writeBuildDir(nw) |
| 2162 | if err != nil { |
| 2163 | return err |
| 2164 | } |
| 2165 | |
| 2166 | err = c.writeGlobalRules(nw) |
| 2167 | if err != nil { |
| 2168 | return err |
| 2169 | } |
| 2170 | |
| 2171 | err = c.writeAllModuleActions(nw) |
| 2172 | if err != nil { |
| 2173 | return err |
| 2174 | } |
| 2175 | |
| 2176 | err = c.writeAllSingletonActions(nw) |
| 2177 | if err != nil { |
| 2178 | return err |
| 2179 | } |
| 2180 | |
| 2181 | return nil |
| 2182 | } |
| 2183 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2184 | type pkgAssociation struct { |
| 2185 | PkgName string |
| 2186 | PkgPath string |
| 2187 | } |
| 2188 | |
| 2189 | type pkgAssociationSorter struct { |
| 2190 | pkgs []pkgAssociation |
| 2191 | } |
| 2192 | |
| 2193 | func (s *pkgAssociationSorter) Len() int { |
| 2194 | return len(s.pkgs) |
| 2195 | } |
| 2196 | |
| 2197 | func (s *pkgAssociationSorter) Less(i, j int) bool { |
| 2198 | iName := s.pkgs[i].PkgName |
| 2199 | jName := s.pkgs[j].PkgName |
| 2200 | return iName < jName |
| 2201 | } |
| 2202 | |
| 2203 | func (s *pkgAssociationSorter) Swap(i, j int) { |
| 2204 | s.pkgs[i], s.pkgs[j] = s.pkgs[j], s.pkgs[i] |
| 2205 | } |
| 2206 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2207 | func (c *Context) writeBuildFileHeader(nw *ninjaWriter) error { |
| 2208 | headerTemplate := template.New("fileHeader") |
| 2209 | _, err := headerTemplate.Parse(fileHeaderTemplate) |
| 2210 | if err != nil { |
| 2211 | // This is a programming error. |
| 2212 | panic(err) |
| 2213 | } |
| 2214 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2215 | var pkgs []pkgAssociation |
| 2216 | maxNameLen := 0 |
| 2217 | for pkg, name := range c.pkgNames { |
| 2218 | pkgs = append(pkgs, pkgAssociation{ |
| 2219 | PkgName: name, |
| 2220 | PkgPath: pkg.pkgPath, |
| 2221 | }) |
| 2222 | if len(name) > maxNameLen { |
| 2223 | maxNameLen = len(name) |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | for i := range pkgs { |
| 2228 | pkgs[i].PkgName += strings.Repeat(" ", maxNameLen-len(pkgs[i].PkgName)) |
| 2229 | } |
| 2230 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2231 | sort.Sort(&pkgAssociationSorter{pkgs}) |
| 2232 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2233 | params := map[string]interface{}{ |
| 2234 | "Pkgs": pkgs, |
| 2235 | } |
| 2236 | |
| 2237 | buf := bytes.NewBuffer(nil) |
| 2238 | err = headerTemplate.Execute(buf, params) |
| 2239 | if err != nil { |
| 2240 | return err |
| 2241 | } |
| 2242 | |
| 2243 | return nw.Comment(buf.String()) |
| 2244 | } |
| 2245 | |
| 2246 | func (c *Context) writeNinjaRequiredVersion(nw *ninjaWriter) error { |
| 2247 | value := fmt.Sprintf("%d.%d.%d", c.requiredNinjaMajor, c.requiredNinjaMinor, |
| 2248 | c.requiredNinjaMicro) |
| 2249 | |
| 2250 | err := nw.Assign("ninja_required_version", value) |
| 2251 | if err != nil { |
| 2252 | return err |
| 2253 | } |
| 2254 | |
| 2255 | return nw.BlankLine() |
| 2256 | } |
| 2257 | |
| 2258 | func (c *Context) writeBuildDir(nw *ninjaWriter) error { |
| 2259 | if c.buildDir != nil { |
| 2260 | err := nw.Assign("builddir", c.buildDir.Value(c.pkgNames)) |
| 2261 | if err != nil { |
| 2262 | return err |
| 2263 | } |
| 2264 | |
| 2265 | err = nw.BlankLine() |
| 2266 | if err != nil { |
| 2267 | return err |
| 2268 | } |
| 2269 | } |
| 2270 | return nil |
| 2271 | } |
| 2272 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2273 | type globalEntity interface { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 2274 | fullName(pkgNames map[*PackageContext]string) string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2277 | type globalEntitySorter struct { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 2278 | pkgNames map[*PackageContext]string |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2279 | entities []globalEntity |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2282 | func (s *globalEntitySorter) Len() int { |
| 2283 | return len(s.entities) |
| 2284 | } |
| 2285 | |
| 2286 | func (s *globalEntitySorter) Less(i, j int) bool { |
| 2287 | iName := s.entities[i].fullName(s.pkgNames) |
| 2288 | jName := s.entities[j].fullName(s.pkgNames) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2289 | return iName < jName |
| 2290 | } |
| 2291 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2292 | func (s *globalEntitySorter) Swap(i, j int) { |
| 2293 | s.entities[i], s.entities[j] = s.entities[j], s.entities[i] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | func (c *Context) writeGlobalVariables(nw *ninjaWriter) error { |
| 2297 | visited := make(map[Variable]bool) |
| 2298 | |
| 2299 | var walk func(v Variable) error |
| 2300 | walk = func(v Variable) error { |
| 2301 | visited[v] = true |
| 2302 | |
| 2303 | // First visit variables on which this variable depends. |
| 2304 | value := c.globalVariables[v] |
| 2305 | for _, dep := range value.variables { |
| 2306 | if !visited[dep] { |
| 2307 | err := walk(dep) |
| 2308 | if err != nil { |
| 2309 | return err |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | err := nw.Assign(v.fullName(c.pkgNames), value.Value(c.pkgNames)) |
| 2315 | if err != nil { |
| 2316 | return err |
| 2317 | } |
| 2318 | |
| 2319 | err = nw.BlankLine() |
| 2320 | if err != nil { |
| 2321 | return err |
| 2322 | } |
| 2323 | |
| 2324 | return nil |
| 2325 | } |
| 2326 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2327 | globalVariables := make([]globalEntity, 0, len(c.globalVariables)) |
| 2328 | for variable := range c.globalVariables { |
| 2329 | globalVariables = append(globalVariables, variable) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2332 | sort.Sort(&globalEntitySorter{c.pkgNames, globalVariables}) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2333 | |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2334 | for _, entity := range globalVariables { |
| 2335 | v := entity.(Variable) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2336 | if !visited[v] { |
| 2337 | err := walk(v) |
| 2338 | if err != nil { |
| 2339 | return nil |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | return nil |
| 2345 | } |
| 2346 | |
| 2347 | func (c *Context) writeGlobalPools(nw *ninjaWriter) error { |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2348 | globalPools := make([]globalEntity, 0, len(c.globalPools)) |
| 2349 | for pool := range c.globalPools { |
| 2350 | globalPools = append(globalPools, pool) |
| 2351 | } |
| 2352 | |
| 2353 | sort.Sort(&globalEntitySorter{c.pkgNames, globalPools}) |
| 2354 | |
| 2355 | for _, entity := range globalPools { |
| 2356 | pool := entity.(Pool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2357 | name := pool.fullName(c.pkgNames) |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2358 | def := c.globalPools[pool] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2359 | err := def.WriteTo(nw, name) |
| 2360 | if err != nil { |
| 2361 | return err |
| 2362 | } |
| 2363 | |
| 2364 | err = nw.BlankLine() |
| 2365 | if err != nil { |
| 2366 | return err |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | return nil |
| 2371 | } |
| 2372 | |
| 2373 | func (c *Context) writeGlobalRules(nw *ninjaWriter) error { |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2374 | globalRules := make([]globalEntity, 0, len(c.globalRules)) |
| 2375 | for rule := range c.globalRules { |
| 2376 | globalRules = append(globalRules, rule) |
| 2377 | } |
| 2378 | |
| 2379 | sort.Sort(&globalEntitySorter{c.pkgNames, globalRules}) |
| 2380 | |
| 2381 | for _, entity := range globalRules { |
| 2382 | rule := entity.(Rule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2383 | name := rule.fullName(c.pkgNames) |
Jamie Gennis | c15544d | 2014-09-24 20:26:52 -0700 | [diff] [blame] | 2384 | def := c.globalRules[rule] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2385 | err := def.WriteTo(nw, name, c.pkgNames) |
| 2386 | if err != nil { |
| 2387 | return err |
| 2388 | } |
| 2389 | |
| 2390 | err = nw.BlankLine() |
| 2391 | if err != nil { |
| 2392 | return err |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | return nil |
| 2397 | } |
| 2398 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2399 | type moduleSorter []*moduleInfo |
Jamie Gennis | 86179fe | 2014-06-11 16:27:16 -0700 | [diff] [blame] | 2400 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2401 | func (s moduleSorter) Len() int { |
Jamie Gennis | 86179fe | 2014-06-11 16:27:16 -0700 | [diff] [blame] | 2402 | return len(s) |
| 2403 | } |
| 2404 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2405 | func (s moduleSorter) Less(i, j int) bool { |
| 2406 | iName := s[i].properties.Name |
| 2407 | jName := s[j].properties.Name |
| 2408 | if iName == jName { |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 2409 | iName = s[i].variantName |
| 2410 | jName = s[j].variantName |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2411 | } |
Jamie Gennis | 86179fe | 2014-06-11 16:27:16 -0700 | [diff] [blame] | 2412 | return iName < jName |
| 2413 | } |
| 2414 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2415 | func (s moduleSorter) Swap(i, j int) { |
Jamie Gennis | 86179fe | 2014-06-11 16:27:16 -0700 | [diff] [blame] | 2416 | s[i], s[j] = s[j], s[i] |
| 2417 | } |
| 2418 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2419 | func (c *Context) writeAllModuleActions(nw *ninjaWriter) error { |
| 2420 | headerTemplate := template.New("moduleHeader") |
| 2421 | _, err := headerTemplate.Parse(moduleHeaderTemplate) |
| 2422 | if err != nil { |
| 2423 | // This is a programming error. |
| 2424 | panic(err) |
| 2425 | } |
| 2426 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2427 | modules := make([]*moduleInfo, 0, len(c.moduleInfo)) |
| 2428 | for _, module := range c.moduleInfo { |
| 2429 | modules = append(modules, module) |
Jamie Gennis | 86179fe | 2014-06-11 16:27:16 -0700 | [diff] [blame] | 2430 | } |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2431 | sort.Sort(moduleSorter(modules)) |
Jamie Gennis | 86179fe | 2014-06-11 16:27:16 -0700 | [diff] [blame] | 2432 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2433 | buf := bytes.NewBuffer(nil) |
| 2434 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2435 | for _, module := range modules { |
Dan Willemsen | 958b3ac | 2015-07-20 15:55:37 -0700 | [diff] [blame] | 2436 | if len(module.actionDefs.variables)+len(module.actionDefs.rules)+len(module.actionDefs.buildDefs) == 0 { |
| 2437 | continue |
| 2438 | } |
| 2439 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2440 | buf.Reset() |
Jamie Gennis | 1ebd3b8 | 2014-06-04 15:33:08 -0700 | [diff] [blame] | 2441 | |
| 2442 | // In order to make the bootstrap build manifest independent of the |
| 2443 | // build dir we need to output the Blueprints file locations in the |
| 2444 | // comments as paths relative to the source directory. |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2445 | relPos := module.pos |
| 2446 | relPos.Filename = module.relBlueprintsFile |
Jamie Gennis | 1ebd3b8 | 2014-06-04 15:33:08 -0700 | [diff] [blame] | 2447 | |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2448 | // Get the name and location of the factory function for the module. |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2449 | factory := c.moduleFactories[module.typeName] |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2450 | factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer()) |
| 2451 | factoryName := factoryFunc.Name() |
| 2452 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2453 | infoMap := map[string]interface{}{ |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2454 | "properties": module.properties, |
| 2455 | "typeName": module.typeName, |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2456 | "goFactory": factoryName, |
Jamie Gennis | 1ebd3b8 | 2014-06-04 15:33:08 -0700 | [diff] [blame] | 2457 | "pos": relPos, |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 2458 | "variant": module.variantName, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2459 | } |
| 2460 | err = headerTemplate.Execute(buf, infoMap) |
| 2461 | if err != nil { |
| 2462 | return err |
| 2463 | } |
| 2464 | |
| 2465 | err = nw.Comment(buf.String()) |
| 2466 | if err != nil { |
| 2467 | return err |
| 2468 | } |
| 2469 | |
| 2470 | err = nw.BlankLine() |
| 2471 | if err != nil { |
| 2472 | return err |
| 2473 | } |
| 2474 | |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2475 | err = c.writeLocalBuildActions(nw, &module.actionDefs) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2476 | if err != nil { |
| 2477 | return err |
| 2478 | } |
| 2479 | |
| 2480 | err = nw.BlankLine() |
| 2481 | if err != nil { |
| 2482 | return err |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | return nil |
| 2487 | } |
| 2488 | |
| 2489 | func (c *Context) writeAllSingletonActions(nw *ninjaWriter) error { |
| 2490 | headerTemplate := template.New("singletonHeader") |
| 2491 | _, err := headerTemplate.Parse(singletonHeaderTemplate) |
| 2492 | if err != nil { |
| 2493 | // This is a programming error. |
| 2494 | panic(err) |
| 2495 | } |
| 2496 | |
| 2497 | buf := bytes.NewBuffer(nil) |
| 2498 | |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 2499 | for _, info := range c.singletonInfo { |
Dan Willemsen | 958b3ac | 2015-07-20 15:55:37 -0700 | [diff] [blame] | 2500 | if len(info.actionDefs.variables)+len(info.actionDefs.rules)+len(info.actionDefs.buildDefs) == 0 { |
| 2501 | continue |
| 2502 | } |
| 2503 | |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2504 | // Get the name of the factory function for the module. |
| 2505 | factory := info.factory |
| 2506 | factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer()) |
| 2507 | factoryName := factoryFunc.Name() |
| 2508 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2509 | buf.Reset() |
| 2510 | infoMap := map[string]interface{}{ |
Yuchen Wu | b9103ef | 2015-08-25 17:58:17 -0700 | [diff] [blame^] | 2511 | "name": info.name, |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2512 | "goFactory": factoryName, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2513 | } |
| 2514 | err = headerTemplate.Execute(buf, infoMap) |
| 2515 | if err != nil { |
| 2516 | return err |
| 2517 | } |
| 2518 | |
| 2519 | err = nw.Comment(buf.String()) |
| 2520 | if err != nil { |
| 2521 | return err |
| 2522 | } |
| 2523 | |
| 2524 | err = nw.BlankLine() |
| 2525 | if err != nil { |
| 2526 | return err |
| 2527 | } |
| 2528 | |
| 2529 | err = c.writeLocalBuildActions(nw, &info.actionDefs) |
| 2530 | if err != nil { |
| 2531 | return err |
| 2532 | } |
| 2533 | |
| 2534 | err = nw.BlankLine() |
| 2535 | if err != nil { |
| 2536 | return err |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | return nil |
| 2541 | } |
| 2542 | |
| 2543 | func (c *Context) writeLocalBuildActions(nw *ninjaWriter, |
| 2544 | defs *localBuildActions) error { |
| 2545 | |
| 2546 | // Write the local variable assignments. |
| 2547 | for _, v := range defs.variables { |
| 2548 | // A localVariable doesn't need the package names or config to |
| 2549 | // determine its name or value. |
| 2550 | name := v.fullName(nil) |
| 2551 | value, err := v.value(nil) |
| 2552 | if err != nil { |
| 2553 | panic(err) |
| 2554 | } |
| 2555 | err = nw.Assign(name, value.Value(c.pkgNames)) |
| 2556 | if err != nil { |
| 2557 | return err |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | if len(defs.variables) > 0 { |
| 2562 | err := nw.BlankLine() |
| 2563 | if err != nil { |
| 2564 | return err |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | // Write the local rules. |
| 2569 | for _, r := range defs.rules { |
| 2570 | // A localRule doesn't need the package names or config to determine |
| 2571 | // its name or definition. |
| 2572 | name := r.fullName(nil) |
| 2573 | def, err := r.def(nil) |
| 2574 | if err != nil { |
| 2575 | panic(err) |
| 2576 | } |
| 2577 | |
| 2578 | err = def.WriteTo(nw, name, c.pkgNames) |
| 2579 | if err != nil { |
| 2580 | return err |
| 2581 | } |
| 2582 | |
| 2583 | err = nw.BlankLine() |
| 2584 | if err != nil { |
| 2585 | return err |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | // Write the build definitions. |
| 2590 | for _, buildDef := range defs.buildDefs { |
| 2591 | err := buildDef.WriteTo(nw, c.pkgNames) |
| 2592 | if err != nil { |
| 2593 | return err |
| 2594 | } |
| 2595 | |
| 2596 | if len(buildDef.Args) > 0 { |
| 2597 | err = nw.BlankLine() |
| 2598 | if err != nil { |
| 2599 | return err |
| 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | return nil |
| 2605 | } |
| 2606 | |
Colin Cross | 65569e4 | 2015-03-10 20:08:19 -0700 | [diff] [blame] | 2607 | func beforeInModuleList(a, b *moduleInfo, list []*moduleInfo) bool { |
| 2608 | found := false |
| 2609 | for _, l := range list { |
| 2610 | if l == a { |
| 2611 | found = true |
| 2612 | } else if l == b { |
| 2613 | return found |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | missing := a |
| 2618 | if found { |
| 2619 | missing = b |
| 2620 | } |
| 2621 | panic(fmt.Errorf("element %v not found in list %v", missing, list)) |
| 2622 | } |
| 2623 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2624 | var fileHeaderTemplate = `****************************************************************************** |
| 2625 | *** This file is generated and should not be edited *** |
| 2626 | ****************************************************************************** |
| 2627 | {{if .Pkgs}} |
| 2628 | This file contains variables, rules, and pools with name prefixes indicating |
| 2629 | they were generated by the following Go packages: |
| 2630 | {{range .Pkgs}} |
| 2631 | {{.PkgName}} [from Go package {{.PkgPath}}]{{end}}{{end}} |
| 2632 | |
| 2633 | ` |
| 2634 | |
| 2635 | var moduleHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 2636 | Module: {{.properties.Name}} |
Colin Cross | ab6d790 | 2015-03-11 16:17:52 -0700 | [diff] [blame] | 2637 | Variant: {{.variant}} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2638 | Type: {{.typeName}} |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2639 | Factory: {{.goFactory}} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2640 | Defined: {{.pos}} |
| 2641 | ` |
| 2642 | |
| 2643 | var singletonHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 2644 | Singleton: {{.name}} |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 2645 | Factory: {{.goFactory}} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 2646 | ` |