all repos — mgba @ 811d654893a68882f15b04f6f3f9bfe47dea20da

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