CMakeLists.txt 11 KB

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