# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2


# Minimum "cmake" version required by mGBA, defined *BEFORE* inheriting the
# "cmake-utils" eclass.

# Instruct "cmake" to *NOT* ignore unrecognized CLI options. (Why on Satan's
# red earth is ignoring such options the eclass default?)

# Here's where things get confusing. Strap in, because you're in for a ride!
# There exist two mGBA repositories:
# * "https://github.com/mgba-emu/mgba.git", the official mGBA repository.
# * "https://github.com/libretro/mgba.git", the Libretro mGBA repository
# Since the latter tracks the former, the two superficially resemble one other.
# All similarities end at their build systems, however. The former ships with:
# * A dynamic "cmake"-based build system reading "CMakeLists.txt" as input and
#   writing a "Makefile" as output. This system provides out-of-the-box support 
#   for compiling the mGBA Libretro core via the "-DBUILD_LIBRETRO=1"
#   configuration-time option.
# The latter ships with two (!) parallel build systems, only one of which is
# documented and only one of which works (...and the documented system is not
# the system that works):
# * A dynamic "cmake"-based build system reading "CMakeLists.txt" as input and
#   overwriting the existing "Makefile" as output. Despite explicitly including
#   a configuration-time option to enable Libretro support (e.g.,
#   "-DBUILD_LIBRETRO=1"), this system fails to compile on enabling this option.
# * An undocumented static "make"-based build system reading "Makefile" which
#   reads "Makefile.libretro" as input. Unlike the prior system, this system
#   actually succeeds in compiling the mGBA Libretro core. Unsurprisingly, this 
#   is the system used by the official "libretro-super" project to build this
#   core. Unfortunately, this system provides substantially fewer
#   configuration-time options than the prior system.
# There exist two options, therefore: "cmake" in the official mGBA repository
# and "make" in the Libretro mGBA repository. Since the former successfully
# compiles the mGBA Libretro core *AND* provides substantially more 
# configuration-time options, we prefer the former. Interestingly, this
# archlinux PKGBUILD for the mGBA Libretro core has made a similar choice:
#     https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=libretro-mgba-git


inherit libretro-core cmake-utils

DESCRIPTION="libretro implementation of mGBA. (Game Boy Advance)"
KEYWORDS="amd64 x86"

IUSE="epoxy ffmpeg gles2 imagemagick lto lzma +opengl pgo pgopost png +zip zlib"
	epoxy? ( gles2 )
	gles2? ( opengl )
	pgopost? ( pgo )
	png? ( zlib )

	epoxy? ( media-libs/libepoxy:0= )
	ffmpeg? ( virtual/ffmpeg:0= )
	imagemagick? ( media-gfx/imagemagick:0= )
	lzma? ( app-arch/xz-utils:0= )
	png? ( media-libs/libpng:0= )
	opengl? ( media-libs/mesa:0=[gles2?] )
	png? ( media-libs/libpng:0= )
	zip? ( dev-libs/libzip:0= )
	zlib? ( sys-libs/zlib:0= )

src_unpack() {

	# For safety, unbundle most bundled third-party libraries.
	mkdir "${S}"/src/third-party-unused
	mv \
		"${S}"/src/third-party/{libpng,lzma,zlib} \

src_configure() {
	local mycmakeargs=(
		# For security, install into the standard games directories.

		# Enable only mGBA's Libretro core as a statically linked library.

		# Disable all mGBA-specific frontends. RetroArch is our frontend!

		# Disable ARM architecture-based debugging.

		# Set USE flag-dependent options.
		-DUSE_EPOXY=$(usex epoxy ON OFF)
		-DUSE_FFMPEG=$(usex ffmpeg ON OFF)
		-DBUILD_GLES2=$(usex gles2 ON OFF)
		-DUSE_MAGICK=$(usex imagemagick ON OFF)
		-DBUILD_LTO=$(usex lto ON OFF)
		-DBUILD_GL=$(usex opengl ON OFF)
		-DBUILD_PGO=$(usex pgo ON OFF)
		-DPGO_STAGE_2=$(usex pgopost ON OFF)
		-DUSE_PNG=$(usex png ON OFF)
		-DUSE_LIBZIP=$(usex zip ON OFF)
		-DUSE_ZLIB=$(usex zlib ON OFF)

src_install() {
	# Install the mGBA Libretro core.

	# Install the mGBA Libretro info file and enforce games-specific security