apex_set is force disabled when necessary

prebuilt_apex has been disabled when the device is configured for
flattened APEXes, sanitized, instrumented, or built unbundled. However,
apex_set which is another type of APEX prebuilt wasn't disabled for the
same conditions.

This change fixes the discripency. apex_set modules are also force
disabled when the prebuilts are not expected.

Bug: 161316762
Bug: 160933444
Test: OVERRIDE_TARGET_FLATTEN_APEX=true m
The built image has only the flattened APEXes and the device boots

Merged-In: I6c90dfb28d565861a473a1bdce93269ec370601d
(cherry picked from commit 10e926bc8d02ff37a631aeaea1784921e164df6f)
Change-Id: I6c90dfb28d565861a473a1bdce93269ec370601d
(cherry picked from commit f220db80b8fe790d23ddcd73792e23936cb5acd2)
diff --git a/apex/apex.go b/apex/apex.go
index f9c9b87..b4fae34 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -2112,7 +2112,7 @@
 			case android.PrebuiltDepTag:
 				// If the prebuilt is force disabled, remember to delete the prebuilt file
 				// that might have been installed in the previous builds
-				if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
+				if prebuilt, ok := child.(prebuilt); ok && prebuilt.isForceDisabled() {
 					a.prebuiltFileToDelete = prebuilt.InstallFilename()
 				}
 			}
diff --git a/apex/prebuilt.go b/apex/prebuilt.go
index 794799e..37457e9 100644
--- a/apex/prebuilt.go
+++ b/apex/prebuilt.go
@@ -40,9 +40,55 @@
 		"abis", "allow-prereleased", "sdk-version")
 )
 
+type prebuilt interface {
+	isForceDisabled() bool
+	InstallFilename() string
+}
+
+type prebuiltCommon struct {
+	prebuilt   android.Prebuilt
+	properties prebuiltCommonProperties
+}
+
+type prebuiltCommonProperties struct {
+	ForceDisable bool `blueprint:"mutated"`
+}
+
+func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
+	return &p.prebuilt
+}
+
+func (p *prebuiltCommon) isForceDisabled() bool {
+	return p.properties.ForceDisable
+}
+
+func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
+	// If the device is configured to use flattened APEX, force disable the prebuilt because
+	// the prebuilt is a non-flattened one.
+	forceDisable := ctx.Config().FlattenApex()
+
+	// Force disable the prebuilts when we are doing unbundled build. We do unbundled build
+	// to build the prebuilts themselves.
+	forceDisable = forceDisable || ctx.Config().UnbundledBuild()
+
+	// Force disable the prebuilts when coverage is enabled.
+	forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
+	forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
+
+	// b/137216042 don't use prebuilts when address sanitizer is on
+	forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
+		android.InList("hwaddress", ctx.Config().SanitizeDevice())
+
+	if forceDisable && p.prebuilt.SourceExists() {
+		p.properties.ForceDisable = true
+		return true
+	}
+	return false
+}
+
 type Prebuilt struct {
 	android.ModuleBase
-	prebuilt android.Prebuilt
+	prebuiltCommon
 
 	properties PrebuiltProperties
 
@@ -58,8 +104,7 @@
 
 type PrebuiltProperties struct {
 	// the path to the prebuilt .apex file to import.
-	Source       string `blueprint:"mutated"`
-	ForceDisable bool   `blueprint:"mutated"`
+	Source string `blueprint:"mutated"`
 
 	Src  *string
 	Arch struct {
@@ -94,10 +139,6 @@
 	return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
 }
 
-func (p *Prebuilt) isForceDisabled() bool {
-	return p.properties.ForceDisable
-}
-
 func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
 	switch tag {
 	case "":
@@ -111,12 +152,8 @@
 	return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
 }
 
-func (p *Prebuilt) Prebuilt() *android.Prebuilt {
-	return &p.prebuilt
-}
-
 func (p *Prebuilt) Name() string {
-	return p.prebuilt.Name(p.ModuleBase.Name())
+	return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
 }
 
 // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
@@ -129,27 +166,6 @@
 }
 
 func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
-	// If the device is configured to use flattened APEX, force disable the prebuilt because
-	// the prebuilt is a non-flattened one.
-	forceDisable := ctx.Config().FlattenApex()
-
-	// Force disable the prebuilts when we are doing unbundled build. We do unbundled build
-	// to build the prebuilts themselves.
-	forceDisable = forceDisable || ctx.Config().UnbundledBuild()
-
-	// Force disable the prebuilts when coverage is enabled.
-	forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
-	forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
-
-	// b/137216042 don't use prebuilts when address sanitizer is on
-	forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
-		android.InList("hwaddress", ctx.Config().SanitizeDevice())
-
-	if forceDisable && p.prebuilt.SourceExists() {
-		p.properties.ForceDisable = true
-		return
-	}
-
 	// This is called before prebuilt_select and prebuilt_postdeps mutators
 	// The mutators requires that src to be set correctly for each arch so that
 	// arch variants are disabled when src is not provided for the arch.
@@ -178,10 +194,6 @@
 }
 
 func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	if p.properties.ForceDisable {
-		return
-	}
-
 	// TODO(jungjw): Check the key validity.
 	p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
 	p.installDir = android.PathForModuleInstall(ctx, "apex")
@@ -195,6 +207,12 @@
 		Input:  p.inputApex,
 		Output: p.outputApex,
 	})
+
+	if p.prebuiltCommon.checkForceDisable(ctx) {
+		p.SkipInstall()
+		return
+	}
+
 	if p.installable() {
 		ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
 	}
@@ -228,7 +246,7 @@
 
 type ApexSet struct {
 	android.ModuleBase
-	prebuilt android.Prebuilt
+	prebuiltCommon
 
 	properties ApexSetProperties
 
@@ -274,12 +292,8 @@
 	return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
 }
 
-func (a *ApexSet) Prebuilt() *android.Prebuilt {
-	return &a.prebuilt
-}
-
 func (a *ApexSet) Name() string {
-	return a.prebuilt.Name(a.ModuleBase.Name())
+	return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
 }
 
 func (a *ApexSet) Overrides() []string {
@@ -301,7 +315,7 @@
 		ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
 	}
 
-	apexSet := a.prebuilt.SingleSourcePath(ctx)
+	apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
 	a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
 	ctx.Build(pctx,
 		android.BuildParams{
@@ -315,6 +329,12 @@
 				"sdk-version":       ctx.Config().PlatformSdkVersion(),
 			},
 		})
+
+	if a.prebuiltCommon.checkForceDisable(ctx) {
+		a.SkipInstall()
+		return
+	}
+
 	a.installDir = android.PathForModuleInstall(ctx, "apex")
 	if a.installable() {
 		ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)