# Template file for 'qemu-user-static'
# This package should be updated together with qemu
pkgname=qemu-user-static
version=9.0.2
revision=2
build_style=configure
configure_args="--prefix=/usr --sysconfdir=/etc --libexecdir=/usr/libexec
 --disable-kvm --disable-png --disable-virtfs --disable-fdt --disable-seccomp
 --enable-linux-user --disable-system --static --disable-pie"
hostmakedepends="flex glib-devel pkg-config perl python3 ninja"
makedepends="dtc-devel glib-devel pixman-devel libuuid-devel"
short_desc="QEMU User-mode emulators (statically compiled)"
maintainer="Orphaned <orphan@voidlinux.org>"
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=6fef38a497ff65f8d45c611b12ed5f5bb3e19ea6d76bfaea6857bf6fd76701e6

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

_omit=("${XBPS_TARGET_MACHINE%-*}")
case "$XBPS_TARGET_MACHINE" in
	x86_64*) _omit+=(i386) ;;
	armv*) _omit+=(arm) ;;
	ppc64le*) _omit+=(ppcle) ;;
	ppc64*) _omit+=(ppc) ;;
esac

case "$XBPS_TARGET_MACHINE" in
	x86_64*|i686*) _host_arch="i386" ;;
	mips*) _host_arch="mips" ;;
	ppc*le) _host_arch="ppcle" ;;
	ppc*) _host_arch="ppc" ;;
	arm*|aarch64*) _host_arch="arm" ;;
	riscv*) _host_arch="riscv" ;;
	*) _host_arch="${XBPS_TARGET_MACHINE%-musl}" ;;
esac

post_install() {
	# Remove unneeded stuff.
	rm -rf ${DESTDIR}/etc ${DESTDIR}/usr/share ${DESTDIR}/usr/libexec
	for f in nbd io img; do
		rm -f ${DESTDIR}/usr/bin/qemu-${f}
	done
	for f in ${DESTDIR}/usr/bin/*; do
		mv ${f} ${f}-static
	done

	vmkdir usr/share/binfmts
	HOST_ARCH="$_host_arch" scripts/qemu-binfmt-conf.sh --debian --exportdir "${DESTDIR}"/usr/share/binfmts \
		--qemu-path /usr/bin --preserve-argv0 yes --persistent yes --credential yes
	for bf in "${DESTDIR}"/usr/share/binfmts/*; do
		for ar in "${_omit[@]}"; do
			if [[ "$bf" = */qemu-"$ar" ]]; then
				rm "$bf"
				continue 2
			fi
		done
		vsed -i -e "s/${bf##*/}/&-static/g" "$bf"
		mv "$bf" "${bf}-static"
	done
}
