Add a function to match modules through a variation map. am: 14cdd71152

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1825132

Change-Id: I9b6765ee40d47bb9470bdb0f0f09bb572d700343
This commit is contained in:
Martin Stjernholm 2021-09-18 00:48:46 +00:00 committed by Automerger Merge Worker
commit f9dd87304f

View file

@ -491,6 +491,66 @@ func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonF
ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory))
}
// ModuleVariantForTests selects a specific variant of the module with the given
// name by matching the variations map against the variations of each module
// variant. A module variant matches the map if every variation that exists in
// both have the same value. Both the module and the map are allowed to have
// extra variations that the other doesn't have. Panics if not exactly one
// module variant matches.
func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[string]string) TestingModule {
modules := []Module{}
ctx.VisitAllModules(func(m blueprint.Module) {
if ctx.ModuleName(m) == name {
am := m.(Module)
amMut := am.base().commonProperties.DebugMutators
amVar := am.base().commonProperties.DebugVariations
matched := true
for i, mut := range amMut {
if wantedVar, found := matchVariations[mut]; found && amVar[i] != wantedVar {
matched = false
break
}
}
if matched {
modules = append(modules, am)
}
}
})
if len(modules) == 0 {
// Show all the modules or module variants that do exist.
var allModuleNames []string
var allVariants []string
ctx.VisitAllModules(func(m blueprint.Module) {
allModuleNames = append(allModuleNames, ctx.ModuleName(m))
if ctx.ModuleName(m) == name {
allVariants = append(allVariants, m.(Module).String())
}
})
if len(allVariants) == 0 {
panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
name, strings.Join(SortedUniqueStrings(allModuleNames), "\n ")))
} else {
sort.Strings(allVariants)
panic(fmt.Errorf("failed to find module %q matching %v. All variants:\n %s",
name, matchVariations, strings.Join(allVariants, "\n ")))
}
}
if len(modules) > 1 {
moduleStrings := []string{}
for _, m := range modules {
moduleStrings = append(moduleStrings, m.String())
}
sort.Strings(moduleStrings)
panic(fmt.Errorf("module %q has more than one variant that match %v:\n %s",
name, matchVariations, strings.Join(moduleStrings, "\n ")))
}
return newTestingModule(ctx.config, modules[0])
}
func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
var module Module
ctx.VisitAllModules(func(m blueprint.Module) {