all repos — mgba @ 2dbaf2edd62c5ca3e07d63f01731640b0f3c26ab

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})
150endif()
151
152if(APPLE)
153	add_definitions(-D_DARWIN_C_SOURCE)
154	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6")
155endif()
156
157if(NOT HAIKU AND NOT MSVC AND NOT PSP2)
158	set(M_LIBRARY m)
159endif()
160list(APPEND OS_LIB ${M_LIBRARY})
161
162if(APPLE OR CMAKE_C_COMPILER_ID STREQUAL "GNU" AND BUILD_LTO)
163	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto")
164	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
165	set_source_files_properties(${CMAKE_SOURCE_DIR}/src/arm/isa-arm.c ${CMAKE_SOURCE_DIR}/src/arm/isa-thumb.c PROPERTIES COMPILE_FLAGS -fno-lto)
166endif()
167
168if(BUILD_BBB OR BUILD_RASPI OR BUILD_PANDORA)
169	if(NOT BUILD_EGL)
170		add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
171	endif()
172endif()
173
174if(BUILD_RASPI)
175	set(BUILD_GL OFF CACHE BOOL "OpenGL not supported" FORCE)
176endif()
177
178if(BUILD_PANDORA)
179	add_definitions(-DBUILD_PANDORA)
180endif()
181
182if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm.*")
183	enable_language(ASM)
184endif()
185
186if(PSP2 OR WII)
187	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
188endif()
189
190if(WII)
191	add_definitions(-U__STRICT_ANSI__)
192endif()
193
194include(CheckFunctionExists)
195check_function_exists(strdup HAVE_STRDUP)
196check_function_exists(strndup HAVE_STRNDUP)
197check_function_exists(localtime_r HAVE_LOCALTIME_R)
198if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
199	check_function_exists(snprintf_l HAVE_SNPRINTF_L)
200	if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
201		# The strtof_l on Linux not actually exposed nor actually strtof_l
202		set(HAVE_STRTOF_L OFF)
203	else()
204		check_function_exists(strtof_l HAVE_STRTOF_L)
205	endif()
206	check_function_exists(newlocale HAVE_NEWLOCALE)
207	check_function_exists(freelocale HAVE_FREELOCALE)
208	check_function_exists(uselocale HAVE_USELOCALE)
209	check_function_exists(setlocale HAVE_SETLOCALE)
210else()
211	set(DISABLE_DEPS ON CACHE BOOL "This platform cannot build with dependencies" FORCE)
212	set(DISABLE_FRONTENDS ON)
213	set(MINIMAL_CORE ON)
214endif()
215
216if(HAVE_STRDUP)
217	add_definitions(-DHAVE_STRDUP)
218endif()
219
220if(HAVE_STRNDUP)
221	add_definitions(-DHAVE_STRNDUP)
222endif()
223
224if(HAVE_LOCALTIME_R)
225	add_definitions(-DHAVE_LOCALTIME_R)
226endif()
227
228if(HAVE_NEWLOCALE AND HAVE_FREELOCALE AND HAVE_USELOCALE)
229	add_definitions(-DHAVE_LOCALE)
230	if (HAVE_STRTOF_L)
231		add_definitions(-DHAVE_STRTOF_L)
232	endif()
233	if (HAVE_SNPRINTF_L)
234		add_definitions(-DHAVE_SNPRINTF_L)
235	endif()
236endif()
237
238if(HAVE_SETLOCALE)
239	add_definitions(-DHAVE_SETLOCALE)
240endif()
241
242# Feature dependencies
243set(FEATURES)
244if(CMAKE_SYSTEM_NAME MATCHES .*BSD)
245	set(LIBEDIT_LIBRARIES -ledit)
246	if (CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
247		list(APPEND LIBEDIT_LIBRARIES -ltermcap)
248	endif()
249else()
250	find_feature(USE_CLI_DEBUGGER "libedit")
251endif()
252if(BUILD_GL)
253	find_package(OpenGL QUIET)
254	if(NOT OPENGL_FOUND)
255		set(BUILD_GL OFF CACHE BOOL "OpenGL not found" FORCE)
256	endif()
257endif()
258if(BUILD_GLES2 AND NOT BUILD_RASPI)
259	find_path(OPENGLES2_INCLUDE_DIR NAMES GLES2/gl2.h)
260	find_library(OPENGLES2_LIBRARY NAMES GLESv2 GLESv2_CM)
261	if(NOT OPENGLES2_INCLUDE_DIR OR NOT OPENGLES2_LIBRARY)
262		set(BUILD_GLES2 OFF CACHE BOOL "OpenGL|ES 2 not found" FORCE)
263	endif()
264endif()
265set(WANT_ZLIB ${USE_ZLIB})
266set(WANT_PNG ${USE_PNG})
267find_feature(USE_FFMPEG "libavcodec;libavformat;libavresample;libavutil;libswscale")
268if(NOT PSP2)
269	find_feature(USE_ZLIB "ZLIB")
270	find_feature(USE_PNG "PNG")
271endif()
272find_feature(USE_LIBZIP "libzip")
273find_feature(USE_MAGICK "MagickWand")
274
275# Features
276set(DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/debugger.c ${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c)
277set(FEATURE_SRC)
278set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
279
280if(DISABLE_DEPS)
281	set(USE_LZMA OFF)
282	set(USE_GDB_STUB OFF)
283endif()
284
285if(USE_CLI_DEBUGGER)
286	list(APPEND FEATURES CLI_DEBUGGER)
287	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c)
288	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/debugger/parser.c)
289	list(APPEND FEATURE_SRC ${CMAKE_SOURCE_DIR}/src/gba/supervisor/cli.c)
290	include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
291	link_directories(${LIBEDIT_LIBRARY_DIRS})
292	set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
293	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libedit2")
294else()
295	set(DEBUGGER_LIB "")
296endif()
297
298if(USE_GDB_STUB)
299	list(APPEND FEATURES GDB_STUB)
300	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/gdb-stub.c)
301endif()
302source_group("ARM debugger" FILES ${DEBUGGER_SRC})
303
304if(USE_FFMPEG)
305	list(APPEND FEATURES FFMPEG)
306	pkg_search_module(LIBSWRESAMPLE QUIET libswresample)
307	if(NOT LIBSWRESAMPLE_FOUND)
308		list(APPEND FEATURES LIBAV)
309	endif()
310	include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVRESAMPLE_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS} ${LIBSWSCALE_INCLUDE_DIRS})
311	link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVRESAMPLE_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS} ${LIBSWSCALE_LIBRARY_DIRS})
312	list(APPEND FEATURE_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-encoder.c")
313	string(REGEX MATCH "^[0-9]+" LIBAVCODEC_VERSION_MAJOR ${libavcodec_VERSION})
314	string(REGEX MATCH "^[0-9]+" LIBAVFORMAT_VERSION_MAJOR ${libavformat_VERSION})
315	string(REGEX MATCH "^[0-9]+" LIBAVRESAMPLE_VERSION_MAJOR ${libavresample_VERSION})
316	string(REGEX MATCH "^[0-9]+" LIBAVUTIL_VERSION_MAJOR ${libavutil_VERSION})
317	string(REGEX MATCH "^[0-9]+" LIBSWSCALE_VERSION_MAJOR ${libswscale_VERSION})
318	list(APPEND DEPENDENCY_LIB ${LIBAVCODEC_LIBRARIES} ${LIBAVFORMAT_LIBRARIES} ${LIBAVRESAMPLE_LIBRARIES} ${LIBAVUTIL_LIBRARIES} ${LIBSWSCALE_LIBRARIES})
319	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}")
320	set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libavcodec-extra")
321	if(APPLE)
322		set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
323		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework VideoDecodeAcceleration -framework CoreVideo")
324	endif()
325endif()
326
327if(USE_BLIP)
328	list(APPEND THIRD_PARTY_SRC "${CMAKE_SOURCE_DIR}/src/third-party/blip_buf/blip_buf.c")
329	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_BLIP_BUF)
330else()
331	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_NN)
332endif()
333
334if(USE_MAGICK)
335	list(APPEND FEATURES MAGICK)
336	include_directories(AFTER ${MAGICKWAND_INCLUDE_DIRS})
337	link_directories(${MAGICKWAND_LIBRARY_DIRS})
338	list(APPEND FEATURE_SRC "${CMAKE_SOURCE_DIR}/src/platform/imagemagick/imagemagick-gif-encoder.c")
339	list(APPEND DEPENDENCY_LIB ${MAGICKWAND_LIBRARIES})
340	string(REGEX MATCH "^[0-9]+\\.[0-9]+" MAGICKWAND_VERSION_PARTIAL ${MagickWand_VERSION})
341	if(${MAGICKWAND_VERSION_PARTIAL} EQUAL "6.7")
342		set(MAGICKWAND_DEB_VERSION "5")
343	else()
344		set(MAGICKWAND_DEB_VERSION "-6.q16-2")
345	endif()
346	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libmagickwand${MAGICKWAND_DEB_VERSION}")
347endif()
348
349if(WANT_ZLIB AND NOT USE_ZLIB)
350	set(SKIP_INSTALL_ALL ON)
351	add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party/zlib zlib)
352	set_property(TARGET zlibstatic PROPERTY INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/zlib;${CMAKE_SOURCE_DIR}/src/third-party/zlib)
353	set_property(TARGET zlib PROPERTY EXCLUDE_FROM_ALL ON)
354	set_property(TARGET example PROPERTY EXCLUDE_FROM_ALL ON)
355	set_property(TARGET minigzip PROPERTY EXCLUDE_FROM_ALL ON)
356	set(ZLIB_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/third-party/zlib ${CMAKE_BINARY_DIR}/zlib)
357	set(ZLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/src/third-party/zlib ${CMAKE_BINARY_DIR}/zlib)
358	set(ZLIB_LIBRARY zlibstatic)
359	list(APPEND DEPENDENCY_LIB zlibstatic)
360	set(USE_ZLIB ON)
361endif()
362
363if(USE_ZLIB)
364	list(APPEND FEATURES ZLIB)
365	include_directories(AFTER ${ZLIB_INCLUDE_DIRS})
366	list(APPEND DEPENDENCY_LIB ${ZLIB_LIBRARIES})
367	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},zlib1g")
368endif()
369
370if(WANT_PNG AND USE_ZLIB AND NOT USE_PNG)
371	set(PNG_STATIC ON CACHE BOOL "" FORCE)
372	set(PNG_SHARED OFF CACHE BOOL "" FORCE)
373	set(PNG_TESTS OFF CACHE BOOL "" FORCE)
374	set(SKIP_INSTALL_ALL ON)
375	add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party/libpng libpng)
376	set_property(TARGET png16_static PROPERTY INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/libpng;${CMAKE_SOURCE_DIR}/src/third-party/libpng;${ZLIB_INCLUDE_DIRS})
377	set(PNG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/third-party/libpng ${CMAKE_BINARY_DIR}/libpng)
378	list(APPEND DEPENDENCY_LIB png16_static)
379	set(USE_PNG ON)
380endif()
381
382if(USE_PNG)
383	list(APPEND FEATURES PNG)
384	if(NOT PSP2)
385		include_directories(AFTER ${PNG_INCLUDE_DIRS})
386		list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
387		set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpng12-0")
388	endif()
389endif()
390
391if(USE_LIBZIP)
392	include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
393	link_directories(${LIBZIP_LIBRARY_DIRS})
394	list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
395	list(APPEND FEATURES LIBZIP)
396	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-zip.c)
397	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip2")
398endif()
399
400if (USE_LZMA)
401	include_directories(AFTER ${CMAKE_SOURCE_DIR}/third-party/lzma)
402	add_definitions(-D_7ZIP_PPMD_SUPPPORT)
403	list(APPEND VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-lzma.c)
404	set(LZMA_SRC
405		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zAlloc.c
406		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zArcIn.c
407		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf.c
408		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf2.c
409		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrc.c
410		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrcOpt.c
411		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zDec.c
412		${CMAKE_SOURCE_DIR}/src/third-party/lzma/CpuArch.c
413		${CMAKE_SOURCE_DIR}/src/third-party/lzma/LzmaDec.c
414		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Lzma2Dec.c
415		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra.c
416		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra86.c
417		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bcj2.c
418		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7.c
419		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7Dec.c
420		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zFile.c
421		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zStream.c)
422	list(APPEND FEATURE_SRC ${LZMA_SRC})
423	list(APPEND FEATURES LZMA)
424endif()
425
426set(FEATURE_DEFINES)
427foreach(FEATURE IN LISTS FEATURES)
428	list(APPEND FEATURE_DEFINES "USE_${FEATURE}")
429endforeach()
430
431source_group("Virtual files" FILES ${VFS_SRC})
432source_group("Extra features" FILES ${FEATURE_SRC})
433source_group("Third-party code" FILES ${THIRD_PARTY_SRC})
434
435# Platform binaries
436if(3DS)
437	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/3ds ${CMAKE_BINARY_DIR}/3ds)
438endif()
439
440if(WII)
441	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/wii ${CMAKE_BINARY_DIR}/wii)
442endif()
443
444if(PSP2)
445	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/psp2 ${CMAKE_BINARY_DIR}/psp2)
446endif()
447
448# Binaries
449set(CORE_SRC
450	${ARM_SRC}
451	${GBA_SRC}
452	${GBA_CHEATS_SRC}
453	${GBA_CTX_SRC}
454	${DEBUGGER_SRC}
455	${RENDERER_SRC}
456	${UTIL_SRC}
457	${VFS_SRC}
458	${OS_SRC}
459	${THIRD_PARTY_SRC})
460
461if(NOT MINIMAL_CORE)
462	set(SRC
463		${CORE_SRC}
464		${GBA_RR_SRC}
465		${GBA_SV_SRC}
466		${SIO_SRC}
467		${FEATURE_SRC})
468else()
469	set(SRC ${CORE_SRC})
470endif()
471
472if(NOT BUILD_STATIC AND NOT BUILD_SHARED)
473	set(BUILD_SHARED ON)
474endif()
475
476if(BUILD_SHARED)
477	add_library(${BINARY_NAME} SHARED ${SRC})
478	if(BUILD_STATIC)
479		add_library(${BINARY_NAME}-static STATIC ${SRC})
480		set_target_properties(${BINARY_NAME}-static PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES}")
481		install(TARGETS ${BINARY_NAME}-static DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
482		add_dependencies(${BINARY_NAME}-static version-info)
483	endif()
484else()
485	add_library(${BINARY_NAME} STATIC ${SRC})
486endif()
487
488add_dependencies(${BINARY_NAME} version-info)
489set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI} COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES}")
490
491target_link_libraries(${BINARY_NAME} ${DEBUGGER_LIB} ${DEPENDENCY_LIB} ${OS_LIB})
492install(TARGETS ${BINARY_NAME} LIBRARY DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME} NAMELINK_SKIP ARCHIVE DESTINATION ${LIBDIR} RUNTIME DESTINATION ${LIBDIR} COMPONENT lib${BINARY_NAME})
493if(UNIX AND NOT APPLE)
494	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-16.png DESTINATION share/icons/hicolor/16x16/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
495	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-24.png DESTINATION share/icons/hicolor/24x24/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
496	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-32.png DESTINATION share/icons/hicolor/32x32/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
497	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-48.png DESTINATION share/icons/hicolor/48x48/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
498	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-64.png DESTINATION share/icons/hicolor/64x64/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
499	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-96.png DESTINATION share/icons/hicolor/96x96/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
500	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-128.png DESTINATION share/icons/hicolor/128x128/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
501	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-256.png DESTINATION share/icons/hicolor/256x256/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
502	install(FILES ${CMAKE_SOURCE_DIR}/res/mgba-512.png DESTINATION share/icons/hicolor/512x512/apps RENAME mgba.png COMPONENT lib${BINARY_NAME})
503endif()
504
505if(BUILD_GL)
506	add_definitions(-DBUILD_GL)
507endif()
508
509if(BUILD_GLES2)
510	add_definitions(-DBUILD_GLES2)
511endif()
512
513if(DISABLE_FRONTENDS)
514	set(BUILD_SDL OFF)
515	set(BUILD_QT OFF)
516endif()
517
518if(BUILD_LIBRETRO)
519	file(GLOB RETRO_SRC ${CMAKE_SOURCE_DIR}/src/platform/libretro/*.c)
520	add_library(${BINARY_NAME}_libretro SHARED ${CORE_SRC} ${RETRO_SRC})
521	set_target_properties(${BINARY_NAME}_libretro PROPERTIES PREFIX "" COMPILE_DEFINITIONS "COLOR_16_BIT;COLOR_5_6_5;DISABLE_THREADING")
522	target_link_libraries(${BINARY_NAME}_libretro ${OS_LIB})
523endif()
524
525if(BUILD_SDL)
526	add_definitions(-DBUILD_SDL)
527	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
528endif()
529
530if(BUILD_QT)
531	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/qt ${CMAKE_BINARY_DIR}/qt)
532endif()
533
534if(BUILD_PERF)
535	set(PERF_SRC ${CMAKE_SOURCE_DIR}/src/platform/test/perf-main.c)
536	if(UNIX AND NOT APPLE)
537		list(APPEND PERF_LIB rt)
538	endif()
539
540	add_executable(${BINARY_NAME}-perf ${PERF_SRC})
541	target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB})
542	install(TARGETS ${BINARY_NAME}-perf DESTINATION bin COMPONENT ${BINARY_NAME}-perf)
543	install(FILES ${CMAKE_SOURCE_DIR}/tools/perf.py DESTINATION "${CMAKE_INSTALL_LIBDIR}/${BINARY_NAME}" COMPONENT ${BINARY_NAME}-perf)
544endif()
545
546if(BUILD_TEST)
547	add_executable(${BINARY_NAME}-fuzz ${CMAKE_SOURCE_DIR}/src/platform/test/fuzz-main.c)
548	target_link_libraries(${BINARY_NAME}-fuzz ${BINARY_NAME})
549	install(TARGETS ${BINARY_NAME}-fuzz DESTINATION bin COMPONENT ${BINARY_NAME}-test)
550endif()
551
552# Packaging
553set(CPACK_PACKAGE_VERSION ${VERSION_STRING})
554set(CPACK_PACKAGE_VERSION_MAJOR ${LIB_VERSION_MAJOR})
555set(CPACK_PACKAGE_VERSION_MINOR ${LIB_VERSION_MINOR})
556set(CPACK_PACKAGE_VERSION_PATCH ${LIB_VERSION_PATCH})
557set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
558set(CPACK_RESOURCE_FILE_README ${CMAKE_SOURCE_DIR}/README.md)
559
560set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "mGBA Game Boy Advance Emulator")
561set(CPACK_PACKAGE_VENDOR "Jeffrey Pfau")
562set(CPACK_PACKAGE_CONTACT "Jeffrey Pfau <jeffrey@endrift.com>")
563set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
564set(CPACK_DEBIAN_PACKAGE_SECTION "games")
565
566SET(CPACK_DEB_COMPONENT_INSTALL ON)
567
568set(CPACK_STRIP_FILES ${BINARY_NAME})
569
570install(FILES ${CMAKE_SOURCE_DIR}/README.md ${CMAKE_SOURCE_DIR}/CHANGES DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT lib${BINARY_NAME})
571
572include(CPack)
573
574# Summaries
575message(STATUS "Feature summary:")
576message(STATUS "	CLI debugger: ${USE_CLI_DEBUGGER}")
577message(STATUS "	GDB stub: ${USE_GDB_STUB}")
578message(STATUS "	Video recording: ${USE_FFMPEG}")
579message(STATUS "	GIF recording: ${USE_MAGICK}")
580message(STATUS "	Screenshot/advanced savestate support: ${USE_PNG}")
581message(STATUS "	ZIP support: ${USE_LIBZIP}")
582message(STATUS "	7-Zip support: ${USE_LZMA}")
583message(STATUS "	Better audio resampling: ${USE_BLIP}")
584message(STATUS "Frontend summary:")
585message(STATUS "	Qt: ${BUILD_QT}")
586message(STATUS "	SDL (${SDL_VERSION}): ${BUILD_SDL}")
587message(STATUS "	Libretro core: ${BUILD_LIBRETRO}")
588message(STATUS "	Profiling: ${BUILD_PERF}")
589message(STATUS "	Test harness: ${BUILD_TEST}")
590message(STATUS "Library summary:")
591message(STATUS "	Static: ${BUILD_STATIC}")
592message(STATUS "	Shared: ${BUILD_SHARED}")