# Template file for 'qemu-user'
# This package should be updated together with qemu
pkgname=qemu-user
version=11.1.0
revision=1
archs="~i686* ~arm*"
build_style=configure
metapackage=yes
configure_args="--prefix=/usr --sysconfdir=/etc --libexecdir=/usr/libexec
 --enable-linux-user --disable-system --static --disable-download
 -Dkvm=disabled -Dpng=disabled -Dvirtfs=disabled -Dfdt=disabled -Dseccomp=disabled
 -Dtools=disabled"
hostmakedepends="meson flex glib-devel pkg-config perl python3-pycotap
 python3-wheel python3-qemu.qmp"
makedepends="dtc-devel glib-devel pixman-devel libuuid-devel"
short_desc="QEMU user-mode emulators"
maintainer="classabbyamp <void@placeviolette.net>"
license="GPL-2.0-only, LGPL-2.1-only"
homepage="https://www.qemu.org"
distfiles="https://wiki.qemu.org/download/qemu-${version}.tar.bz2"
checksum=690009fe8572ae9fa5066abf3374aaab989def33a59e37f6588a520c538991d9
subpackages="qemu-user-static"

if [ "$CROSS_BUILD" ]; then
	configure_args+=" --cross-prefix=${XBPS_CROSS_TRIPLET}-"
fi

_archs=(
 arm
 armeb
 hexagon
 i386
 m68k
 microblaze
 microblazeel
 mips
 mipsel
 or1k
 ppc
 riscv32
 sh4
 sh4eb
 sparc
 xtensa
 xtensaeb
 aarch64
 aarch64_be
 alpha
 hppa
 loongarch64
 mips64
 mips64el
 mipsn32
 mipsn32el
 ppc64
 ppc64le
 riscv64
 s390x
 sparc32plus
 sparc64
 x86_64
)

for _arch in "${_archs[@]}"; do
	# bug in xbps: it hates package names ending in _N
	_pkgname="qemu-user-${_arch//x86_64/amd64}"

	subpackages+=" ${_pkgname}"
	depends+=" ${_pkgname}>=${version}"

	case "$_arch" in
	i386)
		_extra_binfmts="i486"
		;;
	esac

	eval "${_pkgname}_package() {
		_user_tmpl '$_arch' '$_extra_binfmts'
	}"
done

export PYTHONUNBUFFERED=1

_user_tmpl() {
	_arch="$1"
	_extra_binfmts="$2"
	short_desc="QEMU ${_arch} user-mode emulator"
	pkg_install() {
		vmove "usr/bin/qemu-$_arch"
		vmove "usr/bin/qemu-$_arch-static"
		vmove "usr/share/binfmts/qemu-$_arch" || :
		for bf in $_extra_binfmts; do
			vmove "usr/share/binfmts/qemu-$bf" || :
		done
	}
}

post_extract() {
	# remove vendored wheels, these should come from (host)makedepends
	rm -r python/wheels
}

post_install() {
	# provided by qemu
	rm -rf ${DESTDIR}/etc ${DESTDIR}/usr/share ${DESTDIR}/usr/libexec ${DESTDIR}/usr/include
	for f in ${DESTDIR}/usr/bin/*; do
		ln -s ${f##*/} ${f}-static
	done

	vmkdir usr/share/binfmts
	HOST_ARCH="${XBPS_TARGET_MACHINE%-musl}" scripts/qemu-binfmt-conf.sh --debian \
		--exportdir "${DESTDIR}"/usr/share/binfmts --qemu-path /usr/bin \
		--preserve-argv0 yes --persistent yes --credential yes
}

qemu-user-static_package() {
	metapackage=yes
	short_desc+=" (transitional dummy package)"
	depends="${sourcepkg}>=${version}_${revision}"
}
