all repos — mgba @ 38cb3eeaa857e744917d3ed57263f7585bf74233

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