Merge pull request #316 from skvadrik/add-dep-ret-mod

Return dependency modules from dependency-adding methods.
This commit is contained in:
colincross 2020-09-22 10:59:46 -07:00 committed by GitHub
commit 66fa73dd6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 31 deletions

View file

@ -1591,13 +1591,13 @@ func findExactVariantOrSingle(module *moduleInfo, possible *moduleGroup, reverse
return found return found
} }
func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName string) []error { func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName string) (*moduleInfo, []error) {
if _, ok := tag.(BaseDependencyTag); ok { if _, ok := tag.(BaseDependencyTag); ok {
panic("BaseDependencyTag is not allowed to be used directly!") panic("BaseDependencyTag is not allowed to be used directly!")
} }
if depName == module.Name() { if depName == module.Name() {
return []error{&BlueprintError{ return nil, []error{&BlueprintError{
Err: fmt.Errorf("%q depends on itself", depName), Err: fmt.Errorf("%q depends on itself", depName),
Pos: module.pos, Pos: module.pos,
}} }}
@ -1605,21 +1605,21 @@ func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName s
possibleDeps := c.moduleGroupFromName(depName, module.namespace()) possibleDeps := c.moduleGroupFromName(depName, module.namespace())
if possibleDeps == nil { if possibleDeps == nil {
return c.discoveredMissingDependencies(module, depName) return nil, c.discoveredMissingDependencies(module, depName)
} }
if m := findExactVariantOrSingle(module, possibleDeps, false); m != nil { if m := findExactVariantOrSingle(module, possibleDeps, false); m != nil {
module.newDirectDeps = append(module.newDirectDeps, depInfo{m, tag}) module.newDirectDeps = append(module.newDirectDeps, depInfo{m, tag})
atomic.AddUint32(&c.depsModified, 1) atomic.AddUint32(&c.depsModified, 1)
return nil return m, nil
} }
if c.allowMissingDependencies { if c.allowMissingDependencies {
// Allow missing variants. // Allow missing variants.
return c.discoveredMissingDependencies(module, depName+c.prettyPrintVariant(module.variant.dependencyVariations)) return nil, c.discoveredMissingDependencies(module, depName+c.prettyPrintVariant(module.variant.dependencyVariations))
} }
return []error{&BlueprintError{ return nil, []error{&BlueprintError{
Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s", Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
depName, module.Name(), depName, module.Name(),
c.prettyPrintVariant(module.variant.dependencyVariations), c.prettyPrintVariant(module.variant.dependencyVariations),
@ -1705,14 +1705,14 @@ func findVariant(module *moduleInfo, possibleDeps *moduleGroup, variations []Var
} }
func (c *Context) addVariationDependency(module *moduleInfo, variations []Variation, func (c *Context) addVariationDependency(module *moduleInfo, variations []Variation,
tag DependencyTag, depName string, far bool) []error { tag DependencyTag, depName string, far bool) (*moduleInfo, []error) {
if _, ok := tag.(BaseDependencyTag); ok { if _, ok := tag.(BaseDependencyTag); ok {
panic("BaseDependencyTag is not allowed to be used directly!") panic("BaseDependencyTag is not allowed to be used directly!")
} }
possibleDeps := c.moduleGroupFromName(depName, module.namespace()) possibleDeps := c.moduleGroupFromName(depName, module.namespace())
if possibleDeps == nil { if possibleDeps == nil {
return c.discoveredMissingDependencies(module, depName) return nil, c.discoveredMissingDependencies(module, depName)
} }
foundDep, newVariant := findVariant(module, possibleDeps, variations, far, false) foundDep, newVariant := findVariant(module, possibleDeps, variations, far, false)
@ -1720,9 +1720,9 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
if foundDep == nil { if foundDep == nil {
if c.allowMissingDependencies { if c.allowMissingDependencies {
// Allow missing variants. // Allow missing variants.
return c.discoveredMissingDependencies(module, depName+c.prettyPrintVariant(newVariant)) return nil, c.discoveredMissingDependencies(module, depName+c.prettyPrintVariant(newVariant))
} }
return []error{&BlueprintError{ return nil, []error{&BlueprintError{
Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s", Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
depName, module.Name(), depName, module.Name(),
c.prettyPrintVariant(newVariant), c.prettyPrintVariant(newVariant),
@ -1732,7 +1732,7 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
} }
if module == foundDep { if module == foundDep {
return []error{&BlueprintError{ return nil, []error{&BlueprintError{
Err: fmt.Errorf("%q depends on itself", depName), Err: fmt.Errorf("%q depends on itself", depName),
Pos: module.pos, Pos: module.pos,
}} }}
@ -1741,18 +1741,18 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
// that module is earlier in the module list than this one, since we always // that module is earlier in the module list than this one, since we always
// run GenerateBuildActions in order for the variants of a module // run GenerateBuildActions in order for the variants of a module
if foundDep.group == module.group && beforeInModuleList(module, foundDep, module.group.modules) { if foundDep.group == module.group && beforeInModuleList(module, foundDep, module.group.modules) {
return []error{&BlueprintError{ return nil, []error{&BlueprintError{
Err: fmt.Errorf("%q depends on later version of itself", depName), Err: fmt.Errorf("%q depends on later version of itself", depName),
Pos: module.pos, Pos: module.pos,
}} }}
} }
module.newDirectDeps = append(module.newDirectDeps, depInfo{foundDep, tag}) module.newDirectDeps = append(module.newDirectDeps, depInfo{foundDep, tag})
atomic.AddUint32(&c.depsModified, 1) atomic.AddUint32(&c.depsModified, 1)
return nil return foundDep, nil
} }
func (c *Context) addInterVariantDependency(origModule *moduleInfo, tag DependencyTag, func (c *Context) addInterVariantDependency(origModule *moduleInfo, tag DependencyTag,
from, to Module) { from, to Module) *moduleInfo {
if _, ok := tag.(BaseDependencyTag); ok { if _, ok := tag.(BaseDependencyTag); ok {
panic("BaseDependencyTag is not allowed to be used directly!") panic("BaseDependencyTag is not allowed to be used directly!")
} }
@ -1779,6 +1779,7 @@ func (c *Context) addInterVariantDependency(origModule *moduleInfo, tag Dependen
fromInfo.newDirectDeps = append(fromInfo.newDirectDeps, depInfo{toInfo, tag}) fromInfo.newDirectDeps = append(fromInfo.newDirectDeps, depInfo{toInfo, tag})
atomic.AddUint32(&c.depsModified, 1) atomic.AddUint32(&c.depsModified, 1)
return toInfo
} }
// findBlueprintDescendants returns a map linking parent Blueprints files to child Blueprints files // findBlueprintDescendants returns a map linking parent Blueprints files to child Blueprints files

View file

@ -776,10 +776,15 @@ type TopDownMutatorContext interface {
type BottomUpMutatorContext interface { type BottomUpMutatorContext interface {
BaseMutatorContext BaseMutatorContext
// AddDependency adds a dependency to the given module. // AddDependency adds a dependency to the given module. It returns a slice of modules for each
// Does not affect the ordering of the current mutator pass, but will be ordered // dependency (some entries may be nil). Does not affect the ordering of the current mutator
// correctly for all future mutator passes. // pass, but will be ordered correctly for all future mutator passes.
AddDependency(module Module, tag DependencyTag, name ...string) //
// If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
// new dependencies have had the current mutator called on them. If the mutator is not
// parallel this method does not affect the ordering of the current mutator pass, but will
// be ordered correctly for all future mutator passes.
AddDependency(module Module, tag DependencyTag, name ...string) []Module
// AddReverseDependency adds a dependency from the destination to the given module. // AddReverseDependency adds a dependency from the destination to the given module.
// Does not affect the ordering of the current mutator pass, but will be ordered // Does not affect the ordering of the current mutator pass, but will be ordered
@ -819,19 +824,30 @@ type BottomUpMutatorContext interface {
SetDefaultDependencyVariation(*string) SetDefaultDependencyVariation(*string)
// AddVariationDependencies adds deps as dependencies of the current module, but uses the variations // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
// argument to select which variant of the dependency to use. A variant of the dependency must // argument to select which variant of the dependency to use. It returns a slice of modules for
// exist that matches the all of the non-local variations of the current module, plus the variations // each dependency (some entries may be nil). A variant of the dependency must exist that matches
// argument. // the all of the non-local variations of the current module, plus the variations argument.
AddVariationDependencies([]Variation, DependencyTag, ...string) //
// If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
// new dependencies have had the current mutator called on them. If the mutator is not
// parallel this method does not affect the ordering of the current mutator pass, but will
// be ordered correctly for all future mutator passes.
AddVariationDependencies([]Variation, DependencyTag, ...string) []Module
// AddFarVariationDependencies adds deps as dependencies of the current module, but uses the // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
// variations argument to select which variant of the dependency to use. A variant of the // variations argument to select which variant of the dependency to use. It returns a slice of
// dependency must exist that matches the variations argument, but may also have other variations. // modules for each dependency (some entries may be nil). A variant of the dependency must
// exist that matches the variations argument, but may also have other variations.
// For any unspecified variation the first variant will be used. // For any unspecified variation the first variant will be used.
// //
// Unlike AddVariationDependencies, the variations of the current module are ignored - the // Unlike AddVariationDependencies, the variations of the current module are ignored - the
// dependency only needs to match the supplied variations. // dependency only needs to match the supplied variations.
AddFarVariationDependencies([]Variation, DependencyTag, ...string) //
// If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
// new dependencies have had the current mutator called on them. If the mutator is not
// parallel this method does not affect the ordering of the current mutator pass, but will
// be ordered correctly for all future mutator passes.
AddFarVariationDependencies([]Variation, DependencyTag, ...string) []Module
// AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
// ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
@ -1009,14 +1025,17 @@ func (mctx *mutatorContext) Module() Module {
return mctx.module.logicModule return mctx.module.logicModule
} }
func (mctx *mutatorContext) AddDependency(module Module, tag DependencyTag, deps ...string) { func (mctx *mutatorContext) AddDependency(module Module, tag DependencyTag, deps ...string) []Module {
depInfos := make([]Module, 0, len(deps))
for _, dep := range deps { for _, dep := range deps {
modInfo := mctx.context.moduleInfo[module] modInfo := mctx.context.moduleInfo[module]
errs := mctx.context.addDependency(modInfo, tag, dep) depInfo, errs := mctx.context.addDependency(modInfo, tag, dep)
if len(errs) > 0 { if len(errs) > 0 {
mctx.errs = append(mctx.errs, errs...) mctx.errs = append(mctx.errs, errs...)
} }
depInfos = append(depInfos, maybeLogicModule(depInfo))
} }
return depInfos
} }
func (mctx *mutatorContext) AddReverseDependency(module Module, tag DependencyTag, destName string) { func (mctx *mutatorContext) AddReverseDependency(module Module, tag DependencyTag, destName string) {
@ -1037,25 +1056,31 @@ func (mctx *mutatorContext) AddReverseDependency(module Module, tag DependencyTa
} }
func (mctx *mutatorContext) AddVariationDependencies(variations []Variation, tag DependencyTag, func (mctx *mutatorContext) AddVariationDependencies(variations []Variation, tag DependencyTag,
deps ...string) { deps ...string) []Module {
depInfos := make([]Module, 0, len(deps))
for _, dep := range deps { for _, dep := range deps {
errs := mctx.context.addVariationDependency(mctx.module, variations, tag, dep, false) depInfo, errs := mctx.context.addVariationDependency(mctx.module, variations, tag, dep, false)
if len(errs) > 0 { if len(errs) > 0 {
mctx.errs = append(mctx.errs, errs...) mctx.errs = append(mctx.errs, errs...)
} }
depInfos = append(depInfos, maybeLogicModule(depInfo))
} }
return depInfos
} }
func (mctx *mutatorContext) AddFarVariationDependencies(variations []Variation, tag DependencyTag, func (mctx *mutatorContext) AddFarVariationDependencies(variations []Variation, tag DependencyTag,
deps ...string) { deps ...string) []Module {
depInfos := make([]Module, 0, len(deps))
for _, dep := range deps { for _, dep := range deps {
errs := mctx.context.addVariationDependency(mctx.module, variations, tag, dep, true) depInfo, errs := mctx.context.addVariationDependency(mctx.module, variations, tag, dep, true)
if len(errs) > 0 { if len(errs) > 0 {
mctx.errs = append(mctx.errs, errs...) mctx.errs = append(mctx.errs, errs...)
} }
depInfos = append(depInfos, maybeLogicModule(depInfo))
} }
return depInfos
} }
func (mctx *mutatorContext) AddInterVariantDependency(tag DependencyTag, from, to Module) { func (mctx *mutatorContext) AddInterVariantDependency(tag DependencyTag, from, to Module) {
@ -1276,3 +1301,11 @@ func CheckBlueprintSyntax(moduleFactories map[string]ModuleFactory, filename str
return errs return errs
} }
func maybeLogicModule(module *moduleInfo) Module {
if module != nil {
return module.logicModule
} else {
return nil
}
}