cppcheck.sh 1.84 KB
Newer Older
1
2
#!/bin/bash -ex

3
SCRIPT_DIR=$(dirname "$0")
4
5
6
# If errors slip through to master this can be used to set a non-zero
# allowed count while those errors are dealt with. This avoids breaking all
# builds for all developers
7
ALLOWED_ERRORS_COUNT=1069
8
9
10
11
12
13
14
15
16
17
18
19

if [[ ${JOB_NAME} == *pull_requests* ]]; then
    # This relies on the fact pull requests use pull/$PR-NAME
    # which squashes the branch into a single merge commit
    cd $WORKSPACE

    if ${SCRIPT_DIR}/check_for_changes cpp; then
        echo "No C++ files have changed. Skipping check."
        exit 0
    fi
fi

20
21
22
23
24
25
###############################################################################
# Create the build directory if it doesn't exist
###############################################################################
[ -d $WORKSPACE/build ] || mkdir $WORKSPACE/build
cd $WORKSPACE/build

26
27
28
29
# remove old results if they exist
find -name cppcheck.xml -delete

# configure cmake
30
31
32
33
34
35
36
37
38
if [ $(command -v scl) ]; then
    CMAKE_EXE=cmake3
    SCL_ENABLE="scl enable devtoolset-7"
else
    CMAKE_EXE=cmake
    SCL_ENABLE=""
fi
$SCL_ENABLE "$CMAKE_EXE --version"

39
40
41
42
43
if [ "$(command -v ninja)" ]; then
  CMAKE_GENERATOR="-G Ninja"
elif [ "$(command -v ninja-build)" ]; then
  CMAKE_GENERATOR="-G Ninja"
fi
44
$SCL_ENABLE "$CMAKE_EXE ${CMAKE_GENERATOR} -DCMAKE_BUILD_TYPE=Debug -DCPPCHECK_GENERATE_XML=TRUE -DCPPCHECK_NUM_THREADS=$BUILD_THREADS .."
45
46

# run cppcheck
47
$SCL_ENABLE "$CMAKE_EXE --build . --target cppcheck"
48
49
50
51
52

# Generate HTML report
cppcheck-htmlreport --file=cppcheck.xml --title=Embedded --report-dir=cppcheck-report

# Mark build as passed or failed
53
errors_count=$(grep -c '</error>' cppcheck.xml)
54
if [ $errors_count -ne ${ALLOWED_ERRORS_COUNT} ]; then
55
  echo "CppCheck found ${errors_count} errors."
56
  echo "See CppCheck link on the job page for more detail, or adjust the count."
57
58
59
60
61
  exit 1
else
  echo "CppCheck found no errors"
  exit 0
fi