all repos — mgba @ 23a346e8ce24b26b1a5bd980a744bf87f56b99a4

mGBA Game Boy Advance Emulator

CMakeLists.txt (view raw)

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