all repos — mgba @ 03163b8497f27d99dd11a86d35fb4231797878f4

mGBA Game Boy Advance Emulator

CMakeLists.txt (view raw)

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