Add function to retrieve module by name. am: 030150d8f9 am: 2322a800cc am: 74137dfbbb

Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/1772793

Change-Id: I525109b32e0007249386a9eb2ccad6aa753e5688
This commit is contained in:
Chris Parsons 2021-07-23 15:40:29 +00:00 committed by Automerger Merge Worker
commit f5f6cbe480

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