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