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