Add ctx.OtherModule(Reverse)DependencyVariantExists.

This allows guarding calls to AddVariationDependencies and
AddReverseDependency to register dependencies on optional modules.
This commit is contained in:
Martin Stjernholm 2020-03-06 00:29:24 +00:00
parent 2bcc43c60f
commit 2f212479e2
2 changed files with 56 additions and 12 deletions

View file

@ -1610,23 +1610,20 @@ func (c *Context) findReverseDependency(module *moduleInfo, destName string) (*m
}}
}
func (c *Context) addVariationDependency(module *moduleInfo, variations []Variation,
tag DependencyTag, depName string, far bool) []error {
if _, ok := tag.(BaseDependencyTag); ok {
panic("BaseDependencyTag is not allowed to be used directly!")
}
possibleDeps := c.moduleGroupFromName(depName, module.namespace())
if possibleDeps == nil {
return c.discoveredMissingDependencies(module, depName)
}
func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, variations []Variation, far bool, reverse bool) (*moduleInfo, variationMap) {
// We can't just append variant.Variant to module.dependencyVariants.variantName and
// compare the strings because the result won't be in mutator registration order.
// Create a new map instead, and then deep compare the maps.
var newVariant variationMap
if !far {
newVariant = module.dependencyVariant.clone()
if !reverse {
// For forward dependency, ignore local variants by matching against
// dependencyVariant which doesn't have the local variants
newVariant = module.dependencyVariant.clone()
} else {
// For reverse dependency, use all the variants
newVariant = module.variant.clone()
}
}
for _, v := range variations {
if newVariant == nil {
@ -1660,6 +1657,22 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
}
}
return foundDep, newVariant
}
func (c *Context) addVariationDependency(module *moduleInfo, variations []Variation,
tag DependencyTag, depName string, far bool) []error {
if _, ok := tag.(BaseDependencyTag); ok {
panic("BaseDependencyTag is not allowed to be used directly!")
}
possibleDeps := c.moduleGroupFromName(depName, module.namespace())
if possibleDeps == nil {
return c.discoveredMissingDependencies(module, depName)
}
foundDep, newVariant := c.findVariant(module, possibleDeps, variations, far, false)
if foundDep == nil {
if c.allowMissingDependencies {
// Allow missing variants.

View file

@ -275,6 +275,19 @@ type BaseModuleContext interface {
// OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
// passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
OtherModuleExists(name string) bool
// OtherModuleDependencyVariantExists returns true if a module with the
// specified name and variant exists. The variant must match the given
// variations. It must also match all the non-local variations of the current
// module. In other words, it checks for the module AddVariationDependencies
// would add a dependency on with the same arguments.
OtherModuleDependencyVariantExists(variations []Variation, name string) bool
// OtherModuleReverseDependencyVariantExists returns true if a module with the
// specified name exists with the same variations as the current module. In
// other words, it checks for the module AddReverseDependency would add a
// dependency on with the same argument.
OtherModuleReverseDependencyVariantExists(name string) bool
}
type DynamicDependerModuleContext BottomUpMutatorContext
@ -492,6 +505,24 @@ func (m *baseModuleContext) OtherModuleExists(name string) bool {
return exists
}
func (m *baseModuleContext) OtherModuleDependencyVariantExists(variations []Variation, name string) bool {
possibleDeps := m.context.moduleGroupFromName(name, m.module.namespace())
if possibleDeps == nil {
return false
}
found, _ := m.context.findVariant(m.module, possibleDeps, variations, false, false)
return found != nil
}
func (m *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
possibleDeps := m.context.moduleGroupFromName(name, m.module.namespace())
if possibleDeps == nil {
return false
}
found, _ := m.context.findVariant(m.module, possibleDeps, nil, false, true)
return found != nil
}
func (m *baseModuleContext) GetDirectDep(name string) (Module, DependencyTag) {
for _, dep := range m.module.directDeps {
if dep.module.Name() == name {