all repos — mgba @ 9c83462343100901461db1b1caae2fa434c4555f

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