Merge "Create/use libportable.wrap file containing lines of "--wrap=symbol""
diff --git a/build/core/init.mk b/build/core/init.mk
index 3db5ce5..8ef84bf 100644
--- a/build/core/init.mk
+++ b/build/core/init.mk
@@ -506,7 +506,7 @@
 ADD_TOOLCHAIN := $(BUILD_SYSTEM)/add-toolchain.mk
 
 # the list of known values
-NDK_KNOWN_ABIS     := armeabi armeabi-v7a x86 mips
+NDK_KNOWN_ABIS     := armeabi-v7a armeabi x86 mips
 NDK_KNOWN_ARCHS    := arm x86 mips
 _archs := $(sort $(strip $(notdir $(wildcard $(NDK_PLATFORMS_ROOT)/android-*/arch-*))))
 NDK_FOUND_ARCHS    := $(_archs:arch-%=%)
diff --git a/build/core/setup-abi.mk b/build/core/setup-abi.mk
index dcd5bb7..c66deaf 100644
--- a/build/core/setup-abi.mk
+++ b/build/core/setup-abi.mk
@@ -30,14 +30,14 @@
 # For now, handle this with a simple substitution. We may want to implement
 # more general filtering in the future when introducing other ABIs.
 TARGET_PLATFORM_SAVED := $(TARGET_PLATFORM)
-ifeq ($(TARGET_ARCH),x86)
+ifneq ($(filter %x86,$(TARGET_ARCH_ABI)),)
 $(foreach _plat,3 4 5 8,\
     $(eval TARGET_PLATFORM := $$(subst android-$(_plat),android-9,$$(TARGET_PLATFORM)))\
 )
 endif
 
 # The minimal platform for mips is android-9
-ifeq ($(TARGET_ARCH),mips)
+ifneq ($(filter %mips,$(TARGET_ARCH_ABI)),)
 $(foreach _plat,3 4 5 8,\
     $(eval TARGET_PLATFORM := $$(subst android-$(_plat),android-9,$$(TARGET_PLATFORM)))\
 )
diff --git a/build/core/setup-app.mk b/build/core/setup-app.mk
index 87b7b59..5cc5ecc 100644
--- a/build/core/setup-app.mk
+++ b/build/core/setup-app.mk
@@ -49,6 +49,11 @@
     NDK_APP_ABI := armeabi
 endif
 
+NDK_ABI_FILTER := $(strip $(NDK_ABI_FILTER))
+ifdef NDK_ABI_FILTER
+    $(eval $(NDK_ABI_FILTER))
+endif
+
 # If APP_ABI is 'all', then set it to all supported ABIs
 # Otherwise, check that we don't have an invalid value here.
 #
@@ -59,10 +64,15 @@
     _unknown_abis := $(strip $(filter-out $(NDK_ALL_ABIS),$(NDK_APP_ABI)))
     ifneq ($(_unknown_abis),)
         ifeq (1,$(words $(filter-out $(NDK_KNOWN_ARCHS),$(NDK_FOUND_ARCHS))))
-            $(foreach _abi,$(NDK_KNOWN_ABIS),\
-                $(eval _unknown_abis := $(subst $(_abi),,$(_unknown_abis)))\
-            )
-            _unknown_abis_prefix := $(sort $(_unknown_abis))
+            ifneq ($(filter %all,$(_unknown_abis)),)
+                _unknown_abis_prefix := $(_unknown_abis:%all=%)
+                NDK_APP_ABI := $(NDK_KNOWN_ABIS:%=$(_unknown_abis_prefix)%)
+            else
+                $(foreach _abi,$(NDK_KNOWN_ABIS),\
+                    $(eval _unknown_abis := $(subst $(_abi),,$(_unknown_abis)))\
+                )
+                _unknown_abis_prefix := $(sort $(_unknown_abis))
+            endif
             ifeq (1,$(words $(_unknown_abis_prefix)))
                 NDK_APP_ABI := $(subst $(_unknown_abis_prefix),$(filter-out $(NDK_KNOWN_ARCHS),$(NDK_FOUND_ARCHS)),$(NDK_APP_ABI))
             endif
diff --git a/build/tools/build-libportable.sh b/build/tools/build-libportable.sh
index 6211b63..7ea6dfe 100755
--- a/build/tools/build-libportable.sh
+++ b/build/tools/build-libportable.sh
@@ -91,8 +91,8 @@
 LIBPORTABLE_SRCDIR_BASE=$ANDROID_NDK_ROOT/../development/ndk/$LIBPORTABLE_SUBDIR
 
 # Compiler flags we want to use
-LIBPORTABLE_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__"
-LIBPORTABLE_CFLAGS=$LIBPORTABLE_CFLAGS" -I$LIBPORTABLE_SRCDIR_BASE/common/include -DHAS_NO_LOG_H"
+LIBPORTABLE_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__" # ToDo: -ffunction-sections -fdata-sections
+LIBPORTABLE_CFLAGS=$LIBPORTABLE_CFLAGS" -I$LIBPORTABLE_SRCDIR_BASE/common/include -D__HOST__"
 LIBPORTABLE_CXXFLAGS="-fno-exceptions -fno-rtti"
 LIBPORTABLE_LDFLAGS=""
 
@@ -132,7 +132,9 @@
     builder_set_dstdir "$DSTDIR"
 
     if [ -z "$VISIBLE_LIBLIBPORTABLE_STATIC" ]; then
-        builder_cflags "$LIBPORTABLE_CFLAGS -fvisibility=hidden -fvisibility-inlines-hidden"
+        # No -fvisibility-inlines-hidden because it is for C++, and there is
+        # no C++ code in libportable
+        builder_cflags "$LIBPORTABLE_CFLAGS -fvisibility=hidden"
     else
         builder_cflags "$LIBPORTABLE_CFLAGS"
     fi
@@ -143,6 +145,13 @@
     builder_static_library libportable
 
     builder_end
+  # Extract __wrap functions and create a *.wrap file of "--wrap=symbol".
+  # This file will be passed to g++ doing the link in
+  #
+  #    g++ -Wl,@/path/to/libportable.wrap
+  #
+    nm -a $DSTDIR/libportable.a | grep -r __wrap_ | awk '{print $3}' | sed '/^$/d' | \
+        sed 's/^__wrap_//g' | sort | awk '{printf "--wrap=%s\n",$1}' > "$DSTDIR/libportable.wrap"
 }
 
 for ABI in $ABIS; do
@@ -156,7 +165,7 @@
 # If needed, package files into tarballs
 if [ -n "$PACKAGE_DIR" ] ; then
     for ABI in $ABIS; do
-        FILES="$FILES $LIBPORTABLE_SUBDIR/libs/$ABI/libportable.a"
+        FILES="$LIBPORTABLE_SUBDIR/libs/$ABI/libportable.*"
         PACKAGE="$PACKAGE_DIR/libportable-libs-$ABI.tar.bz2"
         log "Packaging: $PACKAGE"
         pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
diff --git a/toolchains/arm-linux-androideabi-clang3.1/setup.mk b/toolchains/arm-linux-androideabi-clang3.1/setup.mk
index 78bcc77..1d71279 100644
--- a/toolchains/arm-linux-androideabi-clang3.1/setup.mk
+++ b/toolchains/arm-linux-androideabi-clang3.1/setup.mk
@@ -54,7 +54,7 @@
     -fstack-protector \
     -no-canonical-prefixes
 
-TARGET_LDFLAGS := \
+TARGET_LDFLAGS += \
     -gcc-toolchain $(call host-path,$(TOOLCHAIN_PREBUILT_ROOT)) \
     -no-canonical-prefixes
 
diff --git a/toolchains/arm-linux-androideabi-clang3.2/setup.mk b/toolchains/arm-linux-androideabi-clang3.2/setup.mk
index acf87b9..786f668 100644
--- a/toolchains/arm-linux-androideabi-clang3.2/setup.mk
+++ b/toolchains/arm-linux-androideabi-clang3.2/setup.mk
@@ -54,7 +54,7 @@
     -fstack-protector \
     -no-canonical-prefixes
 
-TARGET_LDFLAGS := \
+TARGET_LDFLAGS += \
     -gcc-toolchain $(call host-path,$(TOOLCHAIN_PREBUILT_ROOT)) \
     -no-canonical-prefixes
 
diff --git a/toolchains/llvm-3.1/setup.mk b/toolchains/llvm-3.1/setup.mk
index 24e1baf..64be1da 100644
--- a/toolchains/llvm-3.1/setup.mk
+++ b/toolchains/llvm-3.1/setup.mk
@@ -29,6 +29,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-arm/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/armeabi-v7a
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/armeabi-v7a/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/armeabi-v7a/libportable.wrap
 include $(NDK_ROOT)/toolchains/arm-linux-androideabi-clang3.1/setup.mk
 
 else
@@ -38,6 +39,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-arm/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/armeabi
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/armeabi/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/armeabi/libportable.wrap
 include $(NDK_ROOT)/toolchains/arm-linux-androideabi-clang3.1/setup.mk
 
 else
@@ -47,6 +49,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-x86/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/x86
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/x86/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/x86/libportable.wrap
 include $(NDK_ROOT)/toolchains/x86-clang3.1/setup.mk
 
 else
@@ -56,6 +59,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-mips/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/mips
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/mips/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/mips/libportable.wrap
 include $(NDK_ROOT)/toolchains/mipsel-linux-android-clang3.1/setup.mk
 
 else
diff --git a/toolchains/llvm-3.2/setup.mk b/toolchains/llvm-3.2/setup.mk
index 2531ee5..4fd9496 100644
--- a/toolchains/llvm-3.2/setup.mk
+++ b/toolchains/llvm-3.2/setup.mk
@@ -29,6 +29,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-arm/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/armeabi-v7a
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/armeabi-v7a/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/armeabi-v7a/libportable.wrap
 include $(NDK_ROOT)/toolchains/arm-linux-androideabi-clang3.2/setup.mk
 
 else
@@ -38,6 +39,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-arm/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/armeabi
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/armeabi/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/armeabi/libportable.wrap
 include $(NDK_ROOT)/toolchains/arm-linux-androideabi-clang3.2/setup.mk
 
 else
@@ -47,6 +49,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-x86/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/x86
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/x86/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/x86/libportable.wrap
 include $(NDK_ROOT)/toolchains/x86-clang3.2/setup.mk
 
 else
@@ -56,6 +59,7 @@
 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-mips/gdbserver/gdbserver
 NDK_APP_DST_DIR  := $(NDK_APP_PROJECT_PATH)/libs/mips
 TARGET_LDLIBS    := $(NDK_ROOT)/sources/android/libportable/libs/mips/libportable.a $(TARGET_LDLIBS)
+TARGET_LDFLAGS   += -Wl,@$(NDK_ROOT)/sources/android/libportable/libs/mips/libportable.wrap
 include $(NDK_ROOT)/toolchains/mipsel-linux-android-clang3.2/setup.mk
 
 else
diff --git a/toolchains/mipsel-linux-android-clang3.1/setup.mk b/toolchains/mipsel-linux-android-clang3.1/setup.mk
index ab6a9a6..39708b4 100644
--- a/toolchains/mipsel-linux-android-clang3.1/setup.mk
+++ b/toolchains/mipsel-linux-android-clang3.1/setup.mk
@@ -59,7 +59,7 @@
         -fmessage-length=0 \
         -no-canonical-prefixes
 
-TARGET_LDFLAGS := \
+TARGET_LDFLAGS += \
         -gcc-toolchain $(call host-path,$(TOOLCHAIN_PREBUILT_ROOT)) \
         -target $(LLVM_TRIPLE) \
         -no-canonical-prefixes
diff --git a/toolchains/mipsel-linux-android-clang3.2/setup.mk b/toolchains/mipsel-linux-android-clang3.2/setup.mk
index f1d96f3..3947a85 100644
--- a/toolchains/mipsel-linux-android-clang3.2/setup.mk
+++ b/toolchains/mipsel-linux-android-clang3.2/setup.mk
@@ -59,7 +59,7 @@
         -fmessage-length=0 \
         -no-canonical-prefixes
 
-TARGET_LDFLAGS := \
+TARGET_LDFLAGS += \
         -gcc-toolchain $(call host-path,$(TOOLCHAIN_PREBUILT_ROOT)) \
         -target $(LLVM_TRIPLE) \
         -no-canonical-prefixes
diff --git a/toolchains/x86-clang3.1/setup.mk b/toolchains/x86-clang3.1/setup.mk
index dc84db8..82d29a3 100644
--- a/toolchains/x86-clang3.1/setup.mk
+++ b/toolchains/x86-clang3.1/setup.mk
@@ -57,7 +57,7 @@
     $(SYSROOT_INC)/usr/include
 
 # Add and LDFLAGS for the target here
-TARGET_LDFLAGS := \
+TARGET_LDFLAGS += \
     -gcc-toolchain $(call host-path,$(TOOLCHAIN_PREBUILT_ROOT)) \
     -target $(LLVM_TRIPLE) \
     -no-canonical-prefixes
diff --git a/toolchains/x86-clang3.2/setup.mk b/toolchains/x86-clang3.2/setup.mk
index e8fd776..3ca5724 100644
--- a/toolchains/x86-clang3.2/setup.mk
+++ b/toolchains/x86-clang3.2/setup.mk
@@ -57,7 +57,7 @@
     $(SYSROOT_INC)/usr/include
 
 # Add and LDFLAGS for the target here
-TARGET_LDFLAGS := \
+TARGET_LDFLAGS += \
     -gcc-toolchain $(call host-path,$(TOOLCHAIN_PREBUILT_ROOT)) \
     -target $(LLVM_TRIPLE) \
     -no-canonical-prefixes