all repos — mgba @ 0.6.2

mGBA Game Boy Advance Emulator

CMakeLists.txt (view raw)

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