all repos — mgba @ 8460a1eea9c1de4983bb99a888297d9733b04885

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