CMakeLists.txt 12 KB

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