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