CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. cmake_minimum_required(VERSION 3.7)
  2. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
  3. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/externals/cmake-modules")
  4. include(DownloadExternals)
  5. include(CMakeDependentOption)
  6. project(yuzu)
  7. # Set bundled sdl2/qt as dependent options.
  8. # OFF by default, but if ENABLE_SDL2 and MSVC are true then ON
  9. option(ENABLE_SDL2 "Enable the SDL2 frontend" ON)
  10. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_SDL2 "Download bundled SDL2 binaries" ON "ENABLE_SDL2;MSVC" OFF)
  11. option(ENABLE_QT "Enable the Qt frontend" ON)
  12. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_QT "Download bundled Qt binaries" ON "ENABLE_QT;MSVC" OFF)
  13. option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
  14. option(YUZU_USE_BUNDLED_UNICORN "Build/Download bundled Unicorn" ON)
  15. option(YUZU_USE_QT_WEB_ENGINE "Use QtWebEngine for web applet implementation" OFF)
  16. option(YUZU_ENABLE_BOXCAT "Enable the Boxcat service, a yuzu high-level implementation of BCAT" ON)
  17. option(ENABLE_CUBEB "Enables the cubeb audio backend" ON)
  18. option(ENABLE_VULKAN "Enables Vulkan backend" ON)
  19. option(USE_DISCORD_PRESENCE "Enables Discord Rich Presence" OFF)
  20. if(EXISTS ${PROJECT_SOURCE_DIR}/hooks/pre-commit AND NOT EXISTS ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit)
  21. message(STATUS "Copying pre-commit hook")
  22. file(COPY hooks/pre-commit
  23. DESTINATION ${PROJECT_SOURCE_DIR}/.git/hooks)
  24. endif()
  25. # Sanity check : Check that all submodules are present
  26. # =======================================================================
  27. function(check_submodules_present)
  28. file(READ "${PROJECT_SOURCE_DIR}/.gitmodules" gitmodules)
  29. string(REGEX MATCHALL "path *= *[^ \t\r\n]*" gitmodules ${gitmodules})
  30. foreach(module ${gitmodules})
  31. string(REGEX REPLACE "path *= *" "" module ${module})
  32. if (NOT EXISTS "${PROJECT_SOURCE_DIR}/${module}/.git")
  33. message(FATAL_ERROR "Git submodule ${module} not found. "
  34. "Please run: git submodule update --init --recursive")
  35. endif()
  36. endforeach()
  37. endfunction()
  38. if(EXISTS ${PROJECT_SOURCE_DIR}/.gitmodules)
  39. check_submodules_present()
  40. endif()
  41. configure_file(${PROJECT_SOURCE_DIR}/dist/compatibility_list/compatibility_list.qrc
  42. ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.qrc
  43. COPYONLY)
  44. if (ENABLE_COMPATIBILITY_LIST_DOWNLOAD AND NOT EXISTS ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json)
  45. message(STATUS "Downloading compatibility list for yuzu...")
  46. file(DOWNLOAD
  47. https://api.yuzu-emu.org/gamedb/
  48. "${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json" SHOW_PROGRESS)
  49. endif()
  50. if (NOT EXISTS ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json)
  51. file(WRITE ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json "")
  52. endif()
  53. # Detect current compilation architecture and create standard definitions
  54. # =======================================================================
  55. include(CheckSymbolExists)
  56. function(detect_architecture symbol arch)
  57. if (NOT DEFINED ARCHITECTURE)
  58. set(CMAKE_REQUIRED_QUIET 1)
  59. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  60. unset(CMAKE_REQUIRED_QUIET)
  61. # The output variable needs to be unique across invocations otherwise
  62. # CMake's crazy scope rules will keep it defined
  63. if (ARCHITECTURE_${arch})
  64. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  65. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  66. add_definitions(-DARCHITECTURE_${arch}=1)
  67. endif()
  68. endif()
  69. endfunction()
  70. if (NOT ENABLE_GENERIC)
  71. if (MSVC)
  72. detect_architecture("_M_AMD64" x86_64)
  73. detect_architecture("_M_IX86" x86)
  74. detect_architecture("_M_ARM" ARM)
  75. detect_architecture("_M_ARM64" ARM64)
  76. else()
  77. detect_architecture("__x86_64__" x86_64)
  78. detect_architecture("__i386__" x86)
  79. detect_architecture("__arm__" ARM)
  80. detect_architecture("__aarch64__" ARM64)
  81. endif()
  82. endif()
  83. if (NOT DEFINED ARCHITECTURE)
  84. set(ARCHITECTURE "GENERIC")
  85. set(ARCHITECTURE_GENERIC 1)
  86. add_definitions(-DARCHITECTURE_GENERIC=1)
  87. endif()
  88. message(STATUS "Target architecture: ${ARCHITECTURE}")
  89. # Configure C++ standard
  90. # ===========================
  91. set(CMAKE_CXX_STANDARD 17)
  92. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  93. # System imported libraries
  94. # ======================
  95. find_package(Boost 1.66.0 QUIET)
  96. if (NOT Boost_FOUND)
  97. message(STATUS "Boost 1.66.0 or newer not found, falling back to externals")
  98. set(BOOST_ROOT "${PROJECT_SOURCE_DIR}/externals/boost")
  99. set(Boost_NO_SYSTEM_PATHS OFF)
  100. find_package(Boost QUIET REQUIRED)
  101. endif()
  102. # Output binaries to bin/
  103. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
  104. # Prefer the -pthread flag on Linux.
  105. set(THREADS_PREFER_PTHREAD_FLAG ON)
  106. find_package(Threads REQUIRED)
  107. if (ENABLE_SDL2)
  108. if (YUZU_USE_BUNDLED_SDL2)
  109. # Detect toolchain and platform
  110. if ((MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1930) AND ARCHITECTURE_x86_64)
  111. set(SDL2_VER "SDL2-2.0.8")
  112. else()
  113. message(FATAL_ERROR "No bundled SDL2 binaries for your toolchain. Disable YUZU_USE_BUNDLED_SDL2 and provide your own.")
  114. endif()
  115. if (DEFINED SDL2_VER)
  116. download_bundled_external("sdl2/" ${SDL2_VER} SDL2_PREFIX)
  117. endif()
  118. set(SDL2_FOUND YES)
  119. set(SDL2_INCLUDE_DIR "${SDL2_PREFIX}/include" CACHE PATH "Path to SDL2 headers")
  120. set(SDL2_LIBRARY "${SDL2_PREFIX}/lib/x64/SDL2.lib" CACHE PATH "Path to SDL2 library")
  121. set(SDL2_DLL_DIR "${SDL2_PREFIX}/lib/x64/" CACHE PATH "Path to SDL2.dll")
  122. add_library(SDL2 INTERFACE)
  123. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARY}")
  124. target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}")
  125. else()
  126. find_package(SDL2 REQUIRED)
  127. # Some installations don't set SDL2_LIBRARIES
  128. if("${SDL2_LIBRARIES}" STREQUAL "")
  129. message(WARNING "SDL2_LIBRARIES wasn't set, manually setting to SDL2::SDL2")
  130. set(SDL2_LIBRARIES "SDL2::SDL2")
  131. endif()
  132. include_directories(${SDL2_INCLUDE_DIRS})
  133. add_library(SDL2 INTERFACE)
  134. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}")
  135. endif()
  136. else()
  137. set(SDL2_FOUND NO)
  138. endif()
  139. # If unicorn isn't found, msvc -> download bundled unicorn; everyone else -> build external
  140. if (YUZU_USE_BUNDLED_UNICORN)
  141. if (MSVC)
  142. message(STATUS "unicorn not found, falling back to bundled")
  143. # Detect toolchain and platform
  144. if ((MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1930) AND ARCHITECTURE_x86_64)
  145. set(UNICORN_VER "unicorn-yuzu")
  146. else()
  147. message(FATAL_ERROR "No bundled Unicorn binaries for your toolchain. Disable YUZU_USE_BUNDLED_UNICORN and provide your own.")
  148. endif()
  149. if (DEFINED UNICORN_VER)
  150. download_bundled_external("unicorn/" ${UNICORN_VER} UNICORN_PREFIX)
  151. endif()
  152. if (DEFINED UNICORN_VER)
  153. download_bundled_external("unicorn/" ${UNICORN_VER} UNICORN_PREFIX)
  154. endif()
  155. set(UNICORN_FOUND YES)
  156. set(LIBUNICORN_INCLUDE_DIR "${UNICORN_PREFIX}/include" CACHE PATH "Path to Unicorn headers" FORCE)
  157. set(LIBUNICORN_LIBRARY "${UNICORN_PREFIX}/lib/x64/unicorn_dynload.lib" CACHE PATH "Path to Unicorn library" FORCE)
  158. set(UNICORN_DLL_DIR "${UNICORN_PREFIX}/lib/x64/" CACHE PATH "Path to unicorn.dll" FORCE)
  159. else()
  160. message(STATUS "unicorn not found, falling back to externals")
  161. if (MINGW)
  162. set(UNICORN_LIB_NAME "unicorn.a")
  163. else()
  164. set(UNICORN_LIB_NAME "libunicorn.a")
  165. endif()
  166. set(UNICORN_FOUND YES)
  167. set(UNICORN_PREFIX ${PROJECT_SOURCE_DIR}/externals/unicorn)
  168. set(LIBUNICORN_LIBRARY "${UNICORN_PREFIX}/${UNICORN_LIB_NAME}" CACHE PATH "Path to Unicorn library" FORCE)
  169. set(LIBUNICORN_INCLUDE_DIR "${UNICORN_PREFIX}/include" CACHE PATH "Path to Unicorn headers" FORCE)
  170. set(UNICORN_DLL_DIR "${UNICORN_PREFIX}/" CACHE PATH "Path to unicorn dynamic library" FORCE)
  171. find_package(PythonInterp 2.7 REQUIRED)
  172. if (MINGW)
  173. add_custom_command(OUTPUT ${LIBUNICORN_LIBRARY}
  174. COMMAND ${CMAKE_COMMAND} -E env UNICORN_ARCHS="aarch64" PYTHON="${PYTHON_EXECUTABLE}" /bin/sh make.sh cross-win64
  175. WORKING_DIRECTORY ${UNICORN_PREFIX}
  176. )
  177. else()
  178. add_custom_command(OUTPUT ${LIBUNICORN_LIBRARY}
  179. COMMAND ${CMAKE_COMMAND} -E env UNICORN_ARCHS="aarch64" PYTHON="${PYTHON_EXECUTABLE}" /bin/sh make.sh macos-universal-no
  180. WORKING_DIRECTORY ${UNICORN_PREFIX}
  181. )
  182. endif()
  183. # ALL makes this custom target build every time
  184. # but it won't actually build if LIBUNICORN_LIBRARY is up to date
  185. add_custom_target(unicorn-build ALL
  186. DEPENDS ${LIBUNICORN_LIBRARY}
  187. )
  188. unset(UNICORN_LIB_NAME)
  189. endif()
  190. else()
  191. find_package(Unicorn REQUIRED)
  192. endif()
  193. if (UNICORN_FOUND)
  194. add_library(unicorn INTERFACE)
  195. add_dependencies(unicorn unicorn-build)
  196. target_link_libraries(unicorn INTERFACE "${LIBUNICORN_LIBRARY}")
  197. target_include_directories(unicorn INTERFACE "${LIBUNICORN_INCLUDE_DIR}")
  198. else()
  199. message(FATAL_ERROR "Could not find or build unicorn which is required.")
  200. endif()
  201. if (ENABLE_QT)
  202. if (YUZU_USE_BUNDLED_QT)
  203. if ((MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1930) AND ARCHITECTURE_x86_64)
  204. set(QT_VER qt-5.12.0-msvc2017_64)
  205. else()
  206. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable YUZU_USE_BUNDLED_QT and provide your own.")
  207. endif()
  208. if (DEFINED QT_VER)
  209. download_bundled_external("qt/" ${QT_VER} QT_PREFIX)
  210. endif()
  211. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  212. else()
  213. # Passing an empty HINTS seems to cause default system paths to get ignored in CMake 2.8 so
  214. # make sure to not pass anything if we don't have one.
  215. set(QT_PREFIX_HINT)
  216. endif()
  217. find_package(Qt5 REQUIRED COMPONENTS Widgets OpenGL ${QT_PREFIX_HINT})
  218. if (YUZU_USE_QT_WEB_ENGINE)
  219. find_package(Qt5 REQUIRED COMPONENTS WebEngineCore WebEngineWidgets ${QT_PREFIX_HINT})
  220. endif ()
  221. endif()
  222. # Platform-specific library requirements
  223. # ======================================
  224. if (APPLE)
  225. # Umbrella framework for everything GUI-related
  226. find_library(COCOA_LIBRARY Cocoa)
  227. set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  228. elseif (WIN32)
  229. # WSAPoll and SHGetKnownFolderPath (AppData/Roaming) didn't exist before WinNT 6.x (Vista)
  230. add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600)
  231. set(PLATFORM_LIBRARIES winmm ws2_32)
  232. if (MINGW)
  233. # PSAPI is the Process Status API
  234. set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} psapi imm32 version)
  235. endif()
  236. elseif (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
  237. set(PLATFORM_LIBRARIES rt)
  238. endif()
  239. # Setup a custom clang-format target (if clang-format can be found) that will run
  240. # against all the src files. This should be used before making a pull request.
  241. # =======================================================================
  242. set(CLANG_FORMAT_POSTFIX "-6.0")
  243. find_program(CLANG_FORMAT
  244. NAMES clang-format${CLANG_FORMAT_POSTFIX}
  245. clang-format
  246. PATHS ${PROJECT_BINARY_DIR}/externals)
  247. # if find_program doesn't find it, try to download from externals
  248. if (NOT CLANG_FORMAT)
  249. if (WIN32)
  250. message(STATUS "Clang format not found! Downloading...")
  251. set(CLANG_FORMAT "${PROJECT_BINARY_DIR}/externals/clang-format${CLANG_FORMAT_POSTFIX}.exe")
  252. file(DOWNLOAD
  253. https://github.com/yuzu-emu/ext-windows-bin/raw/master/clang-format${CLANG_FORMAT_POSTFIX}.exe
  254. "${CLANG_FORMAT}" SHOW_PROGRESS
  255. STATUS DOWNLOAD_SUCCESS)
  256. if (NOT DOWNLOAD_SUCCESS EQUAL 0)
  257. message(WARNING "Could not download clang format! Disabling the clang format target")
  258. file(REMOVE ${CLANG_FORMAT})
  259. unset(CLANG_FORMAT)
  260. endif()
  261. else()
  262. message(WARNING "Clang format not found! Disabling the clang format target")
  263. endif()
  264. endif()
  265. if (CLANG_FORMAT)
  266. set(SRCS ${PROJECT_SOURCE_DIR}/src)
  267. set(CCOMMENT "Running clang format against all the .h and .cpp files in src/")
  268. if (WIN32)
  269. add_custom_target(clang-format
  270. COMMAND powershell.exe -Command "Get-ChildItem '${SRCS}/*' -Include *.cpp,*.h -Recurse | Foreach {&'${CLANG_FORMAT}' -i $_.fullname}"
  271. COMMENT ${CCOMMENT})
  272. elseif(MINGW)
  273. add_custom_target(clang-format
  274. COMMAND find `cygpath -u ${SRCS}` -iname *.h -o -iname *.cpp | xargs `cygpath -u ${CLANG_FORMAT}` -i
  275. COMMENT ${CCOMMENT})
  276. else()
  277. add_custom_target(clang-format
  278. COMMAND find ${SRCS} -iname *.h -o -iname *.cpp | xargs ${CLANG_FORMAT} -i
  279. COMMENT ${CCOMMENT})
  280. endif()
  281. unset(SRCS)
  282. unset(CCOMMENT)
  283. endif()
  284. # Include source code
  285. # ===================
  286. # This function should be passed a list of all files in a target. It will automatically generate
  287. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  288. # one in the filesystem.
  289. function(create_target_directory_groups target_name)
  290. # Place any files that aren't in the source list in a separate group so that they don't get in
  291. # the way.
  292. source_group("Other Files" REGULAR_EXPRESSION ".")
  293. get_target_property(target_sources "${target_name}" SOURCES)
  294. foreach(file_name IN LISTS target_sources)
  295. get_filename_component(dir_name "${file_name}" PATH)
  296. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  297. string(REPLACE "/" "\\" group_name "${dir_name}")
  298. source_group("${group_name}" FILES "${file_name}")
  299. endforeach()
  300. endfunction()
  301. # Prevent boost from linking against libs when building
  302. add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY
  303. -DBOOST_SYSTEM_NO_LIB
  304. -DBOOST_DATE_TIME_NO_LIB
  305. -DBOOST_REGEX_NO_LIB
  306. )
  307. enable_testing()
  308. add_subdirectory(externals)
  309. add_subdirectory(src)
  310. # Set yuzu project or yuzu-cmd project as default StartUp Project in Visual Studio depending on whether QT is enabled or not
  311. if(ENABLE_QT)
  312. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu)
  313. else()
  314. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu-cmd)
  315. endif()
  316. # Installation instructions
  317. # =========================
  318. # Install freedesktop.org metadata files, following those specifications:
  319. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  320. # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  321. # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  322. if(ENABLE_QT AND UNIX AND NOT APPLE)
  323. install(FILES "${PROJECT_SOURCE_DIR}/dist/yuzu.desktop"
  324. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
  325. install(FILES "${PROJECT_SOURCE_DIR}/dist/yuzu.svg"
  326. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/scalable/apps")
  327. install(FILES "${PROJECT_SOURCE_DIR}/dist/yuzu.xml"
  328. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/mime/packages")
  329. endif()