all repos — mgba @ 660ac6a6beb4ada2ba25b3a7870d7d640fb24ce7

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