#!/bin/bash
#
# 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 a wrapper to launch the NDK build from the
#  command-line inside an application project path.
#
#  Typical usage is:
#
#     cd $PROJECT_PATH
#     ndk-build
#
#  Assuming that the Android NDK root path is in your PATH. However,
#  you can also invoke it directly as:
#
#     $NDK_ROOT/ndk-build
#
#  This really is a tiny wrapper around GNU Make.
#

# Ensure we get the full path of this script's directory
# this is needed if the caller uses the -C <path> GNU Make
# option, as in:
#
#    cd ndk
#    ./ndk-build -C <project-path>
#
PROGDIR=`dirname $0`
PROGDIR=`cd $PROGDIR && pwd`

# If GNUMAKE is defined, check that it points to a valid file
if [ -n "$GNUMAKE" ] ; then
    ABS_GNUMAKE=`which $GNUMAKE 2> /dev/null`
    if [ $? != 0 ] ; then
        echo "ERROR: Your GNUMAKE variable is defined to an invalid name: $GNUMAKE"
        echo "Please fix it to point to a valid make executable (e.g. /usr/bin/make)"
        exit 1
    fi
    GNUMAKE="$ABS_GNUMAKE"
else
    # Otherwise, use 'make' and check that it is available
    GNUMAKE=`which make 2> /dev/null`
    if [ $? != 0 ] ; then
        echo "ERROR: Cannot find 'make' program. Please install Cygwin make package"
        echo "or define the GNUMAKE variable to point to it."
        exit 1
    fi
fi

# On Windows, when running under cygwin, check that we are
# invoking a cygwin-compatible GNU Make binary. It is unfortunately
# common for app developers to have another non-cygwin compatible
#
if [ "$OSTYPE" = "cygwin" ] ; then
    GNUMAKE=`cygpath -u $GNUMAKE`
    PROGDIR_MIXED=`cygpath -m $PROGDIR`
    CYGWIN_GNUMAKE=`$GNUMAKE -f "$PROGDIR_MIXED/build/core/check-cygwin-make.mk" 2>&1`
    if [ $? != 0 ] ; then
        echo "ERROR: You are using a non-Cygwin compatible Make program."
        echo "Currently using: `cygpath -m $GNUMAKE`"
        echo ""
        echo "To solve the issue, follow these steps:"
        echo ""
        echo "1. Ensure that the Cygwin 'make' package is installed."
        echo "   NOTE: You will need GNU Make 3.81 or later!"
        echo ""
        echo "2. Define the GNUMAKE environment variable to point to it, as in:"
        echo ""
        echo "     export GNUMAKE=/usr/bin/make"
        echo ""
        echo "3. Call 'ndk-build' again."
        echo ""
        exit 1
    fi
fi

# Detect if ANALYZE option specified in command line
# It can be specified more than once; in this case last
# occurence overrides all previous.
ANALYZE=
for param in "$@"; do
    case $param in
        ANALYZE=1|ANALYZE=yes|ANALYZE=true )
            ANALYZE=1
            ;;
        ANALYZE=0|ANALYZE=no|ANALYZE=false )
            ANALYZE=
            ;;
        * )
            ;;
    esac
done

# Hook point for user; if someone has custom scan-build it
# might be specified in SCAN_BUILD environment variable or
# in command line
for param in "$@"; do
    case $param in
        SCAN_BUILD=* )
            eval $param
            ;;
        * )
            ;;
    esac
done
[ -z "$SCAN_BUILD" ] && SCAN_BUILD=scan-build

if [ -n "$ANALYZE" ]; then
    # Use clang static analyzer

    . $PROGDIR/build/core/ndk-common.sh

    force_32bit_binaries
    compute_host_tag

    # Detect working directory
    WRKDIR=
    for param in "$@"; do
        if [ -n "$WRKDIR" ]; then
            WRKDIR=$param
            break
        fi
        if [ "$param" = "-C" ]; then
            WRKDIR=1
        fi
    done

    case "$WRKDIR" in
        ""|1 )
            WRKDIR=$(pwd)
            ;;
        /* ) ;;
        * )
            WRKDIR=$(pwd)/$WRKDIR
            ;;
    esac

    HOST_SED=$PROGDIR/prebuilt/$HOST_TAG/bin/sed$HOST_EXE
    [ -f $HOST_SED ] && HOST_SED=sed

    TOOLCHAIN_ABI=
    TOOLCHAIN_VERSION=
    if [ -f $WRKDIR/jni/Application.mk ]; then
        TOOLCHAIN_ABI=$(grep '^\s*APP_ABI\s*:=\s*' $WRKDIR/jni/Application.mk | $HOST_SED 's/^\s*APP_ABI\s*:=\s*\(.*\)$/\1/')
        TOOLCHAIN_VERSION=$(grep '^\s*APP_TOOLCHAIN_VERSION\s*:=\s*' $WRKDIR/jni/Application.mk | $HOST_SED 's/^\s*APP_TOOLCHAIN_VERSION\s*:=\s*\(\S*\).*$/\1/')
    fi
    [ -z "$TOOLCHAIN_ABI" ] && TOOLCHAIN_ABI="armeabi"
    [ "$TOOLCHAIN_ABI" = "all" ] && TOOLCHAIN_ABI="armeabi armeabi-v7a x86"
    [ -z "$TOOLCHAIN_VERSION" ] && TOOLCHAIN_VERSION="4.4.3"

    get_toolchain_name_for_abi()
    {
        case $1 in
            armeabi* )
                echo "arm-linux-androideabi"
                ;;
            x86 )
                echo "x86"
                ;;
        esac
    }

    get_toolchain_prefix_for_abi()
    {
        case $1 in
            armeabi* )
                echo "arm-linux-androideabi"
                ;;
            x86 )
                echo "i686-android-linux"
                ;;
        esac
    }

    VERBOSE=
    for p in "$@"; do
        case $p in
            V=1 )
                VERBOSE=-v
                ;;
            V=0 )
                VERBOSE=
                ;;
            * )
                ;;
        esac
    done

    for ABI in $TOOLCHAIN_ABI; do
        TOOLCHAIN_NAME=$(get_toolchain_name_for_abi $ABI)
        TOOLCHAIN_PREFIX=$(get_toolchain_prefix_for_abi $ABI)
        TOOLCHAIN_PATH=$PROGDIR/toolchains/$TOOLCHAIN_NAME-$TOOLCHAIN_VERSION/prebuilt/$HOST_TAG/bin
        SCAN_BUILD_PARAMS="--use-cc $TOOLCHAIN_PREFIX-gcc --use-c++ $TOOLCHAIN_PREFIX-g++ --status-bugs $VERBOSE"
        PATH=$TOOLCHAIN_PATH:$PATH $SCAN_BUILD $SCAN_BUILD_PARAMS $GNUMAKE -f $PROGDIR/build/core/build-local.mk NDK_APP_ABI=$ABI "$@"
        [ $? -ne 0 ] && exit $?
    done
else
    $GNUMAKE -f $PROGDIR/build/core/build-local.mk "$@"
fi
