blob: 97a3462363df6ded1cc30fcaf0908f36911f8242 [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// 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 Gennis1bc967e2014-05-27 16:34:41 -070015package blueprint
16
17import (
Jamie Gennis1bc967e2014-05-27 16:34:41 -070018 "bytes"
19 "errors"
20 "fmt"
21 "io"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070022 "path/filepath"
23 "reflect"
Romain Guy28529652014-08-12 17:50:11 -070024 "runtime"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070025 "sort"
Colin Cross6134a5c2015-02-10 11:26:26 -080026 "strconv"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070027 "strings"
Colin Cross127d2ea2016-11-01 11:10:51 -070028 "sync"
Colin Cross23d7aa12015-06-30 16:05:22 -070029 "sync/atomic"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070030 "text/scanner"
31 "text/template"
Colin Cross1fef5362015-04-20 16:50:54 -070032
33 "github.com/google/blueprint/parser"
Colin Crossb519a7e2017-02-01 13:21:35 -080034 "github.com/google/blueprint/pathtools"
Colin Cross1fef5362015-04-20 16:50:54 -070035 "github.com/google/blueprint/proptools"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070036)
37
38var ErrBuildActionsNotReady = errors.New("build actions are not ready")
39
40const maxErrors = 10
41
Jamie Gennisd4e10182014-06-12 20:06:50 -070042// A Context contains all the state needed to parse a set of Blueprints files
43// and generate a Ninja file. The process of generating a Ninja file proceeds
44// through a series of four phases. Each phase corresponds with a some methods
45// on the Context object
46//
47// Phase Methods
48// ------------ -------------------------------------------
Jamie Gennis7d5b2f82014-09-24 17:51:52 -070049// 1. Registration RegisterModuleType, RegisterSingletonType
Jamie Gennisd4e10182014-06-12 20:06:50 -070050//
51// 2. Parse ParseBlueprintsFiles, Parse
52//
Jamie Gennis7d5b2f82014-09-24 17:51:52 -070053// 3. Generate ResolveDependencies, PrepareBuildActions
Jamie Gennisd4e10182014-06-12 20:06:50 -070054//
55// 4. Write WriteBuildFile
56//
57// The registration phase prepares the context to process Blueprints files
58// containing various types of modules. The parse phase reads in one or more
59// Blueprints files and validates their contents against the module types that
60// have been registered. The generate phase then analyzes the parsed Blueprints
61// contents to create an internal representation for the build actions that must
62// be performed. This phase also performs validation of the module dependencies
63// and property values defined in the parsed Blueprints files. Finally, the
64// write phase generates the Ninja manifest text based on the generated build
65// actions.
Jamie Gennis1bc967e2014-05-27 16:34:41 -070066type Context struct {
67 // set at instantiation
Colin Cross65569e42015-03-10 20:08:19 -070068 moduleFactories map[string]ModuleFactory
Colin Cross0b7e83e2016-05-17 14:58:05 -070069 moduleNames map[string]*moduleGroup
70 moduleGroups []*moduleGroup
Colin Cross65569e42015-03-10 20:08:19 -070071 moduleInfo map[Module]*moduleInfo
72 modulesSorted []*moduleInfo
Yuchen Wub9103ef2015-08-25 17:58:17 -070073 singletonInfo []*singletonInfo
Colin Cross65569e42015-03-10 20:08:19 -070074 mutatorInfo []*mutatorInfo
Colin Crossf8b50422016-08-10 12:56:40 -070075 earlyMutatorInfo []*mutatorInfo
Colin Cross65569e42015-03-10 20:08:19 -070076 variantMutatorNames []string
77 moduleNinjaNames map[string]*moduleGroup
Jamie Gennis1bc967e2014-05-27 16:34:41 -070078
Colin Cross3702ac72016-08-11 11:09:00 -070079 depsModified uint32 // positive if a mutator modified the dependencies
80
Jamie Gennis1bc967e2014-05-27 16:34:41 -070081 dependenciesReady bool // set to true on a successful ResolveDependencies
82 buildActionsReady bool // set to true on a successful PrepareBuildActions
83
84 // set by SetIgnoreUnknownModuleTypes
85 ignoreUnknownModuleTypes bool
86
Colin Cross036a1df2015-12-17 15:49:30 -080087 // set by SetAllowMissingDependencies
88 allowMissingDependencies bool
89
Jamie Gennis1bc967e2014-05-27 16:34:41 -070090 // set during PrepareBuildActions
Dan Willemsenaeffbf72015-11-25 15:29:32 -080091 pkgNames map[*packageContext]string
Jamie Gennis1bc967e2014-05-27 16:34:41 -070092 globalVariables map[Variable]*ninjaString
93 globalPools map[Pool]*poolDef
94 globalRules map[Rule]*ruleDef
95
96 // set during PrepareBuildActions
Colin Crossa2599452015-11-18 16:01:01 -080097 ninjaBuildDir *ninjaString // The builddir special Ninja variable
Jamie Gennis1bc967e2014-05-27 16:34:41 -070098 requiredNinjaMajor int // For the ninja_required_version variable
99 requiredNinjaMinor int // For the ninja_required_version variable
100 requiredNinjaMicro int // For the ninja_required_version variable
Jamie Gennisc15544d2014-09-24 20:26:52 -0700101
102 // set lazily by sortedModuleNames
103 cachedSortedModuleNames []string
Colin Crossd7b0f602016-06-02 15:30:20 -0700104
Colin Cross127d2ea2016-11-01 11:10:51 -0700105 globs map[string]GlobPath
106 globLock sync.Mutex
107
Colin Crossb519a7e2017-02-01 13:21:35 -0800108 fs pathtools.FileSystem
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700109}
110
Jamie Gennisd4e10182014-06-12 20:06:50 -0700111// An Error describes a problem that was encountered that is related to a
112// particular location in a Blueprints file.
Colin Cross2c628442016-10-07 17:13:10 -0700113type BlueprintError struct {
Jamie Gennisd4e10182014-06-12 20:06:50 -0700114 Err error // the error that occurred
115 Pos scanner.Position // the relevant Blueprints file location
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700116}
117
Colin Cross2c628442016-10-07 17:13:10 -0700118// A ModuleError describes a problem that was encountered that is related to a
119// particular module in a Blueprints file
120type ModuleError struct {
121 BlueprintError
122 module *moduleInfo
123}
124
125// A PropertyError describes a problem that was encountered that is related to a
126// particular property in a Blueprints file
127type PropertyError struct {
128 ModuleError
129 property string
130}
131
132func (e *BlueprintError) Error() string {
133 return fmt.Sprintf("%s: %s", e.Pos, e.Err)
134}
135
136func (e *ModuleError) Error() string {
137 return fmt.Sprintf("%s: %s: %s", e.Pos, e.module, e.Err)
138}
139
140func (e *PropertyError) Error() string {
141 return fmt.Sprintf("%s: %s: %s: %s", e.Pos, e.module, e.property, e.Err)
142}
143
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700144type localBuildActions struct {
145 variables []*localVariable
146 rules []*localRule
147 buildDefs []*buildDef
148}
149
Colin Crossbbfa51a2014-12-17 16:12:41 -0800150type moduleGroup struct {
Colin Crossed342d92015-03-11 00:57:25 -0700151 name string
152 ninjaName string
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700153
Colin Crossbbfa51a2014-12-17 16:12:41 -0800154 modules []*moduleInfo
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700155}
156
Colin Crossbbfa51a2014-12-17 16:12:41 -0800157type moduleInfo struct {
Colin Crossed342d92015-03-11 00:57:25 -0700158 // set during Parse
159 typeName string
160 relBlueprintsFile string
161 pos scanner.Position
162 propertyPos map[string]scanner.Position
Colin Crossed342d92015-03-11 00:57:25 -0700163
Colin Crossf5e34b92015-03-13 16:02:36 -0700164 variantName string
165 variant variationMap
166 dependencyVariant variationMap
Colin Crosse7daa222015-03-11 14:35:41 -0700167
Colin Crossc9028482014-12-18 16:28:54 -0800168 logicModule Module
169 group *moduleGroup
170 moduleProperties []interface{}
171
172 // set during ResolveDependencies
Colin Cross2c1f3d12016-04-11 15:47:28 -0700173 directDeps []depInfo
Colin Cross036a1df2015-12-17 15:49:30 -0800174 missingDeps []string
Colin Crossc9028482014-12-18 16:28:54 -0800175
Colin Cross7addea32015-03-11 15:43:52 -0700176 // set during updateDependencies
177 reverseDeps []*moduleInfo
Colin Cross3702ac72016-08-11 11:09:00 -0700178 forwardDeps []*moduleInfo
Colin Cross7addea32015-03-11 15:43:52 -0700179
180 // used by parallelVisitAllBottomUp
181 waitingCount int
182
Colin Crossc9028482014-12-18 16:28:54 -0800183 // set during each runMutator
184 splitModules []*moduleInfo
Colin Crossab6d7902015-03-11 16:17:52 -0700185
186 // set during PrepareBuildActions
187 actionDefs localBuildActions
Colin Crossc9028482014-12-18 16:28:54 -0800188}
189
Colin Cross2c1f3d12016-04-11 15:47:28 -0700190type depInfo struct {
191 module *moduleInfo
192 tag DependencyTag
193}
194
Colin Cross0b7e83e2016-05-17 14:58:05 -0700195func (module *moduleInfo) Name() string {
196 return module.group.name
197}
198
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800199func (module *moduleInfo) String() string {
Colin Cross0b7e83e2016-05-17 14:58:05 -0700200 s := fmt.Sprintf("module %q", module.Name())
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800201 if module.variantName != "" {
202 s += fmt.Sprintf(" variant %q", module.variantName)
203 }
204 return s
205}
206
Colin Crossf5e34b92015-03-13 16:02:36 -0700207// A Variation is a way that a variant of a module differs from other variants of the same module.
208// For example, two variants of the same module might have Variation{"arch","arm"} and
209// Variation{"arch","arm64"}
210type Variation struct {
211 // Mutator is the axis on which this variation applies, i.e. "arch" or "link"
Colin Cross65569e42015-03-10 20:08:19 -0700212 Mutator string
Colin Crossf5e34b92015-03-13 16:02:36 -0700213 // Variation is the name of the variation on the axis, i.e. "arm" or "arm64" for arch, or
214 // "shared" or "static" for link.
215 Variation string
Colin Cross65569e42015-03-10 20:08:19 -0700216}
217
Colin Crossf5e34b92015-03-13 16:02:36 -0700218// A variationMap stores a map of Mutator to Variation to specify a variant of a module.
219type variationMap map[string]string
Colin Crosse7daa222015-03-11 14:35:41 -0700220
Colin Crossf5e34b92015-03-13 16:02:36 -0700221func (vm variationMap) clone() variationMap {
222 newVm := make(variationMap)
Colin Crosse7daa222015-03-11 14:35:41 -0700223 for k, v := range vm {
224 newVm[k] = v
225 }
226
227 return newVm
Colin Crossc9028482014-12-18 16:28:54 -0800228}
229
Colin Cross89486232015-05-08 11:14:54 -0700230// Compare this variationMap to another one. Returns true if the every entry in this map
231// is either the same in the other map or doesn't exist in the other map.
232func (vm variationMap) subset(other variationMap) bool {
233 for k, v1 := range vm {
234 if v2, ok := other[k]; ok && v1 != v2 {
235 return false
236 }
237 }
238 return true
239}
240
Colin Crossf5e34b92015-03-13 16:02:36 -0700241func (vm variationMap) equal(other variationMap) bool {
Colin Crosse7daa222015-03-11 14:35:41 -0700242 return reflect.DeepEqual(vm, other)
Colin Crossbbfa51a2014-12-17 16:12:41 -0800243}
244
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700245type singletonInfo struct {
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700246 // set during RegisterSingletonType
247 factory SingletonFactory
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700248 singleton Singleton
Yuchen Wub9103ef2015-08-25 17:58:17 -0700249 name string
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700250
251 // set during PrepareBuildActions
252 actionDefs localBuildActions
253}
254
Colin Crossc9028482014-12-18 16:28:54 -0800255type mutatorInfo struct {
256 // set during RegisterMutator
Colin Crossc0dbc552015-01-02 15:19:28 -0800257 topDownMutator TopDownMutator
258 bottomUpMutator BottomUpMutator
259 name string
Colin Cross49c279a2016-08-05 22:30:44 -0700260 parallel bool
Colin Crossc9028482014-12-18 16:28:54 -0800261}
262
Jamie Gennisd4e10182014-06-12 20:06:50 -0700263// NewContext creates a new Context object. The created context initially has
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700264// no module or singleton factories registered, so the RegisterModuleFactory and
265// RegisterSingletonFactory methods must be called before it can do anything
266// useful.
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700267func NewContext() *Context {
Colin Cross763b6f12015-10-29 15:32:56 -0700268 ctx := &Context{
Colin Cross6134a5c2015-02-10 11:26:26 -0800269 moduleFactories: make(map[string]ModuleFactory),
Colin Cross0b7e83e2016-05-17 14:58:05 -0700270 moduleNames: make(map[string]*moduleGroup),
Colin Cross6134a5c2015-02-10 11:26:26 -0800271 moduleInfo: make(map[Module]*moduleInfo),
Colin Cross6134a5c2015-02-10 11:26:26 -0800272 moduleNinjaNames: make(map[string]*moduleGroup),
Colin Cross127d2ea2016-11-01 11:10:51 -0700273 globs: make(map[string]GlobPath),
Colin Crossb519a7e2017-02-01 13:21:35 -0800274 fs: pathtools.OsFs,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700275 }
Colin Cross763b6f12015-10-29 15:32:56 -0700276
277 ctx.RegisterBottomUpMutator("blueprint_deps", blueprintDepsMutator)
278
279 return ctx
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700280}
281
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700282// A ModuleFactory function creates a new Module object. See the
283// Context.RegisterModuleType method for details about how a registered
284// ModuleFactory is used by a Context.
285type ModuleFactory func() (m Module, propertyStructs []interface{})
286
Jamie Gennisd4e10182014-06-12 20:06:50 -0700287// RegisterModuleType associates a module type name (which can appear in a
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700288// Blueprints file) with a Module factory function. When the given module type
289// name is encountered in a Blueprints file during parsing, the Module factory
290// is invoked to instantiate a new Module object to handle the build action
Colin Crossc9028482014-12-18 16:28:54 -0800291// generation for the module. If a Mutator splits a module into multiple variants,
292// the factory is invoked again to create a new Module for each variant.
Jamie Gennisd4e10182014-06-12 20:06:50 -0700293//
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700294// The module type names given here must be unique for the context. The factory
295// function should be a named function so that its package and name can be
296// included in the generated Ninja file for debugging purposes.
297//
298// The factory function returns two values. The first is the newly created
299// Module object. The second is a slice of pointers to that Module object's
300// properties structs. Each properties struct is examined when parsing a module
301// definition of this type in a Blueprints file. Exported fields of the
302// properties structs are automatically set to the property values specified in
303// the Blueprints file. The properties struct field names determine the name of
304// the Blueprints file properties that are used - the Blueprints property name
305// matches that of the properties struct field name with the first letter
306// converted to lower-case.
307//
308// The fields of the properties struct must be either []string, a string, or
309// bool. The Context will panic if a Module gets instantiated with a properties
310// struct containing a field that is not one these supported types.
311//
312// Any properties that appear in the Blueprints files that are not built-in
313// module properties (such as "name" and "deps") and do not have a corresponding
314// field in the returned module properties struct result in an error during the
315// Context's parse phase.
316//
317// As an example, the follow code:
318//
319// type myModule struct {
320// properties struct {
321// Foo string
322// Bar []string
323// }
324// }
325//
326// func NewMyModule() (blueprint.Module, []interface{}) {
327// module := new(myModule)
328// properties := &module.properties
329// return module, []interface{}{properties}
330// }
331//
332// func main() {
333// ctx := blueprint.NewContext()
334// ctx.RegisterModuleType("my_module", NewMyModule)
335// // ...
336// }
337//
338// would support parsing a module defined in a Blueprints file as follows:
339//
340// my_module {
341// name: "myName",
342// foo: "my foo string",
343// bar: ["my", "bar", "strings"],
344// }
345//
Colin Cross7ad621c2015-01-07 16:22:45 -0800346// The factory function may be called from multiple goroutines. Any accesses
347// to global variables must be synchronized.
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700348func (c *Context) RegisterModuleType(name string, factory ModuleFactory) {
349 if _, present := c.moduleFactories[name]; present {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700350 panic(errors.New("module type name is already registered"))
351 }
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700352 c.moduleFactories[name] = factory
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700353}
354
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700355// A SingletonFactory function creates a new Singleton object. See the
356// Context.RegisterSingletonType method for details about how a registered
357// SingletonFactory is used by a Context.
358type SingletonFactory func() Singleton
359
360// RegisterSingletonType registers a singleton type that will be invoked to
361// generate build actions. Each registered singleton type is instantiated and
Yuchen Wub9103ef2015-08-25 17:58:17 -0700362// and invoked exactly once as part of the generate phase. Each registered
363// singleton is invoked in registration order.
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700364//
365// The singleton type names given here must be unique for the context. The
366// factory function should be a named function so that its package and name can
367// be included in the generated Ninja file for debugging purposes.
368func (c *Context) RegisterSingletonType(name string, factory SingletonFactory) {
Yuchen Wub9103ef2015-08-25 17:58:17 -0700369 for _, s := range c.singletonInfo {
370 if s.name == name {
371 panic(errors.New("singleton name is already registered"))
372 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700373 }
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700374
Yuchen Wub9103ef2015-08-25 17:58:17 -0700375 c.singletonInfo = append(c.singletonInfo, &singletonInfo{
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700376 factory: factory,
377 singleton: factory(),
Yuchen Wub9103ef2015-08-25 17:58:17 -0700378 name: name,
379 })
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700380}
381
382func singletonPkgPath(singleton Singleton) string {
383 typ := reflect.TypeOf(singleton)
384 for typ.Kind() == reflect.Ptr {
385 typ = typ.Elem()
386 }
387 return typ.PkgPath()
388}
389
390func singletonTypeName(singleton Singleton) string {
391 typ := reflect.TypeOf(singleton)
392 for typ.Kind() == reflect.Ptr {
393 typ = typ.Elem()
394 }
395 return typ.PkgPath() + "." + typ.Name()
396}
397
Colin Cross3702ac72016-08-11 11:09:00 -0700398// RegisterTopDownMutator registers a mutator that will be invoked to propagate dependency info
399// top-down between Modules. Each registered mutator is invoked in registration order (mixing
400// TopDownMutators and BottomUpMutators) once per Module, and the invocation on any module will
401// have returned before it is in invoked on any of its dependencies.
Colin Crossc9028482014-12-18 16:28:54 -0800402//
Colin Cross65569e42015-03-10 20:08:19 -0700403// The mutator type names given here must be unique to all top down mutators in
404// the Context.
Colin Cross3702ac72016-08-11 11:09:00 -0700405//
406// Returns a MutatorHandle, on which Parallel can be called to set the mutator to visit modules in
407// parallel while maintaining ordering.
408func (c *Context) RegisterTopDownMutator(name string, mutator TopDownMutator) MutatorHandle {
Colin Crossc9028482014-12-18 16:28:54 -0800409 for _, m := range c.mutatorInfo {
410 if m.name == name && m.topDownMutator != nil {
411 panic(fmt.Errorf("mutator name %s is already registered", name))
412 }
413 }
414
Colin Cross3702ac72016-08-11 11:09:00 -0700415 info := &mutatorInfo{
Colin Crossc9028482014-12-18 16:28:54 -0800416 topDownMutator: mutator,
Colin Crossc0dbc552015-01-02 15:19:28 -0800417 name: name,
Colin Cross3702ac72016-08-11 11:09:00 -0700418 }
419
420 c.mutatorInfo = append(c.mutatorInfo, info)
421
422 return info
Colin Crossc9028482014-12-18 16:28:54 -0800423}
424
Colin Cross3702ac72016-08-11 11:09:00 -0700425// RegisterBottomUpMutator registers a mutator that will be invoked to split Modules into variants.
426// Each registered mutator is invoked in registration order (mixing TopDownMutators and
427// BottomUpMutators) once per Module, will not be invoked on a module until the invocations on all
428// of the modules dependencies have returned.
Colin Crossc9028482014-12-18 16:28:54 -0800429//
Colin Cross65569e42015-03-10 20:08:19 -0700430// The mutator type names given here must be unique to all bottom up or early
431// mutators in the Context.
Colin Cross49c279a2016-08-05 22:30:44 -0700432//
Colin Cross3702ac72016-08-11 11:09:00 -0700433// Returns a MutatorHandle, on which Parallel can be called to set the mutator to visit modules in
434// parallel while maintaining ordering.
435func (c *Context) RegisterBottomUpMutator(name string, mutator BottomUpMutator) MutatorHandle {
Colin Cross65569e42015-03-10 20:08:19 -0700436 for _, m := range c.variantMutatorNames {
437 if m == name {
Colin Crossc9028482014-12-18 16:28:54 -0800438 panic(fmt.Errorf("mutator name %s is already registered", name))
439 }
440 }
441
Colin Cross49c279a2016-08-05 22:30:44 -0700442 info := &mutatorInfo{
Colin Crossc9028482014-12-18 16:28:54 -0800443 bottomUpMutator: mutator,
Colin Crossc0dbc552015-01-02 15:19:28 -0800444 name: name,
Colin Cross49c279a2016-08-05 22:30:44 -0700445 }
446 c.mutatorInfo = append(c.mutatorInfo, info)
Colin Cross65569e42015-03-10 20:08:19 -0700447
448 c.variantMutatorNames = append(c.variantMutatorNames, name)
Colin Cross49c279a2016-08-05 22:30:44 -0700449
450 return info
451}
452
Colin Cross3702ac72016-08-11 11:09:00 -0700453type MutatorHandle interface {
454 // Set the mutator to visit modules in parallel while maintaining ordering. Calling any
455 // method on the mutator context is thread-safe, but the mutator must handle synchronization
456 // for any modifications to global state or any modules outside the one it was invoked on.
457 Parallel() MutatorHandle
Colin Cross49c279a2016-08-05 22:30:44 -0700458}
459
Colin Cross3702ac72016-08-11 11:09:00 -0700460func (mutator *mutatorInfo) Parallel() MutatorHandle {
Colin Cross49c279a2016-08-05 22:30:44 -0700461 mutator.parallel = true
462 return mutator
Colin Cross65569e42015-03-10 20:08:19 -0700463}
464
465// RegisterEarlyMutator registers a mutator that will be invoked to split
466// Modules into multiple variant Modules before any dependencies have been
467// created. Each registered mutator is invoked in registration order once
468// per Module (including each variant from previous early mutators). Module
469// order is unpredictable.
470//
471// In order for dependencies to be satisifed in a later pass, all dependencies
Colin Crossf5e34b92015-03-13 16:02:36 -0700472// of a module either must have an identical variant or must have no variations.
Colin Cross65569e42015-03-10 20:08:19 -0700473//
474// The mutator type names given here must be unique to all bottom up or early
475// mutators in the Context.
Colin Cross763b6f12015-10-29 15:32:56 -0700476//
477// Deprecated, use a BottomUpMutator instead. The only difference between
478// EarlyMutator and BottomUpMutator is that EarlyMutator runs before the
479// deprecated DynamicDependencies.
Colin Cross65569e42015-03-10 20:08:19 -0700480func (c *Context) RegisterEarlyMutator(name string, mutator EarlyMutator) {
481 for _, m := range c.variantMutatorNames {
482 if m == name {
483 panic(fmt.Errorf("mutator name %s is already registered", name))
484 }
485 }
486
Colin Crossf8b50422016-08-10 12:56:40 -0700487 c.earlyMutatorInfo = append(c.earlyMutatorInfo, &mutatorInfo{
488 bottomUpMutator: func(mctx BottomUpMutatorContext) {
489 mutator(mctx)
490 },
491 name: name,
Colin Cross65569e42015-03-10 20:08:19 -0700492 })
493
494 c.variantMutatorNames = append(c.variantMutatorNames, name)
Colin Crossc9028482014-12-18 16:28:54 -0800495}
496
Jamie Gennisd4e10182014-06-12 20:06:50 -0700497// SetIgnoreUnknownModuleTypes sets the behavior of the context in the case
498// where it encounters an unknown module type while parsing Blueprints files. By
499// default, the context will report unknown module types as an error. If this
500// method is called with ignoreUnknownModuleTypes set to true then the context
501// will silently ignore unknown module types.
502//
503// This method should generally not be used. It exists to facilitate the
504// bootstrapping process.
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700505func (c *Context) SetIgnoreUnknownModuleTypes(ignoreUnknownModuleTypes bool) {
506 c.ignoreUnknownModuleTypes = ignoreUnknownModuleTypes
507}
508
Colin Cross036a1df2015-12-17 15:49:30 -0800509// SetAllowMissingDependencies changes the behavior of Blueprint to ignore
510// unresolved dependencies. If the module's GenerateBuildActions calls
511// ModuleContext.GetMissingDependencies Blueprint will not emit any errors
512// for missing dependencies.
513func (c *Context) SetAllowMissingDependencies(allowMissingDependencies bool) {
514 c.allowMissingDependencies = allowMissingDependencies
515}
516
Jamie Gennisd4e10182014-06-12 20:06:50 -0700517// Parse parses a single Blueprints file from r, creating Module objects for
518// each of the module definitions encountered. If the Blueprints file contains
519// an assignment to the "subdirs" variable, then the subdirectories listed are
Colin Cross1fef5362015-04-20 16:50:54 -0700520// searched for Blueprints files returned in the subBlueprints return value.
521// If the Blueprints file contains an assignment to the "build" variable, then
522// the file listed are returned in the subBlueprints return value.
Jamie Gennisd4e10182014-06-12 20:06:50 -0700523//
524// rootDir specifies the path to the root directory of the source tree, while
525// filename specifies the path to the Blueprints file. These paths are used for
526// error reporting and for determining the module's directory.
Colin Cross7ad621c2015-01-07 16:22:45 -0800527func (c *Context) parse(rootDir, filename string, r io.Reader,
Colin Cross127d2ea2016-11-01 11:10:51 -0700528 scope *parser.Scope) (file *parser.File, subBlueprints []stringAndScope, errs []error) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700529
Jamie Gennisec701282014-06-12 20:06:31 -0700530 relBlueprintsFile, err := filepath.Rel(rootDir, filename)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700531 if err != nil {
Colin Cross127d2ea2016-11-01 11:10:51 -0700532 return nil, nil, []error{err}
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700533 }
534
Colin Crossc0dbc552015-01-02 15:19:28 -0800535 scope = parser.NewScope(scope)
536 scope.Remove("subdirs")
Colin Cross7f507402015-12-16 13:03:41 -0800537 scope.Remove("optional_subdirs")
Colin Cross1fef5362015-04-20 16:50:54 -0700538 scope.Remove("build")
Colin Cross23d7aa12015-06-30 16:05:22 -0700539 file, errs = parser.ParseAndEval(filename, r, scope)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700540 if len(errs) > 0 {
541 for i, err := range errs {
542 if parseErr, ok := err.(*parser.ParseError); ok {
Colin Cross2c628442016-10-07 17:13:10 -0700543 err = &BlueprintError{
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700544 Err: parseErr.Err,
545 Pos: parseErr.Pos,
546 }
547 errs[i] = err
548 }
549 }
550
551 // If there were any parse errors don't bother trying to interpret the
552 // result.
Colin Cross127d2ea2016-11-01 11:10:51 -0700553 return nil, nil, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700554 }
Colin Cross23d7aa12015-06-30 16:05:22 -0700555 file.Name = relBlueprintsFile
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700556
Colin Cross6d8780f2015-07-10 17:51:55 -0700557 subdirs, subdirsPos, err := getLocalStringListFromScope(scope, "subdirs")
Colin Cross1fef5362015-04-20 16:50:54 -0700558 if err != nil {
559 errs = append(errs, err)
560 }
Michael Beardsworth1ec44532015-03-31 20:39:02 -0700561
Colin Cross7f507402015-12-16 13:03:41 -0800562 optionalSubdirs, optionalSubdirsPos, err := getLocalStringListFromScope(scope, "optional_subdirs")
563 if err != nil {
564 errs = append(errs, err)
565 }
566
Colin Cross6d8780f2015-07-10 17:51:55 -0700567 build, buildPos, err := getLocalStringListFromScope(scope, "build")
Colin Cross1fef5362015-04-20 16:50:54 -0700568 if err != nil {
569 errs = append(errs, err)
570 }
571
Colin Cross29394222015-04-27 13:18:21 -0700572 subBlueprintsName, _, err := getStringFromScope(scope, "subname")
Colin Cross127d2ea2016-11-01 11:10:51 -0700573 if err != nil {
574 errs = append(errs, err)
575 }
576
577 if subBlueprintsName == "" {
578 subBlueprintsName = "Blueprints"
579 }
Colin Cross29394222015-04-27 13:18:21 -0700580
Colin Cross7f507402015-12-16 13:03:41 -0800581 var blueprints []string
582
Colin Cross127d2ea2016-11-01 11:10:51 -0700583 newBlueprints, newErrs := c.findBuildBlueprints(filepath.Dir(filename), build, buildPos)
Colin Cross7f507402015-12-16 13:03:41 -0800584 blueprints = append(blueprints, newBlueprints...)
Colin Cross7f507402015-12-16 13:03:41 -0800585 errs = append(errs, newErrs...)
586
Colin Cross127d2ea2016-11-01 11:10:51 -0700587 newBlueprints, newErrs = c.findSubdirBlueprints(filepath.Dir(filename), subdirs, subdirsPos,
Colin Cross7f507402015-12-16 13:03:41 -0800588 subBlueprintsName, false)
589 blueprints = append(blueprints, newBlueprints...)
Colin Cross7f507402015-12-16 13:03:41 -0800590 errs = append(errs, newErrs...)
591
Colin Cross127d2ea2016-11-01 11:10:51 -0700592 newBlueprints, newErrs = c.findSubdirBlueprints(filepath.Dir(filename), optionalSubdirs,
Colin Cross7f507402015-12-16 13:03:41 -0800593 optionalSubdirsPos, subBlueprintsName, true)
594 blueprints = append(blueprints, newBlueprints...)
Colin Cross7f507402015-12-16 13:03:41 -0800595 errs = append(errs, newErrs...)
Colin Crossc0dbc552015-01-02 15:19:28 -0800596
Colin Cross1fef5362015-04-20 16:50:54 -0700597 subBlueprintsAndScope := make([]stringAndScope, len(blueprints))
598 for i, b := range blueprints {
599 subBlueprintsAndScope[i] = stringAndScope{b, scope}
600 }
601
Colin Cross127d2ea2016-11-01 11:10:51 -0700602 return file, subBlueprintsAndScope, errs
Colin Crossc0dbc552015-01-02 15:19:28 -0800603}
604
Colin Cross7ad621c2015-01-07 16:22:45 -0800605type stringAndScope struct {
606 string
607 *parser.Scope
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700608}
609
Jamie Gennisd4e10182014-06-12 20:06:50 -0700610// ParseBlueprintsFiles parses a set of Blueprints files starting with the file
611// at rootFile. When it encounters a Blueprints file with a set of subdirs
612// listed it recursively parses any Blueprints files found in those
613// subdirectories.
614//
615// If no errors are encountered while parsing the files, the list of paths on
616// which the future output will depend is returned. This list will include both
617// Blueprints file paths as well as directory paths for cases where wildcard
618// subdirs are found.
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700619func (c *Context) ParseBlueprintsFiles(rootFile string) (deps []string,
620 errs []error) {
621
Colin Cross7ad621c2015-01-07 16:22:45 -0800622 c.dependenciesReady = false
623
Colin Cross23d7aa12015-06-30 16:05:22 -0700624 moduleCh := make(chan *moduleInfo)
625 errsCh := make(chan []error)
626 doneCh := make(chan struct{})
627 var numErrs uint32
628 var numGoroutines int32
629
630 // handler must be reentrant
631 handler := func(file *parser.File) {
632 if atomic.LoadUint32(&numErrs) > maxErrors {
633 return
634 }
635
636 atomic.AddInt32(&numGoroutines, 1)
637 go func() {
638 for _, def := range file.Defs {
639 var module *moduleInfo
640 var errs []error
641 switch def := def.(type) {
642 case *parser.Module:
643 module, errs = c.processModuleDef(def, file.Name)
644 case *parser.Assignment:
645 // Already handled via Scope object
646 default:
647 panic("unknown definition type")
648 }
649
650 if len(errs) > 0 {
651 atomic.AddUint32(&numErrs, uint32(len(errs)))
652 errsCh <- errs
653 } else if module != nil {
654 moduleCh <- module
655 }
656 }
657 doneCh <- struct{}{}
658 }()
659 }
660
661 atomic.AddInt32(&numGoroutines, 1)
662 go func() {
663 var errs []error
664 deps, errs = c.WalkBlueprintsFiles(rootFile, handler)
665 if len(errs) > 0 {
666 errsCh <- errs
667 }
668 doneCh <- struct{}{}
669 }()
670
671loop:
672 for {
673 select {
674 case newErrs := <-errsCh:
675 errs = append(errs, newErrs...)
676 case module := <-moduleCh:
677 newErrs := c.addModule(module)
678 if len(newErrs) > 0 {
679 errs = append(errs, newErrs...)
680 }
681 case <-doneCh:
682 n := atomic.AddInt32(&numGoroutines, -1)
683 if n == 0 {
684 break loop
685 }
686 }
687 }
688
689 return deps, errs
690}
691
692type FileHandler func(*parser.File)
693
694// Walk a set of Blueprints files starting with the file at rootFile, calling handler on each.
695// When it encounters a Blueprints file with a set of subdirs listed it recursively parses any
696// Blueprints files found in those subdirectories. handler will be called from a goroutine, so
697// it must be reentrant.
698//
699// If no errors are encountered while parsing the files, the list of paths on
700// which the future output will depend is returned. This list will include both
701// Blueprints file paths as well as directory paths for cases where wildcard
702// subdirs are found.
703func (c *Context) WalkBlueprintsFiles(rootFile string, handler FileHandler) (deps []string,
704 errs []error) {
705
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700706 rootDir := filepath.Dir(rootFile)
707
Colin Cross7ad621c2015-01-07 16:22:45 -0800708 blueprintsSet := make(map[string]bool)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700709
Colin Cross7ad621c2015-01-07 16:22:45 -0800710 // Channels to receive data back from parseBlueprintsFile goroutines
711 blueprintsCh := make(chan stringAndScope)
712 errsCh := make(chan []error)
Colin Cross23d7aa12015-06-30 16:05:22 -0700713 fileCh := make(chan *parser.File)
Colin Cross7ad621c2015-01-07 16:22:45 -0800714 depsCh := make(chan string)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700715
Colin Cross7ad621c2015-01-07 16:22:45 -0800716 // Channel to notify main loop that a parseBlueprintsFile goroutine has finished
717 doneCh := make(chan struct{})
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700718
Colin Cross7ad621c2015-01-07 16:22:45 -0800719 // Number of outstanding goroutines to wait for
720 count := 0
721
722 startParseBlueprintsFile := func(filename string, scope *parser.Scope) {
723 count++
724 go func() {
725 c.parseBlueprintsFile(filename, scope, rootDir,
Colin Cross23d7aa12015-06-30 16:05:22 -0700726 errsCh, fileCh, blueprintsCh, depsCh)
Colin Cross7ad621c2015-01-07 16:22:45 -0800727 doneCh <- struct{}{}
728 }()
729 }
730
731 tooManyErrors := false
732
733 startParseBlueprintsFile(rootFile, nil)
734
735loop:
736 for {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700737 if len(errs) > maxErrors {
Colin Cross7ad621c2015-01-07 16:22:45 -0800738 tooManyErrors = true
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700739 }
740
Colin Cross7ad621c2015-01-07 16:22:45 -0800741 select {
742 case newErrs := <-errsCh:
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700743 errs = append(errs, newErrs...)
Colin Cross7ad621c2015-01-07 16:22:45 -0800744 case dep := <-depsCh:
745 deps = append(deps, dep)
Colin Cross23d7aa12015-06-30 16:05:22 -0700746 case file := <-fileCh:
747 handler(file)
Colin Cross7ad621c2015-01-07 16:22:45 -0800748 case blueprint := <-blueprintsCh:
749 if tooManyErrors {
750 continue
751 }
752 if blueprintsSet[blueprint.string] {
753 continue
754 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700755
Colin Cross7ad621c2015-01-07 16:22:45 -0800756 blueprintsSet[blueprint.string] = true
757 startParseBlueprintsFile(blueprint.string, blueprint.Scope)
758 case <-doneCh:
759 count--
760 if count == 0 {
761 break loop
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700762 }
763 }
764 }
765
Colin Cross7ad621c2015-01-07 16:22:45 -0800766 return
767}
768
Colin Crossd7b0f602016-06-02 15:30:20 -0700769// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
770// filenames to contents stored as a byte slice.
771func (c *Context) MockFileSystem(files map[string][]byte) {
Colin Crossb519a7e2017-02-01 13:21:35 -0800772 c.fs = pathtools.MockFs(files)
Colin Crossd7b0f602016-06-02 15:30:20 -0700773}
774
Colin Cross7ad621c2015-01-07 16:22:45 -0800775// parseBlueprintFile parses a single Blueprints file, returning any errors through
776// errsCh, any defined modules through modulesCh, any sub-Blueprints files through
777// blueprintsCh, and any dependencies on Blueprints files or directories through
778// depsCh.
779func (c *Context) parseBlueprintsFile(filename string, scope *parser.Scope, rootDir string,
Colin Cross23d7aa12015-06-30 16:05:22 -0700780 errsCh chan<- []error, fileCh chan<- *parser.File, blueprintsCh chan<- stringAndScope,
Colin Cross7ad621c2015-01-07 16:22:45 -0800781 depsCh chan<- string) {
782
Colin Crossd7b0f602016-06-02 15:30:20 -0700783 f, err := c.fs.Open(filename)
Colin Cross7ad621c2015-01-07 16:22:45 -0800784 if err != nil {
785 errsCh <- []error{err}
786 return
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700787 }
Colin Cross23d7aa12015-06-30 16:05:22 -0700788 defer func() {
789 err = f.Close()
790 if err != nil {
791 errsCh <- []error{err}
792 }
793 }()
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700794
Colin Cross127d2ea2016-11-01 11:10:51 -0700795 file, subBlueprints, errs := c.parse(rootDir, filename, f, scope)
Colin Cross7ad621c2015-01-07 16:22:45 -0800796 if len(errs) > 0 {
797 errsCh <- errs
Colin Cross23d7aa12015-06-30 16:05:22 -0700798 } else {
799 fileCh <- file
Colin Cross7ad621c2015-01-07 16:22:45 -0800800 }
801
Colin Cross1fef5362015-04-20 16:50:54 -0700802 for _, b := range subBlueprints {
803 blueprintsCh <- b
Colin Cross127d2ea2016-11-01 11:10:51 -0700804 depsCh <- b.string
Colin Cross1fef5362015-04-20 16:50:54 -0700805 }
Colin Cross1fef5362015-04-20 16:50:54 -0700806}
807
Colin Cross7f507402015-12-16 13:03:41 -0800808func (c *Context) findBuildBlueprints(dir string, build []string,
Colin Cross127d2ea2016-11-01 11:10:51 -0700809 buildPos scanner.Position) ([]string, []error) {
810
811 var blueprints []string
812 var errs []error
Colin Cross7f507402015-12-16 13:03:41 -0800813
814 for _, file := range build {
Colin Cross127d2ea2016-11-01 11:10:51 -0700815 pattern := filepath.Join(dir, file)
816 var matches []string
817 var err error
818
Colin Cross08e49542016-11-14 15:23:33 -0800819 matches, err = c.glob(pattern, nil)
Colin Cross127d2ea2016-11-01 11:10:51 -0700820
Colin Cross7f507402015-12-16 13:03:41 -0800821 if err != nil {
Colin Cross2c628442016-10-07 17:13:10 -0700822 errs = append(errs, &BlueprintError{
Colin Cross127d2ea2016-11-01 11:10:51 -0700823 Err: fmt.Errorf("%q: %s", pattern, err.Error()),
Colin Cross7f507402015-12-16 13:03:41 -0800824 Pos: buildPos,
825 })
826 continue
827 }
828
829 if len(matches) == 0 {
Colin Cross2c628442016-10-07 17:13:10 -0700830 errs = append(errs, &BlueprintError{
Colin Cross127d2ea2016-11-01 11:10:51 -0700831 Err: fmt.Errorf("%q: not found", pattern),
Colin Cross7f507402015-12-16 13:03:41 -0800832 Pos: buildPos,
833 })
834 }
835
Colin Cross7f507402015-12-16 13:03:41 -0800836 for _, foundBlueprints := range matches {
Colin Cross7f507402015-12-16 13:03:41 -0800837 blueprints = append(blueprints, foundBlueprints)
838 }
839 }
840
Colin Cross127d2ea2016-11-01 11:10:51 -0700841 return blueprints, errs
Colin Cross7f507402015-12-16 13:03:41 -0800842}
843
844func (c *Context) findSubdirBlueprints(dir string, subdirs []string, subdirsPos scanner.Position,
Colin Cross127d2ea2016-11-01 11:10:51 -0700845 subBlueprintsName string, optional bool) ([]string, []error) {
846
847 var blueprints []string
848 var errs []error
Colin Cross7ad621c2015-01-07 16:22:45 -0800849
850 for _, subdir := range subdirs {
Colin Cross127d2ea2016-11-01 11:10:51 -0700851 pattern := filepath.Join(dir, subdir, subBlueprintsName)
852 var matches []string
853 var err error
854
Colin Cross08e49542016-11-14 15:23:33 -0800855 matches, err = c.glob(pattern, nil)
Colin Cross127d2ea2016-11-01 11:10:51 -0700856
Michael Beardsworth1ec44532015-03-31 20:39:02 -0700857 if err != nil {
Colin Cross2c628442016-10-07 17:13:10 -0700858 errs = append(errs, &BlueprintError{
Colin Cross127d2ea2016-11-01 11:10:51 -0700859 Err: fmt.Errorf("%q: %s", pattern, err.Error()),
Colin Cross1fef5362015-04-20 16:50:54 -0700860 Pos: subdirsPos,
861 })
862 continue
863 }
864
Colin Cross7f507402015-12-16 13:03:41 -0800865 if len(matches) == 0 && !optional {
Colin Cross2c628442016-10-07 17:13:10 -0700866 errs = append(errs, &BlueprintError{
Colin Cross127d2ea2016-11-01 11:10:51 -0700867 Err: fmt.Errorf("%q: not found", pattern),
Colin Cross1fef5362015-04-20 16:50:54 -0700868 Pos: subdirsPos,
869 })
Michael Beardsworth1ec44532015-03-31 20:39:02 -0700870 }
Colin Cross7ad621c2015-01-07 16:22:45 -0800871
Colin Cross127d2ea2016-11-01 11:10:51 -0700872 for _, subBlueprints := range matches {
873 blueprints = append(blueprints, subBlueprints)
Colin Cross7ad621c2015-01-07 16:22:45 -0800874 }
875 }
Colin Cross1fef5362015-04-20 16:50:54 -0700876
Colin Cross127d2ea2016-11-01 11:10:51 -0700877 return blueprints, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700878}
879
Colin Cross6d8780f2015-07-10 17:51:55 -0700880func getLocalStringListFromScope(scope *parser.Scope, v string) ([]string, scanner.Position, error) {
881 if assignment, local := scope.Get(v); assignment == nil || !local {
882 return nil, scanner.Position{}, nil
883 } else {
Colin Crosse32cc802016-06-07 12:28:16 -0700884 switch value := assignment.Value.Eval().(type) {
885 case *parser.List:
886 ret := make([]string, 0, len(value.Values))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700887
Colin Crosse32cc802016-06-07 12:28:16 -0700888 for _, listValue := range value.Values {
889 s, ok := listValue.(*parser.String)
890 if !ok {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700891 // The parser should not produce this.
892 panic("non-string value found in list")
893 }
894
Colin Crosse32cc802016-06-07 12:28:16 -0700895 ret = append(ret, s.Value)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700896 }
897
Colin Crossb3d0b8d2016-06-09 17:03:57 -0700898 return ret, assignment.EqualsPos, nil
Colin Crosse32cc802016-06-07 12:28:16 -0700899 case *parser.Bool, *parser.String:
Colin Cross2c628442016-10-07 17:13:10 -0700900 return nil, scanner.Position{}, &BlueprintError{
Colin Cross1fef5362015-04-20 16:50:54 -0700901 Err: fmt.Errorf("%q must be a list of strings", v),
Colin Crossb3d0b8d2016-06-09 17:03:57 -0700902 Pos: assignment.EqualsPos,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700903 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700904 default:
905 panic(fmt.Errorf("unknown value type: %d", assignment.Value.Type))
906 }
907 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700908}
909
Colin Cross29394222015-04-27 13:18:21 -0700910func getStringFromScope(scope *parser.Scope, v string) (string, scanner.Position, error) {
Colin Cross6d8780f2015-07-10 17:51:55 -0700911 if assignment, _ := scope.Get(v); assignment == nil {
912 return "", scanner.Position{}, nil
913 } else {
Colin Crosse32cc802016-06-07 12:28:16 -0700914 switch value := assignment.Value.Eval().(type) {
915 case *parser.String:
Colin Crossb3d0b8d2016-06-09 17:03:57 -0700916 return value.Value, assignment.EqualsPos, nil
Colin Crosse32cc802016-06-07 12:28:16 -0700917 case *parser.Bool, *parser.List:
Colin Cross2c628442016-10-07 17:13:10 -0700918 return "", scanner.Position{}, &BlueprintError{
Colin Cross29394222015-04-27 13:18:21 -0700919 Err: fmt.Errorf("%q must be a string", v),
Colin Crossb3d0b8d2016-06-09 17:03:57 -0700920 Pos: assignment.EqualsPos,
Colin Cross29394222015-04-27 13:18:21 -0700921 }
922 default:
923 panic(fmt.Errorf("unknown value type: %d", assignment.Value.Type))
924 }
925 }
Colin Cross29394222015-04-27 13:18:21 -0700926}
927
Colin Cross910242b2016-04-11 15:41:52 -0700928// Clones a build logic module by calling the factory method for its module type, and then cloning
929// property values. Any values stored in the module object that are not stored in properties
930// structs will be lost.
931func (c *Context) cloneLogicModule(origModule *moduleInfo) (Module, []interface{}) {
932 typeName := origModule.typeName
933 factory, ok := c.moduleFactories[typeName]
934 if !ok {
935 panic(fmt.Sprintf("unrecognized module type %q during cloning", typeName))
936 }
937
Colin Cross910242b2016-04-11 15:41:52 -0700938 newLogicModule, newProperties := factory()
939
Colin Cross910242b2016-04-11 15:41:52 -0700940 if len(newProperties) != len(origModule.moduleProperties) {
Colin Cross0b7e83e2016-05-17 14:58:05 -0700941 panic("mismatched properties array length in " + origModule.Name())
Colin Cross910242b2016-04-11 15:41:52 -0700942 }
943
944 for i := range newProperties {
945 dst := reflect.ValueOf(newProperties[i]).Elem()
946 src := reflect.ValueOf(origModule.moduleProperties[i]).Elem()
947
948 proptools.CopyProperties(dst, src)
949 }
950
951 return newLogicModule, newProperties
952}
953
Colin Crossf5e34b92015-03-13 16:02:36 -0700954func (c *Context) createVariations(origModule *moduleInfo, mutatorName string,
955 variationNames []string) ([]*moduleInfo, []error) {
Colin Crossc9028482014-12-18 16:28:54 -0800956
Colin Crossf4d18a62015-03-18 17:43:15 -0700957 if len(variationNames) == 0 {
958 panic(fmt.Errorf("mutator %q passed zero-length variation list for module %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -0700959 mutatorName, origModule.Name()))
Colin Crossf4d18a62015-03-18 17:43:15 -0700960 }
961
Colin Crossc9028482014-12-18 16:28:54 -0800962 newModules := []*moduleInfo{}
Colin Crossc9028482014-12-18 16:28:54 -0800963
Colin Cross174ae052015-03-03 17:37:03 -0800964 var errs []error
965
Colin Crossf5e34b92015-03-13 16:02:36 -0700966 for i, variationName := range variationNames {
Colin Crossc9028482014-12-18 16:28:54 -0800967 var newLogicModule Module
968 var newProperties []interface{}
969
970 if i == 0 {
971 // Reuse the existing module for the first new variant
Colin Cross21e078a2015-03-16 10:57:54 -0700972 // This both saves creating a new module, and causes the insertion in c.moduleInfo below
973 // with logicModule as the key to replace the original entry in c.moduleInfo
Colin Cross910242b2016-04-11 15:41:52 -0700974 newLogicModule, newProperties = origModule.logicModule, origModule.moduleProperties
Colin Crossc9028482014-12-18 16:28:54 -0800975 } else {
Colin Cross910242b2016-04-11 15:41:52 -0700976 newLogicModule, newProperties = c.cloneLogicModule(origModule)
Colin Crossc9028482014-12-18 16:28:54 -0800977 }
978
Colin Crossf5e34b92015-03-13 16:02:36 -0700979 newVariant := origModule.variant.clone()
980 newVariant[mutatorName] = variationName
Colin Crossc9028482014-12-18 16:28:54 -0800981
Colin Crossed342d92015-03-11 00:57:25 -0700982 m := *origModule
983 newModule := &m
Colin Cross2c1f3d12016-04-11 15:47:28 -0700984 newModule.directDeps = append([]depInfo{}, origModule.directDeps...)
Colin Crossed342d92015-03-11 00:57:25 -0700985 newModule.logicModule = newLogicModule
Colin Crossf5e34b92015-03-13 16:02:36 -0700986 newModule.variant = newVariant
987 newModule.dependencyVariant = origModule.dependencyVariant.clone()
Colin Crossed342d92015-03-11 00:57:25 -0700988 newModule.moduleProperties = newProperties
Colin Crossc9028482014-12-18 16:28:54 -0800989
Colin Crossbadc8812016-08-11 17:01:46 -0700990 if variationName != "" {
991 if newModule.variantName == "" {
992 newModule.variantName = variationName
993 } else {
994 newModule.variantName += "_" + variationName
995 }
Colin Crosse7daa222015-03-11 14:35:41 -0700996 }
997
Colin Crossc9028482014-12-18 16:28:54 -0800998 newModules = append(newModules, newModule)
Colin Cross21e078a2015-03-16 10:57:54 -0700999
Colin Crossf5e34b92015-03-13 16:02:36 -07001000 newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName)
Colin Cross174ae052015-03-03 17:37:03 -08001001 if len(newErrs) > 0 {
1002 errs = append(errs, newErrs...)
1003 }
Colin Crossc9028482014-12-18 16:28:54 -08001004 }
1005
1006 // Mark original variant as invalid. Modules that depend on this module will still
1007 // depend on origModule, but we'll fix it when the mutator is called on them.
1008 origModule.logicModule = nil
1009 origModule.splitModules = newModules
1010
Colin Cross3702ac72016-08-11 11:09:00 -07001011 atomic.AddUint32(&c.depsModified, 1)
1012
Colin Cross174ae052015-03-03 17:37:03 -08001013 return newModules, errs
Colin Crossc9028482014-12-18 16:28:54 -08001014}
1015
Colin Crossf5e34b92015-03-13 16:02:36 -07001016func (c *Context) convertDepsToVariation(module *moduleInfo,
1017 mutatorName, variationName string) (errs []error) {
Colin Cross174ae052015-03-03 17:37:03 -08001018
Colin Crossc9028482014-12-18 16:28:54 -08001019 for i, dep := range module.directDeps {
Colin Cross2c1f3d12016-04-11 15:47:28 -07001020 if dep.module.logicModule == nil {
Colin Crossc9028482014-12-18 16:28:54 -08001021 var newDep *moduleInfo
Colin Cross2c1f3d12016-04-11 15:47:28 -07001022 for _, m := range dep.module.splitModules {
Colin Crossf5e34b92015-03-13 16:02:36 -07001023 if m.variant[mutatorName] == variationName {
Colin Crossc9028482014-12-18 16:28:54 -08001024 newDep = m
1025 break
1026 }
1027 }
1028 if newDep == nil {
Colin Cross2c628442016-10-07 17:13:10 -07001029 errs = append(errs, &BlueprintError{
Colin Crossf5e34b92015-03-13 16:02:36 -07001030 Err: fmt.Errorf("failed to find variation %q for module %q needed by %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001031 variationName, dep.module.Name(), module.Name()),
Colin Crossed342d92015-03-11 00:57:25 -07001032 Pos: module.pos,
Colin Cross174ae052015-03-03 17:37:03 -08001033 })
1034 continue
Colin Crossc9028482014-12-18 16:28:54 -08001035 }
Colin Cross2c1f3d12016-04-11 15:47:28 -07001036 module.directDeps[i].module = newDep
Colin Crossc9028482014-12-18 16:28:54 -08001037 }
1038 }
Colin Cross174ae052015-03-03 17:37:03 -08001039
1040 return errs
Colin Crossc9028482014-12-18 16:28:54 -08001041}
1042
Colin Crossf5e34b92015-03-13 16:02:36 -07001043func (c *Context) prettyPrintVariant(variant variationMap) string {
Colin Cross65569e42015-03-10 20:08:19 -07001044 names := make([]string, 0, len(variant))
1045 for _, m := range c.variantMutatorNames {
1046 if v, ok := variant[m]; ok {
1047 names = append(names, m+":"+v)
1048 }
1049 }
1050
1051 return strings.Join(names, ", ")
1052}
1053
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001054func (c *Context) processModuleDef(moduleDef *parser.Module,
Colin Cross7ad621c2015-01-07 16:22:45 -08001055 relBlueprintsFile string) (*moduleInfo, []error) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001056
Colin Crossc32c4792016-06-09 15:52:30 -07001057 factory, ok := c.moduleFactories[moduleDef.Type]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001058 if !ok {
1059 if c.ignoreUnknownModuleTypes {
Colin Cross7ad621c2015-01-07 16:22:45 -08001060 return nil, nil
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001061 }
1062
Colin Cross7ad621c2015-01-07 16:22:45 -08001063 return nil, []error{
Colin Cross2c628442016-10-07 17:13:10 -07001064 &BlueprintError{
Colin Crossc32c4792016-06-09 15:52:30 -07001065 Err: fmt.Errorf("unrecognized module type %q", moduleDef.Type),
1066 Pos: moduleDef.TypePos,
Jamie Gennisd4c53d82014-06-22 17:02:55 -07001067 },
1068 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001069 }
1070
Colin Crossbbfa51a2014-12-17 16:12:41 -08001071 logicModule, properties := factory()
Colin Crossed342d92015-03-11 00:57:25 -07001072
1073 module := &moduleInfo{
1074 logicModule: logicModule,
Colin Crossc32c4792016-06-09 15:52:30 -07001075 typeName: moduleDef.Type,
Jamie Gennisec701282014-06-12 20:06:31 -07001076 relBlueprintsFile: relBlueprintsFile,
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001077 }
1078
Colin Crossed342d92015-03-11 00:57:25 -07001079 module.moduleProperties = properties
Jamie Gennisd4c53d82014-06-22 17:02:55 -07001080
Jamie Gennis87622922014-09-30 11:38:25 -07001081 propertyMap, errs := unpackProperties(moduleDef.Properties, properties...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001082 if len(errs) > 0 {
Colin Cross7ad621c2015-01-07 16:22:45 -08001083 return nil, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001084 }
1085
Colin Crossc32c4792016-06-09 15:52:30 -07001086 module.pos = moduleDef.TypePos
Colin Crossed342d92015-03-11 00:57:25 -07001087 module.propertyPos = make(map[string]scanner.Position)
Jamie Gennis87622922014-09-30 11:38:25 -07001088 for name, propertyDef := range propertyMap {
Colin Crossb3d0b8d2016-06-09 17:03:57 -07001089 module.propertyPos[name] = propertyDef.ColonPos
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001090 }
1091
Colin Cross7ad621c2015-01-07 16:22:45 -08001092 return module, nil
1093}
1094
Colin Cross23d7aa12015-06-30 16:05:22 -07001095func (c *Context) addModule(module *moduleInfo) []error {
Colin Cross0b7e83e2016-05-17 14:58:05 -07001096 name := module.logicModule.Name()
Colin Cross23d7aa12015-06-30 16:05:22 -07001097 c.moduleInfo[module.logicModule] = module
Colin Crossed342d92015-03-11 00:57:25 -07001098
Colin Cross0b7e83e2016-05-17 14:58:05 -07001099 if group, present := c.moduleNames[name]; present {
Colin Cross23d7aa12015-06-30 16:05:22 -07001100 return []error{
Colin Cross2c628442016-10-07 17:13:10 -07001101 &BlueprintError{
Colin Cross23d7aa12015-06-30 16:05:22 -07001102 Err: fmt.Errorf("module %q already defined", name),
1103 Pos: module.pos,
1104 },
Colin Cross2c628442016-10-07 17:13:10 -07001105 &BlueprintError{
Colin Cross23d7aa12015-06-30 16:05:22 -07001106 Err: fmt.Errorf("<-- previous definition here"),
1107 Pos: group.modules[0].pos,
1108 },
Colin Crossed342d92015-03-11 00:57:25 -07001109 }
Colin Cross7ad621c2015-01-07 16:22:45 -08001110 }
1111
Colin Cross0b7e83e2016-05-17 14:58:05 -07001112 ninjaName := toNinjaName(name)
1113
1114 // The sanitizing in toNinjaName can result in collisions, uniquify the name if it
1115 // already exists
1116 for i := 0; c.moduleNinjaNames[ninjaName] != nil; i++ {
1117 ninjaName = toNinjaName(name) + strconv.Itoa(i)
1118 }
1119
1120 group := &moduleGroup{
1121 name: name,
1122 ninjaName: ninjaName,
1123 modules: []*moduleInfo{module},
1124 }
1125 module.group = group
1126 c.moduleNames[name] = group
1127 c.moduleNinjaNames[ninjaName] = group
1128 c.moduleGroups = append(c.moduleGroups, group)
1129
Colin Cross23d7aa12015-06-30 16:05:22 -07001130 return nil
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001131}
1132
Jamie Gennisd4e10182014-06-12 20:06:50 -07001133// ResolveDependencies checks that the dependencies specified by all of the
1134// modules defined in the parsed Blueprints files are valid. This means that
1135// the modules depended upon are defined and that no circular dependencies
1136// exist.
Jamie Gennisb9e87f62014-09-24 20:28:11 -07001137func (c *Context) ResolveDependencies(config interface{}) []error {
Colin Crossf8b50422016-08-10 12:56:40 -07001138 errs := c.updateDependencies()
1139 if len(errs) > 0 {
1140 return errs
1141 }
1142
1143 errs = c.runMutators(config)
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001144 if len(errs) > 0 {
1145 return errs
1146 }
1147
Colin Cross910242b2016-04-11 15:41:52 -07001148 c.cloneModules()
1149
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001150 c.dependenciesReady = true
1151 return nil
1152}
1153
Colin Cross763b6f12015-10-29 15:32:56 -07001154// Default dependencies handling. If the module implements the (deprecated)
Colin Cross65569e42015-03-10 20:08:19 -07001155// DynamicDependerModule interface then this set consists of the union of those
Colin Cross0b7e83e2016-05-17 14:58:05 -07001156// module names returned by its DynamicDependencies method and those added by calling
1157// AddDependencies or AddVariationDependencies on DynamicDependencyModuleContext.
Colin Cross763b6f12015-10-29 15:32:56 -07001158func blueprintDepsMutator(ctx BottomUpMutatorContext) {
Colin Cross763b6f12015-10-29 15:32:56 -07001159 if dynamicDepender, ok := ctx.Module().(DynamicDependerModule); ok {
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001160 func() {
1161 defer func() {
1162 if r := recover(); r != nil {
1163 ctx.error(newPanicErrorf(r, "DynamicDependencies for %s", ctx.moduleInfo()))
1164 }
1165 }()
1166 dynamicDeps := dynamicDepender.DynamicDependencies(ctx)
Jamie Gennisb9e87f62014-09-24 20:28:11 -07001167
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001168 if ctx.Failed() {
1169 return
1170 }
Colin Cross763b6f12015-10-29 15:32:56 -07001171
Colin Cross2c1f3d12016-04-11 15:47:28 -07001172 ctx.AddDependency(ctx.Module(), nil, dynamicDeps...)
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001173 }()
Jamie Gennisb9e87f62014-09-24 20:28:11 -07001174 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001175}
1176
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001177// findMatchingVariant searches the moduleGroup for a module with the same variant as module,
1178// and returns the matching module, or nil if one is not found.
Colin Cross0b7e83e2016-05-17 14:58:05 -07001179func (c *Context) findMatchingVariant(module *moduleInfo, possible []*moduleInfo) *moduleInfo {
1180 if len(possible) == 1 {
1181 return possible[0]
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001182 } else {
Colin Cross0b7e83e2016-05-17 14:58:05 -07001183 for _, m := range possible {
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001184 if m.variant.equal(module.dependencyVariant) {
1185 return m
1186 }
1187 }
1188 }
1189
1190 return nil
1191}
1192
Colin Cross2c1f3d12016-04-11 15:47:28 -07001193func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName string) []error {
Nan Zhang346b2d02017-03-10 16:39:27 -08001194 if _, ok := tag.(BaseDependencyTag); ok {
1195 panic("BaseDependencyTag is not allowed to be used directly!")
1196 }
1197
Colin Cross0b7e83e2016-05-17 14:58:05 -07001198 if depName == module.Name() {
Colin Cross2c628442016-10-07 17:13:10 -07001199 return []error{&BlueprintError{
Colin Crossc9028482014-12-18 16:28:54 -08001200 Err: fmt.Errorf("%q depends on itself", depName),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001201 Pos: module.pos,
Colin Crossc9028482014-12-18 16:28:54 -08001202 }}
1203 }
1204
Colin Cross0b7e83e2016-05-17 14:58:05 -07001205 possibleDeps := c.modulesFromName(depName)
1206 if possibleDeps == nil {
Colin Cross036a1df2015-12-17 15:49:30 -08001207 if c.allowMissingDependencies {
1208 module.missingDeps = append(module.missingDeps, depName)
1209 return nil
1210 }
Colin Cross2c628442016-10-07 17:13:10 -07001211 return []error{&BlueprintError{
Colin Crossc9028482014-12-18 16:28:54 -08001212 Err: fmt.Errorf("%q depends on undefined module %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001213 module.Name(), depName),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001214 Pos: module.pos,
Colin Crossc9028482014-12-18 16:28:54 -08001215 }}
1216 }
1217
Colin Cross0b7e83e2016-05-17 14:58:05 -07001218 if m := c.findMatchingVariant(module, possibleDeps); m != nil {
Colin Cross2c1f3d12016-04-11 15:47:28 -07001219 for _, dep := range module.directDeps {
1220 if m == dep.module {
1221 // TODO(ccross): what if adding a dependency with a different tag?
1222 return nil
1223 }
Colin Cross65569e42015-03-10 20:08:19 -07001224 }
Colin Cross2c1f3d12016-04-11 15:47:28 -07001225 module.directDeps = append(module.directDeps, depInfo{m, tag})
Colin Cross3702ac72016-08-11 11:09:00 -07001226 atomic.AddUint32(&c.depsModified, 1)
Colin Cross65569e42015-03-10 20:08:19 -07001227 return nil
Colin Cross65569e42015-03-10 20:08:19 -07001228 }
Colin Crossc9028482014-12-18 16:28:54 -08001229
Colin Cross2c628442016-10-07 17:13:10 -07001230 return []error{&BlueprintError{
Colin Cross65569e42015-03-10 20:08:19 -07001231 Err: fmt.Errorf("dependency %q of %q missing variant %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001232 depName, module.Name(),
Colin Crossf5e34b92015-03-13 16:02:36 -07001233 c.prettyPrintVariant(module.dependencyVariant)),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001234 Pos: module.pos,
Colin Cross65569e42015-03-10 20:08:19 -07001235 }}
1236}
1237
Colin Cross8d8a7af2015-11-03 16:41:29 -08001238func (c *Context) findReverseDependency(module *moduleInfo, destName string) (*moduleInfo, []error) {
Colin Cross0b7e83e2016-05-17 14:58:05 -07001239 if destName == module.Name() {
Colin Cross2c628442016-10-07 17:13:10 -07001240 return nil, []error{&BlueprintError{
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001241 Err: fmt.Errorf("%q depends on itself", destName),
1242 Pos: module.pos,
1243 }}
1244 }
1245
Colin Cross0b7e83e2016-05-17 14:58:05 -07001246 possibleDeps := c.modulesFromName(destName)
1247 if possibleDeps == nil {
Colin Cross2c628442016-10-07 17:13:10 -07001248 return nil, []error{&BlueprintError{
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001249 Err: fmt.Errorf("%q has a reverse dependency on undefined module %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001250 module.Name(), destName),
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001251 Pos: module.pos,
1252 }}
1253 }
1254
Colin Cross0b7e83e2016-05-17 14:58:05 -07001255 if m := c.findMatchingVariant(module, possibleDeps); m != nil {
Colin Cross8d8a7af2015-11-03 16:41:29 -08001256 return m, nil
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001257 }
1258
Colin Cross2c628442016-10-07 17:13:10 -07001259 return nil, []error{&BlueprintError{
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001260 Err: fmt.Errorf("reverse dependency %q of %q missing variant %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001261 destName, module.Name(),
Dan Willemsenfdeb7242015-07-24 16:53:27 -07001262 c.prettyPrintVariant(module.dependencyVariant)),
1263 Pos: module.pos,
1264 }}
1265}
1266
Colin Crossf5e34b92015-03-13 16:02:36 -07001267func (c *Context) addVariationDependency(module *moduleInfo, variations []Variation,
Colin Cross2c1f3d12016-04-11 15:47:28 -07001268 tag DependencyTag, depName string, far bool) []error {
Nan Zhang346b2d02017-03-10 16:39:27 -08001269 if _, ok := tag.(BaseDependencyTag); ok {
1270 panic("BaseDependencyTag is not allowed to be used directly!")
1271 }
Colin Cross65569e42015-03-10 20:08:19 -07001272
Colin Cross0b7e83e2016-05-17 14:58:05 -07001273 possibleDeps := c.modulesFromName(depName)
1274 if possibleDeps == nil {
Colin Cross036a1df2015-12-17 15:49:30 -08001275 if c.allowMissingDependencies {
1276 module.missingDeps = append(module.missingDeps, depName)
1277 return nil
1278 }
Colin Cross2c628442016-10-07 17:13:10 -07001279 return []error{&BlueprintError{
Colin Cross65569e42015-03-10 20:08:19 -07001280 Err: fmt.Errorf("%q depends on undefined module %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001281 module.Name(), depName),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001282 Pos: module.pos,
Colin Cross65569e42015-03-10 20:08:19 -07001283 }}
1284 }
1285
1286 // We can't just append variant.Variant to module.dependencyVariants.variantName and
1287 // compare the strings because the result won't be in mutator registration order.
1288 // Create a new map instead, and then deep compare the maps.
Colin Cross89486232015-05-08 11:14:54 -07001289 var newVariant variationMap
1290 if !far {
1291 newVariant = module.dependencyVariant.clone()
1292 } else {
1293 newVariant = make(variationMap)
1294 }
Colin Crossf5e34b92015-03-13 16:02:36 -07001295 for _, v := range variations {
1296 newVariant[v.Mutator] = v.Variation
Colin Cross65569e42015-03-10 20:08:19 -07001297 }
1298
Colin Cross0b7e83e2016-05-17 14:58:05 -07001299 for _, m := range possibleDeps {
Colin Cross89486232015-05-08 11:14:54 -07001300 var found bool
1301 if far {
1302 found = m.variant.subset(newVariant)
1303 } else {
1304 found = m.variant.equal(newVariant)
1305 }
1306 if found {
Colin Cross045a5972015-11-03 16:58:48 -08001307 if module == m {
Colin Cross2c628442016-10-07 17:13:10 -07001308 return []error{&BlueprintError{
Colin Cross045a5972015-11-03 16:58:48 -08001309 Err: fmt.Errorf("%q depends on itself", depName),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001310 Pos: module.pos,
Colin Cross045a5972015-11-03 16:58:48 -08001311 }}
1312 }
Colin Crossf5e34b92015-03-13 16:02:36 -07001313 // AddVariationDependency allows adding a dependency on itself, but only if
Colin Cross65569e42015-03-10 20:08:19 -07001314 // that module is earlier in the module list than this one, since we always
Colin Crossf5e34b92015-03-13 16:02:36 -07001315 // run GenerateBuildActions in order for the variants of a module
Colin Cross0b7e83e2016-05-17 14:58:05 -07001316 if m.group == module.group && beforeInModuleList(module, m, module.group.modules) {
Colin Cross2c628442016-10-07 17:13:10 -07001317 return []error{&BlueprintError{
Colin Cross65569e42015-03-10 20:08:19 -07001318 Err: fmt.Errorf("%q depends on later version of itself", depName),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001319 Pos: module.pos,
Colin Cross65569e42015-03-10 20:08:19 -07001320 }}
1321 }
Colin Cross2c1f3d12016-04-11 15:47:28 -07001322 module.directDeps = append(module.directDeps, depInfo{m, tag})
Colin Cross3702ac72016-08-11 11:09:00 -07001323 atomic.AddUint32(&c.depsModified, 1)
Colin Cross65569e42015-03-10 20:08:19 -07001324 return nil
1325 }
1326 }
1327
Colin Cross2c628442016-10-07 17:13:10 -07001328 return []error{&BlueprintError{
Colin Cross65569e42015-03-10 20:08:19 -07001329 Err: fmt.Errorf("dependency %q of %q missing variant %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001330 depName, module.Name(),
Colin Crossf5e34b92015-03-13 16:02:36 -07001331 c.prettyPrintVariant(newVariant)),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001332 Pos: module.pos,
Colin Cross65569e42015-03-10 20:08:19 -07001333 }}
Colin Crossc9028482014-12-18 16:28:54 -08001334}
1335
Colin Crossf1875462016-04-11 17:33:13 -07001336func (c *Context) addInterVariantDependency(origModule *moduleInfo, tag DependencyTag,
1337 from, to Module) {
Nan Zhang346b2d02017-03-10 16:39:27 -08001338 if _, ok := tag.(BaseDependencyTag); ok {
1339 panic("BaseDependencyTag is not allowed to be used directly!")
1340 }
Colin Crossf1875462016-04-11 17:33:13 -07001341
1342 var fromInfo, toInfo *moduleInfo
1343 for _, m := range origModule.splitModules {
1344 if m.logicModule == from {
1345 fromInfo = m
1346 }
1347 if m.logicModule == to {
1348 toInfo = m
1349 if fromInfo != nil {
Colin Cross0b7e83e2016-05-17 14:58:05 -07001350 panic(fmt.Errorf("%q depends on later version of itself", origModule.Name()))
Colin Crossf1875462016-04-11 17:33:13 -07001351 }
1352 }
1353 }
1354
1355 if fromInfo == nil || toInfo == nil {
1356 panic(fmt.Errorf("AddInterVariantDependency called for module %q on invalid variant",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001357 origModule.Name()))
Colin Crossf1875462016-04-11 17:33:13 -07001358 }
1359
1360 fromInfo.directDeps = append(fromInfo.directDeps, depInfo{toInfo, tag})
Colin Cross3702ac72016-08-11 11:09:00 -07001361 atomic.AddUint32(&c.depsModified, 1)
Colin Crossf1875462016-04-11 17:33:13 -07001362}
1363
Colin Cross3702ac72016-08-11 11:09:00 -07001364type visitOrderer interface {
1365 // returns the number of modules that this module needs to wait for
1366 waitCount(module *moduleInfo) int
1367 // returns the list of modules that are waiting for this module
1368 propagate(module *moduleInfo) []*moduleInfo
1369 // visit modules in order
1370 visit(modules []*moduleInfo, visit func(*moduleInfo) bool)
1371}
1372
1373type bottomUpVisitorImpl struct{}
1374
1375func (bottomUpVisitorImpl) waitCount(module *moduleInfo) int {
1376 return len(module.forwardDeps)
1377}
1378
1379func (bottomUpVisitorImpl) propagate(module *moduleInfo) []*moduleInfo {
1380 return module.reverseDeps
1381}
1382
1383func (bottomUpVisitorImpl) visit(modules []*moduleInfo, visit func(*moduleInfo) bool) {
1384 for _, module := range modules {
Colin Cross49c279a2016-08-05 22:30:44 -07001385 if visit(module) {
1386 return
1387 }
1388 }
1389}
1390
Colin Cross3702ac72016-08-11 11:09:00 -07001391type topDownVisitorImpl struct{}
1392
1393func (topDownVisitorImpl) waitCount(module *moduleInfo) int {
1394 return len(module.reverseDeps)
1395}
1396
1397func (topDownVisitorImpl) propagate(module *moduleInfo) []*moduleInfo {
1398 return module.forwardDeps
1399}
1400
1401func (topDownVisitorImpl) visit(modules []*moduleInfo, visit func(*moduleInfo) bool) {
1402 for i := 0; i < len(modules); i++ {
1403 module := modules[len(modules)-1-i]
1404 if visit(module) {
1405 return
1406 }
1407 }
1408}
1409
1410var (
1411 bottomUpVisitor bottomUpVisitorImpl
1412 topDownVisitor topDownVisitorImpl
1413)
1414
Colin Cross49c279a2016-08-05 22:30:44 -07001415// Calls visit on each module, guaranteeing that visit is not called on a module until visit on all
1416// of its dependencies has finished.
Colin Cross3702ac72016-08-11 11:09:00 -07001417func (c *Context) parallelVisit(order visitOrderer, visit func(group *moduleInfo) bool) {
Colin Cross7addea32015-03-11 15:43:52 -07001418 doneCh := make(chan *moduleInfo)
Colin Cross0fff7422016-08-11 15:37:45 -07001419 cancelCh := make(chan bool)
Colin Cross691a60d2015-01-07 18:08:56 -08001420 count := 0
Colin Cross8900e9b2015-03-02 14:03:01 -08001421 cancel := false
Colin Cross691a60d2015-01-07 18:08:56 -08001422
Colin Cross7addea32015-03-11 15:43:52 -07001423 for _, module := range c.modulesSorted {
Colin Cross3702ac72016-08-11 11:09:00 -07001424 module.waitingCount = order.waitCount(module)
Colin Cross691a60d2015-01-07 18:08:56 -08001425 }
1426
Colin Cross7addea32015-03-11 15:43:52 -07001427 visitOne := func(module *moduleInfo) {
Colin Cross691a60d2015-01-07 18:08:56 -08001428 count++
1429 go func() {
Colin Cross7addea32015-03-11 15:43:52 -07001430 ret := visit(module)
Colin Cross8900e9b2015-03-02 14:03:01 -08001431 if ret {
Colin Cross0fff7422016-08-11 15:37:45 -07001432 cancelCh <- true
Colin Cross8900e9b2015-03-02 14:03:01 -08001433 }
Colin Cross7addea32015-03-11 15:43:52 -07001434 doneCh <- module
Colin Cross691a60d2015-01-07 18:08:56 -08001435 }()
1436 }
1437
Colin Cross7addea32015-03-11 15:43:52 -07001438 for _, module := range c.modulesSorted {
1439 if module.waitingCount == 0 {
1440 visitOne(module)
Colin Cross691a60d2015-01-07 18:08:56 -08001441 }
1442 }
1443
Colin Cross11e3b0d2015-02-04 10:41:00 -08001444 for count > 0 {
Colin Cross691a60d2015-01-07 18:08:56 -08001445 select {
Colin Cross0fff7422016-08-11 15:37:45 -07001446 case cancel = <-cancelCh:
Colin Cross7addea32015-03-11 15:43:52 -07001447 case doneModule := <-doneCh:
Colin Cross8900e9b2015-03-02 14:03:01 -08001448 if !cancel {
Colin Cross3702ac72016-08-11 11:09:00 -07001449 for _, module := range order.propagate(doneModule) {
1450 module.waitingCount--
1451 if module.waitingCount == 0 {
1452 visitOne(module)
Colin Cross8900e9b2015-03-02 14:03:01 -08001453 }
Colin Cross691a60d2015-01-07 18:08:56 -08001454 }
1455 }
1456 count--
Colin Cross691a60d2015-01-07 18:08:56 -08001457 }
1458 }
1459}
1460
1461// updateDependencies recursively walks the module dependency graph and updates
1462// additional fields based on the dependencies. It builds a sorted list of modules
1463// such that dependencies of a module always appear first, and populates reverse
1464// dependency links and counts of total dependencies. It also reports errors when
1465// it encounters dependency cycles. This should called after resolveDependencies,
1466// as well as after any mutator pass has called addDependency
1467func (c *Context) updateDependencies() (errs []error) {
Colin Cross7addea32015-03-11 15:43:52 -07001468 visited := make(map[*moduleInfo]bool) // modules that were already checked
1469 checking := make(map[*moduleInfo]bool) // modules actively being checked
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001470
Colin Cross7addea32015-03-11 15:43:52 -07001471 sorted := make([]*moduleInfo, 0, len(c.moduleInfo))
Colin Cross573a2fd2014-12-17 14:16:51 -08001472
Colin Cross7addea32015-03-11 15:43:52 -07001473 var check func(group *moduleInfo) []*moduleInfo
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001474
Colin Cross7addea32015-03-11 15:43:52 -07001475 cycleError := func(cycle []*moduleInfo) {
Colin Cross10b54db2015-03-11 14:40:30 -07001476 // We are the "start" of the cycle, so we're responsible
1477 // for generating the errors. The cycle list is in
1478 // reverse order because all the 'check' calls append
1479 // their own module to the list.
Colin Cross2c628442016-10-07 17:13:10 -07001480 errs = append(errs, &BlueprintError{
Colin Cross10b54db2015-03-11 14:40:30 -07001481 Err: fmt.Errorf("encountered dependency cycle:"),
Colin Cross7addea32015-03-11 15:43:52 -07001482 Pos: cycle[len(cycle)-1].pos,
Colin Cross10b54db2015-03-11 14:40:30 -07001483 })
1484
1485 // Iterate backwards through the cycle list.
Colin Cross0e4607e2015-03-24 16:42:56 -07001486 curModule := cycle[0]
Colin Cross10b54db2015-03-11 14:40:30 -07001487 for i := len(cycle) - 1; i >= 0; i-- {
Colin Cross7addea32015-03-11 15:43:52 -07001488 nextModule := cycle[i]
Colin Cross2c628442016-10-07 17:13:10 -07001489 errs = append(errs, &BlueprintError{
Colin Cross10b54db2015-03-11 14:40:30 -07001490 Err: fmt.Errorf(" %q depends on %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001491 curModule.Name(),
1492 nextModule.Name()),
Colin Cross7fcb7b02015-11-03 17:33:29 -08001493 Pos: curModule.pos,
Colin Cross10b54db2015-03-11 14:40:30 -07001494 })
Colin Cross7addea32015-03-11 15:43:52 -07001495 curModule = nextModule
Colin Cross10b54db2015-03-11 14:40:30 -07001496 }
1497 }
1498
Colin Cross7addea32015-03-11 15:43:52 -07001499 check = func(module *moduleInfo) []*moduleInfo {
1500 visited[module] = true
1501 checking[module] = true
1502 defer delete(checking, module)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001503
Colin Cross7addea32015-03-11 15:43:52 -07001504 deps := make(map[*moduleInfo]bool)
1505
1506 // Add an implicit dependency ordering on all earlier modules in the same module group
1507 for _, dep := range module.group.modules {
1508 if dep == module {
1509 break
Colin Crossbbfa51a2014-12-17 16:12:41 -08001510 }
Colin Cross7addea32015-03-11 15:43:52 -07001511 deps[dep] = true
Colin Crossbbfa51a2014-12-17 16:12:41 -08001512 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001513
Colin Cross7addea32015-03-11 15:43:52 -07001514 for _, dep := range module.directDeps {
Colin Cross2c1f3d12016-04-11 15:47:28 -07001515 deps[dep.module] = true
Colin Cross7addea32015-03-11 15:43:52 -07001516 }
1517
1518 module.reverseDeps = []*moduleInfo{}
Colin Cross3702ac72016-08-11 11:09:00 -07001519 module.forwardDeps = []*moduleInfo{}
Colin Cross691a60d2015-01-07 18:08:56 -08001520
Colin Crossbbfa51a2014-12-17 16:12:41 -08001521 for dep := range deps {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001522 if checking[dep] {
1523 // This is a cycle.
Colin Cross7addea32015-03-11 15:43:52 -07001524 return []*moduleInfo{dep, module}
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001525 }
1526
1527 if !visited[dep] {
1528 cycle := check(dep)
1529 if cycle != nil {
Colin Cross7addea32015-03-11 15:43:52 -07001530 if cycle[0] == module {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001531 // We are the "start" of the cycle, so we're responsible
1532 // for generating the errors. The cycle list is in
1533 // reverse order because all the 'check' calls append
1534 // their own module to the list.
Colin Cross10b54db2015-03-11 14:40:30 -07001535 cycleError(cycle)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001536
1537 // We can continue processing this module's children to
1538 // find more cycles. Since all the modules that were
1539 // part of the found cycle were marked as visited we
1540 // won't run into that cycle again.
1541 } else {
1542 // We're not the "start" of the cycle, so we just append
1543 // our module to the list and return it.
Colin Cross7addea32015-03-11 15:43:52 -07001544 return append(cycle, module)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001545 }
1546 }
1547 }
Colin Cross691a60d2015-01-07 18:08:56 -08001548
Colin Cross3702ac72016-08-11 11:09:00 -07001549 module.forwardDeps = append(module.forwardDeps, dep)
Colin Cross7addea32015-03-11 15:43:52 -07001550 dep.reverseDeps = append(dep.reverseDeps, module)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001551 }
1552
Colin Cross7addea32015-03-11 15:43:52 -07001553 sorted = append(sorted, module)
Colin Cross573a2fd2014-12-17 14:16:51 -08001554
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001555 return nil
1556 }
1557
Colin Cross7addea32015-03-11 15:43:52 -07001558 for _, module := range c.moduleInfo {
1559 if !visited[module] {
1560 cycle := check(module)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001561 if cycle != nil {
Colin Cross7addea32015-03-11 15:43:52 -07001562 if cycle[len(cycle)-1] != module {
Colin Cross10b54db2015-03-11 14:40:30 -07001563 panic("inconceivable!")
1564 }
1565 cycleError(cycle)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001566 }
1567 }
1568 }
1569
Colin Cross7addea32015-03-11 15:43:52 -07001570 c.modulesSorted = sorted
Colin Cross573a2fd2014-12-17 14:16:51 -08001571
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001572 return
1573}
1574
Jamie Gennisd4e10182014-06-12 20:06:50 -07001575// PrepareBuildActions generates an internal representation of all the build
1576// actions that need to be performed. This process involves invoking the
1577// GenerateBuildActions method on each of the Module objects created during the
1578// parse phase and then on each of the registered Singleton objects.
1579//
1580// If the ResolveDependencies method has not already been called it is called
1581// automatically by this method.
1582//
1583// The config argument is made available to all of the Module and Singleton
1584// objects via the Config method on the ModuleContext and SingletonContext
1585// objects passed to GenerateBuildActions. It is also passed to the functions
1586// specified via PoolFunc, RuleFunc, and VariableFunc so that they can compute
1587// config-specific values.
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001588//
1589// The returned deps is a list of the ninja files dependencies that were added
Dan Willemsena481ae22015-12-18 15:18:03 -08001590// by the modules and singletons via the ModuleContext.AddNinjaFileDeps(),
1591// SingletonContext.AddNinjaFileDeps(), and PackageContext.AddNinjaFileDeps()
1592// methods.
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001593func (c *Context) PrepareBuildActions(config interface{}) (deps []string, errs []error) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001594 c.buildActionsReady = false
1595
1596 if !c.dependenciesReady {
Jamie Gennisb9e87f62014-09-24 20:28:11 -07001597 errs := c.ResolveDependencies(config)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001598 if len(errs) > 0 {
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001599 return nil, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001600 }
1601 }
1602
1603 liveGlobals := newLiveTracker(config)
1604
1605 c.initSpecialVariables()
1606
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001607 depsModules, errs := c.generateModuleBuildActions(config, liveGlobals)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001608 if len(errs) > 0 {
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001609 return nil, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001610 }
1611
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001612 depsSingletons, errs := c.generateSingletonBuildActions(config, liveGlobals)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001613 if len(errs) > 0 {
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001614 return nil, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001615 }
1616
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001617 deps = append(depsModules, depsSingletons...)
1618
Colin Crossa2599452015-11-18 16:01:01 -08001619 if c.ninjaBuildDir != nil {
1620 liveGlobals.addNinjaStringDeps(c.ninjaBuildDir)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001621 }
1622
Dan Willemsena481ae22015-12-18 15:18:03 -08001623 pkgNames, depsPackages := c.makeUniquePackageNames(liveGlobals)
1624
1625 deps = append(deps, depsPackages...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001626
1627 // This will panic if it finds a problem since it's a programming error.
1628 c.checkForVariableReferenceCycles(liveGlobals.variables, pkgNames)
1629
1630 c.pkgNames = pkgNames
1631 c.globalVariables = liveGlobals.variables
1632 c.globalPools = liveGlobals.pools
1633 c.globalRules = liveGlobals.rules
1634
1635 c.buildActionsReady = true
1636
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001637 return deps, nil
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001638}
1639
Colin Crossc9028482014-12-18 16:28:54 -08001640func (c *Context) runMutators(config interface{}) (errs []error) {
Colin Crossf8b50422016-08-10 12:56:40 -07001641 var mutators []*mutatorInfo
Colin Cross763b6f12015-10-29 15:32:56 -07001642
Colin Crossf8b50422016-08-10 12:56:40 -07001643 mutators = append(mutators, c.earlyMutatorInfo...)
1644 mutators = append(mutators, c.mutatorInfo...)
1645
1646 for _, mutator := range mutators {
Colin Crossc9028482014-12-18 16:28:54 -08001647 if mutator.topDownMutator != nil {
Colin Cross3702ac72016-08-11 11:09:00 -07001648 errs = c.runMutator(config, mutator, topDownMutator)
Colin Crossc9028482014-12-18 16:28:54 -08001649 } else if mutator.bottomUpMutator != nil {
Colin Cross3702ac72016-08-11 11:09:00 -07001650 errs = c.runMutator(config, mutator, bottomUpMutator)
Colin Crossc9028482014-12-18 16:28:54 -08001651 } else {
1652 panic("no mutator set on " + mutator.name)
1653 }
1654 if len(errs) > 0 {
1655 return errs
1656 }
1657 }
1658
1659 return nil
1660}
1661
Colin Cross3702ac72016-08-11 11:09:00 -07001662type mutatorDirection interface {
1663 run(mutator *mutatorInfo, ctx *mutatorContext)
1664 orderer() visitOrderer
1665 fmt.Stringer
Colin Crossc9028482014-12-18 16:28:54 -08001666}
1667
Colin Cross3702ac72016-08-11 11:09:00 -07001668type bottomUpMutatorImpl struct{}
1669
1670func (bottomUpMutatorImpl) run(mutator *mutatorInfo, ctx *mutatorContext) {
1671 mutator.bottomUpMutator(ctx)
1672}
1673
1674func (bottomUpMutatorImpl) orderer() visitOrderer {
1675 return bottomUpVisitor
1676}
1677
1678func (bottomUpMutatorImpl) String() string {
1679 return "bottom up mutator"
1680}
1681
1682type topDownMutatorImpl struct{}
1683
1684func (topDownMutatorImpl) run(mutator *mutatorInfo, ctx *mutatorContext) {
1685 mutator.topDownMutator(ctx)
1686}
1687
1688func (topDownMutatorImpl) orderer() visitOrderer {
1689 return topDownVisitor
1690}
1691
1692func (topDownMutatorImpl) String() string {
1693 return "top down mutator"
1694}
1695
1696var (
1697 topDownMutator topDownMutatorImpl
1698 bottomUpMutator bottomUpMutatorImpl
1699)
1700
Colin Cross49c279a2016-08-05 22:30:44 -07001701type reverseDep struct {
1702 module *moduleInfo
1703 dep depInfo
1704}
1705
Colin Cross3702ac72016-08-11 11:09:00 -07001706func (c *Context) runMutator(config interface{}, mutator *mutatorInfo,
1707 direction mutatorDirection) (errs []error) {
Colin Cross49c279a2016-08-05 22:30:44 -07001708
1709 newModuleInfo := make(map[Module]*moduleInfo)
1710 for k, v := range c.moduleInfo {
1711 newModuleInfo[k] = v
1712 }
Colin Crossc9028482014-12-18 16:28:54 -08001713
Colin Cross0ce142c2016-12-09 10:29:05 -08001714 type globalStateChange struct {
1715 reverse []reverseDep
1716 rename []rename
1717 replace []replace
1718 }
1719
Colin Cross2c1f3d12016-04-11 15:47:28 -07001720 reverseDeps := make(map[*moduleInfo][]depInfo)
Colin Cross0ce142c2016-12-09 10:29:05 -08001721 var rename []rename
1722 var replace []replace
Colin Cross8d8a7af2015-11-03 16:41:29 -08001723
Colin Cross49c279a2016-08-05 22:30:44 -07001724 errsCh := make(chan []error)
Colin Cross0ce142c2016-12-09 10:29:05 -08001725 globalStateCh := make(chan globalStateChange)
Colin Cross49c279a2016-08-05 22:30:44 -07001726 newModulesCh := make(chan []*moduleInfo)
1727 done := make(chan bool)
Colin Crossc9028482014-12-18 16:28:54 -08001728
Colin Cross3702ac72016-08-11 11:09:00 -07001729 c.depsModified = 0
1730
Colin Cross49c279a2016-08-05 22:30:44 -07001731 visit := func(module *moduleInfo) bool {
Jamie Gennisc7988252015-04-14 23:28:10 -04001732 if module.splitModules != nil {
1733 panic("split module found in sorted module list")
1734 }
1735
Colin Cross7addea32015-03-11 15:43:52 -07001736 mctx := &mutatorContext{
1737 baseModuleContext: baseModuleContext{
1738 context: c,
1739 config: config,
1740 module: module,
1741 },
Colin Cross49c279a2016-08-05 22:30:44 -07001742 name: mutator.name,
Colin Cross7addea32015-03-11 15:43:52 -07001743 }
Colin Crossc9028482014-12-18 16:28:54 -08001744
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001745 func() {
1746 defer func() {
1747 if r := recover(); r != nil {
Colin Cross3702ac72016-08-11 11:09:00 -07001748 in := fmt.Sprintf("%s %q for %s", direction, mutator.name, module)
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001749 if err, ok := r.(panicError); ok {
1750 err.addIn(in)
1751 mctx.error(err)
1752 } else {
1753 mctx.error(newPanicErrorf(r, in))
1754 }
1755 }
1756 }()
Colin Cross3702ac72016-08-11 11:09:00 -07001757 direction.run(mutator, mctx)
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001758 }()
Colin Cross49c279a2016-08-05 22:30:44 -07001759
Colin Cross7addea32015-03-11 15:43:52 -07001760 if len(mctx.errs) > 0 {
Colin Cross0fff7422016-08-11 15:37:45 -07001761 errsCh <- mctx.errs
Colin Cross49c279a2016-08-05 22:30:44 -07001762 return true
Colin Cross7addea32015-03-11 15:43:52 -07001763 }
Colin Crossc9028482014-12-18 16:28:54 -08001764
Colin Cross49c279a2016-08-05 22:30:44 -07001765 if len(mctx.newModules) > 0 {
1766 newModulesCh <- mctx.newModules
1767 }
1768
Colin Cross0ce142c2016-12-09 10:29:05 -08001769 if len(mctx.reverseDeps) > 0 || len(mctx.replace) > 0 || len(mctx.rename) > 0 {
1770 globalStateCh <- globalStateChange{
1771 reverse: mctx.reverseDeps,
1772 replace: mctx.replace,
1773 rename: mctx.rename,
1774 }
Colin Cross49c279a2016-08-05 22:30:44 -07001775 }
1776
1777 return false
1778 }
1779
1780 // Process errs and reverseDeps in a single goroutine
1781 go func() {
1782 for {
1783 select {
1784 case newErrs := <-errsCh:
1785 errs = append(errs, newErrs...)
Colin Cross0ce142c2016-12-09 10:29:05 -08001786 case globalStateChange := <-globalStateCh:
1787 for _, r := range globalStateChange.reverse {
Colin Cross49c279a2016-08-05 22:30:44 -07001788 reverseDeps[r.module] = append(reverseDeps[r.module], r.dep)
1789 }
Colin Cross0ce142c2016-12-09 10:29:05 -08001790 replace = append(replace, globalStateChange.replace...)
1791 rename = append(rename, globalStateChange.rename...)
Colin Cross49c279a2016-08-05 22:30:44 -07001792 case newModules := <-newModulesCh:
1793 for _, m := range newModules {
1794 newModuleInfo[m.logicModule] = m
1795 }
1796 case <-done:
1797 return
Colin Crossc9028482014-12-18 16:28:54 -08001798 }
1799 }
Colin Cross49c279a2016-08-05 22:30:44 -07001800 }()
Colin Crossc9028482014-12-18 16:28:54 -08001801
Colin Cross49c279a2016-08-05 22:30:44 -07001802 if mutator.parallel {
Colin Cross3702ac72016-08-11 11:09:00 -07001803 c.parallelVisit(direction.orderer(), visit)
Colin Cross49c279a2016-08-05 22:30:44 -07001804 } else {
Colin Cross3702ac72016-08-11 11:09:00 -07001805 direction.orderer().visit(c.modulesSorted, visit)
Colin Cross49c279a2016-08-05 22:30:44 -07001806 }
1807
1808 done <- true
1809
1810 if len(errs) > 0 {
1811 return errs
1812 }
1813
1814 c.moduleInfo = newModuleInfo
1815
1816 for _, group := range c.moduleGroups {
1817 for i := 0; i < len(group.modules); i++ {
1818 module := group.modules[i]
1819
1820 // Update module group to contain newly split variants
1821 if module.splitModules != nil {
1822 group.modules, i = spliceModules(group.modules, i, module.splitModules)
1823 }
1824
1825 // Fix up any remaining dependencies on modules that were split into variants
1826 // by replacing them with the first variant
1827 for j, dep := range module.directDeps {
1828 if dep.module.logicModule == nil {
1829 module.directDeps[j].module = dep.module.splitModules[0]
1830 }
1831 }
Colin Cross7addea32015-03-11 15:43:52 -07001832 }
Colin Crossc9028482014-12-18 16:28:54 -08001833 }
1834
Colin Cross8d8a7af2015-11-03 16:41:29 -08001835 for module, deps := range reverseDeps {
Colin Cross2c1f3d12016-04-11 15:47:28 -07001836 sort.Sort(depSorter(deps))
Colin Cross8d8a7af2015-11-03 16:41:29 -08001837 module.directDeps = append(module.directDeps, deps...)
Colin Cross3702ac72016-08-11 11:09:00 -07001838 c.depsModified++
Colin Cross8d8a7af2015-11-03 16:41:29 -08001839 }
1840
Colin Cross0ce142c2016-12-09 10:29:05 -08001841 errs = c.handleRenames(rename)
1842 if len(errs) > 0 {
1843 return errs
1844 }
1845
1846 errs = c.handleReplacements(replace)
Colin Crossc4e5b812016-10-12 10:45:05 -07001847 if len(errs) > 0 {
1848 return errs
1849 }
1850
Colin Cross3702ac72016-08-11 11:09:00 -07001851 if c.depsModified > 0 {
1852 errs = c.updateDependencies()
1853 if len(errs) > 0 {
1854 return errs
1855 }
Colin Crossc9028482014-12-18 16:28:54 -08001856 }
1857
1858 return errs
1859}
1860
Colin Cross910242b2016-04-11 15:41:52 -07001861// Replaces every build logic module with a clone of itself. Prevents introducing problems where
1862// a mutator sets a non-property member variable on a module, which works until a later mutator
1863// creates variants of that module.
1864func (c *Context) cloneModules() {
Colin Crossc93490c2016-08-09 14:21:02 -07001865 type update struct {
1866 orig Module
1867 clone *moduleInfo
1868 }
1869 ch := make(chan update, 100)
1870
Colin Cross910242b2016-04-11 15:41:52 -07001871 for _, m := range c.modulesSorted {
Colin Crossc93490c2016-08-09 14:21:02 -07001872 go func(m *moduleInfo) {
1873 origLogicModule := m.logicModule
1874 m.logicModule, m.moduleProperties = c.cloneLogicModule(m)
1875 ch <- update{origLogicModule, m}
1876 }(m)
1877 }
1878
1879 for i := 0; i < len(c.modulesSorted); i++ {
1880 update := <-ch
1881 delete(c.moduleInfo, update.orig)
1882 c.moduleInfo[update.clone.logicModule] = update.clone
Colin Cross910242b2016-04-11 15:41:52 -07001883 }
1884}
1885
Colin Cross49c279a2016-08-05 22:30:44 -07001886// Removes modules[i] from the list and inserts newModules... where it was located, returning
1887// the new slice and the index of the last inserted element
1888func spliceModules(modules []*moduleInfo, i int, newModules []*moduleInfo) ([]*moduleInfo, int) {
Colin Cross7addea32015-03-11 15:43:52 -07001889 spliceSize := len(newModules)
1890 newLen := len(modules) + spliceSize - 1
1891 var dest []*moduleInfo
1892 if cap(modules) >= len(modules)-1+len(newModules) {
1893 // We can fit the splice in the existing capacity, do everything in place
1894 dest = modules[:newLen]
1895 } else {
1896 dest = make([]*moduleInfo, newLen)
1897 copy(dest, modules[:i])
1898 }
1899
1900 // Move the end of the slice over by spliceSize-1
Colin Cross72bd1932015-03-16 00:13:59 -07001901 copy(dest[i+spliceSize:], modules[i+1:])
Colin Cross7addea32015-03-11 15:43:52 -07001902
1903 // Copy the new modules into the slice
Colin Cross72bd1932015-03-16 00:13:59 -07001904 copy(dest[i:], newModules)
Colin Cross7addea32015-03-11 15:43:52 -07001905
Colin Cross49c279a2016-08-05 22:30:44 -07001906 return dest, i + spliceSize - 1
Colin Cross7addea32015-03-11 15:43:52 -07001907}
1908
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001909func (c *Context) initSpecialVariables() {
Colin Crossa2599452015-11-18 16:01:01 -08001910 c.ninjaBuildDir = nil
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001911 c.requiredNinjaMajor = 1
Dan Willemsen5c43e072016-10-25 21:26:12 -07001912 c.requiredNinjaMinor = 7
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001913 c.requiredNinjaMicro = 0
1914}
1915
Jamie Gennis6eb4d242014-06-11 18:31:16 -07001916func (c *Context) generateModuleBuildActions(config interface{},
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001917 liveGlobals *liveTracker) ([]string, []error) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001918
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001919 var deps []string
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001920 var errs []error
1921
Colin Cross691a60d2015-01-07 18:08:56 -08001922 cancelCh := make(chan struct{})
1923 errsCh := make(chan []error)
1924 depsCh := make(chan []string)
1925
1926 go func() {
1927 for {
1928 select {
1929 case <-cancelCh:
1930 close(cancelCh)
1931 return
1932 case newErrs := <-errsCh:
1933 errs = append(errs, newErrs...)
1934 case newDeps := <-depsCh:
1935 deps = append(deps, newDeps...)
1936
1937 }
1938 }
1939 }()
1940
Colin Cross3702ac72016-08-11 11:09:00 -07001941 c.parallelVisit(bottomUpVisitor, func(module *moduleInfo) bool {
Colin Cross7addea32015-03-11 15:43:52 -07001942 // The parent scope of the moduleContext's local scope gets overridden to be that of the
1943 // calling Go package on a per-call basis. Since the initial parent scope doesn't matter we
1944 // just set it to nil.
1945 prefix := moduleNamespacePrefix(module.group.ninjaName + "_" + module.variantName)
1946 scope := newLocalScope(nil, prefix)
Colin Cross6134a5c2015-02-10 11:26:26 -08001947
Colin Cross7addea32015-03-11 15:43:52 -07001948 mctx := &moduleContext{
1949 baseModuleContext: baseModuleContext{
1950 context: c,
1951 config: config,
1952 module: module,
1953 },
Colin Cross036a1df2015-12-17 15:49:30 -08001954 scope: scope,
1955 handledMissingDeps: module.missingDeps == nil,
Colin Cross7addea32015-03-11 15:43:52 -07001956 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001957
Colin Cross0aa6a5f2016-01-07 13:43:09 -08001958 func() {
1959 defer func() {
1960 if r := recover(); r != nil {
1961 in := fmt.Sprintf("GenerateBuildActions for %s", module)
1962 if err, ok := r.(panicError); ok {
1963 err.addIn(in)
1964 mctx.error(err)
1965 } else {
1966 mctx.error(newPanicErrorf(r, in))
1967 }
1968 }
1969 }()
1970 mctx.module.logicModule.GenerateBuildActions(mctx)
1971 }()
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001972
Colin Cross7addea32015-03-11 15:43:52 -07001973 if len(mctx.errs) > 0 {
1974 errsCh <- mctx.errs
1975 return true
1976 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001977
Colin Cross036a1df2015-12-17 15:49:30 -08001978 if module.missingDeps != nil && !mctx.handledMissingDeps {
1979 var errs []error
1980 for _, depName := range module.missingDeps {
Colin Cross2c628442016-10-07 17:13:10 -07001981 errs = append(errs, &BlueprintError{
Colin Cross036a1df2015-12-17 15:49:30 -08001982 Err: fmt.Errorf("%q depends on undefined module %q",
Colin Cross0b7e83e2016-05-17 14:58:05 -07001983 module.Name(), depName),
Colin Cross036a1df2015-12-17 15:49:30 -08001984 Pos: module.pos,
1985 })
1986 }
1987 errsCh <- errs
1988 return true
1989 }
1990
Colin Cross7addea32015-03-11 15:43:52 -07001991 depsCh <- mctx.ninjaFileDeps
Mathias Agopian5b8477d2014-06-25 17:21:54 -07001992
Colin Crossab6d7902015-03-11 16:17:52 -07001993 newErrs := c.processLocalBuildActions(&module.actionDefs,
Colin Cross7addea32015-03-11 15:43:52 -07001994 &mctx.actionDefs, liveGlobals)
1995 if len(newErrs) > 0 {
1996 errsCh <- newErrs
1997 return true
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001998 }
Colin Cross8900e9b2015-03-02 14:03:01 -08001999 return false
Colin Cross691a60d2015-01-07 18:08:56 -08002000 })
2001
2002 cancelCh <- struct{}{}
2003 <-cancelCh
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002004
Mathias Agopian5b8477d2014-06-25 17:21:54 -07002005 return deps, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002006}
2007
Jamie Gennis6eb4d242014-06-11 18:31:16 -07002008func (c *Context) generateSingletonBuildActions(config interface{},
Mathias Agopian5b8477d2014-06-25 17:21:54 -07002009 liveGlobals *liveTracker) ([]string, []error) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002010
Mathias Agopian5b8477d2014-06-25 17:21:54 -07002011 var deps []string
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002012 var errs []error
Mathias Agopian5b8477d2014-06-25 17:21:54 -07002013
Yuchen Wub9103ef2015-08-25 17:58:17 -07002014 for _, info := range c.singletonInfo {
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07002015 // The parent scope of the singletonContext's local scope gets overridden to be that of the
2016 // calling Go package on a per-call basis. Since the initial parent scope doesn't matter we
2017 // just set it to nil.
Yuchen Wub9103ef2015-08-25 17:58:17 -07002018 scope := newLocalScope(nil, singletonNamespacePrefix(info.name))
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002019
2020 sctx := &singletonContext{
2021 context: c,
2022 config: config,
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07002023 scope: scope,
Dan Willemsen4bb62762016-01-14 15:42:54 -08002024 globals: liveGlobals,
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002025 }
2026
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002027 func() {
2028 defer func() {
2029 if r := recover(); r != nil {
2030 in := fmt.Sprintf("GenerateBuildActions for singleton %s", info.name)
2031 if err, ok := r.(panicError); ok {
2032 err.addIn(in)
2033 sctx.error(err)
2034 } else {
2035 sctx.error(newPanicErrorf(r, in))
2036 }
2037 }
2038 }()
2039 info.singleton.GenerateBuildActions(sctx)
2040 }()
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002041
2042 if len(sctx.errs) > 0 {
2043 errs = append(errs, sctx.errs...)
2044 if len(errs) > maxErrors {
2045 break
2046 }
2047 continue
2048 }
2049
Mathias Agopian5b8477d2014-06-25 17:21:54 -07002050 deps = append(deps, sctx.ninjaFileDeps...)
2051
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002052 newErrs := c.processLocalBuildActions(&info.actionDefs,
2053 &sctx.actionDefs, liveGlobals)
2054 errs = append(errs, newErrs...)
2055 if len(errs) > maxErrors {
2056 break
2057 }
2058 }
2059
Mathias Agopian5b8477d2014-06-25 17:21:54 -07002060 return deps, errs
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002061}
2062
2063func (c *Context) processLocalBuildActions(out, in *localBuildActions,
2064 liveGlobals *liveTracker) []error {
2065
2066 var errs []error
2067
2068 // First we go through and add everything referenced by the module's
2069 // buildDefs to the live globals set. This will end up adding the live
2070 // locals to the set as well, but we'll take them out after.
2071 for _, def := range in.buildDefs {
2072 err := liveGlobals.AddBuildDefDeps(def)
2073 if err != nil {
2074 errs = append(errs, err)
2075 }
2076 }
2077
2078 if len(errs) > 0 {
2079 return errs
2080 }
2081
Colin Crossc9028482014-12-18 16:28:54 -08002082 out.buildDefs = append(out.buildDefs, in.buildDefs...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002083
2084 // We use the now-incorrect set of live "globals" to determine which local
2085 // definitions are live. As we go through copying those live locals to the
Colin Crossc9028482014-12-18 16:28:54 -08002086 // moduleGroup we remove them from the live globals set.
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002087 for _, v := range in.variables {
Colin Crossab6d7902015-03-11 16:17:52 -07002088 isLive := liveGlobals.RemoveVariableIfLive(v)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002089 if isLive {
2090 out.variables = append(out.variables, v)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002091 }
2092 }
2093
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002094 for _, r := range in.rules {
Colin Crossab6d7902015-03-11 16:17:52 -07002095 isLive := liveGlobals.RemoveRuleIfLive(r)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002096 if isLive {
2097 out.rules = append(out.rules, r)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002098 }
2099 }
2100
2101 return nil
2102}
2103
Yuchen Wu222e2452015-10-06 14:03:27 -07002104func (c *Context) walkDeps(topModule *moduleInfo,
Colin Crossbafd5f52016-08-06 22:52:01 -07002105 visitDown func(depInfo, *moduleInfo) bool, visitUp func(depInfo, *moduleInfo)) {
Yuchen Wu222e2452015-10-06 14:03:27 -07002106
2107 visited := make(map[*moduleInfo]bool)
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002108 var visiting *moduleInfo
2109
2110 defer func() {
2111 if r := recover(); r != nil {
Colin Crossbafd5f52016-08-06 22:52:01 -07002112 panic(newPanicErrorf(r, "WalkDeps(%s, %s, %s) for dependency %s",
2113 topModule, funcName(visitDown), funcName(visitUp), visiting))
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002114 }
2115 }()
Yuchen Wu222e2452015-10-06 14:03:27 -07002116
2117 var walk func(module *moduleInfo)
2118 walk = func(module *moduleInfo) {
Colin Cross2c1f3d12016-04-11 15:47:28 -07002119 for _, dep := range module.directDeps {
2120 if !visited[dep.module] {
2121 visited[dep.module] = true
2122 visiting = dep.module
Colin Crossbafd5f52016-08-06 22:52:01 -07002123 recurse := true
2124 if visitDown != nil {
2125 recurse = visitDown(dep, module)
2126 }
2127 if recurse {
Colin Cross2c1f3d12016-04-11 15:47:28 -07002128 walk(dep.module)
Yuchen Wu222e2452015-10-06 14:03:27 -07002129 }
Colin Crossbafd5f52016-08-06 22:52:01 -07002130 if visitUp != nil {
2131 visitUp(dep, module)
2132 }
Yuchen Wu222e2452015-10-06 14:03:27 -07002133 }
2134 }
2135 }
2136
2137 walk(topModule)
2138}
2139
Colin Cross9cfd1982016-10-11 09:58:53 -07002140type replace struct {
2141 from, to *moduleInfo
2142}
2143
Colin Crossc4e5b812016-10-12 10:45:05 -07002144type rename struct {
2145 group *moduleGroup
2146 name string
2147}
2148
Colin Cross0ce142c2016-12-09 10:29:05 -08002149func (c *Context) moduleMatchingVariant(module *moduleInfo, name string) *moduleInfo {
Colin Cross9cfd1982016-10-11 09:58:53 -07002150 targets := c.modulesFromName(name)
2151
2152 if targets == nil {
Colin Cross0ce142c2016-12-09 10:29:05 -08002153 return nil
Colin Cross9cfd1982016-10-11 09:58:53 -07002154 }
2155
Colin Cross9cfd1982016-10-11 09:58:53 -07002156 for _, m := range targets {
2157 if module.variantName == m.variantName {
Colin Cross0ce142c2016-12-09 10:29:05 -08002158 return m
Colin Cross9cfd1982016-10-11 09:58:53 -07002159 }
2160 }
2161
Colin Cross0ce142c2016-12-09 10:29:05 -08002162 return nil
Colin Cross9cfd1982016-10-11 09:58:53 -07002163}
2164
Colin Cross0ce142c2016-12-09 10:29:05 -08002165func (c *Context) handleRenames(renames []rename) []error {
Colin Crossc4e5b812016-10-12 10:45:05 -07002166 var errs []error
Colin Cross0ce142c2016-12-09 10:29:05 -08002167 for _, rename := range renames {
Colin Crossc4e5b812016-10-12 10:45:05 -07002168 group, name := rename.group, rename.name
2169 if name == group.name {
2170 continue
2171 }
2172
2173 existing := c.moduleNames[name]
2174 if existing != nil {
2175 errs = append(errs,
2176 &BlueprintError{
2177 Err: fmt.Errorf("renaming module %q to %q conflicts with existing module",
2178 group.name, name),
2179 Pos: group.modules[0].pos,
2180 },
2181 &BlueprintError{
2182 Err: fmt.Errorf("<-- existing module defined here"),
2183 Pos: existing.modules[0].pos,
2184 },
2185 )
2186 continue
2187 }
2188
2189 c.moduleNames[name] = group
2190 delete(c.moduleNames, group.name)
2191 group.name = name
2192 }
2193
Colin Cross0ce142c2016-12-09 10:29:05 -08002194 return errs
2195}
2196
2197func (c *Context) handleReplacements(replacements []replace) []error {
2198 var errs []error
2199 for _, replace := range replacements {
Colin Cross9cfd1982016-10-11 09:58:53 -07002200 for _, m := range replace.from.reverseDeps {
2201 for i, d := range m.directDeps {
2202 if d.module == replace.from {
2203 m.directDeps[i].module = replace.to
2204 }
2205 }
2206 }
2207
2208 atomic.AddUint32(&c.depsModified, 1)
2209 }
Colin Cross0ce142c2016-12-09 10:29:05 -08002210
Colin Crossc4e5b812016-10-12 10:45:05 -07002211 return errs
2212}
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002213
Colin Cross0b7e83e2016-05-17 14:58:05 -07002214func (c *Context) modulesFromName(name string) []*moduleInfo {
2215 if group := c.moduleNames[name]; group != nil {
2216 return group.modules
2217 }
2218 return nil
2219}
2220
Jamie Gennisc15544d2014-09-24 20:26:52 -07002221func (c *Context) sortedModuleNames() []string {
2222 if c.cachedSortedModuleNames == nil {
Colin Cross0b7e83e2016-05-17 14:58:05 -07002223 c.cachedSortedModuleNames = make([]string, 0, len(c.moduleNames))
2224 for moduleName := range c.moduleNames {
Jamie Gennisc15544d2014-09-24 20:26:52 -07002225 c.cachedSortedModuleNames = append(c.cachedSortedModuleNames,
2226 moduleName)
2227 }
2228 sort.Strings(c.cachedSortedModuleNames)
2229 }
2230
2231 return c.cachedSortedModuleNames
2232}
2233
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002234func (c *Context) visitAllModules(visit func(Module)) {
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002235 var module *moduleInfo
2236
2237 defer func() {
2238 if r := recover(); r != nil {
2239 panic(newPanicErrorf(r, "VisitAllModules(%s) for %s",
2240 funcName(visit), module))
2241 }
2242 }()
2243
Jamie Gennisc15544d2014-09-24 20:26:52 -07002244 for _, moduleName := range c.sortedModuleNames() {
Colin Cross0b7e83e2016-05-17 14:58:05 -07002245 modules := c.modulesFromName(moduleName)
2246 for _, module = range modules {
Colin Crossbbfa51a2014-12-17 16:12:41 -08002247 visit(module.logicModule)
2248 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002249 }
2250}
2251
2252func (c *Context) visitAllModulesIf(pred func(Module) bool,
2253 visit func(Module)) {
2254
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002255 var module *moduleInfo
2256
2257 defer func() {
2258 if r := recover(); r != nil {
2259 panic(newPanicErrorf(r, "VisitAllModulesIf(%s, %s) for %s",
2260 funcName(pred), funcName(visit), module))
2261 }
2262 }()
2263
Jamie Gennisc15544d2014-09-24 20:26:52 -07002264 for _, moduleName := range c.sortedModuleNames() {
Colin Cross0b7e83e2016-05-17 14:58:05 -07002265 modules := c.modulesFromName(moduleName)
2266 for _, module := range modules {
Colin Crossbbfa51a2014-12-17 16:12:41 -08002267 if pred(module.logicModule) {
2268 visit(module.logicModule)
2269 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002270 }
2271 }
2272}
2273
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002274func (c *Context) visitAllModuleVariants(module *moduleInfo,
2275 visit func(Module)) {
2276
2277 var variant *moduleInfo
2278
2279 defer func() {
2280 if r := recover(); r != nil {
2281 panic(newPanicErrorf(r, "VisitAllModuleVariants(%s, %s) for %s",
2282 module, funcName(visit), variant))
2283 }
2284 }()
2285
2286 for _, variant = range module.group.modules {
2287 visit(variant.logicModule)
2288 }
2289}
2290
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002291func (c *Context) requireNinjaVersion(major, minor, micro int) {
2292 if major != 1 {
2293 panic("ninja version with major version != 1 not supported")
2294 }
2295 if c.requiredNinjaMinor < minor {
2296 c.requiredNinjaMinor = minor
2297 c.requiredNinjaMicro = micro
2298 }
2299 if c.requiredNinjaMinor == minor && c.requiredNinjaMicro < micro {
2300 c.requiredNinjaMicro = micro
2301 }
2302}
2303
Colin Crossa2599452015-11-18 16:01:01 -08002304func (c *Context) setNinjaBuildDir(value *ninjaString) {
2305 if c.ninjaBuildDir == nil {
2306 c.ninjaBuildDir = value
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002307 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002308}
2309
2310func (c *Context) makeUniquePackageNames(
Dan Willemsena481ae22015-12-18 15:18:03 -08002311 liveGlobals *liveTracker) (map[*packageContext]string, []string) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002312
Dan Willemsenaeffbf72015-11-25 15:29:32 -08002313 pkgs := make(map[string]*packageContext)
2314 pkgNames := make(map[*packageContext]string)
2315 longPkgNames := make(map[*packageContext]bool)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002316
Dan Willemsenaeffbf72015-11-25 15:29:32 -08002317 processPackage := func(pctx *packageContext) {
Jamie Gennis2fb20952014-10-03 02:49:58 -07002318 if pctx == nil {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002319 // This is a built-in rule and has no package.
2320 return
2321 }
Jamie Gennis2fb20952014-10-03 02:49:58 -07002322 if _, ok := pkgNames[pctx]; ok {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002323 // We've already processed this package.
2324 return
2325 }
2326
Jamie Gennis2fb20952014-10-03 02:49:58 -07002327 otherPkg, present := pkgs[pctx.shortName]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002328 if present {
2329 // Short name collision. Both this package and the one that's
2330 // already there need to use their full names. We leave the short
2331 // name in pkgNames for now so future collisions still get caught.
Jamie Gennis2fb20952014-10-03 02:49:58 -07002332 longPkgNames[pctx] = true
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002333 longPkgNames[otherPkg] = true
2334 } else {
2335 // No collision so far. Tentatively set the package's name to be
2336 // its short name.
Jamie Gennis2fb20952014-10-03 02:49:58 -07002337 pkgNames[pctx] = pctx.shortName
Colin Cross0d441252015-04-14 18:02:20 -07002338 pkgs[pctx.shortName] = pctx
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002339 }
2340 }
2341
2342 // We try to give all packages their short name, but when we get collisions
2343 // we need to use the full unique package name.
2344 for v, _ := range liveGlobals.variables {
Jamie Gennis2fb20952014-10-03 02:49:58 -07002345 processPackage(v.packageContext())
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002346 }
2347 for p, _ := range liveGlobals.pools {
Jamie Gennis2fb20952014-10-03 02:49:58 -07002348 processPackage(p.packageContext())
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002349 }
2350 for r, _ := range liveGlobals.rules {
Jamie Gennis2fb20952014-10-03 02:49:58 -07002351 processPackage(r.packageContext())
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002352 }
2353
2354 // Add the packages that had collisions using their full unique names. This
2355 // will overwrite any short names that were added in the previous step.
Jamie Gennis2fb20952014-10-03 02:49:58 -07002356 for pctx := range longPkgNames {
2357 pkgNames[pctx] = pctx.fullName
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002358 }
2359
Dan Willemsena481ae22015-12-18 15:18:03 -08002360 // Create deps list from calls to PackageContext.AddNinjaFileDeps
2361 deps := []string{}
2362 for _, pkg := range pkgs {
2363 deps = append(deps, pkg.ninjaFileDeps...)
2364 }
2365
2366 return pkgNames, deps
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002367}
2368
2369func (c *Context) checkForVariableReferenceCycles(
Dan Willemsenaeffbf72015-11-25 15:29:32 -08002370 variables map[Variable]*ninjaString, pkgNames map[*packageContext]string) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002371
2372 visited := make(map[Variable]bool) // variables that were already checked
2373 checking := make(map[Variable]bool) // variables actively being checked
2374
2375 var check func(v Variable) []Variable
2376
2377 check = func(v Variable) []Variable {
2378 visited[v] = true
2379 checking[v] = true
2380 defer delete(checking, v)
2381
2382 value := variables[v]
2383 for _, dep := range value.variables {
2384 if checking[dep] {
2385 // This is a cycle.
2386 return []Variable{dep, v}
2387 }
2388
2389 if !visited[dep] {
2390 cycle := check(dep)
2391 if cycle != nil {
2392 if cycle[0] == v {
2393 // We are the "start" of the cycle, so we're responsible
2394 // for generating the errors. The cycle list is in
2395 // reverse order because all the 'check' calls append
2396 // their own module to the list.
2397 msgs := []string{"detected variable reference cycle:"}
2398
2399 // Iterate backwards through the cycle list.
2400 curName := v.fullName(pkgNames)
2401 curValue := value.Value(pkgNames)
2402 for i := len(cycle) - 1; i >= 0; i-- {
2403 next := cycle[i]
2404 nextName := next.fullName(pkgNames)
2405 nextValue := variables[next].Value(pkgNames)
2406
2407 msgs = append(msgs, fmt.Sprintf(
2408 " %q depends on %q", curName, nextName))
2409 msgs = append(msgs, fmt.Sprintf(
2410 " [%s = %s]", curName, curValue))
2411
2412 curName = nextName
2413 curValue = nextValue
2414 }
2415
2416 // Variable reference cycles are a programming error,
2417 // not the fault of the Blueprint file authors.
2418 panic(strings.Join(msgs, "\n"))
2419 } else {
2420 // We're not the "start" of the cycle, so we just append
2421 // our module to the list and return it.
2422 return append(cycle, v)
2423 }
2424 }
2425 }
2426 }
2427
2428 return nil
2429 }
2430
2431 for v := range variables {
2432 if !visited[v] {
2433 cycle := check(v)
2434 if cycle != nil {
2435 panic("inconceivable!")
2436 }
2437 }
2438 }
2439}
2440
Jamie Gennisaf435562014-10-27 22:34:56 -07002441// AllTargets returns a map all the build target names to the rule used to build
2442// them. This is the same information that is output by running 'ninja -t
2443// targets all'. If this is called before PrepareBuildActions successfully
2444// completes then ErrbuildActionsNotReady is returned.
2445func (c *Context) AllTargets() (map[string]string, error) {
2446 if !c.buildActionsReady {
2447 return nil, ErrBuildActionsNotReady
2448 }
2449
2450 targets := map[string]string{}
2451
2452 // Collect all the module build targets.
Colin Crossab6d7902015-03-11 16:17:52 -07002453 for _, module := range c.moduleInfo {
2454 for _, buildDef := range module.actionDefs.buildDefs {
Jamie Gennisaf435562014-10-27 22:34:56 -07002455 ruleName := buildDef.Rule.fullName(c.pkgNames)
Dan Willemsen5c43e072016-10-25 21:26:12 -07002456 for _, output := range append(buildDef.Outputs, buildDef.ImplicitOutputs...) {
Christian Zander6e2b2322014-11-21 15:12:08 -08002457 outputValue, err := output.Eval(c.globalVariables)
2458 if err != nil {
2459 return nil, err
2460 }
Jamie Gennisaf435562014-10-27 22:34:56 -07002461 targets[outputValue] = ruleName
2462 }
2463 }
2464 }
2465
2466 // Collect all the singleton build targets.
2467 for _, info := range c.singletonInfo {
2468 for _, buildDef := range info.actionDefs.buildDefs {
2469 ruleName := buildDef.Rule.fullName(c.pkgNames)
Dan Willemsen5c43e072016-10-25 21:26:12 -07002470 for _, output := range append(buildDef.Outputs, buildDef.ImplicitOutputs...) {
Christian Zander6e2b2322014-11-21 15:12:08 -08002471 outputValue, err := output.Eval(c.globalVariables)
2472 if err != nil {
Colin Crossfea2b752014-12-30 16:05:02 -08002473 return nil, err
Christian Zander6e2b2322014-11-21 15:12:08 -08002474 }
Jamie Gennisaf435562014-10-27 22:34:56 -07002475 targets[outputValue] = ruleName
2476 }
2477 }
2478 }
2479
2480 return targets, nil
2481}
2482
Colin Crossa2599452015-11-18 16:01:01 -08002483func (c *Context) NinjaBuildDir() (string, error) {
2484 if c.ninjaBuildDir != nil {
2485 return c.ninjaBuildDir.Eval(c.globalVariables)
2486 } else {
2487 return "", nil
2488 }
2489}
2490
Colin Cross4572edd2015-05-13 14:36:24 -07002491// ModuleTypePropertyStructs returns a mapping from module type name to a list of pointers to
2492// property structs returned by the factory for that module type.
2493func (c *Context) ModuleTypePropertyStructs() map[string][]interface{} {
2494 ret := make(map[string][]interface{})
2495 for moduleType, factory := range c.moduleFactories {
2496 _, ret[moduleType] = factory()
2497 }
2498
2499 return ret
2500}
2501
2502func (c *Context) ModuleName(logicModule Module) string {
2503 module := c.moduleInfo[logicModule]
Colin Cross0b7e83e2016-05-17 14:58:05 -07002504 return module.Name()
Colin Cross4572edd2015-05-13 14:36:24 -07002505}
2506
2507func (c *Context) ModuleDir(logicModule Module) string {
2508 module := c.moduleInfo[logicModule]
2509 return filepath.Dir(module.relBlueprintsFile)
2510}
2511
Colin Cross8c602f72015-12-17 18:02:11 -08002512func (c *Context) ModuleSubDir(logicModule Module) string {
2513 module := c.moduleInfo[logicModule]
2514 return module.variantName
2515}
2516
Dan Willemsenc98e55b2016-07-25 15:51:50 -07002517func (c *Context) ModuleType(logicModule Module) string {
2518 module := c.moduleInfo[logicModule]
2519 return module.typeName
2520}
2521
Colin Cross4572edd2015-05-13 14:36:24 -07002522func (c *Context) BlueprintFile(logicModule Module) string {
2523 module := c.moduleInfo[logicModule]
2524 return module.relBlueprintsFile
2525}
2526
2527func (c *Context) ModuleErrorf(logicModule Module, format string,
2528 args ...interface{}) error {
2529
2530 module := c.moduleInfo[logicModule]
Colin Cross2c628442016-10-07 17:13:10 -07002531 return &BlueprintError{
Colin Cross4572edd2015-05-13 14:36:24 -07002532 Err: fmt.Errorf(format, args...),
2533 Pos: module.pos,
2534 }
2535}
2536
2537func (c *Context) VisitAllModules(visit func(Module)) {
2538 c.visitAllModules(visit)
2539}
2540
2541func (c *Context) VisitAllModulesIf(pred func(Module) bool,
2542 visit func(Module)) {
2543
2544 c.visitAllModulesIf(pred, visit)
2545}
2546
2547func (c *Context) VisitDepsDepthFirst(module Module,
2548 visit func(Module)) {
2549
Colin Crossbafd5f52016-08-06 22:52:01 -07002550 topModule := c.moduleInfo[module]
2551
2552 var visiting *moduleInfo
2553
2554 defer func() {
2555 if r := recover(); r != nil {
2556 panic(newPanicErrorf(r, "VisitDepsDepthFirst(%s, %s) for dependency %s",
2557 topModule, funcName(visit), visiting))
2558 }
2559 }()
2560
2561 c.walkDeps(topModule, nil, func(dep depInfo, parent *moduleInfo) {
2562 visiting = dep.module
2563 visit(dep.module.logicModule)
2564 })
Colin Cross4572edd2015-05-13 14:36:24 -07002565}
2566
2567func (c *Context) VisitDepsDepthFirstIf(module Module,
2568 pred func(Module) bool, visit func(Module)) {
2569
Colin Crossbafd5f52016-08-06 22:52:01 -07002570 topModule := c.moduleInfo[module]
2571
2572 var visiting *moduleInfo
2573
2574 defer func() {
2575 if r := recover(); r != nil {
2576 panic(newPanicErrorf(r, "VisitDepsDepthFirstIf(%s, %s, %s) for dependency %s",
2577 topModule, funcName(pred), funcName(visit), visiting))
2578 }
2579 }()
2580
2581 c.walkDeps(topModule, nil, func(dep depInfo, parent *moduleInfo) {
2582 if pred(dep.module.logicModule) {
2583 visiting = dep.module
2584 visit(dep.module.logicModule)
2585 }
2586 })
Colin Cross4572edd2015-05-13 14:36:24 -07002587}
2588
Colin Cross24ad5872015-11-17 16:22:29 -08002589func (c *Context) PrimaryModule(module Module) Module {
2590 return c.moduleInfo[module].group.modules[0].logicModule
2591}
2592
2593func (c *Context) FinalModule(module Module) Module {
2594 modules := c.moduleInfo[module].group.modules
2595 return modules[len(modules)-1].logicModule
2596}
2597
2598func (c *Context) VisitAllModuleVariants(module Module,
2599 visit func(Module)) {
2600
Colin Cross0aa6a5f2016-01-07 13:43:09 -08002601 c.visitAllModuleVariants(c.moduleInfo[module], visit)
Colin Cross24ad5872015-11-17 16:22:29 -08002602}
2603
Jamie Gennisd4e10182014-06-12 20:06:50 -07002604// WriteBuildFile writes the Ninja manifeset text for the generated build
2605// actions to w. If this is called before PrepareBuildActions successfully
2606// completes then ErrBuildActionsNotReady is returned.
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002607func (c *Context) WriteBuildFile(w io.Writer) error {
2608 if !c.buildActionsReady {
2609 return ErrBuildActionsNotReady
2610 }
2611
2612 nw := newNinjaWriter(w)
2613
2614 err := c.writeBuildFileHeader(nw)
2615 if err != nil {
2616 return err
2617 }
2618
2619 err = c.writeNinjaRequiredVersion(nw)
2620 if err != nil {
2621 return err
2622 }
2623
2624 // TODO: Group the globals by package.
2625
2626 err = c.writeGlobalVariables(nw)
2627 if err != nil {
2628 return err
2629 }
2630
2631 err = c.writeGlobalPools(nw)
2632 if err != nil {
2633 return err
2634 }
2635
2636 err = c.writeBuildDir(nw)
2637 if err != nil {
2638 return err
2639 }
2640
2641 err = c.writeGlobalRules(nw)
2642 if err != nil {
2643 return err
2644 }
2645
2646 err = c.writeAllModuleActions(nw)
2647 if err != nil {
2648 return err
2649 }
2650
2651 err = c.writeAllSingletonActions(nw)
2652 if err != nil {
2653 return err
2654 }
2655
2656 return nil
2657}
2658
Jamie Gennisc15544d2014-09-24 20:26:52 -07002659type pkgAssociation struct {
2660 PkgName string
2661 PkgPath string
2662}
2663
2664type pkgAssociationSorter struct {
2665 pkgs []pkgAssociation
2666}
2667
2668func (s *pkgAssociationSorter) Len() int {
2669 return len(s.pkgs)
2670}
2671
2672func (s *pkgAssociationSorter) Less(i, j int) bool {
2673 iName := s.pkgs[i].PkgName
2674 jName := s.pkgs[j].PkgName
2675 return iName < jName
2676}
2677
2678func (s *pkgAssociationSorter) Swap(i, j int) {
2679 s.pkgs[i], s.pkgs[j] = s.pkgs[j], s.pkgs[i]
2680}
2681
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002682func (c *Context) writeBuildFileHeader(nw *ninjaWriter) error {
2683 headerTemplate := template.New("fileHeader")
2684 _, err := headerTemplate.Parse(fileHeaderTemplate)
2685 if err != nil {
2686 // This is a programming error.
2687 panic(err)
2688 }
2689
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002690 var pkgs []pkgAssociation
2691 maxNameLen := 0
2692 for pkg, name := range c.pkgNames {
2693 pkgs = append(pkgs, pkgAssociation{
2694 PkgName: name,
2695 PkgPath: pkg.pkgPath,
2696 })
2697 if len(name) > maxNameLen {
2698 maxNameLen = len(name)
2699 }
2700 }
2701
2702 for i := range pkgs {
2703 pkgs[i].PkgName += strings.Repeat(" ", maxNameLen-len(pkgs[i].PkgName))
2704 }
2705
Jamie Gennisc15544d2014-09-24 20:26:52 -07002706 sort.Sort(&pkgAssociationSorter{pkgs})
2707
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002708 params := map[string]interface{}{
2709 "Pkgs": pkgs,
2710 }
2711
2712 buf := bytes.NewBuffer(nil)
2713 err = headerTemplate.Execute(buf, params)
2714 if err != nil {
2715 return err
2716 }
2717
2718 return nw.Comment(buf.String())
2719}
2720
2721func (c *Context) writeNinjaRequiredVersion(nw *ninjaWriter) error {
2722 value := fmt.Sprintf("%d.%d.%d", c.requiredNinjaMajor, c.requiredNinjaMinor,
2723 c.requiredNinjaMicro)
2724
2725 err := nw.Assign("ninja_required_version", value)
2726 if err != nil {
2727 return err
2728 }
2729
2730 return nw.BlankLine()
2731}
2732
2733func (c *Context) writeBuildDir(nw *ninjaWriter) error {
Colin Crossa2599452015-11-18 16:01:01 -08002734 if c.ninjaBuildDir != nil {
2735 err := nw.Assign("builddir", c.ninjaBuildDir.Value(c.pkgNames))
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002736 if err != nil {
2737 return err
2738 }
2739
2740 err = nw.BlankLine()
2741 if err != nil {
2742 return err
2743 }
2744 }
2745 return nil
2746}
2747
Jamie Gennisc15544d2014-09-24 20:26:52 -07002748type globalEntity interface {
Dan Willemsenaeffbf72015-11-25 15:29:32 -08002749 fullName(pkgNames map[*packageContext]string) string
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002750}
2751
Jamie Gennisc15544d2014-09-24 20:26:52 -07002752type globalEntitySorter struct {
Dan Willemsenaeffbf72015-11-25 15:29:32 -08002753 pkgNames map[*packageContext]string
Jamie Gennisc15544d2014-09-24 20:26:52 -07002754 entities []globalEntity
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002755}
2756
Jamie Gennisc15544d2014-09-24 20:26:52 -07002757func (s *globalEntitySorter) Len() int {
2758 return len(s.entities)
2759}
2760
2761func (s *globalEntitySorter) Less(i, j int) bool {
2762 iName := s.entities[i].fullName(s.pkgNames)
2763 jName := s.entities[j].fullName(s.pkgNames)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002764 return iName < jName
2765}
2766
Jamie Gennisc15544d2014-09-24 20:26:52 -07002767func (s *globalEntitySorter) Swap(i, j int) {
2768 s.entities[i], s.entities[j] = s.entities[j], s.entities[i]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002769}
2770
2771func (c *Context) writeGlobalVariables(nw *ninjaWriter) error {
2772 visited := make(map[Variable]bool)
2773
2774 var walk func(v Variable) error
2775 walk = func(v Variable) error {
2776 visited[v] = true
2777
2778 // First visit variables on which this variable depends.
2779 value := c.globalVariables[v]
2780 for _, dep := range value.variables {
2781 if !visited[dep] {
2782 err := walk(dep)
2783 if err != nil {
2784 return err
2785 }
2786 }
2787 }
2788
2789 err := nw.Assign(v.fullName(c.pkgNames), value.Value(c.pkgNames))
2790 if err != nil {
2791 return err
2792 }
2793
2794 err = nw.BlankLine()
2795 if err != nil {
2796 return err
2797 }
2798
2799 return nil
2800 }
2801
Jamie Gennisc15544d2014-09-24 20:26:52 -07002802 globalVariables := make([]globalEntity, 0, len(c.globalVariables))
2803 for variable := range c.globalVariables {
2804 globalVariables = append(globalVariables, variable)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002805 }
2806
Jamie Gennisc15544d2014-09-24 20:26:52 -07002807 sort.Sort(&globalEntitySorter{c.pkgNames, globalVariables})
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002808
Jamie Gennisc15544d2014-09-24 20:26:52 -07002809 for _, entity := range globalVariables {
2810 v := entity.(Variable)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002811 if !visited[v] {
2812 err := walk(v)
2813 if err != nil {
2814 return nil
2815 }
2816 }
2817 }
2818
2819 return nil
2820}
2821
2822func (c *Context) writeGlobalPools(nw *ninjaWriter) error {
Jamie Gennisc15544d2014-09-24 20:26:52 -07002823 globalPools := make([]globalEntity, 0, len(c.globalPools))
2824 for pool := range c.globalPools {
2825 globalPools = append(globalPools, pool)
2826 }
2827
2828 sort.Sort(&globalEntitySorter{c.pkgNames, globalPools})
2829
2830 for _, entity := range globalPools {
2831 pool := entity.(Pool)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002832 name := pool.fullName(c.pkgNames)
Jamie Gennisc15544d2014-09-24 20:26:52 -07002833 def := c.globalPools[pool]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002834 err := def.WriteTo(nw, name)
2835 if err != nil {
2836 return err
2837 }
2838
2839 err = nw.BlankLine()
2840 if err != nil {
2841 return err
2842 }
2843 }
2844
2845 return nil
2846}
2847
2848func (c *Context) writeGlobalRules(nw *ninjaWriter) error {
Jamie Gennisc15544d2014-09-24 20:26:52 -07002849 globalRules := make([]globalEntity, 0, len(c.globalRules))
2850 for rule := range c.globalRules {
2851 globalRules = append(globalRules, rule)
2852 }
2853
2854 sort.Sort(&globalEntitySorter{c.pkgNames, globalRules})
2855
2856 for _, entity := range globalRules {
2857 rule := entity.(Rule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002858 name := rule.fullName(c.pkgNames)
Jamie Gennisc15544d2014-09-24 20:26:52 -07002859 def := c.globalRules[rule]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002860 err := def.WriteTo(nw, name, c.pkgNames)
2861 if err != nil {
2862 return err
2863 }
2864
2865 err = nw.BlankLine()
2866 if err != nil {
2867 return err
2868 }
2869 }
2870
2871 return nil
2872}
2873
Colin Cross2c1f3d12016-04-11 15:47:28 -07002874type depSorter []depInfo
2875
2876func (s depSorter) Len() int {
2877 return len(s)
2878}
2879
2880func (s depSorter) Less(i, j int) bool {
Colin Cross0b7e83e2016-05-17 14:58:05 -07002881 iName := s[i].module.Name()
2882 jName := s[j].module.Name()
Colin Cross2c1f3d12016-04-11 15:47:28 -07002883 if iName == jName {
2884 iName = s[i].module.variantName
2885 jName = s[j].module.variantName
2886 }
2887 return iName < jName
2888}
2889
2890func (s depSorter) Swap(i, j int) {
2891 s[i], s[j] = s[j], s[i]
2892}
2893
Colin Crossab6d7902015-03-11 16:17:52 -07002894type moduleSorter []*moduleInfo
Jamie Gennis86179fe2014-06-11 16:27:16 -07002895
Colin Crossab6d7902015-03-11 16:17:52 -07002896func (s moduleSorter) Len() int {
Jamie Gennis86179fe2014-06-11 16:27:16 -07002897 return len(s)
2898}
2899
Colin Crossab6d7902015-03-11 16:17:52 -07002900func (s moduleSorter) Less(i, j int) bool {
Colin Cross0b7e83e2016-05-17 14:58:05 -07002901 iName := s[i].Name()
2902 jName := s[j].Name()
Colin Crossab6d7902015-03-11 16:17:52 -07002903 if iName == jName {
Colin Cross65569e42015-03-10 20:08:19 -07002904 iName = s[i].variantName
2905 jName = s[j].variantName
Colin Crossab6d7902015-03-11 16:17:52 -07002906 }
Jamie Gennis86179fe2014-06-11 16:27:16 -07002907 return iName < jName
2908}
2909
Colin Crossab6d7902015-03-11 16:17:52 -07002910func (s moduleSorter) Swap(i, j int) {
Jamie Gennis86179fe2014-06-11 16:27:16 -07002911 s[i], s[j] = s[j], s[i]
2912}
2913
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002914func (c *Context) writeAllModuleActions(nw *ninjaWriter) error {
2915 headerTemplate := template.New("moduleHeader")
2916 _, err := headerTemplate.Parse(moduleHeaderTemplate)
2917 if err != nil {
2918 // This is a programming error.
2919 panic(err)
2920 }
2921
Colin Crossab6d7902015-03-11 16:17:52 -07002922 modules := make([]*moduleInfo, 0, len(c.moduleInfo))
2923 for _, module := range c.moduleInfo {
2924 modules = append(modules, module)
Jamie Gennis86179fe2014-06-11 16:27:16 -07002925 }
Colin Crossab6d7902015-03-11 16:17:52 -07002926 sort.Sort(moduleSorter(modules))
Jamie Gennis86179fe2014-06-11 16:27:16 -07002927
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002928 buf := bytes.NewBuffer(nil)
2929
Colin Crossab6d7902015-03-11 16:17:52 -07002930 for _, module := range modules {
Dan Willemsen958b3ac2015-07-20 15:55:37 -07002931 if len(module.actionDefs.variables)+len(module.actionDefs.rules)+len(module.actionDefs.buildDefs) == 0 {
2932 continue
2933 }
2934
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002935 buf.Reset()
Jamie Gennis1ebd3b82014-06-04 15:33:08 -07002936
2937 // In order to make the bootstrap build manifest independent of the
2938 // build dir we need to output the Blueprints file locations in the
2939 // comments as paths relative to the source directory.
Colin Crossab6d7902015-03-11 16:17:52 -07002940 relPos := module.pos
2941 relPos.Filename = module.relBlueprintsFile
Jamie Gennis1ebd3b82014-06-04 15:33:08 -07002942
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07002943 // Get the name and location of the factory function for the module.
Colin Crossab6d7902015-03-11 16:17:52 -07002944 factory := c.moduleFactories[module.typeName]
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07002945 factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer())
2946 factoryName := factoryFunc.Name()
2947
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002948 infoMap := map[string]interface{}{
Colin Cross0b7e83e2016-05-17 14:58:05 -07002949 "name": module.Name(),
2950 "typeName": module.typeName,
2951 "goFactory": factoryName,
2952 "pos": relPos,
2953 "variant": module.variantName,
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002954 }
2955 err = headerTemplate.Execute(buf, infoMap)
2956 if err != nil {
2957 return err
2958 }
2959
2960 err = nw.Comment(buf.String())
2961 if err != nil {
2962 return err
2963 }
2964
2965 err = nw.BlankLine()
2966 if err != nil {
2967 return err
2968 }
2969
Colin Crossab6d7902015-03-11 16:17:52 -07002970 err = c.writeLocalBuildActions(nw, &module.actionDefs)
Jamie Gennis1bc967e2014-05-27 16:34:41 -07002971 if err != nil {
2972 return err
2973 }
2974
2975 err = nw.BlankLine()
2976 if err != nil {
2977 return err
2978 }
2979 }
2980
2981 return nil
2982}
2983
2984func (c *Context) writeAllSingletonActions(nw *ninjaWriter) error {
2985 headerTemplate := template.New("singletonHeader")
2986 _, err := headerTemplate.Parse(singletonHeaderTemplate)
2987 if err != nil {
2988 // This is a programming error.
2989 panic(err)
2990 }
2991
2992 buf := bytes.NewBuffer(nil)
2993
Yuchen Wub9103ef2015-08-25 17:58:17 -07002994 for _, info := range c.singletonInfo {
Dan Willemsen958b3ac2015-07-20 15:55:37 -07002995 if len(info.actionDefs.variables)+len(info.actionDefs.rules)+len(info.actionDefs.buildDefs) == 0 {
2996 continue
2997 }
2998
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07002999 // Get the name of the factory function for the module.
3000 factory := info.factory
3001 factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer())
3002 factoryName := factoryFunc.Name()
3003
Jamie Gennis1bc967e2014-05-27 16:34:41 -07003004 buf.Reset()
3005 infoMap := map[string]interface{}{
Yuchen Wub9103ef2015-08-25 17:58:17 -07003006 "name": info.name,
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07003007 "goFactory": factoryName,
Jamie Gennis1bc967e2014-05-27 16:34:41 -07003008 }
3009 err = headerTemplate.Execute(buf, infoMap)
3010 if err != nil {
3011 return err
3012 }
3013
3014 err = nw.Comment(buf.String())
3015 if err != nil {
3016 return err
3017 }
3018
3019 err = nw.BlankLine()
3020 if err != nil {
3021 return err
3022 }
3023
3024 err = c.writeLocalBuildActions(nw, &info.actionDefs)
3025 if err != nil {
3026 return err
3027 }
3028
3029 err = nw.BlankLine()
3030 if err != nil {
3031 return err
3032 }
3033 }
3034
3035 return nil
3036}
3037
3038func (c *Context) writeLocalBuildActions(nw *ninjaWriter,
3039 defs *localBuildActions) error {
3040
3041 // Write the local variable assignments.
3042 for _, v := range defs.variables {
3043 // A localVariable doesn't need the package names or config to
3044 // determine its name or value.
3045 name := v.fullName(nil)
3046 value, err := v.value(nil)
3047 if err != nil {
3048 panic(err)
3049 }
3050 err = nw.Assign(name, value.Value(c.pkgNames))
3051 if err != nil {
3052 return err
3053 }
3054 }
3055
3056 if len(defs.variables) > 0 {
3057 err := nw.BlankLine()
3058 if err != nil {
3059 return err
3060 }
3061 }
3062
3063 // Write the local rules.
3064 for _, r := range defs.rules {
3065 // A localRule doesn't need the package names or config to determine
3066 // its name or definition.
3067 name := r.fullName(nil)
3068 def, err := r.def(nil)
3069 if err != nil {
3070 panic(err)
3071 }
3072
3073 err = def.WriteTo(nw, name, c.pkgNames)
3074 if err != nil {
3075 return err
3076 }
3077
3078 err = nw.BlankLine()
3079 if err != nil {
3080 return err
3081 }
3082 }
3083
3084 // Write the build definitions.
3085 for _, buildDef := range defs.buildDefs {
3086 err := buildDef.WriteTo(nw, c.pkgNames)
3087 if err != nil {
3088 return err
3089 }
3090
3091 if len(buildDef.Args) > 0 {
3092 err = nw.BlankLine()
3093 if err != nil {
3094 return err
3095 }
3096 }
3097 }
3098
3099 return nil
3100}
3101
Colin Cross65569e42015-03-10 20:08:19 -07003102func beforeInModuleList(a, b *moduleInfo, list []*moduleInfo) bool {
3103 found := false
Colin Cross045a5972015-11-03 16:58:48 -08003104 if a == b {
3105 return false
3106 }
Colin Cross65569e42015-03-10 20:08:19 -07003107 for _, l := range list {
3108 if l == a {
3109 found = true
3110 } else if l == b {
3111 return found
3112 }
3113 }
3114
3115 missing := a
3116 if found {
3117 missing = b
3118 }
3119 panic(fmt.Errorf("element %v not found in list %v", missing, list))
3120}
3121
Colin Cross0aa6a5f2016-01-07 13:43:09 -08003122type panicError struct {
3123 panic interface{}
3124 stack []byte
3125 in string
3126}
3127
3128func newPanicErrorf(panic interface{}, in string, a ...interface{}) error {
3129 buf := make([]byte, 4096)
3130 count := runtime.Stack(buf, false)
3131 return panicError{
3132 panic: panic,
3133 in: fmt.Sprintf(in, a...),
3134 stack: buf[:count],
3135 }
3136}
3137
3138func (p panicError) Error() string {
3139 return fmt.Sprintf("panic in %s\n%s\n%s\n", p.in, p.panic, p.stack)
3140}
3141
3142func (p *panicError) addIn(in string) {
3143 p.in += " in " + in
3144}
3145
3146func funcName(f interface{}) string {
3147 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
3148}
3149
Jamie Gennis1bc967e2014-05-27 16:34:41 -07003150var fileHeaderTemplate = `******************************************************************************
3151*** This file is generated and should not be edited ***
3152******************************************************************************
3153{{if .Pkgs}}
3154This file contains variables, rules, and pools with name prefixes indicating
3155they were generated by the following Go packages:
3156{{range .Pkgs}}
3157 {{.PkgName}} [from Go package {{.PkgPath}}]{{end}}{{end}}
3158
3159`
3160
3161var moduleHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Colin Cross0b7e83e2016-05-17 14:58:05 -07003162Module: {{.name}}
Colin Crossab6d7902015-03-11 16:17:52 -07003163Variant: {{.variant}}
Jamie Gennis1bc967e2014-05-27 16:34:41 -07003164Type: {{.typeName}}
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07003165Factory: {{.goFactory}}
Jamie Gennis1bc967e2014-05-27 16:34:41 -07003166Defined: {{.pos}}
3167`
3168
3169var singletonHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
3170Singleton: {{.name}}
Jamie Gennis7d5b2f82014-09-24 17:51:52 -07003171Factory: {{.goFactory}}
Jamie Gennis1bc967e2014-05-27 16:34:41 -07003172`