Build/package libportable.a

Change-Id: If3e5941373ec42bcf4c99a49794f5b7406fd074e
diff --git a/build/tools/build-libportable.sh b/build/tools/build-libportable.sh
new file mode 100755
index 0000000..6211b63
--- /dev/null
+++ b/build/tools/build-libportable.sh
@@ -0,0 +1,175 @@
+#!/bin/sh
+#
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#  This shell script is used to rebuild the libportable binaries from
+#  their sources. It requires a working NDK installation.
+#
+
+# include common function and variable definitions
+. `dirname $0`/prebuilt-common.sh
+. `dirname $0`/builder-funcs.sh
+
+PROGRAM_PARAMETERS=""
+
+PROGRAM_DESCRIPTION=\
+"Rebuild libportable.a for the Android NDK.
+
+This script builds libportable.a for app link with portable header.
+
+This requires a platform.git/development and a temporary NDK installation
+containing platforms and toolchain binaries for all target architectures.
+
+By default, this will try with the current NDK directory, unless
+you use the --ndk-dir=<path> option.
+
+The output will be placed in appropriate sub-directories of
+<ndk>/$LIBPORTABLE_SUBDIR, but you can override this with the --out-dir=<path>
+option.
+"
+
+PACKAGE_DIR=
+register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
+
+NDK_DIR=
+register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build."
+
+BUILD_DIR=
+OPTION_BUILD_DIR=
+register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir."
+
+OUT_DIR=
+register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly."
+
+ABIS="$PREBUILT_ABIS"
+register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."
+
+NO_MAKEFILE=
+register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"
+
+VISIBLE_LIBPORTABLE_STATIC=
+register_var_option "--visible-libportable-static" VISIBLE_LIBPORTABLE_STATIC "Do not use hidden visibility for libportable.a"
+
+register_jobs_option
+
+extract_parameters "$@"
+
+ABIS=$(commas_to_spaces $ABIS)
+
+# Handle NDK_DIR
+if [ -z "$NDK_DIR" ] ; then
+    NDK_DIR=$ANDROID_NDK_ROOT
+    log "Auto-config: --ndk-dir=$NDK_DIR"
+else
+    if [ ! -d "$NDK_DIR" ] ; then
+        echo "ERROR: NDK directory does not exists: $NDK_DIR"
+        exit 1
+    fi
+fi
+
+if [ -z "$OPTION_BUILD_DIR" ]; then
+    BUILD_DIR=$NDK_TMPDIR/build-libportable
+else
+    BUILD_DIR=$OPTION_BUILD_DIR
+fi
+mkdir -p "$BUILD_DIR"
+fail_panic "Could not create build directory: $BUILD_DIR"
+
+# Location of the libportable source tree base
+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_CXXFLAGS="-fno-exceptions -fno-rtti"
+LIBPORTABLE_LDFLAGS=""
+
+# If the --no-makefile flag is not used, we're going to put all build
+# commands in a temporary Makefile that we will be able to invoke with
+# -j$NUM_JOBS to build stuff in parallel.
+#
+if [ -z "$NO_MAKEFILE" ]; then
+    MAKEFILE=$BUILD_DIR/Makefile
+else
+    MAKEFILE=
+fi
+
+# build_libportable_for_abi
+# $1: ABI
+# $2: build directory
+# $3: (optional) installation directory
+build_libportable_libs_for_abi ()
+{
+    local ARCH BINPREFIX
+    local ABI=$1
+    local BUILDDIR="$2"
+    local DSTDIR="$3"
+    local SRC OBJ OBJECTS CFLAGS CXXFLAGS
+
+    mkdir -p "$BUILDDIR"
+
+    # If the output directory is not specified, use default location
+    if [ -z "$DSTDIR" ]; then
+        DSTDIR=$NDK_DIR/$LIBPORTABLE_SUBDIR/libs/$ABI
+    fi
+
+    mkdir -p "$DSTDIR"
+
+    builder_begin_android $ABI "$BUILDDIR" "$LLVM_VERSION" "$MAKEFILE"
+    builder_set_srcdir "$LIBPORTABLE_SRCDIR"
+    builder_set_dstdir "$DSTDIR"
+
+    if [ -z "$VISIBLE_LIBLIBPORTABLE_STATIC" ]; then
+        builder_cflags "$LIBPORTABLE_CFLAGS -fvisibility=hidden -fvisibility-inlines-hidden"
+    else
+        builder_cflags "$LIBPORTABLE_CFLAGS"
+    fi
+    builder_ldflags "$LIBPORTABLE_LDFLAGS"
+    builder_sources $LIBPORTABLE_SOURCES
+
+    log "Building $DSTDIR/libportable.a"
+    builder_static_library libportable
+
+    builder_end
+}
+
+for ABI in $ABIS; do
+    # List of sources to compile
+    ARCH=$(convert_abi_to_arch $ABI)
+    LIBPORTABLE_SRCDIR=$LIBPORTABLE_SRCDIR_BASE/arch-$ARCH
+    LIBPORTABLE_SOURCES=$(cd $LIBPORTABLE_SRCDIR && ls *.[cS])
+    build_libportable_libs_for_abi $ABI "$BUILD_DIR/$ABI"
+done
+
+# If needed, package files into tarballs
+if [ -n "$PACKAGE_DIR" ] ; then
+    for ABI in $ABIS; do
+        FILES="$FILES $LIBPORTABLE_SUBDIR/libs/$ABI/libportable.a"
+        PACKAGE="$PACKAGE_DIR/libportable-libs-$ABI.tar.bz2"
+        log "Packaging: $PACKAGE"
+        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
+        fail_panic "Could not package $ABI libportable binaries!"
+        dump "Packaging: $PACKAGE"
+    done
+fi
+
+if [ -z "$OPTION_BUILD_DIR" ]; then
+    log "Cleaning up..."
+    rm -rf $BUILD_DIR
+else
+    log "Don't forget to cleanup: $BUILD_DIR"
+fi
+
+log "Done!"
diff --git a/build/tools/build-target-prebuilts.sh b/build/tools/build-target-prebuilts.sh
index 4d17e3d..c4afd05 100755
--- a/build/tools/build-target-prebuilts.sh
+++ b/build/tools/build-target-prebuilts.sh
@@ -104,6 +104,10 @@
 run $BUILDTOOLS/build-gnu-libstdc++.sh $FLAGS "$SRC_DIR"
 fail_panic "Could not build gnustl!"
 
+dump "Building $ABIS libportable binaries..."
+run $BUILDTOOLS/build-libportable.sh $FLAGS
+fail_panic "Could not build libportable!"
+
 if [ "$PACKAGE_DIR" ]; then
     dump "Done, see $PACKAGE_DIR"
 else
diff --git a/build/tools/builder-funcs.sh b/build/tools/builder-funcs.sh
index 7a6bb42..6204088 100644
--- a/build/tools/builder-funcs.sh
+++ b/build/tools/builder-funcs.sh
@@ -268,6 +268,11 @@
                 cc=$_BUILD_CXX
                 cflags="$cflags $_BUILD_CXXFLAGS"
                 ;;
+            *.S|*.s)
+                obj=${obj%%.$obj}
+                text="ASM"
+                cc=$_BUILD_CC
+                ;;
             *)
                 echo "Unknown source file extension: $obj"
                 exit 1
diff --git a/build/tools/dev-cleanup.sh b/build/tools/dev-cleanup.sh
index c11f315..2c123dc 100755
--- a/build/tools/dev-cleanup.sh
+++ b/build/tools/dev-cleanup.sh
@@ -45,6 +45,7 @@
 for VERSION in $DEFAULT_GCC_VERSION_LIST; do
     rm -rf $DIR/$GNUSTL_SUBDIR/$VERSION
 done
+rm -rf $DIR/$LIBPORTABLE/libs
 
 rm -f $DIR/ndk-stack*
 
diff --git a/build/tools/dev-defaults.sh b/build/tools/dev-defaults.sh
index f8f3043..d569ab4 100644
--- a/build/tools/dev-defaults.sh
+++ b/build/tools/dev-defaults.sh
@@ -21,6 +21,9 @@
 # root directory.
 GNUSTL_SUBDIR=sources/cxx-stl/gnu-libstdc++
 
+# Location of the libportable sources, relative to the NDK root directory
+LIBPORTABLE_SUBDIR=sources/android/libportable
+
 # The date to use when downloading toolchain sources from AOSP servers
 # Leave it empty for tip of tree.
 TOOLCHAIN_GIT_DATE=now
diff --git a/build/tools/package-release.sh b/build/tools/package-release.sh
index 0479ad8..40cd83d 100755
--- a/build/tools/package-release.sh
+++ b/build/tools/package-release.sh
@@ -406,6 +406,7 @@
         for VERSION in $DEFAULT_GCC_VERSION_LIST; do
             unpack_prebuilt gnu-libstdc++-libs-$VERSION-$ABI "$REFERENCE"
         done
+        unpack_prebuilt libportable-libs-$ABI "$REFERENCE"
     done
 fi
 
@@ -467,6 +468,15 @@
                 copy_prebuilt "$GNUSTL_SUBDIR/$VERSION/libs/$STL_ABI" "$GNUSTL_SUBDIR/$VERSION/libs"
             done
         done
+
+        if [ -d "$DSTDIR/$LIBPORTABLE_SUBDIR" ]; then
+            LIBPORTABLE_ABIS=$PREBUILT_ABIS
+            for LIBPORTABLE_ABI in $LIBPORTABLE_ABIS; do
+                copy_prebuilt "$LIBPORTABLE_SUBDIR/libs/$LIBPORTABLE_ABI" "$LIBPORTABLE_SUBDIR/libs"
+            done
+        else
+            echo "WARNING: Could not find libportable source tree!"
+        fi
     else
         # Unpack toolchains
         for TC in $TOOLCHAINS; do