# Contributor: August Klein <amatcoder@gmail.com>
# Maintainer: André Klitzing <aklitzing@gmail.com>
pkgname=cppcheck
pkgver=2.14.2
pkgrel=1
pkgdesc="Static analysis tool for C/C++ code"
url="https://cppcheck.sourceforge.io/"
arch="all"
license="GPL-3.0-or-later"
makedepends="
	cmake
	docbook-xsl
	pcre-dev
	python3
	samurai
	tinyxml2-dev
	"
subpackages="$pkgname-doc $pkgname-htmlreport::noarch"
source="$pkgname-$pkgver.tar.gz::https://github.com/danmar/cppcheck/archive/refs/tags/$pkgver.tar.gz
	set_datadir.patch
	feenableexcept.patch
	"

case "$CARCH" in
	armhf) ;; # qt6-qtdeclarative
	*)
		makedepends="$makedepends qt6-qtcharts-dev qt6-qttools-dev"
		subpackages="$subpackages $pkgname-gui"
		_opts="-DBUILD_GUI=ON -DUSE_QT6=ON -DWITH_QCHART=ON"
		;;
esac

build() {
	make DB2MAN=/usr/share/xml/docbook/xsl-stylesheets-*/manpages/docbook.xsl man

	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DBUILD_TESTS="$(want_check && echo ON || echo OFF)" \
		-DFILESDIR=/usr/share/cppcheck \
		-DHAVE_RULES=ON \
		-DUSE_BUNDLED_TINYXML2=OFF \
		-DUSE_MATCHCOMPILER=On \
		$_opts

	cmake --build build
}

check() {
	# fails outside of x86 with:
	# Expected:
	# [test.cpp:5]: (style) Condition 'buffer.back()=='\0'' is always false\n
	# Actual:
	# [test.cpp:3] -> [test.cpp:5]: (style) Condition 'buffer.back()=='\0'' is always false\n
	# TestThreadExecutor: flaky result

	ctest -E "(TestCondition|TestThreadExecutor)" --test-dir build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	install -Dm644 cppcheck.1 -t "$pkgdir"/usr/share/man/man1
	install -Dm755 htmlreport/cppcheck-htmlreport -t "$pkgdir"/usr/bin
}

htmlreport() {
	pkgdesc="Utility to generate a html report of a XML file produced by cppcheck"
	depends="$pkgname=$pkgver-r$pkgrel python3 py3-pygments"

	amove usr/bin/cppcheck-htmlreport
}

gui() {
	pkgdesc="Qt gui for cppcheck"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/cppcheck-gui
	amove usr/share/icons
	amove usr/share/applications

	mkdir -p "$subpkgdir"/usr/share/cppcheck/lang
	mv "$pkgdir"/usr/bin/cppcheck*.qm "$subpkgdir"/usr/share/cppcheck/lang
}

sha512sums="
8e90fa87845a083a4de66ec86edf707184a2293eb346fd79bfcafb73f93db049216472fcb2ab802ce529680f9708261ba3c26675c6fd9637e1dc83903bc44c4b  cppcheck-2.14.2.tar.gz
2e1d1fe1ba66c091cdd33f2c883370ea216718b2e7b997717623dd36cd73a9b82fa116144d93b7a4f3ab6bfea1c49274c950e751fdda45a61160d3cef913ea88  set_datadir.patch
d483e39b4b5b6482081f656708433b6f45da3e9e7ba4adf9c6ff347273cae7cc6bf854248d460278f39054b36582aa009d9ce3b87d55dc4efe1f45cc865c6cc4  feenableexcept.patch
"
