extract_files: Implement blueprint file generation

Switch to blueprint on:
 - shared objects
 - $partiton/etc/ files
 - JARs
 - executable binaries and scripts
 - APKs

Only /sbin binaries are still in Android.mk because blueprint
doesn't handle sbin installation yet

Change-Id: I1dfd7e8bb575367b2a7fa9e333c4c6fa3aa68180
diff --git a/build/tools/extract_utils.sh b/build/tools/extract_utils.sh
index 51e6361..5b5a732 100644
--- a/build/tools/extract_utils.sh
+++ b/build/tools/extract_utils.sh
@@ -317,7 +317,7 @@
 }
 
 #
-# write_packages:
+# write_blueprint_packages:
 #
 # $1: The LOCAL_MODULE_CLASS for the given module list
 # $2: /system, /odm, /product, or /vendor partition
@@ -328,7 +328,169 @@
 # for all modules in the list. This is called by write_product_packages
 # after the modules are categorized.
 #
-function write_packages() {
+function write_blueprint_packages() {
+
+    local CLASS="$1"
+    local PARTITION="$2"
+    local EXTRA="$3"
+
+    # Yes, this is a horrible hack - we create a new array using indirection
+    local ARR_NAME="$4[@]"
+    local FILELIST=("${!ARR_NAME}")
+
+    local FILE=
+    local ARGS=
+    local BASENAME=
+    local EXTENSION=
+    local PKGNAME=
+    local SRC=
+
+    for P in "${FILELIST[@]}"; do
+        FILE=$(target_file "$P")
+        ARGS=$(target_args "$P")
+
+        BASENAME=$(basename "$FILE")
+        DIRNAME=$(dirname "$FILE")
+        EXTENSION=${BASENAME##*.}
+        PKGNAME=${BASENAME%.*}
+
+        # Add to final package list
+        PACKAGE_LIST+=("$PKGNAME")
+
+        SRC="proprietary"
+        if [ "$PARTITION" = "system" ]; then
+            SRC+="/system"
+        elif [ "$PARTITION" = "vendor" ]; then
+            SRC+="/vendor"
+        elif [ "$PARTITION" = "product" ]; then
+            SRC+="/product"
+        elif [ "$PARTITION" = "odm" ]; then
+            SRC+="/odm"
+        fi
+
+        if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
+            printf 'cc_prebuilt_library_shared {\n'
+            printf '\tname: "%s",\n' "$PKGNAME"
+            printf '\towner: "%s",\n' "$VENDOR"
+            printf '\tstrip: {\n'
+            printf '\t\tnone: true,\n'
+            printf '\t},\n'
+            printf '\ttarget: {\n'
+            if [ "$EXTRA" = "both" ]; then
+                printf '\t\tandroid_arm: {\n'
+                printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
+                printf '\t\t},\n'
+                printf '\t\tandroid_arm64: {\n'
+                printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
+                printf '\t\t},\n'
+            elif [ "$EXTRA" = "64" ]; then
+                printf '\t\tandroid_arm64: {\n'
+                printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
+                printf '\t\t},\n'
+            else
+                printf '\t\tandroid_arm: {\n'
+                printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
+                printf '\t\t},\n'
+            fi
+            printf '\t},\n'
+            if [ "$EXTRA" != "none" ]; then
+                printf '\tcompile_multilib: "%s",\n' "$EXTRA"
+            fi
+        elif [ "$CLASS" = "APPS" ]; then
+            printf 'android_app_import {\n'
+            printf '\tname: "%s",\n' "$PKGNAME"
+            printf '\towner: "%s",\n' "$VENDOR"
+            if [ "$EXTRA" = "priv-app" ]; then
+                SRC="$SRC/priv-app"
+            else
+                SRC="$SRC/app"
+            fi
+            printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
+            if [ "$ARGS" = "PRESIGNED" ]; then
+                printf '\tpresigned: true,\n'
+            elif [ ! -z "$ARGS" ]; then
+                printf '\tcertificate: "%s",\n' "$ARGS"
+            else
+                printf '\tcertificate: "platform",\n'
+            fi
+        elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
+            printf 'dex_import {\n'
+            printf '\tname: "%s",\n' "$PKGNAME"
+            printf '\towner: "%s",\n' "$VENDOR"
+            printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
+        elif [ "$CLASS" = "ETC" ]; then
+            if [ "$EXTENSION" = "xml" ]; then
+                printf 'prebuilt_etc_xml {\n'
+            else
+                printf 'prebuilt_etc {\n'
+            fi
+            printf '\tname: "%s",\n' "$PKGNAME"
+            printf '\towner: "%s",\n' "$VENDOR"
+            printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
+        elif [ "$CLASS" = "EXECUTABLES" ]; then
+            if [ "$EXTENSION" = "sh" ]; then
+                printf 'sh_binary {\n'
+            else
+                printf 'cc_prebuilt_binary {\n'
+            fi
+            printf '\tname: "%s",\n' "$PKGNAME"
+            printf '\towner: "%s",\n' "$VENDOR"
+            if [ "$ARGS" = "rootfs" ]; then
+                SRC="$SRC/rootfs"
+                if [ "$EXTRA" = "sbin" ]; then
+                    SRC="$SRC/sbin"
+                    printf '\tdist {\n'
+                    printf '\t\tdest: "%s",\n' "root/sbin"
+                    printf '\t},'
+                fi
+            else
+                SRC="$SRC/bin"
+            fi
+            printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
+            unset EXTENSION
+        else
+            printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
+        fi
+        if [ "$CLASS" = "APPS" ]; then
+            printf '\tdex_preopt: {\n'
+            printf '\t\tenabled: false,\n'
+            printf '\t},\n'
+        fi
+        if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] || [ "$CLASS" = "ETC" ] ; then
+            if [ "$DIRNAME" != "." ]; then
+                printf '\tsub_dir: "%s",\n' "$DIRNAME"
+            fi
+        fi
+        if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
+            printf '\tprefer: true,\n'
+        fi
+        if [ "$EXTRA" = "priv-app" ]; then
+            printf '\tprivileged: true,\n'
+        fi
+        if [ "$PARTITION" = "vendor" ]; then
+            printf '\tsoc_specific: true,\n'
+        elif [ "$PARTITION" = "product" ]; then
+            printf '\tproduct_specific: true,\n'
+        elif [ "$PARTITION" = "odm" ]; then
+            printf '\tdevice_specific: true,\n'
+        fi
+        printf '}\n\n'
+    done
+}
+
+#
+# write_makefile_packages:
+#
+# $1: The LOCAL_MODULE_CLASS for the given module list
+# $2: /odm, /product, or /vendor partition
+# $3: type-specific extra flags
+# $4: Name of the array holding the target list
+#
+# Internal function which writes out the BUILD_PREBUILT stanzas
+# for all modules in the list. This is called by write_product_packages
+# after the modules are categorized.
+#
+function write_makefile_packages() {
 
     local CLASS="$1"
     local PARTITION="$2"
@@ -480,13 +642,13 @@
     local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
 
     if [ "${#MULTILIBS[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
     fi
     if [ "${#LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
     fi
     if [ "${#LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
     fi
 
     local T_S_LIB32=( $(prefix_match "system/lib/") )
@@ -496,13 +658,13 @@
     local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
 
     if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
     fi
     if [ "${#S_LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
     fi
     if [ "${#S_LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
     fi
 
     local T_V_LIB32=( $(prefix_match "vendor/lib/") )
@@ -512,13 +674,13 @@
     local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
 
     if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
     fi
     if [ "${#V_LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
     fi
     if [ "${#V_LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
     fi
 
     local T_P_LIB32=( $(prefix_match "product/lib/") )
@@ -528,13 +690,13 @@
     local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
 
     if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
     fi
     if [ "${#P_LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
     fi
     if [ "${#P_LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
     fi
 
     local T_O_LIB32=( $(prefix_match "odm/lib/") )
@@ -544,125 +706,125 @@
     local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
 
     if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
     fi
     if [ "${#O_LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
     fi
     if [ "${#O_LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDMK"
+        write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
     fi
 
     # Apps
     local APPS=( $(prefix_match "app/") )
     if [ "${#APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
     fi
     local PRIV_APPS=( $(prefix_match "priv-app/") )
     if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
     fi
     local S_APPS=( $(prefix_match "system/app/") )
     if [ "${#S_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
     fi
     local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
     if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
     fi
     local V_APPS=( $(prefix_match "vendor/app/") )
     if [ "${#V_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
     fi
     local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
     if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
     fi
     local P_APPS=( $(prefix_match "product/app/") )
     if [ "${#P_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
     fi
     local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
     if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
     fi
     local O_APPS=( $(prefix_match "odm/app/") )
     if [ "${#O_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
     fi
     local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
     if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
+        write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
     fi
 
     # Framework
     local FRAMEWORK=( $(prefix_match "framework/") )
     if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
+        write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
     fi
     local S_FRAMEWORK=( $(prefix_match "system/framework/") )
     if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDMK"
+        write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
     fi
     local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
     if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
+        write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
     fi
     local P_FRAMEWORK=( $(prefix_match "product/framework/") )
     if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
+        write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
     fi
     local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
     if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
+        write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
     fi
 
     # Etc
     local ETC=( $(prefix_match "etc/") )
     if [ "${#ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
+        write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
     fi
     local S_ETC=( $(prefix_match "system/etc/") )
     if [ "${#ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDMK"
+        write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
     fi
     local V_ETC=( $(prefix_match "vendor/etc/") )
     if [ "${#V_ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
+        write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
     fi
     local P_ETC=( $(prefix_match "product/etc/") )
     if [ "${#P_ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
+        write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
     fi
     local O_ETC=( $(prefix_match "odm/etc/") )
     if [ "${#O_ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDMK"
+        write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
     fi
 
     # Executables
     local BIN=( $(prefix_match "bin/") )
     if [ "${#BIN[@]}" -gt "0"  ]; then
-        write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
+        write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
     fi
     local S_BIN=( $(prefix_match "system/bin/") )
     if [ "${#BIN[@]}" -gt "0"  ]; then
-        write_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDMK"
+        write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
     fi
     local V_BIN=( $(prefix_match "vendor/bin/") )
     if [ "${#V_BIN[@]}" -gt "0" ]; then
-        write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
+        write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
     fi
     local P_BIN=( $(prefix_match "product/bin/") )
     if [ "${#P_BIN[@]}" -gt "0" ]; then
-        write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
+        write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
     fi
     local O_BIN=( $(prefix_match "odm/bin/") )
     if [ "${#O_BIN[@]}" -gt "0" ]; then
-        write_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDMK"
+        write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
     fi
     local SBIN=( $(prefix_match "sbin/") )
     if [ "${#SBIN[@]}" -gt "0" ]; then
-        write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
+        write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
     fi