1 #!/bin/sh 2 3 # This script is invoked by Android build system "WITH_SYNTAX_CHECK=1 make ..." and 4 # accepts command line in the following format: 5 # 6 # cxx-syntax ARCH LOCAL_CXX ... 7 # 8 # It calls "clang++ -fsyntax-only ..." to utilize clang's better diagnostics 9 # before calling "LOCAL_CXX ..." for code generation. ARCH is translated 10 # into "-target XXX" (see below) to get proper pre-defined preprocessor 11 # symbols/macros. Some clang-specific warnings are disabled to be compatible 12 # with Android toolchain mostly gcc. 13 # 14 # The compilation time is slightly longer, and the generated object file 15 # should be the same as w/o WITH_SYNTAX_CHECK 16 # 17 # Note that although Android build system doesn't call LOCAL_CXX with any of 18 # the following flags, this script should check and skip "clang -fsyntax-only" 19 # if options contain any of the following: '-print-*', '-dump*', '@*', '-E', 20 # '-' or '-M', for use elsewhere. 21 # 22 23 ARCH="$1" 24 LOCAL_CXX="$2" 25 shift ; shift 26 27 # Turn on syntax-only 28 CLANG_FLAGS="-fsyntax-only" 29 30 # Turn off warning about unused options 31 CLANG_FLAGS="$CLANG_FLAGS -Qunused-arguments" 32 33 # Turn off unknown warning options 34 CLANG_FLAGS="$CLANG_FLAGS -Wno-unknown-warning-option" 35 36 # Define WITH_SYNTAX_CHECK for code wish to behave differently when check 37 CLANG_FLAGS="$CLANG_FLAGS -DWITH_SYNTAX_CHECK" 38 39 # Ignore C standard like -std=gnu99 in LOCAL_CFLAGS but get 40 # passed for C++ compilation by build system 41 CLANG_FLAGS="$CLANG_FLAGS -Qignore-c-std-not-allowed-with-cplusplus" 42 43 # Turn off warnings which aren't useful in this context 44 CLANG_FLAGS="$CLANG_FLAGS -Wno-ignored-attributes -Wno-pedantic -Wno-builtin-requires-header -Wno-gnu -Wno-gnu-designator -Wno-knr-promoted-parameter" 45 46 if [ "$ARCH" != "host" ]; then 47 # Add target to get proper pre-defined preprocessor symbols/macros. 48 case $ARCH in 49 arm) CLANG_FLAGS="$CLANG_FLAGS -target armv5te-none-linux-androideabi" 50 ;; 51 mips) CLANG_FLAGS="$CLANG_FLAGS -target mipsel-none-linux-android" 52 ;; 53 x86) CLANG_FLAGS="$CLANG_FLAGS -target i686-none-linux-android" 54 ;; 55 esac 56 if [ "$LOCAL_CXX" != "${LOCAL_CXX%clang++*}" ]; then 57 # Don't look for its own lib/clang/x.y/include when LOCAL_CXX is clang 58 # which is rebuilt from source w/o installing its include as well 59 CLANG_FLAGS="$CLANG_FLAGS -nostdinc" 60 fi 61 else 62 # Note that unlike target flags where Android build system explicitly specify 63 # everything in command line, host tools have their own sysroot and --sysroot 64 # isn't explicitly added in the commmand line. Likelywise for other gcc implicit 65 # search directories. 66 # 67 # We can query search paths by doing "g++ -v" and parsing the output with 68 # sed -n '1,/BEGIN/!{ /END/,/BEING/!p; }' (*1), but forking gcc here adds overhead. 69 # Because host tool refresh only once 1-2 year, here we hard-code the path obtained by (*2). 70 # ToDo: The build system can do it once for each module, although it still needs to 71 # prepare both -m32 and -m64 versions, anding 2 more args in additional to $ARCH 72 # and $LOCAL_CXX 73 # 74 # (*1) as described in http://sed.sourceforge.net/sedfaq4.html#s4.24 75 # (*2) prebuilts/tools/gcc-sdk/g++ -m64 -v -E - < /dev/null 2>&1 | \ 76 # sed -n '1,/> search starts here/!{ /End of search list/,/> search starts here/!p; }' |\ 77 # sed -e 's/^/-I/' 78 # Likewise for -m32 79 # 80 # Please refer to prebuilts/tools/gcc-sdk/gcc for similar trick to determine 81 # bitness 82 options=" ${@} " # sentinel prefix/suffix space to simplify pattern match below 83 suffix_m32=${options##* -m32 } # suffix after the last -m32 84 suffix_m64=${options##* -m64 } # suffix after the last -m64 85 len_m32=${#suffix_m32} # length of suffix after the last -m32 86 len_m64=${#suffix_m64} # length of suffix after the last -m64 87 if [ $len_m32 -ge $len_m64 ] ; then 88 read -d '' CLANG_FLAGS_END <<"EOF" 89 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google 90 -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 91 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google/backward 92 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include 93 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include-fixed 94 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include 95 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/sysroot/usr/include 96 EOF 97 else 98 read -d '' CLANG_FLAGS_END <<"EOF" 99 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google 100 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google/i686-linux 101 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google/backward 102 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include 103 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include-fixed 104 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include 105 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/sysroot/usr/include 106 EOF 107 fi 108 fi 109 110 # Turn off warnings (at the end) on features we exploit 111 CLANG_FLAGS_END="$CLANG_FLAGS_END -Wno-return-type-c-linkage" 112 113 # Call it 114 `dirname $0`/analyzer++ $CLANG_FLAGS "$@" $CLANG_FLAGS_END 115 if [ "$?" != 0 ]; then 116 test $WITH_SYNTAX_CHECK -ge 2 && echo '*** ERROR ***': `dirname $0`/analyzer++ $CLANG_FLAGS "$@" $CLANG_FLAGS_END 117 exit 1 118 fi 119 $LOCAL_CXX "$@" 120