Convert Visit*Deps from blueprint.Module to android.Module

Also adds checks that the dependencies are android.Modules and
are not disabled.

Test: m checkbuild
Change-Id: I05e945f38915d49cd3c0ab72a86576949bc7eff2
diff --git a/android/defaults.go b/android/defaults.go
index 4bf872e..c704529 100644
--- a/android/defaults.go
+++ b/android/defaults.go
@@ -131,7 +131,7 @@
 func defaultsMutator(ctx TopDownMutatorContext) {
 	if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 {
 		var defaultsList []Defaults
-		ctx.WalkDeps(func(module, parent blueprint.Module) bool {
+		ctx.WalkDeps(func(module, parent Module) bool {
 			if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
 				if defaults, ok := module.(Defaults); ok {
 					defaultsList = append(defaultsList, defaults)
diff --git a/android/module.go b/android/module.go
index 0fada78..4d4462b 100644
--- a/android/module.go
+++ b/android/module.go
@@ -108,11 +108,11 @@
 
 	ModuleSubDir() string
 
-	VisitDirectDeps(visit func(blueprint.Module))
-	VisitDirectDepsIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
-	VisitDepsDepthFirst(visit func(blueprint.Module))
-	VisitDepsDepthFirstIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
-	WalkDeps(visit func(blueprint.Module, blueprint.Module) bool)
+	VisitDirectDeps(visit func(Module))
+	VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
+	VisitDepsDepthFirst(visit func(Module))
+	VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
+	WalkDeps(visit func(Module, Module) bool)
 
 	Variable(pctx blueprint.PackageContext, name, value string)
 	Rule(pctx blueprint.PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
@@ -663,9 +663,89 @@
 func (a *androidModuleContext) AddMissingDependencies(deps []string) {
 	if deps != nil {
 		a.missingDeps = append(a.missingDeps, deps...)
+		a.missingDeps = FirstUniqueStrings(a.missingDeps)
 	}
 }
 
+func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
+	aModule, _ := module.(Module)
+	if aModule == nil {
+		a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
+		return nil
+	}
+
+	if !aModule.Enabled() {
+		if a.AConfig().AllowMissingDependencies() {
+			a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
+		} else {
+			a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
+		}
+		return nil
+	}
+
+	return aModule
+}
+
+func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
+	a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
+		if aModule := a.validateAndroidModule(module); aModule != nil {
+			visit(aModule)
+		}
+	})
+}
+
+func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
+	a.ModuleContext.VisitDirectDepsIf(
+		// pred
+		func(module blueprint.Module) bool {
+			if aModule := a.validateAndroidModule(module); aModule != nil {
+				return pred(aModule)
+			} else {
+				return false
+			}
+		},
+		// visit
+		func(module blueprint.Module) {
+			visit(module.(Module))
+		})
+}
+
+func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
+	a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
+		if aModule := a.validateAndroidModule(module); aModule != nil {
+			visit(aModule)
+		}
+	})
+}
+
+func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
+	a.ModuleContext.VisitDepsDepthFirstIf(
+		// pred
+		func(module blueprint.Module) bool {
+			if aModule := a.validateAndroidModule(module); aModule != nil {
+				return pred(aModule)
+			} else {
+				return false
+			}
+		},
+		// visit
+		func(module blueprint.Module) {
+			visit(module.(Module))
+		})
+}
+
+func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
+	a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
+		childAndroidModule := a.validateAndroidModule(child)
+		parentAndroidModule := a.validateAndroidModule(parent)
+		if childAndroidModule != nil && parentAndroidModule != nil {
+			return visit(childAndroidModule, parentAndroidModule)
+		} else {
+			return false
+		}
+	})
+}
+
 func (a *androidBaseContextImpl) Target() Target {
 	return a.target
 }
diff --git a/android/mutator.go b/android/mutator.go
index b485940..afff700 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -123,11 +123,11 @@
 	GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
 	GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
 
-	VisitDirectDeps(visit func(blueprint.Module))
-	VisitDirectDepsIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
-	VisitDepsDepthFirst(visit func(blueprint.Module))
-	VisitDepsDepthFirstIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
-	WalkDeps(visit func(blueprint.Module, blueprint.Module) bool)
+	VisitDirectDeps(visit func(Module))
+	VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
+	VisitDepsDepthFirst(visit func(Module))
+	VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
+	WalkDeps(visit func(Module, Module) bool)
 }
 
 type androidTopDownMutatorContext struct {
@@ -191,3 +191,63 @@
 		m.DepsMutator(ctx)
 	}
 }
+
+func (a *androidTopDownMutatorContext) VisitDirectDeps(visit func(Module)) {
+	a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
+		if aModule, _ := module.(Module); aModule != nil {
+			visit(aModule)
+		}
+	})
+}
+
+func (a *androidTopDownMutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
+	a.TopDownMutatorContext.VisitDirectDepsIf(
+		// pred
+		func(module blueprint.Module) bool {
+			if aModule, _ := module.(Module); aModule != nil {
+				return pred(aModule)
+			} else {
+				return false
+			}
+		},
+		// visit
+		func(module blueprint.Module) {
+			visit(module.(Module))
+		})
+}
+
+func (a *androidTopDownMutatorContext) VisitDepsDepthFirst(visit func(Module)) {
+	a.TopDownMutatorContext.VisitDepsDepthFirst(func(module blueprint.Module) {
+		if aModule, _ := module.(Module); aModule != nil {
+			visit(aModule)
+		}
+	})
+}
+
+func (a *androidTopDownMutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
+	a.TopDownMutatorContext.VisitDepsDepthFirstIf(
+		// pred
+		func(module blueprint.Module) bool {
+			if aModule, _ := module.(Module); aModule != nil {
+				return pred(aModule)
+			} else {
+				return false
+			}
+		},
+		// visit
+		func(module blueprint.Module) {
+			visit(module.(Module))
+		})
+}
+
+func (a *androidTopDownMutatorContext) WalkDeps(visit func(Module, Module) bool) {
+	a.TopDownMutatorContext.WalkDeps(func(child, parent blueprint.Module) bool {
+		childAndroidModule, _ := child.(Module)
+		parentAndroidModule, _ := parent.(Module)
+		if childAndroidModule != nil && parentAndroidModule != nil {
+			return visit(childAndroidModule, parentAndroidModule)
+		} else {
+			return false
+		}
+	})
+}
diff --git a/android/prebuilt.go b/android/prebuilt.go
index f61a0dd..f29f865 100644
--- a/android/prebuilt.go
+++ b/android/prebuilt.go
@@ -109,7 +109,7 @@
 			p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
 		}
 	} else if s, ok := ctx.Module().(Module); ok {
-		ctx.VisitDirectDeps(func(m blueprint.Module) {
+		ctx.VisitDirectDeps(func(m Module) {
 			if ctx.OtherModuleDependencyTag(m) == prebuiltDepTag {
 				p := m.(PrebuiltInterface).Prebuilt()
 				if p.usePrebuilt(ctx, s) {
diff --git a/cc/cc.go b/cc/cc.go
index 36b97e1..b423ca6 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1037,16 +1037,10 @@
 
 	directStaticDeps := []*Module{}
 
-	ctx.VisitDirectDeps(func(dep blueprint.Module) {
+	ctx.VisitDirectDeps(func(dep android.Module) {
 		depName := ctx.OtherModuleName(dep)
 		depTag := ctx.OtherModuleDependencyTag(dep)
 
-		aDep, _ := dep.(android.Module)
-		if aDep == nil {
-			ctx.ModuleErrorf("module %q not an android module", depName)
-			return
-		}
-
 		ccDep, _ := dep.(*Module)
 		if ccDep == nil {
 			// handling for a few module types that aren't cc Module but that are also supported
@@ -1096,20 +1090,11 @@
 			return
 		}
 
-		// some validation
-		if !aDep.Enabled() {
-			if ctx.AConfig().AllowMissingDependencies() {
-				ctx.AddMissingDependencies([]string{depName})
-			} else {
-				ctx.ModuleErrorf("depends on disabled module %q", depName)
-			}
-			return
-		}
-		if aDep.Target().Os != ctx.Os() {
+		if dep.Target().Os != ctx.Os() {
 			ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
 			return
 		}
-		if aDep.Target().Arch.ArchType != ctx.Arch().ArchType {
+		if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
 			ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
 			return
 		}
diff --git a/cc/coverage.go b/cc/coverage.go
index 0b4188f..d2eede2 100644
--- a/cc/coverage.go
+++ b/cc/coverage.go
@@ -16,8 +16,6 @@
 
 import (
 	"android/soong/android"
-
-	"github.com/google/blueprint"
 )
 
 type CoverageProperties struct {
@@ -61,7 +59,7 @@
 			// For static libraries, the only thing that changes our object files
 			// are included whole static libraries, so check to see if any of
 			// those have coverage enabled.
-			ctx.VisitDirectDeps(func(m blueprint.Module) {
+			ctx.VisitDirectDeps(func(m android.Module) {
 				if ctx.OtherModuleDependencyTag(m) != wholeStaticDepTag {
 					return
 				}
@@ -75,7 +73,7 @@
 		} else {
 			// For executables and shared libraries, we need to check all of
 			// our static dependencies.
-			ctx.VisitDirectDeps(func(m blueprint.Module) {
+			ctx.VisitDirectDeps(func(m android.Module) {
 				cc, ok := m.(*Module)
 				if !ok || cc.coverage == nil {
 					return
diff --git a/cc/lto.go b/cc/lto.go
index 6a5ecde..fdb7688 100644
--- a/cc/lto.go
+++ b/cc/lto.go
@@ -15,8 +15,6 @@
 package cc
 
 import (
-	"github.com/google/blueprint"
-
 	"android/soong/android"
 )
 
@@ -104,7 +102,7 @@
 			mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
 		}
 
-		mctx.VisitDepsDepthFirst(func(m blueprint.Module) {
+		mctx.VisitDepsDepthFirst(func(m android.Module) {
 			tag := mctx.OtherModuleDependencyTag(m)
 			switch tag {
 			case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag, objDepTag, reuseObjTag:
diff --git a/cc/sabi.go b/cc/sabi.go
index 8086f5b..ec1d246 100644
--- a/cc/sabi.go
+++ b/cc/sabi.go
@@ -17,8 +17,6 @@
 import (
 	"strings"
 
-	"github.com/google/blueprint"
-
 	"android/soong/android"
 	"android/soong/cc/config"
 )
@@ -81,7 +79,7 @@
 	if c, ok := mctx.Module().(*Module); ok &&
 		((c.isVndk() && c.useVndk()) || inList(c.Name(), llndkLibraries) ||
 			(c.sabi != nil && c.sabi.Properties.CreateSAbiDumps)) {
-		mctx.VisitDirectDeps(func(m blueprint.Module) {
+		mctx.VisitDirectDeps(func(m android.Module) {
 			tag := mctx.OtherModuleDependencyTag(m)
 			switch tag {
 			case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag:
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 74f4bdb..d5535cb 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -19,8 +19,6 @@
 	"io"
 	"strings"
 
-	"github.com/google/blueprint"
-
 	"android/soong/android"
 	"android/soong/cc/config"
 )
@@ -493,7 +491,7 @@
 func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
 	return func(mctx android.TopDownMutatorContext) {
 		if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
-			mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
+			mctx.VisitDepsDepthFirst(func(module android.Module) {
 				if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
 					!c.sanitize.Properties.Sanitize.Never {
 					d.sanitize.Properties.SanitizeDep = true
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 9fcc403..03e10ba 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -157,7 +157,7 @@
 	tools := map[string]android.Path{}
 
 	if len(g.properties.Tools) > 0 {
-		ctx.VisitDirectDeps(func(module blueprint.Module) {
+		ctx.VisitDirectDeps(func(module android.Module) {
 			switch ctx.OtherModuleDependencyTag(module) {
 			case android.SourceDepTag:
 				// Nothing to do
diff --git a/java/app.go b/java/app.go
index 42ae236..e8028a0 100644
--- a/java/app.go
+++ b/java/app.go
@@ -20,7 +20,6 @@
 	"path/filepath"
 	"strings"
 
-	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
@@ -231,7 +230,7 @@
 	aaptFlags = append(aaptFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
 	aaptFlags = append(aaptFlags, android.JoinWithPrefix(resourceDirs.Strings(), "-S "))
 
-	ctx.VisitDirectDeps(func(module blueprint.Module) {
+	ctx.VisitDirectDeps(func(module android.Module) {
 		var depFiles android.Paths
 		if javaDep, ok := module.(Dependency); ok {
 			if ctx.OtherModuleName(module) == "framework-res" {
diff --git a/java/java.go b/java/java.go
index c12ada2..26514f3 100644
--- a/java/java.go
+++ b/java/java.go
@@ -406,25 +406,10 @@
 		deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
 	}
 
-	ctx.VisitDirectDeps(func(module blueprint.Module) {
+	ctx.VisitDirectDeps(func(module android.Module) {
 		otherName := ctx.OtherModuleName(module)
 		tag := ctx.OtherModuleDependencyTag(module)
 
-		aDep, _ := module.(android.Module)
-		if aDep == nil {
-			ctx.ModuleErrorf("module %q not an android module", ctx.OtherModuleName(aDep))
-			return
-		}
-
-		if !aDep.Enabled() {
-			if ctx.AConfig().AllowMissingDependencies() {
-				ctx.AddMissingDependencies([]string{ctx.OtherModuleName(aDep)})
-			} else {
-				ctx.ModuleErrorf("depends on disabled module %q", ctx.OtherModuleName(aDep))
-			}
-			return
-		}
-
 		dep, _ := module.(Dependency)
 		if dep == nil {
 			switch tag {
diff --git a/java/system_modules.go b/java/system_modules.go
index f6f572b..a459b7a 100644
--- a/java/system_modules.go
+++ b/java/system_modules.go
@@ -112,7 +112,7 @@
 func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	var jars android.Paths
 
-	ctx.VisitDirectDeps(func(module blueprint.Module) {
+	ctx.VisitDirectDeps(func(module android.Module) {
 		if ctx.OtherModuleDependencyTag(module) == libTag {
 			dep, _ := module.(Dependency)
 			jars = append(jars, dep.HeaderJars()...)
diff --git a/python/binary.go b/python/binary.go
index b7b5056..c2e38bf 100644
--- a/python/binary.go
+++ b/python/binary.go
@@ -21,8 +21,6 @@
 	"path/filepath"
 	"strings"
 
-	"github.com/google/blueprint"
-
 	"android/soong/android"
 )
 
@@ -135,7 +133,7 @@
 
 	var launcher_path android.Path
 	if embedded_launcher {
-		ctx.VisitDirectDeps(func(m blueprint.Module) {
+		ctx.VisitDirectDeps(func(m android.Module) {
 			if ctx.OtherModuleDependencyTag(m) != launcherTag {
 				return
 			}
diff --git a/python/python.go b/python/python.go
index e63c26c..1b146a8 100644
--- a/python/python.go
+++ b/python/python.go
@@ -508,7 +508,7 @@
 	}
 
 	// visit all its dependencies in depth first.
-	ctx.VisitDepsDepthFirst(func(module blueprint.Module) {
+	ctx.VisitDepsDepthFirst(func(module android.Module) {
 		if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
 			return
 		}