all repos — mgba @ 03c17cdc5637d3609b08caaa2c2c416fee9f9cbc

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