# Template file for 'mame'
pkgname=mame
version=0265
revision=1
hostmakedepends="pkg-config python3 qt6-base"
makedepends="qt6-base-devel SDL2_ttf-devel asio libutf8proc-devel libgomp-devel
 rapidjson glm pugixml-devel portmidi-devel portaudio-devel"
depends="liberation-fonts-ttf"
short_desc="Multiple Arcade Machine Emulator"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-2.0-or-later"
homepage="https://mamedev.org"
distfiles="https://github.com/mamedev/mame/archive/mame${version}.tar.gz"
checksum=3de68db66efb783eb5c5d7ccad1f4a4f5eadc6c7a29c1a9c2097e532c3e0b8b1
nodebug=yes
replaces="sdlmame>=0 sdlmess>=0"

post_patch() {
	vsed -i scripts/toolchain.lua -i 3rdparty/genie/src/host/scripts.c -e 's/-m32//' -e 's/-m64//'
}

do_configure() {
	if [ "${CROSS_BUILD}" ]; then
		CC="$CC_host"
		CXX="$CXX_host"
		LD="$CXX_host"
		CFLAGS="$CFLAGS_host"
		CXXFLAGS="$CXXFLAGS_host"
		LDFLAGS="$LDFLAGS_host"
	fi

	make $makejobs genie
}

do_build() {
	case "$XBPS_TARGET_MACHINE" in
		ppc*)
			CFLAGS+=" -mlong-double-64"
			CXXFLAGS+=" -mlong-double-64"
			;;
		armv*)
			LDFLAGS+=" -Wl,--long-plt"
			;;
		aarch64*)
			# Help to reduce impact on our aarch64 builders
			LDFLAGS+=" -Wl,--no-keep-memory"
			;;
	esac

	CXXFLAGS+=" -I$XBPS_CROSS_BASE/include/qt6"
	[ "$XBPS_TARGET_LIBC" = "musl" ] && CXXFLAGS+=" -DBX_CRT_MUSL=1"

	if [ "$XBPS_TARGET_WORDSIZE" = 32 ]; then
		ptr64=0
		# Overcome linker memory exhaustion
		LDFLAGS+=" -Wl,--no-keep-memory"
	else
		ptr64=1
	fi

	[ "$XBPS_TARGET_ENDIAN" = be ] && bigendian=1 || bigendian=0

	make $makejobs TOOLS=1 NOWERROR=1 OPENMP=1 SYMBOLS=0 \
	 OVERRIDE_CC="$CC" OVERRIDE_CXX="$CXX" OVERRIDE_LD="$CXX" \
	 PTR64=$ptr64 BIGENDIAN=$bigendian QT_HOME=/usr/lib/qt6 \
	 USE_SYSTEM_LIB_ASIO=1 USE_SYSTEM_LIB_EXPAT=1 \
	 USE_SYSTEM_LIB_ZLIB=1 USE_SYSTEM_LIB_ZSTD=1 \
	 USE_SYSTEM_LIB_JPEG=1 USE_SYSTEM_LIB_FLAC=1 \
	 USE_SYSTEM_LIB_SQLITE3=1 \
	 USE_SYSTEM_LIB_PORTMIDI=1 USE_SYSTEM_LIB_PORTAUDIO=1 \
	 USE_SYSTEM_LIB_UTF8PROC=1 USE_SYSTEM_LIB_GLM=1 \
	 USE_SYSTEM_LIB_RAPIDJSON=1 USE_SYSTEM_LIB_PUGIXML=1
}

do_install() {
	# Install the mame script
	vbin ${FILESDIR}/mame.sh mame

	# Install the main application(s)
	vinstall mame 755 usr/libexec/mame mame
	vman docs/man/mame.6

	# Install the tools
	for f in chdman jedutil regrep pngcmp romcmp srcclean \
		ldverify ldresample unidasm castool floptool imgtool; do
		vbin ${f}
		if [ -f docs/man/${f}.1 ]; then
			vman docs/man/${f}.1
		fi
	done

	# Install the extra bits
	for f in artwork bgfx docs hash hlsl keymaps plugins samples; do
		vcopy ${f} usr/share/${pkgname}
	done
}
