Label APEX files correctly when TARGET_FLATTEN_APEX=true

When TARGET_FLATTEN_APEX=true, APEX files are not packaged in *.apex
files but flattened to the system partition under /system/apex/<name>
directories. There was a bug that those flattened files are not labeled
because the per-APEX file_contexts were applied only when building
*.apex. Fixing this by converting the file_contexts files so that
/system/apex/<name> path is prepended and applying the generated
file_contexts file for system.img when TARGET_FLATTEN_APEX=true.

Bug: 123314817
Test: TARGET_FLATTEN_APEX=true m
ls -alZ /system/apex/*/* shows that the files are correctly labeled

Change-Id: Ia82740a1da0083d5bcfd71354a6d374d2a918342
diff --git a/Android.mk b/Android.mk
index 7b39fc1..df4a004 100644
--- a/Android.mk
+++ b/Android.mk
@@ -173,6 +173,19 @@
 SHAREDLIB_EXT=so
 endif
 
+# Convert a file_context file for a non-flattened APEX into a file for
+# flattened APEX. /system/apex/<apex_name> path is prepended to the original paths
+# $(1): path to the input file_contexts file for non-flattened APEX
+# $(2): name of the APEX
+# $(3): path to the generated file_contexs file for flattened APEX
+# $(4): variable where $(3) is added to
+define build_flattened_apex_file_contexts
+$(4) += $(3)
+$(3): PRIVATE_APEX_PATH := /system/apex/$(subst .,\\.,$(2))
+$(3): $(1)
+	$(hide) awk '/object_r/{printf("$$(PRIVATE_APEX_PATH)%s\n",$$$$0)}' $$< > $$@
+endef
+
 #################################
 
 include $(CLEAR_VARS)
@@ -1029,6 +1042,14 @@
 ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
   local_fc_files += $(wildcard $(addsuffix /file_contexts_overlayfs, $(PLAT_PRIVATE_POLICY)))
 endif
+ifeq ($(TARGET_FLATTEN_APEX),true)
+  apex_fc_files := $(wildcard $(LOCAL_PATH)/apex/*-file_contexts)
+  $(foreach _input,$(apex_fc_files),\
+    $(eval _output := $(intermediates)/$(notdir $(_input))-flattened)\
+    $(eval _apex_name := $(patsubst %-file_contexts,%,$(notdir $(_input))))\
+    $(eval $(call build_flattened_apex_file_contexts,$(_input),$(_apex_name),$(_output),local_fc_files))\
+   )
+endif
 local_fcfiles_with_nl := $(call add_nl, $(local_fc_files), $(built_nl))
 
 file_contexts.local.tmp := $(intermediates)/file_contexts.local.tmp
diff --git a/file_contexts.mk b/file_contexts.mk
index 58ecd87..267b68f 100644
--- a/file_contexts.mk
+++ b/file_contexts.mk
@@ -14,6 +14,14 @@
 ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
   local_fc_files += $(wildcard $(addsuffix /file_contexts_overlayfs, $(PLAT_PRIVATE_POLICY)))
 endif
+ifeq ($(TARGET_FLATTEN_APEX),true)
+  apex_fc_files := $(wildcard $(LOCAL_PATH)/apex/*-file_contexts)
+  $(foreach _input,$(apex_fc_files),\
+    $(eval _output := $(intermediates)/$(notdir $(_input))-flattened)\
+    $(eval _apex_name := $(patsubst %-file_contexts,%,$(notdir $(_input))))\
+    $(eval $(call build_flattened_apex_file_contexts,$(_input),$(_apex_name),$(_output),local_fc_files))\
+   )
+endif
 local_fcfiles_with_nl := $(call add_nl, $(local_fc_files), $(built_nl))
 
 $(LOCAL_BUILT_MODULE): PRIVATE_FC_FILES := $(local_fcfiles_with_nl)