all repos — mgba @ a77d6e202d50880507d6dd4f5f1113be45050a1f

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