#!/bin/sh
#
# Copyright (C) 2010 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 prebuilt STLport binaries from
#  their sources. It requires a working NDK installation.
#

# include common function and variable definitions
. `dirname $0`/prebuilt-common.sh

# Location of the  test project we use to force the rebuild.
# This is relative to the current NDK directory.
PROJECT_SUBDIR=tests/build/prebuild-stlport

PROGRAM_PARAMETERS=""

PROGRAM_DESCRIPTION=\
"Rebuild the prebuilt STLport binaries for the Android NDK.

This script is called when packaging a new NDK release. It will simply
rebuild the STLport static and shared libraries from sources.

This requires 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>/$STLPORT_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="$STLPORT_ABIS"
register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."

JOBS="$BUILD_NUM_CPUS"
register_var_option "-j<number>" JOBS "Use <number> build jobs in parallel"

NO_MAKEFILE=
register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"

NUM_JOBS=$BUILD_NUM_CPUS
register_var_option "-j<number>" NUM_JOBS "Run <number> build jobs in parallel"

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-stlport
else
    BUILD_DIR=$OPTION_BUILD_DIR
fi
mkdir -p "$BUILD_DIR"
fail_panic "Could not create build directory: $BUILD_DIR"

# Location of the STLPort source tree
STLPORT_SRCDIR=$ANDROID_NDK_ROOT/$STLPORT_SUBDIR

# Compiler flags we want to use
STLPORT_CFLAGS="-DGNU_SOURCE -fPIC -O2 -I$STLPORT_SRCDIR/stlport -DANDROID -D__ANDROID__"
STLPORT_CFLAGS=$STLPORT_CFLAGS" -I$ANDROID_NDK_ROOT/sources/cxx-stl/system/include"
STLPORT_CXXFLAGS="-fuse-cxa-atexit -fno-exceptions -fno-rtti"

# List of sources to compile
STLPORT_SOURCES=\
"src/dll_main.cpp \
src/fstream.cpp \
src/strstream.cpp \
src/sstream.cpp \
src/ios.cpp \
src/stdio_streambuf.cpp \
src/istream.cpp \
src/ostream.cpp \
src/iostream.cpp \
src/codecvt.cpp \
src/collate.cpp \
src/ctype.cpp \
src/monetary.cpp \
src/num_get.cpp \
src/num_put.cpp \
src/num_get_float.cpp \
src/num_put_float.cpp \
src/numpunct.cpp \
src/time_facets.cpp \
src/messages.cpp \
src/locale.cpp \
src/locale_impl.cpp \
src/locale_catalog.cpp \
src/facets_byname.cpp \
src/complex.cpp \
src/complex_io.cpp \
src/complex_trig.cpp \
src/string.cpp \
src/bitset.cpp \
src/allocators.cpp \
src/c_locale.c \
src/cxa.c"

# 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
    TAB=$(echo " " | tr ' ' '\t')
    MAKEFILE=$BUILD_DIR/Makefile
    log "Creating temporary build Makefile: $MAKEFILE"
    rm -f $MAKEFILE &&
    echo "# Auto-generated by $0 - do not edit!" > $MAKEFILE
    echo ".PHONY: all" >> $MAKEFILE
    echo "all:" >> $MAKEFILE
else
    MAKEFILE=
fi

# Build stlport as a static library
# $1: ABI name
# $2: Build directory
# $3: Output directory (optional)

make_command ()
{
    if [ -z "$MAKEFILE" ]; then
        if [ "$VERBOSE2" = "yes" ]; then
            echo "$@"
        fi
        $@
    else
        echo "${TAB}${HIDE}$@" >> $MAKEFILE
    fi
}

# HIDE is used to hide the Makefile output, unless --verbose --verbose
# is used.
if [ "$VERBOSE2" = "yes" ]; then
    HIDE=""
else
    HIDE=@
fi

make_log ()
{
    if [ "$VERBOSE" = "yes" ]; then
        echo "${TAB}${HIDE}echo $@" >> $MAKEFILE
    fi
}

build_stlport_libs_for_abi ()
{
    local ARCH BINPREFIX SYSROOT
    local ABI=$1
    local BUILDDIR="$2"
    local DSTDIR="$3"
    local SRC OBJ OBJECTS CFLAGS CXXFLAGS

    log "stlport_static $ABI"
    mkdir -p "$BUILDDIR"

    ARCH=$(convert_abi_to_arch $ABI)
    BINPREFIX=$NDK_DIR/$(get_default_toolchain_binprefix_for_arch $ARCH)
    SYSROOT=$NDK_DIR/$(get_default_platform_sysroot_for_arch $ARCH)

    CFLAGS=$STLPORT_CFLAGS
    CXXFLAGS=$STLPORT_CXXFLAGS
    case $ABI in
        armeabi)
            CFLAGS=$CFLAGS" -mthumb"
            ;;
        armeabi-v7a)
            CFLAGS=$CFLAGS" -march=armv7-a -mfloat-abi=softfp"
            ;;
    esac
    # If the output directory is not specified, use default location
    if [ -z "$DSTDIR" ]; then
        DSTDIR=$NDK_DIR/$STLPORT_SUBDIR/libs/$ABI
    fi
    mkdir -p $DSTDIR
    OBJECTS=
    for SRC in $STLPORT_SOURCES; do
        OBJ=$(basename "$SRC")
        OBJ=${OBJ%%.cpp}
        OBJ=${OBJ%%.c}
        OBJ="$BUILDDIR/$OBJ.o"
        if [ "$MAKEFILE" ]; then
            echo "$OBJ: $STLPORT_SRCDIR/$SRC" >> $MAKEFILE
            make_log "$ABI C++: $SRC"
        else
            log "$ABI C++: $SRC"
        fi
        make_command ${BINPREFIX}g++ -c -o "$OBJ" "$STLPORT_SRCDIR/$SRC" $CFLAGS --sysroot="$SYSROOT" $CXXFLAGS
        fail_panic "Could not compile $SRC"
        OBJECTS=$OBJECTS" $OBJ"
    done

    if [ "$MAKEFILE" ]; then
        echo "all: $DSTDIR/libstlport_static.a" >> $MAKEFILE
        echo "$DSTDIR/libstlport_static.a: $OBJECTS" >> $MAKEFILE
        make_log "$ABI Archive: stlport_static"
    else
        log "$ABI Archive: stlport_static"
    fi
    make_command ${BINPREFIX}ar crs "$DSTDIR/libstlport_static.a" $OBJECTS
    fail_panic "Could not archive $ABI stlport_static objects!"

    if [ "$MAKEFILE" ]; then
        echo "all: $DSTDIR/libstlport_shared.so" >> $MAKEFILE
        echo "$DSTDIR/libstlport_shared.so: $OBJECTS" >> $MAKEFILE
        make_log "$ABI SharedLibrary: stlport_shared"
    else
        log "$ABI SharedLibrary: stlport_shared"
    fi
    CRTBEGIN_SO_O=$SYSROOT/usr/lib/crtbegin_so.o
    CRTEND_SO_O=$SYSROOT/usr/lib/crtend_so.o
    if [ ! -f "$CRTBEGIN_SO_O" ]; then
        CRTBEGIN_SO_O=$SYSROOT/usr/lib/crtbegin_dynamic.o
    fi
    if [ ! -f "$CRTEND_SO_O" ]; then
        CRTEND_SO_O=$SYSROOT/usr/lib/crtend_android.o
    fi
    make_command ${BINPREFIX}g++ \
        -nostdlib -Wl,-soname,libstlport_shared.so \
        -Wl,-shared,-Bsymbolic \
        --sysroot="$SYSROOT" \
        $CRTBEGIN_SO_O \
        $OBJECTS \
        -lgcc \
        -lc -lm \
        $CRTEND_SO_O \
        -o $DSTDIR/libstlport_shared.so
    fail_panic "Could not create $ABI shared library libstlport_shared!"
}


for ABI in $ABIS; do
    build_stlport_libs_for_abi $ABI "$BUILD_DIR/$ABI"
done

if [ "$MAKEFILE" ]; then
    make -j$NUM_JOBS -f $MAKEFILE
    fail_panic "Could not build STLport libraries!"
fi

# If needed, package files into tarballs
if [ -n "$PACKAGE_DIR" ] ; then
    for ABI in $ABIS; do
        FILES=""
        for LIB in libstlport_static.a libstlport_shared.so; do
            FILES="$FILES $STLPORT_SUBDIR/libs/$ABI/$LIB"
        done
        PACKAGE="$PACKAGE_DIR/stlport-libs-$ABI.tar.bz2"
        log "Packaging: $PACKAGE"
        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
        fail_panic "Could not package $ABI STLport 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!"
