# Template file for 'qemu-user'
# This package should be updated together with qemu
pkgname=qemu-user
version=9.2.2
revision=1
build_style=meta
configure_args="--prefix=/usr --sysconfdir=/etc --libexecdir=/usr/libexec
 --enable-linux-user --disable-system --static
 -Dkvm=disabled -Dpng=disabled -Dvirtfs=disabled -Dfdt=disabled -Dseccomp=disabled
 -Dtools=disabled"
hostmakedepends="meson flex glib-devel pkg-config perl python3-pycotap"
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=29cdfb019c14f5669d16bb952d2a324daf715db82aa2d70569f125a8650669a0
subpackages="qemu-user-static"

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

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

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}"

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

. /void-packages/common/build-style/configure.sh 2>/dev/null

export PYTHONUNBUFFERED=1

_user_tmpl() {
	_arch="$1"
	_binfmt="$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" || :
	}
}

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() {
	build_style=meta
	short_desc+=" (transitional dummy package)"
	depends="${sourcepkg}>=${version}_${revision}"
}
