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="http://wiki.qemu.org/"
COPYRIGHT="2003-2017 Fabrice Bellard"
LICENSE="GNU GPL v2"
REVISION="4"
SOURCE_URI="https://download.qemu.org/qemu-$portVersion.tar.xz"
CHECKSUM_SHA256="8a5145d1f8bd2eadc6776f3e13c68cd28d01349e30639bdbcb26ac588d668686"
SOURCE_DIR="qemu-$portVersion"
PATCHES="qemu-$portVersion.patchset"
ADDITIONAL_FILES="qemu.rdef.in"

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

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

PROVIDES="
	qemu$secondaryArchSuffix = $portVersion
	cmd:qemu$commandSuffix = $portVersion
	cmd:qemu_img$commandSuffix = $portVersion
	cmd:qemu_io$commandSuffix = $portVersion
#	cmd:qemu_system_aarch64$commandSuffix = $portVersion
#	cmd:qemu_system_alpha$commandSuffix = $portVersion
#	cmd:qemu_system_arm$commandSuffix = $portVersion
#	cmd:qemu_system_cris$commandSuffix = $portVersion
	cmd:qemu_system_i386$commandSuffix = $portVersion
#	cmd:qemu_system_lm32$commandSuffix = $portVersion
#	cmd:qemu_system_m68k$commandSuffix = $portVersion
#	cmd:qemu_system_microblaze$commandSuffix = $portVersion
#	cmd:qemu_system_microblazeel$commandSuffix = $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
#	cmd:qemu_system_moxie$commandSuffix = $portVersion
#	cmd:qemu_system_or32$commandSuffix = $portVersion
#	cmd:qemu_system_ppc$commandSuffix = $portVersion
#	cmd:qemu_system_ppc64$commandSuffix = $portVersion
#	cmd:qemu_system_ppcemb$commandSuffix = $portVersion
#	cmd:qemu_system_s390x$commandSuffix = $portVersion
#	cmd:qemu_system_sh4$commandSuffix = $portVersion
#	cmd:qemu_system_sh4eb$commandSuffix = $portVersion
#	cmd:qemu_system_sparc$commandSuffix = $portVersion
#	cmd:qemu_system_sparc64$commandSuffix = $portVersion
#	cmd:qemu_system_unicore32$commandSuffix = $portVersion
	cmd:qemu_system_x86_64$commandSuffix = $portVersion
#	cmd:qemu_system_xtensa$commandSuffix = $portVersion
#	cmd:qemu_system_xtensaeb$commandSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	cmd:iasl$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libcapstone$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libfdt$secondaryArchSuffix
	lib:libgcrypt$secondaryArchSuffix
	lib:libGL$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:libsdl2_2.0$secondaryArchSuffix
	lib:libsnappy$secondaryArchSuffix
	lib:libssh2$secondaryArchSuffix
	lib:libssp$secondaryArchSuffix
	lib:libtasn1$secondaryArchSuffix
	lib:libusb_1.0$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	devel:libbz2$secondaryArchSuffix
	devel:libcapstone$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
	devel:libfdt$secondaryArchSuffix
	devel:libgcrypt$secondaryArchSuffix
	devel:libGL$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
	devel:libpixman_1$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libsdl2_2.0$secondaryArchSuffix
	devel:libsnappy$secondaryArchSuffix
	devel:libssh2$secondaryArchSuffix
	devel:libtasn1$secondaryArchSuffix
	devel:libusb_1.0$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$secondaryArchSuffix
	cmd:pkg_config$secondaryArchSuffix
	cmd:pod2man
	cmd:python
	"

defineDebugInfoPackage qemu$secondaryArchSuffix \
	"$commandBinDir"/qemu-img \
	"$commandBinDir"/qemu-io \
	"$commandBinDir"/qemu-system-i386 \
	"$commandBinDir"/qemu-system-x86_64

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

	runConfigure --omit-dirs "binDir sbinDir" \
		--omit-buildspec ./configure \
		--bindir="$commandBinDir" --sbindir="$commandBinDir" \
		--enable-capstone=system \
		--enable-gcrypt \
		--disable-tpm \
		--disable-strip \
		--target-list=i386-softmmu,x86_64-softmmu
		# remove the line above to get all the supported targets
		# aarch64-softmmu broken with --disable-stack-protector

	make $jobArgs
}

INSTALL()
{
	make install $jobArgs

	local APP_SIGNATURE="application/x-vnd.qemu"
	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"
	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.rdef

	addResourcesToBinaries qemu.rdef $commandBinDir/qemu-system-i386
	if [ "$effectiveTargetArchitecture" = x86_64 ]; then
		addResourcesToBinaries qemu.rdef $commandBinDir/qemu-system-x86_64
	fi

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