summaryrefslogtreecommitdiff
path: root/run-cppcheck.sh
blob: c505fd184dedc3d6af462eaaa07768d21527b61e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash

# This script automatically runs the basic cppcheck and sloccount tools to generate a static analysis report.

usage()
{
    echo "Usage: run-cppcheck.sh [options] [files]"
    echo "Option:                   Description"
    echo "  --no-sloccount          Don't run sloccount"
    echo "  --no-cppcheck           Don't run cppcheck"
    echo "  --cppcheck-out <file>   Output file for cppcheck step.  Default cppcheck.xml"
    echo "  --sloccount-out <file>  Output file for sloccount step.  Default sloccount.sc"
    echo "Files:"
    echo "  files                   Files to run cppcheck through.  Default src/**"
}

check_dependencies()
{
    # Check presence of cppcheck on the path
    if [ "$RunCppCheck" == true ]
    then
        hash cppcheck 2>/dev/null || { echo >&2 "Please install cppcheck before running this script"; exit 1; }
    fi
    
    # Check presence of sloccount on the path
    if [ "$RunSlocCount" == true ]
    then
        hash sloccount 2>/dev/null || { echo >&2 "Please install sloccount before running this script"; exit 1; }
    fi
}

ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
RunSlocCount=true
RunCppCheck=true
Files="$ProjectRoot/src/**"
FilesFromArgs=""
CppCheckOutput="cppcheck.xml"
SloccountOutput="sloccount.sc"
# Get the number of processors available to the scheduler
# Other techniques such as `nproc` only get the number of
# processors available to a single process.
if [ `uname` = "FreeBSD" ]; then
NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
elif [ `uname` = "NetBSD" ]; then
NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
else
NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
fi

while [[ $# > 0 ]]
do
    opt="$1"
    shift
    case $opt in
        -?|-h|--help)
        usage
        exit 1
        ;;
        --no-sloccount)
        RunSlocCount=false
        ;;
        --no-cppcheck)
        RunCppCheck=false
        ;;
        --cppcheck-out)
        CppCheckOutput=$1
        shift
        ;;
        --sloccount-out)
        SloccountOutput=$1
        shift
        ;;
        --*)
        echo "Unrecognized option: $opt"
        usage
        exit 1
        ;;
        *)
        FilesFromArgs="$FilesFromArgs $opt"
    esac
done

if [ "$FilesFromArgs" != "" ];
then
    Files=$FilesFromArgs
fi

if [ "$CppCheckOutput" == "" ];
then
    echo "Expected: file for cppcheck output"
    usage
    exit 1
fi

if [ "$SloccountOutput" == "" ];
then
    echo "Expected: file for sloccount output"
    usage
    exit 1
fi

check_dependencies

if [ "$RunCppCheck" == true ]
then
    echo "Running cppcheck for files: $Files"
    cppcheck --enable=all -j $NumProc --xml --xml-version=2 --force $Files 2> $CppCheckOutput
    CppCheckOutputs="$CppCheckOutput (cppcheck)"
fi

if [ "$RunSlocCount" == true ]
then
    echo "Running sloccount for files: $Files"
    sloccount --wide --details $Files > $SloccountOutput
    SlocCountOutputs="$SloccountOutput (sloccount)"
fi

echo Check finished.  Results can be found in: $CppCheckOutputs $SlocCountOutputs
exit 0