CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
  4. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/externals/cmake-modules")
  5. include(DownloadExternals)
  6. project(citra)
  7. option(ENABLE_SDL2 "Enable the SDL2 frontend" ON)
  8. option(CITRA_USE_BUNDLED_SDL2 "Download bundled SDL2 binaries" OFF)
  9. option(ENABLE_QT "Enable the Qt frontend" ON)
  10. option(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" OFF)
  11. option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
  12. option(CITRA_USE_BUNDLED_CURL "FOR MINGW ONLY: Download curl configured against winssl instead of openssl" OFF)
  13. if (ENABLE_WEB_SERVICE AND CITRA_USE_BUNDLED_CURL AND WINDOWS AND MSVC)
  14. message("Turning off use bundled curl as msvc can compile curl on cpr")
  15. SET(CITRA_USE_BUNDLED_CURL OFF CACHE BOOL "" FORCE)
  16. endif()
  17. if (ENABLE_WEB_SERVICE AND NOT CITRA_USE_BUNDLED_CURL AND MINGW)
  18. message(AUTHOR_WARNING "Turning on CITRA_USE_BUNDLED_CURL. Override it only if you know what you are doing.")
  19. SET(CITRA_USE_BUNDLED_CURL ON CACHE BOOL "" FORCE)
  20. endif()
  21. if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
  22. message(STATUS "Copying pre-commit hook")
  23. file(COPY hooks/pre-commit
  24. DESTINATION ${CMAKE_SOURCE_DIR}/.git/hooks)
  25. endif()
  26. # Sanity check : Check that all submodules are present
  27. # =======================================================================
  28. function(check_submodules_present)
  29. file(READ "${CMAKE_SOURCE_DIR}/.gitmodules" gitmodules)
  30. string(REGEX MATCHALL "path *= *[^ \t\r\n]*" gitmodules ${gitmodules})
  31. foreach(module ${gitmodules})
  32. string(REGEX REPLACE "path *= *" "" module ${module})
  33. if (NOT EXISTS "${CMAKE_SOURCE_DIR}/${module}/.git")
  34. message(SEND_ERROR "Git submodule ${module} not found."
  35. "Please run: git submodule update --init --recursive")
  36. endif()
  37. endforeach()
  38. endfunction()
  39. check_submodules_present()
  40. # Detect current compilation architecture and create standard definitions
  41. # =======================================================================
  42. include(CheckSymbolExists)
  43. function(detect_architecture symbol arch)
  44. if (NOT DEFINED ARCHITECTURE)
  45. set(CMAKE_REQUIRED_QUIET 1)
  46. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  47. unset(CMAKE_REQUIRED_QUIET)
  48. # The output variable needs to be unique across invocations otherwise
  49. # CMake's crazy scope rules will keep it defined
  50. if (ARCHITECTURE_${arch})
  51. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  52. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  53. add_definitions(-DARCHITECTURE_${arch}=1)
  54. endif()
  55. endif()
  56. endfunction()
  57. if (MSVC)
  58. detect_architecture("_M_AMD64" x86_64)
  59. detect_architecture("_M_IX86" x86)
  60. detect_architecture("_M_ARM" ARM)
  61. else()
  62. detect_architecture("__x86_64__" x86_64)
  63. detect_architecture("__i386__" x86)
  64. detect_architecture("__arm__" ARM)
  65. endif()
  66. if (NOT DEFINED ARCHITECTURE)
  67. set(ARCHITECTURE "GENERIC")
  68. set(ARCHITECTURE_GENERIC 1)
  69. add_definitions(-DARCHITECTURE_GENERIC=1)
  70. endif()
  71. message(STATUS "Target architecture: ${ARCHITECTURE}")
  72. # Configure compilation flags
  73. # ===========================
  74. set(CMAKE_CXX_STANDARD 14)
  75. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  76. if (NOT MSVC)
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-attributes")
  78. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  79. if (MINGW)
  80. add_definitions(-DMINGW_HAS_SECURE_API)
  81. if (MINGW_STATIC_BUILD)
  82. add_definitions(-DQT_STATICPLUGIN)
  83. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
  84. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
  85. endif()
  86. endif()
  87. else()
  88. # Silence "deprecation" warnings
  89. add_definitions(/D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE /D_SCL_SECURE_NO_WARNINGS)
  90. # Avoid windows.h junk
  91. add_definitions(/DNOMINMAX)
  92. # Avoid windows.h from including some usually unused libs like winsocks.h, since this might cause some redefinition errors.
  93. add_definitions(/DWIN32_LEAN_AND_MEAN)
  94. # set up output paths for executable binaries (.exe-files, and .dll-files on DLL-capable platforms)
  95. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  96. set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
  97. # Tweak optimization settings
  98. # As far as I can tell, there's no way to override the CMake defaults while leaving user
  99. # changes intact, so we'll just clobber everything and say sorry.
  100. message(STATUS "Cache compiler flags ignored, please edit CMakeLists.txt to change the flags.")
  101. # /W3 - Level 3 warnings
  102. # /MP - Multi-threaded compilation
  103. # /Zi - Output debugging information
  104. # /Zo - enhanced debug info for optimized builds
  105. # /permissive- - enables stricter C++ standards conformance checks
  106. set(CMAKE_C_FLAGS "/W3 /MP /Zi /Zo /permissive-" CACHE STRING "" FORCE)
  107. # /EHsc - C++-only exception handling semantics
  108. # /Zc:throwingNew - let codegen assume `operator new` will never return null
  109. # /Zc:inline - let codegen omit inline functions in object files
  110. set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} /EHsc /Zc:throwingNew,inline" CACHE STRING "" FORCE)
  111. # /MDd - Multi-threaded Debug Runtime DLL
  112. set(CMAKE_C_FLAGS_DEBUG "/Od /MDd" CACHE STRING "" FORCE)
  113. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING "" FORCE)
  114. # /O2 - Optimization level 2
  115. # /GS- - No stack buffer overflow checks
  116. # /MD - Multi-threaded runtime DLL
  117. set(CMAKE_C_FLAGS_RELEASE "/O2 /GS- /MD" CACHE STRING "" FORCE)
  118. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
  119. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG /MANIFEST:NO" CACHE STRING "" FORCE)
  120. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/DEBUG /MANIFEST:NO /INCREMENTAL:NO /OPT:REF,ICF" CACHE STRING "" FORCE)
  121. endif()
  122. # Set file offset size to 64 bits.
  123. #
  124. # On modern Unixes, this is typically already the case. The lone exception is
  125. # glibc, which may default to 32 bits. glibc allows this to be configured
  126. # by setting _FILE_OFFSET_BITS.
  127. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  128. add_definitions(-D_FILE_OFFSET_BITS=64)
  129. endif()
  130. add_definitions(-DSINGLETHREADED)
  131. # CMake seems to only define _DEBUG on Windows
  132. set_property(DIRECTORY APPEND PROPERTY
  133. COMPILE_DEFINITIONS $<$<CONFIG:Debug>:_DEBUG> $<$<NOT:$<CONFIG:Debug>>:NDEBUG>)
  134. # System imported libraries
  135. # ======================
  136. find_package(PNG QUIET)
  137. if (NOT PNG_FOUND)
  138. message(STATUS "libpng not found. Some debugging features have been disabled.")
  139. endif()
  140. find_package(Boost 1.63.0 QUIET)
  141. if (NOT Boost_FOUND)
  142. message(STATUS "Boost 1.63.0 or newer not found, falling back to externals")
  143. set(BOOST_ROOT "${CMAKE_SOURCE_DIR}/externals/boost")
  144. set(Boost_NO_SYSTEM_PATHS OFF)
  145. find_package(Boost QUIET REQUIRED)
  146. endif()
  147. # Prefer the -pthread flag on Linux.
  148. set(THREADS_PREFER_PTHREAD_FLAG ON)
  149. find_package(Threads REQUIRED)
  150. if (ENABLE_SDL2)
  151. if (CITRA_USE_BUNDLED_SDL2)
  152. # Detect toolchain and platform
  153. if (MSVC14 AND ARCHITECTURE_x86_64)
  154. set(SDL2_VER "SDL2-2.0.5")
  155. else()
  156. message(FATAL_ERROR "No bundled SDL2 binaries for your toolchain. Disable CITRA_USE_BUNDLED_SDL2 and provide your own.")
  157. endif()
  158. if (DEFINED SDL2_VER)
  159. download_bundled_external("sdl2/" ${SDL2_VER} SDL2_PREFIX)
  160. endif()
  161. set(SDL2_FOUND YES)
  162. set(SDL2_INCLUDE_DIR "${SDL2_PREFIX}/include" CACHE PATH "Path to SDL2 headers")
  163. set(SDL2_LIBRARY "${SDL2_PREFIX}/lib/x64/SDL2.lib" CACHE PATH "Path to SDL2 library")
  164. set(SDL2_DLL_DIR "${SDL2_PREFIX}/lib/x64/" CACHE PATH "Path to SDL2.dll")
  165. else()
  166. find_package(SDL2 REQUIRED)
  167. endif()
  168. if (SDL2_FOUND)
  169. # TODO(yuriks): Make FindSDL2.cmake export an IMPORTED library instead
  170. add_library(SDL2 INTERFACE)
  171. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARY}")
  172. target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}")
  173. endif()
  174. else()
  175. set(SDL2_FOUND NO)
  176. endif()
  177. if (ENABLE_QT)
  178. if (CITRA_USE_BUNDLED_QT)
  179. if (MSVC14 AND ARCHITECTURE_x86_64)
  180. set(QT_VER qt-5.7-msvc2015_64)
  181. else()
  182. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable CITRA_USE_BUNDLED_QT and provide your own.")
  183. endif()
  184. if (DEFINED QT_VER)
  185. download_bundled_external("qt/" ${QT_VER} QT_PREFIX)
  186. endif()
  187. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  188. else()
  189. # Passing an empty HINTS seems to cause default system paths to get ignored in CMake 2.8 so
  190. # make sure to not pass anything if we don't have one.
  191. set(QT_PREFIX_HINT)
  192. endif()
  193. find_package(Qt5 REQUIRED COMPONENTS Widgets OpenGL ${QT_PREFIX_HINT})
  194. endif()
  195. if (ENABLE_WEB_SERVICE)
  196. add_definitions(-DENABLE_WEB_SERVICE)
  197. endif()
  198. # Platform-specific library requirements
  199. # ======================================
  200. IF (APPLE)
  201. FIND_LIBRARY(COCOA_LIBRARY Cocoa) # Umbrella framework for everything GUI-related
  202. set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  203. if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  204. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  205. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
  206. endif()
  207. ELSEIF (WIN32)
  208. # WSAPoll and SHGetKnownFolderPath (AppData/Roaming) didn't exist before WinNT 6.x (Vista)
  209. add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600)
  210. set(PLATFORM_LIBRARIES winmm ws2_32)
  211. IF (MINGW)
  212. # PSAPI is the Process Status API
  213. set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} psapi imm32 version)
  214. ENDIF (MINGW)
  215. ELSEIF (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
  216. set(PLATFORM_LIBRARIES rt)
  217. ENDIF (APPLE)
  218. # MINGW: GCC does not support codecvt, so use iconv instead
  219. if (UNIX OR MINGW)
  220. find_library(ICONV_LIBRARY NAMES iconv)
  221. if (ICONV_LIBRARY)
  222. list(APPEND PLATFORM_LIBRARIES ${ICONV_LIBRARY})
  223. endif()
  224. endif()
  225. # Include source code
  226. # ===================
  227. # This function should be passed a list of all files in a target. It will automatically generate
  228. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  229. # one in the filesystem.
  230. function(create_directory_groups)
  231. # Place any files that aren't in the source list in a separate group so that they don't get in
  232. # the way.
  233. source_group("Other Files" REGULAR_EXPRESSION ".")
  234. foreach(file_name ${ARGV})
  235. get_filename_component(dir_name "${file_name}" PATH)
  236. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  237. string(REPLACE "/" "\\" group_name "${dir_name}")
  238. source_group("${group_name}" FILES "${file_name}")
  239. endforeach()
  240. endfunction()
  241. # Gets a UTC timstamp and sets the provided variable to it
  242. function(get_timestamp _var)
  243. string(TIMESTAMP timestamp UTC)
  244. set(${_var} "${timestamp}" PARENT_SCOPE)
  245. endfunction()
  246. # generate git/build information
  247. include(GetGitRevisionDescription)
  248. get_git_head_revision(GIT_REF_SPEC GIT_REV)
  249. git_describe(GIT_DESC --always --long --dirty)
  250. git_branch_name(GIT_BRANCH)
  251. get_timestamp(BUILD_DATE)
  252. enable_testing()
  253. add_subdirectory(externals)
  254. add_subdirectory(src)
  255. # Installation instructions
  256. # =========================
  257. # Install freedesktop.org metadata files, following those specifications:
  258. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  259. # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  260. # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  261. if(ENABLE_QT AND UNIX AND NOT APPLE)
  262. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.desktop"
  263. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
  264. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.svg"
  265. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/pixmaps")
  266. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.xml"
  267. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/mime/packages")
  268. endif()
  269. if(UNIX)
  270. if(ENABLE_SDL2)
  271. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.6"
  272. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/man/man6")
  273. endif()
  274. if (ENABLE_QT)
  275. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra-qt.6"
  276. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/man/man6")
  277. endif()
  278. endif()