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(BUILD_SDL ON CACHE BOOL "Build SDL frontend")
11set(BUILD_PERF OFF CACHE BOOL "Build performance profiling tool")
12file(GLOB ARM_SRC ${CMAKE_SOURCE_DIR}/src/arm/*.c)
13file(GLOB GBA_SRC ${CMAKE_SOURCE_DIR}/src/gba/*.c)
14file(GLOB UTIL_SRC ${CMAKE_SOURCE_DIR}/src/util/*.[cS])
15file(GLOB VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/*.c)
16file(GLOB RENDERER_SRC ${CMAKE_SOURCE_DIR}/src/gba/renderers/video-software.c)
17list(APPEND UTIL_SRC ${CMAKE_SOURCE_DIR}/src/platform/commandline.c)
18source_group("ARM core" FILES ${ARM_SRC})
19source_group("GBA board" FILES ${GBA_SRC} ${RENDERER_SRC})
20source_group("Utilities" FILES ${UTIL_SRC} ${VFS_SRC}})
21include_directories(${CMAKE_SOURCE_DIR}/src/arm)
22include_directories(${CMAKE_SOURCE_DIR}/src/gba)
23include_directories(${CMAKE_SOURCE_DIR}/src)
24
25include(GNUInstallDirs)
26
27# Function definitions
28include(FindPkgConfig)
29function(find_feature FEATURE_NAME FEATURE_REQUIRES)
30 if (NOT ${FEATURE_NAME})
31 return()
32 endif()
33 foreach(REQUIRE ${FEATURE_REQUIRES})
34 if(NOT ${REQUIRE}_FOUND)
35 find_package(${REQUIRE} QUIET)
36 if(NOT ${REQUIRE}_FOUND)
37 pkg_search_module(${REQUIRE} ${REQUIRE})
38 if (NOT ${REQUIRE}_FOUND)
39 message(WARNING "Requested module ${REQUIRE} missing for feature ${FEATURE_NAME}. Feature disabled.")
40 set(${FEATURE_NAME} OFF PARENT_SCOPE)
41 return()
42 endif()
43 endif()
44 endif()
45 string(TOUPPER ${REQUIRE} UREQUIRE)
46 set(${UREQUIRE}_CFLAGS_OTHER ${${REQUIRE}_CFLAGS_OTHER} PARENT_SCOPE)
47 set(${UREQUIRE}_FOUND ${${REQUIRE}_FOUND} PARENT_SCOPE)
48 set(${UREQUIRE}_INCLUDE_DIRS ${${REQUIRE}_INCLUDE_DIRS} PARENT_SCOPE)
49 set(${UREQUIRE}_LIBRARIES ${${REQUIRE}_LIBRARIES} PARENT_SCOPE)
50 set(${UREQUIRE}_LIBRARY_DIRS ${${REQUIRE}_LIBRARY_DIRS} PARENT_SCOPE)
51 set(${UREQUIRE}_LDFLAGS_OTHER ${${REQUIRE}_LDFLAGS_OTHER} PARENT_SCOPE)
52 endforeach()
53endfunction()
54
55# Version information
56set(LIB_VERSION_MAJOR 0)
57set(LIB_VERSION_MINOR 1)
58set(LIB_VERSION_PATCH 0)
59set(LIB_VERSION_ABI 0.1)
60set(LIB_VERSION_STRING ${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}.${LIB_VERSION_PATCH})
61
62# Advanced settings
63set(BUILD_PGO CACHE BOOL "Build with profiling-guided optimization")
64set(PGO_STAGE_2 CACHE BOOL "Rebuild for profiling-guided optimization after profiles have been generated")
65set(PGO_DIR "/tmp/gba-pgo/" CACHE PATH "Profiling-guided optimization profiles path")
66mark_as_advanced(BUILD_PGO PGO_STAGE_2 PGO_DIR)
67set(PGO_PRE_FLAGS "-pg -fprofile-generate=${PGO_DIR} -fprofile-arcs")
68set(PGO_POST_FLAGS "-fprofile-use=${PGO_DIR} -fbranch-probabilities")
69
70if(BUILD_PGO AND NOT PGO_STAGE_2)
71 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_PRE_FLAGS}")
72 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
73 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_PRE_FLAGS}")
74elseif(BUILD_PGO AND PGO_STAGE_2)
75 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PGO_POST_FLAGS}")
76 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_POST_FLAGS}")
77 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_POST_FLAGS}")
78endif()
79
80add_definitions(-DBINARY_NAME="${BINARY_NAME}" -DPROJECT_NAME="${PROJECT_NAME}" -DPROJECT_VERSION="${LIB_VERSION_STRING}")
81
82# Feature dependencies
83find_feature(USE_CLI_DEBUGGER "libedit")
84find_feature(USE_FFMPEG "libavcodec;libavformat;libavutil")
85find_feature(USE_PNG "ZLIB;PNG")
86find_feature(USE_LIBZIP "libzip")
87
88include(CheckFunctionExists)
89check_function_exists(strndup HAVE_STRNDUP)
90
91if(HAVE_STRNDUP)
92 add_definitions(-DHAVE_STRNDUP)
93endif()
94
95# Platform support
96if(WIN32)
97 add_definitions(-D_WIN32_WINNT=0x0600)
98 list(APPEND OS_LIB Ws2_32)
99 file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/windows/*.c)
100 source_group("Windows-specific code" FILES ${OS_SRC})
101else()
102 add_definitions(-DUSE_PTHREADS)
103 list(APPEND OS_LIB pthread)
104 file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/posix/*.c)
105 source_group("POSIX-specific code" FILES ${OS_SRC})
106endif()
107
108if(BUILD_BBB OR BUILD_RASPI)
109 enable_language(ASM)
110 if(NOT BUILD_EGL)
111 add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
112 endif()
113endif()
114
115# Features
116set(DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/debugger.c ${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c)
117
118if(USE_CLI_DEBUGGER)
119 add_definitions(-DUSE_CLI_DEBUGGER)
120 list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c)
121 list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/parser.c)
122 include_directories(AFTER ${LIBEDIT_INCLUDE_DIRS})
123 link_directories(${LIBEDIT_LIBRARY_DIRS})
124 set(DEBUGGER_LIB ${LIBEDIT_LIBRARIES})
125else()
126 set(DEBUGGER_LIB "")
127endif()
128
129if(USE_GDB_STUB)
130 add_definitions(-DUSE_GDB_STUB)
131 list(APPEND DEBUGGER_SRC ${CMAKE_SOURCE_DIR}/src/debugger/gdb-stub.c)
132endif()
133source_group("ARM debugger" FILES ${DEBUGGER_SRC})
134
135if(USE_FFMPEG)
136 add_definitions(-DUSE_FFMPEG)
137 include_directories(AFTER ${LIBAVCODEC_INCLUDE_DIRS} ${LIBAVFORMAT_INCLUDE_DIRS} ${LIBAVUTIL_INCLUDE_DIRS})
138 link_directories(${LIBAVCODEC_LIBRARY_DIRS} ${LIBAVFORMAT_LIBRARY_DIRS} ${LIBAVUTIL_LIBRARY_DIRS})
139 list(APPEND UTIL_SRC "${CMAKE_SOURCE_DIR}/src/platform/ffmpeg/ffmpeg-encoder.c")
140 list(APPEND DEPENDENCY_LIB ${LIBAVCODEC_LIBRARIES} ${LIBAVFORMAT_LIBRARIES} ${LIBAVUTIL_LIBRARIES})
141endif()
142
143if(USE_PNG)
144 add_definitions(-DUSE_PNG)
145 include_directories(AFTER ${PNG_INCLUDE_DIRS})
146 list(APPEND DEPENDENCY_LIB ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
147endif()
148
149if(USE_LIBZIP)
150 include_directories(AFTER ${LIBZIP_INCLUDE_DIRS})
151 link_directories(${LIBZIP_LIBRARY_DIRS})
152 list(APPEND DEPENDENCY_LIB ${LIBZIP_LIBRARIES})
153 add_definitions(-DENABLE_LIBZIP)
154endif()
155
156# Binaries
157add_library(${BINARY_NAME} SHARED ${ARM_SRC} ${GBA_SRC} ${DEBUGGER_SRC} ${RENDERER_SRC} ${UTIL_SRC} ${VFS_SRC} ${OS_SRC})
158target_link_libraries(${BINARY_NAME} m ${DEBUGGER_LIB} ${OS_LIB} ${DEPENDENCY_LIB})
159install(TARGETS ${BINARY_NAME} DESTINATION lib)
160set_target_properties(${BINARY_NAME} PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_ABI})
161
162if(BUILD_SDL)
163 add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
164endif()
165
166if(BUILD_PERF)
167 set(PERF_SRC ${CMAKE_SOURCE_DIR}/src/platform/perf-main.c)
168 if(UNIX AND NOT APPLE)
169 list(APPEND PERF_LIB rt)
170 endif()
171
172 add_executable(${BINARY_NAME}-perf ${PERF_SRC})
173 target_link_libraries(${BINARY_NAME}-perf ${BINARY_NAME} ${PERF_LIB})
174 install(TARGETS ${BINARY_NAME}-perf DESTINATION bin)
175 install(FILES ${CMAKE_SOURCE_DIR}/tools/perf.py DESTINATION "${CMAKE_INSTALL_LIBDIR}/${BINARY_NAME}")
176endif()
177
178# Summaries
179message(STATUS "Feature summary:")
180message(STATUS " CLI debugger: ${USE_CLI_DEBUGGER}")
181message(STATUS " GDB stub: ${USE_GDB_STUB}")
182message(STATUS " Video recording: ${USE_FFMPEG}")
183message(STATUS " Screenshot/advanced savestate support: ${USE_PNG}")
184message(STATUS " ZIP support: ${USE_LIBZIP}")
185message(STATUS "Frontend summary:")
186message(STATUS " SDL (${SDL_VERSION}): ${BUILD_SDL}")
187message(STATUS " Profiling: ${BUILD_PERF}")