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