Switch to use gccunwind library.

Change-Id: Ia622aabff6e1909a7cc49b3d649a426801a6534f
diff --git a/build/tools/build-on-device-toolchain.sh b/build/tools/build-on-device-toolchain.sh
index aadf2da..67325d7 100755
--- a/build/tools/build-on-device-toolchain.sh
+++ b/build/tools/build-on-device-toolchain.sh
@@ -96,9 +96,12 @@
 run cp -f $NDK_DIR/$LIBPORTABLE_SUBDIR/libs/$ABI/libportable.a $OUT_SYSROOT/usr/lib
 run cp -f $NDK_DIR/$LIBPORTABLE_SUBDIR/libs/$ABI/libportable.wrap $OUT_SYSROOT/usr/lib
 
-dump "Copy $ABI libportable library"
+dump "Copy $ABI compiler-rt library"
 run cp -f $NDK_DIR/$COMPILER_RT_SUBDIR/libs/$ABI/libcompiler_rt_static.a $OUT_SYSROOT/usr/lib
 
+dump "Copy $ABI gccunwind library"
+run cp -f $NDK_DIR/$GCCUNWIND_SUBDIR/libs/$ABI/libgccunwind.a $OUT_SYSROOT/usr/lib
+
 if [ "$TESTING" = "yes" ]; then
   dump "Copy stuff for testing"
   run cp -f $NDK_DIR/$GNUSTL_SUBDIR/$GCC_TOOLCHAIN_VERSION/libs/$ABI/libsupc++.a $OUT_SYSROOT/usr/lib
diff --git a/build/tools/make-standalone-toolchain.sh b/build/tools/make-standalone-toolchain.sh
index eca5dd0..8819bb3 100755
--- a/build/tools/make-standalone-toolchain.sh
+++ b/build/tools/make-standalone-toolchain.sh
@@ -434,6 +434,7 @@
     cp -a $NDK_DIR/$LIBPORTABLE_SUBDIR/libs/$ABI/* $TMPDIR/sysroot/usr/lib
     cp -a $NDK_DIR/$GABIXX_SUBDIR/libs/$ABI/* $TMPDIR/sysroot/usr/lib
     cp -a $NDK_DIR/$COMPILER_RT_SUBDIR/libs/$ABI/* $TMPDIR/sysroot/usr/lib
+    cp -a $NDK_DIR/$GCCUNWIND_SUBDIR/libs/$ABI/* $TMPDIR/sysroot/usr/lib
 fi
 
 if [ "$ARCH_LIB" != "$ARCH" ]; then
diff --git a/tests/abcc/jni/device/Abcc_device.cpp b/tests/abcc/jni/device/Abcc_device.cpp
index 62a8ea3..ea1f972 100644
--- a/tests/abcc/jni/device/Abcc_device.cpp
+++ b/tests/abcc/jni/device/Abcc_device.cpp
@@ -191,13 +191,12 @@
 
   cmd = " @" + mSysroot + "/usr/lib/libportable.wrap " + mSysroot + "/usr/lib/libportable.a";
   cmd += " " + mSysroot + "/usr/lib/libcompiler_rt_static.a";
-  cmd += " " + mSysroot + "/usr/lib/libgabi++_shared.so";
+  cmd += " " + mSysroot + "/usr/lib/libgccunwind.a";
   cmd += " -ldl";
   mExecutableToolsPath[(unsigned)CMD_LINK_RUNTIME] = cmd;
 }
 
 void DeviceBitcodeCompiler::copyRuntime(const BitcodeInfo &info) {
-  runCmd(std::string("cp -f ") + mSysroot + "/usr/lib/libgabi++_shared.so " + mWorkingDir + "/libgabi++_shared.so");
 
   std::stringstream ss(info.mLDLibsStr);
   std::string deplib;
diff --git a/tests/abcc/jni/host/Abcc_host.cpp b/tests/abcc/jni/host/Abcc_host.cpp
index 57ca00c..9fb9084 100644
--- a/tests/abcc/jni/host/Abcc_host.cpp
+++ b/tests/abcc/jni/host/Abcc_host.cpp
@@ -25,6 +25,7 @@
                                          const std::string &platform)
   : BitcodeCompiler(abi, sysroot, working_dir), mIn(input), mOut(output),
     mNDKDir(""), mPlatform(platform) {
+  initRuntimePath();
 }
 
 HostBitcodeCompiler::HostBitcodeCompiler(const std::string &abi, const std::string &sysroot, const std::string &ndk_dir, const std::string &toolchain_bin,
@@ -32,6 +33,25 @@
                                          const std::string &platform)
   : BitcodeCompiler(abi, sysroot, working_dir), mIn(input), mOut(output),
     mNDKDir(ndk_dir), mPlatform(platform), mToolchainBinPath(toolchain_bin) {
+  initRuntimePath();
+}
+
+void HostBitcodeCompiler::initRuntimePath() {
+  mRuntimePath.clear();
+
+  mRuntimePath.insert(std::make_pair("gabi++_static", getGAbixxPath() + "/libgabi++_static.a"));
+  mRuntimePath.insert(std::make_pair("gabi++_shared", getGAbixxPath() + "/libgabi++_shared.so"));
+  mRuntimePath.insert(std::make_pair("compiler_rt_static", getCompilerRTPath() + "/libcompiler_rt_static.a"));
+  mRuntimePath.insert(std::make_pair("portable", getLibPortablePath() + "/libportable.a"));
+  mRuntimePath.insert(std::make_pair("portable.wrap", getLibPortablePath() + "/libportable.wrap"));
+  mRuntimePath.insert(std::make_pair("gccunwind", getGCCUnwindPath() + "/libgccunwind.a"));
+}
+
+const std::string HostBitcodeCompiler::getRuntimePath(const std::string &libname) {
+  if (mRuntimePath.count(libname)) {
+    return mRuntimePath.find(libname)->second;
+  }
+  return "";
 }
 
 int HostBitcodeCompiler::parseLDFlags(BitcodeInfo &info, const std::string &orig_str) {
@@ -72,8 +92,14 @@
     // Parse -lxxx
     if (str.size() > 2 &&
         str.substr(0, 2) == "-l") {
-      info.mLDLibs.push_back(str.substr(2));
-      info.mLDLibsStr += " " + str;
+      std::string runtime_path = getRuntimePath(str.substr(2));
+      if (!runtime_path.empty()) {
+        info.mLDLibsStr += " " + runtime_path;
+      }
+      else {
+        info.mLDLibs.push_back(str.substr(2));
+        info.mLDLibsStr += " " + str;
+      }
       continue;
     }
 
@@ -109,9 +135,9 @@
   cmd += " -L" + mSysroot + "/usr/lib";
   mExecutableToolsPath[(unsigned)CMD_LINK] = cmd;
 
-  cmd = " @" + getLibPortablePath() + "/libportable.wrap " + getLibPortablePath() + "/libportable.a";
-  cmd += " " + getCompilerRTPath() + "/libcompiler_rt_static.a";
-  cmd += " " + getGAbixxPath() + "/libgabi++_shared.so";
+  cmd = " @" + getRuntimePath("portable.wrap") + " " + getRuntimePath("portable");
+  cmd += " " + getRuntimePath("compiler_rt_static");
+  cmd += " " + getRuntimePath("gccunwind");
   cmd += " -ldl";
   mExecutableToolsPath[(unsigned)CMD_LINK_RUNTIME] = cmd;
 }
@@ -151,3 +177,10 @@
   else
     return mSysroot + "/usr/lib";
 }
+
+const std::string HostBitcodeCompiler::getGCCUnwindPath() const {
+  if (!mNDKDir.empty())
+    return std::string(mNDKDir) + "/sources/android/gccunwind/libs/" + (const char*)mAbi;
+  else
+    return mSysroot + "/usr/lib";
+}
diff --git a/tests/abcc/jni/host/Abcc_host.h b/tests/abcc/jni/host/Abcc_host.h
index f0caf05..0cac7c9 100644
--- a/tests/abcc/jni/host/Abcc_host.h
+++ b/tests/abcc/jni/host/Abcc_host.h
@@ -39,6 +39,7 @@
   std::string mNDKDir;  // empty string if standalone mode
   std::string mPlatform;
   std::string mToolchainBinPath;  // Used at ndk mode to prevent tedious path issue
+  std::map<std::string, std::string> mRuntimePath; // mappings of library name and full path
 
 public:
   HostBitcodeCompiler(const std::string &abi, const std::string &sysroot,
@@ -56,12 +57,15 @@
   virtual void prepareToolchain();
   virtual void copyRuntime(const BitcodeInfo &info);
   virtual void removeIntermediateFile(const std::string &path);
+  void initRuntimePath();
+  const std::string getRuntimePath(const std::string &libname);
 
 private:
   const std::string getToolchainBinPath() const;
   const std::string getCompilerRTPath() const;
   const std::string getGAbixxPath() const;
   const std::string getLibPortablePath() const;
+  const std::string getGCCUnwindPath() const;
 };
 
 } // namespace abcc
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
index 67d97ae..dfed27a 100755
--- a/tests/run-tests.sh
+++ b/tests/run-tests.sh
@@ -824,17 +824,6 @@
             run $ADB_CMD -s "$DEVICE" shell rm -rf $DSTDIR/abcc_tmp
         fi
 
-        # Prepare gabi++ runtime
-        if [ ! -z "$NDK_ABI_FILTER" ]; then
-            SRCFILE="$NDK/sources/cxx-stl/gabi++/libs/$CPU_ABI/libgabi++_shared.so"
-            run $ADB_CMD -s "$DEVICE" push "$SRCFILE" "$DSTDIR" &&
-            run $ADB_CMD -s "$DEVICE" shell chmod 0755 "$DSTDIR/libgabi++_shared.so"
-            if [ $? != 0 ] ; then
-                dump "ERROR: Could not install $SRCFILE to device $DEVICE!"
-                exit 1
-            fi
-        fi
-
         for SRCFILE in `ls $SRCDIR`; do
             DSTFILE=`basename $SRCFILE`
             echo "$DSTFILE" | grep -q -e '\.so$'