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