Don't create unnecessary APEX variations

This change fixes a problem that APEX variations are created for the
modules that actually shouldn't built for any APEX. For example,
consider this case.

apex { name: "myapex", native_shared_libs: ["mylib"],}

cc_library { name: "mylib", shared_libs: ["libfoo#10"],}
cc_library { name: "libfoo",
             shared_libs: ["libbar"],
             stubs: { versions: ["10"], }, }
cc_library { name: "libbar", ...}

Before this change, both the stubs and non-stubs variations of libfoo
were mutated with apexMuator, which is incorrect for the non-stubs
varia; there is no dependency chain from the apex "myapex" to the
non-stubs variation, but to the stubs variation due to the #10 syntax.

This was happening becauses we used the name of the module to determine
whether it should be built for APEX or not. Both stubs and non-stubs
variations have the same module name "libfoo".

Fixing this issue by recording the list of APEX variations required
directly on the module. So, the stubs variation of libfoo has myapex in
its apex variations list, but the non-stubs variation doesn't, and thus
apexMutator does not pick up the non-stubs variation.

Test: m (apex_test updated and passing)
Test: cherry-pick ag/5747464 and m
Change-Id: I31e618626809a828a55fff513ef5f81f79637afa
diff --git a/apex/apex.go b/apex/apex.go
index f412d86..46207b2 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -150,11 +150,13 @@
 	if _, ok := mctx.Module().(*apexBundle); ok {
 		apexBundleName := mctx.ModuleName()
 		mctx.WalkDeps(func(child, parent android.Module) bool {
+			depName := mctx.OtherModuleName(child)
+			// If the parent is apexBundle, this child is directly depended.
+			_, directDep := parent.(*apexBundle)
+			android.UpdateApexDependency(apexBundleName, depName, directDep)
+
 			if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
-				moduleName := mctx.OtherModuleName(am) + "-" + am.Target().String()
-				// If the parent is apexBundle, this child is directly depended.
-				_, directDep := parent.(*apexBundle)
-				android.BuildModuleForApexBundle(mctx, moduleName, apexBundleName, directDep)
+				am.BuildForApex(apexBundleName)
 				return true
 			} else {
 				return false
@@ -166,21 +168,7 @@
 // Create apex variations if a module is included in APEX(s).
 func apexMutator(mctx android.BottomUpMutatorContext) {
 	if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
-		moduleName := mctx.ModuleName() + "-" + am.Target().String()
-		bundleNames := android.GetApexBundlesForModule(mctx, moduleName)
-		if len(bundleNames) > 0 {
-			variations := []string{"platform"}
-			for bn := range bundleNames {
-				variations = append(variations, bn)
-			}
-			modules := mctx.CreateVariations(variations...)
-			for i, m := range modules {
-				if i == 0 {
-					continue // platform
-				}
-				m.(android.ApexModule).BuildForApex(variations[i])
-			}
-		}
+		am.CreateApexVariations(mctx)
 	} else if _, ok := mctx.Module().(*apexBundle); ok {
 		// apex bundle itself is mutated so that it and its modules have same
 		// apex variant.