1#! /bin/sh
2#
3# Routine sanity checks for libpqxx source tree.
4#
5# Usage: lint [srcdir]
6#
7# srcdir is the source directory; it defaults to the current directory but
8# may be set for out-of-tree builds.
9
10set -e
11set -u
12
13# Run from source directory.  We don't write any artefacts.  Without this,
14# out-of-tree builds will fail to find the files this script needs to read.
15cd $(dirname "$0")/..
16
17PQXXVERSION="$(./tools/extract_version)"
18
19# This version must be at the top of the NEWS file.
20check_news_version() {
21	if ! head -n1 NEWS | grep -q "^$PQXXVERSION\$"
22	then
23		cat <<EOF >&2
24Version $PQXXVERSION is not at the top of NEWS.
25EOF
26		exit 1
27	fi
28}
29
30
31# Count number of times header $1 is included from each of given input files.
32# Output is lines of <filename>:<count>, one line per file, sorted.
33count_includes() {
34	local HEADER_NAME WS PAT
35	HEADER_NAME="$1"
36	shift
37	WS="[[:space:]]*"
38	PAT="^${WS}#${WS}include${WS}[<\"]$HEADER_NAME[>\"]"
39	grep -c "$PAT" $* | sort
40}
41
42
43# Any file that includes compiler-internal-pre.hxx must also include
44# compiler-internal-post.hxx, and vice versa.
45check_compiler_internal_headers() {
46	local TEMPDIR PRE POST HEADERS
47	TEMPDIR="$(mktemp -d)"
48	if test -z "$TEMPDIR"
49	then
50		echo >&2 "Could not create temporary directory."
51                exit 1
52	fi
53	PRE="$TEMPDIR/pre"
54	POST="$TEMPDIR/post"
55	HEADERS=$(find include/pqxx/* -type f | grep -v '\.swp$')
56	count_includes pqxx/internal/compiler-internal-pre.hxx $HEADERS >"$PRE"
57	count_includes pqxx/internal/compiler-internal-post.hxx $HEADERS >"$POST"
58	DIFF="$(diff "$PRE" "$POST")" || /bin/true
59	rm -r -- "$TEMPDIR"
60	if test -n "$DIFF"
61	then
62		cat <<EOF >&2
63The number of inclusions of compiler-internal.post.hxx does not match the number
64of inclusions of compiler-internal.pre.hxx:
65
66$DIFF
67EOF
68		exit 1
69	fi
70}
71
72
73cpplint() {
74        echo "Skipping cppcheck; it's not up to date with C++17."
75	# if which cppcheck >/dev/null
76	# then
77	# 	cppcheck -q -j$(nproc) -I include src/*.cxx test/*.cxx test/unit/*.cxx
78	# fi
79}
80
81
82pylint() {
83	local PYFILES="tools/*.py tools/splitconfig"
84        echo "Skipping pocketlint; it's not up to date with Python3."
85	# if which pocketlint >/dev/null
86	# then
87	# 	pocketlint $PYFILES
88	# fi
89
90        echo "Skipping flake8; it's not up to date with Python3."
91	# if which flake8 >/dev/null
92	# then
93	# 	flake8 $PYFILES
94	# fi
95}
96
97cpplint
98pylint
99check_news_version
100check_compiler_internal_headers
101