3 # This script automatically runs the basic cppcheck and sloccount tools to generate a static analysis report.
7 echo "Usage: run-cppcheck.sh [options] [files]"
8 echo "Option: Description"
9 echo " --no-sloccount Don't run sloccount"
10 echo " --no-cppcheck Don't run cppcheck"
11 echo " --cppcheck-out <file> Output file for cppcheck step. Default cppcheck.xml"
12 echo " --sloccount-out <file> Output file for sloccount step. Default sloccount.sc"
14 echo " files Files to run cppcheck through. Default src/**"
19 # Check presence of cppcheck on the path
20 if [ "$RunCppCheck" == true ]
22 hash cppcheck 2>/dev/null || { echo >&2 "Please install cppcheck before running this script"; exit 1; }
25 # Check presence of sloccount on the path
26 if [ "$RunSlocCount" == true ]
28 hash sloccount 2>/dev/null || { echo >&2 "Please install sloccount before running this script"; exit 1; }
32 ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
35 Files="$ProjectRoot/src/**"
37 CppCheckOutput="cppcheck.xml"
38 SloccountOutput="sloccount.sc"
39 # Get the number of processors available to the scheduler
40 # Other techniques such as `nproc` only get the number of
41 # processors available to a single process.
42 if [ `uname` = "FreeBSD" ]; then
43 NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
44 elif [ `uname` = "NetBSD" ]; then
45 NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
47 NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
74 echo "Unrecognized option: $opt"
79 FilesFromArgs="$FilesFromArgs $opt"
83 if [ "$FilesFromArgs" != "" ];
88 if [ "$CppCheckOutput" == "" ];
90 echo "Expected: file for cppcheck output"
95 if [ "$SloccountOutput" == "" ];
97 echo "Expected: file for sloccount output"
104 if [ "$RunCppCheck" == true ]
106 echo "Running cppcheck for files: $Files"
107 cppcheck --enable=all -j $NumProc --xml --xml-version=2 --force $Files 2> $CppCheckOutput
108 CppCheckOutputs="$CppCheckOutput (cppcheck)"
111 if [ "$RunSlocCount" == true ]
113 echo "Running sloccount for files: $Files"
114 sloccount --wide --details $Files > $SloccountOutput
115 SlocCountOutputs="$SloccountOutput (sloccount)"
118 echo Check finished. Results can be found in: $CppCheckOutputs $SlocCountOutputs