all repos — mgba @ 6a3e1f49956f32c3e55ca93132bd44b4c66e7079

mGBA Game Boy Advance Emulator

CMakeLists.txt (view raw)

  1cmake_minimum_required(VERSION 2.6)
  2project(mGBA C)
  3set(BINARY_NAME mgba CACHE INTERNAL "Name of output binaries")
  4if(NOT MSVC)
  5	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99")
  6else()
  7	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /wd4003 /wd4244 /wd4146")
  8endif()
  9set(USE_CLI_DEBUGGER ON CACHE BOOL "Whether or not to enable the CLI-mode ARM debugger")
 10set(USE_GDB_STUB ON CACHE BOOL "Whether or not to enable the GDB stub ARM debugger")
 11set(USE_FFMPEG ON CACHE BOOL "Whether or not to enable FFmpeg support")
 12set(USE_ZLIB ON CACHE BOOL "Whether or not to enable zlib support")
 13set(USE_PNG ON CACHE BOOL "Whether or not to enable PNG support")
 14set(USE_LIBZIP ON CACHE BOOL "Whether or not to enable ZIP support")
 15set(USE_MAGICK ON CACHE BOOL "Whether or not to enable ImageMagick support")
 16set(USE_BLIP ON CACHE BOOL "Whether or not to enable blip_buf support")
 17set(USE_LZMA ON CACHE BOOL "Whether or not to enable 7-Zip support")
 18set(BUILD_QT ON CACHE BOOL "Build Qt frontend")
 19set(BUILD_SDL ON CACHE BOOL "Build SDL frontend")
 20set(BUILD_LIBRETRO OFF CACHE BOOL "Build libretro core")
 21set(BUILD_PERF OFF CACHE BOOL "Build performance profiling tool")
 22set(BUILD_TEST OFF CACHE BOOL "Build testing harness")
 23set(BUILD_STATIC OFF CACHE BOOL "Build a static library")
 24set(BUILD_SHARED ON CACHE BOOL "Build a shared library")
 25set(BUILD_GL ON CACHE STRING "Build with OpenGL")
 26set(BUILD_GLES2 OFF CACHE STRING "Build with OpenGL|ES 2")
 27set(DISABLE_DEPS OFF CACHE BOOL "Build without dependencies")
 28file(GLOB ARM_SRC ${CMAKE_SOURCE_DIR}/src/arm/*.c)
 29file(GLOB GBA_SRC ${CMAKE_SOURCE_DIR}/src/gba/*.c)
 30file(GLOB GBA_CHEATS_SRC ${CMAKE_SOURCE_DIR}/src/gba/cheats/*.c)
 31file(GLOB GBA_RR_SRC ${CMAKE_SOURCE_DIR}/src/gba/rr/*.c)
 32file(GLOB GBA_SV_SRC ${CMAKE_SOURCE_DIR}/src/gba/supervisor/*.c)
 33file(GLOB GBA_CTX_SRC ${CMAKE_SOURCE_DIR}/src/gba/context/*.c)
 34file(GLOB UTIL_SRC ${CMAKE_SOURCE_DIR}/src/util/*.[cSs])
 35file(GLOB GUI_SRC ${CMAKE_SOURCE_DIR}/src/util/gui/*.c ${CMAKE_SOURCE_DIR}/src/gba/gui/*.c)
 36file(GLOB RENDERER_SRC ${CMAKE_SOURCE_DIR}/src/gba/renderers/*.c)
 37file(GLOB SIO_SRC ${CMAKE_SOURCE_DIR}/src/gba/sio/lockstep.c)
 38file(GLOB THIRD_PARTY_SRC ${CMAKE_SOURCE_DIR}/src/third-party/inih/*.c)
 39list(APPEND UTIL_SRC ${CMAKE_SOURCE_DIR}/src/platform/commandline.c)
 40set(VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-mem.c)
 41source_group("ARM core" FILES ${ARM_SRC})
 42source_group("GBA board" FILES ${GBA_SRC} ${RENDERER_SRC} ${SIO_SRC})
 43source_group("GBA extra" FILES ${GBA_CHEATS_SRC} ${GBA_CTX_SRC} ${GBA_SV_SRC} ${GBA_RR_SRC})
 44source_group("Utilities" FILES ${UTIL_SRC})
 45include_directories(${CMAKE_SOURCE_DIR}/src/arm)
 46include_directories(${CMAKE_SOURCE_DIR}/src)
 47
 48if(NOT CMAKE_BUILD_TYPE)
 49	set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type (e.g. Release or Debug)" FORCE)
 50endif()
 51
 52set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIBDIR}")
 53
 54include(GNUInstallDirs)
 55
 56if (NOT DEFINED LIBDIR)
 57	set(LIBDIR "lib")
 58endif()
 59
 60if (NOT DEFINED MANDIR)
 61	set(MANDIR ${CMAKE_INSTALL_MANDIR})
 62endif()
 63
 64# Function definitions
 65include(FindPkgConfig)
 66function(find_feature FEATURE_NAME FEATURE_REQUIRES)
 67	if (NOT ${FEATURE_NAME})
 68		return()
 69	endif()
 70	if (DISABLE_DEPS)
 71		set(${FEATURE_NAME} OFF PARENT_SCOPE)
 72		return()
 73	endif()
 74	foreach(REQUIRE ${FEATURE_REQUIRES})
 75		if(NOT ${REQUIRE}_FOUND)
 76			find_package(${REQUIRE} QUIET)
 77			if(NOT ${REQUIRE}_FOUND)
 78				pkg_search_module(${REQUIRE} ${REQUIRE})
 79				if (NOT ${REQUIRE}_FOUND)
 80					message(WARNING "Requested module ${REQUIRE} missing for feature ${FEATURE_NAME}. Feature disabled.")
 81					set(${FEATURE_NAME} OFF PARENT_SCOPE)
 82					return()
 83				endif()
 84			endif()
 85		endif()
 86		string(TOUPPER ${REQUIRE} UREQUIRE)
 87		set(${UREQUIRE}_CFLAGS_OTHER ${${REQUIRE}_CFLAGS_OTHER} PARENT_SCOPE)
 88		set(${UREQUIRE}_FOUND ${${REQUIRE}_FOUND} PARENT_SCOPE)
 89		set(${UREQUIRE}_INCLUDE_DIRS ${${REQUIRE}_INCLUDE_DIRS} PARENT_SCOPE)
 90		set(${UREQUIRE}_LIBRARIES ${${REQUIRE}_LIBRARIES} PARENT_SCOPE)
 91		set(${UREQUIRE}_LIBRARY_DIRS ${${REQUIRE}_LIBRARY_DIRS} PARENT_SCOPE)
 92		set(${UREQUIRE}_LDFLAGS_OTHER ${${REQUIRE}_LDFLAGS_OTHER} PARENT_SCOPE)
 93	endforeach()
 94endfunction()
 95
 96# Version information
 97add_custom_target(version-info ALL ${CMAKE_COMMAND} -E touch ${CMAKE_SOURCE_DIR}/src/util/version.c.in
 98	COMMAND ${CMAKE_COMMAND}
 99	-DBINARY_NAME=${BINARY_NAME}
100	-DCONFIG_FILE=${CMAKE_SOURCE_DIR}/src/util/version.c.in
101	-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/version.c
102	-P ${CMAKE_SOURCE_DIR}/version.cmake
103	WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
104
105include(${CMAKE_SOURCE_DIR}/version.cmake)
106configure_file(${CMAKE_SOURCE_DIR}/src/util/version.c.in ${CMAKE_CURRENT_BINARY_DIR}/version.c)
107list(APPEND UTIL_SRC ${CMAKE_BINARY_DIR}/version.c)
108source_group("Generated sources" FILES ${CMAKE_BINARY_DIR}/version.c)
109
110# Advanced settings
111set(BUILD_LTO ON CACHE BOOL "Build with link-time optimization")
112set(BUILD_PGO OFF CACHE BOOL "Build with profiling-guided optimization")
113set(PGO_STAGE_2 CACHE BOOL "Rebuild for profiling-guided optimization after profiles have been generated")
114set(PGO_DIR "/tmp/gba-pgo/" CACHE PATH "Profiling-guided optimization profiles path")
115mark_as_advanced(BUILD_LTO BUILD_PGO PGO_STAGE_2 PGO_DIR)
116set(PGO_PRE_FLAGS "-pg -fprofile-generate=${PGO_DIR} -fprofile-arcs")
117set(PGO_POST_FLAGS "-fprofile-use=${PGO_DIR} -fbranch-probabilities")
118
119if(BUILD_PGO AND NOT PGO_STAGE_2)
120	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_PRE_FLAGS}")
121	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
122	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
123elseif(BUILD_PGO AND PGO_STAGE_2)
124	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_POST_FLAGS}")
125	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_POST_FLAGS}")
126	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_POST_FLAGS}")
127endif()
128
129# Platform support
130if(WIN32)
131	set(WIN32_VERSION "${LIB_VERSION_MAJOR},${LIB_VERSION_MINOR},${LIB_VERSION_PATCH}")
132	add_definitions(-D_WIN32_WINNT=0x0600)
133	list(APPEND OS_LIB ws2_32 shlwapi)
134	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
135	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/windows/*.c)
136	source_group("Windows-specific code" FILES ${OS_SRC})
137elseif(UNIX)
138	add_definitions(-DUSE_PTHREADS)
139
140	if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
141		add_definitions(-D_GNU_SOURCE)
142	endif()
143	if(NOT APPLE AND NOT HAIKU)
144		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
145	endif()
146
147	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
148	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/posix/*.c)
149	source_group("POSIX-specific code" FILES ${OS_SRC})
150elseif(WII)
151	set(M_LIBRARY m)
152	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-file.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
153	add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5 -DUSE_VFS_FILE)
154	include_directories(${CMAKE_BINARY_DIR})
155	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/wii/*.c)
156	list(APPEND OS_LIB wiiuse bte fat ogc)
157	source_group("Wii-specific code" FILES ${OS_SRC})
158elseif(3DS)
159	set(M_LIBRARY m)
160	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
161	add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
162	if (${CMAKE_SOURCE_DIR}/src/platform/3ds/font.raw IS_NEWER_THAN ${CMAKE_BINARY_DIR}/font.c)
163		execute_process(COMMAND ${RAW2C} ${CMAKE_SOURCE_DIR}/src/platform/3ds/font.raw)
164	endif()
165	include_directories(${CMAKE_BINARY_DIR})
166	list(APPEND OS_LIB sf2d ctru)
167	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/3ds/*.c ${CMAKE_BINARY_DIR}/font.c)
168	set(USE_VFS_3DS ON)
169	if(USE_VFS_3DS)
170		add_definitions(-DUSE_VFS_3DS)
171	else()
172		add_definitions(-DUSE_VFS_FILE)
173		list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-file.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
174	endif()
175	source_group("3DS-specific code" FILES ${OS_SRC})
176endif()
177
178if(APPLE)
179	add_definitions(-D_DARWIN_C_SOURCE)
180	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6")
181endif()
182
183if(NOT HAIKU AND NOT MSVC AND NOT PSP2)
184	list(APPEND OS_LIB m)
185endif()
186
187if(APPLE OR CMAKE_C_COMPILER_ID STREQUAL "GNU" AND BUILD_LTO)
188	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto")
189	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
190endif()
191
192if(BUILD_BBB OR BUILD_RASPI OR BUILD_PANDORA)
193	if(NOT BUILD_EGL)
194		add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
195	endif()
196endif()
197
198if(WII)
199	add_definitions(-U__STRICT_ANSI__)
200	if (${CMAKE_SOURCE_DIR}/src/platform/wii/font.tpl IS_NEWER_THAN ${CMAKE_BINARY_DIR}/font.c)
201		execute_process(COMMAND ${RAW2C} ${CMAKE_SOURCE_DIR}/src/platform/wii/font.tpl OUTPUT_QUIET ERROR_QUIET)
202	endif()
203endif()
204
205if(BUILD_RASPI)
206	set(BUILD_GL OFF CACHE BOOL "OpenGL not supported" FORCE)
207endif()
208
209if(BUILD_PANDORA)
210	add_definitions(-DBUILD_PANDORA)
211endif()
212
213if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm.*")
214	enable_language(ASM)
215endif()
216
217if(PSP2)
218	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
219endif()
220
221include(CheckFunctionExists)
222check_function_exists(strdup HAVE_STRDUP)
223check_function_exists(strndup HAVE_STRNDUP)
224if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
225	check_function_exists(snprintf_l HAVE_SNPRINTF_L)
226	if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
227		# The strtof_l on Linux not actually exposed nor actually strtof_l
228		set(HAVE_STRTOF_L OFF)
229	else()
230		check_function_exists(strtof_l HAVE_STRTOF_L)
231	endif()
232	check_function_exists(newlocale HAVE_NEWLOCALE)
233	check_function_exists(freelocale HAVE_FREELOCALE)
234	check_function_exists(uselocale HAVE_USELOCALE)
235	check_function_exists(setlocale HAVE_SETLOCALE)
236else()
237	set(DISABLE_DEPS ON CACHE BOOL "This platform cannot build with dependencies" FORCE)
238	set(DISABLE_FRONTENDS ON)
239	set(MINIMAL_CORE ON)
240endif()
241
242if(HAVE_STRDUP)
243	add_definitions(-DHAVE_STRDUP)
244endif()
245
246if(HAVE_STRNDUP)
247	add_definitions(-DHAVE_STRNDUP)
248endif()
249
250if(HAVE_NEWLOCALE AND HAVE_FREELOCALE AND HAVE_USELOCALE)
251	add_definitions(-DHAVE_LOCALE)
252	if (HAVE_STRTOF_L)
253		add_definitions(-DHAVE_STRTOF_L)
254	endif()
255	if (HAVE_SNPRINTF_L)
256		add_definitions(-DHAVE_SNPRINTF_L)
257	endif()
258endif()
259
260if(HAVE_SETLOCALE)
261	add_definitions(-DHAVE_SETLOCALE)
262endif()
263
264if(DISABLE_DEPS)
265endif()
266
267# Feature dependencies
268set(FEATURES)
269if(CMAKE_SYSTEM_NAME MATCHES .*BSD)
270	set(LIBEDIT_LIBRARIES -ledit)
271	if (CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
272		list(APPEND LIBEDIT_LIBRARIES -ltermcap)
273	endif()
274else()
275	find_feature(USE_CLI_DEBUGGER "libedit")
276endif()
277if(BUILD_GL)
278	find_package(OpenGL QUIET)
279	if(NOT OPENGL_FOUND)
280		set(BUILD_GL OFF CACHE BOOL "OpenGL not found" FORCE)
281	endif()
282endif()
283if(BUILD_GLES2 AND NOT BUILD_RASPI)
284	find_path(OPENGLES2_INCLUDE_DIR NAMES GLES2/gl2.h)
285	find_library(OPENGLES2_LIBRARY NAMES GLESv2 GLESv2_CM)
286	if(NOT OPENGLES2_INCLUDE_DIR OR NOT OPENGLES2_LIBRARY)
287		set(BUILD_GLES2 OFF CACHE BOOL "OpenGL|ES 2 not found" FORCE)
288	endif()
289endif()
290set(WANT_ZLIB ${USE_ZLIB})
291set(WANT_PNG ${USE_PNG})
292find_feature(USE_FFMPEG "libavcodec;libavformat;libavresample;libavutil;libswscale")
293if(NOT PSP2)
294	find_feature(USE_ZLIB "ZLIB")
295	find_feature(USE_PNG "PNG")
296endif()
297find_feature(USE_LIBZIP "libzip")
298find_feature(USE_MAGICK "MagickWand")
299
300# Features
301set(DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/debugger.c ${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c)
302set(FEATURE_SRC)
303set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
304
305if(DISABLE_DEPS)
306	set(USE_LZMA OFF)
307	set(USE_GDB_STUB OFF)
308endif()
309
310if(USE_CLI_DEBUGGER)
311	list(APPEND FEATURES CLI_DEBUGGER)
312	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c)
313	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/debugger/parser.c)
314	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/gba/supervisor/cli.c)
315	include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
316	link_directories(${LIBEDIT_LIBRARY_DIRS})
317	set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
318	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libedit2")
319else()
320	set(DEBUGGER_LIB "")
321endif()
322
323if(USE_GDB_STUB)
324	list(APPEND FEATURES GDB_STUB)
325	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/gdb-stub.c)
326endif()
327source_group("ARM debugger" FILES ${DEBUGGER_SRC})
328
329if(USE_FFMPEG)
330	list(APPEND FEATURES FFMPEG)
331	pkg_search_module(LIBSWRESAMPLE QUIET libswresample)
332	if(NOT LIBSWRESAMPLE_FOUND)
333		list(APPEND FEATURES LIBAV)
334	endif()
335	include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVRESAMPLE_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS} ${LIBSWSCALE_INCLUDE_DIRS})
336	link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVRESAMPLE_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS} ${LIBSWSCALE_LIBRARY_DIRS})
337	list(APPEND FEATURE_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-encoder.c")
338	string(REGEX MATCH "^[0-9]+" LIBAVCODEC_VERSION_MAJOR ${libavcodec_VERSION})
339	string(REGEX MATCH "^[0-9]+" LIBAVFORMAT_VERSION_MAJOR ${libavformat_VERSION})
340	string(REGEX MATCH "^[0-9]+" LIBAVRESAMPLE_VERSION_MAJOR ${libavresample_VERSION})
341	string(REGEX MATCH "^[0-9]+" LIBAVUTIL_VERSION_MAJOR ${libavutil_VERSION})
342	string(REGEX MATCH "^[0-9]+" LIBSWSCALE_VERSION_MAJOR ${libswscale_VERSION})
343	list(APPEND DEPENDENCY_LIB ${LIBAVCODEC_LIBRARIES} ${LIBAVFORMAT_LIBRARIES} ${LIBAVRESAMPLE_LIBRARIES} ${LIBAVUTIL_LIBRARIES} ${LIBSWSCALE_LIBRARIES})
344	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavcodec${LIBAVCODEC_VERSION_MAJOR}|libavcodec-extra-${LIBAVCODEC_VERSION_MAJOR},libavformat${LIBAVFORMAT_VERSION_MAJOR},libavresample${LIBAVRESAMPLE_VERSION_MAJOR},libavutil${LIBAVUTIL_VERSION_MAJOR},libswscale${LIBSWSCALE_VERSION_MAJOR}")
345	set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libavcodec-extra")
346	if(APPLE)
347		set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
348		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
349	endif()
350endif()
351
352if(USE_BLIP)
353	list(APPEND THIRD_PARTY_SRC "${CMAKE_SOURCE_DIR}/src/third-party/blip_buf/blip_buf.c")
354	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_BLIP_BUF)
355else()
356	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_NN)
357endif()
358
359if(USE_MAGICK)
360	list(APPEND FEATURES MAGICK)
361	include_directories(AFTER ${MAGICKWAND_INCLUDE_DIRS})
362	link_directories(${MAGICKWAND_LIBRARY_DIRS})
363	list(APPEND FEATURE_SRC "${CMAKE_SOURCE_DIR}/src/platform/imagemagick/imagemagick-gif-encoder.c")
364	list(APPEND DEPENDENCY_LIB ${MAGICKWAND_LIBRARIES})
365	string(REGEX MATCH "^[0-9]+\\.[0-9]+" MAGICKWAND_VERSION_PARTIAL ${MagickWand_VERSION})
366	if(${MAGICKWAND_VERSION_PARTIAL} EQUAL "6.7")
367		set(MAGICKWAND_DEB_VERSION "5")
368	else()
369		set(MAGICKWAND_DEB_VERSION "-6.q16-2")
370	endif()
371	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libmagickwand${MAGICKWAND_DEB_VERSION}")
372endif()
373
374if(WANT_ZLIB AND NOT USE_ZLIB)
375	set(CMAKE_C_FLAGS_RELEASE_OLD "${CMAKE_C_FLAGS_RELEASE}")
376	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fno-lto")
377	add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party/zlib zlib)
378	set_property(TARGET zlibstatic PROPERTY INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/zlib;${CMAKE_SOURCE_DIR}/src/third-party/zlib)
379	set_property(TARGET zlib PROPERTY EXCLUDE_FROM_ALL ON)
380	set_property(TARGET example PROPERTY EXCLUDE_FROM_ALL ON)
381	set_property(TARGET minigzip PROPERTY EXCLUDE_FROM_ALL ON)
382	set(ZLIB_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/third-party/zlib ${CMAKE_BINARY_DIR}/zlib)
383	set(ZLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/src/third-party/zlib ${CMAKE_BINARY_DIR}/zlib)
384	set(ZLIB_LIBRARY zlibstatic)
385	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_OLD}")
386	list(APPEND DEPENDENCY_LIB zlibstatic)
387	set(USE_ZLIB ON)
388endif()
389
390if(USE_ZLIB)
391	list(APPEND FEATURES ZLIB)
392	include_directories(AFTER ${ZLIB_INCLUDE_DIRS})
393	list(APPEND DEPENDENCY_LIB ${ZLIB_LIBRARIES})
394	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},zlib1g")
395endif()
396
397if(WANT_PNG AND USE_ZLIB AND NOT USE_PNG)
398	set(PNG_STATIC ON CACHE BOOL "" FORCE)
399	set(PNG_SHARED OFF CACHE BOOL "" FORCE)
400	set(PNG_TESTS OFF CACHE BOOL "" FORCE)
401	set(SKIP_INSTALL_ALL ON)
402	set(CMAKE_C_FLAGS_RELEASE_OLD "${CMAKE_C_FLAGS_RELEASE}")
403	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fno-lto")
404	add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party/libpng libpng)
405	set_property(TARGET png16_static PROPERTY INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/libpng;${CMAKE_SOURCE_DIR}/src/third-party/libpng;${ZLIB_INCLUDE_DIRS})
406	set(PNG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/third-party/libpng ${CMAKE_BINARY_DIR}/libpng)
407	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_OLD}")
408	list(APPEND DEPENDENCY_LIB png16_static)
409	set(USE_PNG ON)
410endif()
411
412if(USE_PNG)
413	list(APPEND FEATURES PNG)
414	if(NOT PSP2)
415		include_directories(AFTER ${PNG_INCLUDE_DIRS})
416		list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
417		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpng12-0")
418	endif()
419endif()
420
421if(USE_LIBZIP)
422	include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
423	link_directories(${LIBZIP_LIBRARY_DIRS})
424	list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
425	list(APPEND FEATURES LIBZIP)
426	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-zip.c)
427	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip2")
428endif()
429
430if (USE_LZMA)
431	include_directories(AFTER ${CMAKE_SOURCE_DIR}/third-party/lzma)
432	add_definitions(-D_7ZIP_PPMD_SUPPPORT)
433	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-lzma.c)
434	set(LZMA_SRC
435		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zAlloc.c
436		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zArcIn.c
437		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf.c
438		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf2.c
439		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrc.c
440		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrcOpt.c
441		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zDec.c
442		${CMAKE_SOURCE_DIR}/src/third-party/lzma/CpuArch.c
443		${CMAKE_SOURCE_DIR}/src/third-party/lzma/LzmaDec.c
444		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Lzma2Dec.c
445		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra.c
446		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra86.c
447		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bcj2.c
448		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7.c
449		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7Dec.c
450		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zFile.c
451		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zStream.c)
452	list(APPEND FEATURE_SRC ${LZMA_SRC})
453	list(APPEND FEATURES LZMA)
454endif()
455
456set(FEATURE_DEFINES)
457foreach(FEATURE IN LISTS FEATURES)
458	list(APPEND FEATURE_DEFINES "USE_${FEATURE}")
459endforeach()
460
461source_group("Virtual files" FILES ${VFS_SRC})
462source_group("Extra features" FILES ${FEATURE_SRC})
463source_group("Third-party code" FILES ${THIRD_PARTY_SRC})
464
465# Binaries
466set(CORE_SRC
467	${ARM_SRC}
468	${GBA_SRC}
469	${GBA_CHEATS_SRC}
470	${GBA_CTX_SRC}
471	${DEBUGGER_SRC}
472	${RENDERER_SRC}
473	${UTIL_SRC}
474	${VFS_SRC}
475	${OS_SRC}
476	${THIRD_PARTY_SRC})
477
478if(NOT MINIMAL_CORE)
479	set(SRC
480		${CORE_SRC}
481		${GBA_RR_SRC}
482		${GBA_SV_SRC}
483		${SIO_SRC}
484		${FEATURE_SRC})
485else()
486	set(SRC ${CORE_SRC})
487endif()
488
489if(NOT BUILD_STATIC AND NOT BUILD_SHARED)
490	set(BUILD_SHARED ON)
491endif()
492
493if(BUILD_SHARED)
494	add_library(${BINARY_NAME} SHARED ${SRC})
495	set_target_properties(${BINARY_NAME} PROPERTIES SOVERSION ${LIB_VERSION_ABI})
496	if(BUILD_STATIC)
497		add_library(${BINARY_NAME}-static STATIC ${SRC})
498		set_target_properties(${BINARY_NAME}-static PROPERTIES COMPILE_DEFINITIONS "${FEATURE_DEFINES}")
499		install(TARGETS ${BINARY_NAME}-static DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
500		add_dependencies(${BINARY_NAME}-static version-info)
501	endif()
502else()
503	add_library(${BINARY_NAME} STATIC ${SRC})
504endif()
505
506add_dependencies(${BINARY_NAME} version-info)
507
508target_link_libraries(${BINARY_NAME} ${DEBUGGER_LIB} ${DEPENDENCY_LIB} ${OS_LIB})
509install(TARGETS ${BINARY_NAME} LIBRARY DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME} NAMELINK_SKIP ARCHIVE DESTINATION ${LIBDIR} RUNTIME DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
510if(UNIX AND NOT APPLE)
511	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-16.png DESTINATION share/icons/hicolor/16x16/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
512	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-24.png DESTINATION share/icons/hicolor/24x24/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
513	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-32.png DESTINATION share/icons/hicolor/32x32/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
514	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-48.png DESTINATION share/icons/hicolor/48x48/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
515	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-64.png DESTINATION share/icons/hicolor/64x64/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
516	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-96.png DESTINATION share/icons/hicolor/96x96/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
517	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-128.png DESTINATION share/icons/hicolor/128x128/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
518	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-256.png DESTINATION share/icons/hicolor/256x256/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
519	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-512.png DESTINATION share/icons/hicolor/512x512/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
520endif()
521set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI} COMPILE_DEFINITIONS "${FEATURE_DEFINES}")
522
523if(BUILD_GL)
524	add_definitions(-DBUILD_GL)
525endif()
526
527if(BUILD_GLES2)
528	add_definitions(-DBUILD_GLES2)
529endif()
530
531if(DISABLE_FRONTENDS)
532	set(BUILD_SDL OFF)
533	set(BUILD_QT OFF)
534endif()
535
536if(BUILD_LIBRETRO)
537	file(GLOB RETRO_SRC ${CMAKE_SOURCE_DIR}/src/platform/libretro/*.c)
538	add_library(${BINARY_NAME}_libretro SHARED ${CORE_SRC} ${RETRO_SRC})
539	set_target_properties(${BINARY_NAME}_libretro PROPERTIES PREFIX "" COMPILE_DEFINITIONS "COLOR_16_BIT;COLOR_5_6_5;DISABLE_THREADING")
540	target_link_libraries(${BINARY_NAME}_libretro ${OS_LIB})
541endif()
542
543if(BUILD_SDL)
544	add_definitions(-DBUILD_SDL)
545	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
546endif()
547
548if(BUILD_QT)
549	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/qt ${CMAKE_BINARY_DIR}/qt)
550endif()
551
552if(WII)
553	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/wii ${CMAKE_BINARY_DIR})
554endif()
555
556if(PSP2)
557	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/psp2 ${CMAKE_BINARY_DIR})
558endif()
559
560if(3DS)
561	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/3ds ${CMAKE_BINARY_DIR})
562endif()
563
564if(BUILD_PERF)
565	set(PERF_SRC ${CMAKE_SOURCE_DIR}/src/platform/test/perf-main.c)
566	if(UNIX AND NOT APPLE)
567		list(APPEND PERF_LIB rt)
568	endif()
569
570	add_executable(${BINARY_NAME}-perf ${PERF_SRC})
571	target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB})
572	install(TARGETS ${BINARY_NAME}-perf DESTINATION bin COMPONENT ${BINARY_NAME}-perf)
573	install(FILES ${CMAKE_SOURCE_DIR}/tools/perf.py DESTINATION "${CMAKE_INSTALL_LIBDIR}/${BINARY_NAME}" COMPONENT ${BINARY_NAME}-perf)
574endif()
575
576if(BUILD_TEST)
577	add_executable(${BINARY_NAME}-fuzz ${CMAKE_SOURCE_DIR}/src/platform/test/fuzz-main.c)
578	target_link_libraries(${BINARY_NAME}-fuzz ${BINARY_NAME})
579	install(TARGETS ${BINARY_NAME}-fuzz DESTINATION bin COMPONENT ${BINARY_NAME}-test)
580endif()
581
582# Packaging
583set(CPACK_PACKAGE_VERSION ${VERSION_STRING})
584set(CPACK_PACKAGE_VERSION_MAJOR ${LIB_VERSION_MAJOR})
585set(CPACK_PACKAGE_VERSION_MINOR ${LIB_VERSION_MINOR})
586set(CPACK_PACKAGE_VERSION_PATCH ${LIB_VERSION_PATCH})
587set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
588set(CPACK_RESOURCE_FILE_README ${CMAKE_SOURCE_DIR}/README.md)
589
590set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "mGBA Game Boy Advance Emulator")
591set(CPACK_PACKAGE_VENDOR "Jeffrey Pfau")
592set(CPACK_PACKAGE_CONTACT "Jeffrey Pfau <jeffrey@endrift.com>")
593set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
594set(CPACK_DEBIAN_PACKAGE_SECTION "games")
595
596SET(CPACK_DEB_COMPONENT_INSTALL ON)
597
598set(CPACK_STRIP_FILES ${BINARY_NAME})
599
600install(FILES ${CMAKE_SOURCE_DIR}/README.md ${CMAKE_SOURCE_DIR}/CHANGES DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT lib${BINARY_NAME})
601
602include(CPack)
603
604# Summaries
605message(STATUS "Feature summary:")
606message(STATUS "	CLI debugger: ${USE_CLI_DEBUGGER}")
607message(STATUS "	GDB stub: ${USE_GDB_STUB}")
608message(STATUS "	Video recording: ${USE_FFMPEG}")
609message(STATUS "	GIF recording: ${USE_MAGICK}")
610message(STATUS "	Screenshot/advanced savestate support: ${USE_PNG}")
611message(STATUS "	ZIP support: ${USE_LIBZIP}")
612message(STATUS "	7-Zip support: ${USE_LZMA}")
613message(STATUS "	Better audio resampling: ${USE_BLIP}")
614message(STATUS "Frontend summary:")
615message(STATUS "	Qt: ${BUILD_QT}")
616message(STATUS "	SDL (${SDL_VERSION}): ${BUILD_SDL}")
617message(STATUS "	Libretro core: ${BUILD_LIBRETRO}")
618message(STATUS "	Profiling: ${BUILD_PERF}")
619message(STATUS "	Test harness: ${BUILD_TEST}")
620message(STATUS "Library summary:")
621message(STATUS "	Static: ${BUILD_STATIC}")
622message(STATUS "	Shared: ${BUILD_SHARED}")