From a5cfd3e5373aabf846c45cad8024c700db8e0880 Mon Sep 17 00:00:00 2001
From: Tri Vo <trong@google.com>
Date: Thu, 22 Mar 2018 11:35:02 -0700
Subject: [PATCH] Soong module selinux compat maps

And migrate 26.0.cil and 27.0.cil build targets from Android.mk to
Android.bp

Bug: 33691272
Test: 26.0.cil and 27.0.cil mapping files on the device are unchanged.
Change-Id: Id0ea45c149e096996bc0657615ea98915df3c9e1
---
 Android.bp                    | 27 +++++++++++
 Android.mk                    | 20 --------
 build/soong/Android.bp        | 25 ++++++++++
 build/soong/cil_compat_map.go | 87 +++++++++++++++++++++++++++++++++++
 4 files changed, 139 insertions(+), 20 deletions(-)
 create mode 100644 Android.bp
 create mode 100644 build/soong/Android.bp
 create mode 100644 build/soong/cil_compat_map.go

diff --git a/Android.bp b/Android.bp
new file mode 100644
index 000000000..8154f48f1
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,27 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+se_cil_compat_map {
+    name: "26.0.cil",
+    srcs: [
+        "private/compat/26.0/26.0.cil",
+    ],
+}
+
+se_cil_compat_map {
+    name: "27.0.cil",
+    srcs: [
+        "private/compat/27.0/27.0.cil",
+    ],
+}
diff --git a/Android.mk b/Android.mk
index e155177ac..ffdc5c453 100644
--- a/Android.mk
+++ b/Android.mk
@@ -476,26 +476,6 @@ current_mapping.cil :=
 #################################
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := 27.0.cil
-LOCAL_SRC_FILES := private/compat/27.0/27.0.cil
-LOCAL_MODULE_CLASS := ETC
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/selinux/mapping
-
-include $(BUILD_PREBUILT)
-#################################
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := 26.0.cil
-LOCAL_SRC_FILES := private/compat/26.0/26.0.cil
-LOCAL_MODULE_CLASS := ETC
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/selinux/mapping
-
-include $(BUILD_PREBUILT)
-#################################
-include $(CLEAR_VARS)
-
 LOCAL_MODULE := plat_and_mapping_sepolicy.cil.sha256
 LOCAL_MODULE_CLASS := ETC
 LOCAL_MODULE_TAGS := optional
diff --git a/build/soong/Android.bp b/build/soong/Android.bp
new file mode 100644
index 000000000..0f21be378
--- /dev/null
+++ b/build/soong/Android.bp
@@ -0,0 +1,25 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+bootstrap_go_package {
+    name: "soong-selinux",
+    pkgPath: "android/soong/selinux",
+    deps: [
+        "soong-android",
+    ],
+    srcs: [
+        "cil_compat_map.go",
+    ],
+    pluginFor: ["soong_build"],
+}
diff --git a/build/soong/cil_compat_map.go b/build/soong/cil_compat_map.go
new file mode 100644
index 000000000..0d91c7678
--- /dev/null
+++ b/build/soong/cil_compat_map.go
@@ -0,0 +1,87 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package selinux
+
+// This file contains "se_cil_compat_map" module type used to build and install
+// sepolicy backwards compatibility mapping files.
+
+import (
+	"android/soong/android"
+	"fmt"
+	"io"
+)
+
+var (
+	pctx = android.NewPackageContext("android/soong/selinux")
+)
+
+func init() {
+	android.RegisterModuleType("se_cil_compat_map", cilCompatMapFactory)
+	pctx.Import("android/soong/common")
+}
+
+func cilCompatMapFactory() android.Module {
+	c := &cilCompatMap{}
+	c.AddProperties(&c.properties)
+	android.InitAndroidModule(c)
+	return c
+}
+
+type cilCompatMapProperties struct {
+	// list of source (.cil) files used to build an sepolicy compatibility mapping
+	// file. srcs may reference the outputs of other modules that produce source
+	// files like genrule or filegroup using the syntax ":module". srcs has to be
+	// non-empty.
+	Srcs []string
+}
+
+type cilCompatMap struct {
+	android.ModuleBase
+	properties cilCompatMapProperties
+	// (.intermediate) module output path as installation source.
+	installSource android.OptionalPath
+}
+
+func (c *cilCompatMap) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	srcFiles := ctx.ExpandSources(c.properties.Srcs, nil)
+	for _, src := range srcFiles {
+		if src.Ext() != ".cil" {
+			ctx.PropertyErrorf("srcs", "%s has to be a .cil file.", src.String())
+		}
+	}
+
+	out := android.PathForModuleGen(ctx, c.Name())
+	ctx.Build(pctx, android.BuildParams{
+		Rule:   android.Cat,
+		Output: out,
+		Inputs: srcFiles,
+	})
+	c.installSource = android.OptionalPathForPath(out)
+}
+
+func (c *cilCompatMap) DepsMutator(ctx android.BottomUpMutatorContext) {
+	android.ExtractSourcesDeps(ctx, c.properties.Srcs)
+}
+
+func (c *cilCompatMap) AndroidMk() android.AndroidMkData {
+	ret := android.AndroidMkData{
+		OutputFile: c.installSource,
+		Class:      "ETC",
+	}
+	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
+		fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/selinux/mapping")
+	})
+	return ret
+}
-- 
GitLab