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