cmake_minimum_required(VERSION 2.6)
project(mGBA C)
set(BINARY_NAME mgba CACHE INTERNAL "Name of output binaries")
if(NOT MSVC)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99")
else()
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /wd4003 /wd4244 /wd4146")
endif()
set(USE_CLI_DEBUGGER ON CACHE BOOL "Whether or not to enable the CLI-mode ARM debugger")
set(USE_GDB_STUB ON CACHE BOOL "Whether or not to enable the GDB stub ARM debugger")
set(USE_FFMPEG ON CACHE BOOL "Whether or not to enable FFmpeg support")
set(USE_ZLIB ON CACHE BOOL "Whether or not to enable zlib support")
set(USE_PNG ON CACHE BOOL "Whether or not to enable PNG support")
set(USE_LIBZIP ON CACHE BOOL "Whether or not to enable LIBZIP support")
set(USE_MAGICK ON CACHE BOOL "Whether or not to enable ImageMagick support")
set(M_CORE_GBA ON CACHE BOOL "Build Game Boy Advance core")
set(M_CORE_GB ON CACHE BOOL "Build Game Boy core")
set(USE_LZMA ON CACHE BOOL "Whether or not to enable 7-Zip support")
set(BUILD_QT ON CACHE BOOL "Build Qt frontend")
set(BUILD_SDL ON CACHE BOOL "Build SDL frontend")
set(BUILD_LIBRETRO OFF CACHE BOOL "Build libretro core")
if(APPLE)
	set(BUILD_OPENEMU OFF CACHE BOOL "Build OpenEmu core")
endif()
set(BUILD_PERF OFF CACHE BOOL "Build performance profiling tool")
set(BUILD_TEST OFF CACHE BOOL "Build testing harness")
set(BUILD_STATIC OFF CACHE BOOL "Build a static library")
set(BUILD_SHARED ON CACHE BOOL "Build a shared library")
set(SKIP_LIBRARY OFF CACHE BOOL "Skip building the library (useful for only building libretro or OpenEmu cores)")
set(BUILD_GL ON CACHE STRING "Build with OpenGL")
set(BUILD_GLES2 OFF CACHE STRING "Build with OpenGL|ES 2")
set(USE_EPOXY ON CACHE STRING "Build with libepoxy")
set(DISABLE_DEPS OFF CACHE BOOL "Build without dependencies")
file(GLOB ARM_SRC ${CMAKE_SOURCE_DIR}/src/arm/*.c)
file(GLOB LR35902_SRC ${CMAKE_SOURCE_DIR}/src/lr35902/*.c)
file(GLOB GBA_SRC ${CMAKE_SOURCE_DIR}/src/gba/*.c)
file(GLOB GB_SRC ${CMAKE_SOURCE_DIR}/src/gb/*.c)
file(GLOB GBA_CHEATS_SRC ${CMAKE_SOURCE_DIR}/src/gba/cheats/*.c)
file(GLOB GBA_RR_SRC ${CMAKE_SOURCE_DIR}/src/gba/rr/*.c)
file(GLOB GBA_SV_SRC ${CMAKE_SOURCE_DIR}/src/gba/supervisor/*.c)
file(GLOB GBA_CTX_SRC ${CMAKE_SOURCE_DIR}/src/gba/context/*.c)
file(GLOB UTIL_SRC ${CMAKE_SOURCE_DIR}/src/util/*.[cSs] ${CMAKE_SOURCE_DIR}/src/core/*.c)
file(GLOB GUI_SRC ${CMAKE_SOURCE_DIR}/src/util/gui/*.c ${CMAKE_SOURCE_DIR}/src/gba/gui/*.c)
file(GLOB GBA_RENDERER_SRC ${CMAKE_SOURCE_DIR}/src/gba/renderers/*.c)
file(GLOB SIO_SRC ${CMAKE_SOURCE_DIR}/src/gba/sio/lockstep.c)
file(GLOB GB_RENDERER_SRC ${CMAKE_SOURCE_DIR}/src/gb/renderers/*.c)
file(GLOB THIRD_PARTY_SRC ${CMAKE_SOURCE_DIR}/src/third-party/inih/*.c)
set(CLI_SRC ${CMAKE_SOURCE_DIR}/src/platform/commandline.c)
set(CORE_VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-mem.c)
set(VFS_SRC)
source_group("ARM core" FILES ${ARM_SRC})
source_group("LR35902 core" FILES ${LR35902_SRC})
source_group("GBA board" FILES ${GBA_SRC} ${GBA_RENDERER_SRC} ${SIO_SRC})
source_group("GBA extra" FILES ${GBA_CHEATS_SRC} ${GBA_CTX_SRC} ${GBA_SV_SRC} ${GBA_RR_SRC})
source_group("GB board" FILES ${GB_SRC})
source_group("Utilities" FILES ${UTIL_SRC})
include_directories(${CMAKE_SOURCE_DIR}/src/arm)
include_directories(${CMAKE_SOURCE_DIR}/src)

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type (e.g. Release or Debug)" FORCE)
endif()

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIBDIR}")

include(GNUInstallDirs)

if (NOT DEFINED LIBDIR)
	set(LIBDIR "lib")
endif()

if (NOT DEFINED MANDIR)
	set(MANDIR ${CMAKE_INSTALL_MANDIR})
endif()

# Function definitions
include(FindPkgConfig)
function(find_feature FEATURE_NAME FEATURE_REQUIRES)
	if (NOT ${FEATURE_NAME})
		return()
	endif()
	if (DISABLE_DEPS)
		set(${FEATURE_NAME} OFF PARENT_SCOPE)
		return()
	endif()
	foreach(REQUIRE ${FEATURE_REQUIRES})
		if(NOT ${REQUIRE}_FOUND)
			find_package(${REQUIRE} QUIET)
			if(NOT ${REQUIRE}_FOUND)
				pkg_search_module(${REQUIRE} ${REQUIRE})
				if (NOT ${REQUIRE}_FOUND)
					message(WARNING "Requested module ${REQUIRE} missing for feature ${FEATURE_NAME}. Feature disabled.")
					set(${FEATURE_NAME} OFF PARENT_SCOPE)
					return()
				endif()
			endif()
		endif()
		string(TOUPPER ${REQUIRE} UREQUIRE)
		set(${UREQUIRE}_CFLAGS_OTHER ${${REQUIRE}_CFLAGS_OTHER} PARENT_SCOPE)
		set(${UREQUIRE}_FOUND ${${REQUIRE}_FOUND} PARENT_SCOPE)
		set(${UREQUIRE}_INCLUDE_DIRS ${${REQUIRE}_INCLUDE_DIRS} PARENT_SCOPE)
		set(${UREQUIRE}_LIBRARIES ${${REQUIRE}_LIBRARIES} PARENT_SCOPE)
		set(${UREQUIRE}_LIBRARY_DIRS ${${REQUIRE}_LIBRARY_DIRS} PARENT_SCOPE)
		set(${UREQUIRE}_LDFLAGS_OTHER ${${REQUIRE}_LDFLAGS_OTHER} PARENT_SCOPE)
	endforeach()
endfunction()

# Version information
add_custom_target(version-info ALL ${CMAKE_COMMAND} -E touch ${CMAKE_SOURCE_DIR}/src/util/version.c.in
	COMMAND ${CMAKE_COMMAND}
	-DBINARY_NAME=${BINARY_NAME}
	-DCONFIG_FILE=${CMAKE_SOURCE_DIR}/src/util/version.c.in
	-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/version.c
	-P ${CMAKE_SOURCE_DIR}/version.cmake
	WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

include(${CMAKE_SOURCE_DIR}/version.cmake)
configure_file(${CMAKE_SOURCE_DIR}/src/util/version.c.in ${CMAKE_CURRENT_BINARY_DIR}/version.c)
list(APPEND UTIL_SRC ${CMAKE_BINARY_DIR}/version.c)
source_group("Generated sources" FILES ${CMAKE_BINARY_DIR}/version.c)

# Advanced settings
set(BUILD_LTO ON CACHE BOOL "Build with link-time optimization")
set(BUILD_PGO OFF CACHE BOOL "Build with profiling-guided optimization")
set(PGO_STAGE_2 CACHE BOOL "Rebuild for profiling-guided optimization after profiles have been generated")
set(PGO_DIR "/tmp/gba-pgo/" CACHE PATH "Profiling-guided optimization profiles path")
mark_as_advanced(BUILD_LTO BUILD_PGO PGO_STAGE_2 PGO_DIR)
set(PGO_PRE_FLAGS "-fprofile-generate=${PGO_DIR} -fprofile-arcs")
set(PGO_POST_FLAGS "-fprofile-use=${PGO_DIR} -fbranch-probabilities")

if(BUILD_PGO AND NOT PGO_STAGE_2)
	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_PRE_FLAGS}")
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
elseif(BUILD_PGO AND PGO_STAGE_2)
	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_POST_FLAGS}")
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_POST_FLAGS}")
	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_POST_FLAGS}")
endif()

# Platform support
if(WIN32)
	set(WIN32_VERSION "${LIB_VERSION_MAJOR},${LIB_VERSION_MINOR},${LIB_VERSION_PATCH}")
	add_definitions(-D_WIN32_WINNT=0x0600)
	list(APPEND OS_LIB ws2_32 shlwapi)
	list(APPEND CORE_VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/windows/*.c)
	source_group("Windows-specific code" FILES ${OS_SRC})
elseif(UNIX)
	add_definitions(-DUSE_PTHREADS)

	if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
		add_definitions(-D_GNU_SOURCE)
	endif()
	if(NOT APPLE AND NOT HAIKU)
		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
	endif()

	list(APPEND CORE_VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/posix/*.c)
	source_group("POSIX-specific code" FILES ${OS_SRC})
endif()

if(APPLE)
	add_definitions(-D_DARWIN_C_SOURCE)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6")
endif()

if(NOT HAIKU AND NOT MSVC AND NOT PSP2)
	set(M_LIBRARY m)
endif()
list(APPEND OS_LIB ${M_LIBRARY})

if(APPLE OR CMAKE_C_COMPILER_ID STREQUAL "GNU" AND BUILD_LTO)
	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto")
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
	set_source_files_properties(${CMAKE_SOURCE_DIR}/src/arm/isa-arm.c ${CMAKE_SOURCE_DIR}/src/arm/isa-thumb.c PROPERTIES COMPILE_FLAGS -fno-lto)
endif()

if(BUILD_BBB OR BUILD_RASPI OR BUILD_PANDORA)
	if(NOT BUILD_EGL)
		add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
	endif()
endif()

if(BUILD_RASPI)
	set(BUILD_GL OFF CACHE BOOL "OpenGL not supported" FORCE)
endif()

if(BUILD_PANDORA)
	add_definitions(-DBUILD_PANDORA)
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm.*")
	enable_language(ASM)
endif()

if(PSP2 OR WII)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
endif()

if(DEFINED 3DS OR DEFINED PSP2 OR DEFINED WII)
	set(USE_GDB_STUB OFF)
endif()

if(WII)
	add_definitions(-U__STRICT_ANSI__)
endif()

include(CheckFunctionExists)
check_function_exists(strdup HAVE_STRDUP)
check_function_exists(strndup HAVE_STRNDUP)
check_function_exists(localtime_r HAVE_LOCALTIME_R)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
	check_function_exists(snprintf_l HAVE_SNPRINTF_L)
	if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
		# The strtof_l on Linux not actually exposed nor actually strtof_l
		set(HAVE_STRTOF_L OFF)
	else()
		check_function_exists(strtof_l HAVE_STRTOF_L)
	endif()
	check_function_exists(newlocale HAVE_NEWLOCALE)
	check_function_exists(freelocale HAVE_FREELOCALE)
	check_function_exists(uselocale HAVE_USELOCALE)
	check_function_exists(setlocale HAVE_SETLOCALE)
else()
	if(NOT DEFINED 3DS AND NOT DEFINED PSP2 AND NOT DEFINED WII)
		set(DISABLE_DEPS ON CACHE BOOL "This platform cannot build with dependencies" FORCE)
	endif()
	set(DISABLE_FRONTENDS ON)
	set(MINIMAL_CORE ON)
endif()

check_function_exists(chmod HAVE_CHMOD)
check_function_exists(umask HAVE_UMASK)

set(FUNCTION_DEFINES)

if(HAVE_STRDUP)
	list(APPEND FUNCTION_DEFINES HAVE_STRDUP)
endif()

if(HAVE_STRNDUP)
	list(APPEND FUNCTION_DEFINES HAVE_STRNDUP)
endif()

if(HAVE_LOCALTIME_R)
	list(APPEND FUNCTION_DEFINES HAVE_LOCALTIME_R)
endif()

if(HAVE_NEWLOCALE AND HAVE_FREELOCALE AND HAVE_USELOCALE)
	list(APPEND FUNCTION_DEFINES HAVE_LOCALE)
	if (HAVE_STRTOF_L)
		list(APPEND FUNCTION_DEFINES HAVE_STRTOF_L)
	endif()
	if (HAVE_SNPRINTF_L)
		list(APPEND FUNCTION_DEFINES HAVE_SNPRINTF_L)
	endif()
endif()

if(HAVE_SETLOCALE)
	list(APPEND FUNCTION_DEFINES HAVE_SETLOCALE)
endif()

if(HAVE_CHMOD)
	list(APPEND FUNCTION_DEFINES HAVE_CHMOD)
endif()

if(HAVE_UMASK)
	list(APPEND FUNCTION_DEFINES HAVE_UMASK)
endif()

# Feature dependencies
set(FEATURES)
if(CMAKE_SYSTEM_NAME MATCHES .*BSD)
	set(LIBEDIT_LIBRARIES -ledit)
	if (CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
		list(APPEND LIBEDIT_LIBRARIES -ltermcap)
	endif()
else()
	find_feature(USE_CLI_DEBUGGER "libedit")
endif()
if(BUILD_GL)
	find_package(OpenGL QUIET)
	if(NOT OPENGL_FOUND)
		set(BUILD_GL OFF CACHE BOOL "OpenGL not found" FORCE)
	endif()
endif()
if(NOT BUILD_GL)
	set(OPENGLE_LIBRARY "" CACHE PATH "" FORCE)
endif()
if(BUILD_GLES2 AND NOT BUILD_RASPI)
	find_path(OPENGLES2_INCLUDE_DIR NAMES GLES2/gl2.h)
	find_library(OPENGLES2_LIBRARY NAMES GLESv2 GLESv2_CM)
	if(NOT OPENGLES2_INCLUDE_DIR OR NOT OPENGLES2_LIBRARY)
		set(BUILD_GLES2 OFF CACHE BOOL "OpenGL|ES 2 not found" FORCE)
	endif()
endif()
if(NOT BUILD_GLES2)
	set(OPENGLES2_LIBRARY "" CACHE PATH "" FORCE)
endif()
set(WANT_ZLIB ${USE_ZLIB})
set(WANT_PNG ${USE_PNG})
set(WANT_LIBZIP ${USE_LIBZIP})

find_feature(USE_FFMPEG "libavcodec;libavformat;libavresample;libavutil;libswscale")
find_feature(USE_ZLIB "ZLIB")
find_feature(USE_PNG "PNG")
find_feature(USE_LIBZIP "libzip")
find_feature(USE_MAGICK "MagickWand")
find_feature(USE_EPOXY "epoxy")

# Features
set(DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/debugger.c ${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c)
set(FEATURE_SRC)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")

if(DISABLE_DEPS)
	set(USE_GDB_STUB OFF)
endif()

if(USE_CLI_DEBUGGER)
	list(APPEND FEATURES CLI_DEBUGGER)
	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c)
	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/debugger/parser.c)
	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/gba/supervisor/cli.c)
	include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
	link_directories(${LIBEDIT_LIBRARY_DIRS})
	set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libedit2")
else()
	set(DEBUGGER_LIB "")
endif()

if(USE_GDB_STUB)
	list(APPEND FEATURES GDB_STUB)
	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/gdb-stub.c)
endif()
source_group("ARM debugger" FILES ${DEBUGGER_SRC})

if(USE_FFMPEG)
	list(APPEND FEATURES FFMPEG)
	pkg_search_module(LIBSWRESAMPLE QUIET libswresample)
	if(NOT LIBSWRESAMPLE_FOUND)
		list(APPEND FEATURES LIBAV)
	endif()
	include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVRESAMPLE_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS} ${LIBSWSCALE_INCLUDE_DIRS})
	link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVRESAMPLE_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS} ${LIBSWSCALE_LIBRARY_DIRS})
	list(APPEND FEATURE_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-encoder.c")
	string(REGEX MATCH "^[0-9]+" LIBAVCODEC_VERSION_MAJOR ${libavcodec_VERSION})
	string(REGEX MATCH "^[0-9]+" LIBAVFORMAT_VERSION_MAJOR ${libavformat_VERSION})
	string(REGEX MATCH "^[0-9]+" LIBAVRESAMPLE_VERSION_MAJOR ${libavresample_VERSION})
	string(REGEX MATCH "^[0-9]+" LIBAVUTIL_VERSION_MAJOR ${libavutil_VERSION})
	string(REGEX MATCH "^[0-9]+" LIBSWSCALE_VERSION_MAJOR ${libswscale_VERSION})
	list(APPEND DEPENDENCY_LIB ${LIBAVCODEC_LIBRARIES} ${LIBAVFORMAT_LIBRARIES} ${LIBAVRESAMPLE_LIBRARIES} ${LIBAVUTIL_LIBRARIES} ${LIBSWSCALE_LIBRARIES})
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavcodec${LIBAVCODEC_VERSION_MAJOR}|libavcodec-extra-${LIBAVCODEC_VERSION_MAJOR}|libavcodec-ffmpeg${LIBAVCODEC_VERSION_MAJOR}|libavcodec-ffmpeg-extra${LIBAVCODEC_VERSION_MAJOR}")
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavformat${LIBAVFORMAT_VERSION_MAJOR}|libavformat-ffmpeg${LIBAVFORMAT_VERSION_MAJOR}")
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavresample${LIBAVRESAMPLE_VERSION_MAJOR}|libavresample-ffmpeg${LIBAVRESAMPLE_VERSION_MAJOR}")
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavutil${LIBAVUTIL_VERSION_MAJOR}|libavutil-ffmpeg${LIBAVUTIL_VERSION_MAJOR}")
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libswscale${LIBSWSCALE_VERSION_MAJOR}|libswscale-ffmpeg${LIBSWSCALE_VERSION_MAJOR}")
	set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libavcodec-extra|libavcodec-ffmpeg-extra${LIBAVCODEC_VERSION_MAJOR}")
	if(APPLE)
		set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
	endif()
endif()

list(APPEND THIRD_PARTY_SRC "${CMAKE_SOURCE_DIR}/src/third-party/blip_buf/blip_buf.c")

if(USE_MAGICK)
	list(APPEND FEATURES MAGICK)
	include_directories(AFTER ${MAGICKWAND_INCLUDE_DIRS})
	link_directories(${MAGICKWAND_LIBRARY_DIRS})
	list(APPEND FEATURE_SRC "${CMAKE_SOURCE_DIR}/src/platform/imagemagick/imagemagick-gif-encoder.c")
	list(APPEND DEPENDENCY_LIB ${MAGICKWAND_LIBRARIES})
	string(REGEX MATCH "^[0-9]+\\.[0-9]+" MAGICKWAND_VERSION_PARTIAL ${MagickWand_VERSION})
	if(${MAGICKWAND_VERSION_PARTIAL} EQUAL "6.7")
		set(MAGICKWAND_DEB_VERSION "5")
	else()
		set(MAGICKWAND_DEB_VERSION "-6.q16-2")
	endif()
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libmagickwand${MAGICKWAND_DEB_VERSION}")
endif()

if(WANT_ZLIB AND NOT USE_ZLIB)
	set(SKIP_INSTALL_ALL ON)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party/zlib zlib)
	set_property(TARGET zlibstatic PROPERTY INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/zlib;${CMAKE_SOURCE_DIR}/src/third-party/zlib)
	set_property(TARGET zlib PROPERTY EXCLUDE_FROM_ALL ON)
	set_property(TARGET example PROPERTY EXCLUDE_FROM_ALL ON)
	set_property(TARGET minigzip PROPERTY EXCLUDE_FROM_ALL ON)
	set(ZLIB_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/third-party/zlib ${CMAKE_BINARY_DIR}/zlib)
	set(ZLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/src/third-party/zlib ${CMAKE_BINARY_DIR}/zlib)
	set(ZLIB_LIBRARY zlibstatic)
	list(APPEND DEPENDENCY_LIB zlibstatic)
	set(USE_ZLIB ON)
endif()

if(USE_ZLIB)
	list(APPEND FEATURES ZLIB)
	include_directories(AFTER ${ZLIB_INCLUDE_DIRS})
	list(APPEND DEPENDENCY_LIB ${ZLIB_LIBRARIES})
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},zlib1g")
endif()

if(WANT_PNG AND USE_ZLIB AND NOT USE_PNG)
	set(PNG_STATIC ON CACHE BOOL "" FORCE)
	set(PNG_SHARED OFF CACHE BOOL "" FORCE)
	set(PNG_TESTS OFF CACHE BOOL "" FORCE)
	set(SKIP_INSTALL_ALL ON)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party/libpng libpng)
	set_property(TARGET png16_static PROPERTY INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/libpng;${CMAKE_SOURCE_DIR}/src/third-party/libpng;${ZLIB_INCLUDE_DIRS})
	set(PNG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/third-party/libpng ${CMAKE_BINARY_DIR}/libpng)
	list(APPEND DEPENDENCY_LIB png16_static)
	set(USE_PNG ON)
endif()

if(USE_PNG)
	list(APPEND FEATURES PNG)
	include_directories(AFTER ${PNG_INCLUDE_DIRS})
	list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpng12-0")
endif()

if(USE_LIBZIP)
	include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
	link_directories(${LIBZIP_LIBRARY_DIRS})
	list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
	list(APPEND FEATURES LIBZIP)
	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-zip.c)
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip2")
elseif(USE_ZLIB)
	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-zip.c
		${CMAKE_SOURCE_DIR}/src/third-party/zlib/contrib/minizip/ioapi.c
		${CMAKE_SOURCE_DIR}/src/third-party/zlib/contrib/minizip/unzip.c)
endif()

if (USE_LZMA)
	include_directories(AFTER ${CMAKE_SOURCE_DIR}/third-party/lzma)
	add_definitions(-D_7ZIP_PPMD_SUPPPORT)
	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-lzma.c)
	set(LZMA_SRC
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zAlloc.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zArcIn.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf2.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrc.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrcOpt.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zDec.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/CpuArch.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Delta.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/LzmaDec.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Lzma2Dec.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra86.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/BraIA64.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bcj2.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7Dec.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zFile.c
		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zStream.c)
	list(APPEND VFS_SRC ${LZMA_SRC})
	list(APPEND FEATURES LZMA)
endif()

if(USE_EPOXY)
	add_definitions(-DBUILD_GL -DBUILD_GLES2)
	list(APPEND FEATURES EPOXY)
	include_directories(AFTER ${EPOXY_INCLUDE_DIRS})
	set(OPENGLES2_LIBRARY ${EPOXY_LIBRARIES})
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libepoxy0")
endif()


set(FEATURE_DEFINES)
foreach(FEATURE IN LISTS FEATURES)
	list(APPEND FEATURE_DEFINES "USE_${FEATURE}")
endforeach()

set(CORE_SRC)
if(M_CORE_GB)
	add_definitions(-DM_CORE_GB)
	list(APPEND CORE_SRC
		${LR35902_SRC}
		${GB_SRC}
		${GB_RENDERER_SRC})
endif()

if(M_CORE_GBA)
	add_definitions(-DM_CORE_GBA)
	list(APPEND CORE_SRC 
		${ARM_SRC}
		${GBA_SRC}
		${GBA_CHEATS_SRC}
		${GBA_CTX_SRC}
		${DEBUGGER_SRC}
		${GBA_RENDERER_SRC})
	if(NOT M_CORE_GB)
		list(APPEND CORE_SRC ${CMAKE_SOURCE_DIR}/src/gb/audio.c)
	endif()
endif()

source_group("Virtual files" FILES ${CORE_VFS_SRC} ${VFS_SRC})
source_group("Extra features" FILES ${FEATURE_SRC})
source_group("Third-party code" FILES ${THIRD_PARTY_SRC})

# Platform binaries
if(3DS)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/3ds ${CMAKE_BINARY_DIR}/3ds)
endif()

if(WII)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/wii ${CMAKE_BINARY_DIR}/wii)
endif()

if(PSP2)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/psp2 ${CMAKE_BINARY_DIR}/psp2)
endif()

# Binaries
list(APPEND CORE_SRC
	${UTIL_SRC}
	${CORE_VFS_SRC}
	${OS_SRC}
	${THIRD_PARTY_SRC})

set(SRC ${CORE_SRC} ${VFS_SRC})
if(NOT MINIMAL_CORE)
	if(M_CORE_GBA)
		list(APPEND SRC
			${GBA_RR_SRC}
			${GBA_SV_SRC}
			${SIO_SRC})
	endif()
	list(APPEND SRC
		${FEATURE_SRC}
		${CLI_SRC})
endif()

if(NOT SKIP_LIBRARY)
	if(NOT BUILD_STATIC AND NOT BUILD_SHARED)
		set(BUILD_SHARED ON)
	endif()

	if(BUILD_SHARED)
		add_library(${BINARY_NAME} SHARED ${SRC} ${VFS_SRC})
		if(BUILD_STATIC)
			add_library(${BINARY_NAME}-static STATIC ${SRC})
			set_target_properties(${BINARY_NAME}-static PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
			install(TARGETS ${BINARY_NAME}-static DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
			add_dependencies(${BINARY_NAME}-static version-info)
		endif()
	else()
		add_library(${BINARY_NAME} STATIC ${SRC})
	endif()

	add_dependencies(${BINARY_NAME} version-info)
	set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI} COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")

	target_link_libraries(${BINARY_NAME} ${DEBUGGER_LIB} ${DEPENDENCY_LIB} ${OS_LIB})
	install(TARGETS ${BINARY_NAME} LIBRARY DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME} NAMELINK_SKIP ARCHIVE DESTINATION ${LIBDIR} RUNTIME DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
	if(UNIX AND NOT APPLE)
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-16.png DESTINATION share/icons/hicolor/16x16/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-24.png DESTINATION share/icons/hicolor/24x24/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-32.png DESTINATION share/icons/hicolor/32x32/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-48.png DESTINATION share/icons/hicolor/48x48/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-64.png DESTINATION share/icons/hicolor/64x64/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-96.png DESTINATION share/icons/hicolor/96x96/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-128.png DESTINATION share/icons/hicolor/128x128/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-256.png DESTINATION share/icons/hicolor/256x256/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
		install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-512.png DESTINATION share/icons/hicolor/512x512/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
	endif()
else()
	set(BUILD_SHARED OFF)
	set(BUILD_STATIC OFF)
	find_library(${BINARY_NAME} ${BINARY_NAME})
	if(NOT ${BINARY_NAME}_FOUND)
		set(DISABLE_FRONTENDS ON)
		set(BUILD_PERF OFF)
		set(BUILD_TEST OFF)
	endif()
endif()

if(BUILD_GL)
	add_definitions(-DBUILD_GL)
endif()

if(BUILD_GLES2)
	add_definitions(-DBUILD_GLES2)
endif()

if(DISABLE_FRONTENDS)
	set(BUILD_SDL OFF)
	set(BUILD_QT OFF)
endif()

if(BUILD_LIBRETRO)
	file(GLOB RETRO_SRC ${CMAKE_SOURCE_DIR}/src/platform/libretro/*.c)
	add_library(${BINARY_NAME}_libretro SHARED ${CORE_SRC} ${RETRO_SRC})
	set_target_properties(${BINARY_NAME}_libretro PROPERTIES PREFIX "" COMPILE_DEFINITIONS "COLOR_16_BIT;COLOR_5_6_5;DISABLE_THREADING;${OS_DEFINES};${FUNCTION_DEFINES};MINIMAL_CORE=2")
	target_link_libraries(${BINARY_NAME}_libretro ${OS_LIB})
	install(TARGETS ${BINARY_NAME}_libretro LIBRARY DESTINATION ${LIBDIR} COMPONENT ${BINARY_NAME}_libretro NAMELINK_SKIP)
endif()

if(BUILD_OPENEMU)
	find_library(FOUNDATION Foundation)
	find_library(OPENEMUBASE OpenEmuBase)
	file(GLOB OE_SRC ${CMAKE_SOURCE_DIR}/src/platform/openemu/*.m)
	add_library(${BINARY_NAME}-openemu MODULE ${CORE_SRC} ${OE_SRC})
	set_target_properties(${BINARY_NAME}-openemu PROPERTIES
		MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/src/platform/openemu/Info.plist.in
		BUNDLE TRUE
		BUNDLE_EXTENSION oecoreplugin
		OUTPUT_NAME ${PROJECT_NAME}
		COMPILE_DEFINITIONS "DISABLE_THREADING;${OS_DEFINES};${FUNCTION_DEFINES};MINIMAL_CORE=1")
	target_link_libraries(${BINARY_NAME}-openemu ${OS_LIB} ${FOUNDATION} ${OPENEMUBASE})
	install(TARGETS ${BINARY_NAME}-openemu LIBRARY DESTINATION ${LIBDIR} COMPONENT ${BINARY_NAME}.oecoreplugin NAMELINK_SKIP)
endif()

if(BUILD_SDL)
	add_definitions(-DBUILD_SDL)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
endif()

if(BUILD_QT)
	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/qt ${CMAKE_BINARY_DIR}/qt)
endif()

if(BUILD_PERF)
	set(PERF_SRC ${CMAKE_SOURCE_DIR}/src/platform/test/perf-main.c ${CLI_SRC})
	if(UNIX AND NOT APPLE)
		list(APPEND PERF_LIB rt)
	endif()

	add_executable(${BINARY_NAME}-perf ${PERF_SRC})
	target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB} ${OS_LIB})
	set_target_properties(${BINARY_NAME}-perf PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
	install(TARGETS ${BINARY_NAME}-perf DESTINATION bin COMPONENT ${BINARY_NAME}-perf)
	install(FILES ${CMAKE_SOURCE_DIR}/tools/perf.py DESTINATION "${CMAKE_INSTALL_LIBDIR}/${BINARY_NAME}" COMPONENT ${BINARY_NAME}-perf)
endif()

if(BUILD_TEST)
	add_executable(${BINARY_NAME}-fuzz ${CMAKE_SOURCE_DIR}/src/platform/test/fuzz-main.c)
	target_link_libraries(${BINARY_NAME}-fuzz ${BINARY_NAME})
	set_target_properties(${BINARY_NAME}-fuzz PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
	install(TARGETS ${BINARY_NAME}-fuzz DESTINATION bin COMPONENT ${BINARY_NAME}-test)
endif()

# Packaging
set(CPACK_PACKAGE_VERSION ${VERSION_STRING})
set(CPACK_PACKAGE_VERSION_MAJOR ${LIB_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${LIB_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${LIB_VERSION_PATCH})
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
set(CPACK_RESOURCE_FILE_README ${CMAKE_SOURCE_DIR}/README.md)

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "mGBA Game Boy Advance Emulator")
set(CPACK_PACKAGE_VENDOR "Jeffrey Pfau")
set(CPACK_PACKAGE_CONTACT "Jeffrey Pfau <jeffrey@endrift.com>")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
set(CPACK_DEBIAN_PACKAGE_SECTION "games")

SET(CPACK_DEB_COMPONENT_INSTALL ON)

set(CPACK_STRIP_FILES ${BINARY_NAME})

install(FILES ${CMAKE_SOURCE_DIR}/README.md ${CMAKE_SOURCE_DIR}/CHANGES DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT lib${BINARY_NAME})

include(CPack)

# Summaries
set(SUMMARY_GL_LIST)
if(USE_EPOXY)
	set(SUMMARY_GL_LIST "libepoxy")
else()
	if(BUILD_GL)
		list(APPEND SUMMARY_GL_LIST "OpenGL")
	endif()
	if(BUILD_GLES2)
		list(APPEND SUMMARY_GL_LIST "OpenGL|ES 2")
	endif()
endif()
if(NOT SUMMARY_GL_LIST)
	set(SUMMARY_GL OFF)
else()
	string(REPLACE ";" ", " SUMMARY_GL "${SUMMARY_GL_LIST}")
endif()
if(USE_LIBZIP)
	set(SUMMARY_ZIP libzip)
elseif(USE_ZLIB)
	set(SUMMARY_ZIP minizip)
else()
	set(SUMMARY_ZIP OFF)
endif()

message(STATUS "Platforms:")
message(STATUS "	Game Boy Advance: ${M_CORE_GBA}")
message(STATUS "	Game Boy: ${M_CORE_GB}")
message(STATUS "Features:")
message(STATUS "	CLI debugger: ${USE_CLI_DEBUGGER}")
message(STATUS "	GDB stub: ${USE_GDB_STUB}")
message(STATUS "	Video recording: ${USE_FFMPEG}")
message(STATUS "	GIF recording: ${USE_MAGICK}")
message(STATUS "	Screenshot/advanced savestate support: ${USE_PNG}")
message(STATUS "	ZIP support: ${SUMMARY_ZIP}")
message(STATUS "	7-Zip support: ${USE_LZMA}")
message(STATUS "	OpenGL support: ${SUMMARY_GL}")
message(STATUS "Frontends:")
message(STATUS "	Qt: ${BUILD_QT}")
message(STATUS "	SDL (${SDL_VERSION}): ${BUILD_SDL}")
message(STATUS "	Profiling: ${BUILD_PERF}")
message(STATUS "	Test harness: ${BUILD_TEST}")
message(STATUS "Cores:")
message(STATUS "	Libretro core: ${BUILD_LIBRETRO}")
if(APPLE)
	message(STATUS "	OpenEmu core: ${BUILD_OPENEMU}")
endif()
message(STATUS "Libraries:")
message(STATUS "	Static: ${BUILD_STATIC}")
message(STATUS "	Shared: ${BUILD_SHARED}")
