all repos — mgba @ bdc1ef40efab26d4be59d6275ae03336489dbba6

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