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