Use module groups more widely

Use module groups instead of passing around the list of modules
extracted from a module group.

Test: blueprint tests
Change-Id: I02a79950c6377441c49129ebeb5f12be257df668
This commit is contained in:
Colin Cross 2019-11-13 20:10:12 -08:00
parent 7e8c766ab8
commit d03b59d03e
3 changed files with 68 additions and 69 deletions

View file

@ -1261,6 +1261,15 @@ func (c *Context) prettyPrintVariant(variant variationMap) string {
return strings.Join(names, ", ")
}
func (c *Context) prettyPrintGroupVariants(group *moduleGroup) string {
var variants []string
for _, mod := range group.modules {
variants = append(variants, c.prettyPrintVariant(mod.variant))
}
sort.Strings(variants)
return strings.Join(variants, "\n ")
}
func (c *Context) newModule(factory ModuleFactory) *moduleInfo {
logicModule, properties := factory()
@ -1410,9 +1419,9 @@ func blueprintDepsMutator(ctx BottomUpMutatorContext) {
// findMatchingVariant searches the moduleGroup for a module with the same variant as module,
// and returns the matching module, or nil if one is not found.
func (c *Context) findMatchingVariant(module *moduleInfo, possible []*moduleInfo, reverse bool) *moduleInfo {
if len(possible) == 1 {
return possible[0]
func (c *Context) findMatchingVariant(module *moduleInfo, possible *moduleGroup, reverse bool) *moduleInfo {
if len(possible.modules) == 1 {
return possible.modules[0]
} else {
var variantToMatch variationMap
if !reverse {
@ -1423,7 +1432,7 @@ func (c *Context) findMatchingVariant(module *moduleInfo, possible []*moduleInfo
// For reverse dependency, use all the variants
variantToMatch = module.variant
}
for _, m := range possible {
for _, m := range possible.modules {
if m.variant.equal(variantToMatch) {
return m
}
@ -1445,7 +1454,7 @@ func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName s
}}
}
possibleDeps := c.modulesFromName(depName, module.namespace())
possibleDeps := c.moduleGroupFromName(depName, module.namespace())
if possibleDeps == nil {
return c.discoveredMissingDependencies(module, depName)
}
@ -1456,17 +1465,11 @@ func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName s
return nil
}
variants := make([]string, len(possibleDeps))
for i, mod := range possibleDeps {
variants[i] = c.prettyPrintVariant(mod.variant)
}
sort.Strings(variants)
return []error{&BlueprintError{
Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
depName, module.Name(),
c.prettyPrintVariant(module.dependencyVariant),
strings.Join(variants, "\n ")),
c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos,
}}
}
@ -1479,7 +1482,7 @@ func (c *Context) findReverseDependency(module *moduleInfo, destName string) (*m
}}
}
possibleDeps := c.modulesFromName(destName, module.namespace())
possibleDeps := c.moduleGroupFromName(destName, module.namespace())
if possibleDeps == nil {
return nil, []error{&BlueprintError{
Err: fmt.Errorf("%q has a reverse dependency on undefined module %q",
@ -1492,17 +1495,11 @@ func (c *Context) findReverseDependency(module *moduleInfo, destName string) (*m
return m, nil
}
variants := make([]string, len(possibleDeps))
for i, mod := range possibleDeps {
variants[i] = c.prettyPrintVariant(mod.variant)
}
sort.Strings(variants)
return nil, []error{&BlueprintError{
Err: fmt.Errorf("reverse dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
destName, module.Name(),
c.prettyPrintVariant(module.dependencyVariant),
strings.Join(variants, "\n ")),
c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos,
}}
}
@ -1513,7 +1510,7 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
panic("BaseDependencyTag is not allowed to be used directly!")
}
possibleDeps := c.modulesFromName(depName, module.namespace())
possibleDeps := c.moduleGroupFromName(depName, module.namespace())
if possibleDeps == nil {
return c.discoveredMissingDependencies(module, depName)
}
@ -1531,48 +1528,50 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
newVariant[v.Mutator] = v.Variation
}
for _, m := range possibleDeps {
var found bool
check := func(variant variationMap) bool {
if far {
found = m.variant.subset(newVariant)
return variant.subset(newVariant)
} else {
found = m.variant.equal(newVariant)
}
if found {
if module == m {
return []error{&BlueprintError{
Err: fmt.Errorf("%q depends on itself", depName),
Pos: module.pos,
}}
}
// AddVariationDependency allows adding a dependency on itself, but only if
// that module is earlier in the module list than this one, since we always
// run GenerateBuildActions in order for the variants of a module
if m.group == module.group && beforeInModuleList(module, m, module.group.modules) {
return []error{&BlueprintError{
Err: fmt.Errorf("%q depends on later version of itself", depName),
Pos: module.pos,
}}
}
module.newDirectDeps = append(module.newDirectDeps, depInfo{m, tag})
atomic.AddUint32(&c.depsModified, 1)
return nil
return variant.equal(newVariant)
}
}
variants := make([]string, len(possibleDeps))
for i, mod := range possibleDeps {
variants[i] = c.prettyPrintVariant(mod.variant)
var foundDep *moduleInfo
for _, m := range possibleDeps.modules {
if check(m.variant) {
foundDep = m
break
}
}
sort.Strings(variants)
return []error{&BlueprintError{
Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
depName, module.Name(),
c.prettyPrintVariant(newVariant),
strings.Join(variants, "\n ")),
Pos: module.pos,
}}
if foundDep == nil {
return []error{&BlueprintError{
Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
depName, module.Name(),
c.prettyPrintVariant(newVariant),
c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos,
}}
}
if module == foundDep {
return []error{&BlueprintError{
Err: fmt.Errorf("%q depends on itself", depName),
Pos: module.pos,
}}
}
// AddVariationDependency allows adding a dependency on itself, but only if
// that module is earlier in the module list than this one, since we always
// run GenerateBuildActions in order for the variants of a module
if foundDep.group == module.group && beforeInModuleList(module, foundDep, module.group.modules) {
return []error{&BlueprintError{
Err: fmt.Errorf("%q depends on later version of itself", depName),
Pos: module.pos,
}}
}
module.newDirectDeps = append(module.newDirectDeps, depInfo{foundDep, tag})
atomic.AddUint32(&c.depsModified, 1)
return nil
}
func (c *Context) addInterVariantDependency(origModule *moduleInfo, tag DependencyTag,
@ -2510,13 +2509,13 @@ type rename struct {
}
func (c *Context) moduleMatchingVariant(module *moduleInfo, name string) *moduleInfo {
targets := c.modulesFromName(name, module.namespace())
group := c.moduleGroupFromName(name, module.namespace())
if targets == nil {
if group == nil {
return nil
}
for _, m := range targets {
for _, m := range group.modules {
if module.variantName == m.variantName {
return m
}
@ -2573,10 +2572,10 @@ func (c *Context) missingDependencyError(module *moduleInfo, depName string) (er
}
}
func (c *Context) modulesFromName(name string, namespace Namespace) []*moduleInfo {
func (c *Context) moduleGroupFromName(name string, namespace Namespace) *moduleGroup {
group, exists := c.nameInterface.ModuleFromName(name, namespace)
if exists {
return group.modules
return group.moduleGroup
}
return nil
}

View file

@ -188,7 +188,7 @@ func TestWalkDeps(t *testing.T) {
var outputDown string
var outputUp string
topModule := ctx.modulesFromName("A", nil)[0]
topModule := ctx.moduleGroupFromName("A", nil).modules[0]
ctx.walkDeps(topModule, false,
func(dep depInfo, parent *moduleInfo) bool {
outputDown += ctx.ModuleName(dep.module.logicModule)
@ -280,7 +280,7 @@ func TestWalkDepsDuplicates(t *testing.T) {
var outputDown string
var outputUp string
topModule := ctx.modulesFromName("A", nil)[0]
topModule := ctx.moduleGroupFromName("A", nil).modules[0]
ctx.walkDeps(topModule, true,
func(dep depInfo, parent *moduleInfo) bool {
outputDown += ctx.ModuleName(dep.module.logicModule)
@ -334,10 +334,10 @@ func TestCreateModule(t *testing.T) {
t.FailNow()
}
a := ctx.modulesFromName("A", nil)[0].logicModule.(*fooModule)
b := ctx.modulesFromName("B", nil)[0].logicModule.(*barModule)
c := ctx.modulesFromName("C", nil)[0].logicModule.(*barModule)
d := ctx.modulesFromName("D", nil)[0].logicModule.(*fooModule)
a := ctx.moduleGroupFromName("A", nil).modules[0].logicModule.(*fooModule)
b := ctx.moduleGroupFromName("B", nil).modules[0].logicModule.(*barModule)
c := ctx.moduleGroupFromName("C", nil).modules[0].logicModule.(*barModule)
d := ctx.moduleGroupFromName("D", nil).modules[0].logicModule.(*fooModule)
checkDeps := func(m Module, expected string) {
var deps []string

View file

@ -149,13 +149,13 @@ func setupVisitTest(t *testing.T) *Context {
func TestVisit(t *testing.T) {
ctx := setupVisitTest(t)
topModule := ctx.modulesFromName("A", nil)[0].logicModule.(*visitModule)
topModule := ctx.moduleGroupFromName("A", nil).modules[0].logicModule.(*visitModule)
assertString(t, topModule.properties.VisitDepsDepthFirst, "FEDCB")
assertString(t, topModule.properties.VisitDepsDepthFirstIf, "FEDC")
assertString(t, topModule.properties.VisitDirectDeps, "B")
assertString(t, topModule.properties.VisitDirectDepsIf, "")
eModule := ctx.modulesFromName("E", nil)[0].logicModule.(*visitModule)
eModule := ctx.moduleGroupFromName("E", nil).modules[0].logicModule.(*visitModule)
assertString(t, eModule.properties.VisitDepsDepthFirst, "F")
assertString(t, eModule.properties.VisitDepsDepthFirstIf, "F")
assertString(t, eModule.properties.VisitDirectDeps, "FF")