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 # ccc-syntax ARCH LOCAL_CC ... 7 # 8 # It calls "clang -fsyntax-only ..." to utilize clang's better diagnostics 9 # before calling "LOCAL_CC ..." 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_CC 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_CC="$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 # If LOCAL_CC is not clang and not compiling in c++ mode, turn on -std=gnu89 by default 40 # and let "$@" later override it, if any 41 test "$LOCAL_CC" != "${LOCAL_CC%-x c++}" -o "$LOCAL_CC" = "${LOCAL_CC%-xc++}" && cxx_mode=true 42 if [ "$LOCAL_CC" = "${LOCAL_CC%clang}" -a "$cxx_mode" != "true" ] ; then 43 CLANG_FLAGS="$CLANG_FLAGS -std=gnu89" 44 fi 45 46 # Turn off warnings which aren't useful in this context 47 CLANG_FLAGS="$CLANG_FLAGS -Wno-ignored-attributes -Wno-pedantic -Wno-builtin-requires-header -Wno-gnu -Wno-gnu-designator -Wno-knr-promoted-parameter" 48 49 if [ "$ARCH" != "host" ]; then 50 # Add target to get proper pre-defined preprocessor symbols/macros. 51 case $ARCH in 52 arm) CLANG_FLAGS="$CLANG_FLAGS -target armv5te-none-linux-androideabi" 53 ;; 54 mips) CLANG_FLAGS="$CLANG_FLAGS -target mipsel-none-linux-android" 55 ;; 56 x86) CLANG_FLAGS="$CLANG_FLAGS -target i686-none-linux-android" 57 ;; 58 esac 59 if [ "$LOCAL_CC" != "${LOCAL_CC%clang*}" ]; then 60 # Don't look for its own lib/clang/x.y/include when LOCAL_CC is clang 61 # which is rebuilt from source w/o installing its include as well 62 CLANG_FLAGS="$CLANG_FLAGS -nostdinc" 63 fi 64 else 65 # Note that unlike target flags where Android build system explicitly specify 66 # everything in command line, host tools have their own sysroot and --sysroot 67 # isn't explicitly added in the commmand line. Likelywise for other gcc implicit 68 # search directories. 69 # 70 # We can query search paths by doing "gcc -v" and parsing the output with 71 # sed -n '1,/BEGIN/!{ /END/,/BEING/!p; }' (*1), but forking gcc here adds overhead. 72 # Because host tool refresh only once 1-2 year, here we hard-code the path obtained by (*2). 73 # ToDo: The build system can do it once for each module, although it still needs to 74 # prepare both -m32 and -m64 versions, anding 2 more args in additional to $ARCH 75 # and $LOCAL_CC 76 # 77 # (*1) as described in http://sed.sourceforge.net/sedfaq4.html#s4.24 78 # (*2) prebuilts/tools/gcc-sdk/gcc -m64 -v -E - < /dev/null 2>&1 | \ 79 # sed -n '1,/> search starts here/!{ /End of search list/,/> search starts here/!p; }' |\ 80 # sed -e 's/^/-I/' 81 # Likewise for -m32 82 # 83 # Please refer to prebuilts/tools/gcc-sdk/gcc for similar trick to determine 84 # bitness 85 options=" ${@} " # sentinel prefix/suffix space to simplify pattern match below 86 suffix_m32=${options##* -m32 } # suffix after the last -m32 87 suffix_m64=${options##* -m64 } # suffix after the last -m64 88 len_m32=${#suffix_m32} # length of suffix after the last -m32 89 len_m64=${#suffix_m64} # length of suffix after the last -m64 90 if [ $len_m32 -ge $len_m64 ] ; then 91 read -d '' CLANG_FLAGS_END <<"EOF" 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/lib/gcc/i686-linux/4.6.x-google/include 100 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/lib/gcc/i686-linux/4.6.x-google/include-fixed 101 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include 102 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/sysroot/usr/include 103 EOF 104 fi 105 fi 106 107 # Turn off warnings (at the end) on features we exploit 108 CLANG_FLAGS_END="$CLANG_FLAGS_END -Wno-return-type-c-linkage" 109 110 # Call it 111 `dirname $0`/analyzer $CLANG_FLAGS "$@" $CLANG_FLAGS_END 112 if [ "$?" != 0 ]; then 113 test $WITH_SYNTAX_CHECK -ge 2 && echo '*** ERROR ***': `dirname $0`/analyzer $CLANG_FLAGS "$@" $CLANG_FLAGS_END 114 exit 1 115 fi 116 $LOCAL_CC "$@"