all repos — mgba @ 0.8.2

mGBA Game Boy Advance Emulator

CMakeLists.txt (view raw)

   1cmake_minimum_required(VERSION 3.1)
   2list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/src/platform/cmake/")
   3
   4if(POLICY CMP0025)
   5	cmake_policy(SET CMP0025 NEW)
   6endif()
   7if(POLICY CMP0072)
   8	cmake_policy(SET CMP0072 NEW)
   9endif()
  10project(mGBA)
  11set(BINARY_NAME mgba CACHE INTERNAL "Name of output binaries")
  12if(NOT MSVC)
  13	set(GCC_STD "c99")
  14	if(SWITCH)
  15		set(GCC_STD "gnu11")
  16	elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_COMPILER_VERSION VERSION_LESS "4.3")
  17		set(GCC_STD "gnu99")
  18	endif()
  19	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-missing-field-initializers -std=${GCC_STD}")
  20else()
  21	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /wd4003 /wd4244 /wd4146")
  22endif()
  23
  24if(NOT DEFINED LIBMGBA_ONLY)
  25	get_directory_property(LIBMGBA_ONLY EXCLUDE_FROM_ALL)
  26endif()
  27
  28if(NOT LIBMGBA_ONLY)
  29	set(USE_DEBUGGERS ON CACHE BOOL "Whether or not to enable the debugging infrastructure")
  30	if (NOT WIN32)
  31		set(USE_EDITLINE ON CACHE BOOL "Whether or not to enable the CLI-mode debugger")
  32	endif()
  33	set(USE_GDB_STUB ON CACHE BOOL "Whether or not to enable the GDB stub ARM debugger")
  34	set(USE_FFMPEG ON CACHE BOOL "Whether or not to enable FFmpeg support")
  35	set(USE_ZLIB ON CACHE BOOL "Whether or not to enable zlib support")
  36	set(USE_MINIZIP ON CACHE BOOL "Whether or not to enable external minizip support")
  37	set(USE_PNG ON CACHE BOOL "Whether or not to enable PNG support")
  38	set(USE_LIBZIP ON CACHE BOOL "Whether or not to enable LIBZIP support")
  39	set(USE_SQLITE3 ON CACHE BOOL "Whether or not to enable SQLite3 support")
  40	set(USE_ELF ON CACHE BOOL "Whether or not to enable ELF support")
  41	set(M_CORE_GBA ON CACHE BOOL "Build Game Boy Advance core")
  42	set(M_CORE_GB ON CACHE BOOL "Build Game Boy core")
  43	set(USE_LZMA ON CACHE BOOL "Whether or not to enable 7-Zip support")
  44	set(USE_DISCORD_RPC ON CACHE BOOL "Whether or not to enable Discord RPC support")
  45	set(ENABLE_SCRIPTING ON CACHE BOOL "Whether or not to enable scripting support")
  46	set(BUILD_QT ON CACHE BOOL "Build Qt frontend")
  47	set(BUILD_SDL ON CACHE BOOL "Build SDL frontend")
  48	set(BUILD_LIBRETRO OFF CACHE BOOL "Build libretro core")
  49	if(APPLE)
  50		set(BUILD_OPENEMU OFF CACHE BOOL "Build OpenEmu core")
  51	endif()
  52	set(BUILD_PERF OFF CACHE BOOL "Build performance profiling tool")
  53	set(BUILD_TEST OFF CACHE BOOL "Build testing harness")
  54	set(BUILD_SUITE OFF CACHE BOOL "Build test suite")
  55	set(BUILD_EXAMPLE OFF CACHE BOOL "Build example frontends")
  56	set(BUILD_PYTHON OFF CACHE BOOL "Build Python bindings")
  57	set(BUILD_STATIC OFF CACHE BOOL "Build a static library")
  58	set(BUILD_SHARED ON CACHE BOOL "Build a shared library")
  59	set(SKIP_LIBRARY OFF CACHE BOOL "Skip building the library (useful for only building libretro or OpenEmu cores)")
  60	set(BUILD_GL ON CACHE BOOL "Build with OpenGL")
  61	set(BUILD_GLES2 ON CACHE BOOL "Build with OpenGL|ES 2")
  62	set(BUILD_GLES3 OFF CACHE BOOL "Build with OpenGL|ES 3")
  63	set(USE_EPOXY ON CACHE STRING "Build with libepoxy")
  64	set(DISABLE_DEPS OFF CACHE BOOL "Build without dependencies")
  65	set(DISTBUILD OFF CACHE BOOL "Build distribution packages")
  66	if(WIN32)
  67		set(WIN32_UNIX_PATHS OFF CACHE BOOL "Use Unix-like paths")
  68		mark_as_advanced(WIN32_UNIX_PATHS)
  69	endif()
  70else()
  71	set(DISABLE_FRONTENDS ON)
  72	set(DISABLE_DEPS ON)
  73	set(BUILD_STATIC ON)
  74	set(BUILD_SHARED OFF)
  75	if(NOT DEFINED M_CORE_GBA)
  76		set(M_CORE_GBA ON)
  77	endif()
  78	if(NOT DEFINED M_CORE_GB)
  79		set(M_CORE_GB ON)
  80	endif()
  81endif()
  82
  83file(GLOB THIRD_PARTY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/inih/*.c)
  84set(CORE_VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-mem.c ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-fifo.c)
  85set(VFS_SRC)
  86include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/include)
  87
  88if(NOT CMAKE_BUILD_TYPE)
  89	set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type (e.g. Release, RelWithDebInfo, or Debug)" FORCE)
  90endif()
  91
  92if(UNIX OR WIN32_UNIX_PATHS)
  93	include(GNUInstallDirs)
  94else()
  95	set(CMAKE_INSTALL_LIBDIR ".")
  96	set(CMAKE_INSTALL_BINDIR ".")
  97	set(CMAKE_INSTALL_DATADIR ".")
  98	set(CMAKE_INSTALL_DOCDIR ".")
  99	set(CMAKE_INSTALL_INCLUDEDIR "include")
 100endif()
 101
 102set(LIBDIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Installed library directory")
 103mark_as_advanced(LIBDIR)
 104
 105if (BUILD_LIBRETRO)
 106	set(LIBRETRO_LIBDIR "${LIBDIR}" CACHE PATH "Installed library directory (Libretro)")
 107	mark_as_advanced(LIBRETRO_LIBDIR)
 108endif()
 109
 110if (BUILD_OPENEMU)
 111	set(OE_LIBDIR "${LIBDIR}" CACHE PATH "Installed library directory (OpenEmu)")
 112	mark_as_advanced(OE_LIBDIR)
 113endif()
 114
 115if (DISTBUILD)
 116       set(EXTRA_LICENSES "" CACHE FILEPATH "Extra licenses to include in distribution packaages")
 117       mark_as_advanced(EXTRA_LICENSES)
 118endif()
 119mark_as_advanced(DISTBUILD)
 120
 121set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIBDIR}")
 122
 123if (NOT DEFINED MANDIR)
 124	set(MANDIR ${CMAKE_INSTALL_MANDIR})
 125endif()
 126
 127include(FindFeature)
 128
 129# Version information
 130add_custom_target(version-info ALL
 131	COMMAND ${CMAKE_COMMAND}
 132	-DBINARY_NAME=${BINARY_NAME}
 133	-DCONFIG_FILE=${CMAKE_CURRENT_SOURCE_DIR}/src/core/version.c.in
 134	-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/version.c
 135	-P ${CMAKE_CURRENT_SOURCE_DIR}/version.cmake
 136	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
 137
 138include(${CMAKE_CURRENT_SOURCE_DIR}/version.cmake)
 139configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/core/version.c.in ${CMAKE_CURRENT_BINARY_DIR}/version.c)
 140
 141source_group("Generated sources" FILES ${CMAKE_CURRENT_BINARY_DIR}/version.c)
 142
 143# Advanced settings
 144if(NOT (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_COMPILER_VERSION VERSION_LESS "4.5"))
 145	set(DEFAULT_LTO ON)
 146else()
 147	set(DEFAULT_LTO OFF)
 148endif()
 149
 150set(BUILD_LTO ${DEFAULT_LTO} CACHE BOOL "Build with link-time optimization")
 151set(BUILD_PGO OFF CACHE BOOL "Build with profiling-guided optimization")
 152set(PGO_STAGE_2 CACHE BOOL "Rebuild for profiling-guided optimization after profiles have been generated")
 153set(PGO_DIR "/tmp/gba-pgo/" CACHE PATH "Profiling-guided optimization profiles path")
 154mark_as_advanced(BUILD_LTO BUILD_PGO PGO_STAGE_2 PGO_DIR)
 155set(PGO_PRE_FLAGS "-fprofile-generate=${PGO_DIR} -fprofile-arcs")
 156set(PGO_POST_FLAGS "-fprofile-use=${PGO_DIR} -fbranch-probabilities")
 157
 158if(BUILD_PGO AND NOT PGO_STAGE_2)
 159	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_PRE_FLAGS}")
 160	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
 161	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
 162elseif(BUILD_PGO AND PGO_STAGE_2)
 163	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_POST_FLAGS}")
 164	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_POST_FLAGS}")
 165	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_POST_FLAGS}")
 166endif()
 167
 168# Platform support
 169if(WIN32)
 170	set(WIN32_VERSION "${LIB_VERSION_MAJOR},${LIB_VERSION_MINOR},${LIB_VERSION_PATCH}")
 171	add_definitions(-D_WIN32_WINNT=0x0600)
 172	if(MSVC)
 173		add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN)
 174		set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
 175		add_definitions(-D_UNICODE -DUNICODE)
 176	else()
 177		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -municode")
 178		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -municode")
 179	endif()
 180	list(APPEND OS_LIB ws2_32 shlwapi)
 181	list(APPEND CORE_VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/windows/vfs-w32.c)
 182	file(GLOB OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/windows/*.c)
 183	source_group("Windows-specific code" FILES ${OS_SRC})
 184elseif(UNIX)
 185	set(USE_PTHREADS ON)
 186
 187	if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
 188		add_definitions(-D_GNU_SOURCE)
 189	endif()
 190
 191	list(APPEND CORE_VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
 192	file(GLOB OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/posix/*.c)
 193	source_group("POSIX-specific code" FILES ${OS_SRC})
 194endif()
 195
 196if(APPLE)
 197	add_definitions(-D_DARWIN_C_SOURCE)
 198	if(CMAKE_SYSTEM_VERSION VERSION_GREATER "10.5.8")
 199		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6")
 200	endif()
 201endif()
 202
 203if(NOT HAIKU AND NOT MSVC AND NOT PSP2)
 204	set(M_LIBRARY m)
 205endif()
 206list(APPEND OS_LIB ${M_LIBRARY})
 207
 208if(APPLE OR CMAKE_C_COMPILER_ID STREQUAL "GNU" AND BUILD_LTO)
 209	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto")
 210	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
 211endif()
 212
 213if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
 214	find_program(OBJCOPY ${cross_prefix}objcopy)
 215	find_program(STRIP ${cross_prefix}strip)
 216
 217	set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELEASE} -gdwarf")
 218	set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf")
 219endif()
 220
 221if(BUILD_BBB OR BUILD_RASPI OR BUILD_PANDORA)
 222	if(NOT BUILD_EGL)
 223		add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
 224	endif()
 225endif()
 226
 227if(BUILD_RASPI)
 228	set(BUILD_GL OFF CACHE BOOL "OpenGL not supported" FORCE)
 229endif()
 230
 231if(BUILD_PANDORA)
 232	add_definitions(-DBUILD_PANDORA)
 233endif()
 234
 235if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm.*")
 236	enable_language(ASM)
 237endif()
 238
 239if(PSP2 OR WII)
 240	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
 241endif()
 242
 243if(DEFINED 3DS OR DEFINED PSP2 OR DEFINED WII OR DEFINED SWITCH)
 244	set(IS_EMBEDDED ON)
 245	set(USE_DEBUGGERS OFF)
 246	set(USE_SQLITE3 OFF)
 247	set(USE_DISCORD_RPC OFF)
 248	set(USE_LIBZIP OFF CACHE BOOL "")
 249endif()
 250
 251if(DEFINED 3DS)
 252	add_definitions(-DFIXED_ROM_BUFFER)
 253endif()
 254
 255if(DEFINED SWITCH)
 256	set(BUILD_GLES3 ON CACHE BOOL "Build with OpenGL|ES 3" FORCE)
 257endif()
 258
 259if(NOT M_CORE_GBA)
 260	set(USE_GDB_STUB OFF)
 261endif()
 262
 263if(NOT USE_DEBUGGERS)
 264	set(USE_EDITLINE OFF)
 265	set(USE_GDB_STUB OFF)
 266endif()
 267
 268if(WII)
 269	add_definitions(-U__STRICT_ANSI__)
 270endif()
 271
 272if(3DS OR WII)
 273	add_definitions(-D_GNU_SOURCE)
 274endif()
 275
 276include(CheckCCompilerFlag)
 277include(CheckFunctionExists)
 278include(CheckIncludeFiles)
 279check_function_exists(strdup HAVE_STRDUP)
 280check_function_exists(strndup HAVE_STRNDUP)
 281if(NOT DEFINED PSP2)
 282	check_function_exists(localtime_r HAVE_LOCALTIME_R)
 283endif()
 284check_include_files("xlocale.h" HAVE_XLOCALE)
 285if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
 286	check_function_exists(snprintf_l HAVE_SNPRINTF_L)
 287	if(ANDROID AND ANDROID_NDK_MAJOR GREATER 13)
 288		set(HAVE_STRTOF_L ON)
 289	elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
 290		# The strtof_l on Linux not actually exposed nor actually strtof_l
 291		set(HAVE_STRTOF_L OFF)
 292	else()
 293		check_function_exists(strtof_l HAVE_STRTOF_L)
 294	endif()
 295	check_function_exists(newlocale HAVE_NEWLOCALE)
 296	check_function_exists(freelocale HAVE_FREELOCALE)
 297	check_function_exists(uselocale HAVE_USELOCALE)
 298	check_function_exists(setlocale HAVE_SETLOCALE)
 299else()
 300	if(DEFINED 3DS OR DEFINED WII OR DEFINED SWITCH)
 301		set(CMAKE_REQUIRED_FLAGS -Wl,--require-defined,snprintf_l)
 302		check_function_exists(snprintf_l HAVE_SNPRINTF_L)
 303		set(CMAKE_REQUIRED_FLAGS -Wl,--require-defined,strtof_l)
 304		check_function_exists(strtof_l HAVE_STRTOF_L)
 305		set(CMAKE_REQUIRED_FLAGS -Wl,--require-defined,newlocale)
 306		check_function_exists(newlocale HAVE_NEWLOCALE)
 307		set(CMAKE_REQUIRED_FLAGS -Wl,--require-defined,freelocale)
 308		check_function_exists(freelocale HAVE_FREELOCALE)
 309		set(CMAKE_REQUIRED_FLAGS -Wl,--require-defined,uselocale)
 310		check_function_exists(uselocale HAVE_USELOCALE)
 311		set(CMAKE_REQUIRED_FLAGS -Wl,--require-defined,setlocale)
 312		check_function_exists(setlocale HAVE_SETLOCALE)
 313		unset(CMAKE_REQUIRED_FLAGS)
 314	endif()
 315	if(NOT IS_EMBEDDED)
 316		set(DISABLE_DEPS ON CACHE BOOL "This platform cannot build with dependencies" FORCE)
 317	endif()
 318	set(BUILD_STATIC ON CACHE BOOL "" FORCE)
 319	set(BUILD_SHARED OFF CACHE BOOL "" FORCE)
 320	set(DISABLE_FRONTENDS ON)
 321	set(MINIMAL_CORE ON)
 322	set(ENABLE_EXTRA ON)
 323endif()
 324
 325check_function_exists(chmod HAVE_CHMOD)
 326check_function_exists(umask HAVE_UMASK)
 327
 328if(USE_PTHREADS)
 329	check_include_files("pthread.h" HAVE_PTHREAD_H)
 330	if(HAVE_PTHREAD_H)
 331		check_c_compiler_flag(-pthread HAVE_PTHREAD)
 332		if(HAVE_PTHREAD AND NOT APPLE AND NOT HAIKU)
 333			set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
 334			set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
 335		endif()
 336
 337		check_function_exists(pthread_create HAVE_PTHREAD_CREATE)
 338		if(HAVE_PTHREAD_CREATE)
 339			add_definitions(-DUSE_PTHREADS)
 340
 341			check_include_files("pthread_np.h" HAVE_PTHREAD_NP_H)
 342
 343			check_function_exists(pthread_setname_np HAVE_PTHREAD_SETNAME_NP)
 344			check_function_exists(pthread_set_name_np HAVE_PTHREAD_SET_NAME_NP)
 345		endif()
 346	endif()
 347endif()
 348
 349set(FUNCTION_DEFINES)
 350
 351if(HAVE_STRDUP)
 352	list(APPEND FUNCTION_DEFINES HAVE_STRDUP)
 353endif()
 354
 355if(HAVE_STRNDUP)
 356	list(APPEND FUNCTION_DEFINES HAVE_STRNDUP)
 357endif()
 358
 359if(HAVE_LOCALTIME_R)
 360	list(APPEND FUNCTION_DEFINES HAVE_LOCALTIME_R)
 361endif()
 362
 363if(HAVE_NEWLOCALE AND HAVE_FREELOCALE AND HAVE_USELOCALE OR APPLE)
 364	list(APPEND FUNCTION_DEFINES HAVE_LOCALE)
 365	if (HAVE_SNPRINTF_L)
 366		list(APPEND FUNCTION_DEFINES HAVE_SNPRINTF_L)
 367	endif()
 368endif()
 369
 370if(HAVE_SETLOCALE)
 371	list(APPEND FUNCTION_DEFINES HAVE_SETLOCALE)
 372endif()
 373
 374if (HAVE_STRTOF_L)
 375	list(APPEND FUNCTION_DEFINES HAVE_STRTOF_L)
 376endif()
 377
 378if(HAVE_XLOCALE)
 379	list(APPEND FUNCTION_DEFINES HAVE_XLOCALE)
 380endif()
 381
 382if(HAVE_CHMOD)
 383	list(APPEND FUNCTION_DEFINES HAVE_CHMOD)
 384endif()
 385
 386if(HAVE_UMASK)
 387	list(APPEND FUNCTION_DEFINES HAVE_UMASK)
 388endif()
 389
 390if(HAVE_PTHREAD_NP_H)
 391	list(APPEND FUNCTION_DEFINES HAVE_PTHREAD_NP_H)
 392endif()
 393
 394if(HAVE_PTHREAD_SETNAME_NP)
 395	list(APPEND FUNCTION_DEFINES HAVE_PTHREAD_SETNAME_NP)
 396endif()
 397
 398if(HAVE_PTHREAD_SET_NAME_NP)
 399	list(APPEND FUNCTION_DEFINES HAVE_PTHREAD_SET_NAME_NP)
 400endif()
 401
 402# Feature dependencies
 403set(FEATURE_DEFINES)
 404set(FEATURE_FLAGS)
 405set(FEATURES)
 406set(ENABLES)
 407if(CMAKE_SYSTEM_NAME MATCHES ".*BSD|DragonFly")
 408	set(LIBEDIT_LIBRARIES -ledit)
 409	if (CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
 410		list(APPEND LIBEDIT_LIBRARIES -ltermcap)
 411	endif()
 412else()
 413	find_feature(USE_EDITLINE "libedit")
 414endif()
 415if(BUILD_GL)
 416	find_package(OpenGL QUIET)
 417	if(NOT OPENGL_FOUND)
 418		set(BUILD_GL OFF CACHE BOOL "OpenGL not found" FORCE)
 419	endif()
 420endif()
 421if(NOT BUILD_GL)
 422	set(OPENGL_LIBRARY "" CACHE PATH "" FORCE)
 423endif()
 424if(BUILD_GLES2 AND NOT BUILD_RASPI AND NOT CMAKE_SYSTEM_NAME MATCHES "^(Windows|Darwin|Linux|.*BSD|DragonFly|Haiku)$")
 425	find_path(OPENGLES2_INCLUDE_DIR NAMES GLES2/gl2.h)
 426	find_library(OPENGLES2_LIBRARY NAMES GLESv2 GLESv2_CM)
 427	if(NOT OPENGLES2_INCLUDE_DIR OR NOT OPENGLES2_LIBRARY)
 428		set(BUILD_GLES2 OFF CACHE BOOL "OpenGL|ES 2 not found" FORCE)
 429	endif()
 430endif()
 431if(NOT BUILD_GLES2)
 432	set(OPENGLES2_LIBRARY "" CACHE PATH "" FORCE)
 433endif()
 434if(BUILD_GL)
 435	list(APPEND OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/opengl/gl.c)
 436	list(APPEND DEPENDENCY_LIB ${OPENGL_LIBRARY})
 437	include_directories(${OPENGL_INCLUDE_DIR})
 438endif()
 439if(BUILD_GLES2)
 440	list(APPEND OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/opengl/gles2.c)
 441	list(APPEND DEPENDENCY_LIB ${OPENGLES2_LIBRARY})
 442	include_directories(${OPENGLES2_INCLUDE_DIR})
 443endif()
 444if(BUILD_GLES3)
 445	find_path(OPENGLES3_INCLUDE_DIR NAMES GLES3/gl3.h)
 446	find_library(OPENGLES3_LIBRARY NAMES GLESv3 GLESv2)
 447	if(NOT OPENGLES3_INCLUDE_DIR OR NOT OPENGLES3_LIBRARY)
 448		set(BUILD_GLES3 OFF CACHE BOOL "OpenGL|ES 3 not found" FORCE)
 449	endif()
 450endif()
 451
 452if(DISABLE_DEPS)
 453	set(USE_GDB_STUB OFF)
 454	set(USE_DISCORD_RPC OFF)
 455	set(USE_SQLITE3 OFF)
 456	set(USE_PNG OFF)
 457	set(USE_ZLIB OFF)
 458endif()
 459
 460set(WANT_ZLIB ${USE_ZLIB})
 461set(WANT_PNG ${USE_PNG})
 462set(WANT_SQLITE3 ${USE_SQLITE3})
 463set(USE_CMOCKA ${BUILD_SUITE})
 464
 465find_feature(USE_FFMPEG "libavcodec;libavfilter;libavformat;libavutil;libswscale")
 466find_feature(USE_ZLIB "ZLIB")
 467find_feature(USE_MINIZIP "minizip")
 468find_feature(USE_PNG "PNG")
 469find_feature(USE_LIBZIP "libzip")
 470find_feature(USE_EPOXY "epoxy")
 471find_feature(USE_CMOCKA "cmocka")
 472find_feature(USE_SQLITE3 "sqlite3")
 473find_feature(USE_ELF "libelf")
 474find_feature(ENABLE_PYTHON "PythonLibs")
 475
 476if(USE_FFMPEG)
 477	set(USE_LIBAVRESAMPLE ON)
 478	set(USE_LIBSWRESAMPLE ON)
 479	find_feature(USE_LIBAVRESAMPLE "libavresample")
 480	find_feature(USE_LIBSWRESAMPLE "libswresample")
 481endif()
 482
 483# Features
 484add_subdirectory(src/debugger)
 485add_subdirectory(src/feature)
 486
 487set(FEATURE_SRC)
 488set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
 489
 490if(USE_EDITLINE)
 491	list(APPEND FEATURES EDITLINE)
 492	include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
 493	link_directories(${LIBEDIT_LIBRARY_DIRS})
 494	set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
 495	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libedit2")
 496	list(APPEND FEATURE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/feature/editline/cli-el-backend.c")
 497else()
 498	set(DEBUGGER_LIB "")
 499endif()
 500
 501if(USE_GDB_STUB)
 502	list(APPEND FEATURES GDB_STUB)
 503	list(APPEND FEATURE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/debugger/gdb-stub.c)
 504endif()
 505source_group("Debugger" FILES ${DEBUGGER_SRC})
 506
 507if(USE_FFMPEG)
 508	list(APPEND FEATURES FFMPEG)
 509	if(USE_LIBSWRESAMPLE)
 510		list(APPEND FEATURES LIBSWRESAMPLE)
 511	else()
 512		list(APPEND FEATURES LIBAVRESAMPLE)
 513		list(APPEND FEATURES LIBAV)
 514	endif()
 515	include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFILTER_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVRESAMPLE_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS} ${LIBSWRESAMPLE_INCLUDE_DIRS} ${LIBSWSCALE_INCLUDE_DIRS})
 516	link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFILTER_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVRESAMPLE_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS} ${LIBSWRESAMPLE_LIBRARY_DIRS} ${LIBSWSCALE_LIBRARY_DIRS})
 517	list(APPEND FEATURE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/feature/ffmpeg/ffmpeg-encoder.c")
 518	string(REGEX MATCH "^[0-9]+" LIBAVCODEC_VERSION_MAJOR ${libavcodec_VERSION})
 519	string(REGEX MATCH "^[0-9]+" LIBAVFILTER_VERSION_MAJOR ${libavfilter_VERSION})
 520	string(REGEX MATCH "^[0-9]+" LIBAVFORMAT_VERSION_MAJOR ${libavformat_VERSION})
 521	string(REGEX MATCH "^[0-9]+" LIBAVUTIL_VERSION_MAJOR ${libavutil_VERSION})
 522	string(REGEX MATCH "^[0-9]+" LIBSWSCALE_VERSION_MAJOR ${libswscale_VERSION})
 523	list(APPEND DEPENDENCY_LIB ${LIBAVCODEC_LIBRARIES} ${LIBAVFILTER_LIBRARIES} ${LIBAVFORMAT_LIBRARIES} ${LIBAVRESAMPLE_LIBRARIES} ${LIBAVUTIL_LIBRARIES} ${LIBSWSCALE_LIBRARIES} ${LIBSWRESAMPLE_LIBRARIES})
 524	if(WIN32)
 525		list(APPEND DEPENDENCY_LIB bcrypt)
 526	endif()
 527	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}")
 528	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavfilter${LIBAVFILTER_VERSION_MAJOR}|libavfilter-ffmpeg${LIBAVFILTER_VERSION_MAJOR}")
 529	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavformat${LIBAVFORMAT_VERSION_MAJOR}|libavformat-ffmpeg${LIBAVFORMAT_VERSION_MAJOR}")
 530	if(USE_LIBSWRESAMPLE)
 531		string(REGEX MATCH "^[0-9]+" LIBSWRESAMPLE_VERSION_MAJOR ${libswresample_VERSION})
 532		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libswresample${LIBSWRESAMPLE_VERSION_MAJOR}|libswresample-ffmpeg${LIBSWRESAMPLE_VERSION_MAJOR}")
 533	else()
 534		string(REGEX MATCH "^[0-9]+" LIBAVRESAMPLE_VERSION_MAJOR ${libavresample_VERSION})
 535		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavresample${LIBAVRESAMPLE_VERSION_MAJOR}|libavresample-ffmpeg${LIBAVRESAMPLE_VERSION_MAJOR}")
 536	endif()
 537	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavutil${LIBAVUTIL_VERSION_MAJOR}|libavutil-ffmpeg${LIBAVUTIL_VERSION_MAJOR}")
 538	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libswscale${LIBSWSCALE_VERSION_MAJOR}|libswscale-ffmpeg${LIBSWSCALE_VERSION_MAJOR}")
 539	set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libavcodec-extra|libavcodec-ffmpeg-extra${LIBAVCODEC_VERSION_MAJOR}")
 540	if(APPLE)
 541		set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
 542		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
 543	endif()
 544endif()
 545
 546list(APPEND THIRD_PARTY_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/blip_buf/blip_buf.c")
 547
 548if(WANT_ZLIB AND NOT USE_ZLIB)
 549	set(SKIP_INSTALL_ALL ON)
 550	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib zlib EXCLUDE_FROM_ALL)
 551	set_target_properties(zlibstatic PROPERTIES INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/zlib;${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib" POSITION_INDEPENDENT_CODE ON)
 552	set(ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib ${CMAKE_CURRENT_BINARY_DIR}/zlib)
 553	set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib ${CMAKE_CURRENT_BINARY_DIR}/zlib)
 554	set(ZLIB_LIBRARY zlibstatic)
 555	list(APPEND DEPENDENCY_LIB zlibstatic)
 556	set(USE_ZLIB ON)
 557endif()
 558
 559if(USE_ZLIB)
 560	list(APPEND FEATURES ZLIB)
 561	include_directories(AFTER ${ZLIB_INCLUDE_DIRS})
 562	list(APPEND DEPENDENCY_LIB ${ZLIB_LIBRARIES})
 563	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},zlib1g")
 564	set(HAVE_CRC32 ON)
 565	list(APPEND OS_LIB ${ZLIB_LIBRARIES})
 566else()
 567	# zlib pulls in crc32
 568	check_function_exists(crc32 HAVE_CRC32)
 569endif()
 570
 571if(HAVE_CRC32)
 572	list(APPEND FUNCTION_DEFINES HAVE_CRC32)
 573endif()
 574
 575if(WANT_PNG AND USE_ZLIB AND NOT USE_PNG)
 576	set(PNG_STATIC ON CACHE BOOL "" FORCE)
 577	set(PNG_SHARED OFF CACHE BOOL "" FORCE)
 578	set(PNG_TESTS OFF CACHE BOOL "" FORCE)
 579	set(SKIP_INSTALL_ALL ON)
 580	if (SWITCH)
 581		set(PNG_ARM_NEON "off" CACHE STRING "" FORCE)
 582	endif()
 583	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/libpng libpng EXCLUDE_FROM_ALL)
 584	set_target_properties(png_static PROPERTIES INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/libpng;${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/libpng;${ZLIB_INCLUDE_DIRS}" POSITION_INDEPENDENT_CODE ON)
 585	set(PNG_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/libpng ${CMAKE_CURRENT_BINARY_DIR}/libpng)
 586	list(APPEND DEPENDENCY_LIB png_static)
 587	set(USE_PNG ON)
 588endif()
 589
 590if(USE_PNG)
 591	list(APPEND FEATURES PNG)
 592	include_directories(AFTER ${PNG_INCLUDE_DIRS})
 593	list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
 594	if(PNG_VERSION_STRING)
 595		string(REGEX MATCH "^[0-9]+\\.[0-9]+" PNG_VERSION_PARTIAL ${PNG_VERSION_STRING})
 596		if(${PNG_VERSION_PARTIAL} STREQUAL "1.6")
 597			set(PNG_DEB_VERSION "16-16")
 598		else()
 599			set(PNG_DEB_VERSION "12-0")
 600		endif()
 601		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpng${PNG_DEB_VERSION}")
 602	endif()
 603endif()
 604
 605if(WANT_SQLITE3 AND NOT USE_SQLITE3)
 606	list(APPEND FEATURE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/sqlite3/sqlite3.c)
 607	include_directories(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/sqlite3/)
 608	set(USE_SQLITE3 ON)
 609endif()
 610
 611if(USE_LIBZIP)
 612	include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
 613	link_directories(${LIBZIP_LIBRARY_DIRS})
 614	list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
 615	list(APPEND FEATURES LIBZIP)
 616	list(APPEND VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-zip.c)
 617	string(REGEX MATCH "^[0-9]+" LIBZIP_VERSION_MAJOR ${libzip_VERSION})
 618	if (LIBZIP_VERSION_MAJOR LESS 1)
 619		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip2")
 620	elseif(LIBZIP_VERSION_MAJOR EQUAL 1)
 621		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip4|libzip5")
 622	else()
 623		message(AUTHOR_WARNING Unknown version of libzip detected: ${libzip_VERSION})
 624	endif()
 625elseif(USE_MINIZIP)
 626	include_directories(AFTER ${MINIZIP_INCLUDE_DIRS})
 627	link_directories(${MINIZIP_LIBRARY_DIRS})
 628	list(APPEND DEPENDENCY_LIB ${MINIZIP_LIBRARIES})
 629	list(APPEND FEATURES MINIZIP)
 630	list(APPEND VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-zip.c)
 631	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libminizip1")
 632elseif(USE_ZLIB)
 633	list(APPEND VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-zip.c
 634		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib/contrib/minizip/ioapi.c
 635		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib/contrib/minizip/unzip.c)
 636	if(NOT MSVC)
 637		set_source_files_properties(
 638			${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib/contrib/minizip/ioapi.c
 639			${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/zlib/contrib/minizip/unzip.c
 640			PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter -Wno-implicit-function-declaration")
 641	endif()
 642endif()
 643
 644if (USE_LZMA)
 645	include_directories(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma)
 646	add_definitions(-D_7ZIP_PPMD_SUPPPORT)
 647	list(APPEND VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-lzma.c)
 648	set(LZMA_SRC
 649		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zAlloc.c
 650		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zArcIn.c
 651		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zBuf.c
 652		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zBuf2.c
 653		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zCrc.c
 654		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zCrcOpt.c
 655		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zDec.c
 656		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/CpuArch.c
 657		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Delta.c
 658		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/LzmaDec.c
 659		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Lzma2Dec.c
 660		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Bra.c
 661		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Bra86.c
 662		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/BraIA64.c
 663		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Bcj2.c
 664		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Ppmd7.c
 665		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/Ppmd7Dec.c
 666		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zFile.c
 667		${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/lzma/7zStream.c)
 668	list(APPEND VFS_SRC ${LZMA_SRC})
 669	list(APPEND FEATURES LZMA)
 670endif()
 671
 672if(USE_EPOXY)
 673	list(APPEND OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/opengl/gl.c ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/opengl/gles2.c)
 674	add_definitions(-DBUILD_GL -DBUILD_GLES2)
 675	list(APPEND FEATURES EPOXY)
 676	include_directories(AFTER ${EPOXY_INCLUDE_DIRS})
 677	link_directories(${EPOXY_LIBRARY_DIRS})
 678	set(OPENGLES2_LIBRARY ${EPOXY_LIBRARIES})
 679	list(APPEND DEPENDENCY_LIB ${EPOXY_LIBRARIES})
 680	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libepoxy0")
 681elseif(BUILD_GL)
 682	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libgl1|libgles2")
 683elseif(BUILD_GLES2)
 684	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libgles2")
 685endif()
 686
 687if(WIN32 AND NOT SKIP_LIBRARY AND NOT USE_EPOXY)
 688    message(FATAL_ERROR "Windows requires epoxy module!")
 689endif()
 690
 691if(USE_SQLITE3)
 692	list(APPEND FEATURES SQLITE3)
 693	include_directories(AFTER ${SQLITE3_INCLUDE_DIRS})
 694	link_directories(${SQLITE3_LIBRARY_DIRS})
 695	list(APPEND DEPENDENCY_LIB ${SQLITE3_LIBRARIES})
 696	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libsqlite3-0")
 697	list(APPEND FEATURE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/feature/sqlite3/no-intro.c")
 698endif()
 699
 700if(USE_ELF)
 701	list(APPEND FEATURES ELF)
 702	include_directories(AFTER ${LIBELF_INCLUDE_DIRS})
 703	link_directories(${LIBELF_LIBRARY_DIRS})
 704	list(APPEND DEPENDENCY_LIB ${LIBELF_LIBRARIES})
 705	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libelf1")
 706endif()
 707
 708if (USE_DISCORD_RPC)
 709	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/discord-rpc discord-rpc EXCLUDE_FROM_ALL)
 710	list(APPEND FEATURES DISCORD_RPC)
 711	include_directories(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/src/third-party/discord-rpc/include)
 712endif()
 713
 714if(ENABLE_SCRIPTING)
 715	list(APPEND ENABLES SCRIPTING)
 716
 717	if(BUILD_PYTHON)
 718		find_package(PythonLibs ${USE_PYTHON_VERSION})
 719		list(APPEND DEPENDENCY_LIB ${PYTHON_LIBRARIES})
 720		include_directories(AFTER ${PYTHON_INCLUDE_DIRS})
 721		list(APPEND ENABLES PYTHON)
 722	endif()
 723endif()
 724
 725add_subdirectory(src/arm)
 726add_subdirectory(src/core)
 727add_subdirectory(src/gb)
 728add_subdirectory(src/gba)
 729add_subdirectory(src/sm83)
 730add_subdirectory(src/util)
 731
 732list(APPEND GUI_SRC ${EXTRA_GUI_SRC})
 733list(APPEND UTIL_SRC ${CMAKE_CURRENT_BINARY_DIR}/version.c)
 734
 735set(TEST_SRC ${CORE_TEST_SRC})
 736if(M_CORE_GB)
 737	add_definitions(-DM_CORE_GB)
 738	list(APPEND CORE_SRC ${SM83_SRC} ${GB_SRC})
 739	list(APPEND DEBUGGER_SRC ${SM83_DEBUGGER_SRC} ${GB_DEBUGGER_SRC})
 740	list(APPEND TEST_SRC ${SM83_TEST_SRC} ${GB_TEST_SRC})
 741endif()
 742
 743if(M_CORE_GBA)
 744	add_definitions(-DM_CORE_GBA)
 745	list(APPEND CORE_SRC ${ARM_SRC} ${GBA_SRC})
 746	list(APPEND DEBUGGER_SRC ${ARM_DEBUGGER_SRC} ${GBA_DEBUGGER_SRC})
 747	list(APPEND TEST_SRC ${ARM_TEST_SRC} ${GBA_TEST_SRC})
 748endif()
 749
 750if(USE_DEBUGGERS)
 751	list(APPEND FEATURE_SRC ${DEBUGGER_SRC})
 752	list(APPEND TEST_SRC ${DEBUGGER_TEST_SRC})
 753	list(APPEND FEATURES DEBUGGERS)
 754endif()
 755
 756foreach(FEATURE IN LISTS FEATURES)
 757	list(APPEND FEATURE_DEFINES "USE_${FEATURE}")
 758endforeach()
 759
 760foreach(ENABLE IN LISTS ENABLES)
 761	list(APPEND FEATURE_DEFINES "ENABLE_${ENABLE}")
 762endforeach()
 763
 764source_group("Virtual files" FILES ${CORE_VFS_SRC} ${VFS_SRC})
 765source_group("Extra features" FILES ${FEATURE_SRC})
 766source_group("Third-party code" FILES ${THIRD_PARTY_SRC})
 767
 768# Platform binaries
 769set(OS_DEFINES)
 770if(DEFINED 3DS)
 771	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/3ds ${CMAKE_CURRENT_BINARY_DIR}/3ds)
 772endif()
 773
 774if(DEFINED WII)
 775	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/wii ${CMAKE_CURRENT_BINARY_DIR}/wii)
 776endif()
 777
 778if(DEFINED PSP2)
 779	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/psp2 ${CMAKE_CURRENT_BINARY_DIR}/psp2)
 780endif()
 781
 782if(DEFINED SWITCH)
 783	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/switch ${CMAKE_CURRENT_BINARY_DIR}/switch)
 784endif()
 785
 786# Binaries
 787list(APPEND CORE_SRC
 788	${UTIL_SRC}
 789	${CORE_VFS_SRC}
 790	${OS_SRC}
 791	${THIRD_PARTY_SRC})
 792list(APPEND TEST_SRC ${UTIL_TEST_SRC})
 793
 794set(SRC ${CORE_SRC} ${VFS_SRC})
 795if(NOT MINIMAL_CORE)
 796	set(ENABLE_EXTRA ON)
 797	if(M_CORE_GBA)
 798		list(APPEND SRC ${GBA_SIO_SRC})
 799	endif()
 800	if(M_CORE_GB)
 801		list(APPEND SRC ${GB_SIO_SRC})
 802	endif()
 803	list(APPEND SRC ${FEATURE_SRC})
 804endif()
 805
 806if(ENABLE_EXTRA)
 807	if(M_CORE_GBA)
 808		list(APPEND SRC ${GBA_EXTRA_SRC})
 809	endif()
 810	if(M_CORE_GB)
 811		list(APPEND SRC ${GB_EXTRA_SRC})
 812	endif()
 813	list(APPEND SRC ${EXTRA_SRC})
 814endif()
 815
 816if(NOT SKIP_LIBRARY)
 817	if(NOT BUILD_STATIC AND NOT BUILD_SHARED)
 818		set(BUILD_SHARED ON)
 819	endif()
 820
 821	if(BUILD_SHARED)
 822		add_library(${BINARY_NAME} SHARED ${SRC} ${VFS_SRC})
 823		if(BUILD_STATIC)
 824			add_library(${BINARY_NAME}-static STATIC ${SRC})
 825			target_include_directories(${BINARY_NAME}-static BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR}/include)
 826			set_target_properties(${BINARY_NAME}-static PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES};BUILD_STATIC" COMPILE_OPTIONS "${FEATURE_FLAGS}")
 827			install(TARGETS ${BINARY_NAME}-static DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
 828			add_dependencies(${BINARY_NAME}-static version-info)
 829		endif()
 830	else()
 831		add_library(${BINARY_NAME} STATIC ${SRC})
 832		list(APPEND OS_DEFINES BUILD_STATIC)
 833	endif()
 834
 835	target_include_directories(${BINARY_NAME} BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR}/include)
 836	set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI} COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}" COMPILE_OPTIONS "${FEATURE_FLAGS}")
 837	add_dependencies(${BINARY_NAME} version-info)
 838
 839	include(GenerateExportHeader)
 840	file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/mgba-util)
 841	generate_export_header(${BINARY_NAME} BASE_NAME ${BINARY_NAME} STATIC_DEFINE BUILD_STATIC EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/mgba-util/dllexports.h)
 842
 843	target_link_libraries(${BINARY_NAME} ${DEBUGGER_LIB} ${DEPENDENCY_LIB} ${OS_LIB})
 844	install(TARGETS ${BINARY_NAME} LIBRARY DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME} NAMELINK_SKIP ARCHIVE DESTINATION ${LIBDIR} RUNTIME DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
 845	if(BUILD_SHARED)
 846		install(TARGETS ${BINARY_NAME} LIBRARY DESTINATION ${LIBDIR} COMPONENT ${BINARY_NAME}-dev NAMELINK_ONLY)
 847	endif()
 848	if(UNIX AND NOT APPLE AND NOT HAIKU)
 849		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-16.png DESTINATION share/icons/hicolor/16x16/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 850		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-24.png DESTINATION share/icons/hicolor/24x24/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 851		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-32.png DESTINATION share/icons/hicolor/32x32/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 852		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-48.png DESTINATION share/icons/hicolor/48x48/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 853		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-64.png DESTINATION share/icons/hicolor/64x64/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 854		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-96.png DESTINATION share/icons/hicolor/96x96/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 855		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-128.png DESTINATION share/icons/hicolor/128x128/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 856		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-256.png DESTINATION share/icons/hicolor/256x256/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 857		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/mgba-512.png DESTINATION share/icons/hicolor/512x512/apps RENAME mgba.png COMPONENT ${BINARY_NAME})
 858	endif()
 859else()
 860	set(BUILD_SHARED OFF)
 861	set(BUILD_STATIC OFF)
 862	find_library(${BINARY_NAME} ${BINARY_NAME})
 863	if(NOT ${BINARY_NAME}_FOUND)
 864		set(DISABLE_FRONTENDS ON)
 865		set(BUILD_PERF OFF)
 866		set(BUILD_TEST OFF)
 867		set(BUILD_SUITE OFF)
 868	endif()
 869endif()
 870
 871if(BUILD_GL)
 872	add_definitions(-DBUILD_GL)
 873endif()
 874
 875if(BUILD_GLES2)
 876	add_definitions(-DBUILD_GLES2)
 877endif()
 878
 879if(BUILD_GLES3)
 880	add_definitions(-DBUILD_GLES3)
 881endif()
 882
 883if(DISABLE_FRONTENDS)
 884	set(BUILD_SDL OFF)
 885	set(BUILD_QT OFF)
 886endif()
 887
 888if(BUILD_PYTHON)
 889	enable_testing()
 890	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/python ${CMAKE_CURRENT_BINARY_DIR}/python)
 891endif()
 892
 893if(BUILD_LIBRETRO)
 894	file(GLOB RETRO_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/libretro/*.c)
 895	add_library(${BINARY_NAME}_libretro SHARED ${CORE_SRC} ${RETRO_SRC})
 896	add_dependencies(${BINARY_NAME}_libretro version-info)
 897	set_target_properties(${BINARY_NAME}_libretro PROPERTIES PREFIX "" COMPILE_DEFINITIONS "__LIBRETRO__;COLOR_16_BIT;COLOR_5_6_5;DISABLE_THREADING;MGBA_STANDALONE;${OS_DEFINES};${FUNCTION_DEFINES};MINIMAL_CORE=2")
 898	target_link_libraries(${BINARY_NAME}_libretro ${OS_LIB})
 899	if(MSVC)
 900		install(TARGETS ${BINARY_NAME}_libretro RUNTIME DESTINATION ${LIBRETRO_LIBDIR} COMPONENT ${BINARY_NAME}_libretro)
 901	else()
 902		install(TARGETS ${BINARY_NAME}_libretro LIBRARY DESTINATION ${LIBRETRO_LIBDIR} COMPONENT ${BINARY_NAME}_libretro NAMELINK_SKIP)
 903	endif()
 904endif()
 905
 906if(BUILD_OPENEMU)
 907	find_library(FOUNDATION Foundation)
 908	find_library(OPENEMUBASE OpenEmuBase)
 909	file(GLOB OE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/openemu/*.m)
 910	add_library(${BINARY_NAME}-openemu MODULE ${CORE_SRC} ${OS_SRC})
 911	set_target_properties(${BINARY_NAME}-openemu PROPERTIES
 912		MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/openemu/Info.plist.in
 913		BUNDLE TRUE
 914		BUNDLE_EXTENSION oecoreplugin
 915		OUTPUT_NAME ${PROJECT_NAME}
 916		COMPILE_DEFINITIONS "DISABLE_THREADING;MGBA_STANDALONE;${OS_DEFINES};${FUNCTION_DEFINES};MINIMAL_CORE=1")
 917	target_link_libraries(${BINARY_NAME}-openemu ${OS_LIB} ${FOUNDATION} ${OPENEMUBASE})
 918	install(TARGETS ${BINARY_NAME}-openemu LIBRARY DESTINATION ${OE_LIBDIR} COMPONENT ${BINARY_NAME}.oecoreplugin NAMELINK_SKIP)
 919endif()
 920
 921if(BUILD_SDL)
 922	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/sdl ${CMAKE_CURRENT_BINARY_DIR}/sdl)
 923endif()
 924
 925if(BUILD_QT)
 926	add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/platform/qt ${CMAKE_CURRENT_BINARY_DIR}/qt)
 927endif()
 928
 929if(BUILD_PERF)
 930	set(PERF_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/test/perf-main.c)
 931	if(UNIX AND NOT APPLE)
 932		list(APPEND PERF_LIB rt)
 933	endif()
 934
 935	add_executable(${BINARY_NAME}-perf ${PERF_SRC})
 936	target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB} ${OS_LIB})
 937	set_target_properties(${BINARY_NAME}-perf PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
 938	install(TARGETS ${BINARY_NAME}-perf DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${BINARY_NAME}-perf)
 939	install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/tools/perf.py DESTINATION "${LIBDIR}/${BINARY_NAME}" COMPONENT ${BINARY_NAME}-perf)
 940endif()
 941
 942if(BUILD_TEST)
 943	add_executable(${BINARY_NAME}-fuzz ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/test/fuzz-main.c)
 944	target_link_libraries(${BINARY_NAME}-fuzz ${BINARY_NAME})
 945	set_target_properties(${BINARY_NAME}-fuzz PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
 946	add_executable(tbl-fuzz ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/test/tbl-fuzz-main.c)
 947	target_link_libraries(tbl-fuzz ${BINARY_NAME})
 948	set_target_properties(tbl-fuzz PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
 949	install(TARGETS ${BINARY_NAME}-fuzz tbl-fuzz DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${BINARY_NAME}-test)
 950endif()
 951
 952if(NOT USE_CMOCKA)
 953	set(BUILD_SUITE OFF)
 954endif()
 955if(BUILD_SUITE)
 956	enable_testing()
 957	include_directories(AFTER ${CMOCKA_INCLUDE_DIRS})
 958	link_directories(${CMOCKA_LIBRARY_DIRS})
 959
 960	foreach(TEST IN LISTS TEST_SRC)
 961		string(REPLACE "${CMAKE_SOURCE_DIR}/src/" "" TEST_NAME "${TEST}")
 962		string(REPLACE "/" "-" TEST_NAME "${TEST_NAME}")
 963		string(REPLACE "-test" "" TEST_NAME "${TEST_NAME}")
 964		string(REPLACE ".c" "" TEST_NAME "${TEST_NAME}")
 965		add_executable(test-${TEST_NAME} ${TEST})
 966		target_link_libraries(test-${TEST_NAME} ${BINARY_NAME} ${PLATFORM_LIBRARY} cmocka)
 967		set_target_properties(test-${TEST_NAME} PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
 968		add_test(${TEST_NAME} test-${TEST_NAME})
 969	endforeach()
 970endif()
 971
 972if(BUILD_EXAMPLE)
 973	add_executable(${BINARY_NAME}-example-server ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/example/client-server/server.c)
 974	target_link_libraries(${BINARY_NAME}-example-server ${BINARY_NAME})
 975	set_target_properties(${BINARY_NAME}-example-server PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
 976
 977	if(SDL_FOUND)
 978		add_executable(${BINARY_NAME}-example-client ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/example/client-server/client.c)
 979		target_link_libraries(${BINARY_NAME}-example-client ${BINARY_NAME} ${SDL_LIBRARY} ${SDLMAIN_LIBRARY} ${OPENGL_LIBRARY} ${OPENGLES2_LIBRARY})
 980		set_target_properties(${BINARY_NAME}-example-client PROPERTIES
 981		                      COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}"
 982		                      INCLUDE_DIRECTORIES "${SDL_INCLUDE_DIR};${CMAKE_CURRENT_SOURCE_DIR}/src;${CMAKE_CURRENT_SOURCE_DIR}/include")
 983	endif()
 984endif()
 985
 986file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/mgba)
 987configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/core/flags.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/mgba/flags.h)
 988install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/mgba DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT ${BINARY_NAME}-dev FILES_MATCHING PATTERN "*.h")
 989install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/mgba-util DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT ${BINARY_NAME}-dev FILES_MATCHING PATTERN "*.h")
 990install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/mgba/flags.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mgba COMPONENT ${BINARY_NAME}-dev)
 991
 992if(WIN32)
 993	set(BIN_DIR ".\\")
 994	string(REGEX REPLACE "[^-A-Za-z0-9_.]" "-" CLEAN_VERSION_STRING "${VERSION_STRING}")
 995	file(RELATIVE_PATH SETUP_DIR_SLASH "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/src/platform/windows/setup")
 996	file(RELATIVE_PATH RES_DIR_SLASH "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res")
 997	string(REPLACE "/" "\\" SETUP_DIR "${SETUP_DIR_SLASH}")
 998	string(REPLACE "/" "\\" RES_DIR "${RES_DIR_SLASH}")
 999	if(CMAKE_SYSTEM_PROCESSOR MATCHES ".*64$")
1000		set(WIN_BITS 64)
1001	else()
1002		set(WIN_BITS 32)
1003	endif()
1004	if(GIT_TAG)
1005		set(IS_RELEASE 1)
1006	else()
1007		set(IS_RELEASE 0)
1008	endif()
1009	configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/platform/windows/setup/setup.iss.in" ${CMAKE_CURRENT_BINARY_DIR}/setup.iss)
1010endif()
1011
1012# Packaging
1013install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/licenses/blip_buf.txt DESTINATION ${CMAKE_INSTALL_DOCDIR}/licenses COMPONENT ${BINARY_NAME})
1014install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/licenses/inih.txt DESTINATION ${CMAKE_INSTALL_DOCDIR}/licenses COMPONENT ${BINARY_NAME})
1015if(USE_DISCORD_RPC)
1016	install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/licenses/discord-rpc.txt DESTINATION ${CMAKE_INSTALL_DOCDIR}/licenses COMPONENT ${BINARY_NAME})
1017	install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/licenses/rapidjson.txt DESTINATION ${CMAKE_INSTALL_DOCDIR}/licenses COMPONENT ${BINARY_NAME})
1018	if(WIN32)
1019		install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/licenses/mingw-std-threads.txt DESTINATION ${CMAKE_INSTALL_DOCDIR}/licenses COMPONENT ${BINARY_NAME})
1020	endif()
1021endif()
1022if(EXTRA_LICENSES)
1023	install(FILES ${EXTRA_LICENSES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/licenses COMPONENT ${BINARY_NAME})
1024endif()
1025
1026file(GLOB READMES ${CMAKE_CURRENT_SOURCE_DIR}/README*.md)
1027
1028find_program(UNIX2DOS NAMES unix2dos)
1029find_program(MARKDOWN NAMES markdown kramdown pandoc)
1030
1031if(UNIX OR NOT UNIX2DOS)
1032	if(UNIX OR NOT MARKDOWN)
1033		install(FILES ${READMES} DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT ${BINARY_NAME})
1034	endif()
1035	install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES" "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE" DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT ${BINARY_NAME})
1036else()
1037	add_custom_command(OUTPUT CHANGES.txt COMMAND ${UNIX2DOS} -n "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES" "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.txt" MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES")
1038	add_custom_command(OUTPUT LICENSE.txt COMMAND ${UNIX2DOS} -n "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE" "${CMAKE_CURRENT_BINARY_DIR}/LICENSE.txt" MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
1039	add_custom_target(CHANGES ALL DEPENDS CHANGES.txt)
1040	add_custom_target(LICENSE ALL DEPENDS LICENSE.txt)
1041	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CHANGES.txt ${CMAKE_CURRENT_BINARY_DIR}/LICENSE.txt DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT ${BINARY_NAME})
1042	if(DISTBUILD AND WIN32)
1043		if(INSTALLER_NAME)
1044			set(INSTALLER_TARGET "${INSTALLER_NAME}.exe")
1045			set(ISCC_FLAGS "/F${INSTALLER_NAME}")
1046		else()
1047			set(INSTALLER_TARGET "${PROJECT_NAME}-setup-${CLEAN_VERSION_STRING}-win${WIN_BITS}.exe")
1048		endif()
1049		if(CMAKE_CROSSCOMPILING)
1050			find_program(WINE NAMES wine wine-stable wine-development)
1051			find_file(ISCC ISCC.exe HINTS "$ENV{HOME}/.wine/drive_c/Program Files/" PATH_SUFFIXES "Inno Setup 5")
1052			message(STATUS "${WINE}" "${ISCC}" setup.iss /Q ${ISCC_FLAGS})
1053			add_custom_command(OUTPUT ${INSTALLER_TARGET}
1054			                   COMMAND "${WINE}" "${ISCC}" setup.iss /Q ${ISCC_FLAGS}
1055			                   DEPENDS ${BINARY_NAME}-qt ${BINARY_NAME}-sdl CHANGES LICENSE)
1056		else()
1057			find_program(ISCC NAMES ISCC ISCC.exe PATH_SUFFIXES "Inno Setup 5")
1058			add_custom_command(OUTPUT ${INSTALLER_TARGET}
1059			                   COMMAND "${ISCC}" setup.iss /Q ${ISCC_FLAGS}
1060			                   DEPENDS ${BINARY_NAME}-qt ${BINARY_NAME}-sdl CHANGES LICENSE)
1061		endif()
1062		if(ISCC)
1063			add_custom_target(installer ALL DEPENDS ${INSTALLER_TARGET})
1064			install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${INSTALLER_TARGET}" DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT installer)
1065		endif()
1066	endif()
1067endif()
1068
1069if(MARKDOWN)
1070	foreach(README ${READMES})
1071		get_filename_component(README_BASE "${README}" NAME_WE)
1072		add_custom_command(OUTPUT ${README_BASE}.html COMMAND ${MARKDOWN} "${README}" > ${README_BASE}.html MAIN_DEPENDENCY "${README}")
1073		add_custom_target(${README_BASE} ALL DEPENDS ${README_BASE}.html)
1074		install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${README_BASE}.html DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT ${BINARY_NAME})
1075	endforeach()
1076endif()
1077
1078set(CPACK_PACKAGE_VERSION ${VERSION_STRING})
1079set(CPACK_PACKAGE_VERSION_MAJOR ${LIB_VERSION_MAJOR})
1080set(CPACK_PACKAGE_VERSION_MINOR ${LIB_VERSION_MINOR})
1081set(CPACK_PACKAGE_VERSION_PATCH ${LIB_VERSION_PATCH})
1082set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE)
1083set(CPACK_RESOURCE_FILE_README ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
1084
1085set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "mGBA Game Boy Advance Emulator")
1086set(CPACK_PACKAGE_VENDOR "Jeffrey Pfau")
1087set(CPACK_PACKAGE_CONTACT "Jeffrey Pfau <jeffrey@endrift.com>")
1088set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
1089set(CPACK_DEBIAN_PACKAGE_SECTION "games")
1090
1091set(CPACK_DEB_COMPONENT_INSTALL ON)
1092
1093if(CMAKE_BUILD_TYPE STREQUAL "Release")
1094	set(CPACK_STRIP_FILES ON)
1095endif()
1096
1097if(DISTBUILD)
1098	set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
1099	if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" AND BUILD_SHARED)
1100		if(NOT APPLE)
1101			add_custom_command(TARGET ${BINARY_NAME} POST_BUILD COMMAND "${OBJCOPY}" --only-keep-debug "$<TARGET_FILE:${BINARY_NAME}>" "$<TARGET_FILE:${BINARY_NAME}>.debug")
1102			add_custom_command(TARGET ${BINARY_NAME} POST_BUILD COMMAND "${STRIP}" "$<TARGET_FILE:${BINARY_NAME}>")
1103			add_custom_command(TARGET ${BINARY_NAME} POST_BUILD COMMAND "${OBJCOPY}" --add-gnu-debuglink "$<TARGET_FILE:${BINARY_NAME}>.debug" "$<TARGET_FILE:${BINARY_NAME}>")
1104			install(FILES "$<TARGET_FILE:${BINARY_NAME}>.debug" DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME}-dbg)
1105		endif()
1106	endif()
1107	if(APPLE)
1108		set(CPACK_COMPONENTS_ALL ${BINARY_NAME} ${BINARY_NAME}-qt ${BINARY_NAME}-sdl ${BINARY_NAME}-qt-dbg ${BINARY_NAME}-sdl-dbg ${BINARY_NAME}-perf)
1109		set(CPACK_COMPONENTS_GROUPING ALL_COMPONENTS_IN_ONE)
1110	elseif(WIN32)
1111		set(CPACK_COMPONENTS_ALL ${BINARY_NAME} ${BINARY_NAME}-qt ${BINARY_NAME}-sdl ${BINARY_NAME}-qt-dbg ${BINARY_NAME}-sdl-dbg ${BINARY_NAME}-perf installer)
1112	elseif(3DS)
1113		set(CPACK_COMPONENTS_ALL ${BINARY_NAME} ${BINARY_NAME}-dbg ${BINARY_NAME}-3ds ${BINARY_NAME}-perf)
1114	elseif(WII)
1115		set(CPACK_COMPONENTS_ALL ${BINARY_NAME} ${BINARY_NAME}-dbg ${BINARY_NAME}-wii)
1116	elseif(PSP2)
1117		set(CPACK_COMPONENTS_ALL ${BINARY_NAME} ${BINARY_NAME}-dbg ${BINARY_NAME}-psp2)
1118	elseif(SWITCH)
1119		set(CPACK_COMPONENTS_ALL ${BINARY_NAME} ${BINARY_NAME}-dbg ${BINARY_NAME}-switch)
1120	endif()
1121endif()
1122
1123include(CPack)
1124
1125cpack_add_component_group(base)
1126cpack_add_component(${BINARY_NAME} GROUP base)
1127
1128cpack_add_component_group(dev PARENT_GROUP base)
1129if(BUILD_SHARED)
1130	cpack_add_component(lib${BINARY_NAME} GROUP base)
1131	if(BUILD_STATIC)
1132		cpack_add_component(lib${BINARY_NAME}-static GROUP dev)
1133	endif()
1134elseif(BUILD_STATIC)
1135	cpack_add_component(lib${BINARY_NAME} GROUP dev)
1136endif()
1137cpack_add_component(${BINARY_NAME}-dev GROUP dev)
1138
1139if(3DS)
1140	cpack_add_component(${BINARY_NAME}-3ds GROUP base)
1141elseif(PSP2)
1142	cpack_add_component(${BINARY_NAME}-psp2 GROUP base)
1143elseif(WII)
1144	cpack_add_component(${BINARY_NAME}-wii GROUP base)
1145elseif(SWITCH)
1146	cpack_add_component(${BINARY_NAME}-switch GROUP base)
1147endif()
1148
1149if(BUILD_QT)
1150	cpack_add_component_group(qt PARENT_GROUP base)
1151	cpack_add_component(${BINARY_NAME}-qt GROUP qt)
1152endif()
1153
1154if(SDL_FOUND)
1155	cpack_add_component_group(sdl PARENT_GROUP base)
1156	cpack_add_component(${BINARY_NAME}-sdl GROUP sdl)
1157endif()
1158
1159if(DISTBUILD)
1160	cpack_add_component_group(debug PARENT_GROUP dev)
1161	if(BUILD_SHARED AND NOT IS_EMBEDDED)
1162		cpack_add_component(lib${BINARY_NAME}-dbg GROUP debug)
1163	endif()
1164	if(IS_EMBEDDED)
1165		cpack_add_component(${BINARY_NAME}-dbg GROUP debug)
1166	endif()
1167	if(BUILD_QT)
1168		cpack_add_component(${BINARY_NAME}-qt-dbg GROUP debug)
1169	endif()
1170	if(SDL_FOUND)
1171		cpack_add_component(${BINARY_NAME}-sdl-dbg GROUP debug)
1172	endif()
1173	if(WIN32)
1174		cpack_add_component_group(installer PARENT_GROUP base)
1175	endif()
1176endif()
1177
1178cpack_add_component_group(test PARENT_GROUP dev)
1179cpack_add_component(${BINARY_NAME}-perf GROUP test)
1180cpack_add_component(${BINARY_NAME}-fuzz GROUP test)
1181cpack_add_component(tbl-fuzz GROUP test)
1182
1183# Summaries
1184set(SUMMARY_GL_LIST)
1185if(USE_EPOXY)
1186	set(SUMMARY_GL_LIST "libepoxy")
1187else()
1188	if(BUILD_GL)
1189		list(APPEND SUMMARY_GL_LIST "OpenGL")
1190	endif()
1191	if(BUILD_GLES2)
1192		list(APPEND SUMMARY_GL_LIST "OpenGL|ES 2")
1193	endif()
1194	if(BUILD_GLES3)
1195		list(APPEND SUMMARY_GL_LIST "OpenGL|ES 3")
1196	endif()
1197endif()
1198if(NOT SUMMARY_GL_LIST)
1199	set(SUMMARY_GL OFF)
1200else()
1201	string(REPLACE ";" ", " SUMMARY_GL "${SUMMARY_GL_LIST}")
1202endif()
1203if(USE_LIBZIP)
1204	set(SUMMARY_ZIP libzip)
1205elseif(USE_MINIZIP)
1206	set(SUMMARY_ZIP "minizip (external)")
1207elseif(USE_ZLIB)
1208	set(SUMMARY_ZIP "minizip (included)")
1209else()
1210	set(SUMMARY_ZIP OFF)
1211endif()
1212
1213if(NOT QUIET AND NOT LIBMGBA_ONLY)
1214	message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
1215	message(STATUS "Platforms:")
1216	message(STATUS "	Game Boy Advance: ${M_CORE_GBA}")
1217	message(STATUS "	Game Boy: ${M_CORE_GB}")
1218	message(STATUS "Features:")
1219	message(STATUS "	Debuggers: ${USE_DEBUGGERS}")
1220	if(NOT WIN32)
1221		message(STATUS "	CLI debugger: ${USE_EDITLINE}")
1222	endif()
1223	message(STATUS "	GDB stub: ${USE_GDB_STUB}")
1224	message(STATUS "	GIF/Video recording: ${USE_FFMPEG}")
1225	message(STATUS "	Screenshot/advanced savestate support: ${USE_PNG}")
1226	message(STATUS "	ZIP support: ${SUMMARY_ZIP}")
1227	message(STATUS "	7-Zip support: ${USE_LZMA}")
1228	message(STATUS "	SQLite3 game database: ${USE_SQLITE3}")
1229	message(STATUS "	ELF loading support: ${USE_ELF}")
1230	message(STATUS "	Discord Rich Presence support: ${USE_DISCORD_RPC}")
1231	message(STATUS "	OpenGL support: ${SUMMARY_GL}")
1232	message(STATUS "Frontends:")
1233	message(STATUS "	Qt: ${BUILD_QT}")
1234	message(STATUS "	SDL (${SDL_VERSION}): ${BUILD_SDL}")
1235	message(STATUS "	Profiling: ${BUILD_PERF}")
1236	message(STATUS "	Test harness: ${BUILD_TEST}")
1237	message(STATUS "	Test suite: ${BUILD_SUITE}")
1238	message(STATUS "	Python bindings: ${BUILD_PYTHON}")
1239	message(STATUS "	Examples: ${BUILD_EXAMPLE}")
1240	message(STATUS "Cores:")
1241	message(STATUS "	Libretro core: ${BUILD_LIBRETRO}")
1242	if(APPLE)
1243		message(STATUS "	OpenEmu core: ${BUILD_OPENEMU}")
1244	endif()
1245	message(STATUS "Libraries:")
1246	message(STATUS "	Static: ${BUILD_STATIC}")
1247	message(STATUS "	Shared: ${BUILD_SHARED}")
1248endif()