[SQUASH] update extract-files and smali

commit 7021706d0d0e00556fd9a6e9085dfe3eade66f7d
Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Thu Jan 5 19:37:02 2017 +0100

    extract_utils: Update smali/backsmali to v2.2b4
    
    Change-Id: I91a2c5569ac0e6cf99d742f2b207f8222f9c01f0

commit 2e994ce952c89f3bc9141cba80b517396a212a55
Author: Sam Mortimer <sam@mortimer.me.uk>
Date:   Wed Oct 5 09:50:49 2016 -0700

    Update to smali/baksmali v2.2b3
    
    The cli changed slightly:
    https://github.com/JesusFreke/smali/wiki/SmaliBaksmali2.2
    
    And, for deodexing angler at least, baksmali needs access to boot*.oat
    rather than just boot.oat so point at source rather than copy
    
    
    Resurrected from http://review.cyanogenmod.org/#/c/164958/
    
    With fixed adb $SRC deodexing
    
    Change-Id: Id6ae0e9d7a5554687124e99fb03a808f3115bb95

commit 7f9fff2317b8e1f64b2a74ae6c6447f537737b14
Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Mon Jul 18 13:47:55 2016 +0200

    cm: extract_utils: Implement oat2dex
    
     * Shipping baksmali and smali (version 2.1.3) in vendor/cm
       (Official sources: https://github.com/JesusFreke/smali)
    
    Change-Id: Iaca6337fa2d4b5f2d6654ef5142ec0313af63f5a


commit 3c6bdac74bc212b26f723aa8697099fd636451a8
Author: Adrian DC <radian.dc@gmail.com>
Date:   Sun Jan 15 14:03:26 2017 +0100

    extract_utils: Detect output existence before moving it for backup
    
     * Avoids:
        Cleaning output directory (./../../../vendor/.../.../proprietary)..
        mv: cannot stat './../../../vendor/.../.../proprietary/*':
        No such file or directory
    
    Change-Id: I3258e64f3997f139d8db4fb4e07d14964a469c78

commit 8945f5e8c796a3fca6fe2ae3cd90df331959c167
Author: Matt Mower <mowerm@gmail.com>
Date:   Sat Jan 7 14:08:17 2017 -0600

    extract_utils: Introduce variable INITIAL_COPYRIGHT_YEAR
    
    Let a device specify INITIAL_COPYRIGHT_YEAR=XXXX in setup-makefiles.sh
    so that the correct copyright(s) is/are included in vendor makefiles.
    The CyanogenMod copyright is retained when INITIAL_COPYRIGHT_YEAR<2017
    and the LineageOS copyright is now included for >=2017.
    
    Change-Id: I6895b5b69bc7ba399042ac3c29e17f3209d15f1b

commit 4cf635a5ad34875878971d5048b3e61084e699e0
Author: Gabriele M <moto.falcon.git@gmail.com>
Date:   Thu Jan 5 22:10:00 2017 +0100

    extract_utils: Update backsmali command
    
    -e no longer exists starting from v2.2b3, so update the
    command. While at it, add the logic to deal with Android N
    separated boot oat files.
    
    Change-Id: Iab8e3946d6421824fea74e26dc43d2db38573022

commit 5b38c4fb349c06303de8342935b7d8039e2904f5
Author: Rashed Abdel-Tawab <rashed@linux.com>
Date:   Wed Nov 23 23:00:37 2016 -0500

    extract_utils: Use shasum on macOS
    
    macOS doesn't have sha1sum but instead shasum, so use that
    if 'uname -s' returns Darwin
    
    Change-Id: I1ab8721327a561e8df78562bbc0b12e884cc4a93

commit 082e0ec07b71947e1594abc483f5f341f198699c
Author: Elektroschmock <elektroschmock78@googlemail.com>
Date:   Tue Oct 4 21:11:43 2016 +0200

    extract_utils: Add option to define the certificate for jars
    
    Change-Id: I3fb4ff29b2ce898989f5599b47f47ee7fcfc5e92

commit 2e994ce952c89f3bc9141cba80b517396a212a55
Author: Sam Mortimer <sam@mortimer.me.uk>
Date:   Wed Oct 5 09:50:49 2016 -0700

    Update to smali/baksmali v2.2b3
    
    The cli changed slightly:
    https://github.com/JesusFreke/smali/wiki/SmaliBaksmali2.2
    
    And, for deodexing angler at least, baksmali needs access to boot*.oat
    rather than just boot.oat so point at source rather than copy
    
    
    Resurrected from http://review.cyanogenmod.org/#/c/164958/
    
    With fixed adb $SRC deodexing
    
    Change-Id: Id6ae0e9d7a5554687124e99fb03a808f3115bb95

commit d303031019db059315e42a78fdfdd538158385e2
Author: HashBang <hashbang173@gmail.com>
Date:   Mon Aug 1 14:36:46 2016 -0400

    extract_utils: Do not change app SRC if there are arguments
    
    Do not prepend app or priv-app to the src when a path is explicitly
    given as an argument. This fixes the SRC path in such conditions.
    
    Change-Id: I43370f17fe224df323fcfa9b0d5eae4ee2996524

commit d53bff1f45de6cc81ca79901b405ea1f935e97e2
Author: Rashed Abdel-Tawab <rashed@linux.com>
Date:   Sun Oct 2 01:00:54 2016 -0400

    extract_utils: Add ability to set custom device guard
    
    In some cases we may not want to check against TARGET_DEVICE so allow
    setting a custom variable to check against
    
    Change-Id: Ia2fb338f453137a95a59c6940b0cc16b261505bf

commit 5f173157c452554311e33ac9c074a4ebaf9c37c4
Author: Rashed Abdel-Tawab <rashed@linux.com>
Date:   Sat Oct 1 20:33:00 2016 -0400

    extract_utils: Add ability to set custom vendor.mk name
    
    In some circumstances we might want to set a custom vendor.mk
    filename. If not set, then just default to the current device name
    
    Change-Id: Icb59ebee67ce17b41ac613020685773efb01103d

commit 79fa59b3be49f4dcef795a4ab2e2a0db8ab6e9ca
Author: Steve Kondik <steve@cyngn.com>
Date:   Fri Sep 2 21:10:02 2016 -0700

    extract_utils: Add flag to disable pinning
    
     * We might want to disable pinning entirely for an extract run.
     * Set DISABLE_PINNING=1 to turn it off and override everything.
    
    Change-Id: I1be90dda68f0de1c5f5a70c946052d70bfaab7ed

commit 7561d19795839ad66393c039dc76301eaa7f4752
Author: Steve Kondik <steve@cyngn.com>
Date:   Thu Sep 1 21:40:27 2016 -0700

    extract_utils: Fix device online check
    
     * No need to look for /sbin/recovery as adbd will report
       the device as being in recovery mode already. Also, this
       check appears to hang on N.
    
    Change-Id: I3bcb427835ae3cd37c7ea56cbc57bba0d18fcc04

commit 3b3a0e1c8b9a2f7e57f47a75749db054e0f82592
Author: Hashbang173 <hashbang173@gmail.com>
Date:   Sun Aug 28 20:38:45 2016 -0400

    extract_utils.sh: Disable dex preopt on prebuilt apks
    
    Change-Id: I230f1a8bb95cbc1df36bc652b03e58829ec278f5

commit 48f8df8ddb2eb3b32dbc44dcb50fcfc01bfbc8bd
Author: Steve Kondik <steve@cyngn.com>
Date:   Sun Aug 14 03:55:08 2016 -0700

    extract-utils: Add pinning support
    
     * In many cases, we would like to keep certain files which do not
       exactly match what might be extracted from a factory ROM. This
       becomes extremely annoying over time to manually reconstruct,
       and it's easy to miss these special cases when updating to a
       new vendor release. It's also useful to flag additions which
       aren't found in the upstream release at all.
     * To solve this, we can now "pin" files to a specific sha1 hash.
       Simply append the sha1sum of the file to the appropriate line
       in your bloblist, prepended by a | delimiter.
     * This works by backing up the current files first, running the
       extraction, then checking if any pinned files need to be
       restored.
     * Also add an exit trap to clean up all of our tempfiles
    
    Change-Id: I2010b5175b5701e19a3efb112e8907062ca37d66

commit a516c2f0ac555f2d9b7284a2b89909373a25b6a5
Author: Louis Popi <theh2o64@gmail.com>
Date:   Mon Jul 25 15:51:13 2016 +0200

    cm: extract_utils: Add a firmware extraction method
    
    Change-Id: If14f6932cbdccf45ca0cc91c403e951363e91260

commit 3a03012242c8303b3616e0630c42992070920fc7
Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Sat Jul 30 12:08:25 2016 +0200

    cm: extract_utils: Fixup xml files
    
    Change-Id: Icb8efcca0e6e37a3ea7432716fcbfbd631d96c19

commit 7f9fff2317b8e1f64b2a74ae6c6447f537737b14
Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Mon Jul 18 13:47:55 2016 +0200

    cm: extract_utils: Implement oat2dex
    
     * Shipping baksmali and smali (version 2.1.3) in vendor/cm
       (Official sources: https://github.com/JesusFreke/smali)
    
    Change-Id: Iaca6337fa2d4b5f2d6654ef5142ec0313af63f5a

commit 03ce400352e772634299d074bdbf9c5ffb636869
Author: Steve Kondik <steve@cyngn.com>
Date:   Fri Jul 29 00:00:16 2016 -0700

    Revert "extract_utils: Don't write multilib tags on 32-bit only devices"
    
     * Not gonna work without preparsing all the lists combined together.
    
    This reverts commit b7b7f884437c76d196f94461751093a7efe47d4c.
    
    Change-Id: I63ceead414d7f8416e2c2c8e4b12098077b545e2

commit 3b96ba58415610171de13861886226d8bc02c4d2
Author: Bruno Martins <bgcngm@gmail.com>
Date:   Wed Jul 27 15:00:05 2016 +0100

    cm: extract_utils: Rewrite file existence checks
    
    Change-Id: I7c1584ec7162e0e18fae471e6aceef6123a5d10b

commit b7b7f884437c76d196f94461751093a7efe47d4c
Author: Steve Kondik <steve@cyngn.com>
Date:   Thu Jul 28 14:15:46 2016 -0700

    extract_utils: Don't write multilib tags on 32-bit only devices
    
    Change-Id: I41b6f71c45a566ba1b4dd5cb72c4ebf44388b363

commit a991cf1f9b2a2fe0b015345724856c956ba2c3f8
Author: Steve Kondik <steve@cyngn.com>
Date:   Thu Jul 28 12:13:12 2016 -0700

    extract_utils: Add support for files in rootfs
    
     * A final "rootfs" tag in the last column will place
       the chosen file into the ramdisk
     * Currently only supports files in /sbin
     * Cleaned up usage of class-specific flags
     * Simplified code
    
    Change-Id: Id823d70aab51f8767522f697eaf65f52fc64b94b

commit 52f3680199a2ee9fd26ca4a724169ea96739d41c
Author: Steve Kondik <steve@cyngn.com>
Date:   Thu Jul 21 09:43:30 2016 -0700

    cm: Remove stray echo from extract_utils
    
    Change-Id: I2d893a3f8472a86835feef24540e82c47b69fd67

commit 60ef86de1a0728a66e0a4147fc3ca69ec58805ce
Author: Steve Kondik <steve@cyngn.com>
Date:   Wed Jul 20 20:03:40 2016 -0700

    cm: Underp dumping of 64-bit only libraries
    
    Change-Id: I9c2140b40747fc7f847f1a532c42fb7ae5bfc3fd

commit 4e2aaabd139d310c892329611639a63af257705e
Author: Steve Kondik <steve@cyngn.com>
Date:   Fri Jul 15 10:39:58 2016 -0700

    cm: Add generic tools for extract-files / setup-makefiles scripts
    
     * Instead of having to enumerate packages and do a bunch of tedious
       shit, we now have a way to do it automatically based only on
       the blob lists.
     * Devices still need to implement a short script, but this library
       handles all the core functions.
     * Yes, we should probably be doing this in Python- Bash is
       absolutely horrible at dealing with arrays and lists.
    
    Change-Id: I73b30ff4531c4d1b33ac53db33abf62555f500b4

Change-Id: I1a373a998688bad7f6a780acd0ef86e1511af02c
diff --git a/build/tools/extract_utils.sh b/build/tools/extract_utils.sh
old mode 100644
new mode 100755
index e7eff82..96c7480
--- a/build/tools/extract_utils.sh
+++ b/build/tools/extract_utils.sh
@@ -48,6 +48,7 @@
 # $3: CM root directory
 # $4: is common device - optional, default to false
 # $5: cleanup - optional, default to true
+# $6: custom vendor makefile name - optional, default to false
 #
 # Must be called before any other functions can be used. This
 # sets up the internal state for a new vendor configuration.
@@ -76,6 +77,11 @@
         mkdir -p "$CM_ROOT/$OUTDIR"
     fi
 
+    VNDNAME="$6"
+    if [ -z "$VNDNAME" ]; then
+        VNDNAME="$DEVICE"
+    fi
+
     export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/device-vendor.mk
     export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
     export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
@@ -249,10 +255,12 @@
                 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
             fi
         elif [ "$CLASS" = "APPS" ]; then
-            if [ "$EXTRA" = "priv-app" ]; then
-                SRC="$SRC/priv-app"
-            else
-                SRC="$SRC/app"
+            if [ -z "$ARGS" ]; then
+                if [ "$EXTRA" = "priv-app" ]; then
+                    SRC="$SRC/priv-app"
+                else
+                    SRC="$SRC/app"
+                fi
             fi
             printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
             local CERT=platform
@@ -430,12 +438,35 @@
 # be executed first!
 #
 function write_header() {
+    if [ -f $1 ]; then
+        rm $1
+    fi
+
     YEAR=$(date +"%Y")
 
     [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
 
-    cat << EOF > $1
-# Copyright (C) $YEAR The CyanogenMod Project
+    NUM_REGEX='^[0-9]+$'
+    if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
+        if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
+            printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
+        elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
+            printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
+        fi
+        if [ $YEAR -eq 2017 ]; then
+            printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
+        elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
+            printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
+        elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
+            printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
+        else
+            printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
+        fi
+    else
+        printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
+    fi
+
+    cat << EOF >> $1
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -458,6 +489,7 @@
 # write_headers:
 #
 # $1: devices falling under common to be added to guard - optional
+# $2: custom guard - optional
 #
 # Calls write_header for each of the makefiles and creates
 # the initial path declaration and device guard for the
@@ -465,13 +497,19 @@
 #
 function write_headers() {
     write_header "$ANDROIDMK"
+
+    GUARD="$2"
+    if [ -z "$GUARD" ]; then
+        GUARD="TARGET_DEVICE"
+    fi
+
     cat << EOF >> "$ANDROIDMK"
 LOCAL_PATH := \$(call my-dir)
 
 EOF
     if [ "$COMMON" -ne 1 ]; then
         cat << EOF >> "$ANDROIDMK"
-ifeq (\$(TARGET_DEVICE),$DEVICE)
+ifeq (\$($GUARD),$DEVICE)
 
 EOF
     else
@@ -480,7 +518,7 @@
             exit 1
         fi
         cat << EOF >> "$ANDROIDMK"
-ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
+ifneq (\$(filter $1,\$($GUARD)),)
 
 EOF
     fi
@@ -504,8 +542,7 @@
 # Return success if adb is up and not in recovery
 function _adb_connected {
     {
-        if [[ "$(adb get-state)" == device &&
-              "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
+        if [[ "$(adb get-state)" == device ]]
         then
             return 0
         fi
@@ -616,7 +653,7 @@
         return 1
     else
         # try to copy
-        cp "$SRC/$1" "$2" 2>/dev/null && return 0
+        cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
 
         return 1
     fi
@@ -645,10 +682,13 @@
 
     # Extract existing boot.oats to the temp folder
     if [ -z "$ARCHES" ]; then
-        echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
+        echo "Checking if system is odexed and locating boot.oats..."
         for ARCH in "arm64" "arm" "x86_64" "x86"; do
-            if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
+            mkdir -p "$TMPDIR/system/framework/$ARCH"
+            if get_file "system/framework/$ARCH/" "$TMPDIR/system/framework/" "$SRC"; then
                 ARCHES+="$ARCH "
+            else
+                rmdir "$TMPDIR/system/framework/$ARCH"
             fi
         done
     fi
@@ -666,20 +706,24 @@
     fi
 
     for ARCH in $ARCHES; do
-        BOOTOAT="$TMPDIR/boot_$ARCH.oat"
+        BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
 
         local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
 
         if get_file "$OAT" "$TMPDIR" "$SRC"; then
-            java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
+            java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
         elif [[ "$CM_TARGET" =~ .jar$ ]]; then
-            # try to extract classes.dex from boot.oat for framework jars
-            java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
+            # try to extract classes.dex from boot.oats for framework jars
+            JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
+            if [ ! -f "$JAROAT" ]; then
+                JAROAT=$BOOTOAT;
+            fi
+            java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
         else
             continue
         fi
 
-        java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
+        java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
     done
 
     rm -rf "$TMPDIR/dexout"
@@ -762,7 +806,9 @@
         echo "Cleaning output directory ($OUTPUT_ROOT).."
         rm -rf "${OUTPUT_TMP:?}"
         mkdir -p "${OUTPUT_TMP:?}"
-        mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
+        if [ -d "$OUTPUT_ROOT" ]; then
+            mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
+        fi
         VENDOR_STATE=1
     fi
 
@@ -834,20 +880,28 @@
 
         # Check pinned files
         local HASH="${HASHLIST[$i-1]}"
-        if [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
+        if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
             local KEEP=""
             local TMP="$TMP_DIR/$FROM"
             if [ -f "$TMP" ]; then
                 if [ ! -f "$DEST" ]; then
                     KEEP="1"
                 else
-                    local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
+                    if [ "$(uname)" == "Darwin" ]; then
+                        local DEST_HASH=$(shasum "$DEST" | awk '{print $1}' )
+                    else
+                        local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
+                    fi
                     if [ "$DEST_HASH" != "$HASH" ]; then
                         KEEP="1"
                     fi
                 fi
                 if [ "$KEEP" = "1" ]; then
-                    local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
+                    if [ "$(uname)" == "Darwin" ]; then
+                        local TMP_HASH=$(shasum "$TMP" | awk '{print $1}' )
+                    else
+                        local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
+                    fi
                     if [ "$TMP_HASH" = "$HASH" ]; then
                         printf '    + (keeping pinned file with hash %s)\n' "$HASH"
                         cp -p "$TMP" "$DEST"