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