Merge "Add clang 3.3 toolchain"
diff --git a/bin/ccc-syntax b/bin/ccc-syntax
new file mode 100755
index 0000000..ab8dc8c
--- /dev/null
+++ b/bin/ccc-syntax
@@ -0,0 +1,115 @@
+#!/bin/sh
+
+# This script is invoked by Android build system "WITH_SYNTAX_CHECK=1 make ..." and
+# accepts command line in the following format:
+#
+#    ccc-syntax ARCH LOCAL_CC ...
+#
+# It calls "clang -fsyntax-only ..." to utilize clang's better diagnostics
+# before calling "LOCAL_CC ..." for code generation.  ARCH is translated
+# into "-target XXX" (see below) to get proper pre-defined preprocessor
+# symbols/macros.  Some clang-specific warnings are disabled to be compatible
+# with Android toolchain mostly gcc.
+#
+# The compilation time is slightly longer, and the generated object file
+# should be the same as w/o WITH_SYNTAX_CHECK
+#
+# Note that although Android build system doesn't call LOCAL_CC with any of
+# the following flags, this script should check and skip "clang -fsyntax-only"
+# if options contain any of the following: '-print-*', '-dump*', '@*', '-E',
+# '-' or '-M', for use elsewhere.
+#
+
+ARCH="$1"
+LOCAL_CC="$2"
+shift ; shift
+
+# Turn on syntax-only
+CLANG_FLAGS="-fsyntax-only"
+
+# Turn off warning about unused options
+CLANG_FLAGS="$CLANG_FLAGS -Qunused-arguments"
+
+# Turn off unknown warning options
+CLANG_FLAGS="$CLANG_FLAGS -Wno-unknown-warning-option"
+
+# Define WITH_SYNTAX_CHECK for code wish to behave differently when check
+CLANG_FLAGS="$CLANG_FLAGS -DWITH_SYNTAX_CHECK"
+
+# If LOCAL_CC is not clang and not compiling in c++ mode, turn on -std=gnu89 by default
+# and let "$@" later override it, if any
+test "$LOCAL_CC" != "${LOCAL_CC%-x c++}" -o "$LOCAL_CC" = "${LOCAL_CC%-xc++}" && cxx_mode=true
+if [ "$LOCAL_CC" = "${LOCAL_CC%clang}" -a "$cxx_mode" != "true" ] ; then
+    CLANG_FLAGS="$CLANG_FLAGS -std=gnu89"
+fi
+
+# Turn off warnings which aren't useful in this context
+CLANG_FLAGS="$CLANG_FLAGS -Wno-ignored-attributes -Wno-pedantic -Wno-builtin-requires-header -Wno-gnu -Wno-gnu-designator -Wno-knr-promoted-parameter"
+
+if [ "$ARCH" != "host" ]; then
+    # Add target to get proper pre-defined preprocessor symbols/macros.
+    case $ARCH in
+        arm) CLANG_FLAGS="$CLANG_FLAGS -target armv5te-none-linux-androideabi"
+            ;;
+        mips) CLANG_FLAGS="$CLANG_FLAGS -target mipsel-none-linux-android"
+            ;;
+        x86) CLANG_FLAGS="$CLANG_FLAGS -target i686-none-linux-android"
+    esac
+    if [ "$LOCAL_CC" != "${LOCAL_CC%clang*}" ]; then
+        # Don't look for its own lib/clang/3.3/include when LOCAL_CC is clang
+        # which is rebuilt from source w/o installing its include as well
+        CLANG_FLAGS="$CLANG_FLAGS -nostdinc"
+    fi
+else
+    # Note that unlike target flags where Android build system explicitly specify
+    # everything in command line, host tools have their own sysroot and --sysroot
+    # isn't explicitly added in the commmand line.  Likelywise for other gcc implicit
+    # search directories.
+    #
+    # We can query search paths by doing "gcc -v" and parsing the output with
+    # sed -n '1,/BEGIN/!{ /END/,/BEING/!p; }' (*1), but forking gcc here adds overhead.
+    # Because host tool refresh only once 1-2 year, here we hard-code the path obtained by (*2).
+    # ToDo: The build system can do it once for each module, although it still needs to
+    # prepare both -m32 and -m64 versions, anding 2 more args in additional to $ARCH
+    # and $LOCAL_CC
+    #
+    # (*1) as described in http://sed.sourceforge.net/sedfaq4.html#s4.24
+    # (*2) prebuilts/tools/gcc-sdk/gcc -m64 -v -E - < /dev/null 2>&1 | \
+    #         sed -n '1,/> search starts here/!{ /End of search list/,/> search starts here/!p; }' |\
+    #         sed -e 's/^/-I/'
+    #      Likewise for -m32
+    #
+    # Please refer to prebuilts/tools/gcc-sdk/gcc for similar trick to determine
+    # bitness
+    options=" ${@} "   # sentinel prefix/suffix space to simplify pattern match below
+    suffix_m32=${options##* -m32 }    # suffix after the last -m32
+    suffix_m64=${options##* -m64 }    # suffix after the last -m64
+    len_m32=${#suffix_m32}            # length of suffix after the last -m32
+    len_m64=${#suffix_m64}            # length of suffix after the last -m64
+    if [ $len_m32 -ge $len_m64 ] ; then
+        read -d '' CLANG_FLAGS_END <<"EOF"
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include-fixed
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/sysroot/usr/include
+EOF
+    else
+        read -d '' CLANG_FLAGS_END <<"EOF"
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include-fixed
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/sysroot/usr/include
+EOF
+    fi
+fi
+
+# Turn off warnings (at the end) on features we exploit
+CLANG_FLAGS_END="$CLANG_FLAGS_END -Wno-return-type-c-linkage"
+
+# Call it
+`dirname $0`/clang $CLANG_FLAGS "$@" $CLANG_FLAGS_END
+if [ "$?" != 0 ]; then
+    test $WITH_SYNTAX_CHECK -ge 2 && echo '*** ERROR ***': `dirname $0`/clang $CLANG_FLAGS "$@" $CLANG_FLAGS_END
+    exit 1
+fi
+$LOCAL_CC "$@"
\ No newline at end of file
diff --git a/bin/cxx-syntax b/bin/cxx-syntax
new file mode 100755
index 0000000..839dace
--- /dev/null
+++ b/bin/cxx-syntax
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+# This script is invoked by Android build system "WITH_SYNTAX_CHECK=1 make ..." and
+# accepts command line in the following format:
+#
+#    cxx-syntax ARCH LOCAL_CXX ...
+#
+# It calls "clang++ -fsyntax-only ..." to utilize clang's better diagnostics
+# before calling "LOCAL_CXX ..." for code generation.  ARCH is translated
+# into "-target XXX" (see below) to get proper pre-defined preprocessor
+# symbols/macros.  Some clang-specific warnings are disabled to be compatible
+# with Android toolchain mostly gcc.
+#
+# The compilation time is slightly longer, and the generated object file
+# should be the same as w/o WITH_SYNTAX_CHECK
+#
+# Note that although Android build system doesn't call LOCAL_CXX with any of
+# the following flags, this script should check and skip "clang -fsyntax-only"
+# if options contain any of the following: '-print-*', '-dump*', '@*', '-E',
+# '-' or '-M', for use elsewhere.
+#
+
+ARCH="$1"
+LOCAL_CXX="$2"
+shift ; shift
+
+# Turn on syntax-only
+CLANG_FLAGS="-fsyntax-only"
+
+# Turn off warning about unused options
+CLANG_FLAGS="$CLANG_FLAGS -Qunused-arguments"
+
+# Turn off unknown warning options
+CLANG_FLAGS="$CLANG_FLAGS -Wno-unknown-warning-option"
+
+# Define WITH_SYNTAX_CHECK for code wish to behave differently when check
+CLANG_FLAGS="$CLANG_FLAGS -DWITH_SYNTAX_CHECK"
+
+# Ignore C standard like -std=gnu99 in LOCAL_CFLAGS but get
+# passed for C++ compilation by build system
+CLANG_FLAGS="$CLANG_FLAGS -Qignore-c-std-not-allowed-with-cplusplus"
+
+# Turn off warnings which aren't useful in this context
+CLANG_FLAGS="$CLANG_FLAGS -Wno-ignored-attributes -Wno-pedantic -Wno-builtin-requires-header -Wno-gnu -Wno-gnu-designator -Wno-knr-promoted-parameter"
+
+if [ "$ARCH" != "host" ]; then
+    # Add target to get proper pre-defined preprocessor symbols/macros.
+    case $ARCH in
+        arm) CLANG_FLAGS="$CLANG_FLAGS -target armv5te-none-linux-androideabi"
+            ;;
+        mips) CLANG_FLAGS="$CLANG_FLAGS -target mipsel-none-linux-android"
+            ;;
+        x86) CLANG_FLAGS="$CLANG_FLAGS -target i686-none-linux-android"
+    esac
+    if [ "$LOCAL_CXX" != "${LOCAL_CXX%clang++*}" ]; then
+        # Don't look for its own lib/clang/3.3/include when LOCAL_CXX is clang
+        # which is rebuilt from source w/o installing its include as well
+        CLANG_FLAGS="$CLANG_FLAGS -nostdinc"
+    fi
+else
+    # Note that unlike target flags where Android build system explicitly specify
+    # everything in command line, host tools have their own sysroot and --sysroot
+    # isn't explicitly added in the commmand line.  Likelywise for other gcc implicit
+    # search directories.
+    #
+    # We can query search paths by doing "g++ -v" and parsing the output with
+    # sed -n '1,/BEGIN/!{ /END/,/BEING/!p; }' (*1), but forking gcc here adds overhead.
+    # Because host tool refresh only once 1-2 year, here we hard-code the path obtained by (*2).
+    # ToDo: The build system can do it once for each module, although it still needs to
+    # prepare both -m32 and -m64 versions, anding 2 more args in additional to $ARCH
+    # and $LOCAL_CXX
+    #
+    # (*1) as described in http://sed.sourceforge.net/sedfaq4.html#s4.24
+    # (*2) prebuilts/tools/gcc-sdk/g++ -m64 -v -E - < /dev/null 2>&1 | \
+    #         sed -n '1,/> search starts here/!{ /End of search list/,/> search starts here/!p; }' |\
+    #         sed -e 's/^/-I/'
+    #      Likewise for -m32
+    #
+    # Please refer to prebuilts/tools/gcc-sdk/gcc for similar trick to determine
+    # bitness
+    options=" ${@} "   # sentinel prefix/suffix space to simplify pattern match below
+    suffix_m32=${options##* -m32 }    # suffix after the last -m32
+    suffix_m64=${options##* -m64 }    # suffix after the last -m64
+    len_m32=${#suffix_m32}            # length of suffix after the last -m32
+    len_m64=${#suffix_m64}            # length of suffix after the last -m64
+    if [ $len_m32 -ge $len_m64 ] ; then
+        read -d '' CLANG_FLAGS_END <<"EOF"
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google/x86_64-linux
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google/backward
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include-fixed
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include
+ -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/sysroot/usr/include
+EOF
+    else
+        read -d '' CLANG_FLAGS_END <<"EOF"
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google/i686-linux
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google/backward
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include-fixed
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include
+ -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/sysroot/usr/include
+EOF
+    fi
+fi
+
+# Turn off warnings (at the end) on features we exploit
+CLANG_FLAGS_END="$CLANG_FLAGS_END -Wno-return-type-c-linkage"
+
+# Call it
+`dirname $0`/clang++ $CLANG_FLAGS "$@" $CLANG_FLAGS_END
+if [ "$?" != 0 ]; then
+    test $WITH_SYNTAX_CHECK -ge 2 && echo '*** ERROR ***': `dirname $0`/clang++ $CLANG_FLAGS "$@" $CLANG_FLAGS_END
+    exit 1
+fi
+$LOCAL_CXX "$@"