# Contributor: Luca Weiss <luca@lucaweiss.eu>
# Maintainer:
pkgname=pyside6
pkgver=6.7.2
pkgrel=0
_llvmver=17
pkgdesc="Enables the use of Qt6 APIs in Python applications"
url="https://doc.qt.io/qtforpython-6/"
# riscv64: segfault while building
# loongarch64: blocked by qt6-qtwebview
arch="all !riscv64 !loongarch64"
license="LGPL-3.0-only AND GPL-2.0-only"
makedepends="
	bash
	clang$_llvmver-dev
	clang$_llvmver-extra-tools
	clang$_llvmver-static
	cmake
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	py3-numpy-dev
	py3-setuptools
	py3-wheel
	python3-dev
	qt6-qtbase-dev
	qt6-qtcharts-dev
	qt6-qtconnectivity-dev
	qt6-qtdatavis3d-dev
	qt6-qtdeclarative-dev
	qt6-qthttpserver-dev
	qt6-qtmultimedia-dev
	qt6-qtnetworkauth-dev
	qt6-qtpositioning-dev
	qt6-qtquick3d-dev
	qt6-qtremoteobjects-dev
	qt6-qtscxml-dev
	qt6-qtsensors-dev
	qt6-qtserialport-dev
	qt6-qtspeech-dev
	qt6-qtsvg-dev
	qt6-qttools-dev
	qt6-qtwebchannel-dev
	qt6-qtwebsockets-dev
	samurai
	"
subpackages="
	py3-pyside6:pyside
	py3-shiboken6:pyshiboken
	$pkgname-dev
	"
source="$pkgver-$pkgname.tar.xz::https://download.qt.io/official_releases/QtForPython/pyside6/PySide6-$pkgver-src/pyside-setup-everywhere-src-$pkgver.tar.xz"
builddir="$srcdir/pyside-setup-everywhere-src-$pkgver"
options="!check" # Tests fail

case "$CARCH" in
	armhf|ppc64le|s390x) makedepends="$makedepends qt6-qt3d-dev" ;;
	x86) makedepends="$makedepends qt6-qtwebview-dev" ;;
	*) makedepends="$makedepends qt6-qt3d-dev qt6-qtwebview-dev" ;;
esac

build() {
	export LLVM_INSTALL_DIR=/usr/lib/llvm$_llvmver
	CFLAGS="$CFLAGS $(pkg-config --cflags python3-embed) -flto=auto" \
	CXXFLAGS="$CXXFLAGS -I/usr/lib/llvm$_llvmver/include -flto=auto" \
	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_SKIP_RPATH=ON \
		-DBUILD_SHARED_LIBS=True \
		-DSHIBOKEN_PYTHON_LIBRARIES="$(pkg-config --libs python3-embed)" \
		-DPYTHON_EXECUTABLE=/usr/bin/python3 \
		-DBUILD_TESTS=OFF

	PYTHONPATH="$PWD/build/sources" \
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build/sources/pyside6
	DESTDIR="$pkgdir" cmake --install build/sources/shiboken6

	# Install egg info
	export PATH="/usr/lib/qt6/bin:$PATH"
	local pythonpath="$(python3 -c "from sysconfig import get_path; print(get_path('platlib'))")"
	python3 setup.py egg_info --build-type=pyside6
	python3 setup.py egg_info --build-type=shiboken6
	python3 setup.py egg_info --build-type=shiboken6-generator
	cp -r PySide6.egg-info "$pkgdir/$pythonpath"
	cp -r shiboken6*.egg-info "$pkgdir/$pythonpath"
}

pyshiboken() {
	pkgdesc="qt shiboken6 python module"

	amove usr/bin # shiboken6 build tool
	amove usr/lib/libshiboken6* # python object
	amove usr/lib/python3.*/site-packages/shiboken6*
}

pyside() {
	pkgdesc="qt pyside6 python module"

	amove usr/lib/libpyside6* # python object
	amove usr/lib/python3.*/site-packages/PySide6*
	amove usr/share/PySide6
}

sha512sums="
4376aba85867b5a04dab9d8889f5e32cb503cb8010757c1e810e60d29755f15a002b582594d0737a4119c030b48ede26964e8b1392700a6c061d7d631cdfe73e  6.7.2-pyside6.tar.xz
"
