Fix NDK_MODULE_PATH handling when there are several directories

A typo prevented NDK_MODULE_PATH to work as advertised when it contained
several directories separated with ":" so fix this.

Also add a unit test to check that it works correctly now.

Change-Id: I6e376026a5319c3165e825c4f71d4829cd395cf7
NOTE: Now, if a build test contains a "build.sh" script, the latter will
      be called to perform the build, instead of calling ndk-build directly
      from run-tests.sh
diff --git a/build/core/setup-imports.mk b/build/core/setup-imports.mk
index 67501b9..96fc1d5 100644
--- a/build/core/setup-imports.mk
+++ b/build/core/setup-imports.mk
@@ -29,7 +29,7 @@
 endif
 
 $(call import-init)
-$(foreach __path,$(patsubst $(HOST_DIRSEP),$(space),$(NDK_MODULE_PATH)),\
+$(foreach __path,$(subst $(HOST_DIRSEP),$(space),$(NDK_MODULE_PATH)),\
   $(call import-add-path,$(__path))\
 )
 $(call import-add-path-optional,$(NDK_ROOT)/sources)
diff --git a/docs/CHANGES.html b/docs/CHANGES.html
index 6c62ee8..6bbe16d 100644
--- a/docs/CHANGES.html
+++ b/docs/CHANGES.html
@@ -19,6 +19,9 @@
   generated machine code, instead of relying on the host tag. That should
   allow the 32-bit toolchain to rebuild properly on Snow Leopard.
 
+- Fixed a typo that prevented several NDK_MODULE_PATH to work properly when
+  it contained multiple directories separated with ":"
+
 -------------------------------------------------------------------------------
 android-ndk-r5
 
diff --git a/tests/build/multi-module-path/build.sh b/tests/build/multi-module-path/build.sh
new file mode 100755
index 0000000..f3cff71
--- /dev/null
+++ b/tests/build/multi-module-path/build.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+echo "\$0=$0"
+cd `dirname $0`
+echo "PWD=`pwd`"
+export NDK_MODULE_PATH=`pwd`/path1:`pwd`/path2
+../../../ndk-build
+if [ $? != 0 ]; then
+    echo "ERROR: Can't build test program!"
+    exit 1
+fi
diff --git a/tests/build/multi-module-path/jni/Android.mk b/tests/build/multi-module-path/jni/Android.mk
new file mode 100644
index 0000000..13f6b71
--- /dev/null
+++ b/tests/build/multi-module-path/jni/Android.mk
@@ -0,0 +1,9 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := multi_path_test
+LOCAL_SRC_FILES := test.c
+LOCAL_STATIC_LIBRARIES := libbar libfoo
+include $(BUILD_EXECUTABLE)
+
+$(call import-module,bar)
diff --git a/tests/build/multi-module-path/jni/test.c b/tests/build/multi-module-path/jni/test.c
new file mode 100644
index 0000000..1e8dbf1
--- /dev/null
+++ b/tests/build/multi-module-path/jni/test.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+#include "bar.h"
+
+int main(void)
+{
+    printf("The answer is %d\n", bar());
+    return 0;
+}
diff --git a/tests/build/multi-module-path/path1/foo/Android.mk b/tests/build/multi-module-path/path1/foo/Android.mk
new file mode 100644
index 0000000..02a4d8c
--- /dev/null
+++ b/tests/build/multi-module-path/path1/foo/Android.mk
@@ -0,0 +1,9 @@
+# A simple module used to demonstrate multi-path NDK_MODULE_PATH imports.
+#
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libfoo
+LOCAL_SRC_FILES := libfoo.c
+LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
+include $(BUILD_STATIC_LIBRARY)
diff --git a/tests/build/multi-module-path/path1/foo/foo.h b/tests/build/multi-module-path/path1/foo/foo.h
new file mode 100644
index 0000000..9f97edc
--- /dev/null
+++ b/tests/build/multi-module-path/path1/foo/foo.h
@@ -0,0 +1,6 @@
+#ifndef FOO_H
+#define FOO_H
+
+extern int foo(void);
+
+#endif /* FOO_H */
diff --git a/tests/build/multi-module-path/path1/foo/libfoo.c b/tests/build/multi-module-path/path1/foo/libfoo.c
new file mode 100644
index 0000000..a4758ad
--- /dev/null
+++ b/tests/build/multi-module-path/path1/foo/libfoo.c
@@ -0,0 +1,6 @@
+#include "foo.h"
+
+int foo(void)
+{
+    return 42;
+}
diff --git a/tests/build/multi-module-path/path2/bar/Android.mk b/tests/build/multi-module-path/path2/bar/Android.mk
new file mode 100644
index 0000000..12bab87
--- /dev/null
+++ b/tests/build/multi-module-path/path2/bar/Android.mk
@@ -0,0 +1,10 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbar
+LOCAL_SRC_FILES := libbar.c
+LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
+LOCAL_STATIC_LIBRARIES := libfoo
+include $(BUILD_STATIC_LIBRARY)
+
+$(call import-module,foo)
diff --git a/tests/build/multi-module-path/path2/bar/bar.h b/tests/build/multi-module-path/path2/bar/bar.h
new file mode 100644
index 0000000..a0c22ed
--- /dev/null
+++ b/tests/build/multi-module-path/path2/bar/bar.h
@@ -0,0 +1,6 @@
+#ifndef BAR_H
+#define BAR_H
+
+extern int bar(void);
+
+#endif /* BAR_H */
diff --git a/tests/build/multi-module-path/path2/bar/libbar.c b/tests/build/multi-module-path/path2/bar/libbar.c
new file mode 100644
index 0000000..e960ded
--- /dev/null
+++ b/tests/build/multi-module-path/path2/bar/libbar.c
@@ -0,0 +1,7 @@
+#include "bar.h"
+#include "foo.h"
+
+int  bar(void)
+{
+    return foo()*2;
+}
diff --git a/tests/build/multi-module-path/run.sh b/tests/build/multi-module-path/run.sh
new file mode 100755
index 0000000..f3cff71
--- /dev/null
+++ b/tests/build/multi-module-path/run.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+echo "\$0=$0"
+cd `dirname $0`
+echo "PWD=`pwd`"
+export NDK_MODULE_PATH=`pwd`/path1:`pwd`/path2
+../../../ndk-build
+if [ $? != 0 ]; then
+    echo "ERROR: Can't build test program!"
+    exit 1
+fi
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
index c0cd585..a423f49 100755
--- a/tests/run-tests.sh
+++ b/tests/run-tests.sh
@@ -329,7 +329,15 @@
     build_build_test ()
     {
         echo "Building NDK build test: `basename $1`"
-        build_project $1
+        if [ -f $1/build.sh ]; then
+            run $1/build.sh
+            if [ $? != 0 ]; then
+                echo "!!! BUILD FAILURE [$1]!!! See $NDK_LOGFILE for details or use --verbose option!"
+                exit 1
+            fi
+        else
+            build_project $1
+        fi
     }
 
     for DIR in `ls -d $ROOTDIR/tests/build/*`; do