summaryrefslogtreecommitdiff
path: root/cppcheck/cppcheck-report.sh
blob: 2e6090dea784f9a20e7d9fefe9738ae78c8ab2be (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env bash
# -*- tab-width : 4; indent-tabs-mode : nil -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

[ "$DEBUG" ] && set -xv

# save stdout and stderr
exec 3>&1 4>&2 >/tmp/foo.log 2>&1

# redirect to log file
exec > /tmp/cppcheck-report.log
exec 2>&1

#
# Functions
#

die()
{
    MESSAGE="$*"
    echo "Error: ${MESSAGE?}" >&2
    mail_failure "${MESSAGE?}"
    exit -1;
}

run_cppcheck()
{
    pushd "${LO_SRC_DIR?}" > /dev/null || die "Failed to change directory to ${LO_SRC_DIR?}"

    "${CPPCHECK_DIR?}"/cppcheck -i external/ -i workdir/ --xml --xml-version=2 --inline-suppr --enable=all --max-configs=25 ./ 2> "${DATA_DIR?}"/err.xml \
    || die "Failed to run cppcheck."

    "${CPPCHECK_DIR?}"/htmlreport/cppcheck-htmlreport --file="${DATA_DIR?}"/err.xml --title="LibreOffice ${COMMIT_DATE_LO?} ${COMMIT_TIME_LO?} ${COMMIT_SHA1_LO?}, CppCheck ${COMMIT_DATE_CPPCHECK?} ${COMMIT_TIME_CPPCHECK?} ${COMMIT_SHA1_CPPCHECK?}" --report-dir="${HTML_DIR?}" --source-dir=. \
    || die "Failed to run cppcheck-htmlreport."

    popd > /dev/null || die "Failed to change directory out of ${LO_SRC_DIR?}"
}

get_cppcheck_src()
{
    if [ ! -d "${CPPCHECK_DIR?}" ]; then
        git clone "${CPPCHECK_GIT_URL?}" "${CPPCHECK_DIR?}" || die "Failed to git clone ${CPPCHECK_GIT_URL?} in ${CPPCHECK_DIR?}"
    else
        if [ ! -d "${CPPCHECK_DIR?}"/.git ] ; then
            die "${CPPCHECK_DIR?} is not a git repository"
        else
            pushd "${CPPCHECK_DIR?}" || die "Failed to change directory to ${CPPCHECK_DIR?}"
            git pull || die "Failed to update git repository ${CPPCHECK_DIR?}"
            popd > /dev/null || die "Failed to change directory out of ${CPPCHECK_DIR?}"
        fi
    fi
}

get_lo_src()
{
    if [ ! -d "${LO_SRC_DIR?}" ]; then
        git clone "${LO_GIT_URL?}" "${LO_SRC_DIR?}" || die "Failed to git clone ${LO_GIT_URL?} in ${LO_SRC_DIR?}"
    else
        if [ ! -d "${LO_SRC_DIR?}"/.git ] ; then
            die "${LO_SRC_DIR?} is not a git repository"
        else
            pushd "${LO_SRC_DIR?}" || die "Failed to change directory to ${LO_SRC_DIR?}"
            git pull || die "Failed to update git repository ${LO_SRC_DIR?}"
            popd > /dev/null || die "Failed to change directory out of ${LO_SRC_DIR?}"
        fi
    fi
}

build_cppcheck()
{
    pushd "${CPPCHECK_DIR?}" > /dev/null || die "Failed to change directory to ${CPPCHECK_DIR?}"
    # cppcheck is quite small so it can be built from scratch to avoid some pb
    make clean
    make all || die "Failed to build cppcheck."
    popd > /dev/null || die "Failed to change directory out of ${CPPCHECK_DIR?}"
}

get_commit_lo()
{
    pushd "${LO_SRC_DIR?}" > /dev/null || die "Failed to change directory to ${LO_SRC_DIR?}"

    COMMIT_SHA1_LO=$(git log --date=iso | head -5 | awk '/^commit/ {print $2}')
    COMMIT_DATE_LO=$(git log --date=iso | head -5 | awk '/^Date/ {print $2}')
    COMMIT_TIME_LO=$(git log --date=iso | head -5 | awk '/^Date/ {print $3}')

    popd > /dev/null || die "Failed to change directory out of ${LO_SRC_DIR?}"
}

get_commit_cppcheck()
{
    pushd "${CPPCHECK_DIR?}" > /dev/null || die "Failed to change directory to ${CPPCHECK_DIR?}"

    COMMIT_SHA1_CPPCHECK=$(git log --date=iso | head -5 | awk '/^commit/ {print $2}')
    COMMIT_DATE_CPPCHECK=$(git log --date=iso | head -5 | awk '/^Date/ {print $2}')
    COMMIT_TIME_CPPCHECK=$(git log --date=iso | head -5 | awk '/^Date/ {print $3}')

    popd > /dev/null || die "Failed to change directory out of ${CPPCHECK_DIR?}"
}

upload_report()
{
    ssh upload@dev-builds.libreoffice.org rm -rf "${UPLOAD_DIR?}" || die "Failed to remove directory ${UPLOAD_DIR?}"
    ssh upload@dev-builds.libreoffice.org mkdir -p "${UPLOAD_DIR?}" || die "Failed to create directory ${UPLOAD_DIR?}"
    scp -r "${HTML_DIR?}"/* upload@dev-builds.libreoffice.org:"${UPLOAD_DIR?}"/ || die "Failed to upload report to ${UPLOAD_DIR?}"
}

mail_success()
{
cat > "$EMAIL_BODY" <<EOF

A new cppcheck report is available at : http://dev-builds.libreoffice.org/cppcheck_reports/master/


Note:
    The script generating this report was run at :
        $(date "+%Y-%d-%m %H:%M:%S") with user $(whoami) at host $(cat /etc/hostname) as $MY_NAME $MY_ARGS

    It can be found and improved here:
        https://gerrit.libreoffice.org/gitweb?p=dev-tools.git;a=blob;f=cppcheck/cppcheck-report.sh

EOF


    "$SENDEMAIL" -o message-file="$EMAIL_BODY" -f "$MAILFROM" -t "$MAILTO" -o tls=yes -s smtp.gmail.com:587 -xu cppcheck.libreoffice@gmail.com -xp "$SMTP_PWD" -u "CppCheck Report Update"

}

mail_failure()
{
    MESSAGE="$*"

    if [ -f /tmp/cppcheck-report.log ] ; then
        cp -f /tmp/cppcheck-report.log "$HOME"/cppcheck-report.log
        rm -f "$HOME"/cppcheck-report.log.gz
        gzip "$HOME"/cppcheck-report.log
    fi

cat > "$EMAIL_BODY" <<EOF

The cppcheck job failed with message: "${MESSAGE?}"


Note:
    The script generating this report was run at :
        $(date "+%Y-%d-%m %H:%M:%S") with user $(whoami) at host $(cat /etc/hostname) as $MY_NAME $MY_ARGS

    It can be found and improved here:
        https://gerrit.libreoffice.org/gitweb?p=dev-tools.git;a=blob;f=cppcheck/cppcheck-report.sh

EOF

    "$SENDEMAIL" -o message-file="$EMAIL_BODY" -f "$MAILFROM" -t "$MAILTO" -o tls=yes -s smtp.gmail.com:587 -xu cppcheck.libreoffice@gmail.com -xp "$SMTP_PWD" -u "CppCheck Report Failure" -a /home/buildslave/cppcheck-report.log.gz

}

usage()
{
    # restore stdout and stderr
    exec 1>&3 2>&4

    echo >&2 "Usage: lcov-report.sh -s [DIRECTORY] -w [DIRECTORY]
        -s    source code directory
        -w    html (www) directory
        -c    ccpcheck (git) directory."
    exit 1
}

#
# Main
#

if [ "$#" = "0" ] ; then
    usage
fi

# Set some defaults here
LO_SRC_DIR="$HOME"/source/libo-core
HTML_DIR="$HOME"/tmp/www
CPPCHECK_DIR="$HOME"/source/cppcheck
DATA_DIR="$HOME"/tmp
CPPCHECK_GIT_URL="git://github.com/danmar/cppcheck.git"
LO_GIT_URL="git://anongit.freedesktop.org/libreoffice/core.git"
UPLOAD_DIR=/srv/www/dev-builds.libreoffice.org/cppcheck_reports/master
MAILTO=libreoffice@lists.freedesktop.org
MAILFROM=cppcheck.libreoffice@gmail.com
MY_NAME=$(readlink -f "$0")
MY_ARGS="$*"
MESSAGE=
SENDEMAIL="$HOME"/source/buildbot/bin/sendEmail
EMAIL_BODY="$HOME"/tmp/email_body
# Dont forget to set SMTP_PWD in your "$HOME"/.cppcheckrc !
SMTP_PWD=
export PYTHONIOENCODING=UTF-8

export LANG=en_US.UTF-8
export LC_CTYPE="en_US.UTF-8"
export LC_NUMERIC="en_US.UTF-8"
export LC_TIME="en_US.UTF-8"
export LC_COLLATE="en_US.UTF-8"
export LC_MONETARY="en_US.UTF-8"
export LC_MESSAGES="en_US.UTF-8"
export LC_PAPER="en_US.UTF-8"
export LC_NAME="en_US.UTF-8"
export LC_ADDRESS="en_US.UTF-8"
export LC_TELEPHONE="en_US.UTF-8"
export LC_MEASUREMENT="en_US.UTF-8"
export LC_IDENTIFICATION="en_US.UTF-8"
export LC_ALL=en_US.UTF-8

if [ -f "$HOME"/.cppcheckrc ]; then
    # override all default vars with entries in "$HOME"/.cppcheckrc
    source "$HOME"/.cppcheckrc
else
    die "Failed to locate $HOME/.cppcheckrc"
fi


# override cppcheckrc and defaults with commandline settings
while getopts ":s:w:c:" opt ; do
    case "$opt" in
    s)
        LO_SRC_DIR="${OPTARG?}"
        ;;
    w)
        HTML_DIR="${OPTARG?}"
        ;;
    c)
        CPPCHECK_DIR="${OPTARG?}"
        ;;
    *)
        usage
        ;;
    esac
done


if [ ! -f "$SENDEMAIL" ] ; then
    echo "Error: sendEmail command $SENDEMAIL not found." >&2
    exit -1;
fi


get_lo_src
get_cppcheck_src
get_commit_cppcheck
get_commit_lo
build_cppcheck
run_cppcheck
upload_report
mail_success