fs_config: generate oem AID header file

Generate an OEM AID_<name> header file seperate from fs_config
header file and provide details on how to export this interface
into native code.

Test: That ls, ps, chown and services function for built in
services as before.
Change-Id: Ie8ce6585e0721b52633ee50d62dcfe796e178f65
Signed-off-by: William Roberts <william.c.roberts@intel.com>
diff --git a/tools/fs_config/Android.mk b/tools/fs_config/Android.mk
index 932442c..7f216d2 100644
--- a/tools/fs_config/Android.mk
+++ b/tools/fs_config/Android.mk
@@ -82,6 +82,17 @@
 
 ifneq ($(TARGET_FS_CONFIG_GEN),)
 system_android_filesystem_config := system/core/include/private/android_filesystem_config.h
+
+# Generate the "generated_oem_aid.h" file
+oem := $(local-generated-sources-dir)/generated_oem_aid.h
+$(oem): PRIVATE_LOCAL_PATH := $(LOCAL_PATH)
+$(oem): PRIVATE_TARGET_FS_CONFIG_GEN := $(TARGET_FS_CONFIG_GEN)
+$(oem): PRIVATE_ANDROID_FS_HDR := $(system_android_filesystem_config)
+$(oem): PRIVATE_CUSTOM_TOOL = $(PRIVATE_LOCAL_PATH)/fs_config_generator.py oemaid --aid-header=$(PRIVATE_ANDROID_FS_HDR) $(PRIVATE_TARGET_FS_CONFIG_GEN) > $@
+$(oem): $(TARGET_FS_CONFIG_GEN) $(LOCAL_PATH)/fs_config_generator.py
+	$(transform-generated-source)
+
+# Generate the fs_config header
 gen := $(local-generated-sources-dir)/$(ANDROID_FS_CONFIG_H)
 $(gen): PRIVATE_LOCAL_PATH := $(LOCAL_PATH)
 $(gen): PRIVATE_TARGET_FS_CONFIG_GEN := $(TARGET_FS_CONFIG_GEN)
@@ -90,13 +101,16 @@
 $(gen): $(TARGET_FS_CONFIG_GEN) $(system_android_filesystem_config) $(LOCAL_PATH)/fs_config_generator.py
 	$(transform-generated-source)
 
-LOCAL_GENERATED_SOURCES := $(gen)
+LOCAL_GENERATED_SOURCES := $(oem) $(gen)
+
 my_fs_config_h := $(gen)
+my_gen_oem_aid := $(oem)
 system_android_filesystem_config :=
 gen :=
+oem :=
 endif
 
-LOCAL_C_INCLUDES := $(dir $(my_fs_config_h))
+LOCAL_C_INCLUDES := $(dir $(my_fs_config_h)) $(dir $(my_gen_oem_aid))
 
 include $(BUILD_HOST_EXECUTABLE)
 fs_config_generate_bin := $(LOCAL_INSTALLED_MODULE)
@@ -125,6 +139,21 @@
 	@mkdir -p $(dir $@)
 	$< -F -o $@
 
+ifneq ($(TARGET_FS_CONFIG_GEN),)
+
+##################################
+# Build the oemaid library when fs config files are present.
+# Intentionally break build if you require generated AIDS
+# header file, but are not using any fs config files.
+include $(CLEAR_VARS)
+LOCAL_MODULE := liboemaids
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(dir $(my_gen_oem_aid))
+LOCAL_EXPORT_C_INCLUDE_DEPS := $(my_gen_oem_aid)
+include $(BUILD_STATIC_LIBRARY)
+
+endif
+
 ANDROID_FS_CONFIG_H :=
 my_fs_config_h :=
 fs_config_generate_bin :=
+my_gen_oem_aid :=
diff --git a/tools/fs_config/README b/tools/fs_config/README
index 0258687..f4a59ca 100644
--- a/tools/fs_config/README
+++ b/tools/fs_config/README
@@ -118,3 +118,8 @@
 representation of value is preserved. Both choices were made for maximum readability of the generated
 file and to line up files. Sync lines are placed with the source file as comments in the generated
 header file.
+
+For OEMs wishing to use the define AIDs in their native code, one can access the generated header
+file like so:
+  1. In your C code just #include "generated_oem_aid.h" and start using the declared identifiers.
+  2. In your Makefile add this static library like so: LOCAL_STATIC_LIBRARIES := liboemaids
diff --git a/tools/fs_config/fs_config_generator.py b/tools/fs_config/fs_config_generator.py
index 0b5e8d5..06130db 100755
--- a/tools/fs_config/fs_config_generator.py
+++ b/tools/fs_config/fs_config_generator.py
@@ -804,7 +804,9 @@
          */
         """)
 
-    _INCLUDE = '#include <private/android_filesystem_config.h>'
+    _INCLUDES = [
+        '<private/android_filesystem_config.h>', '"generated_oem_aid.h"'
+    ]
 
     _DEFINE_NO_DIRS = '#define NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS'
     _DEFINE_NO_FILES = '#define NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_FILES'
@@ -889,6 +891,15 @@
         print '    ' + expanded
 
     @staticmethod
+    def _gen_inc():
+        """
+        Generate the include header lines and print to stdout.
+        Internal use only.
+        """
+        for include in FSConfigGen._INCLUDES:
+            print '#include %s' % include
+
+    @staticmethod
     def _generate(files, dirs, aids):
         """Generates an OEM android_filesystem_config.h header file to stdout.
 
@@ -899,7 +910,9 @@
             aids ([AIDS]): A list of AID objects for Android Id entries.
         """
         print FSConfigGen._GENERATED
-        print FSConfigGen._INCLUDE
+        print
+
+        FSConfigGen._gen_inc()
         print
 
         are_dirs = len(dirs) > 0
@@ -997,6 +1010,71 @@
         print
 
 
+@generator('oemaid')
+class OEMAidGen(BaseGenerator):
+    """Generates the OEM AID_<name> value header file."""
+
+    _GENERATED = ('/*\n'
+                  ' * THIS IS AN AUTOGENERATED FILE! DO NOT MODIFY!\n'
+                  ' */')
+
+    _GENERIC_DEFINE = "#define %s\t%s"
+
+    _FILE_COMMENT = '// Defined in file: \"%s\"'
+
+    # Intentional trailing newline for readability.
+    _FILE_IFNDEF_DEFINE = ('#ifndef GENERATED_OEM_AIDS_H_\n'
+                           '#define GENERATED_OEM_AIDS_H_\n')
+
+    _FILE_ENDIF = '#endif'
+
+    def __init__(self):
+
+        self._old_file = None
+
+    def add_opts(self, opt_group):
+
+        opt_group.add_argument(
+            'fsconfig', nargs='+', help='The list of fsconfig files to parse.')
+
+        opt_group.add_argument(
+            '--aid-header',
+            required=True,
+            help='An android_filesystem_config.h file'
+            'to parse AIDs and OEM Ranges from')
+
+    def __call__(self, args):
+
+        hdr_parser = AIDHeaderParser(args['aid_header'])
+
+        parser = FSConfigFileParser(args['fsconfig'], hdr_parser.oem_ranges)
+
+        print OEMAidGen._GENERATED
+
+        print OEMAidGen._FILE_IFNDEF_DEFINE
+
+        for aid in parser.aids:
+            self._print_aid(aid)
+            print
+
+        print OEMAidGen._FILE_ENDIF
+
+    def _print_aid(self, aid):
+        """Prints a valid #define AID identifier to stdout.
+
+        Args:
+            aid to print
+        """
+
+        # print the source file location of the AID
+        found_file = aid.found
+        if found_file != self._old_file:
+            print OEMAidGen._FILE_COMMENT % found_file
+            self._old_file = found_file
+
+        print OEMAidGen._GENERIC_DEFINE % (aid.identifier, aid.value)
+
+
 def main():
     """Main entry point for execution."""