Merge "Download/build/use perl prebuilt"
diff --git a/build/tools/build-host-prebuilts.sh b/build/tools/build-host-prebuilts.sh
index e546ae0..244717a 100755
--- a/build/tools/build-host-prebuilts.sh
+++ b/build/tools/build-host-prebuilts.sh
@@ -271,6 +271,13 @@
     run $BUILDTOOLS/build-host-sed.sh $TOOLCHAIN_FLAGS
     fail_panic "sed build failure!"
 
+    # ToDo: perl in windows
+    if [ "$SYSTEM" != "windows" ]; then
+        echo "Building $SYSNAME ndk-perl"
+        run $BUILDTOOLS/build-host-perl.sh $TOOLCHAIN_FLAGS "$SRC_DIR"
+        fail_panic "perl build failure!"
+    fi
+
     if [ "$SYSTEM" = "windows" ]; then
         echo "Building $SYSNAME toolbox"
         run $BUILDTOOLS/build-host-toolbox.sh $FLAGS
diff --git a/build/tools/download-toolchain-sources.sh b/build/tools/download-toolchain-sources.sh
index bb739a4..ff222d2 100755
--- a/build/tools/download-toolchain-sources.sh
+++ b/build/tools/download-toolchain-sources.sh
@@ -199,6 +199,7 @@
 toolchain_clone gcc
 toolchain_clone gdb
 toolchain_clone python
+toolchain_clone perl
 toolchain_clone clang
 toolchain_clone llvm
 toolchain_clone mclinker
@@ -214,6 +215,7 @@
 toolchain_checkout "" $BRANCH gcc gcc-4.4.3 gcc-4.6 gcc-4.7
 toolchain_checkout "" $BRANCH gdb gdb-6.6 gdb-7.3.x
 toolchain_checkout "" $BRANCH python Python-2.7.3
+toolchain_checkout "" $BRANCH perl perl-5.16.2
 toolchain_checkout "" $BRANCH mclinker .
 
 for LLVM_VERSION in $LLVM_VERSION_LIST; do
diff --git a/ndk-build b/ndk-build
index a8b8110..a1d8e9e 100755
--- a/ndk-build
+++ b/ndk-build
@@ -246,6 +246,35 @@
 if [ "$NDK_ANALYZE" = 1 ]; then
     . $PROGDIR/build/tools/dev-defaults.sh  # for DEFAULT_LLVM_VERSION
 
+    # Find perl.  If HOST_PERL is defined, check that it points to a valid file
+    HOST_PERL_LIB=
+    if [ -n "$HOST_PERL" ] ; then
+        ABS_PERL=`which $HOST_PERL 2> /dev/null`
+        if [ $? != 0 ] ; then
+            echo "ERROR: Your HOST_PERL variable is defined to an invalid name: $HOST_PERL"
+            echo "Please fix it to point to a valid perl executable (e.g. /usr/bin/perl)"
+            exit 1
+        fi
+        HOST_PERL="$ABS_PERL"
+        log "HOST_PERL=$HOST_PERL (from environment variable)"
+    else
+        # Otherwise use the prebuilt version for our host tag
+        HOST_PERL="$PROGDIR/prebuilt/$HOST_TAG/bin/perl"
+        if [ ! -f "$HOST_PERL" ]; then
+            # Otherwise, use 'perl' and check that it is available
+            HOST_PERL=`which perl 2> /dev/null`
+            if [ $? != 0 ] ; then
+                echo "ERROR: Cannot find 'perl' program. Please install perl package"
+                echo "or define the HOST_PERL variable to point to it."
+                exit 1
+            fi
+            log "HOST_PERL=$HOST_PERL (system path)"
+        else
+            HOST_PERL_LIB="$PROGDIR/prebuilt/$HOST_TAG/lib/perl5"
+            log "HOST_PERL=$HOST_PERL (NDK prebuilt)"
+        fi
+    fi
+
     # Return flags send in env. or command line which are enough to retrive APP_ABI and TOOLCHAIN_PREFIX later
     gen_flags ()
     {
@@ -284,7 +313,7 @@
     APP_ABIS=`get_build_var APP_ABI`
     for ABI in $APP_ABIS; do
         TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $ABI`
-        $PROGDIR/prebuilt/common/scan-build/scan-build \
+        PERL5LIB="$HOST_PERL_LIB" "$HOST_PERL" $PROGDIR/prebuilt/common/scan-build/scan-build \
             --use-analyzer $PROGDIR/toolchains/llvm-${DEFAULT_LLVM_VERSION}/prebuilt/$HOST_TAG/bin/${ABI}/analyzer \
             --use-cc ${TOOLCHAIN_PREFIX}gcc \
             --use-c++ ${TOOLCHAIN_PREFIX}g++ \