Add a String() method to android.Module

Add a String() method to android.Module for use in debugging.  Store
the name and variations of the module as they are mutated.

Test: TestModuleString
Bug: 136473661
Change-Id: I74e393703dcfc96ed4e21ac4a4419a7858b59216
This commit is contained in:
Colin Cross 2019-07-01 15:32:45 -07:00
parent cb55e089f0
commit 9a36223d59
4 changed files with 157 additions and 4 deletions

View file

@ -129,6 +129,8 @@ func registerLoadHookMutator(ctx RegisterMutatorsContext) {
func LoadHookMutator(ctx TopDownMutatorContext) {
if m, ok := ctx.Module().(Module); ok {
m.base().commonProperties.DebugName = ctx.ModuleName()
// Cast through *topDownMutatorContext because AppendProperties is implemented
// on *topDownMutatorContext but not exposed through TopDownMutatorContext
var loadHookCtx LoadHookContext = ctx.(*topDownMutatorContext)

View file

@ -203,6 +203,9 @@ type Module interface {
RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
VariablesForTests() map[string]string
// String returns a string that includes the module name and variants for printing during debugging.
String() string
// Get the qualified module id for this module.
qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName
@ -408,6 +411,11 @@ type commonProperties struct {
NamespaceExportedToMake bool `blueprint:"mutated"`
MissingDeps []string `blueprint:"mutated"`
// Name and variant strings stored by mutators to enable Module.String()
DebugName string `blueprint:"mutated"`
DebugMutators []string `blueprint:"mutated"`
DebugVariations []string `blueprint:"mutated"`
}
type hostAndDeviceProperties struct {
@ -625,6 +633,23 @@ func (m *ModuleBase) Name() string {
return String(m.nameProperties.Name)
}
// String returns a string that includes the module name and variants for printing during debugging.
func (m *ModuleBase) String() string {
sb := strings.Builder{}
sb.WriteString(m.commonProperties.DebugName)
sb.WriteString("{")
for i := range m.commonProperties.DebugMutators {
if i != 0 {
sb.WriteString(",")
}
sb.WriteString(m.commonProperties.DebugMutators[i])
sb.WriteString(":")
sb.WriteString(m.commonProperties.DebugVariations[i])
}
sb.WriteString("}")
return sb.String()
}
// BaseModuleName returns the name of the module as specified in the blueprints file.
func (m *ModuleBase) BaseModuleName() string {
return String(m.nameProperties.Name)

View file

@ -239,6 +239,7 @@ func (t *topDownMutatorContext) MutatorName() string {
func (t *topDownMutatorContext) Rename(name string) {
t.bp.Rename(name)
t.Module().base().commonProperties.DebugName = name
}
func (t *topDownMutatorContext) CreateModule(factory blueprint.ModuleFactory, props ...interface{}) {
@ -251,6 +252,7 @@ func (b *bottomUpMutatorContext) MutatorName() string {
func (b *bottomUpMutatorContext) Rename(name string) {
b.bp.Rename(name)
b.Module().base().commonProperties.DebugName = name
}
func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
@ -262,11 +264,27 @@ func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, t
}
func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
return b.bp.CreateVariations(variations...)
modules := b.bp.CreateVariations(variations...)
for i := range variations {
base := modules[i].(Module).base()
base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
}
return modules
}
func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
return b.bp.CreateLocalVariations(variations...)
modules := b.bp.CreateLocalVariations(variations...)
for i := range variations {
base := modules[i].(Module).base()
base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
}
return modules
}
func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {

View file

@ -24,6 +24,8 @@ import (
type mutatorTestModule struct {
ModuleBase
props struct {
Deps_missing_deps []string
Mutator_missing_deps []string
}
missingDeps []string
@ -46,11 +48,11 @@ func (m *mutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
}
func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) {
ctx.AddDependency(ctx.Module(), nil, "regular_missing_dep")
ctx.AddDependency(ctx.Module(), nil, m.props.Deps_missing_deps...)
}
func addMissingDependenciesMutator(ctx TopDownMutatorContext) {
ctx.AddMissingDependencies([]string{"added_missing_dep"})
ctx.AddMissingDependencies(ctx.Module().(*mutatorTestModule).props.Mutator_missing_deps)
}
func TestMutatorAddMissingDependencies(t *testing.T) {
@ -68,6 +70,8 @@ func TestMutatorAddMissingDependencies(t *testing.T) {
bp := `
test {
name: "foo",
deps_missing_deps: ["regular_missing_dep"],
mutator_missing_deps: ["added_missing_dep"],
}
`
@ -89,3 +93,107 @@ func TestMutatorAddMissingDependencies(t *testing.T) {
t.Errorf("want foo missing deps %q, got %q", w, g)
}
}
func TestModuleString(t *testing.T) {
ctx := NewTestContext()
var moduleStrings []string
ctx.PreArchMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateVariations("a", "b")
})
ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.Rename(ctx.Module().base().Name() + "_renamed1")
})
})
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateVariations("c", "d")
})
})
ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateLocalVariations("e", "f")
})
ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.Rename(ctx.Module().base().Name() + "_renamed2")
})
ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
})
})
ctx.RegisterModuleType("test", ModuleFactoryAdaptor(mutatorTestModuleFactory))
bp := `
test {
name: "foo",
}
`
mockFS := map[string][]byte{
"Android.bp": []byte(bp),
}
ctx.MockFileSystem(mockFS)
ctx.Register()
config := TestConfig(buildDir, nil)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
want := []string{
// Initial name.
"foo{}",
// After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order).
"foo{pre_arch:b}",
"foo{pre_arch:a}",
// After rename_top_down.
"foo_renamed1{pre_arch:a}",
"foo_renamed1{pre_arch:b}",
// After pre_deps.
"foo_renamed1{pre_arch:a,pre_deps:c}",
"foo_renamed1{pre_arch:a,pre_deps:d}",
"foo_renamed1{pre_arch:b,pre_deps:c}",
"foo_renamed1{pre_arch:b,pre_deps:d}",
// After post_deps.
"foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}",
"foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}",
"foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}",
"foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}",
"foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}",
"foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}",
"foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}",
"foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}",
// After rename_bottom_up.
"foo_renamed2{pre_arch:a,pre_deps:c,post_deps:e}",
"foo_renamed2{pre_arch:a,pre_deps:c,post_deps:f}",
"foo_renamed2{pre_arch:a,pre_deps:d,post_deps:e}",
"foo_renamed2{pre_arch:a,pre_deps:d,post_deps:f}",
"foo_renamed2{pre_arch:b,pre_deps:c,post_deps:e}",
"foo_renamed2{pre_arch:b,pre_deps:c,post_deps:f}",
"foo_renamed2{pre_arch:b,pre_deps:d,post_deps:e}",
"foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}",
}
if !reflect.DeepEqual(moduleStrings, want) {
t.Errorf("want module String() values:\n%q\ngot:\n%q", want, moduleStrings)
}
}