CMakeLists.txt 13 KB

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