CMakeLists.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. # SPDX-FileCopyrightText: 2018 yuzu Emulator Project
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. cmake_minimum_required(VERSION 3.15)
  4. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
  5. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/externals/cmake-modules")
  6. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/externals/find-modules")
  7. include(DownloadExternals)
  8. include(CMakeDependentOption)
  9. project(yuzu)
  10. # Set bundled sdl2/qt as dependent options.
  11. # OFF by default, but if ENABLE_SDL2 and MSVC are true then ON
  12. option(ENABLE_SDL2 "Enable the SDL2 frontend" ON)
  13. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_SDL2 "Download bundled SDL2 binaries" ON "ENABLE_SDL2;MSVC" OFF)
  14. # On Linux system SDL2 is likely to be lacking HIDAPI support which have drawbacks but is needed for SDL motion
  15. CMAKE_DEPENDENT_OPTION(YUZU_USE_EXTERNAL_SDL2 "Compile external SDL2" ON "ENABLE_SDL2;NOT MSVC" OFF)
  16. option(ENABLE_QT "Enable the Qt frontend" ON)
  17. option(ENABLE_QT6 "Allow usage of Qt6 to be attempted" OFF)
  18. set(QT6_LOCATION "" CACHE PATH "Additional Location to search for Qt6 libraries like C:/Qt/6.3.1/msvc2019_64/")
  19. option(ENABLE_QT_TRANSLATION "Enable translations for the Qt frontend" OFF)
  20. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_QT "Download bundled Qt binaries" "${MSVC}" "ENABLE_QT" OFF)
  21. option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
  22. option(YUZU_USE_BUNDLED_LIBUSB "Compile bundled libusb" OFF)
  23. option(YUZU_USE_BUNDLED_FFMPEG "Download/Build bundled FFmpeg" "${WIN32}")
  24. option(YUZU_USE_QT_MULTIMEDIA "Use QtMultimedia for Camera" OFF)
  25. option(YUZU_USE_QT_WEB_ENGINE "Use QtWebEngine for web applet implementation" OFF)
  26. option(ENABLE_CUBEB "Enables the cubeb audio backend" ON)
  27. option(USE_DISCORD_PRESENCE "Enables Discord Rich Presence" OFF)
  28. option(YUZU_USE_BUNDLED_OPUS "Compile bundled opus" ON)
  29. option(YUZU_TESTS "Compile tests" ON)
  30. option(YUZU_USE_PRECOMPILED_HEADERS "Use precompiled headers" ON)
  31. CMAKE_DEPENDENT_OPTION(YUZU_CRASH_DUMPS "Compile Windows crash dump (Minidump) support" OFF "WIN32" OFF)
  32. option(YUZU_USE_BUNDLED_VCPKG "Use vcpkg for yuzu dependencies" "${MSVC}")
  33. option(YUZU_CHECK_SUBMODULES "Check if submodules are present" ON)
  34. if (YUZU_USE_BUNDLED_VCPKG)
  35. if (YUZU_TESTS)
  36. list(APPEND VCPKG_MANIFEST_FEATURES "yuzu-tests")
  37. endif()
  38. if (YUZU_CRASH_DUMPS)
  39. list(APPEND VCPKG_MANIFEST_FEATURES "dbghelp")
  40. endif()
  41. include(${CMAKE_SOURCE_DIR}/externals/vcpkg/scripts/buildsystems/vcpkg.cmake)
  42. elseif(NOT "$ENV{VCPKG_TOOLCHAIN_FILE}" STREQUAL "")
  43. # Disable manifest mode (use vcpkg classic mode) when using a custom vcpkg installation
  44. option(VCPKG_MANIFEST_MODE "")
  45. include("$ENV{VCPKG_TOOLCHAIN_FILE}")
  46. endif()
  47. if (YUZU_USE_PRECOMPILED_HEADERS)
  48. if (MSVC AND CCACHE)
  49. # buildcache does not properly cache PCH files, leading to compilation errors.
  50. # See https://github.com/mbitsnbites/buildcache/discussions/230
  51. message(WARNING "buildcache does not properly support Precompiled Headers. Disabling PCH")
  52. set(YUZU_USE_PRECOMPILED_HEADERS OFF)
  53. endif()
  54. endif()
  55. if (YUZU_USE_PRECOMPILED_HEADERS)
  56. message(STATUS "Using Precompiled Headers.")
  57. set(CMAKE_PCH_INSTANTIATE_TEMPLATES ON)
  58. endif()
  59. # Default to a Release build
  60. get_property(IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  61. if (NOT IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE)
  62. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
  63. message(STATUS "Defaulting to a Release build")
  64. endif()
  65. if(EXISTS ${PROJECT_SOURCE_DIR}/hooks/pre-commit AND NOT EXISTS ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit)
  66. if (EXISTS ${PROJECT_SOURCE_DIR}/.git/)
  67. message(STATUS "Copying pre-commit hook")
  68. file(COPY hooks/pre-commit DESTINATION ${PROJECT_SOURCE_DIR}/.git/hooks)
  69. endif()
  70. endif()
  71. # Sanity check : Check that all submodules are present
  72. # =======================================================================
  73. function(check_submodules_present)
  74. file(READ "${PROJECT_SOURCE_DIR}/.gitmodules" gitmodules)
  75. string(REGEX MATCHALL "path *= *[^ \t\r\n]*" gitmodules ${gitmodules})
  76. foreach(module ${gitmodules})
  77. string(REGEX REPLACE "path *= *" "" module ${module})
  78. if (NOT EXISTS "${PROJECT_SOURCE_DIR}/${module}/.git")
  79. message(FATAL_ERROR "Git submodule ${module} not found. "
  80. "Please run: \ngit submodule update --init --recursive")
  81. endif()
  82. endforeach()
  83. endfunction()
  84. if(EXISTS ${PROJECT_SOURCE_DIR}/.gitmodules AND YUZU_CHECK_SUBMODULES)
  85. check_submodules_present()
  86. endif()
  87. configure_file(${PROJECT_SOURCE_DIR}/dist/compatibility_list/compatibility_list.qrc
  88. ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.qrc
  89. COPYONLY)
  90. if (EXISTS ${PROJECT_SOURCE_DIR}/dist/compatibility_list/compatibility_list.json)
  91. configure_file("${PROJECT_SOURCE_DIR}/dist/compatibility_list/compatibility_list.json"
  92. "${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json"
  93. COPYONLY)
  94. endif()
  95. if (ENABLE_COMPATIBILITY_LIST_DOWNLOAD AND NOT EXISTS ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json)
  96. message(STATUS "Downloading compatibility list for yuzu...")
  97. file(DOWNLOAD
  98. https://api.yuzu-emu.org/gamedb/
  99. "${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json" SHOW_PROGRESS)
  100. endif()
  101. if (NOT EXISTS ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json)
  102. file(WRITE ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json "")
  103. endif()
  104. # Detect current compilation architecture and create standard definitions
  105. # =======================================================================
  106. include(CheckSymbolExists)
  107. function(detect_architecture symbol arch)
  108. if (NOT DEFINED ARCHITECTURE)
  109. set(CMAKE_REQUIRED_QUIET 1)
  110. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  111. unset(CMAKE_REQUIRED_QUIET)
  112. # The output variable needs to be unique across invocations otherwise
  113. # CMake's crazy scope rules will keep it defined
  114. if (ARCHITECTURE_${arch})
  115. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  116. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  117. add_definitions(-DARCHITECTURE_${arch}=1)
  118. endif()
  119. endif()
  120. endfunction()
  121. if (NOT ENABLE_GENERIC)
  122. if (MSVC)
  123. detect_architecture("_M_AMD64" x86_64)
  124. detect_architecture("_M_IX86" x86)
  125. detect_architecture("_M_ARM" arm)
  126. detect_architecture("_M_ARM64" arm64)
  127. else()
  128. detect_architecture("__x86_64__" x86_64)
  129. detect_architecture("__i386__" x86)
  130. detect_architecture("__arm__" arm)
  131. detect_architecture("__aarch64__" arm64)
  132. endif()
  133. endif()
  134. if (NOT DEFINED ARCHITECTURE)
  135. set(ARCHITECTURE "GENERIC")
  136. set(ARCHITECTURE_GENERIC 1)
  137. add_definitions(-DARCHITECTURE_GENERIC=1)
  138. endif()
  139. message(STATUS "Target architecture: ${ARCHITECTURE}")
  140. if (UNIX)
  141. add_definitions(-DYUZU_UNIX=1)
  142. endif()
  143. # Configure C++ standard
  144. # ===========================
  145. # boost asio's concept usage doesn't play nicely with some compilers yet.
  146. add_definitions(-DBOOST_ASIO_DISABLE_CONCEPTS)
  147. if (MSVC)
  148. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/std:c++latest>)
  149. # boost still makes use of deprecated result_of.
  150. add_definitions(-D_HAS_DEPRECATED_RESULT_OF)
  151. else()
  152. set(CMAKE_CXX_STANDARD 20)
  153. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  154. endif()
  155. # Output binaries to bin/
  156. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
  157. # System imported libraries
  158. # =======================================================================
  159. find_package(fmt 8.0.1 REQUIRED CONFIG)
  160. find_package(nlohmann_json 3.8 REQUIRED CONFIG)
  161. find_package(ZLIB 1.2 REQUIRED)
  162. # Search for config-only package first (for vcpkg), then try non-config
  163. find_package(zstd 1.5 CONFIG)
  164. if (NOT zstd_FOUND)
  165. find_package(zstd 1.5 REQUIRED)
  166. endif()
  167. # lz4 1.8 is required, but vcpkg's lz4-config.cmake does not have version info
  168. find_package(lz4 CONFIG)
  169. if (NOT lz4_FOUND)
  170. find_package(lz4 1.8 REQUIRED)
  171. endif()
  172. if (YUZU_TESTS)
  173. find_package(Catch2 2.13.7 REQUIRED CONFIG)
  174. endif()
  175. find_package(Boost 1.73.0 COMPONENTS context)
  176. if (Boost_FOUND)
  177. set(Boost_LIBRARIES Boost::boost)
  178. # Conditionally add Boost::context only if the found Boost package provides it
  179. # The old version is missing Boost::context, so we want to avoid adding in that case
  180. # The new version requires adding Boost::context to prevent linking issues
  181. if (TARGET Boost::context)
  182. list(APPEND Boost_LIBRARIES Boost::context)
  183. endif()
  184. else()
  185. message(FATAL_ERROR "Boost 1.73.0 or newer not found")
  186. endif()
  187. # boost:asio has functions that require AcceptEx et al
  188. if (MINGW)
  189. find_library(MSWSOCK_LIBRARY mswsock REQUIRED)
  190. endif()
  191. # Please consider this as a stub
  192. if(ENABLE_QT6 AND Qt6_LOCATION)
  193. list(APPEND CMAKE_PREFIX_PATH "${Qt6_LOCATION}")
  194. endif()
  195. function(set_yuzu_qt_components)
  196. # Best practice is to ask for all components at once, so they are from the same version
  197. set(YUZU_QT_COMPONENTS2 Core Widgets Concurrent)
  198. if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  199. list(APPEND YUZU_QT_COMPONENTS2 DBus)
  200. endif()
  201. if (YUZU_USE_QT_MULTIMEDIA)
  202. list(APPEND YUZU_QT_COMPONENTS2 Multimedia)
  203. endif()
  204. if (YUZU_USE_QT_WEB_ENGINE)
  205. list(APPEND YUZU_QT_COMPONENTS2 WebEngineCore WebEngineWidgets)
  206. endif()
  207. if (ENABLE_QT_TRANSLATION)
  208. list(APPEND YUZU_QT_COMPONENTS2 LinguistTools)
  209. endif()
  210. set(YUZU_QT_COMPONENTS ${YUZU_QT_COMPONENTS2} PARENT_SCOPE)
  211. endfunction(set_yuzu_qt_components)
  212. # Qt5 requires that we find components, so it doesn't fit our pretty little find package function
  213. if(ENABLE_QT)
  214. set(QT_VERSION 5.15)
  215. # These are used to specify minimum versions
  216. set(QT5_VERSION 5.15)
  217. set(QT6_VERSION 6.3.1)
  218. set_yuzu_qt_components()
  219. if (ENABLE_QT6)
  220. find_package(Qt6 ${QT6_VERSION} COMPONENTS ${YUZU_QT_COMPONENTS})
  221. endif()
  222. if (Qt6_FOUND)
  223. message(STATUS "yuzu/CMakeLists.txt: Qt6Widgets_VERSION ${Qt6Widgets_VERSION}, setting QT_VERSION")
  224. set(QT_VERSION ${Qt6Widgets_VERSION})
  225. set(QT_MAJOR_VERSION 6)
  226. # Qt6 sets cxx_std_17 and we need to undo that
  227. set_target_properties(Qt6::Platform PROPERTIES INTERFACE_COMPILE_FEATURES "")
  228. else()
  229. message(STATUS "yuzu/CMakeLists.txt: Qt6 not found/not selected, trying for Qt5")
  230. # When Qt6 partially found, need this set to use Qt5 when not specifying version
  231. set(QT_DEFAULT_MAJOR_VERSION 5)
  232. set(QT_MAJOR_VERSION 5)
  233. set(YUZU_USE_QT_MULTIMEDIA ON)
  234. # Check for system Qt on Linux, fallback to bundled Qt
  235. if (UNIX AND NOT APPLE)
  236. if (NOT YUZU_USE_BUNDLED_QT)
  237. find_package(Qt5 ${QT5_VERSION} COMPONENTS Widgets DBus Multimedia)
  238. endif()
  239. if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND (NOT Qt5_FOUND OR YUZU_USE_BUNDLED_QT))
  240. # Check for dependencies, then enable bundled Qt download
  241. # Check that the system GLIBCXX version is compatible
  242. find_program(OBJDUMP objdump)
  243. if (NOT OBJDUMP)
  244. message(FATAL_ERROR "Required program `objdump` not found.")
  245. endif()
  246. find_library(LIBSTDCXX libstdc++.so.6)
  247. execute_process(
  248. COMMAND
  249. ${OBJDUMP} -T ${LIBSTDCXX}
  250. COMMAND
  251. grep GLIBCXX_3.4.28
  252. COMMAND
  253. sed "s/[0-9a-f]*.* //"
  254. COMMAND
  255. sed "s/ .*//"
  256. COMMAND
  257. sort -u
  258. OUTPUT_VARIABLE
  259. GLIBCXX_MET
  260. )
  261. if (NOT GLIBCXX_MET)
  262. message(FATAL_ERROR "Qt too old or not found, and bundled Qt package is not \
  263. compatible with this system. Either install Qt ${QT_VERSION}, or provide the path \
  264. to Qt by setting the variable Qt5_ROOT.")
  265. endif()
  266. # Check for headers
  267. find_package(PkgConfig REQUIRED)
  268. pkg_check_modules(QT_DEP_GLU QUIET glu>=9.0.0)
  269. if (NOT QT_DEP_GLU_FOUND)
  270. message(FATAL_ERROR "Qt bundled pacakge dependency `glu` not found. \
  271. Perhaps `libglu1-mesa-dev` needs to be installed?")
  272. endif()
  273. pkg_check_modules(QT_DEP_MESA QUIET dri>=20.0.8)
  274. if (NOT QT_DEP_MESA_FOUND)
  275. message(FATAL_ERROR "Qt bundled pacakge dependency `dri` not found. \
  276. Perhaps `mesa-common-dev` needs to be installed?")
  277. endif()
  278. # Check for X libraries
  279. set(BUNDLED_QT_REQUIREMENTS
  280. libxcb-icccm.so.4
  281. libxcb-image.so.0
  282. libxcb-keysyms.so.1
  283. libxcb-randr.so.0
  284. libxcb-render-util.so.0
  285. libxcb-render.so.0
  286. libxcb-shape.so.0
  287. libxcb-shm.so.0
  288. libxcb-sync.so.1
  289. libxcb-xfixes.so.0
  290. libxcb-xinerama.so.0
  291. libxcb-xkb.so.1
  292. libxcb.so.1
  293. libxkbcommon-x11.so.0
  294. libxkbcommon.so.0
  295. )
  296. set(UNRESOLVED_QT_DEPS "")
  297. foreach (REQUIREMENT ${BUNDLED_QT_REQUIREMENTS})
  298. find_library(BUNDLED_QT_${REQUIREMENT} ${REQUIREMENT})
  299. if (NOT BUNDLED_QT_${REQUIREMENT})
  300. set(UNRESOLVED_QT_DEPS ${UNRESOLVED_QT_DEPS} ${REQUIREMENT})
  301. endif()
  302. unset(BUNDLED_QT_${REQUIREMENT})
  303. endforeach()
  304. unset(BUNDLED_QT_REQUIREMENTS)
  305. if (NOT "${UNRESOLVED_QT_DEPS}" STREQUAL "")
  306. message(FATAL_ERROR "Bundled Qt package missing required dependencies: ${UNRESOLVED_QT_DEPS}")
  307. endif()
  308. set(YUZU_USE_BUNDLED_QT ON CACHE BOOL "Download bundled Qt" FORCE)
  309. endif()
  310. if (YUZU_USE_BUNDLED_QT)
  311. # Binary package currently does not support Qt webengine, so make sure it's disabled
  312. set(YUZU_USE_QT_WEB_ENGINE OFF CACHE BOOL "Use Qt Webengine" FORCE)
  313. endif()
  314. endif()
  315. set(YUZU_QT_NO_CMAKE_SYSTEM_PATH)
  316. if(YUZU_USE_BUNDLED_QT)
  317. if ((MSVC_VERSION GREATER_EQUAL 1920 AND MSVC_VERSION LESS 1940) AND ARCHITECTURE_x86_64)
  318. set(QT_BUILD qt-5.15.2-msvc2019_64)
  319. elseif ((${CMAKE_SYSTEM_NAME} STREQUAL "Linux") AND NOT MINGW AND ARCHITECTURE_x86_64)
  320. set(QT_BUILD qt5_5_15_2)
  321. else()
  322. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable YUZU_USE_BUNDLED_QT and provide your own.")
  323. endif()
  324. if (DEFINED QT_BUILD)
  325. download_bundled_external("qt/" ${QT_BUILD} QT_PREFIX)
  326. endif()
  327. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  328. set(YUZU_QT_NO_CMAKE_SYSTEM_PATH "NO_CMAKE_SYSTEM_PATH")
  329. # Binary package for Qt5 has Qt Multimedia
  330. set(YUZU_USE_QT_MULTIMEDIA ON CACHE BOOL "Use Qt Multimedia" FORCE)
  331. endif()
  332. set_yuzu_qt_components()
  333. find_package(Qt5 ${QT5_VERSION} COMPONENTS ${YUZU_QT_COMPONENTS} ${QT_PREFIX_HINT} ${YUZU_QT_NO_CMAKE_SYSTEM_PATH})
  334. endif()
  335. endif()
  336. # find SDL2 exports a bunch of variables that are needed, so its easier to do this outside of the yuzu_find_package
  337. if (ENABLE_SDL2)
  338. if (YUZU_USE_BUNDLED_SDL2)
  339. # Detect toolchain and platform
  340. if ((MSVC_VERSION GREATER_EQUAL 1920 AND MSVC_VERSION LESS 1940) AND ARCHITECTURE_x86_64)
  341. set(SDL2_VER "SDL2-2.0.18")
  342. else()
  343. message(FATAL_ERROR "No bundled SDL2 binaries for your toolchain. Disable YUZU_USE_BUNDLED_SDL2 and provide your own.")
  344. endif()
  345. if (DEFINED SDL2_VER)
  346. download_bundled_external("sdl2/" ${SDL2_VER} SDL2_PREFIX)
  347. endif()
  348. set(SDL2_FOUND YES)
  349. set(SDL2_INCLUDE_DIR "${SDL2_PREFIX}/include" CACHE PATH "Path to SDL2 headers")
  350. set(SDL2_LIBRARY "${SDL2_PREFIX}/lib/x64/SDL2.lib" CACHE PATH "Path to SDL2 library")
  351. set(SDL2_DLL_DIR "${SDL2_PREFIX}/lib/x64/" CACHE PATH "Path to SDL2.dll")
  352. add_library(SDL2 INTERFACE)
  353. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARY}")
  354. target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}")
  355. elseif (YUZU_USE_EXTERNAL_SDL2)
  356. message(STATUS "Using SDL2 from externals.")
  357. else()
  358. find_package(SDL2 2.0.18 REQUIRED)
  359. # Some installations don't set SDL2_LIBRARIES
  360. if("${SDL2_LIBRARIES}" STREQUAL "")
  361. message(WARNING "SDL2_LIBRARIES wasn't set, manually setting to SDL2::SDL2")
  362. set(SDL2_LIBRARIES "SDL2::SDL2")
  363. endif()
  364. include_directories(SYSTEM ${SDL2_INCLUDE_DIRS})
  365. add_library(SDL2 INTERFACE)
  366. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}")
  367. endif()
  368. endif()
  369. # Reexport some targets that are named differently when using the upstream CmakeConfig
  370. # In order to ALIAS targets to a new name, they first need to be IMPORTED_GLOBAL
  371. # Dynarmic checks for target `boost` and so we want to make sure it can find it through our system instead of using their external
  372. if (TARGET Boost::boost)
  373. set_target_properties(Boost::boost PROPERTIES IMPORTED_GLOBAL TRUE)
  374. add_library(boost ALIAS Boost::boost)
  375. endif()
  376. # Ensure libusb is properly configured (based on dolphin libusb include)
  377. if(NOT APPLE AND NOT YUZU_USE_BUNDLED_LIBUSB)
  378. find_package(PkgConfig)
  379. if (PKG_CONFIG_FOUND AND NOT CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD")
  380. pkg_check_modules(LIBUSB QUIET libusb-1.0>=1.0.24)
  381. else()
  382. find_package(LibUSB)
  383. endif()
  384. if (LIBUSB_FOUND)
  385. add_library(usb INTERFACE)
  386. target_include_directories(usb INTERFACE "${LIBUSB_INCLUDE_DIRS}")
  387. target_link_libraries(usb INTERFACE "${LIBUSB_LIBRARIES}")
  388. else()
  389. message(WARNING "libusb not found, falling back to externals")
  390. set(YUZU_USE_BUNDLED_LIBUSB ON)
  391. endif()
  392. endif()
  393. # List of all FFmpeg components required
  394. set(FFmpeg_COMPONENTS
  395. avcodec
  396. avutil
  397. swscale)
  398. if (UNIX AND NOT APPLE)
  399. find_package(PkgConfig REQUIRED)
  400. pkg_check_modules(LIBVA libva)
  401. endif()
  402. if (NOT YUZU_USE_BUNDLED_FFMPEG)
  403. # Use system installed FFmpeg
  404. find_package(FFmpeg 4.3 QUIET COMPONENTS ${FFmpeg_COMPONENTS})
  405. if (FFmpeg_FOUND)
  406. # Overwrite aggregate defines from FFmpeg module to avoid over-linking libraries.
  407. # Prevents shipping too many libraries with the AppImage.
  408. set(FFmpeg_LIBRARIES "")
  409. set(FFmpeg_INCLUDE_DIR "")
  410. foreach(COMPONENT ${FFmpeg_COMPONENTS})
  411. set(FFmpeg_LIBRARIES ${FFmpeg_LIBRARIES} ${FFmpeg_LIBRARY_${COMPONENT}} CACHE PATH "Paths to FFmpeg libraries" FORCE)
  412. set(FFmpeg_INCLUDE_DIR ${FFmpeg_INCLUDE_DIR} ${FFmpeg_INCLUDE_${COMPONENT}} CACHE PATH "Path to FFmpeg headers" FORCE)
  413. endforeach()
  414. else()
  415. message(WARNING "FFmpeg not found or too old, falling back to externals")
  416. set(YUZU_USE_BUNDLED_FFMPEG ON)
  417. endif()
  418. endif()
  419. # Prefer the -pthread flag on Linux.
  420. set(THREADS_PREFER_PTHREAD_FLAG ON)
  421. find_package(Threads REQUIRED)
  422. # Platform-specific library requirements
  423. # ======================================
  424. if (APPLE)
  425. # Umbrella framework for everything GUI-related
  426. find_library(COCOA_LIBRARY Cocoa)
  427. set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  428. elseif (WIN32)
  429. # WSAPoll and SHGetKnownFolderPath (AppData/Roaming) didn't exist before WinNT 6.x (Vista)
  430. add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600)
  431. set(PLATFORM_LIBRARIES winmm ws2_32 iphlpapi)
  432. if (MINGW)
  433. # PSAPI is the Process Status API
  434. set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} psapi imm32 version)
  435. endif()
  436. if (YUZU_CRASH_DUMPS)
  437. find_library(DBGHELP_LIBRARY dbghelp)
  438. if ("${DBGHELP_LIBRARY}" STREQUAL "DBGHELP_LIBRARY-NOTFOUND")
  439. message(FATAL_ERROR "YUZU_CRASH_DUMPS enabled but dbghelp library not found")
  440. endif()
  441. endif()
  442. elseif (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
  443. set(PLATFORM_LIBRARIES rt)
  444. endif()
  445. # Setup a custom clang-format target (if clang-format can be found) that will run
  446. # against all the src files. This should be used before making a pull request.
  447. # =======================================================================
  448. set(CLANG_FORMAT_POSTFIX "-12")
  449. find_program(CLANG_FORMAT
  450. NAMES clang-format${CLANG_FORMAT_POSTFIX}
  451. clang-format
  452. PATHS ${PROJECT_BINARY_DIR}/externals)
  453. # if find_program doesn't find it, try to download from externals
  454. if (NOT CLANG_FORMAT)
  455. if (WIN32 AND NOT CMAKE_CROSSCOMPILING)
  456. message(STATUS "Clang format not found! Downloading...")
  457. set(CLANG_FORMAT "${PROJECT_BINARY_DIR}/externals/clang-format${CLANG_FORMAT_POSTFIX}.exe")
  458. file(DOWNLOAD
  459. https://github.com/yuzu-emu/ext-windows-bin/raw/master/clang-format${CLANG_FORMAT_POSTFIX}.exe
  460. "${CLANG_FORMAT}" SHOW_PROGRESS
  461. STATUS DOWNLOAD_SUCCESS)
  462. if (NOT DOWNLOAD_SUCCESS EQUAL 0)
  463. message(WARNING "Could not download clang format! Disabling the clang format target")
  464. file(REMOVE ${CLANG_FORMAT})
  465. unset(CLANG_FORMAT)
  466. endif()
  467. else()
  468. message(WARNING "Clang format not found! Disabling the clang format target")
  469. endif()
  470. endif()
  471. if (CLANG_FORMAT)
  472. set(SRCS ${PROJECT_SOURCE_DIR}/src)
  473. set(CCOMMENT "Running clang format against all the .h and .cpp files in src/")
  474. if (WIN32)
  475. add_custom_target(clang-format
  476. COMMAND powershell.exe -Command "Get-ChildItem '${SRCS}/*' -Include *.cpp,*.h -Recurse | Foreach {&'${CLANG_FORMAT}' -i $_.fullname}"
  477. COMMENT ${CCOMMENT})
  478. elseif(MINGW)
  479. add_custom_target(clang-format
  480. COMMAND find `cygpath -u ${SRCS}` -iname *.h -o -iname *.cpp | xargs `cygpath -u ${CLANG_FORMAT}` -i
  481. COMMENT ${CCOMMENT})
  482. else()
  483. add_custom_target(clang-format
  484. COMMAND find ${SRCS} -iname *.h -o -iname *.cpp | xargs ${CLANG_FORMAT} -i
  485. COMMENT ${CCOMMENT})
  486. endif()
  487. unset(SRCS)
  488. unset(CCOMMENT)
  489. endif()
  490. # Include source code
  491. # ===================
  492. # This function should be passed a list of all files in a target. It will automatically generate
  493. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  494. # one in the filesystem.
  495. function(create_target_directory_groups target_name)
  496. # Place any files that aren't in the source list in a separate group so that they don't get in
  497. # the way.
  498. source_group("Other Files" REGULAR_EXPRESSION ".")
  499. get_target_property(target_sources "${target_name}" SOURCES)
  500. foreach(file_name IN LISTS target_sources)
  501. get_filename_component(dir_name "${file_name}" PATH)
  502. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  503. string(REPLACE "/" "\\" group_name "${dir_name}")
  504. source_group("${group_name}" FILES "${file_name}")
  505. endforeach()
  506. endfunction()
  507. # Prevent boost from linking against libs when building
  508. add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY
  509. -DBOOST_SYSTEM_NO_LIB
  510. -DBOOST_DATE_TIME_NO_LIB
  511. -DBOOST_REGEX_NO_LIB
  512. )
  513. # Adjustments for MSVC + Ninja
  514. if (MSVC AND CMAKE_GENERATOR STREQUAL "Ninja")
  515. add_compile_options(
  516. /wd4464 # relative include path contains '..'
  517. /wd4711 # function 'function' selected for automatic inline expansion
  518. /wd4820 # 'bytes' bytes padding added after construct 'member_name'
  519. )
  520. endif()
  521. enable_testing()
  522. add_subdirectory(externals)
  523. add_subdirectory(src)
  524. # Set yuzu project or yuzu-cmd project as default StartUp Project in Visual Studio depending on whether QT is enabled or not
  525. if(ENABLE_QT)
  526. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu)
  527. else()
  528. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu-cmd)
  529. endif()
  530. # Installation instructions
  531. # =========================
  532. # Install freedesktop.org metadata files, following those specifications:
  533. # https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  534. # https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  535. # https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  536. # https://www.freedesktop.org/software/appstream/docs/
  537. if(ENABLE_QT AND UNIX AND NOT APPLE)
  538. install(FILES "dist/org.yuzu_emu.yuzu.desktop"
  539. DESTINATION "share/applications")
  540. install(FILES "dist/yuzu.svg"
  541. DESTINATION "share/icons/hicolor/scalable/apps"
  542. RENAME "org.yuzu_emu.yuzu.svg")
  543. install(FILES "dist/org.yuzu_emu.yuzu.xml"
  544. DESTINATION "share/mime/packages")
  545. install(FILES "dist/org.yuzu_emu.yuzu.metainfo.xml"
  546. DESTINATION "share/metainfo")
  547. endif()