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