| 1 | #!/bin/bash |
| 2 | # |
| 3 | # clang-scan-build: configure and compile asterisk using the llvm static analyzer |
| 4 | |
| 5 | # Options/Flags: |
| 6 | # -c|--compiler : either [clang|gcc] |
| 7 | # --cflags : cflags you would like to add to the default set |
| 8 | # --configure : configure flags you would like to use instead off the default set |
| 9 | # --make : make flags you would like to use instead off the default set |
| 10 | # --scanbuild : scanbuild flags you would like to use instead of the default set |
| 11 | # --outputdir : directory where scan-build should create the html files |
| 12 | # -h|--help : this help |
| 13 | |
| 14 | # Usage: |
| 15 | # contrib/scripts/clang-scan-build |
| 16 | # This script will use clang if available and no compiler has been specified |
| 17 | # |
| 18 | # Example usage: |
| 19 | # |
| 20 | # contrib/scripts/clang-scan-build |
| 21 | # contrib/scripts/clang-scan-build -c gcc |
| 22 | # contrib/scripts/clang-scan-build --compiler clang --configure "--enable-dev-mode" --outputdir="/tmp/scan-build_output" |
| 23 | # contrib/scripts/clang-scan-build --make "-j2" |
| 24 | # |
| 25 | # scan-build will generate html files during the make process, which will be stored in the specified outputdir or ./scan-build_output" by default |
| 26 | |
| 27 | # Copyright (C) 2015 Diederik de Groot <[email protected]> |
| 28 | # |
| 29 | # This program is free software; you can redistribute it and/or modify |
| 30 | # it under the terms of the GNU General Public License as published by |
| 31 | # the Free Software Foundation; either version 2 of the License, or |
| 32 | # (at your option) any later version. |
| 33 | # |
| 34 | # This program is distributed in the hope that it will be useful, |
| 35 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | # GNU General Public License for more details. |
| 38 | # |
| 39 | # You should have received a copy of the GNU General Public License |
| 40 | # along with this program; if not, write to the Free Software |
| 41 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 42 | # USA |
| 43 | |
| 44 | COMPILER=clang |
| 45 | SCANBUILD="`which scan-build`" |
| 46 | CFLAGS="" |
| 47 | CONFIGURE_FLAGS="--enable-coverage --disable-xmldoc" |
| 48 | MAKE_FLAGS="" |
| 49 | SCANBUILD_FLAGS="-maxloop 10 -disable-checker deadcode.DeadStores -enable-checker alpha.core.BoolAssignment -enable-checker alpha.core.CallAndMessageUnInitRefArg -enable-checker alpha.core.CastSize -enable-checker alpha.core.CastToStruct -enable-checker alpha.core.IdenticalExpr -enable-checker alpha.core.PointerArithm -enable-checker alpha.core.PointerSub -enable-checker alpha.core.SizeofPtr -enable-checker alpha.core.TestAfterDivZero -enable-checker alpha.security.ArrayBound -enable-checker alpha.security.ArrayBoundV2 -enable-checker alpha.security.MallocOverflow -enable-checker alpha.security.ReturnPtrRange -enable-checker alpha.security.taint.TaintPropagation -enable-checker alpha.unix.MallocWithAnnotations -enable-checker alpha.unix.PthreadLock -enable-checker alpha.unix.SimpleStream -enable-checker alpha.unix.Stream -enable-checker alpha.unix.cstring.BufferOverlap -enable-checker alpha.unix.cstring.NotNullTerminated -enable-checker alpha.unix.cstring.OutOfBounds" |
| 50 | OUTPUTDIR="scan-build_output" |
| 51 | |
| 52 | function print_usage { |
| 53 | cat << EOF |
| 54 | $0 Usage: |
| 55 | |
| 56 | Options/Flags: |
| 57 | -c|--compiler : either [clang|gcc] |
| 58 | --cflags : cflags you would like to add to the default set: |
| 59 | "${CFLAGS}" |
| 60 | |
| 61 | --configure : configure flags you would like to use instead off the default set: |
| 62 | "${CONFIGURE_FLAGS}" |
| 63 | |
| 64 | --make : make flags you would like to use instead off the default set: |
| 65 | "${MAKE_FLAGS}" |
| 66 | |
| 67 | --scanbuild : scanbuild flags you would like to use instead of the default set: |
| 68 | "${SCANBUILD_FLAGS}" |
| 69 | |
| 70 | --outputdir : directory where scan-build should create the html files. default: |
| 71 | "`pwd`/${OUTPUTDIR}" |
| 72 | |
| 73 | -h|--help : this help |
| 74 | EOF |
| 75 | } |
| 76 | |
| 77 | for i in "$@" |
| 78 | do |
| 79 | case $i in |
| 80 | -c=*|--compiler=*) |
| 81 | COMPILER="${i#*=}" |
| 82 | shift |
| 83 | ;; |
| 84 | --cflags=*) |
| 85 | CFLAGS="${i#*=}" |
| 86 | shift |
| 87 | ;; |
| 88 | --configure=*) |
| 89 | CONFIGURE_FLAGS="${i#*=}" |
| 90 | shift |
| 91 | ;; |
| 92 | --make=*) |
| 93 | MAKE_FLAGS="${i#*=}" |
| 94 | shift |
| 95 | ;; |
| 96 | --scanbuild=*) |
| 97 | SCANBUILD_FLAGS="${i#*=}" |
| 98 | shift |
| 99 | ;; |
| 100 | --outputdir=*) |
| 101 | OUTPUTDIR="${i#*=}" |
| 102 | shift |
| 103 | ;; |
| 104 | -h|--help) |
| 105 | print_usage |
| 106 | exit |
| 107 | ;; |
| 108 | esac |
| 109 | done |
| 110 | |
| 111 | if [ "${COMPILER}" == "clang" ] && [ ! -z "`which clang`" ]; then |
| 112 | CCC_CC="`which`clang" |
| 113 | CCC_CXX="`which clang++`" |
| 114 | CFLAGS="-fblocks ${CFLAGS}" |
| 115 | elif [ "${COMPILER}" == "gcc" ] && [ ! -z "`which gcc`" ]; then |
| 116 | CCC_CC="`which gcc`" |
| 117 | CCC_CXX="`which g++`" |
| 118 | CFLAGS="${CFLAGS}" |
| 119 | else |
| 120 | echo "Unknown compiler: $2, needs to be either clang or gcc" |
| 121 | exit |
| 122 | fi |
| 123 | |
| 124 | if [ ! -f config.status ]; then |
| 125 | echo "Running ./configure ${CONFIGURE_FLAGS} ..." |
| 126 | ${SCANBUILD} ${SCANBUILD_FLAGS} -o ${OUTPUTDIR} ./configure ${CONFIGURE_FLAGS} |
| 127 | if [ $? != 0 ]; then |
| 128 | echo "Configure error occurred, see output / config.log" |
| 129 | exit |
| 130 | fi |
| 131 | make clean |
| 132 | fi |
| 133 | if [ -f config.status ]; then |
| 134 | echo "Running scan-build make ${MAKE_FLAGS} ..." |
| 135 | ${SCANBUILD} ${SCANBUILD_FLAGS} -o ${OUTPUTDIR} make ${MAKE_FLAGS} |
| 136 | fi |