extract_utils: Support extracting from product partition

Change-Id: I2d0d01253fdf93d63a26556208d45cd69e76329f
(cherry picked from commit ea15be6e92e3be0c103948d220de64fd2e1d781f)
diff --git a/build/tools/extract_utils.sh b/build/tools/extract_utils.sh
index 81487ca..d335307 100644
--- a/build/tools/extract_utils.sh
+++ b/build/tools/extract_utils.sh
@@ -279,6 +279,10 @@
                 local OUTTARGET=$(truncate_file $TARGET)
                 printf '    %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
                     "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
+            elif prefix_match_file "product/" $TARGET ; then
+                local OUTTARGET=$(truncate_file $TARGET)
+                printf '    %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
+                    "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
             else
                 printf '    %s/proprietary/%s:system/%s%s\n' \
                     "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
@@ -295,7 +299,7 @@
 # write_packages:
 #
 # $1: The LOCAL_MODULE_CLASS for the given module list
-# $2: "true" if this package is part of the vendor/ path
+# $2: /product or /vendor partition
 # $3: type-specific extra flags
 # $4: Name of the array holding the target list
 #
@@ -306,7 +310,7 @@
 function write_packages() {
 
     local CLASS="$1"
-    local VENDOR_PKG="$2"
+    local PARTITION="$2"
     local EXTRA="$3"
 
     # Yes, this is a horrible hack - we create a new array using indirection
@@ -333,8 +337,10 @@
         PACKAGE_LIST+=("$PKGNAME")
 
         SRC="proprietary"
-        if [ "$VENDOR_PKG" = "true" ]; then
+        if [ "$PARTITION" = "vendor" ]; then
             SRC+="/vendor"
+        elif [ "$PARTITION" = "product" ]; then
+            SRC+="/product"
         fi
 
         printf 'include $(CLEAR_VARS)\n'
@@ -412,8 +418,10 @@
         if [ "$EXTRA" = "priv-app" ]; then
             printf 'LOCAL_PRIVILEGED_MODULE := true\n'
         fi
-        if [ "$VENDOR_PKG" = "true" ]; then
+        if [ "$PARTITION" = "vendor" ]; then
             printf 'LOCAL_VENDOR_MODULE := true\n'
+        elif [ "$PARTITION" = "product" ]; then
+            printf 'LOCAL_PRODUCT_MODULE := true\n'
         fi
         printf 'include $(BUILD_PREBUILT)\n\n'
     done
@@ -445,13 +453,13 @@
     local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
 
     if [ "${#MULTILIBS[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
+        write_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDMK"
     fi
     if [ "${#LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
+        write_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDMK"
     fi
     if [ "${#LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
+        write_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDMK"
     fi
 
     local T_V_LIB32=( $(prefix_match "vendor/lib/") )
@@ -461,65 +469,101 @@
     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" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
+        write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
     fi
     if [ "${#V_LIB32[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
+        write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
     fi
     if [ "${#V_LIB64[@]}" -gt "0" ]; then
-        write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
+        write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
+    fi
+
+    local T_P_LIB32=( $(prefix_match "product/lib/") )
+    local T_P_LIB64=( $(prefix_match "product/lib64/") )
+    local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
+    local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
+    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"
+    fi
+    if [ "${#P_LIB32[@]}" -gt "0" ]; then
+        write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
+    fi
+    if [ "${#P_LIB64[@]}" -gt "0" ]; then
+        write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
     fi
 
     # Apps
     local APPS=( $(prefix_match "app/") )
     if [ "${#APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
+        write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
     fi
     local PRIV_APPS=( $(prefix_match "priv-app/") )
     if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
+        write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
     fi
     local V_APPS=( $(prefix_match "vendor/app/") )
     if [ "${#V_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
+        write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
     fi
     local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
     if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
-        write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
+        write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
+    fi
+    local P_APPS=( $(prefix_match "product/app/") )
+    if [ "${#P_APPS[@]}" -gt "0" ]; then
+        write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
+    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"
     fi
 
     # Framework
     local FRAMEWORK=( $(prefix_match "framework/") )
     if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
+        write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
     fi
     local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
     if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
-        write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
+        write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
+    fi
+    local P_FRAMEWORK=( $(prefix_match "product/framework/") )
+    if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
+        write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
     fi
 
     # Etc
     local ETC=( $(prefix_match "etc/") )
     if [ "${#ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
+        write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
     fi
     local V_ETC=( $(prefix_match "vendor/etc/") )
     if [ "${#V_ETC[@]}" -gt "0" ]; then
-        write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
+        write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
+    fi
+    local P_ETC=( $(prefix_match "product/etc/") )
+    if [ "${#P_ETC[@]}" -gt "0" ]; then
+        write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
     fi
 
     # Executables
     local BIN=( $(prefix_match "bin/") )
     if [ "${#BIN[@]}" -gt "0"  ]; then
-        write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
+        write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
     fi
     local V_BIN=( $(prefix_match "vendor/bin/") )
     if [ "${#V_BIN[@]}" -gt "0" ]; then
-        write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
+        write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
+    fi
+    local P_BIN=( $(prefix_match "product/bin/") )
+    if [ "${#P_BIN[@]}" -gt "0" ]; then
+        write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
     fi
     local SBIN=( $(prefix_match "sbin/") )
     if [ "${#SBIN[@]}" -gt "0" ]; then
-        write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
+        write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
     fi