Combine variant fields into variant struct

Combine variant, variations and dependencyVariations into a single
struct.

Test: blueprint tests
Change-Id: I76811bb2bdf76f1b4cffc6a7b9bc7362ecb5f7b9
This commit is contained in:
Colin Cross 2020-08-13 12:07:30 -07:00
parent 576b0fd577
commit edc41769fe
4 changed files with 90 additions and 82 deletions

View file

@ -159,10 +159,8 @@ type localBuildActions struct {
}
type moduleAlias struct {
variantName string
variant variationMap
dependencyVariant variationMap
target *moduleInfo
variant variant
target *moduleInfo
}
type moduleGroup struct {
@ -184,9 +182,7 @@ type moduleInfo struct {
propertyPos map[string]scanner.Position
createdBy *moduleInfo
variantName string
variant variationMap
dependencyVariant variationMap
variant variant
logicModule Module
group *moduleGroup
@ -212,6 +208,12 @@ type moduleInfo struct {
actionDefs localBuildActions
}
type variant struct {
name string
variations variationMap
dependencyVariations variationMap
}
type depInfo struct {
module *moduleInfo
tag DependencyTag
@ -232,8 +234,8 @@ func (module *moduleInfo) Name() string {
func (module *moduleInfo) String() string {
s := fmt.Sprintf("module %q", module.Name())
if module.variantName != "" {
s += fmt.Sprintf(" variant %q", module.variantName)
if module.variant.name != "" {
s += fmt.Sprintf(" variant %q", module.variant.name)
}
if module.createdBy != nil {
s += fmt.Sprintf(" (created by %s)", module.createdBy)
@ -1224,8 +1226,37 @@ func (c *Context) cloneLogicModule(origModule *moduleInfo) (Module, []interface{
return newLogicModule, newProperties
}
func newVariant(module *moduleInfo, mutatorName string, variationName string,
local bool) variant {
newVariantName := module.variant.name
if variationName != "" {
if newVariantName == "" {
newVariantName = variationName
} else {
newVariantName += "_" + variationName
}
}
newVariations := module.variant.variations.clone()
if newVariations == nil {
newVariations = make(variationMap)
}
newVariations[mutatorName] = variationName
newDependencyVariations := module.variant.dependencyVariations.clone()
if !local {
if newDependencyVariations == nil {
newDependencyVariations = make(variationMap)
}
newDependencyVariations[mutatorName] = variationName
}
return variant{newVariantName, newVariations, newDependencyVariations}
}
func (c *Context) createVariations(origModule *moduleInfo, mutatorName string,
defaultVariationName *string, variationNames []string) ([]*moduleInfo, []error) {
defaultVariationName *string, variationNames []string, local bool) ([]*moduleInfo, []error) {
if len(variationNames) == 0 {
panic(fmt.Errorf("mutator %q passed zero-length variation list for module %q",
@ -1249,28 +1280,13 @@ func (c *Context) createVariations(origModule *moduleInfo, mutatorName string,
newLogicModule, newProperties = c.cloneLogicModule(origModule)
}
newVariant := origModule.variant.clone()
if newVariant == nil {
newVariant = make(variationMap)
}
newVariant[mutatorName] = variationName
m := *origModule
newModule := &m
newModule.directDeps = append([]depInfo{}, origModule.directDeps...)
newModule.logicModule = newLogicModule
newModule.variant = newVariant
newModule.dependencyVariant = origModule.dependencyVariant.clone()
newModule.variant = newVariant(origModule, mutatorName, variationName, local)
newModule.properties = newProperties
if variationName != "" {
if newModule.variantName == "" {
newModule.variantName = variationName
} else {
newModule.variantName += "_" + variationName
}
}
newModules = append(newModules, newModule)
newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName, defaultVariationName)
@ -1296,7 +1312,7 @@ func (c *Context) convertDepsToVariation(module *moduleInfo,
if dep.module.logicModule == nil {
var newDep *moduleInfo
for _, m := range dep.module.splitModules {
if m.variant[mutatorName] == variationName {
if m.variant.variations[mutatorName] == variationName {
newDep = m
break
}
@ -1304,7 +1320,7 @@ func (c *Context) convertDepsToVariation(module *moduleInfo,
if newDep == nil && defaultVariationName != nil {
// give it a second chance; match with defaultVariationName
for _, m := range dep.module.splitModules {
if m.variant[mutatorName] == *defaultVariationName {
if m.variant.variations[mutatorName] == *defaultVariationName {
newDep = m
break
}
@ -1325,10 +1341,10 @@ func (c *Context) convertDepsToVariation(module *moduleInfo,
return errs
}
func (c *Context) prettyPrintVariant(variant variationMap) string {
names := make([]string, 0, len(variant))
func (c *Context) prettyPrintVariant(variations variationMap) string {
names := make([]string, 0, len(variations))
for _, m := range c.variantMutatorNames {
if v, ok := variant[m]; ok {
if v, ok := variations[m]; ok {
names = append(names, m+":"+v)
}
}
@ -1339,11 +1355,11 @@ func (c *Context) prettyPrintVariant(variant variationMap) string {
func (c *Context) prettyPrintGroupVariants(group *moduleGroup) string {
var variants []string
for _, mod := range group.modules {
variants = append(variants, c.prettyPrintVariant(mod.variant))
variants = append(variants, c.prettyPrintVariant(mod.variant.variations))
}
for _, mod := range group.aliases {
variants = append(variants, c.prettyPrintVariant(mod.variant)+
"(alias to "+c.prettyPrintVariant(mod.target.variant)+")")
variants = append(variants, c.prettyPrintVariant(mod.variant.variations)+
"(alias to "+c.prettyPrintVariant(mod.target.variant.variations)+")")
}
sort.Strings(variants)
return strings.Join(variants, "\n ")
@ -1518,18 +1534,18 @@ func (c *Context) findMatchingVariant(module *moduleInfo, possible *moduleGroup,
if !reverse {
// For forward dependency, ignore local variants by matching against
// dependencyVariant which doesn't have the local variants
variantToMatch = module.dependencyVariant
variantToMatch = module.variant.dependencyVariations
} else {
// For reverse dependency, use all the variants
variantToMatch = module.variant
variantToMatch = module.variant.variations
}
for _, m := range possible.modules {
if m.variant.equal(variantToMatch) {
if m.variant.variations.equal(variantToMatch) {
return m
}
}
for _, m := range possible.aliases {
if m.variant.equal(variantToMatch) {
if m.variant.variations.equal(variantToMatch) {
return m.target
}
}
@ -1563,13 +1579,13 @@ func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName s
if c.allowMissingDependencies {
// Allow missing variants.
return c.discoveredMissingDependencies(module, depName+c.prettyPrintVariant(module.dependencyVariant))
return c.discoveredMissingDependencies(module, depName+c.prettyPrintVariant(module.variant.dependencyVariations))
}
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),
c.prettyPrintVariant(module.variant.dependencyVariations),
c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos,
}}
@ -1598,20 +1614,20 @@ func (c *Context) findReverseDependency(module *moduleInfo, destName string) (*m
if c.allowMissingDependencies {
// Allow missing variants.
return module, c.discoveredMissingDependencies(module, destName+c.prettyPrintVariant(module.dependencyVariant))
return module, c.discoveredMissingDependencies(module, destName+c.prettyPrintVariant(module.variant.dependencyVariations))
}
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),
c.prettyPrintVariant(module.variant.dependencyVariations),
c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos,
}}
}
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
// We can't just append variant.Variant to module.dependencyVariant.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
@ -1619,10 +1635,10 @@ func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, var
if !reverse {
// For forward dependency, ignore local variants by matching against
// dependencyVariant which doesn't have the local variants
newVariant = module.dependencyVariant.clone()
newVariant = module.variant.dependencyVariations.clone()
} else {
// For reverse dependency, use all the variants
newVariant = module.variant.clone()
newVariant = module.variant.variations.clone()
}
}
for _, v := range variations {
@ -1642,7 +1658,7 @@ func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, var
var foundDep *moduleInfo
for _, m := range possibleDeps.modules {
if check(m.variant) {
if check(m.variant.variations) {
foundDep = m
break
}
@ -1650,7 +1666,7 @@ func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, var
if foundDep == nil {
for _, m := range possibleDeps.aliases {
if check(m.variant) {
if check(m.variant.variations) {
foundDep = m.target
break
}
@ -2301,10 +2317,8 @@ func (c *Context) runMutator(config interface{}, mutator *mutatorInfo,
// Create any new aliases.
if module.aliasTarget != nil {
group.aliases = append(group.aliases, &moduleAlias{
variantName: module.variantName,
variant: module.variant,
dependencyVariant: module.dependencyVariant,
target: module.aliasTarget,
variant: module.variant,
target: module.aliasTarget,
})
}
@ -2461,7 +2475,7 @@ func (c *Context) generateModuleBuildActions(config interface{},
uniqueName := c.nameInterface.UniqueName(newNamespaceContext(module), module.group.name)
sanitizedName := toNinjaName(uniqueName)
prefix := moduleNamespacePrefix(sanitizedName + "_" + module.variantName)
prefix := moduleNamespacePrefix(sanitizedName + "_" + module.variant.name)
// The parent scope of the moduleContext's local scope gets overridden to be that of the
// calling Go package on a per-call basis. Since the initial parent scope doesn't matter we
@ -2675,13 +2689,13 @@ func (c *Context) moduleMatchingVariant(module *moduleInfo, name string) *module
}
for _, m := range group.modules {
if module.variantName == m.variantName {
if module.variant.name == m.variant.name {
return m
}
}
for _, m := range group.aliases {
if module.variantName == m.variantName {
if module.variant.name == m.variant.name {
return m.target
}
}
@ -3053,7 +3067,7 @@ func (c *Context) ModuleDir(logicModule Module) string {
func (c *Context) ModuleSubDir(logicModule Module) string {
module := c.moduleInfo[logicModule]
return module.variantName
return module.variant.name
}
func (c *Context) ModuleType(logicModule Module) string {
@ -3499,8 +3513,8 @@ func (s depSorter) Less(i, j int) bool {
iName := s[i].module.Name()
jName := s[j].module.Name()
if iName == jName {
iName = s[i].module.variantName
jName = s[j].module.variantName
iName = s[i].module.variant.name
jName = s[j].module.variant.name
}
return iName < jName
}
@ -3524,8 +3538,8 @@ func (s moduleSorter) Less(i, j int) bool {
iName := s.nameInterface.UniqueName(newNamespaceContext(iMod), iMod.group.name)
jName := s.nameInterface.UniqueName(newNamespaceContext(jMod), jMod.group.name)
if iName == jName {
iName = s.modules[i].variantName
jName = s.modules[j].variantName
iName = s.modules[i].variant.name
jName = s.modules[j].variant.name
}
if iName == jName {
@ -3576,7 +3590,7 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error {
"typeName": module.typeName,
"goFactory": factoryName,
"pos": relPos,
"variant": module.variantName,
"variant": module.variant.name,
}
err = headerTemplate.Execute(buf, infoMap)
if err != nil {

View file

@ -464,7 +464,7 @@ func (m *baseModuleContext) OtherModuleDir(logicModule Module) string {
func (m *baseModuleContext) OtherModuleSubDir(logicModule Module) string {
module := m.context.moduleInfo[logicModule]
return module.variantName
return module.variant.name
}
func (m *baseModuleContext) OtherModuleType(logicModule Module) string {
@ -655,7 +655,7 @@ func (m *baseModuleContext) ModuleFactories() map[string]ModuleFactory {
}
func (m *moduleContext) ModuleSubDir() string {
return m.module.variantName
return m.module.variant.name
}
func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
@ -901,19 +901,13 @@ func (mctx *mutatorContext) CreateLocalVariations(variationNames ...string) []Mo
func (mctx *mutatorContext) createVariations(variationNames []string, local bool) []Module {
ret := []Module{}
modules, errs := mctx.context.createVariations(mctx.module, mctx.name, mctx.defaultVariation, variationNames)
modules, errs := mctx.context.createVariations(mctx.module, mctx.name, mctx.defaultVariation, variationNames, local)
if len(errs) > 0 {
mctx.errs = append(mctx.errs, errs...)
}
for i, module := range modules {
for _, module := range modules {
ret = append(ret, module.logicModule)
if !local {
if module.dependencyVariant == nil {
module.dependencyVariant = make(variationMap)
}
module.dependencyVariant[mctx.name] = variationNames[i]
}
}
if mctx.newVariations != nil {
@ -934,7 +928,7 @@ func (mctx *mutatorContext) AliasVariation(variationName string) {
}
for _, variant := range mctx.newVariations {
if variant.variant[mctx.name] == variationName {
if variant.variant.variations[mctx.name] == variationName {
mctx.module.aliasTarget = variant
return
}
@ -942,7 +936,7 @@ func (mctx *mutatorContext) AliasVariation(variationName string) {
var foundVariations []string
for _, variant := range mctx.newVariations {
foundVariations = append(foundVariations, variant.variant[mctx.name])
foundVariations = append(foundVariations, variant.variant.variations[mctx.name])
}
panic(fmt.Errorf("no %q variation in module variations %q", variationName, foundVariations))
}
@ -1023,7 +1017,7 @@ func (mctx *mutatorContext) ReplaceDependenciesIf(name string, predicate Replace
if target == nil {
panic(fmt.Errorf("ReplaceDependencies could not find identical variant %q for module %q",
mctx.module.variantName, name))
mctx.module.variant.name, name))
}
mctx.replace = append(mctx.replace, replace{target, mctx.module, predicate})

View file

@ -123,7 +123,7 @@ func TestAliases(t *testing.T) {
foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barB := ctx.moduleGroupFromName("bar", nil).modules[1]
if g, w := barB.variantName, "b"; g != w {
if g, w := barB.variant.name, "b"; g != w {
t.Fatalf("expected bar.modules[1] variant to be %q, got %q", w, g)
}
@ -147,7 +147,7 @@ func TestAliases(t *testing.T) {
foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barBB := ctx.moduleGroupFromName("bar", nil).modules[3]
if g, w := barBB.variantName, "b_b"; g != w {
if g, w := barBB.variant.name, "b_b"; g != w {
t.Fatalf("expected bar.modules[3] variant to be %q, got %q", w, g)
}
@ -171,7 +171,7 @@ func TestAliases(t *testing.T) {
foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barAB := ctx.moduleGroupFromName("bar", nil).modules[1]
if g, w := barAB.variantName, "a_b"; g != w {
if g, w := barAB.variant.name, "a_b"; g != w {
t.Fatalf("expected bar.modules[1] variant to be %q, got %q", w, g)
}

View file

@ -20,12 +20,12 @@ import (
)
var (
testModuleA = &moduleInfo{variantName: "testModuleA"}
testModuleB = &moduleInfo{variantName: "testModuleB"}
testModuleC = &moduleInfo{variantName: "testModuleC"}
testModuleD = &moduleInfo{variantName: "testModuleD"}
testModuleE = &moduleInfo{variantName: "testModuleE"}
testModuleF = &moduleInfo{variantName: "testModuleF"}
testModuleA = &moduleInfo{variant: variant{name: "testModuleA"}}
testModuleB = &moduleInfo{variant: variant{name: "testModuleB"}}
testModuleC = &moduleInfo{variant: variant{name: "testModuleC"}}
testModuleD = &moduleInfo{variant: variant{name: "testModuleD"}}
testModuleE = &moduleInfo{variant: variant{name: "testModuleE"}}
testModuleF = &moduleInfo{variant: variant{name: "testModuleF"}}
)
var spliceModulesTestCases = []struct {