forked from alisw/alidist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flpprotocheckcode.sh
92 lines (81 loc) · 4.78 KB
/
flpprotocheckcode.sh
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
package: flpprotocheckcode
version: "1.0"
requires:
- flpproto
- o2codechecker
build_requires:
- CMake
force_rebuild: 1
---
#!/bin/bash -e
# Runs a set of (selected) code checks on one of the FLP Prototype modules
# MODULE_TO_CHECK env var specifies module name that will be checked, it must be set by user
# Assumes that compile_commands.json file is available in the INSTALLDIR of the module
: ${MODULE_TO_CHECK?"MODULE_TO_CHECK env variable needs to be set"}
REPO_ROOT=$(eval echo "\$${MODULE_TO_CHECK}_ROOT")
REPO_VERSION=$(eval echo "\$${MODULE_TO_CHECK}_VERSION")
cp "${REPO_ROOT}"/compile_commands.json .
sed -ie "s|BUILD/[^/]*|SOURCES|g" compile_commands.json
# We will try to setup a list of files to be checked by using 2 specific Git commits to compare
# Heuristically guess source directory of the module
O2_SRC=$(python -c 'import json,sys,os; sys.stdout.write( os.path.commonprefix([ x["file"] for x in json.loads(open("compile_commands.json").read()) if not "G__" in x["file"] and x["file"].endswith(".cxx") ]) )')
[[ -e "$O2_SRC"/CMakeLists.txt && -d "$O2_SRC"/.git ]]
# We have something to compare our working directory to (ALIBUILD_BASE_HASH). We check only the
# changed files (including the untracked ones) if the list of relevant files that changed is up to
# 50 entries long
if [[ $ALIBUILD_BASE_HASH ]]; then
pushd "$O2_SRC"
( git diff --name-only $ALIBUILD_BASE_HASH${ALIBUILD_HEAD_HASH:+...$ALIBUILD_HEAD_HASH} ; git ls-files --others --exclude-standard ) | grep -E '\.cxx$|\.h$' | sort -u > $BUILDDIR/changed
if [[ $(cat $BUILDDIR/changed | wc -l) -le 50 ]]; then
O2_CHECKCODE_CHANGEDFILES=$(while read FILE; do [[ -e "$O2_SRC/$FILE" ]] && echo "$FILE" || true; done < <(cat $BUILDDIR/changed) | \
xargs echo | sed -e 's/ /:/g')
if [[ ! $O2_CHECKCODE_CHANGEDFILES ]]; then
echo "Nothing changed with respect to base commit: not checking anything"
exit 0
fi
fi
popd
fi
# Call a tool to filter out unwanted sources (ROOT dicts, etc) from the compilations database.
# Further, also optionally restrict the checks on a specific set of files, which can be passed here via means of the
# environment variable O2_CHECKCODE_CHANGEDFILES. The environment variable is supposed to hold a colon separated list of files.
ThinCompilationsDatabase.py -exclude-files '(?:.*G\_\_.*\.cxx|.*\.pb.cc)' ${O2_CHECKCODE_CHANGEDFILES:+-use-files ${O2_CHECKCODE_CHANGEDFILES}}
cp thinned_compile_commands.json compile_commands.json
# List of enabled C++ checks (make sure they are all green)
CHECKS="${O2_CHECKER_CHECKS:--*,modernize-*,-modernize-use-default,-modernize-pass-by-value,-modernize-use-auto,-modernize-use-bool-literals,-modernize-use-using,-modernize-loop-convert,-modernize-use-bool-literals,-modernize-make-unique,aliceO2-member-name}"
# Run C++ checks
run_O2CodeChecker.py -clang-tidy-binary $(which O2codecheck) -header-filter=.*SOURCES.* ${O2_CHECKER_FIX:+-fix} -checks=${CHECKS} 2>&1 | tee error-log.txt
# Turn warnings into errors
sed -e 's/ warning:/ error:/g' error-log.txt > error-log.txt.0 && mv error-log.txt.0 error-log.txt
# Run copyright notice check
COPYRIGHT="$(cat <<'EOF'
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
EOF
)"
COPYRIGHT_LINES=$(echo "$COPYRIGHT" | wc -l)
COPYRIGHT_EXCLUDE_REGEXP="^Framework/DebugGUI/" # exclude files from the copyright check
set +x
while read FILE; do
[[ ${FILE:0:2} != "./" ]] || FILE=${FILE:2}
[[ ! $FILE =~ $COPYRIGHT_EXCLUDE_REGEXP ]] || continue
FILE="$O2_SRC/$FILE"
[[ "$(head -n$COPYRIGHT_LINES "$FILE")" == "$COPYRIGHT" ]] || { printf "$FILE:1:1: error: missing or malformed copyright notice\n" >> error-log.txt; }
done < <([[ $O2_CHECKCODE_CHANGEDFILES ]] && echo "$O2_CHECKCODE_CHANGEDFILES" | sed -e 's/:/\n/g' \
|| (cd "$O2_SRC"; find . -name '*.cxx' -o -name '*.h'))
# Tell user what to do in case of copyright notice error
if grep -q "malformed copyright notice" error-log.txt; then
printf "\nerror: Some files are missing the correct copyright notice on top.\n"
printf "error: Make sure all your source files begin with the following exact lines:\nerror:\n"
while read LINE; do printf "error: $LINE\n"; done < <(echo "$COPYRIGHT")
printf "error:\nerror: List of non-compliant files will follow.\n\n"
fi
# Filter the actual errors from the log. Break with nonzero if errors are found
! ( grep " error:" error-log.txt )