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