all repos — mgba @ b92ed79a85cc980fc30045c22d0eb9bfdfa46f4c

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
104set(USE_RESAMPLE OFF)
105set(RESAMPLE_LIBRARY "None")
106
107# Platform support
108if(WIN32)
109	add_definitions(-D_WIN32_WINNT=0x0600)
110	list(APPEND OS_LIB ws2_32)
111	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/windows/*.c)
112	source_group("Windows-specific code" FILES ${OS_SRC})
113else()
114	add_definitions(-DUSE_PTHREADS)
115	list(APPEND OS_LIB pthread)
116	file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/posix/*.c)
117	source_group("POSIX-specific code" FILES ${OS_SRC})
118endif()
119
120if(APPLE)
121	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6")
122	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.7 -stdlib=libc++")
123endif()
124
125if(BUILD_BBB OR BUILD_RASPI)
126	enable_language(ASM)
127	if(NOT BUILD_EGL)
128		add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
129	endif()
130endif()
131
132# Features
133set(DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/debugger.c ${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c)
134set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
135
136if(USE_CLI_DEBUGGER)
137	add_definitions(-DUSE_CLI_DEBUGGER)
138	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c)
139	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/parser.c)
140	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/gba/gba-cli.c)
141	include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
142	link_directories(${LIBEDIT_LIBRARY_DIRS})
143	set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
144	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libedit2")
145else()
146	set(DEBUGGER_LIB "")
147endif()
148
149if(USE_GDB_STUB)
150	add_definitions(-DUSE_GDB_STUB)
151	list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/gdb-stub.c)
152endif()
153source_group("ARM debugger" FILES ${DEBUGGER_SRC})
154
155if(USE_FFMPEG)
156	add_definitions(-DUSE_FFMPEG)
157	pkg_search_module(LIBSWRESAMPLE QUIET libswresample)
158	if(NOT LIBSWRESAMPLE_FOUND)
159		add_definitions(-DUSE_LIBAV)
160	endif()
161	include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVRESAMPLE_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS} ${LIBSWSCALE_INCLUDE_DIRS})
162	link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVRESAMPLE_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS} ${LIBSWSCALE_LIBRARY_DIRS})
163	list(APPEND UTIL_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-encoder.c")
164	set(RESAMPLE_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-resample.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(RESAMPLE_LIBRARY "FFmpeg")
172	set(USE_RESAMPLE ON)
173	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}")
174	set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libavcodec-extra")
175endif()
176
177if(USE_BLIP)
178	set(RESAMPLE_SRC "${CMAKE_SOURCE_DIR}/src/third-party/blip_buf/blip_buf.c")
179	set(RESAMPLE_LIBRARY "blip_buf")
180	set(USE_RESAMPLE ON)
181endif()
182
183if(USE_MAGICK)
184	add_definitions(-DUSE_MAGICK)
185	include_directories(AFTER ${MAGICKWAND_INCLUDE_DIRS})
186	link_directories(${MAGICKWAND_LIBRARY_DIRS})
187	list(APPEND UTIL_SRC "${CMAKE_SOURCE_DIR}/src/platform/imagemagick/imagemagick-gif-encoder.c")
188	list(APPEND DEPENDENCY_LIB ${MAGICKWAND_LIBRARIES})
189	string(REGEX MATCH "^[0-9]+\\.[0-9]+" MAGICKWAND_VERSION_PARTIAL ${MagickWand_VERSION})
190	if(${MAGICKWAND_VERSION_PARTIAL} EQUAL "6.7")
191		set(MAGICKWAND_DEB_VERSION "5")
192	else()
193		set(MAGICKWAND_DEB_VERSION "-6.q16-2")
194	endif()
195	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libmagickwand${MAGICKWAND_DEB_VERSION}")
196endif()
197
198if(USE_PNG)
199	add_definitions(-DUSE_PNG)
200	include_directories(AFTER ${PNG_INCLUDE_DIRS})
201	list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
202	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpng12-0,zlib1g")
203endif()
204
205if(USE_LIBZIP)
206	include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
207	link_directories(${LIBZIP_LIBRARY_DIRS})
208	list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
209	add_definitions(-DENABLE_LIBZIP)
210	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libzip2")
211endif()
212
213if(USE_RESAMPLE)
214	string(TOUPPER ${RESAMPLE_LIBRARY} RESAMPLE_DEFINE)
215	set(RESAMPLE_DEFINE "RESAMPLE_${RESAMPLE_DEFINE}")
216	list(APPEND UTIL_SRC ${RESAMPLE_SRC})
217	add_definitions(-DRESAMPLE_LIBRARY=${RESAMPLE_DEFINE})
218else()
219	add_definitions(-DRESAMPLE_LIBRARY=RESAMPLE_NN)
220endif()
221
222# Binaries
223add_library(${BINARY_NAME} SHARED
224	${ARM_SRC}
225	${GBA_SRC}
226	${DEBUGGER_SRC}
227	${RENDERER_SRC}
228	${UTIL_SRC}
229	${VFS_SRC}
230	${OS_SRC}
231	${THIRD_PARTY_SRC})
232target_link_libraries(${BINARY_NAME} m ${DEBUGGER_LIB} ${OS_LIB} ${DEPENDENCY_LIB})
233install(TARGETS ${BINARY_NAME} DESTINATION lib COMPONENT lib${BINARY_NAME})
234set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI})
235
236if(BUILD_SDL)
237	add_definitions(-DBUILD_SDL)
238	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
239endif()
240
241if(BUILD_QT)
242	add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/qt ${CMAKE_BINARY_DIR}/qt)
243endif()
244
245if(BUILD_PERF)
246	set(PERF_SRC ${CMAKE_SOURCE_DIR}/src/platform/perf-main.c)
247	if(UNIX AND NOT APPLE)
248		list(APPEND PERF_LIB rt)
249	endif()
250
251	add_executable(${BINARY_NAME}-perf ${PERF_SRC})
252	target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB})
253	install(TARGETS ${BINARY_NAME}-perf DESTINATION bin COMPONENT ${BINARY_NAME}-perf)
254	install(FILES ${CMAKE_SOURCE_DIR}/tools/perf.py DESTINATION "${CMAKE_INSTALL_LIBDIR}/${BINARY_NAME}" COMPONENT ${BINARY_NAME}-perf)
255endif()
256
257# Packaging
258set(CPACK_PACKAGE_VERSION_MAJOR ${LIB_VERSION_MAJOR})
259set(CPACK_PACKAGE_VERSION_MINOR ${LIB_VERSION_MINOR})
260set(CPACK_PACKAGE_VERSION_PATCH ${LIB_VERSION_PATCH})
261set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
262set(CPACK_RESOURCE_FILE_README ${CMAKE_SOURCE_DIR}/LICENSE)
263
264set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "mGBA Game Boy Advance Emulator")
265set(CPACK_PACKAGE_VENDOR "Jeffrey Pfau")
266set(CPACK_PACKAGE_CONTACT "Jeffrey Pfau <jeffrey@endrift.com>")
267set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
268
269set(CPACK_STRIP_FILES ${BINARY_NAME})
270
271include(CPack)
272
273# Summaries
274message(STATUS "Feature summary:")
275message(STATUS "	CLI debugger: ${USE_CLI_DEBUGGER}")
276message(STATUS "	GDB stub: ${USE_GDB_STUB}")
277message(STATUS "	Video recording: ${USE_FFMPEG}")
278message(STATUS "	GIF recording: ${USE_MAGICK}")
279message(STATUS "	Screenshot/advanced savestate support: ${USE_PNG}")
280message(STATUS "	ZIP support: ${USE_LIBZIP}")
281message(STATUS "	Better audio resampling (${RESAMPLE_LIBRARY}): ${USE_RESAMPLE}")
282message(STATUS "Frontend summary:")
283message(STATUS "	Qt: ${BUILD_QT}")
284message(STATUS "	SDL (${SDL_VERSION}): ${BUILD_SDL}")
285message(STATUS "	Profiling: ${BUILD_PERF}")