# Template file for 'vtk'
pkgname=vtk
version=9.3.1
revision=3
build_style=cmake
# vtk can be huge, especially with -DVTK_BUILD_ALL_MODULES=ON"
# Build only the core modules plus python bindings for now
configure_args="-DBUILD_SHARED_LIBS=ON -DVTK_FORBID_DOWNLOADS=ON
 -DVTK_USE_EXTERNAL=ON
 -DVTK_MODULE_USE_EXTERNAL_VTK_utf8=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_pegtl=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_libharu=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_exprtk=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_ioss=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_cgns=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_verdict=OFF
 -DVTK_MODULE_USE_EXTERNAL_VTK_fast_float=OFF
 -DVTK_WRAP_PYTHON=ON -DVTK_PYTHON_VERSION=3"
# vtk forks libharu, bumps to v2.4.0, and requires libharu>=2.4.0
makedepends="zlib-devel freetype-devel liblz4-devel expat-devel MesaLib-devel
 libXt-devel libjpeg-turbo-devel tiff-devel hdf5-devel netcdf-devel
 libxml2-devel jsoncpp-devel libogg-devel libtheora-devel
 eigen double-conversion-devel glew-devel pugixml-devel sqlite-devel
 gl2ps-devel proj-devel python3-devel json-c++ fmt-devel"
short_desc="System for 3D computer graphics, image processing, and visualization"
maintainer="Piraty <mail@piraty.dev>"
license="BSD-3-Clause"
homepage="https://www.vtk.org"
distfiles="https://www.vtk.org/files/release/${version:0:3}/VTK-${version}.tar.gz"
checksum=8354ec084ea0d2dc3d23dbe4243823c4bfc270382d0ce8d658939fd50061cab8
nocross="It seems to need vtk compile tools for the host"

case "$XBPS_TARGET_MACHINE" in
	# List of supported architectures copied from openmpi.
	x86_64*|aarch64*|ppc64*|riscv64*)
		makedepends+=" openmpi-devel"
		;;
esac

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" libexecinfo-devel"
	LDFLAGS="-lexecinfo"
fi

post_extract() {
	[ "$XBPS_TARGET_NO_ATOMIC8" ] || return 0

	echo "vtk_module_link(VTK::CommonDataModel PRIVATE atomic)" >> \
		Common/DataModel/CMakeLists.txt
}

post_install() {
	vlicense Copyright.txt

	# Mangle CPython extension names in CMake like xbps-src will do
	vsed -e 's,\(vtkmodules/vtk.*\)\.cpython-.*\.so,\1.so,' \
		-i "${DESTDIR}/usr/lib/cmake/vtk-${version:0:3}/VTKPython-targets-none.cmake"
}

vtk-devel_package() {
	short_desc+=" - development files"
	depends="vtk-python3>=${version}_${revision} ${makedepends}"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/*.so
		vmove usr/lib/cmake
	}
}

vtk-python3_package() {
	short_desc+=" - Python3 bindings"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/bin/vtkpython
		vmove "${py3_sitelib}"
		vmove "usr/lib/lib*Python*.so.*"
		vmove "usr/lib/vtk-${version:0:3}/hierarchy/VTK/vtk*Python*.txt"
	}
}
