Home | History | Annotate | Download | only in bin
      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/gcc/darwin-x86/host/i686-apple-darwin-4.2.1/bin/i686-apple-darwin10-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         read -d '' CLANG_FLAGS_END <<"EOF"
     84  -I prebuilts/gcc/darwin-x86/host/i686-apple-darwin-4.2.1/lib/gcc/i686-apple-darwin10/4.2.1/include
     85 EOF
     86 fi
     87 
     88 # Turn off warnings (at the end) on features we exploit
     89 CLANG_FLAGS_END="$CLANG_FLAGS_END -Wno-return-type-c-linkage"
     90 
     91 # Call it
     92 `dirname $0`/analyzer $CLANG_FLAGS "$@" $CLANG_FLAGS_END
     93 if [ "$?" != 0 ]; then
     94     test $WITH_SYNTAX_CHECK -ge 2 && echo '*** ERROR ***': `dirname $0`/analyzer $CLANG_FLAGS "$@" $CLANG_FLAGS_END
     95     exit 1
     96 fi
     97 $LOCAL_CC "$@"