Merge remote-tracking branch 'aosp/upstream' am: ce522b4826

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

Change-Id: Ib14cd39cef93cefdcbcb78564767b936c77856af
This commit is contained in:
Colin Cross 2020-08-14 23:00:02 +00:00 committed by Automerger Merge Worker
commit ba9a3b9ea8
4 changed files with 322 additions and 110 deletions

View file

@ -159,9 +159,7 @@ type localBuildActions struct {
} }
type moduleAlias struct { type moduleAlias struct {
variantName string variant variant
variant variationMap
dependencyVariant variationMap
target *moduleInfo target *moduleInfo
} }
@ -184,9 +182,7 @@ type moduleInfo struct {
propertyPos map[string]scanner.Position propertyPos map[string]scanner.Position
createdBy *moduleInfo createdBy *moduleInfo
variantName string variant variant
variant variationMap
dependencyVariant variationMap
logicModule Module logicModule Module
group *moduleGroup group *moduleGroup
@ -206,12 +202,18 @@ type moduleInfo struct {
// set during each runMutator // set during each runMutator
splitModules []*moduleInfo splitModules []*moduleInfo
aliasTarget *moduleInfo pendingAliases []pendingAlias
// set during PrepareBuildActions // set during PrepareBuildActions
actionDefs localBuildActions actionDefs localBuildActions
} }
type variant struct {
name string
variations variationMap
dependencyVariations variationMap
}
type depInfo struct { type depInfo struct {
module *moduleInfo module *moduleInfo
tag DependencyTag tag DependencyTag
@ -232,8 +234,8 @@ func (module *moduleInfo) Name() string {
func (module *moduleInfo) String() string { func (module *moduleInfo) String() string {
s := fmt.Sprintf("module %q", module.Name()) s := fmt.Sprintf("module %q", module.Name())
if module.variantName != "" { if module.variant.name != "" {
s += fmt.Sprintf(" variant %q", module.variantName) s += fmt.Sprintf(" variant %q", module.variant.name)
} }
if module.createdBy != nil { if module.createdBy != nil {
s += fmt.Sprintf(" (created by %s)", module.createdBy) s += fmt.Sprintf(" (created by %s)", module.createdBy)
@ -1224,8 +1226,37 @@ func (c *Context) cloneLogicModule(origModule *moduleInfo) (Module, []interface{
return newLogicModule, newProperties 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, 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 { if len(variationNames) == 0 {
panic(fmt.Errorf("mutator %q passed zero-length variation list for module %q", 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) newLogicModule, newProperties = c.cloneLogicModule(origModule)
} }
newVariant := origModule.variant.clone()
if newVariant == nil {
newVariant = make(variationMap)
}
newVariant[mutatorName] = variationName
m := *origModule m := *origModule
newModule := &m newModule := &m
newModule.directDeps = append([]depInfo{}, origModule.directDeps...) newModule.directDeps = append([]depInfo{}, origModule.directDeps...)
newModule.logicModule = newLogicModule newModule.logicModule = newLogicModule
newModule.variant = newVariant newModule.variant = newVariant(origModule, mutatorName, variationName, local)
newModule.dependencyVariant = origModule.dependencyVariant.clone()
newModule.properties = newProperties newModule.properties = newProperties
if variationName != "" {
if newModule.variantName == "" {
newModule.variantName = variationName
} else {
newModule.variantName += "_" + variationName
}
}
newModules = append(newModules, newModule) newModules = append(newModules, newModule)
newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName, defaultVariationName) newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName, defaultVariationName)
@ -1296,19 +1312,37 @@ func (c *Context) convertDepsToVariation(module *moduleInfo,
if dep.module.logicModule == nil { if dep.module.logicModule == nil {
var newDep *moduleInfo var newDep *moduleInfo
for _, m := range dep.module.splitModules { for _, m := range dep.module.splitModules {
if m.variant[mutatorName] == variationName { if m.variant.variations[mutatorName] == variationName {
newDep = m newDep = m
break break
} }
} }
if newDep == nil {
// check aliases against variationNam
for _, a := range dep.module.pendingAliases {
if a.fromVariant.variations[mutatorName] == variationName {
newDep = a.target
break
}
}
}
if newDep == nil && defaultVariationName != nil { if newDep == nil && defaultVariationName != nil {
// give it a second chance; match with defaultVariationName // give it a second chance; match with defaultVariationName
for _, m := range dep.module.splitModules { for _, m := range dep.module.splitModules {
if m.variant[mutatorName] == *defaultVariationName { if m.variant.variations[mutatorName] == *defaultVariationName {
newDep = m newDep = m
break break
} }
} }
if newDep == nil {
// check aliases against defaultVariationName
for _, a := range dep.module.pendingAliases {
if a.fromVariant.variations[mutatorName] == *defaultVariationName {
newDep = a.target
break
}
}
}
} }
if newDep == nil { if newDep == nil {
errs = append(errs, &BlueprintError{ errs = append(errs, &BlueprintError{
@ -1325,10 +1359,10 @@ func (c *Context) convertDepsToVariation(module *moduleInfo,
return errs return errs
} }
func (c *Context) prettyPrintVariant(variant variationMap) string { func (c *Context) prettyPrintVariant(variations variationMap) string {
names := make([]string, 0, len(variant)) names := make([]string, 0, len(variations))
for _, m := range c.variantMutatorNames { for _, m := range c.variantMutatorNames {
if v, ok := variant[m]; ok { if v, ok := variations[m]; ok {
names = append(names, m+":"+v) names = append(names, m+":"+v)
} }
} }
@ -1339,11 +1373,11 @@ func (c *Context) prettyPrintVariant(variant variationMap) string {
func (c *Context) prettyPrintGroupVariants(group *moduleGroup) string { func (c *Context) prettyPrintGroupVariants(group *moduleGroup) string {
var variants []string var variants []string
for _, mod := range group.modules { 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 { for _, mod := range group.aliases {
variants = append(variants, c.prettyPrintVariant(mod.variant)+ variants = append(variants, c.prettyPrintVariant(mod.variant.variations)+
"(alias to "+c.prettyPrintVariant(mod.target.variant)+")") "(alias to "+c.prettyPrintVariant(mod.target.variant.variations)+")")
} }
sort.Strings(variants) sort.Strings(variants)
return strings.Join(variants, "\n ") return strings.Join(variants, "\n ")
@ -1518,18 +1552,18 @@ func (c *Context) findMatchingVariant(module *moduleInfo, possible *moduleGroup,
if !reverse { if !reverse {
// For forward dependency, ignore local variants by matching against // For forward dependency, ignore local variants by matching against
// dependencyVariant which doesn't have the local variants // dependencyVariant which doesn't have the local variants
variantToMatch = module.dependencyVariant variantToMatch = module.variant.dependencyVariations
} else { } else {
// For reverse dependency, use all the variants // For reverse dependency, use all the variants
variantToMatch = module.variant variantToMatch = module.variant.variations
} }
for _, m := range possible.modules { for _, m := range possible.modules {
if m.variant.equal(variantToMatch) { if m.variant.variations.equal(variantToMatch) {
return m return m
} }
} }
for _, m := range possible.aliases { for _, m := range possible.aliases {
if m.variant.equal(variantToMatch) { if m.variant.variations.equal(variantToMatch) {
return m.target return m.target
} }
} }
@ -1563,13 +1597,13 @@ func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName s
if c.allowMissingDependencies { if c.allowMissingDependencies {
// Allow missing variants. // 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{ return []error{&BlueprintError{
Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s", Err: fmt.Errorf("dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
depName, module.Name(), depName, module.Name(),
c.prettyPrintVariant(module.dependencyVariant), c.prettyPrintVariant(module.variant.dependencyVariations),
c.prettyPrintGroupVariants(possibleDeps)), c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos, Pos: module.pos,
}} }}
@ -1598,20 +1632,20 @@ func (c *Context) findReverseDependency(module *moduleInfo, destName string) (*m
if c.allowMissingDependencies { if c.allowMissingDependencies {
// Allow missing variants. // 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{ return nil, []error{&BlueprintError{
Err: fmt.Errorf("reverse dependency %q of %q missing variant:\n %s\navailable variants:\n %s", Err: fmt.Errorf("reverse dependency %q of %q missing variant:\n %s\navailable variants:\n %s",
destName, module.Name(), destName, module.Name(),
c.prettyPrintVariant(module.dependencyVariant), c.prettyPrintVariant(module.variant.dependencyVariations),
c.prettyPrintGroupVariants(possibleDeps)), c.prettyPrintGroupVariants(possibleDeps)),
Pos: module.pos, Pos: module.pos,
}} }}
} }
func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, variations []Variation, far bool, reverse bool) (*moduleInfo, variationMap) { 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. // compare the strings because the result won't be in mutator registration order.
// Create a new map instead, and then deep compare the maps. // Create a new map instead, and then deep compare the maps.
var newVariant variationMap var newVariant variationMap
@ -1619,10 +1653,10 @@ func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, var
if !reverse { if !reverse {
// For forward dependency, ignore local variants by matching against // For forward dependency, ignore local variants by matching against
// dependencyVariant which doesn't have the local variants // dependencyVariant which doesn't have the local variants
newVariant = module.dependencyVariant.clone() newVariant = module.variant.dependencyVariations.clone()
} else { } else {
// For reverse dependency, use all the variants // For reverse dependency, use all the variants
newVariant = module.variant.clone() newVariant = module.variant.variations.clone()
} }
} }
for _, v := range variations { for _, v := range variations {
@ -1642,7 +1676,7 @@ func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, var
var foundDep *moduleInfo var foundDep *moduleInfo
for _, m := range possibleDeps.modules { for _, m := range possibleDeps.modules {
if check(m.variant) { if check(m.variant.variations) {
foundDep = m foundDep = m
break break
} }
@ -1650,7 +1684,7 @@ func (c *Context) findVariant(module *moduleInfo, possibleDeps *moduleGroup, var
if foundDep == nil { if foundDep == nil {
for _, m := range possibleDeps.aliases { for _, m := range possibleDeps.aliases {
if check(m.variant) { if check(m.variant.variations) {
foundDep = m.target foundDep = m.target
break break
} }
@ -2205,6 +2239,8 @@ func (c *Context) runMutator(config interface{}, mutator *mutatorInfo,
panic("split module found in sorted module list") panic("split module found in sorted module list")
} }
module.pendingAliases = nil
mctx := &mutatorContext{ mctx := &mutatorContext{
baseModuleContext: baseModuleContext{ baseModuleContext: baseModuleContext{
context: c, context: c,
@ -2299,12 +2335,10 @@ func (c *Context) runMutator(config interface{}, mutator *mutatorInfo,
} }
// Create any new aliases. // Create any new aliases.
if module.aliasTarget != nil { for _, alias := range module.pendingAliases {
group.aliases = append(group.aliases, &moduleAlias{ group.aliases = append(group.aliases, &moduleAlias{
variantName: module.variantName, variant: alias.fromVariant,
variant: module.variant, target: alias.target,
dependencyVariant: module.dependencyVariant,
target: module.aliasTarget,
}) })
} }
@ -2325,13 +2359,23 @@ func (c *Context) runMutator(config interface{}, mutator *mutatorInfo,
module.newDirectDeps = nil module.newDirectDeps = nil
} }
findAliasTarget := func(variant variant) *moduleInfo {
for _, alias := range group.aliases {
if alias.variant.variations.equal(variant.variations) {
return alias.target
}
}
return nil
}
// Forward or delete any dangling aliases. // Forward or delete any dangling aliases.
for i := 0; i < len(group.aliases); i++ { for i := 0; i < len(group.aliases); i++ {
alias := group.aliases[i] alias := group.aliases[i]
if alias.target.logicModule == nil { if alias.target.logicModule == nil {
if alias.target.aliasTarget != nil { newTarget := findAliasTarget(alias.target.variant)
alias.target = alias.target.aliasTarget if newTarget != nil {
alias.target = newTarget
} else { } else {
// The alias was left dangling, remove it. // The alias was left dangling, remove it.
group.aliases = append(group.aliases[:i], group.aliases[i+1:]...) group.aliases = append(group.aliases[:i], group.aliases[i+1:]...)
@ -2461,7 +2505,7 @@ func (c *Context) generateModuleBuildActions(config interface{},
uniqueName := c.nameInterface.UniqueName(newNamespaceContext(module), module.group.name) uniqueName := c.nameInterface.UniqueName(newNamespaceContext(module), module.group.name)
sanitizedName := toNinjaName(uniqueName) 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 // 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 // calling Go package on a per-call basis. Since the initial parent scope doesn't matter we
@ -2675,13 +2719,13 @@ func (c *Context) moduleMatchingVariant(module *moduleInfo, name string) *module
} }
for _, m := range group.modules { for _, m := range group.modules {
if module.variantName == m.variantName { if module.variant.name == m.variant.name {
return m return m
} }
} }
for _, m := range group.aliases { for _, m := range group.aliases {
if module.variantName == m.variantName { if module.variant.name == m.variant.name {
return m.target return m.target
} }
} }
@ -3053,7 +3097,7 @@ func (c *Context) ModuleDir(logicModule Module) string {
func (c *Context) ModuleSubDir(logicModule Module) string { func (c *Context) ModuleSubDir(logicModule Module) string {
module := c.moduleInfo[logicModule] module := c.moduleInfo[logicModule]
return module.variantName return module.variant.name
} }
func (c *Context) ModuleType(logicModule Module) string { func (c *Context) ModuleType(logicModule Module) string {
@ -3499,8 +3543,8 @@ func (s depSorter) Less(i, j int) bool {
iName := s[i].module.Name() iName := s[i].module.Name()
jName := s[j].module.Name() jName := s[j].module.Name()
if iName == jName { if iName == jName {
iName = s[i].module.variantName iName = s[i].module.variant.name
jName = s[j].module.variantName jName = s[j].module.variant.name
} }
return iName < jName return iName < jName
} }
@ -3524,15 +3568,18 @@ func (s moduleSorter) Less(i, j int) bool {
iName := s.nameInterface.UniqueName(newNamespaceContext(iMod), iMod.group.name) iName := s.nameInterface.UniqueName(newNamespaceContext(iMod), iMod.group.name)
jName := s.nameInterface.UniqueName(newNamespaceContext(jMod), jMod.group.name) jName := s.nameInterface.UniqueName(newNamespaceContext(jMod), jMod.group.name)
if iName == jName { if iName == jName {
iName = s.modules[i].variantName iVariantName := s.modules[i].variant.name
jName = s.modules[j].variantName jVariantName := s.modules[j].variant.name
} if iVariantName == jVariantName {
panic(fmt.Sprintf("duplicate module name: %s %s: %#v and %#v\n",
if iName == jName { iName, iVariantName, iMod.variant.variations, jMod.variant.variations))
panic(fmt.Sprintf("duplicate module name: %s: %#v and %#v\n", iName, iMod, jMod)) } else {
return iVariantName < jVariantName
} }
} else {
return iName < jName return iName < jName
} }
}
func (s moduleSorter) Swap(i, j int) { func (s moduleSorter) Swap(i, j int) {
s.modules[i], s.modules[j] = s.modules[j], s.modules[i] s.modules[i], s.modules[j] = s.modules[j], s.modules[i]
@ -3576,7 +3623,7 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error {
"typeName": module.typeName, "typeName": module.typeName,
"goFactory": factoryName, "goFactory": factoryName,
"pos": relPos, "pos": relPos,
"variant": module.variantName, "variant": module.variant.name,
} }
err = headerTemplate.Execute(buf, infoMap) err = headerTemplate.Execute(buf, infoMap)
if err != nil { if err != nil {

View file

@ -464,7 +464,7 @@ func (m *baseModuleContext) OtherModuleDir(logicModule Module) string {
func (m *baseModuleContext) OtherModuleSubDir(logicModule Module) string { func (m *baseModuleContext) OtherModuleSubDir(logicModule Module) string {
module := m.context.moduleInfo[logicModule] module := m.context.moduleInfo[logicModule]
return module.variantName return module.variant.name
} }
func (m *baseModuleContext) OtherModuleType(logicModule Module) string { func (m *baseModuleContext) OtherModuleType(logicModule Module) string {
@ -655,7 +655,7 @@ func (m *baseModuleContext) ModuleFactories() map[string]ModuleFactory {
} }
func (m *moduleContext) ModuleSubDir() string { func (m *moduleContext) ModuleSubDir() string {
return m.module.variantName return m.module.variant.name
} }
func (m *moduleContext) Variable(pctx PackageContext, name, value string) { func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
@ -850,12 +850,21 @@ type BottomUpMutatorContext interface {
// Replacements don't take effect until after the mutator pass is finished. // Replacements don't take effect until after the mutator pass is finished.
ReplaceDependenciesIf(string, ReplaceDependencyPredicate) ReplaceDependenciesIf(string, ReplaceDependencyPredicate)
// AliasVariation takes a variationName that was passed to CreateVariations for this module, and creates an // AliasVariation takes a variationName that was passed to CreateVariations for this module,
// alias from the current variant to the new variant. The alias will be valid until the next time a mutator // and creates an alias from the current variant (before the mutator has run) to the new
// calls CreateVariations or CreateLocalVariations on this module without also calling AliasVariation. The // variant. The alias will be valid until the next time a mutator calls CreateVariations or
// alias can be used to add dependencies on the newly created variant using the variant map from before // CreateLocalVariations on this module without also calling AliasVariation. The alias can
// CreateVariations was run. // be used to add dependencies on the newly created variant using the variant map from
// before CreateVariations was run.
AliasVariation(variationName string) AliasVariation(variationName string)
// CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
// module, and creates an alias from a new fromVariationName variant the toVariationName
// variant. The alias will be valid until the next time a mutator calls CreateVariations or
// CreateLocalVariations on this module without also calling AliasVariation. The alias can
// be used to add dependencies on the toVariationName variant using the fromVariationName
// variant.
CreateAliasVariation(fromVariationName, toVariationName string)
} }
// A Mutator function is called for each Module, and can use // A Mutator function is called for each Module, and can use
@ -899,21 +908,20 @@ func (mctx *mutatorContext) CreateLocalVariations(variationNames ...string) []Mo
return mctx.createVariations(variationNames, true) return mctx.createVariations(variationNames, true)
} }
type pendingAlias struct {
fromVariant variant
target *moduleInfo
}
func (mctx *mutatorContext) createVariations(variationNames []string, local bool) []Module { func (mctx *mutatorContext) createVariations(variationNames []string, local bool) []Module {
ret := []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 { if len(errs) > 0 {
mctx.errs = append(mctx.errs, errs...) mctx.errs = append(mctx.errs, errs...)
} }
for i, module := range modules { for _, module := range modules {
ret = append(ret, module.logicModule) 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 { if mctx.newVariations != nil {
@ -929,24 +937,55 @@ func (mctx *mutatorContext) createVariations(variationNames []string, local bool
} }
func (mctx *mutatorContext) AliasVariation(variationName string) { func (mctx *mutatorContext) AliasVariation(variationName string) {
if mctx.module.aliasTarget != nil { for _, alias := range mctx.module.pendingAliases {
if alias.fromVariant.variations.equal(mctx.module.variant.variations) {
panic(fmt.Errorf("AliasVariation already called")) panic(fmt.Errorf("AliasVariation already called"))
} }
}
for _, variant := range mctx.newVariations { for _, variant := range mctx.newVariations {
if variant.variant[mctx.name] == variationName { if variant.variant.variations[mctx.name] == variationName {
mctx.module.aliasTarget = variant mctx.module.pendingAliases = append(mctx.module.pendingAliases, pendingAlias{
fromVariant: mctx.module.variant,
target: variant,
})
return return
} }
} }
var foundVariations []string var foundVariations []string
for _, variant := range mctx.newVariations { 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)) panic(fmt.Errorf("no %q variation in module variations %q", variationName, foundVariations))
} }
func (mctx *mutatorContext) CreateAliasVariation(aliasVariationName, targetVariationName string) {
newVariant := newVariant(mctx.module, mctx.name, aliasVariationName, false)
for _, alias := range mctx.module.pendingAliases {
if alias.fromVariant.variations.equal(newVariant.variations) {
panic(fmt.Errorf("can't alias %q to %q, already aliased to %q", aliasVariationName, targetVariationName, alias.target.variant.name))
}
}
for _, variant := range mctx.newVariations {
if variant.variant.variations[mctx.name] == targetVariationName {
mctx.module.pendingAliases = append(mctx.module.pendingAliases, pendingAlias{
fromVariant: newVariant,
target: variant,
})
return
}
}
var foundVariations []string
for _, variant := range mctx.newVariations {
foundVariations = append(foundVariations, variant.variant.variations[mctx.name])
}
panic(fmt.Errorf("no %q variation in module variations %q", targetVariationName, foundVariations))
}
func (mctx *mutatorContext) SetDependencyVariation(variationName string) { func (mctx *mutatorContext) SetDependencyVariation(variationName string) {
mctx.context.convertDepsToVariation(mctx.module, mctx.name, variationName, nil) mctx.context.convertDepsToVariation(mctx.module, mctx.name, variationName, nil)
} }
@ -1023,7 +1062,7 @@ func (mctx *mutatorContext) ReplaceDependenciesIf(name string, predicate Replace
if target == nil { if target == nil {
panic(fmt.Errorf("ReplaceDependencies could not find identical variant %q for module %q", 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}) mctx.replace = append(mctx.replace, replace{target, mctx.module, predicate})

View file

@ -32,7 +32,7 @@ func newModuleCtxTestModule() (Module, []interface{}) {
func (f *moduleCtxTestModule) GenerateBuildActions(ModuleContext) { func (f *moduleCtxTestModule) GenerateBuildActions(ModuleContext) {
} }
func noCreateAliasMutator(name string) func(ctx BottomUpMutatorContext) { func noAliasMutator(name string) func(ctx BottomUpMutatorContext) {
return func(ctx BottomUpMutatorContext) { return func(ctx BottomUpMutatorContext) {
if ctx.ModuleName() == name { if ctx.ModuleName() == name {
ctx.CreateVariations("a", "b") ctx.CreateVariations("a", "b")
@ -40,11 +40,22 @@ func noCreateAliasMutator(name string) func(ctx BottomUpMutatorContext) {
} }
} }
func aliasMutator(name string) func(ctx BottomUpMutatorContext) {
return func(ctx BottomUpMutatorContext) {
if ctx.ModuleName() == name {
ctx.CreateVariations("a", "b")
ctx.AliasVariation("b")
}
}
}
func createAliasMutator(name string) func(ctx BottomUpMutatorContext) { func createAliasMutator(name string) func(ctx BottomUpMutatorContext) {
return func(ctx BottomUpMutatorContext) { return func(ctx BottomUpMutatorContext) {
if ctx.ModuleName() == name { if ctx.ModuleName() == name {
ctx.CreateVariations("a", "b") ctx.CreateVariations("a", "b")
ctx.AliasVariation("b") ctx.CreateAliasVariation("c", "a")
ctx.CreateAliasVariation("d", "b")
ctx.CreateAliasVariation("e", "a")
} }
} }
} }
@ -57,7 +68,7 @@ func addVariantDepsMutator(variants []Variation, tag DependencyTag, from, to str
} }
} }
func TestAliases(t *testing.T) { func TestAliasVariation(t *testing.T) {
runWithFailures := func(ctx *Context, expectedErr string) { runWithFailures := func(ctx *Context, expectedErr string) {
t.Helper() t.Helper()
bp := ` bp := `
@ -115,7 +126,7 @@ func TestAliases(t *testing.T) {
// Tests a dependency from "foo" to "bar" variant "b" through alias "". // Tests a dependency from "foo" to "bar" variant "b" through alias "".
ctx := NewContext() ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule) ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar")) ctx.RegisterBottomUpMutator("1", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", addVariantDepsMutator(nil, nil, "foo", "bar")) ctx.RegisterBottomUpMutator("2", addVariantDepsMutator(nil, nil, "foo", "bar"))
run(ctx) run(ctx)
@ -123,7 +134,7 @@ func TestAliases(t *testing.T) {
foo := ctx.moduleGroupFromName("foo", nil).modules[0] foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barB := ctx.moduleGroupFromName("bar", nil).modules[1] 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) t.Fatalf("expected bar.modules[1] variant to be %q, got %q", w, g)
} }
@ -138,8 +149,8 @@ func TestAliases(t *testing.T) {
// Tests a dependency from "foo" to "bar" variant "b_b" through alias "". // Tests a dependency from "foo" to "bar" variant "b_b" through alias "".
ctx := NewContext() ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule) ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar")) ctx.RegisterBottomUpMutator("1", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", createAliasMutator("bar")) ctx.RegisterBottomUpMutator("2", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("3", addVariantDepsMutator(nil, nil, "foo", "bar")) ctx.RegisterBottomUpMutator("3", addVariantDepsMutator(nil, nil, "foo", "bar"))
run(ctx) run(ctx)
@ -147,7 +158,7 @@ func TestAliases(t *testing.T) {
foo := ctx.moduleGroupFromName("foo", nil).modules[0] foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barBB := ctx.moduleGroupFromName("bar", nil).modules[3] 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) t.Fatalf("expected bar.modules[3] variant to be %q, got %q", w, g)
} }
@ -162,8 +173,8 @@ func TestAliases(t *testing.T) {
// Tests a dependency from "foo" to "bar" variant "a_b" through alias "a". // Tests a dependency from "foo" to "bar" variant "a_b" through alias "a".
ctx := NewContext() ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule) ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar")) ctx.RegisterBottomUpMutator("1", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", createAliasMutator("bar")) ctx.RegisterBottomUpMutator("2", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("3", addVariantDepsMutator([]Variation{{"1", "a"}}, nil, "foo", "bar")) ctx.RegisterBottomUpMutator("3", addVariantDepsMutator([]Variation{{"1", "a"}}, nil, "foo", "bar"))
run(ctx) run(ctx)
@ -171,7 +182,7 @@ func TestAliases(t *testing.T) {
foo := ctx.moduleGroupFromName("foo", nil).modules[0] foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barAB := ctx.moduleGroupFromName("bar", nil).modules[1] 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) t.Fatalf("expected bar.modules[1] variant to be %q, got %q", w, g)
} }
@ -186,8 +197,8 @@ func TestAliases(t *testing.T) {
// Tests a dependency from "foo" to removed "bar" alias "" fails. // Tests a dependency from "foo" to removed "bar" alias "" fails.
ctx := NewContext() ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule) ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar")) ctx.RegisterBottomUpMutator("1", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", noCreateAliasMutator("bar")) ctx.RegisterBottomUpMutator("2", noAliasMutator("bar"))
ctx.RegisterBottomUpMutator("3", addVariantDepsMutator(nil, nil, "foo", "bar")) ctx.RegisterBottomUpMutator("3", addVariantDepsMutator(nil, nil, "foo", "bar"))
runWithFailures(ctx, `dependency "bar" of "foo" missing variant:`+"\n \n"+ runWithFailures(ctx, `dependency "bar" of "foo" missing variant:`+"\n \n"+
@ -196,6 +207,121 @@ func TestAliases(t *testing.T) {
}) })
} }
func TestCreateAliasVariations(t *testing.T) {
runWithFailures := func(ctx *Context, expectedErr string) {
t.Helper()
bp := `
test {
name: "foo",
}
test {
name: "bar",
}
`
mockFS := map[string][]byte{
"Blueprints": []byte(bp),
}
ctx.MockFileSystem(mockFS)
_, errs := ctx.ParseFileList(".", []string{"Blueprints"}, nil)
if len(errs) > 0 {
t.Errorf("unexpected parse errors:")
for _, err := range errs {
t.Errorf(" %s", err)
}
}
_, errs = ctx.ResolveDependencies(nil)
if len(errs) > 0 {
if expectedErr == "" {
t.Errorf("unexpected dep errors:")
for _, err := range errs {
t.Errorf(" %s", err)
}
} else {
for _, err := range errs {
if strings.Contains(err.Error(), expectedErr) {
continue
} else {
t.Errorf("unexpected dep error: %s", err)
}
}
}
} else if expectedErr != "" {
t.Errorf("missing dep error: %s", expectedErr)
}
}
run := func(ctx *Context) {
t.Helper()
runWithFailures(ctx, "")
}
t.Run("simple", func(t *testing.T) {
// Creates a module "bar" with variants "a" and "b" and alias "c" -> "a", "d" -> "b", and "e" -> "a".
// Tests a dependency from "foo" to "bar" variant "b" through alias "d".
ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", addVariantDepsMutator([]Variation{{"1", "d"}}, nil, "foo", "bar"))
run(ctx)
foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barB := ctx.moduleGroupFromName("bar", nil).modules[1]
if g, w := barB.variant.name, "b"; g != w {
t.Fatalf("expected bar.modules[1] variant to be %q, got %q", w, g)
}
if g, w := foo.forwardDeps, []*moduleInfo{barB}; !reflect.DeepEqual(g, w) {
t.Fatalf("expected foo deps to be %q, got %q", w, g)
}
})
t.Run("chained", func(t *testing.T) {
// Creates a module "bar" with variants "a_a", "a_b", "b_a" and "b_b" and aliases "c" -> "a_b",
// "d" -> "b_b", and "d" -> "a_b".
// Tests a dependency from "foo" to "bar" variant "b_b" through alias "d".
ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", aliasMutator("bar"))
ctx.RegisterBottomUpMutator("3", addVariantDepsMutator([]Variation{{"1", "d"}}, nil, "foo", "bar"))
run(ctx)
foo := ctx.moduleGroupFromName("foo", nil).modules[0]
barBB := ctx.moduleGroupFromName("bar", nil).modules[3]
if g, w := barBB.variant.name, "b_b"; g != w {
t.Fatalf("expected bar.modules[3] variant to be %q, got %q", w, g)
}
if g, w := foo.forwardDeps, []*moduleInfo{barBB}; !reflect.DeepEqual(g, w) {
t.Fatalf("expected foo deps to be %q, got %q", w, g)
}
})
t.Run("removed dangling alias", func(t *testing.T) {
// Creates a module "bar" with variants "a" and "b" and alias "c" -> "a", "d" -> "b", and "e" -> "a",
// then splits the variants into "a_a", "a_b", "b_a" and "b_b" without creating new aliases.
// Tests a dependency from "foo" to removed "bar" alias "d" fails.
ctx := NewContext()
ctx.RegisterModuleType("test", newModuleCtxTestModule)
ctx.RegisterBottomUpMutator("1", createAliasMutator("bar"))
ctx.RegisterBottomUpMutator("2", noAliasMutator("bar"))
ctx.RegisterBottomUpMutator("3", addVariantDepsMutator([]Variation{{"1", "d"}}, nil, "foo", "bar"))
runWithFailures(ctx, `dependency "bar" of "foo" missing variant:`+"\n 1:d\n"+
"available variants:"+
"\n 1:a, 2:a\n 1:a, 2:b\n 1:b, 2:a\n 1:b, 2:b")
})
}
func expectedErrors(t *testing.T, errs []error, expectedMessages ...string) { func expectedErrors(t *testing.T, errs []error, expectedMessages ...string) {
t.Helper() t.Helper()
if len(errs) != len(expectedMessages) { if len(errs) != len(expectedMessages) {

View file

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