all repos — mgba @ 342b02134db8e3cec36ea6db26be3296a4cd109e

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