Add function to retrieve module by name.

This function should only be used in corner-cases where it is not
possible to retrieve the module by traversing a dependency graph; there
are no guarantees about which variant is returned, and the function will
eagerly panic if something is amiss (the name doesn't belong to a module
but to an alias instead).

This function is particularly useful for bp2build, which does not use
real variants when evaluating the blueprint graph.

Test: With soong change, USE_BAZEL_ANALYSIS=1 m libc
Change-Id: I72b8335b642ed2d05e0a38e448cd380acc8d65b0
This commit is contained in:
Chris Parsons 2021-07-21 14:32:20 -04:00
parent 1c3530ab58
commit 030150d8f9

View file

@ -294,6 +294,14 @@ type BaseModuleContext interface {
// passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
OtherModuleExists(name string) bool
// ModuleFromName returns (module, true) if a module exists by the given name and same context namespace,
// or (nil, false) if it does not exist. It panics if there is either more than one
// module of the given name, or if the given name refers to an alias instead of a module.
// There are no guarantees about which variant of the module will be returned.
// Prefer retrieving the module using GetDirectDep or a visit function, when possible, as
// this will guarantee the appropriate module-variant dependency is returned.
ModuleFromName(name string) (Module, 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
@ -532,6 +540,23 @@ func (m *baseModuleContext) OtherModuleDependencyTag(logicModule Module) Depende
return nil
}
func (m *baseModuleContext) ModuleFromName(name string) (Module, bool) {
moduleGroup, exists := m.context.nameInterface.ModuleFromName(name, m.module.namespace())
if exists {
if len(moduleGroup.modules) != 1 {
panic(fmt.Errorf("Expected exactly one module named %q, but got %d", name, len(moduleGroup.modules)))
}
moduleInfo := moduleGroup.modules[0].module()
if moduleInfo != nil {
return moduleInfo.logicModule, true
} else {
panic(fmt.Errorf(`Expected actual module named %q, but group did not contain a module.
There may instead be an alias by that name.`, name))
}
}
return nil, exists
}
func (m *baseModuleContext) OtherModuleExists(name string) bool {
_, exists := m.context.nameInterface.ModuleFromName(name, m.module.namespace())
return exists