Add isFlattenedVariant function

The return value is true if TARGET_FLATTEN_APEX is true,
TARGET_BUILD_APPS is false and the module is flatten variant.
Only the module name of flattened variant is appended with
".flattened" as suffix.

Test: m -j
Bug: 139716748
Change-Id: I966a178b43c5d08bd69d19e97e59456aa66d9d28
diff --git a/apex/apex.go b/apex/apex.go
index 64df6cf..6024deb 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -289,11 +289,14 @@
 }
 
 func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
-	if _, ok := mctx.Module().(*apexBundle); ok {
+	if ab, ok := mctx.Module().(*apexBundle); ok {
 		if !mctx.Config().FlattenApex() || mctx.Config().UnbundledBuild() {
 			modules := mctx.CreateLocalVariations("", "flattened")
 			modules[0].(*apexBundle).SetFlattened(false)
 			modules[1].(*apexBundle).SetFlattened(true)
+		} else {
+			ab.SetFlattened(true)
+			ab.SetFlattenedConfigValue()
 		}
 	}
 }
@@ -409,10 +412,14 @@
 	// List of APKs to package inside APEX
 	Apps []string
 
-	// To distinguish between flattened and non-flattened variants.
-	// if set true, then this variant is flattened variant.
+	// To distinguish between flattened and non-flattened apex.
+	// if set true, then output files are flattened.
 	Flattened bool `blueprint:"mutated"`
 
+	// if true, it means that TARGET_FLATTEN_APEX is true and
+	// TARGET_BUILD_APPS is false
+	FlattenedConfigValue bool `blueprint:"mutated"`
+
 	// List of SDKs that are used to build this APEX. A reference to an SDK should be either
 	// `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
 	// is implied. This value affects all modules included in this APEX. In other words, they are
@@ -576,9 +583,6 @@
 
 	// intermediate path for apex_manifest.json
 	manifestOut android.WritablePath
-
-	// A config value of (TARGET_FLATTEN_APEX && !TARGET_BUILD_APPS)
-	flattenedConfigValue bool
 }
 
 func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
@@ -839,6 +843,20 @@
 	a.properties.Flattened = flattened
 }
 
+func (a *apexBundle) SetFlattenedConfigValue() {
+	a.properties.FlattenedConfigValue = true
+}
+
+// isFlattenedVariant returns true when the current module is the flattened
+// variant of an apex that has both a flattened and an unflattened variant.
+// It returns false when the current module is flattened but there is no
+// unflattened variant, which occurs when ctx.Config().FlattenedApex() returns
+// true. It can be used to avoid collisions between the install paths of the
+// flattened and unflattened variants.
+func (a *apexBundle) isFlattenedVariant() bool {
+	return a.properties.Flattened && !a.properties.FlattenedConfigValue
+}
+
 func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
 	// Decide the APEX-local directory by the multilib of the library
 	// In the future, we may query this to the module.
@@ -1160,10 +1178,6 @@
 		return false
 	})
 
-	a.flattenedConfigValue = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
-	if a.flattenedConfigValue {
-		a.properties.Flattened = true
-	}
 	if a.private_key_file == nil {
 		ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
 		return
@@ -1502,7 +1516,7 @@
 	})
 
 	// Install to $OUT/soong/{target,host}/.../apex
-	if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && (!a.properties.Flattened || a.flattenedConfigValue) {
+	if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && !a.isFlattenedVariant() {
 		ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
 	}
 }
@@ -1567,7 +1581,7 @@
 		}
 
 		var suffix string
-		if a.properties.Flattened && !a.flattenedConfigValue {
+		if a.isFlattenedVariant() {
 			suffix = ".flattened"
 		}
 
@@ -1585,7 +1599,7 @@
 			// /system/apex/<name>/{lib|framework|...}
 			fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
 				a.installDir.RelPathString(), name, fi.installDir))
-			if a.flattenedConfigValue {
+			if !a.isFlattenedVariant() {
 				fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
 			}
 			if len(fi.symlinks) > 0 {
@@ -1667,7 +1681,7 @@
 				moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
 			}
 
-			if a.properties.Flattened && !a.flattenedConfigValue {
+			if a.isFlattenedVariant() {
 				name = name + ".flattened"
 			}
 
@@ -1682,7 +1696,7 @@
 				fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
 				fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
 
-			} else if !a.properties.Flattened || a.flattenedConfigValue {
+			} else if !a.isFlattenedVariant() {
 				// zip-apex is the less common type so have the name refer to the image-apex
 				// only and use {name}.zip if you want the zip-apex
 				if apexType == zipApex && a.apexTypes == both {