Enforce dependencies have right architecture

ctx.AddDependency will succeed if the named dependency only has a
single variant, even if that variant is the wrong architecture.
Use ctx.AddVariationDependency(nil, ...) instead, which requires
that all variations of the calling module match the dependency.

Bug: 112707915
Test: no change to out/soong/build.ninja
Test: using a device dependency in a host java module is an error
Change-Id: I70b661a57d4412eb63b8c9841febfb756e9e025d
diff --git a/java/aar.go b/java/aar.go
index 175e794..29f5597 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -156,7 +156,7 @@
 	if !ctx.Config().UnbundledBuild() {
 		sdkDep := decodeSdkDep(ctx, sdkContext)
 		if sdkDep.frameworkResModule != "" {
-			ctx.AddDependency(ctx.Module(), frameworkResTag, sdkDep.frameworkResModule)
+			ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
 		}
 	}
 }
@@ -436,12 +436,12 @@
 	if !ctx.Config().UnbundledBuild() {
 		sdkDep := decodeSdkDep(ctx, sdkContext(a))
 		if sdkDep.useModule && sdkDep.frameworkResModule != "" {
-			ctx.AddDependency(ctx.Module(), frameworkResTag, sdkDep.frameworkResModule)
+			ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
 		}
 	}
 
-	ctx.AddDependency(ctx.Module(), libTag, a.properties.Libs...)
-	ctx.AddDependency(ctx.Module(), staticLibTag, a.properties.Static_libs...)
+	ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
+	ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
 }
 
 // Unzip an AAR into its constituent files and directories.  Any files in Outputs that don't exist in the AAR will be
diff --git a/java/droiddoc.go b/java/droiddoc.go
index e4c9e38..68d7861 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -371,24 +371,24 @@
 	if ctx.Device() {
 		sdkDep := decodeSdkDep(ctx, sdkContext(j))
 		if sdkDep.useDefaultLibs {
-			ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
+			ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
 			if ctx.Config().TargetOpenJDK9() {
-				ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
+				ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
 			}
 			if !Bool(j.properties.No_framework_libs) {
-				ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
+				ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
 			}
 		} else if sdkDep.useModule {
 			if ctx.Config().TargetOpenJDK9() {
-				ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
+				ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
 			}
-			ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
+			ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
 		}
 	}
 
-	ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
+	ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
 	if j.properties.Srcs_lib != nil {
-		ctx.AddDependency(ctx.Module(), srcsLibTag, *j.properties.Srcs_lib)
+		ctx.AddVariationDependencies(nil, srcsLibTag, *j.properties.Srcs_lib)
 	}
 
 	android.ExtractSourcesDeps(ctx, j.properties.Srcs)
diff --git a/java/java.go b/java/java.go
index b911078..f6c9484 100644
--- a/java/java.go
+++ b/java/java.go
@@ -552,41 +552,41 @@
 		if !Bool(j.properties.No_standard_libs) {
 			sdkDep := decodeSdkDep(ctx, sdkContext(j))
 			if sdkDep.useDefaultLibs {
-				ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
+				ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
 				if ctx.Config().TargetOpenJDK9() {
-					ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
+					ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
 				}
 				if !Bool(j.properties.No_framework_libs) {
-					ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
+					ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
 				}
 			} else if sdkDep.useModule {
 				if ctx.Config().TargetOpenJDK9() {
-					ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
+					ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
 				}
-				ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
+				ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
 				if Bool(j.deviceProperties.Optimize.Enabled) {
-					ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
-					ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
+					ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
+					ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
 				}
 			}
 		} else if j.deviceProperties.System_modules == nil {
 			ctx.PropertyErrorf("no_standard_libs",
 				"system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
 		} else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
-			ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
+			ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
 		}
 		if ctx.ModuleName() == "framework" {
-			ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
+			ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
 		}
 		if ctx.ModuleName() == "android_stubs_current" ||
 			ctx.ModuleName() == "android_system_stubs_current" ||
 			ctx.ModuleName() == "android_test_stubs_current" {
-			ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
+			ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
 		}
 	}
 
-	ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
-	ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
+	ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
+	ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
 	ctx.AddFarVariationDependencies([]blueprint.Variation{
 		{Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
 	}, annoTag, j.properties.Annotation_processors...)
@@ -602,11 +602,11 @@
 	if j.hasSrcExt(".kt") {
 		// TODO(ccross): move this to a mutator pass that can tell if generated sources contain
 		// Kotlin files
-		ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
+		ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
 	}
 
 	if j.shouldInstrumentStatic(ctx) {
-		ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
+		ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
 	}
 }
 
@@ -1619,7 +1619,7 @@
 
 func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
 	android.ExtractSourcesDeps(ctx, j.properties.Jars)
-	ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
+	ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
 }
 
 func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
diff --git a/java/proto.go b/java/proto.go
index 58b039e..8028039 100644
--- a/java/proto.go
+++ b/java/proto.go
@@ -73,14 +73,14 @@
 func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
 	switch String(p.Proto.Type) {
 	case "micro":
-		ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro")
+		ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
 	case "nano":
-		ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano")
+		ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
 	case "lite", "":
-		ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite")
+		ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
 	case "full":
 		if ctx.Host() {
-			ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full")
+			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
 		} else {
 			ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
 		}
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 525652a..d588801 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -156,14 +156,14 @@
 
 func (module *sdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
 	// Add dependencies to the stubs library
-	ctx.AddDependency(ctx.Module(), publicApiStubsTag, module.stubsName(apiScopePublic))
-	ctx.AddDependency(ctx.Module(), systemApiStubsTag, module.stubsName(apiScopeSystem))
-	ctx.AddDependency(ctx.Module(), testApiStubsTag, module.stubsName(apiScopeTest))
-	ctx.AddDependency(ctx.Module(), implLibTag, module.implName())
+	ctx.AddVariationDependencies(nil, publicApiStubsTag, module.stubsName(apiScopePublic))
+	ctx.AddVariationDependencies(nil, systemApiStubsTag, module.stubsName(apiScopeSystem))
+	ctx.AddVariationDependencies(nil, testApiStubsTag, module.stubsName(apiScopeTest))
+	ctx.AddVariationDependencies(nil, implLibTag, module.implName())
 
-	ctx.AddDependency(ctx.Module(), publicApiFileTag, module.docsName(apiScopePublic))
-	ctx.AddDependency(ctx.Module(), systemApiFileTag, module.docsName(apiScopeSystem))
-	ctx.AddDependency(ctx.Module(), testApiFileTag, module.docsName(apiScopeTest))
+	ctx.AddVariationDependencies(nil, publicApiFileTag, module.docsName(apiScopePublic))
+	ctx.AddVariationDependencies(nil, systemApiFileTag, module.docsName(apiScopeSystem))
+	ctx.AddVariationDependencies(nil, testApiFileTag, module.docsName(apiScopeTest))
 }
 
 func (module *sdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
diff --git a/java/system_modules.go b/java/system_modules.go
index d98e0ab..9ee0307 100644
--- a/java/system_modules.go
+++ b/java/system_modules.go
@@ -123,7 +123,7 @@
 }
 
 func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
-	ctx.AddDependency(ctx.Module(), libTag, system.properties.Libs...)
+	ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
 }
 
 func (system *SystemModules) AndroidMk() android.AndroidMkData {