Build & package libgccunwind for app linking.

Change-Id: I086e59c0c359636ae8d4a130f5ee6f5fd2326e62
diff --git a/build/tools/build-gcc.sh b/build/tools/build-gcc.sh
index 4e8d31c..4639050 100755
--- a/build/tools/build-gcc.sh
+++ b/build/tools/build-gcc.sh
@@ -406,6 +406,68 @@
     exit 1
 fi
 
+unwind_library_for_abi ()
+{
+    local ABI="$1"
+    local BASE_DIR OBJS UNWIND_OBJS
+
+    case $ABI in
+    armeabi)
+    BASE_DIR="$BUILD_OUT/gcc-$GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
+    OBJS="unwind-arm.o \
+          libunwind.o \
+          pr-support.o \
+          unwind-c.o"
+    ;;
+    armeabi-v7a)
+    BASE_DIR="$BUILD_OUT/gcc-$GCC_VERSION/$ABI_CONFIGURE_TARGET/armv7-a/libgcc/"
+    OBJS="unwind-arm.o \
+          libunwind.o \
+          pr-support.o \
+          unwind-c.o"
+    ;;
+    x86)
+    BASE_DIR="$BUILD_OUT/gcc-$GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
+    OBJS="unwind-c.o \
+          unwind-dw2-fde-glibc.o \
+          unwind-dw2.o"
+    ;;
+    mips)
+    BASE_DIR="$BUILD_OUT/gcc-$GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
+    OBJS="unwind-c.o \
+          unwind-dw2-fde-glibc.o \
+          unwind-dw2.o"
+    ;;
+    esac
+
+    for OBJ in $OBJS; do
+        UNWIND_OBJS=$UNWIND_OBJS" $BASE_DIR/$OBJ"
+    done
+    echo $UNWIND_OBJS
+}
+
+# Create libgccunwind.a for app linking
+# $1: arch name
+# $2: NDK_DIR
+create_unwind_library ()
+{
+    local ARCH="$1"
+    local NDK_DIR="$2"
+    local ABIS=$(commas_to_spaces $(convert_archs_to_abis $ARCH))
+    local ABI UNWIND_OBJS UNWIND_LIB
+    for ABI in $ABIS; do
+        UNWIND_OBJS=$(unwind_library_for_abi $ABI)
+        UNWIND_LIB_DIR="$NDK_DIR/$GCCUNWIND_SUBDIR/libs/$ABI/"
+        run mkdir -p $UNWIND_LIB_DIR
+        run ar crs $UNWIND_LIB_DIR/libgccunwind.a $UNWIND_OBJS
+    done
+}
+
+# Only create libgccunwind.a when building default version of gcc
+if [ "$GCC_VERSION" = "$DEFAULT_GCC_VERSION" ]; then
+    run create_unwind_library $ARCH $NDK_DIR
+fi
+
 # copy to toolchain path
 run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
 
@@ -539,6 +601,18 @@
         EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE}"
     fi
     pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR" $EXCLUSIONS
+    # package libgccunwind.a
+    if [ "$GCC_VERSION" = "$DEFAULT_GCC_VERSION" ]; then
+        ABIS=$(commas_to_spaces $(convert_archs_to_abis $ARCH))
+        for ABI in $ABIS; do
+            FILES="$GCCUNWIND_SUBDIR/libs/$ABI/libgccunwind.a"
+            PACKAGE="$PACKAGE_DIR/libgccunwind-libs-$ABI.tar.bz2"
+            log "Packaging: $PACKAGE"
+            pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
+            fail_panic "Could not package $ABI libgccunwind binaries!"
+            dump "Packaging: $PACKAGE"
+        done
+    fi
 fi
 
 dump "Done."
diff --git a/build/tools/dev-defaults.sh b/build/tools/dev-defaults.sh
index a728b14..9877af1 100644
--- a/build/tools/dev-defaults.sh
+++ b/build/tools/dev-defaults.sh
@@ -28,6 +28,9 @@
 # Location of the libportable sources, relative to the NDK root directory
 LIBPORTABLE_SUBDIR=sources/android/libportable
 
+# Location of the gccunwind sources, relative to the NDK root directory
+GCCUNWIND_SUBDIR=sources/android/gccunwind
+
 # Location of the compiler-rt sources, relative to the NDK root directory
 COMPILER_RT_SUBDIR=sources/android/compiler-rt
 
diff --git a/build/tools/package-release.sh b/build/tools/package-release.sh
index 0f5ee9f..bc7e169 100755
--- a/build/tools/package-release.sh
+++ b/build/tools/package-release.sh
@@ -419,6 +419,7 @@
         done
         unpack_prebuilt libportable-libs-$ABI "$REFERENCE"
         unpack_prebuilt compiler-rt-libs-$ABI "$REFERENCE"
+        unpack_prebuilt libgccunwind-libs-$ABI "$REFERENCE"
     done
     for ABI in $UNKNOWN_ABIS; do
         unpack_prebuilt stlport-libs-$ABI "$REFERENCE"
@@ -515,6 +516,15 @@
         else
             echo "WARNING: Could not find compiler-rt source tree!"
         fi
+
+        if [ -d "$DSTDIR/$GCCUNWIND_SUBDIR" ]; then
+            GCCUNWIND_ABIS=$PREBUILT_ABIS
+            for GCCUNWIND_ABI in $GCCUNWIND_ABIS; do
+                copy_prebuilt "$GCCUNWIND_SUBDIR/libs/$GCCUNWIND_ABI" "$GCCUNWIND_SUBDIR/libs"
+            done
+        else
+            echo "WARNING: Could not find libgccunwind source tree!"
+        fi
     else
         # Unpack toolchains
         for TC in $TOOLCHAINS; do
diff --git a/sources/android/.gitignore b/sources/android/.gitignore
index 237ab18..526b8fa 100644
--- a/sources/android/.gitignore
+++ b/sources/android/.gitignore
@@ -1,2 +1,3 @@
 compiler-rt/libs
 libportable/libs
+gccunwind/libs