#!/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 -P)
ANDROID_NDK_ROOT=$PROGDIR/..

# Unset PYTHONPATH and PYTHONHOME to prevent the user's environment from
# affecting the Python that we invoke.
# See https://github.com/googlesamples/vulkan-basic-samples/issues/25
unset PYTHONHOME
unset PYTHONPATH

# Check if absolute NDK path contain space
#
case "$PROGDIR" in
    *\ *) echo "ERROR: NDK path cannot contain space"
          exit 1
        ;;
esac

# If NDK_LOG is set to 1 or true in the environment, or the command-line
# then enable log messages below
if [ -z "$NDK_LOG" ]; then
  NDK_LOG=0
fi

if [ -z "$NDK_ANALYZE" ]; then
  NDK_ANALYZE=0
fi

PROJECT_PATH=
PROJECT_PATH_NEXT=
for opt; do
    if [ -z "$PROJECT_PATH" ] && [ "$PROJECT_PATH_NEXT" = "yes" ] ; then
        PROJECT_PATH=$opt
        PROJECT_PATH_NEXT=
    else
        case $opt in
          NDK_LOG=1|NDK_LOG=true)
            NDK_LOG=1
            ;;
          NDK_LOG=*)
            NDK_LOG=0
            ;;
          NDK_ANALYZE=1|NDK_ANALYZE=true)
            NDK_ANALYZE=1
            ;;
          NDK_ANALYZE=*)
            NDK_ANALYZE=0
            ;;
          -C)
            PROJECT_PATH_NEXT="yes"
            ;;
        esac
    fi
done

if [ "$NDK_LOG" = "true" ]; then
  NDK_LOG=1
fi

if [ "$NDK_ANALYZE" = "true" ]; then
  NDK_ANALYZE=1
fi

if [ "$NDK_LOG" = "1" ]; then
  log () {
    echo "$@"
  }
else
  log () {
    : # nothing
  }
fi

# Detect host operating system and architecture
. "$ANDROID_NDK_ROOT/build/tools/ndk_bin_common.sh"
log "HOST_OS=$HOST_OS"
log "HOST_ARCH=$HOST_ARCH"
log "HOST_TAG=$HOST_TAG"

# If GNUMAKE is defined, check that it points to a valid file
if [ -n "$GNUMAKE" ] ; then
    if ! ABS_GNUMAKE=$(which "$GNUMAKE" 2> /dev/null); 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"
    log "GNUMAKE=$GNUMAKE (from environment variable)"
else
    # Otherwise use the prebuilt version for our host tag, if it exists
    # Note: we intentionally do not provide prebuilt make binaries for Cygwin
    # or MSys.
    GNUMAKE=$ANDROID_NDK_ROOT/prebuilt/$HOST_TAG/bin/make
    if [ ! -f "$GNUMAKE" ]; then
        # Otherwise, use 'make' and check that it is available
        if ! GNUMAKE=$(which make 2> /dev/null); 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
        log "GNUMAKE=$GNUMAKE (system path)"
    else
        log "GNUMAKE=$GNUMAKE (NDK prebuilt)"
    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
# 'make' program in their PATH.
#
if [ "$OSTYPE" = "cygwin" ] ; then
    GNUMAKE=$(cygpath -u "$GNUMAKE")
    PROGDIR_MIXED=$(cygpath -m "$PROGDIR")
    if ! ("$GNUMAKE" -f "$PROGDIR_MIXED/core/check-cygwin-make.mk" >/dev/null 2>&1); 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
    log "Cygwin-compatible GNU make detected"
fi

NDK_ANALYZER_FLAGS=
if [ "$NDK_ANALYZE" = 1 ]; then
    # Continue supporting the old interface to the static analyzer. clang-tidy
    # does all the same checks by default (and some new ones).
    NDK_ANALYZER_FLAGS=APP_CLANG_TIDY=true
fi

"$GNUMAKE" -O -f "$PROGDIR/core/build-local.mk" $NDK_ANALYZER_FLAGS "$@"
