all repos — mgba @ dc753d8117161f819f392ad6a667e8800a1e9003

mGBA Game Boy Advance Emulator

CMakeLists.txt (view raw)

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