Skip building compat files if REL

Bug: 296780580
Test: build with next
Change-Id: I588d249f35fc7049d0db3b64692ed818050af0ed
diff --git a/build/soong/cil_compat_map.go b/build/soong/cil_compat_map.go
index c9daf7c..eb7cb06 100644
--- a/build/soong/cil_compat_map.go
+++ b/build/soong/cil_compat_map.go
@@ -20,7 +20,6 @@
 import (
 	"android/soong/android"
 	"fmt"
-	"io"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -67,18 +66,21 @@
 	Bottom_half []string `android:"path"`
 	// name of the output
 	Stem *string
+	// Target version that this module supports. This module will be ignored if platform sepolicy
+	// version is same as this module's version.
+	Version *string
 }
 
 type cilCompatMap struct {
 	android.ModuleBase
 	properties cilCompatMapProperties
 	// (.intermediate) module output path as installation source.
-	installSource android.Path
+	installSource android.OptionalPath
 	installPath   android.InstallPath
 }
 
 type CilCompatMapGenerator interface {
-	GeneratedMapFile() android.Path
+	GeneratedMapFile() android.OptionalPath
 }
 
 func expandTopHalf(ctx android.ModuleContext) android.OptionalPath {
@@ -87,7 +89,7 @@
 		depTag := ctx.OtherModuleDependencyTag(dep)
 		switch depTag {
 		case TopHalfDepTag:
-			topHalf = android.OptionalPathForPath(dep.(CilCompatMapGenerator).GeneratedMapFile())
+			topHalf = dep.(CilCompatMapGenerator).GeneratedMapFile()
 		}
 	})
 	return topHalf
@@ -97,7 +99,15 @@
 	return android.PathsForModuleSrc(ctx, srcFiles)
 }
 
+func (c *cilCompatMap) shouldSkipBuild(ctx android.ModuleContext) bool {
+	return proptools.String(c.properties.Version) == ctx.DeviceConfig().PlatformSepolicyVersion()
+}
+
 func (c *cilCompatMap) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	if c.shouldSkipBuild(ctx) {
+		return
+	}
+
 	c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux", "mapping")
 
 	srcFiles := expandSeSources(ctx, c.properties.Bottom_half)
@@ -130,9 +140,9 @@
 				"bottomHalf": bottomHalf.String(),
 			},
 		})
-		c.installSource = out
+		c.installSource = android.OptionalPathForPath(out)
 	} else {
-		c.installSource = bottomHalf
+		c.installSource = android.OptionalPathForPath(bottomHalf)
 	}
 }
 
@@ -142,30 +152,38 @@
 	}
 }
 
-func (c *cilCompatMap) AndroidMk() android.AndroidMkData {
-	ret := android.AndroidMkData{
-		OutputFile: android.OptionalPathForPath(c.installSource),
-		Class:      "ETC",
+func (c *cilCompatMap) AndroidMkEntries() []android.AndroidMkEntries {
+	if !c.installSource.Valid() {
+		return nil
 	}
-	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
-		fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", c.installPath.String())
-		if c.properties.Stem != nil {
-			fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", String(c.properties.Stem))
-		}
-	})
-	return ret
+	return []android.AndroidMkEntries{android.AndroidMkEntries{
+		Class:      "ETC",
+		OutputFile: c.installSource,
+		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
+			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
+				entries.SetPath("LOCAL_MODULE_PATH", c.installPath)
+				if c.properties.Stem != nil {
+					entries.SetString("LOCAL_INSTALLED_MODULE_STEM", String(c.properties.Stem))
+				}
+			},
+		},
+	}}
 }
 
 var _ CilCompatMapGenerator = (*cilCompatMap)(nil)
 var _ android.OutputFileProducer = (*cilCompatMap)(nil)
 
-func (c *cilCompatMap) GeneratedMapFile() android.Path {
+func (c *cilCompatMap) GeneratedMapFile() android.OptionalPath {
 	return c.installSource
 }
 
 func (c *cilCompatMap) OutputFiles(tag string) (android.Paths, error) {
 	if tag == "" {
-		return android.Paths{c.installSource}, nil
+		if c.installSource.Valid() {
+			return android.Paths{c.installSource.Path()}, nil
+		} else {
+			return nil, nil
+		}
 	}
 	return nil, fmt.Errorf("Unknown tag %q", tag)
 }
diff --git a/build/soong/compat_cil.go b/build/soong/compat_cil.go
index 881f7da..1f7901b 100644
--- a/build/soong/compat_cil.go
+++ b/build/soong/compat_cil.go
@@ -43,7 +43,7 @@
 type compatCil struct {
 	android.ModuleBase
 	properties    compatCilProperties
-	installSource android.Path
+	installSource android.OptionalPath
 	installPath   android.InstallPath
 }
 
@@ -53,6 +53,10 @@
 
 	// Output file name. Defaults to module name if unspecified.
 	Stem *string
+
+	// Target version that this module supports. This module will be ignored if platform sepolicy
+	// version is same as this module's version.
+	Version *string
 }
 
 func (c *compatCil) stem() string {
@@ -63,11 +67,19 @@
 	return android.PathsForModuleSrc(ctx, c.properties.Srcs)
 }
 
+func (c *compatCil) shouldSkipBuild(ctx android.ModuleContext) bool {
+	return proptools.String(c.properties.Version) == ctx.DeviceConfig().PlatformSepolicyVersion()
+}
+
 func (c *compatCil) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	if c.ProductSpecific() || c.SocSpecific() || c.DeviceSpecific() {
 		ctx.ModuleErrorf("Compat cil files only support system and system_ext partitions")
 	}
 
+	if c.shouldSkipBuild(ctx) {
+		return
+	}
+
 	srcPaths := c.expandSeSources(ctx)
 	out := android.PathForModuleGen(ctx, c.Name())
 	ctx.Build(pctx, android.BuildParams{
@@ -78,14 +90,17 @@
 	})
 
 	c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux", "mapping")
-	c.installSource = out
-	ctx.InstallFile(c.installPath, c.stem(), c.installSource)
+	c.installSource = android.OptionalPathForPath(out)
+	ctx.InstallFile(c.installPath, c.stem(), out)
 }
 
 func (c *compatCil) AndroidMkEntries() []android.AndroidMkEntries {
+	if !c.installSource.Valid() {
+		return nil
+	}
 	return []android.AndroidMkEntries{android.AndroidMkEntries{
 		Class:      "ETC",
-		OutputFile: android.OptionalPathForPath(c.installSource),
+		OutputFile: c.installSource,
 		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
 			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
 				entries.SetPath("LOCAL_MODULE_PATH", c.installPath)
@@ -98,7 +113,11 @@
 func (c *compatCil) OutputFiles(tag string) (android.Paths, error) {
 	switch tag {
 	case "":
-		return android.Paths{c.installSource}, nil
+		if c.installSource.Valid() {
+			return android.Paths{c.installSource.Path()}, nil
+		} else {
+			return nil, nil
+		}
 	default:
 		return nil, fmt.Errorf("unsupported module reference tag %q", tag)
 	}
diff --git a/compat/Android.bp b/compat/Android.bp
index 39da7fd..9768eb1 100644
--- a/compat/Android.bp
+++ b/compat/Android.bp
@@ -133,6 +133,7 @@
     stem: "29.0.cil",
     bottom_half: [":29.0.board.compat.map{.plat_private}"],
     top_half: "plat_30.0.cil",
+    version: "29.0",
 }
 
 se_cil_compat_map {
@@ -140,6 +141,7 @@
     stem: "30.0.cil",
     bottom_half: [":30.0.board.compat.map{.plat_private}"],
     top_half: "plat_31.0.cil",
+    version: "30.0",
 }
 
 se_cil_compat_map {
@@ -147,6 +149,7 @@
     stem: "31.0.cil",
     bottom_half: [":31.0.board.compat.map{.plat_private}"],
     top_half: "plat_32.0.cil",
+    version: "31.0",
 }
 
 se_cil_compat_map {
@@ -154,6 +157,7 @@
     stem: "32.0.cil",
     bottom_half: [":32.0.board.compat.map{.plat_private}"],
     top_half: "plat_33.0.cil",
+    version: "32.0",
 }
 
 se_cil_compat_map {
@@ -161,6 +165,7 @@
     stem: "33.0.cil",
     bottom_half: [":33.0.board.compat.map{.plat_private}"],
     top_half: "plat_34.0.cil",
+    version: "33.0",
 }
 
 se_cil_compat_map {
@@ -169,6 +174,7 @@
     bottom_half: [":29.0.board.compat.map{.system_ext_private}"],
     top_half: "system_ext_30.0.cil",
     system_ext_specific: true,
+    version: "29.0",
 }
 
 se_cil_compat_map {
@@ -177,6 +183,7 @@
     bottom_half: [":30.0.board.compat.map{.system_ext_private}"],
     top_half: "system_ext_31.0.cil",
     system_ext_specific: true,
+    version: "30.0",
 }
 
 se_cil_compat_map {
@@ -185,6 +192,7 @@
     bottom_half: [":31.0.board.compat.map{.system_ext_private}"],
     top_half: "system_ext_32.0.cil",
     system_ext_specific: true,
+    version: "31.0",
 }
 
 se_cil_compat_map {
@@ -193,6 +201,7 @@
     bottom_half: [":32.0.board.compat.map{.system_ext_private}"],
     top_half: "system_ext_33.0.cil",
     system_ext_specific: true,
+    version: "32.0",
 }
 
 se_cil_compat_map {
@@ -201,6 +210,7 @@
     bottom_half: [":33.0.board.compat.map{.system_ext_private}"],
     system_ext_specific: true,
     top_half: "system_ext_34.0.cil",
+    version: "33.0",
 }
 
 se_cil_compat_map {
@@ -209,6 +219,7 @@
     bottom_half: [":29.0.board.compat.map{.product_private}"],
     top_half: "product_30.0.cil",
     product_specific: true,
+    version: "29.0",
 }
 
 se_cil_compat_map {
@@ -217,6 +228,7 @@
     bottom_half: [":30.0.board.compat.map{.product_private}"],
     top_half: "product_31.0.cil",
     product_specific: true,
+    version: "30.0",
 }
 
 se_cil_compat_map {
@@ -225,6 +237,7 @@
     bottom_half: [":31.0.board.compat.map{.product_private}"],
     top_half: "product_32.0.cil",
     product_specific: true,
+    version: "31.0",
 }
 
 se_cil_compat_map {
@@ -233,6 +246,7 @@
     bottom_half: [":32.0.board.compat.map{.product_private}"],
     top_half: "product_33.0.cil",
     product_specific: true,
+    version: "32.0",
 }
 
 se_cil_compat_map {
@@ -241,36 +255,42 @@
     bottom_half: [":33.0.board.compat.map{.product_private}"],
     product_specific: true,
     top_half: "product_34.0.cil",
+    version: "33.0",
 }
 
 se_cil_compat_map {
     name: "29.0.ignore.cil",
     bottom_half: [":29.0.board.ignore.map{.plat_private}"],
     top_half: "30.0.ignore.cil",
+    version: "29.0",
 }
 
 se_cil_compat_map {
     name: "30.0.ignore.cil",
     bottom_half: [":30.0.board.ignore.map{.plat_private}"],
     top_half: "31.0.ignore.cil",
+    version: "30.0",
 }
 
 se_cil_compat_map {
     name: "31.0.ignore.cil",
     bottom_half: [":31.0.board.ignore.map{.plat_private}"],
     top_half: "32.0.ignore.cil",
+    version: "31.0",
 }
 
 se_cil_compat_map {
     name: "32.0.ignore.cil",
     bottom_half: [":32.0.board.ignore.map{.plat_private}"],
     top_half: "33.0.ignore.cil",
+    version: "32.0",
 }
 
 se_cil_compat_map {
     name: "33.0.ignore.cil",
     bottom_half: [":33.0.board.ignore.map{.plat_private}"],
     top_half: "34.0.ignore.cil",
+    version: "33.0",
 }
 
 se_cil_compat_map {
@@ -278,6 +298,7 @@
     bottom_half: [":30.0.board.ignore.map{.system_ext_private}"],
     top_half: "system_ext_31.0.ignore.cil",
     system_ext_specific: true,
+    version: "30.0",
 }
 
 se_cil_compat_map {
@@ -285,6 +306,7 @@
     bottom_half: [":31.0.board.ignore.map{.system_ext_private}"],
     top_half: "system_ext_32.0.ignore.cil",
     system_ext_specific: true,
+    version: "31.0",
 }
 
 se_cil_compat_map {
@@ -292,6 +314,7 @@
     bottom_half: [":32.0.board.ignore.map{.system_ext_private}"],
     top_half: "system_ext_33.0.ignore.cil",
     system_ext_specific: true,
+    version: "32.0",
 }
 
 se_cil_compat_map {
@@ -299,6 +322,7 @@
     bottom_half: [":33.0.board.ignore.map{.system_ext_private}"],
     system_ext_specific: true,
     top_half: "system_ext_34.0.ignore.cil",
+    version: "33.0",
 }
 
 se_cil_compat_map {
@@ -306,6 +330,7 @@
     bottom_half: [":30.0.board.ignore.map{.product_private}"],
     top_half: "product_31.0.ignore.cil",
     product_specific: true,
+    version: "30.0",
 }
 
 se_cil_compat_map {
@@ -313,6 +338,7 @@
     bottom_half: [":31.0.board.ignore.map{.product_private}"],
     top_half: "product_32.0.ignore.cil",
     product_specific: true,
+    version: "31.0",
 }
 
 se_cil_compat_map {
@@ -320,6 +346,7 @@
     bottom_half: [":32.0.board.ignore.map{.product_private}"],
     top_half: "product_33.0.ignore.cil",
     product_specific: true,
+    version: "32.0",
 }
 
 se_cil_compat_map {
@@ -327,31 +354,37 @@
     bottom_half: [":33.0.board.ignore.map{.product_private}"],
     product_specific: true,
     top_half: "product_34.0.ignore.cil",
+    version: "33.0",
 }
 
 se_compat_cil {
     name: "29.0.compat.cil",
     srcs: [":29.0.board.compat.cil{.plat_private}"],
+    version: "29.0",
 }
 
 se_compat_cil {
     name: "30.0.compat.cil",
     srcs: [":30.0.board.compat.cil{.plat_private}"],
+    version: "30.0",
 }
 
 se_compat_cil {
     name: "31.0.compat.cil",
     srcs: [":31.0.board.compat.cil{.plat_private}"],
+    version: "31.0",
 }
 
 se_compat_cil {
     name: "32.0.compat.cil",
     srcs: [":32.0.board.compat.cil{.plat_private}"],
+    version: "32.0",
 }
 
 se_compat_cil {
     name: "33.0.compat.cil",
     srcs: [":33.0.board.compat.cil{.plat_private}"],
+    version: "33.0",
 }
 
 se_compat_cil {
@@ -359,6 +392,7 @@
     srcs: [":29.0.board.compat.cil{.system_ext_private}"],
     stem: "29.0.compat.cil",
     system_ext_specific: true,
+    version: "29.0",
 }
 
 se_compat_cil {
@@ -366,6 +400,7 @@
     srcs: [":30.0.board.compat.cil{.system_ext_private}"],
     stem: "30.0.compat.cil",
     system_ext_specific: true,
+    version: "30.0",
 }
 
 se_compat_cil {
@@ -373,6 +408,7 @@
     srcs: [":31.0.board.compat.cil{.system_ext_private}"],
     stem: "31.0.compat.cil",
     system_ext_specific: true,
+    version: "31.0",
 }
 
 se_compat_cil {
@@ -380,6 +416,7 @@
     srcs: [":32.0.board.compat.cil{.system_ext_private}"],
     stem: "32.0.compat.cil",
     system_ext_specific: true,
+    version: "32.0",
 }
 
 se_compat_cil {
@@ -387,6 +424,7 @@
     srcs: [":33.0.board.compat.cil{.system_ext_private}"],
     stem: "33.0.compat.cil",
     system_ext_specific: true,
+    version: "33.0",
 }
 
 se_compat_test {
@@ -412,6 +450,7 @@
     name: "plat_34.0.cil",
     stem: "34.0.cil",
     bottom_half: [":34.0.board.compat.map{.plat_private}"],
+    version: "34.0",
 }
 
 se_cil_compat_map {
@@ -419,6 +458,7 @@
     stem: "34.0.cil",
     bottom_half: [":34.0.board.compat.map{.system_ext_private}"],
     system_ext_specific: true,
+    version: "34.0",
 }
 
 se_cil_compat_map {
@@ -426,11 +466,13 @@
     stem: "34.0.cil",
     bottom_half: [":34.0.board.compat.map{.product_private}"],
     product_specific: true,
+    version: "34.0",
 }
 
 se_cil_compat_map {
     name: "34.0.ignore.cil",
     bottom_half: [":34.0.board.ignore.map{.plat_private}"],
+    version: "34.0",
 }
 
 se_cil_compat_map {
@@ -438,6 +480,7 @@
     stem: "34.0.ignore.cil",
     bottom_half: [":34.0.board.ignore.map{.system_ext_private}"],
     system_ext_specific: true,
+    version: "34.0",
 }
 
 se_cil_compat_map {
@@ -445,11 +488,13 @@
     stem: "34.0.ignore.cil",
     bottom_half: [":34.0.board.ignore.map{.product_private}"],
     product_specific: true,
+    version: "34.0",
 }
 
 se_compat_cil {
     name: "34.0.compat.cil",
     srcs: [":34.0.board.compat.cil{.plat_private}"],
+    version: "34.0",
 }
 
 se_compat_cil {
@@ -457,4 +502,5 @@
     stem: "34.0.compat.cil",
     srcs: [":34.0.board.compat.cil{.system_ext_private}"],
     system_ext_specific: true,
+    version: "34.0",
 }
diff --git a/tools/sepolicy_generate_compat.py b/tools/sepolicy_generate_compat.py
index cd61c9a..a941d6f 100644
--- a/tools/sepolicy_generate_compat.py
+++ b/tools/sepolicy_generate_compat.py
@@ -223,6 +223,7 @@
     name: "plat_{ver}.cil",
     stem: "{ver}.cil",
     bottom_half: [":{ver}.board.compat.map{{.plat_private}}"],
+    version: "{ver}",
 }}
 
 se_cil_compat_map {{
@@ -230,6 +231,7 @@
     stem: "{ver}.cil",
     bottom_half: [":{ver}.board.compat.map{{.system_ext_private}}"],
     system_ext_specific: true,
+    version: "{ver}",
 }}
 
 se_cil_compat_map {{
@@ -237,11 +239,13 @@
     stem: "{ver}.cil",
     bottom_half: [":{ver}.board.compat.map{{.product_private}}"],
     product_specific: true,
+    version: "{ver}",
 }}
 
 se_cil_compat_map {{
     name: "{ver}.ignore.cil",
     bottom_half: [":{ver}.board.ignore.map{{.plat_private}}"],
+    version: "{ver}",
 }}
 
 se_cil_compat_map {{
@@ -249,6 +253,7 @@
     stem: "{ver}.ignore.cil",
     bottom_half: [":{ver}.board.ignore.map{{.system_ext_private}}"],
     system_ext_specific: true,
+    version: "{ver}",
 }}
 
 se_cil_compat_map {{
@@ -256,11 +261,13 @@
     stem: "{ver}.ignore.cil",
     bottom_half: [":{ver}.board.ignore.map{{.product_private}}"],
     product_specific: true,
+    version: "{ver}",
 }}
 
 se_compat_cil {{
     name: "{ver}.compat.cil",
     srcs: [":{ver}.board.compat.cil{{.plat_private}}"],
+    version: "{ver}",
 }}
 
 se_compat_cil {{
@@ -268,6 +275,7 @@
     stem: "{ver}.compat.cil",
     srcs: [":{ver}.board.compat.cil{{.system_ext_private}}"],
     system_ext_specific: true,
+    version: "{ver}",
 }}
 """