all repos — mgba @ e9a2b2a57c4f478fb3600fe9d87e1b97804d73de

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")
  4set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=gnu99")
  5set(USE_CLI_DEBUGGER ON CACHE BOOL "Whether or not to enable the CLI-mode ARM debugger")
  6set(USE_GDB_STUB ON CACHE BOOL "Whether or not to enable the GDB stub ARM debugger")
  7set(USE_FFMPEG ON CACHE BOOL "Whether or not to enable FFmpeg support")
  8set(USE_PNG ON CACHE BOOL "Whether or not to enable PNG support")
  9set(USE_LIBZIP ON CACHE BOOL "Whether or not to enable ZIP support")
 10set(USE_MAGICK ON CACHE BOOL "Whether or not to enable ImageMagick support")
 11set(USE_BLIP ON CACHE BOOL "Whether or not to enable blip_buf support")
 12set(USE_LZMA ON CACHE BOOL "Whether or not to enable 7-Zip support")
 13set(BUILD_QT ON CACHE BOOL "Build Qt frontend")
 14set(BUILD_SDL ON CACHE BOOL "Build SDL frontend")
 15set(BUILD_PERF OFF CACHE BOOL "Build performance profiling tool")
 16file(GLOB ARM_SRC ${CMAKE_SOURCE_DIR}/src/arm/*.c)
 17file(GLOB GBA_SRC ${CMAKE_SOURCE_DIR}/src/gba/*.c)
 18file(GLOB UTIL_SRC ${CMAKE_SOURCE_DIR}/src/util/*.[cSs])
 19file(GLOB VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/*.c)
 20file(GLOB RENDERER_SRC ${CMAKE_SOURCE_DIR}/src/gba/renderers/video-software.c)
 21file(GLOB THIRD_PARTY_SRC ${CMAKE_SOURCE_DIR}/src/third-party/inih/*.c)
 22list(APPEND UTIL_SRC ${CMAKE_SOURCE_DIR}/src/platform/commandline.c)
 23source_group("ARM core" FILES ${ARM_SRC})
 24source_group("GBA board" FILES ${GBA_SRC} ${RENDERER_SRC})
 25source_group("Utilities" FILES ${UTIL_SRC} ${VFS_SRC}})
 26include_directories(${CMAKE_SOURCE_DIR}/src/arm)
 27include_directories(${CMAKE_SOURCE_DIR}/src/gba)
 28include_directories(${CMAKE_SOURCE_DIR}/src)
 29
 30if(NOT CMAKE_BUILD_TYPE)
 31	set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type (e.g. Release or Debug)" FORCE)
 32endif()
 33
 34include(GNUInstallDirs)
 35
 36# Function definitions
 37include(FindPkgConfig)
 38function(find_feature FEATURE_NAME FEATURE_REQUIRES)
 39	if (NOT ${FEATURE_NAME})
 40		return()
 41	endif()
 42	foreach(REQUIRE ${FEATURE_REQUIRES})
 43		if(NOT ${REQUIRE}_FOUND)
 44			find_package(${REQUIRE} QUIET)
 45			if(NOT ${REQUIRE}_FOUND)
 46				pkg_search_module(${REQUIRE} ${REQUIRE})
 47				if (NOT ${REQUIRE}_FOUND)
 48					message(WARNING "Requested module ${REQUIRE} missing for feature ${FEATURE_NAME}. Feature disabled.")
 49					set(${FEATURE_NAME} OFF PARENT_SCOPE)
 50					return()
 51				endif()
 52			endif()
 53		endif()
 54		string(TOUPPER ${REQUIRE} UREQUIRE)
 55		set(${UREQUIRE}_CFLAGS_OTHER ${${REQUIRE}_CFLAGS_OTHER} PARENT_SCOPE)
 56		set(${UREQUIRE}_FOUND ${${REQUIRE}_FOUND} PARENT_SCOPE)
 57		set(${UREQUIRE}_INCLUDE_DIRS ${${REQUIRE}_INCLUDE_DIRS} PARENT_SCOPE)
 58		set(${UREQUIRE}_LIBRARIES ${${REQUIRE}_LIBRARIES} PARENT_SCOPE)
 59		set(${UREQUIRE}_LIBRARY_DIRS ${${REQUIRE}_LIBRARY_DIRS} PARENT_SCOPE)
 60		set(${UREQUIRE}_LDFLAGS_OTHER ${${REQUIRE}_LDFLAGS_OTHER} PARENT_SCOPE)
 61	endforeach()
 62endfunction()
 63
 64# Version information
 65set(LIB_VERSION_MAJOR 0)
 66set(LIB_VERSION_MINOR 2)
 67set(LIB_VERSION_PATCH 0)
 68set(LIB_VERSION_ABI 0.2)
 69set(LIB_VERSION_STRING ${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}.${LIB_VERSION_PATCH})
 70
 71# Advanced settings
 72set(BUILD_PGO CACHE BOOL "Build with profiling-guided optimization")
 73set(PGO_STAGE_2 CACHE BOOL "Rebuild for profiling-guided optimization after profiles have been generated")
 74set(PGO_DIR "/tmp/gba-pgo/" CACHE PATH "Profiling-guided optimization profiles path")
 75mark_as_advanced(BUILD_PGO PGO_STAGE_2 PGO_DIR)
 76set(PGO_PRE_FLAGS "-pg -fprofile-generate=${PGO_DIR} -fprofile-arcs")
 77set(PGO_POST_FLAGS "-fprofile-use=${PGO_DIR} -fbranch-probabilities")
 78
 79if(BUILD_PGO AND NOT PGO_STAGE_2)
 80	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_PRE_FLAGS}")
 81	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
 82	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
 83elseif(BUILD_PGO AND PGO_STAGE_2)
 84	set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_POST_FLAGS}")
 85	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_POST_FLAGS}")
 86	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_POST_FLAGS}")
 87endif()
 88
 89add_definitions(-DBINARY_NAME="${BINARY_NAME}" -DPROJECT_NAME="${PROJECT_NAME}" -DPROJECT_VERSION="${LIB_VERSION_STRING}")
 90
 91# Feature dependencies
 92if(CMAKE_SYSTEM_NAME MATCHES .*BSD)
 93	set(LIBEDIT_LIBRARIES -ledit)
 94else()
 95	find_feature(USE_CLI_DEBUGGER "libedit")
 96endif()
 97find_feature(USE_FFMPEG "libavcodec;libavformat;libavresample;libavutil;libswscale")
 98find_feature(USE_PNG "ZLIB;PNG")
 99find_feature(USE_LIBZIP "libzip")
100find_feature(USE_MAGICK "MagickWand")
101
102include(CheckFunctionExists)
103check_function_exists(strndup HAVE_STRNDUP)
104
105if(HAVE_STRNDUP)
106	add_definitions(-DHAVE_STRNDUP)
107endif()
108
109# Platform support
110if(WIN32)
111	add_definitions(-D_WIN32_WINNT=0x0600)
112	list(APPEND OS_LIB ws2_32)
113	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/windows/*.c)
114	source_group("Windows-specific code" FILES ${OS_SRC})
115else()
116	add_definitions(-DUSE_PTHREADS)
117	list(APPEND OS_LIB pthread)
118	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/posix/*.c)
119	source_group("POSIX-specific code" FILES ${OS_SRC})
120endif()
121
122if(APPLE)
123	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6")
124endif()
125
126if(BUILD_BBB OR BUILD_RASPI)
127	enable_language(ASM)
128	if(NOT BUILD_EGL)
129		add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
130	endif()
131endif()
132
133# Features
134set(DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/debugger.c ${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c)
135set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
136
137if(USE_CLI_DEBUGGER)
138	add_definitions(-DUSE_CLI_DEBUGGER)
139	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c)
140	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/parser.c)
141	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/gba/gba-cli.c)
142	include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
143	link_directories(${LIBEDIT_LIBRARY_DIRS})
144	set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
145	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libedit2")
146else()
147	set(DEBUGGER_LIB "")
148endif()
149
150if(USE_GDB_STUB)
151	add_definitions(-DUSE_GDB_STUB)
152	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/gdb-stub.c)
153endif()
154source_group("ARM debugger" FILES ${DEBUGGER_SRC})
155
156if(USE_FFMPEG)
157	add_definitions(-DUSE_FFMPEG)
158	pkg_search_module(LIBSWRESAMPLE QUIET libswresample)
159	if(NOT LIBSWRESAMPLE_FOUND)
160		add_definitions(-DUSE_LIBAV)
161	endif()
162	include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVRESAMPLE_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS} ${LIBSWSCALE_INCLUDE_DIRS})
163	link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVRESAMPLE_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS} ${LIBSWSCALE_LIBRARY_DIRS})
164	list(APPEND UTIL_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-encoder.c")
165	string(REGEX MATCH "^[0-9]+" LIBAVCODEC_VERSION_MAJOR ${libavcodec_VERSION})
166	string(REGEX MATCH "^[0-9]+" LIBAVFORMAT_VERSION_MAJOR ${libavformat_VERSION})
167	string(REGEX MATCH "^[0-9]+" LIBAVRESAMPLE_VERSION_MAJOR ${libavresample_VERSION})
168	string(REGEX MATCH "^[0-9]+" LIBAVUTIL_VERSION_MAJOR ${libavutil_VERSION})
169	string(REGEX MATCH "^[0-9]+" LIBSWSCALE_VERSION_MAJOR ${libswscale_VERSION})
170	list(APPEND DEPENDENCY_LIB ${LIBAVCODEC_LIBRARIES} ${LIBAVFORMAT_LIBRARIES} ${LIBAVRESAMPLE_LIBRARIES} ${LIBAVUTIL_LIBRARIES} ${LIBSWSCALE_LIBRARIES})
171	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libavcodec${LIBAVCODEC_VERSION_MAJOR},libavformat${LIBAVFORMAT_VERSION_MAJOR},libavresample${LIBAVRESAMPLE_VERSION_MAJOR},libavutil${LIBAVUTIL_VERSION_MAJOR},libswscale${LIBSWSCALE_VERSION_MAJOR}")
172	set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libavcodec-extra")
173endif()
174
175if(USE_BLIP)
176	list(APPEND UTIL_SRC "${CMAKE_SOURCE_DIR}/src/third-party/blip_buf/blip_buf.c")
177	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_BLIP_BUF)
178else()
179	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_NN)
180endif()
181
182if(USE_MAGICK)
183	add_definitions(-DUSE_MAGICK)
184	include_directories(AFTER ${MAGICKWAND_INCLUDE_DIRS})
185	link_directories(${MAGICKWAND_LIBRARY_DIRS})
186	list(APPEND UTIL_SRC "${CMAKE_SOURCE_DIR}/src/platform/imagemagick/imagemagick-gif-encoder.c")
187	list(APPEND DEPENDENCY_LIB ${MAGICKWAND_LIBRARIES})
188	string(REGEX MATCH "^[0-9]+\\.[0-9]+" MAGICKWAND_VERSION_PARTIAL ${MagickWand_VERSION})
189	if(${MAGICKWAND_VERSION_PARTIAL} EQUAL "6.7")
190		set(MAGICKWAND_DEB_VERSION "5")
191	else()
192		set(MAGICKWAND_DEB_VERSION "-6.q16-2")
193	endif()
194	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libmagickwand${MAGICKWAND_DEB_VERSION}")
195endif()
196
197if(USE_PNG)
198	add_definitions(-DUSE_PNG)
199	include_directories(AFTER ${PNG_INCLUDE_DIRS})
200	list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
201	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpng12-0,zlib1g")
202endif()
203
204if(USE_LIBZIP)
205	include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
206	link_directories(${LIBZIP_LIBRARY_DIRS})
207	list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
208	add_definitions(-DENABLE_LIBZIP)
209	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip2")
210endif()
211
212if (USE_LZMA)
213	include_directories(AFTER ${CMAKE_SOURCE_DIR}/third-party/lzma)
214	add_definitions(-D_7ZIP_PPMD_SUPPPORT)
215	set(LZMA_SRC
216		${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-lzma.c
217		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zAlloc.c
218		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zArcIn.c
219		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf.c
220		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zBuf2.c
221		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrc.c
222		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zCrcOpt.c
223		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zDec.c
224		${CMAKE_SOURCE_DIR}/src/third-party/lzma/CpuArch.c
225		${CMAKE_SOURCE_DIR}/src/third-party/lzma/LzmaDec.c
226		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Lzma2Dec.c
227		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra.c
228		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bra86.c
229		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Bcj2.c
230		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7.c
231		${CMAKE_SOURCE_DIR}/src/third-party/lzma/Ppmd7Dec.c
232		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zFile.c
233		${CMAKE_SOURCE_DIR}/src/third-party/lzma/7zStream.c)
234	list(APPEND UTIL_SRC ${LZMA_SRC})
235	add_definitions(-DENABLE_LZMA)
236endif()
237
238# Binaries
239add_library(${BINARY_NAME} SHARED
240	${ARM_SRC}
241	${GBA_SRC}
242	${DEBUGGER_SRC}
243	${RENDERER_SRC}
244	${UTIL_SRC}
245	${VFS_SRC}
246	${OS_SRC}
247	${THIRD_PARTY_SRC})
248target_link_libraries(${BINARY_NAME} m ${DEBUGGER_LIB} ${OS_LIB} ${DEPENDENCY_LIB})
249install(TARGETS ${BINARY_NAME} DESTINATION lib COMPONENT lib${BINARY_NAME})
250set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI})
251
252if(BUILD_SDL)
253	add_definitions(-DBUILD_SDL)
254	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
255endif()
256
257if(BUILD_QT)
258	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/qt ${CMAKE_BINARY_DIR}/qt)
259endif()
260
261if(BUILD_PERF)
262	set(PERF_SRC ${CMAKE_SOURCE_DIR}/src/platform/perf-main.c)
263	if(UNIX AND NOT APPLE)
264		list(APPEND PERF_LIB rt)
265	endif()
266
267	add_executable(${BINARY_NAME}-perf ${PERF_SRC})
268	target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB})
269	install(TARGETS ${BINARY_NAME}-perf DESTINATION bin COMPONENT ${BINARY_NAME}-perf)
270	install(FILES ${CMAKE_SOURCE_DIR}/tools/perf.py DESTINATION "${CMAKE_INSTALL_LIBDIR}/${BINARY_NAME}" COMPONENT ${BINARY_NAME}-perf)
271endif()
272
273# Packaging
274set(CPACK_PACKAGE_VERSION_MAJOR ${LIB_VERSION_MAJOR})
275set(CPACK_PACKAGE_VERSION_MINOR ${LIB_VERSION_MINOR})
276set(CPACK_PACKAGE_VERSION_PATCH ${LIB_VERSION_PATCH})
277set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
278set(CPACK_RESOURCE_FILE_README ${CMAKE_SOURCE_DIR}/LICENSE)
279
280set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "mGBA Game Boy Advance Emulator")
281set(CPACK_PACKAGE_VENDOR "Jeffrey Pfau")
282set(CPACK_PACKAGE_CONTACT "Jeffrey Pfau <jeffrey@endrift.com>")
283set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
284
285set(CPACK_STRIP_FILES ${BINARY_NAME})
286
287include(CPack)
288
289# Summaries
290message(STATUS "Feature summary:")
291message(STATUS "	CLI debugger: ${USE_CLI_DEBUGGER}")
292message(STATUS "	GDB stub: ${USE_GDB_STUB}")
293message(STATUS "	Video recording: ${USE_FFMPEG}")
294message(STATUS "	GIF recording: ${USE_MAGICK}")
295message(STATUS "	Screenshot/advanced savestate support: ${USE_PNG}")
296message(STATUS "	ZIP support: ${USE_LIBZIP}")
297message(STATUS "	7-Zip support: ${USE_LZMA}")
298message(STATUS "	Better audio resampling: ${USE_BLIP}")
299message(STATUS "Frontend summary:")
300message(STATUS "	Qt: ${BUILD_QT}")
301message(STATUS "	SDL (${SDL_VERSION}): ${BUILD_SDL}")
302message(STATUS "	Profiling: ${BUILD_PERF}")