SUMMARY="A generic and open source machine emulator and virtualizer"
DESCRIPTION="QEMU is a generic and open source machine emulator and virtualizer.

QEMU can run OSes and programs made for one machine (e.g. an ARM board) on a \
different machine (e.g. your own PC). By using dynamic translation, it \
achieves very good performance."
HOMEPAGE="https://www.qemu.org/"
COPYRIGHT="2003-2019 Fabrice Bellard"
LICENSE="GNU GPL v2"
REVISION="7"
SOURCE_URI="https://download.qemu.org/qemu-$portVersion.tar.xz"
CHECKSUM_SHA256="b148fc3c7382c5addd915db433383160ca7b840bc6ea90bb0d35c6b253526d56"
SOURCE_DIR="qemu-$portVersion"
PATCHES="qemu-$portVersion.patchset"
ADDITIONAL_FILES="qemu.rdef.in"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

platformList="x86 arm mips ppc sparc m68k riscv"
archList_x86="i386 x86_64"
archList_arm="arm aarch64"
archList_mips="mips mipsel mips64 mips64el"
archList_ppc="ppc ppc64"
archList_sparc="sparc sparc64"
archList_m68k="m68k"
archList_riscv="riscv32 riscv64"

PROVIDES="
	qemu$secondaryArchSuffix = $portVersion
	cmd:qemu_edid$commandSuffix = $portVersion
	cmd:qemu_img$commandSuffix = $portVersion
	cmd:qemu_io$commandSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	cmd:iasl
	cmd:smbd$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libcapstone$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libfdt$secondaryArchSuffix
	lib:libgcrypt$secondaryArchSuffix
	lib:libglib_2.0$secondaryArchSuffix
	lib:libgnutls$secondaryArchSuffix
	lib:libgpg_error$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:liblzo2$secondaryArchSuffix
	lib:libncursesw$secondaryArchSuffix
	lib:libnettle$secondaryArchSuffix
	lib:libpixman_1$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libSDL_1.2$secondaryArchSuffix
	lib:libsnappy$secondaryArchSuffix
	lib:libssh2$secondaryArchSuffix
	lib:libssp$secondaryArchSuffix
	lib:libtasn1$secondaryArchSuffix
	lib:libusb_1.0$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_x86="
	qemu${secondaryArchSuffix}_x86 = $portVersion
	cmd:qemu$commandSuffix = $portVersion
	cmd:qemu_system_i386$commandSuffix = $portVersion
	cmd:qemu_system_x86_64$commandSuffix = $portVersion
	"
REQUIRES_x86="
	$REQUIRES
	qemu$secondaryArchSuffix
	"
SUPPLEMENTS_x86="
	qemu$secondaryArchSuffix
	"

PROVIDES_arm="
	qemu${secondaryArchSuffix}_arm = $portVersion
	cmd:qemu_system_aarch64$commandSuffix = $portVersion
	cmd:qemu_system_arm$commandSuffix = $portVersion
	"
REQUIRES_arm=$REQUIRES_x86

PROVIDES_mips="
	qemu${secondaryArchSuffix}_mips = $portVersion
	cmd:qemu_system_mips$commandSuffix = $portVersion
	cmd:qemu_system_mips64$commandSuffix = $portVersion
	cmd:qemu_system_mips64el$commandSuffix = $portVersion
	cmd:qemu_system_mipsel$commandSuffix = $portVersion
	"
REQUIRES_mips=$REQUIRES_x86

PROVIDES_ppc="
	qemu${secondaryArchSuffix}_ppc = $portVersion
	cmd:qemu_system_ppc$commandSuffix = $portVersion
	cmd:qemu_system_ppc64$commandSuffix = $portVersion
	"
REQUIRES_ppc=$REQUIRES_x86

PROVIDES_sparc="
	qemu${secondaryArchSuffix}_sparc = $portVersion
	cmd:qemu_system_sparc$commandSuffix = $portVersion
	cmd:qemu_system_sparc64$commandSuffix = $portVersion
	"
REQUIRES_sparc=$REQUIRES_x86

PROVIDES_m68k="
	qemu${secondaryArchSuffix}_m68k = $portVersion
	cmd:qemu_system_m68k$commandSuffix = $portVersion
	"
REQUIRES_m68k=$REQUIRES_x86

PROVIDES_riscv="
	qemu${secondaryArchSuffix}_riscv = $portVersion
	cmd:qemu_system_riscv$commandSuffix = $portVersion
	cmd:qemu_system_riscv64$commandSuffix = $portVersion
	"
REQUIRES_riscv=$REQUIRES_x86

BUILD_REQUIRES="
	devel:libbz2$secondaryArchSuffix
	devel:libcapstone$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
	devel:libfdt$secondaryArchSuffix
	devel:libgcrypt$secondaryArchSuffix
	devel:libglib_2.0$secondaryArchSuffix
	devel:libgnutls$secondaryArchSuffix
	devel:libgpg_error$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:liblzo2$secondaryArchSuffix
	devel:libncursesw$secondaryArchSuffix
	devel:libnettle$secondaryArchSuffix >= 8
	devel:libpixman_1$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libSDL_1.2$secondaryArchSuffix
	devel:libsnappy$secondaryArchSuffix
	devel:libssh2$secondaryArchSuffix
	devel:libtasn1$secondaryArchSuffix
	devel:libusb_1.0$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:awk
	cmd:cmp
	cmd:gcc$secondaryArchSuffix
	cmd:gcov$secondaryArchSuffix
	cmd:gprof$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:find
	cmd:iasl
	cmd:pkg_config$secondaryArchSuffix
	cmd:pod2man
	cmd:python
	"

BUILD()
{
	export CFLAGS="-I`finddir B_SYSTEM_HEADERS_DIRECTORY`/capstone"

	for platformItem in $platformList; do
		platformArchList=$(eval echo \$archList_$platformItem)
		archList="$archList	$platformArchList"
	done

	for archItem in $archList; do
		targetList="$archItem-softmmu,$targetList"
	done

	runConfigure --omit-dirs "binDir sbinDir" \
		--omit-buildspec ./configure \
		--bindir="$commandBinDir" --sbindir="$commandBinDir" \
		--enable-capstone=system \
		--enable-gcrypt \
		--disable-tpm \
		--smbd=/$relativeBinDir/smbd \
		--target-list="${targetList::-1}"

	make $jobArgs
}

INSTALL()
{
	make install $jobArgs

	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local LONG_INFO="$SUMMARY"

	archList=""

	for platformItem in $platformList; do
		platformArchList=$(eval echo \$archList_$platformItem)
		archList="$archList	$platformArchList"
	done

	for archItem in $archList; do
		echo $archItem
		local APP_SIGNATURE="application/x-vnd.qemu-system-$archItem"
		sed \
			-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
			-e "s|@MAJOR@|$MAJOR|" \
			-e "s|@MIDDLE@|$MIDDLE|" \
			-e "s|@MINOR@|$MINOR|" \
			-e "s|@LONG_INFO@|$LONG_INFO|" \
			$portDir/additional-files/qemu.rdef.in > qemu-system-$archItem.rdef
		addResourcesToBinaries qemu-system-$archItem.rdef $commandBinDir/qemu-system-$archItem
	done

	# provide convenience symlink to just "qemu"
	if [ "$effectiveTargetArchitecture" = x86_64 ]; then
		ln -s $commandBinDir/qemu-system-x86_64 $commandBinDir/qemu
	else
		ln -s $commandBinDir/qemu-system-i386 $commandBinDir/qemu
	fi

	packageEntries x86 $commandBinDir/qemu

	for platformItem in $platformList; do
		platformArchList=$(eval echo \$archList_$platformItem)
		for archItem in $platformArchList; do
			packageEntries $platformItem $commandBinDir/qemu-system-$archItem
		done
	done
}
