CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # CMake 3.6 required for FindBoost to define IMPORTED libs properly on unknown Boost versions
  2. cmake_minimum_required(VERSION 3.6)
  3. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules")
  4. function(download_bundled_external remote_path lib_name prefix_var)
  5. set(prefix "${CMAKE_BINARY_DIR}/externals/${lib_name}")
  6. if (NOT EXISTS "${prefix}")
  7. message(STATUS "Downloading binaries for ${lib_name}...")
  8. file(DOWNLOAD
  9. https://github.com/citra-emu/ext-windows-bin/raw/master/${remote_path}${lib_name}.7z
  10. "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z" SHOW_PROGRESS)
  11. execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z"
  12. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/externals")
  13. endif()
  14. message(STATUS "Using bundled binaries at ${prefix}")
  15. set(${prefix_var} "${prefix}" PARENT_SCOPE)
  16. endfunction()
  17. include(CheckSymbolExists)
  18. function(detect_architecture symbol arch)
  19. if (NOT DEFINED ARCHITECTURE)
  20. set(CMAKE_REQUIRED_QUIET 1)
  21. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  22. unset(CMAKE_REQUIRED_QUIET)
  23. # The output variable needs to be unique across invocations otherwise
  24. # CMake's crazy scope rules will keep it defined
  25. if (ARCHITECTURE_${arch})
  26. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  27. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  28. add_definitions(-DARCHITECTURE_${arch}=1)
  29. endif()
  30. endif()
  31. endfunction()
  32. project(citra)
  33. option(ENABLE_SDL2 "Enable the SDL2 frontend" ON)
  34. option(CITRA_USE_BUNDLED_SDL2 "Download bundled SDL2 binaries" OFF)
  35. option(ENABLE_QT "Enable the Qt frontend" ON)
  36. option(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" OFF)
  37. if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit)
  38. message(STATUS "Copying pre-commit hook")
  39. file(COPY hooks/pre-commit
  40. DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks)
  41. endif()
  42. if (MSVC)
  43. detect_architecture("_M_AMD64" x86_64)
  44. detect_architecture("_M_IX86" x86)
  45. detect_architecture("_M_ARM" ARM)
  46. else()
  47. detect_architecture("__x86_64__" x86_64)
  48. detect_architecture("__i386__" x86)
  49. detect_architecture("__arm__" ARM)
  50. endif()
  51. if (NOT DEFINED ARCHITECTURE)
  52. set(ARCHITECTURE "GENERIC")
  53. set(ARCHITECTURE_GENERIC 1)
  54. add_definitions(-DARCHITECTURE_GENERIC=1)
  55. endif()
  56. message(STATUS "Target architecture: ${ARCHITECTURE}")
  57. set(CMAKE_CXX_STANDARD 14)
  58. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  59. if (NOT MSVC)
  60. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-attributes")
  61. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  62. if (MINGW)
  63. add_definitions(-DMINGW_HAS_SECURE_API)
  64. if (MINGW_STATIC_BUILD)
  65. add_definitions(-DQT_STATICPLUGIN)
  66. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
  67. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
  68. endif()
  69. endif()
  70. else()
  71. # Silence "deprecation" warnings
  72. add_definitions(/D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE /D_SCL_SECURE_NO_WARNINGS)
  73. # Avoid windows.h junk
  74. add_definitions(/DNOMINMAX)
  75. # set up output paths for executable binaries (.exe-files, and .dll-files on DLL-capable platforms)
  76. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  77. set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
  78. # Tweak optimization settings
  79. # As far as I can tell, there's no way to override the CMake defaults while leaving user
  80. # changes intact, so we'll just clobber everything and say sorry.
  81. message(STATUS "Cache compiler flags ignored, please edit CMakeLists.txt to change the flags.")
  82. # /W3 - Level 3 warnings
  83. # /MP - Multi-threaded compilation
  84. # /Zi - Output debugging information
  85. # /Zo - enahnced debug info for optimized builds
  86. set(CMAKE_C_FLAGS "/W3 /MP /Zi /Zo" CACHE STRING "" FORCE)
  87. # /EHsc - C++-only exception handling semantics
  88. set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} /EHsc" CACHE STRING "" FORCE)
  89. # /MDd - Multi-threaded Debug Runtime DLL
  90. set(CMAKE_C_FLAGS_DEBUG "/Od /MDd" CACHE STRING "" FORCE)
  91. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING "" FORCE)
  92. # /O2 - Optimization level 2
  93. # /GS- - No stack buffer overflow checks
  94. # /MD - Multi-threaded runtime DLL
  95. set(CMAKE_C_FLAGS_RELEASE "/O2 /GS- /MD" CACHE STRING "" FORCE)
  96. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
  97. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG" CACHE STRING "" FORCE)
  98. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/DEBUG /INCREMENTAL:NO /OPT:REF,ICF" CACHE STRING "" FORCE)
  99. endif()
  100. # Set file offset size to 64 bits.
  101. #
  102. # On modern Unixes, this is typically already the case. The lone exception is
  103. # glibc, which may default to 32 bits. glibc allows this to be configured
  104. # by setting _FILE_OFFSET_BITS.
  105. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  106. add_definitions(-D_FILE_OFFSET_BITS=64)
  107. endif()
  108. add_definitions(-DSINGLETHREADED)
  109. # CMake seems to only define _DEBUG on Windows
  110. set_property(DIRECTORY APPEND PROPERTY
  111. COMPILE_DEFINITIONS $<$<CONFIG:Debug>:_DEBUG> $<$<NOT:$<CONFIG:Debug>>:NDEBUG>)
  112. find_package(PNG QUIET)
  113. if (NOT PNG_FOUND)
  114. message(STATUS "libpng not found. Some debugging features have been disabled.")
  115. endif()
  116. find_package(Boost 1.63.0 QUIET)
  117. if (NOT Boost_FOUND)
  118. message(STATUS "Boost 1.63.0 or newer not found, falling back to externals")
  119. set(BOOST_ROOT "${CMAKE_SOURCE_DIR}/externals/boost")
  120. set(Boost_NO_SYSTEM_PATHS OFF)
  121. find_package(Boost QUIET REQUIRED)
  122. endif()
  123. # Include bundled CMake modules
  124. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/externals/cmake-modules")
  125. # Prefer the -pthread flag on Linux.
  126. set (THREADS_PREFER_PTHREAD_FLAG ON)
  127. find_package(Threads REQUIRED)
  128. if (ENABLE_SDL2)
  129. if (CITRA_USE_BUNDLED_SDL2)
  130. # Detect toolchain and platform
  131. if (MSVC14 AND ARCHITECTURE_x86_64)
  132. set(SDL2_VER "SDL2-2.0.5")
  133. else()
  134. message(FATAL_ERROR "No bundled SDL2 binaries for your toolchain. Disable CITRA_USE_BUNDLED_SDL2 and provide your own.")
  135. endif()
  136. if (DEFINED SDL2_VER)
  137. download_bundled_external("sdl2/" ${SDL2_VER} SDL2_PREFIX)
  138. endif()
  139. set(SDL2_FOUND YES)
  140. set(SDL2_INCLUDE_DIR "${SDL2_PREFIX}/include" CACHE PATH "Path to SDL2 headers")
  141. set(SDL2_LIBRARY "${SDL2_PREFIX}/lib/x64/SDL2.lib" CACHE PATH "Path to SDL2 library")
  142. set(SDL2_DLL_DIR "${SDL2_PREFIX}/lib/x64/" CACHE PATH "Path to SDL2.dll")
  143. else()
  144. find_package(SDL2 REQUIRED)
  145. endif()
  146. else()
  147. set(SDL2_FOUND NO)
  148. endif()
  149. IF (APPLE)
  150. FIND_LIBRARY(COCOA_LIBRARY Cocoa) # Umbrella framework for everything GUI-related
  151. set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  152. if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  153. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  154. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
  155. endif()
  156. ELSEIF (WIN32)
  157. # WSAPoll and SHGetKnownFolderPath (AppData/Roaming) didn't exist before WinNT 6.x (Vista)
  158. add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600)
  159. set(PLATFORM_LIBRARIES winmm ws2_32)
  160. IF (MINGW)
  161. # PSAPI is the Process Status API
  162. set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} psapi imm32 version)
  163. ENDIF (MINGW)
  164. ELSEIF (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
  165. set(PLATFORM_LIBRARIES rt)
  166. ENDIF (APPLE)
  167. # MINGW: GCC does not support codecvt, so use iconv instead
  168. if (UNIX OR MINGW)
  169. find_library(ICONV_LIBRARY NAMES iconv)
  170. if (ICONV_LIBRARY)
  171. list(APPEND PLATFORM_LIBRARIES ${ICONV_LIBRARY})
  172. endif()
  173. endif()
  174. if (ENABLE_QT)
  175. if (CITRA_USE_BUNDLED_QT)
  176. if (MSVC14 AND ARCHITECTURE_x86_64)
  177. set(QT_VER qt-5.7-msvc2015_64)
  178. else()
  179. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable CITRA_USE_BUNDLED_QT and provide your own.")
  180. endif()
  181. if (DEFINED QT_VER)
  182. download_bundled_external("qt/" ${QT_VER} QT_PREFIX)
  183. endif()
  184. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  185. else()
  186. # Passing an empty HINTS seems to cause default system paths to get ignored in CMake 2.8 so
  187. # make sure to not pass anything if we don't have one.
  188. set(QT_PREFIX_HINT)
  189. endif()
  190. find_package(Qt5 REQUIRED COMPONENTS Widgets OpenGL ${QT_PREFIX_HINT})
  191. set(CITRA_QT_LIBS Qt5::Widgets Qt5::OpenGL)
  192. endif()
  193. # This function should be passed a list of all files in a target. It will automatically generate
  194. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  195. # one in the filesystem.
  196. function(create_directory_groups)
  197. # Place any files that aren't in the source list in a separate group so that they don't get in
  198. # the way.
  199. source_group("Other Files" REGULAR_EXPRESSION ".")
  200. foreach(file_name ${ARGV})
  201. get_filename_component(dir_name "${file_name}" PATH)
  202. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  203. string(REPLACE "/" "\\" group_name "${dir_name}")
  204. source_group("${group_name}" FILES "${file_name}")
  205. endforeach()
  206. endfunction()
  207. # generate git revision information
  208. include(GetGitRevisionDescription)
  209. get_git_head_revision(GIT_REF_SPEC GIT_REV)
  210. git_describe(GIT_DESC --always --long --dirty)
  211. git_branch_name(GIT_BRANCH)
  212. set(INI_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/externals/inih")
  213. include_directories(${INI_PREFIX})
  214. add_subdirectory(${INI_PREFIX})
  215. add_subdirectory(externals)
  216. option(DYNARMIC_TESTS OFF)
  217. set(DYNARMIC_NO_BUNDLED_FMT ON)
  218. add_subdirectory(externals/dynarmic)
  219. add_subdirectory(externals/glad)
  220. include_directories(externals/microprofile)
  221. include_directories(externals/nihstro/include)
  222. if (MSVC)
  223. add_subdirectory(externals/getopt)
  224. endif()
  225. # process subdirectories
  226. add_subdirectory(externals/soundtouch)
  227. enable_testing()
  228. add_subdirectory(src)
  229. # Install freedesktop.org metadata files, following those specifications:
  230. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  231. # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  232. # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  233. if(ENABLE_QT AND UNIX AND NOT APPLE)
  234. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.desktop"
  235. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
  236. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.svg"
  237. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/pixmaps")
  238. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.xml"
  239. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/mime/packages")
  240. endif()
  241. if(UNIX)
  242. if(ENABLE_SDL2)
  243. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.6"
  244. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/man/man6")
  245. endif()
  246. if (ENABLE_QT)
  247. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra-qt.6"
  248. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/man/man6")
  249. endif()
  250. endif()