Add ReplaceDependeciesIf to allow for conditional replacement

Adds support for making the replacement of one dependency with another
conditional on the from and to module as well as the tag used.
This commit is contained in:
Paul Duffin 2020-06-30 12:15:26 +01:00
parent fe13954f3c
commit 8969cb6170
2 changed files with 25 additions and 4 deletions

View file

@ -2658,7 +2658,8 @@ func (c *Context) walkDeps(topModule *moduleInfo, allowDuplicates bool,
}
type replace struct {
from, to *moduleInfo
from, to *moduleInfo
predicate ReplaceDependencyPredicate
}
type rename struct {
@ -2704,18 +2705,25 @@ func (c *Context) handleRenames(renames []rename) []error {
func (c *Context) handleReplacements(replacements []replace) []error {
var errs []error
changedDeps := false
for _, replace := range replacements {
for _, m := range replace.from.reverseDeps {
for i, d := range m.directDeps {
if d.module == replace.from {
m.directDeps[i].module = replace.to
// If the replacement has a predicate then check it.
if replace.predicate == nil || replace.predicate(m.logicModule, d.tag, d.module.logicModule) {
m.directDeps[i].module = replace.to
changedDeps = true
}
}
}
}
atomic.AddUint32(&c.depsModified, 1)
}
if changedDeps {
atomic.AddUint32(&c.depsModified, 1)
}
return errs
}

View file

@ -843,6 +843,13 @@ type BottomUpMutatorContext interface {
// after the mutator pass is finished.
ReplaceDependencies(string)
// ReplaceDependencies replaces all dependencies on the identical variant of the module with the
// specified name with the current variant of this module as long as the supplied predicate returns
// true.
//
// Replacements don't take effect until after the mutator pass is finished.
ReplaceDependenciesIf(string, ReplaceDependencyPredicate)
// AliasVariation takes a variationName that was passed to CreateVariations for this module, and creates an
// alias from the current variant to the new variant. The alias will be valid until the next time a mutator
// calls CreateVariations or CreateLocalVariations on this module without also calling AliasVariation. The
@ -1006,6 +1013,12 @@ func (mctx *mutatorContext) AddInterVariantDependency(tag DependencyTag, from, t
}
func (mctx *mutatorContext) ReplaceDependencies(name string) {
mctx.ReplaceDependenciesIf(name, nil)
}
type ReplaceDependencyPredicate func(from Module, tag DependencyTag, to Module) bool
func (mctx *mutatorContext) ReplaceDependenciesIf(name string, predicate ReplaceDependencyPredicate) {
target := mctx.context.moduleMatchingVariant(mctx.module, name)
if target == nil {
@ -1013,7 +1026,7 @@ func (mctx *mutatorContext) ReplaceDependencies(name string) {
mctx.module.variantName, name))
}
mctx.replace = append(mctx.replace, replace{target, mctx.module})
mctx.replace = append(mctx.replace, replace{target, mctx.module, predicate})
}
func (mctx *mutatorContext) Rename(name string) {