Fix cygwin-specific awk script to deal with very short paths.

This patch fixes convert-deps-to-cygwin.awk to work properly whe very
short paths are used on Windows to install the NDK or the project path.

This leads gcc to generate dependency files that contain several file
names per line, which the old script could not handle.

+ Add support for unit tests for Awk scripts in tests/run-tests.sh
+ Add missing license disclaimer in tests/run-standalone-tests.sh

Change-Id: I60d67e13f039aa071233c33ec8c72b36f2fe2c9a
diff --git a/build/awk/convert-deps-to-cygwin.awk b/build/awk/convert-deps-to-cygwin.awk
index 5d57f33..5dc3646 100644
--- a/build/awk/convert-deps-to-cygwin.awk
+++ b/build/awk/convert-deps-to-cygwin.awk
@@ -38,15 +38,20 @@
 }
 
 {
-    # First case: D:/Stuff/source.o: \
-    if ( $0 ~ /^[A-Za-z]:/ ) {
-        print CYGDRIVE_PREFIX tolower(substr($0,1,1)) "/" substr($0,4)
+    LINE=""
+    SEP=""
+    for (nn = 1; nn <= NF; nn++) {
+        if ($nn ~ /^[A-Za-z]:/) {
+            LINE = LINE SEP CYGDRIVE_PREFIX tolower(substr($nn,1,1)) "/" substr($nn,4)
+        } else {
+            LINE = LINE SEP $nn
+        }
+        SEP=" "
     }
-    # Second case: <space>D:/Stuff/source.h
-    else if ( $0 ~ /^ [A-Za-z]:/ ) {
-        print " " CYGDRIVE_PREFIX tolower(substr($0,2,1)) "/" substr($0,5)
+    # Any leading space on the original line should be preserved
+    MARGIN=""
+    if (match($0,"^[[:space:]]+")) {
+        MARGIN=substr($0,RSTART,RLENGTH)
     }
-    else {
-        print $0
-    }
+    printf("%s%s\n", MARGIN, LINE)
 }
diff --git a/docs/CHANGES.html b/docs/CHANGES.html
index ff9d0ab..bf9bc5a 100644
--- a/docs/CHANGES.html
+++ b/docs/CHANGES.html
@@ -23,6 +23,11 @@
   compilation error occured on Windows, preventing building properly after
   the error was fixed in the source code.
 
+- ndk-build: Fix a Cygwin-specific bug where using very short paths for
+  the Android NDK installation or the project path could lead to the
+  generation of invalid dependency files, making incremental builds
+  impossible.
+
 - Fix a typo that prevented the cpufeatures library to work correctly
   with the new NDK toolchain.
 
diff --git a/tests/awk/convert-deps-to-cygwin/cygwin-deps-1.in b/tests/awk/convert-deps-to-cygwin/cygwin-deps-1.in
new file mode 100644
index 0000000..fdc788a
--- /dev/null
+++ b/tests/awk/convert-deps-to-cygwin/cygwin-deps-1.in
@@ -0,0 +1,4 @@
+D:/Stuff/source.o: \
+ D:/Stuff/source.h \
+ C:/NDK/sysroot/include/string.h \
+ C:/NDK/sysroot/include/malloc.h
diff --git a/tests/awk/convert-deps-to-cygwin/cygwin-deps-1.out b/tests/awk/convert-deps-to-cygwin/cygwin-deps-1.out
new file mode 100644
index 0000000..d1f25c4
--- /dev/null
+++ b/tests/awk/convert-deps-to-cygwin/cygwin-deps-1.out
@@ -0,0 +1,4 @@
+/cygdrive/d/Stuff/source.o: \
+ /cygdrive/d/Stuff/source.h \
+ /cygdrive/c/NDK/sysroot/include/string.h \
+ /cygdrive/c/NDK/sysroot/include/malloc.h
diff --git a/tests/awk/convert-deps-to-cygwin/cygwin-deps-2.in b/tests/awk/convert-deps-to-cygwin/cygwin-deps-2.in
new file mode 100644
index 0000000..4a63aeb
--- /dev/null
+++ b/tests/awk/convert-deps-to-cygwin/cygwin-deps-2.in
@@ -0,0 +1,2 @@
+D:/Stuff/source.o: D:/Stuff/source.h C:/NDK/sysroot/include/string.h \
+ C:/NDK/sysroot/include/malloc.h
diff --git a/tests/awk/convert-deps-to-cygwin/cygwin-deps-2.out b/tests/awk/convert-deps-to-cygwin/cygwin-deps-2.out
new file mode 100644
index 0000000..da15b42
--- /dev/null
+++ b/tests/awk/convert-deps-to-cygwin/cygwin-deps-2.out
@@ -0,0 +1,2 @@
+/cygdrive/d/Stuff/source.o: /cygdrive/d/Stuff/source.h /cygdrive/c/NDK/sysroot/include/string.h \
+ /cygdrive/c/NDK/sysroot/include/malloc.h
diff --git a/tests/run-standalone-tests.sh b/tests/run-standalone-tests.sh
index 8a0803b..b012959 100755
--- a/tests/run-standalone-tests.sh
+++ b/tests/run-standalone-tests.sh
@@ -1,5 +1,19 @@
 #!/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.
+#
 
 # Extract current directory
 PROGDIR=`dirname $0`
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
index a423f49..9b5ff02 100755
--- a/tests/run-tests.sh
+++ b/tests/run-tests.sh
@@ -41,7 +41,7 @@
 NDK_ROOT=
 JOBS=$BUILD_NUM_CPUS
 find_program ADB_CMD adb
-TESTABLES="samples build device"
+TESTABLES="samples build device awk"
 FULL_TESTS=no
 RUN_TESTS=
 NDK_PACKAGE=
@@ -91,6 +91,9 @@
         --only-device)
             TESTABLES=device
             ;;
+        --only-awk)
+            TESTABLES=awk
+            ;;
         -*) # unknown options
             echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
             exit 1
@@ -123,6 +126,7 @@
     echo "    --only-samples    Only rebuild samples"
     echo "    --only-build      Only rebuild build tests"
     echo "    --only-device     Only rebuild & run device tests"
+    echo "    --only-awk        Only run awk tests."
     echo "    --full            Run all device tests, even very long ones."
     echo ""
     echo "NOTE: You cannot use --ndk and --package at the same time."
@@ -240,6 +244,83 @@
 BUILD_DIR=`mktemp -d /tmp/ndk-tests/build-XXXXXX`
 set_adb_cmd_log "$BUILD_DIR/adb-cmd.log"
 
+###
+### RUN AWK TESTS
+###
+
+# Run a simple awk script
+# $1: awk script to run
+# $2: input file
+# $3: expected output file
+# $4+: optional additional command-line arguments for the awk command
+run_awk_test ()
+{
+    local SCRIPT="$1"
+    local SCRIPT_NAME="`basename $SCRIPT`"
+    local INPUT="$2"
+    local INPUT_NAME="`basename $INPUT`"
+    local EXPECTED="$3"
+    local EXPECTED_NAME="`basename $EXPECTED`"
+    shift; shift; shift;
+    local OUTPUT="$BUILD_DIR/$EXPECTED_NAME"
+    if [ "$VERBOSE2" = "yes" ]; then
+        echo "### COMMAND: awk -f \"$SCRIPT\" $@ < \"$INPUT\" > \"$OUTPUT\""
+    fi
+    awk -f "$SCRIPT" $@ < "$INPUT" > "$OUTPUT"
+    fail_panic "Can't run awk script: $SCRIPT"
+    if [ "$VERBOSE2" = "yes" ]; then
+        echo "OUTPUT FROM SCRIPT:"
+        cat "$OUTPUT"
+        echo "EXPECTED VALUES:"
+        cat "$EXPECTED"
+    fi
+    cmp -s "$OUTPUT" "$EXPECTED"
+    if [ $? = 0 ] ; then
+        echo "Awk script: $SCRIPT_NAME: passed $INPUT_NAME"
+        if [ "$VERBOSE2" = "yes" ]; then
+            cat "$OUTPUT"
+        fi
+    else
+        if [ "$VERBOSE" = "yes" ]; then
+            run diff -burN "$EXPECTED" "$OUTPUT"
+        fi
+        echo "Awk script: $SCRIPT_NAME: $INPUT_NAME FAILED!!"
+        rm -f "$OUTPUT"
+        exit 1
+    fi
+}
+
+run_awk_test_dir ()
+{
+    local SCRIPT_NAME="`basename \"$DIR\"`"
+    local SCRIPT="$ROOTDIR/build/awk/$SCRIPT_NAME.awk"
+    local INPUT
+    local OUTPUT
+    if [ ! -f "$SCRIPT" ]; then
+        echo "Awk script: $SCRIPT_NAME: Missing script: $SCRIPT"
+        continue
+    fi
+    for INPUT in `ls "$PROGDIR"/tests/awk/$SCRIPT_NAME/*.in`; do
+        OUTPUT=`echo $INPUT | sed 's/\.in$/.out/g'`
+        if [ ! -f "$OUTPUT" ]; then
+            echo "Awk script: $SCRIPT_NAME: Missing awk output file: $OUTPUT"
+            continue
+        fi
+        run_awk_test "$SCRIPT" "$INPUT" "$OUTPUT"
+    done
+}
+
+if is_testable awk; then
+    AWKDIR="$ROOTDIR/build/awk"
+    for DIR in `ls -d "$PROGDIR"/tests/awk/*`; do
+        run_awk_test_dir "$DIR"
+    done
+fi
+
+###
+###  REBUILD ALL SAMPLES FIRST
+###
+
 NDK_BUILD_FLAGS="-B"
 # Use --verbose twice to see build commands for the tests
 if [ "$VERBOSE2" = "yes" ] ; then
@@ -267,10 +348,6 @@
     fi
 }
 
-###
-###  REBUILD ALL SAMPLES FIRST
-###
-
 #
 # Determine list of samples directories.
 #