Home | History | Annotate | Download | only in bin
      1 #!/bin/sh
      2 
      3 # This script is invoked by Android build system "WITH_SYNTAX_CHECK=1 m/mm/mmm ..." and
      4 # accepts command line in the following format:
      5 #
      6 #    ccc-analyzer ARCH LOCAL_CXX ...
      7 #
      8 # Under WITH_SYNTAX_CHECK=1, m/mm/mmm/mma/mmma wrap "make" with "tools/scan-build/scan-build"
      9 # which utillizes "clang++ --analyze" for static code analyses.  Script scan-build interposes
     10 # on compiler with CC/CXX ignored by the Android build system unfortunately.  Instead,
     11 # Android build system recognizes WITH_SYNTAX_CHECK and replace LOCAL_CXX with our own cxx-syntax,
     12 # which in turn setup the following env. vars expected by tools/scan-build/c++-analyzer
     13 #
     14 #   CCC_CXX: The origianl LOCAL_CXX which does the real compilation and code-gen
     15 #   CLANG_CXX: The clang++ compiler which run code analyses
     16 #
     17 # Our own cxx-syntax also export the following, and tools/scan-build/ccc-analyzer is slightly
     18 # modified to prefix/append them in options passed to clang++ for successful compilation
     19 #
     20 #   CLANG_FLAGS: Flags to set correct target, be compatible with gcc, etc
     21 #   CLANG_FLAGS_END:  Flags to override "$@"
     22 #
     23 
     24 ARCH="$1"
     25 LOCAL_CXX="$2"
     26 shift ; shift
     27 
     28 # Turn off warning about unused options
     29 CLANG_FLAGS="-Qunused-arguments"
     30 
     31 # Turn off unknown warning options
     32 CLANG_FLAGS="$CLANG_FLAGS -Wno-unknown-warning-option"
     33 
     34 # Define WITH_SYNTAX_CHECK for code wish to behave differently when check
     35 CLANG_FLAGS="$CLANG_FLAGS -DWITH_SYNTAX_CHECK"
     36 
     37 # Ignore C standard like -std=gnu99 in LOCAL_CFLAGS but get
     38 # passed for C++ compilation by build system
     39 CLANG_FLAGS="$CLANG_FLAGS -Qignore-c-std-not-allowed-with-cplusplus"
     40 
     41 # Turn off warnings which aren't useful in this context
     42 CLANG_FLAGS="$CLANG_FLAGS -Wno-ignored-attributes -Wno-pedantic -Wno-builtin-requires-header -Wno-gnu -Wno-gnu-designator -Wno-knr-promoted-parameter"
     43 
     44 if [ "$ARCH" != "host" ]; then
     45     # Add target to get proper pre-defined preprocessor symbols/macros.
     46     case $ARCH in
     47         arm) CLANG_FLAGS="$CLANG_FLAGS -target armv5te-none-linux-androideabi"
     48             ;;
     49         mips) CLANG_FLAGS="$CLANG_FLAGS -target mipsel-none-linux-android"
     50             ;;
     51         x86) CLANG_FLAGS="$CLANG_FLAGS -target i686-none-linux-android"
     52             ;;
     53     esac
     54     if [ "$LOCAL_CXX" != "${LOCAL_CXX%clang++*}" ]; then
     55         # Don't look for its own lib/clang/x.y/include when LOCAL_CXX is clang
     56         # which is rebuilt from source w/o installing its include as well
     57         CLANG_FLAGS="$CLANG_FLAGS -nostdinc"
     58     fi
     59 else
     60     # Note that unlike target flags where Android build system explicitly specify
     61     # everything in command line, host tools have their own sysroot and --sysroot
     62     # isn't explicitly added in the commmand line.  Likelywise for other gcc implicit
     63     # search directories.
     64     #
     65     # We can query search paths by doing "g++ -v" and parsing the output with
     66     # sed -n '1,/BEGIN/!{ /END/,/BEING/!p; }' (*1), but forking gcc here adds overhead.
     67     # Because host tool refresh only once 1-2 year, here we hard-code the path obtained by (*2).
     68     # ToDo: The build system can do it once for each module, although it still needs to
     69     # prepare both -m32 and -m64 versions, anding 2 more args in additional to $ARCH
     70     # and $LOCAL_CXX
     71     #
     72     # (*1) as described in http://sed.sourceforge.net/sedfaq4.html#s4.24
     73     # (*2) prebuilts/tools/gcc-sdk/g++ -m64 -v -E - < /dev/null 2>&1 | \
     74     #         sed -n '1,/> search starts here/!{ /End of search list/,/> search starts here/!p; }' |\
     75     #         sed -e 's/^/-I/'
     76     #      Likewise for -m32
     77     #
     78     # Please refer to prebuilts/tools/gcc-sdk/gcc for similar trick to determine
     79     # bitness
     80     options=" ${@} "   # sentinel prefix/suffix space to simplify pattern match below
     81     suffix_m32=${options##* -m32 }    # suffix after the last -m32
     82     suffix_m64=${options##* -m64 }    # suffix after the last -m64
     83     len_m32=${#suffix_m32}            # length of suffix after the last -m32
     84     len_m64=${#suffix_m64}            # length of suffix after the last -m64
     85     if [ $len_m32 -ge $len_m64 ] ; then
     86         CLANG_FLAGS_END="\
     87 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google \
     88 -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 \
     89 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include/c++/4.6.x-google/backward \
     90 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include \
     91 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/lib/gcc/x86_64-linux/4.6.x-google/include-fixed \
     92 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/x86_64-linux/include \
     93 -I prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/sysroot/usr/include"
     94     else
     95         CLANG_FLAGS_END="\
     96 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google \
     97 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google/i686-linux \
     98 -I prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/i686-linux/include/c++/4.6.x-google/backward \
     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     fi
    104 fi
    105 
    106 # Turn off warnings (at the end) on features we exploit
    107 CLANG_FLAGS_END="$CLANG_FLAGS_END -Wno-return-type-c-linkage"
    108 
    109 # Call the real c++-analyzer.  Note that tools/scan-build/c++-analyzer "exec" $CCC_CXX,
    110 # which is LOCAL_CXX w/o optional ccache (in "ccache g++" format)
    111 export CCC_CXX="${LOCAL_CXX##* }"
    112 export CLANG_CXX="`dirname $0`/analyzer++"
    113 export CLANG_FLAGS
    114 export CLANG_FLAGS_END
    115 `dirname $0`/../tools/scan-build/c++-analyzer "$@"
    116