CMakeLists.txt 12 KB

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