Build recovery policy with Android.bp

Bug: 33691272
Test: enter recovery mode
Change-Id: Ifc38ed99e6615431d81ade76ec10ea4d34fbbf90
diff --git a/build/soong/policy.go b/build/soong/policy.go
index 3308e2c..2b190e6 100644
--- a/build/soong/policy.go
+++ b/build/soong/policy.go
@@ -83,6 +83,9 @@
 	// Whether to build CTS specific policy or not. Default is false
 	Cts *bool
 
+	// Whether to build recovery specific policy or not. Default is false
+	Target_recovery *bool
+
 	// Whether this module is directly installable to one of the partitions. Default is true
 	Installable *bool
 }
@@ -130,6 +133,10 @@
 	return proptools.Bool(c.properties.Cts)
 }
 
+func (c *policyConf) isTargetRecovery() bool {
+	return proptools.Bool(c.properties.Target_recovery)
+}
+
 func (c *policyConf) withAsan(ctx android.ModuleContext) string {
 	isAsanDevice := android.InList("address", ctx.Config().SanitizeDevice())
 	return strconv.FormatBool(proptools.BoolDefault(c.properties.With_asan, isAsanDevice))
@@ -139,6 +146,9 @@
 	if c.cts() {
 		return "cts"
 	}
+	if c.isTargetRecovery() {
+		return "false"
+	}
 	return strconv.FormatBool(ctx.DeviceConfig().SepolicySplit())
 }
 
@@ -146,6 +156,9 @@
 	if c.cts() {
 		return "cts"
 	}
+	if c.isTargetRecovery() {
+		return "false"
+	}
 	return "true"
 }
 
@@ -153,6 +166,9 @@
 	if c.cts() {
 		return "cts"
 	}
+	if c.isTargetRecovery() {
+		return "false"
+	}
 	return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenTrebleSyspropNeverallow())
 }
 
@@ -160,6 +176,9 @@
 	if c.cts() {
 		return "cts"
 	}
+	if c.isTargetRecovery() {
+		return "false"
+	}
 	return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenEnforceSyspropOwner())
 }
 
@@ -206,6 +225,7 @@
 		FlagWithArg("-D target_exclude_build_test=", strconv.FormatBool(proptools.Bool(c.properties.Exclude_build_test))).
 		FlagWithArg("-D target_requires_insecure_execmem_for_swiftshader=", strconv.FormatBool(ctx.DeviceConfig().RequiresInsecureExecmemForSwiftshader())).
 		FlagWithArg("-D target_enforce_debugfs_restriction=", c.enforceDebugfsRestrictions(ctx)).
+		FlagWithArg("-D target_recovery=", strconv.FormatBool(c.isTargetRecovery())).
 		Flag("-s").
 		Inputs(srcs).
 		Text("> ").Output(conf)
@@ -439,6 +459,10 @@
 	return c
 }
 
+func (c *policyBinary) InstallInRoot() bool {
+	return c.InstallInRecovery()
+}
+
 func (c *policyBinary) Installable() bool {
 	return proptools.BoolDefault(c.properties.Installable, true)
 }
@@ -505,7 +529,12 @@
 		c.SkipInstall()
 	}
 
-	c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
+	if c.InstallInRecovery() {
+		// install in root
+		c.installPath = android.PathForModuleInstall(ctx)
+	} else {
+		c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
+	}
 	c.installSource = out
 	ctx.InstallFile(c.installPath, c.stem(), c.installSource)
 }