all repos — mgba @ d7900fdf5fdaa9d432a79ac22dbd1bdde6ce2c5d

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