CMakeLists.txt 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. cmake_minimum_required(VERSION 3.15)
  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. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/externals/find-modules")
  5. include(DownloadExternals)
  6. include(CMakeDependentOption)
  7. project(yuzu)
  8. # Set bundled sdl2/qt as dependent options.
  9. # OFF by default, but if ENABLE_SDL2 and MSVC are true then ON
  10. option(ENABLE_SDL2 "Enable the SDL2 frontend" ON)
  11. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_SDL2 "Download bundled SDL2 binaries" ON "ENABLE_SDL2;MSVC" OFF)
  12. # On Linux system SDL2 is likely to be lacking HIDAPI support which have drawbacks but is needed for SDL motion
  13. CMAKE_DEPENDENT_OPTION(YUZU_ALLOW_SYSTEM_SDL2 "Try using system SDL2 before fallling back to one from externals" NOT UNIX "ENABLE_SDL2" OFF)
  14. option(ENABLE_QT "Enable the Qt frontend" ON)
  15. option(ENABLE_QT_TRANSLATION "Enable translations for the Qt frontend" OFF)
  16. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_QT "Download bundled Qt binaries" ON "ENABLE_QT;MSVC" OFF)
  17. option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
  18. option(YUZU_USE_BUNDLED_BOOST "Download bundled Boost" OFF)
  19. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_FFMPEG "Download/Build bundled FFmpeg" ON "WIN32" OFF)
  20. option(YUZU_USE_QT_WEB_ENGINE "Use QtWebEngine for web applet implementation" OFF)
  21. option(YUZU_ENABLE_BOXCAT "Enable the Boxcat service, a yuzu high-level implementation of BCAT" ON)
  22. option(ENABLE_CUBEB "Enables the cubeb audio backend" ON)
  23. option(USE_DISCORD_PRESENCE "Enables Discord Rich Presence" OFF)
  24. if (NOT ENABLE_WEB_SERVICE)
  25. set(YUZU_ENABLE_BOXCAT OFF)
  26. endif()
  27. # Default to a Release build
  28. get_property(IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  29. if (NOT IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE)
  30. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
  31. message(STATUS "Defaulting to a Release build")
  32. endif()
  33. if(EXISTS ${PROJECT_SOURCE_DIR}/hooks/pre-commit AND NOT EXISTS ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit)
  34. message(STATUS "Copying pre-commit hook")
  35. file(COPY hooks/pre-commit
  36. DESTINATION ${PROJECT_SOURCE_DIR}/.git/hooks)
  37. endif()
  38. # Sanity check : Check that all submodules are present
  39. # =======================================================================
  40. function(check_submodules_present)
  41. file(READ "${PROJECT_SOURCE_DIR}/.gitmodules" gitmodules)
  42. string(REGEX MATCHALL "path *= *[^ \t\r\n]*" gitmodules ${gitmodules})
  43. foreach(module ${gitmodules})
  44. string(REGEX REPLACE "path *= *" "" module ${module})
  45. if (NOT EXISTS "${PROJECT_SOURCE_DIR}/${module}/.git")
  46. message(FATAL_ERROR "Git submodule ${module} not found. "
  47. "Please run: git submodule update --init --recursive")
  48. endif()
  49. endforeach()
  50. endfunction()
  51. if(EXISTS ${PROJECT_SOURCE_DIR}/.gitmodules)
  52. check_submodules_present()
  53. endif()
  54. configure_file(${PROJECT_SOURCE_DIR}/dist/compatibility_list/compatibility_list.qrc
  55. ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.qrc
  56. COPYONLY)
  57. if (ENABLE_COMPATIBILITY_LIST_DOWNLOAD AND NOT EXISTS ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json)
  58. message(STATUS "Downloading compatibility list for yuzu...")
  59. file(DOWNLOAD
  60. https://api.yuzu-emu.org/gamedb/
  61. "${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json" SHOW_PROGRESS)
  62. endif()
  63. if (NOT EXISTS ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json)
  64. file(WRITE ${PROJECT_BINARY_DIR}/dist/compatibility_list/compatibility_list.json "")
  65. endif()
  66. # Detect current compilation architecture and create standard definitions
  67. # =======================================================================
  68. include(CheckSymbolExists)
  69. function(detect_architecture symbol arch)
  70. if (NOT DEFINED ARCHITECTURE)
  71. set(CMAKE_REQUIRED_QUIET 1)
  72. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  73. unset(CMAKE_REQUIRED_QUIET)
  74. # The output variable needs to be unique across invocations otherwise
  75. # CMake's crazy scope rules will keep it defined
  76. if (ARCHITECTURE_${arch})
  77. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  78. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  79. add_definitions(-DARCHITECTURE_${arch}=1)
  80. endif()
  81. endif()
  82. endfunction()
  83. if (NOT ENABLE_GENERIC)
  84. if (MSVC)
  85. detect_architecture("_M_AMD64" x86_64)
  86. detect_architecture("_M_IX86" x86)
  87. detect_architecture("_M_ARM" ARM)
  88. detect_architecture("_M_ARM64" ARM64)
  89. else()
  90. detect_architecture("__x86_64__" x86_64)
  91. detect_architecture("__i386__" x86)
  92. detect_architecture("__arm__" ARM)
  93. detect_architecture("__aarch64__" ARM64)
  94. endif()
  95. endif()
  96. if (NOT DEFINED ARCHITECTURE)
  97. set(ARCHITECTURE "GENERIC")
  98. set(ARCHITECTURE_GENERIC 1)
  99. add_definitions(-DARCHITECTURE_GENERIC=1)
  100. endif()
  101. message(STATUS "Target architecture: ${ARCHITECTURE}")
  102. if (UNIX)
  103. add_definitions(-DYUZU_UNIX=1)
  104. endif()
  105. # Configure C++ standard
  106. # ===========================
  107. # boost asio's concept usage doesn't play nicely with some compilers yet.
  108. add_definitions(-DBOOST_ASIO_DISABLE_CONCEPTS)
  109. if (MSVC)
  110. add_compile_options(/std:c++latest)
  111. # cubeb and boost still make use of deprecated result_of.
  112. add_definitions(-D_HAS_DEPRECATED_RESULT_OF)
  113. else()
  114. set(CMAKE_CXX_STANDARD 20)
  115. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  116. endif()
  117. # Output binaries to bin/
  118. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
  119. # System imported libraries
  120. # If not found, download any missing through Conan
  121. # =======================================================================
  122. set(CONAN_CMAKE_SILENT_OUTPUT TRUE)
  123. set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
  124. if (YUZU_CONAN_INSTALLED)
  125. if (IS_MULTI_CONFIG)
  126. include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
  127. else()
  128. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
  129. endif()
  130. list(APPEND CMAKE_MODULE_PATH "${CMAKE_BINARY_DIR}")
  131. list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
  132. conan_basic_setup()
  133. message(STATUS "Adding conan installed libraries to the search path")
  134. endif()
  135. macro(yuzu_find_packages)
  136. set(options FORCE_REQUIRED)
  137. cmake_parse_arguments(FN "${options}" "" "" ${ARGN})
  138. # Cmake has a *serious* lack of 2D array or associative array...
  139. # Capitalization matters here. We need the naming to match the generated paths from Conan
  140. set(REQUIRED_LIBS
  141. # Cmake Pkg Prefix Version Conan Pkg
  142. "Catch2 2.13 catch2/2.13.0"
  143. "fmt 7.1 fmt/7.1.2"
  144. "lz4 1.8 lz4/1.9.2"
  145. "nlohmann_json 3.8 nlohmann_json/3.8.0"
  146. "ZLIB 1.2 zlib/1.2.11"
  147. "zstd 1.4 zstd/1.4.8"
  148. # can't use opus until AVX check is fixed: https://github.com/yuzu-emu/yuzu/pull/4068
  149. #"opus 1.3 opus/1.3.1"
  150. )
  151. foreach(PACKAGE ${REQUIRED_LIBS})
  152. string(REGEX REPLACE "[ \t\r\n]+" ";" PACKAGE_SPLIT ${PACKAGE})
  153. list(GET PACKAGE_SPLIT 0 PACKAGE_PREFIX)
  154. list(GET PACKAGE_SPLIT 1 PACKAGE_VERSION)
  155. list(GET PACKAGE_SPLIT 2 PACKAGE_CONAN)
  156. # This function is called twice, once to check if the packages exist on the system already
  157. # and a second time to check if conan installed them properly. The second check passes in FORCE_REQUIRED
  158. if (NOT ${PACKAGE_PREFIX}_FOUND)
  159. if (FN_FORCE_REQUIRED)
  160. find_package(${PACKAGE_PREFIX} ${PACKAGE_VERSION} REQUIRED)
  161. else()
  162. find_package(${PACKAGE_PREFIX} ${PACKAGE_VERSION})
  163. endif()
  164. endif()
  165. if (NOT ${PACKAGE_PREFIX}_FOUND)
  166. list(APPEND CONAN_REQUIRED_LIBS ${PACKAGE_CONAN})
  167. else()
  168. # Set a legacy findPackage.cmake style PACKAGE_LIBRARIES variable for subprojects that rely on this
  169. set(${PACKAGE_PREFIX}_LIBRARIES "${PACKAGE_PREFIX}::${PACKAGE_PREFIX}")
  170. endif()
  171. endforeach()
  172. unset(FN_FORCE_REQUIRED)
  173. endmacro()
  174. if (NOT YUZU_USE_BUNDLED_BOOST)
  175. find_package(Boost 1.73.0 CONFIG COMPONENTS context headers QUIET)
  176. endif()
  177. if (Boost_FOUND)
  178. set(Boost_LIBRARIES Boost::boost)
  179. # Conditionally add Boost::context only if the active version of the Conan or system Boost package provides it
  180. # The old version is missing Boost::context, so we want to avoid adding in that case
  181. # The new version requires adding Boost::context to prevent linking issues
  182. #
  183. # This one is used by Conan on subsequent CMake configures, not the first configure.
  184. if (TARGET Boost::context)
  185. list(APPEND Boost_LIBRARIES Boost::context)
  186. endif()
  187. elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR YUZU_USE_BUNDLED_BOOST)
  188. message(STATUS "Boost 1.73.0 or newer not found, falling back to externals")
  189. set(YUZU_USE_BUNDLED_BOOST ON CACHE BOOL "Download bundled Boost" FORCE)
  190. # Use yuzu Boost binaries
  191. set(Boost_EXT_NAME "boost_1_75_0")
  192. set(Boost_PATH "${CMAKE_BINARY_DIR}/externals/${Boost_EXT_NAME}")
  193. download_bundled_external("boost/" ${Boost_EXT_NAME} "")
  194. set(Boost_USE_DEBUG_RUNTIME FALSE)
  195. set(Boost_USE_STATIC_LIBS ON)
  196. find_package(Boost 1.75.0 CONFIG REQUIRED COMPONENTS context headers PATHS ${Boost_PATH} NO_DEFAULT_PATH)
  197. # Manually set the include dirs since the find_package sets it incorrectly
  198. set(Boost_INCLUDE_DIRS ${Boost_PATH}/include CACHE PATH "Path to Boost headers" FORCE)
  199. include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")
  200. else()
  201. message(STATUS "Boost 1.73.0 or newer not found, falling back to Conan")
  202. list(APPEND CONAN_REQUIRED_LIBS "boost/1.73.0")
  203. endif()
  204. # Attempt to locate any packages that are required and report the missing ones in CONAN_REQUIRED_LIBS
  205. yuzu_find_packages()
  206. # Qt5 requires that we find components, so it doesn't fit our pretty little find package function
  207. if(ENABLE_QT)
  208. # We want to load the generated conan qt config so that we get the QT_ROOT var so that we can use the official
  209. # Qt5Config inside the root folder instead of the conan generated one.
  210. if(EXISTS ${CMAKE_BINARY_DIR}/qtConfig.cmake)
  211. include(${CMAKE_BINARY_DIR}/qtConfig.cmake)
  212. list(APPEND CMAKE_MODULE_PATH "${CONAN_QT_ROOT_RELEASE}")
  213. list(APPEND CMAKE_PREFIX_PATH "${CONAN_QT_ROOT_RELEASE}")
  214. endif()
  215. # Workaround for an issue where conan tries to build Qt from scratch instead of download prebuilt binaries
  216. set(QT_PREFIX_HINT)
  217. if(YUZU_USE_BUNDLED_QT)
  218. if ((MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1930) AND ARCHITECTURE_x86_64)
  219. set(QT_VER qt-5.12.8-msvc2017_64)
  220. else()
  221. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable YUZU_USE_BUNDLED_QT and provide your own.")
  222. endif()
  223. if (DEFINED QT_VER)
  224. download_bundled_external("qt/" ${QT_VER} QT_PREFIX)
  225. endif()
  226. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  227. endif()
  228. find_package(Qt5 5.9 COMPONENTS Widgets ${QT_PREFIX_HINT})
  229. if (YUZU_USE_QT_WEB_ENGINE)
  230. find_package(Qt5 COMPONENTS WebEngineCore WebEngineWidgets)
  231. endif()
  232. if (ENABLE_QT_TRANSLATION)
  233. find_package(Qt5 REQUIRED COMPONENTS LinguistTools ${QT_PREFIX_HINT})
  234. endif()
  235. endif()
  236. # find SDL2 exports a bunch of variables that are needed, so its easier to do this outside of the yuzu_find_package
  237. if (ENABLE_SDL2)
  238. if (YUZU_USE_BUNDLED_SDL2)
  239. # Detect toolchain and platform
  240. if ((MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1930) AND ARCHITECTURE_x86_64)
  241. set(SDL2_VER "SDL2-2.0.15-prerelease")
  242. else()
  243. message(FATAL_ERROR "No bundled SDL2 binaries for your toolchain. Disable YUZU_USE_BUNDLED_SDL2 and provide your own.")
  244. endif()
  245. if (DEFINED SDL2_VER)
  246. download_bundled_external("sdl2/" ${SDL2_VER} SDL2_PREFIX)
  247. endif()
  248. set(SDL2_FOUND YES)
  249. set(SDL2_INCLUDE_DIR "${SDL2_PREFIX}/include" CACHE PATH "Path to SDL2 headers")
  250. set(SDL2_LIBRARY "${SDL2_PREFIX}/lib/x64/SDL2.lib" CACHE PATH "Path to SDL2 library")
  251. set(SDL2_DLL_DIR "${SDL2_PREFIX}/lib/x64/" CACHE PATH "Path to SDL2.dll")
  252. add_library(SDL2 INTERFACE)
  253. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARY}")
  254. target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}")
  255. else()
  256. if (YUZU_ALLOW_SYSTEM_SDL2)
  257. find_package(SDL2 2.0.15 QUIET)
  258. if (SDL2_FOUND)
  259. # Some installations don't set SDL2_LIBRARIES
  260. if("${SDL2_LIBRARIES}" STREQUAL "")
  261. message(WARNING "SDL2_LIBRARIES wasn't set, manually setting to SDL2::SDL2")
  262. set(SDL2_LIBRARIES "SDL2::SDL2")
  263. endif()
  264. include_directories(SYSTEM ${SDL2_INCLUDE_DIRS})
  265. add_library(SDL2 INTERFACE)
  266. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}")
  267. else()
  268. message(STATUS "SDL2 2.0.15 or newer not found, falling back to externals.")
  269. endif()
  270. else()
  271. message(STATUS "Using SDL2 from externals.")
  272. endif()
  273. endif()
  274. endif()
  275. # Install any missing dependencies with conan install
  276. if (CONAN_REQUIRED_LIBS)
  277. message(STATUS "Packages ${CONAN_REQUIRED_LIBS} not found!")
  278. # Use Conan to fetch the libraries that aren't found
  279. # Download conan.cmake automatically, you can also just copy the conan.cmake file
  280. if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
  281. message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
  282. file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake"
  283. "${CMAKE_BINARY_DIR}/conan.cmake")
  284. endif()
  285. include(${CMAKE_BINARY_DIR}/conan.cmake)
  286. set(CONAN_LIB_OPTIONS
  287. libzip:with_openssl=False
  288. libzip:enable_windows_crypto=False
  289. )
  290. conan_check(VERSION 1.24.0 REQUIRED)
  291. # Manually add iconv to fix a dep conflict between qt and sdl2
  292. # We don't need to add it through find_package or anything since the other two can find it just fine
  293. if ("${CONAN_REQUIRED_LIBS}" MATCHES "qt" AND "${CONAN_REQUIRED_LIBS}" MATCHES "sdl")
  294. list(APPEND CONAN_REQUIRED_LIBS "libiconv/1.16")
  295. endif()
  296. if (IS_MULTI_CONFIG)
  297. conan_cmake_run(REQUIRES ${CONAN_REQUIRED_LIBS}
  298. OPTIONS ${CONAN_LIB_OPTIONS}
  299. BUILD missing
  300. CONFIGURATION_TYPES "Release;Debug"
  301. GENERATORS cmake_multi cmake_find_package_multi)
  302. include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
  303. else()
  304. conan_cmake_run(REQUIRES ${CONAN_REQUIRED_LIBS}
  305. OPTIONS ${CONAN_LIB_OPTIONS}
  306. BUILD missing
  307. GENERATORS cmake cmake_find_package_multi)
  308. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
  309. endif()
  310. list(APPEND CMAKE_MODULE_PATH "${CMAKE_BINARY_DIR}")
  311. list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
  312. conan_basic_setup()
  313. set(YUZU_CONAN_INSTALLED TRUE CACHE BOOL "If true, the following builds will add conan to the lib search path" FORCE)
  314. # Now that we've installed what we are missing, try to locate them again,
  315. # this time with required, so we bail if its not found.
  316. yuzu_find_packages(FORCE_REQUIRED)
  317. if (NOT Boost_FOUND)
  318. find_package(Boost 1.73.0 REQUIRED COMPONENTS context headers)
  319. set(Boost_LIBRARIES Boost::boost)
  320. # Conditionally add Boost::context only if the active version of the Conan Boost package provides it
  321. # The old version is missing Boost::context, so we want to avoid adding in that case
  322. # The new version requires adding Boost::context to prevent linking issues
  323. if (TARGET Boost::context)
  324. list(APPEND Boost_LIBRARIES Boost::context)
  325. endif()
  326. endif()
  327. # Due to issues with variable scopes in functions, we need to also find_package(qt5) outside of the function
  328. if(ENABLE_QT)
  329. list(APPEND CMAKE_MODULE_PATH "${CONAN_QT_ROOT_RELEASE}")
  330. list(APPEND CMAKE_PREFIX_PATH "${CONAN_QT_ROOT_RELEASE}")
  331. find_package(Qt5 5.9 REQUIRED COMPONENTS Widgets)
  332. if (YUZU_USE_QT_WEB_ENGINE)
  333. find_package(Qt5 REQUIRED COMPONENTS WebEngineCore WebEngineWidgets)
  334. endif()
  335. endif()
  336. endif()
  337. # Reexport some targets that are named differently when using the upstream CmakeConfig vs the generated Conan config
  338. # In order to ALIAS targets to a new name, they first need to be IMPORTED_GLOBAL
  339. # Dynarmic checks for target `boost` and so we want to make sure it can find it through our system instead of using their external
  340. if (TARGET Boost::Boost)
  341. set_target_properties(Boost::Boost PROPERTIES IMPORTED_GLOBAL TRUE)
  342. add_library(Boost::boost ALIAS Boost::Boost)
  343. add_library(boost ALIAS Boost::Boost)
  344. elseif (TARGET Boost::boost)
  345. set_target_properties(Boost::boost PROPERTIES IMPORTED_GLOBAL TRUE)
  346. add_library(boost ALIAS Boost::boost)
  347. endif()
  348. # Ensure libusb is properly configured (based on dolphin libusb include)
  349. if(NOT APPLE)
  350. include(FindPkgConfig)
  351. find_package(LibUSB)
  352. endif()
  353. if (NOT LIBUSB_FOUND)
  354. add_subdirectory(externals/libusb)
  355. set(LIBUSB_INCLUDE_DIR "")
  356. set(LIBUSB_LIBRARIES usb)
  357. endif()
  358. # List of all FFmpeg components required
  359. set(FFmpeg_COMPONENTS
  360. avcodec
  361. avutil
  362. swscale)
  363. if (NOT YUZU_USE_BUNDLED_FFMPEG)
  364. # Use system installed FFmpeg
  365. find_package(FFmpeg QUIET COMPONENTS ${FFmpeg_COMPONENTS})
  366. if (FFmpeg_FOUND)
  367. # Overwrite aggregate defines from FFmpeg module to avoid over-linking libraries.
  368. # Prevents shipping too many libraries with the AppImage.
  369. set(FFmpeg_LIBRARIES "")
  370. set(FFmpeg_INCLUDE_DIR "")
  371. foreach(COMPONENT ${FFmpeg_COMPONENTS})
  372. set(FFmpeg_LIBRARIES ${FFmpeg_LIBRARIES} ${FFmpeg_LIBRARY_${COMPONENT}} CACHE PATH "Paths to FFmpeg libraries" FORCE)
  373. set(FFmpeg_INCLUDE_DIR ${FFmpeg_INCLUDE_DIR} ${FFmpeg_INCLUDE_${COMPONENT}} CACHE PATH "Path to FFmpeg headers" FORCE)
  374. endforeach()
  375. else()
  376. message(WARNING "FFmpeg not found, falling back to externals")
  377. set(YUZU_USE_BUNDLED_FFMPEG ON)
  378. endif()
  379. endif()
  380. if (YUZU_USE_BUNDLED_FFMPEG)
  381. if (NOT WIN32)
  382. # Build FFmpeg from externals
  383. message(STATUS "Using FFmpeg from externals")
  384. # FFmpeg has source that requires one of nasm or yasm to assemble it.
  385. # REQUIRED throws an error if not found here during configuration rather than during compilation.
  386. find_program(ASSEMBLER NAMES nasm yasm REQUIRED)
  387. set(FFmpeg_PREFIX ${PROJECT_SOURCE_DIR}/externals/ffmpeg)
  388. set(FFmpeg_BUILD_DIR ${PROJECT_BINARY_DIR}/externals/ffmpeg)
  389. set(FFmpeg_MAKEFILE ${FFmpeg_BUILD_DIR}/Makefile)
  390. make_directory(${FFmpeg_BUILD_DIR})
  391. # Read version string from external
  392. file(READ ${FFmpeg_PREFIX}/RELEASE FFmpeg_VERSION)
  393. set(FFmpeg_FOUND NO)
  394. if (NOT FFmpeg_VERSION STREQUAL "")
  395. set(FFmpeg_FOUND YES)
  396. endif()
  397. unset(FFmpeg_LIBRARIES CACHE)
  398. foreach(COMPONENT ${FFmpeg_COMPONENTS})
  399. set(FFmpeg_${COMPONENT}_PREFIX "${FFmpeg_BUILD_DIR}/lib${COMPONENT}")
  400. set(FFmpeg_${COMPONENT}_LIB_NAME "lib${COMPONENT}.a")
  401. set(FFmpeg_${COMPONENT}_LIBRARY "${FFmpeg_${COMPONENT}_PREFIX}/${FFmpeg_${COMPONENT}_LIB_NAME}")
  402. set(FFmpeg_LIBRARIES
  403. ${FFmpeg_LIBRARIES}
  404. ${FFmpeg_${COMPONENT}_LIBRARY}
  405. CACHE PATH "Paths to FFmpeg libraries" FORCE)
  406. endforeach()
  407. set(FFmpeg_INCLUDE_DIR
  408. "${FFmpeg_PREFIX};${FFmpeg_BUILD_DIR}"
  409. CACHE PATH "Path to FFmpeg headers" FORCE)
  410. # `configure` parameters builds only exactly what yuzu needs from FFmpeg
  411. # `--disable-{vaapi,vdpau}` is needed to avoid linking issues
  412. add_custom_command(
  413. OUTPUT
  414. ${FFmpeg_MAKEFILE}
  415. COMMAND
  416. /bin/bash ${FFmpeg_PREFIX}/configure
  417. --disable-avdevice
  418. --disable-avfilter
  419. --disable-avformat
  420. --disable-doc
  421. --disable-everything
  422. --disable-ffmpeg
  423. --disable-ffprobe
  424. --disable-network
  425. --disable-postproc
  426. --disable-swresample
  427. --disable-vaapi
  428. --disable-vdpau
  429. --enable-decoder=h264
  430. --enable-decoder=vp9
  431. WORKING_DIRECTORY
  432. ${FFmpeg_BUILD_DIR}
  433. )
  434. # Workaround for Ubuntu 18.04's older version of make not being able to call make as a child
  435. # with context of the jobserver. Also helps ninja users.
  436. execute_process(
  437. COMMAND
  438. nproc
  439. OUTPUT_VARIABLE
  440. SYSTEM_THREADS)
  441. add_custom_command(
  442. OUTPUT
  443. ${FFmpeg_LIBRARIES}
  444. COMMAND
  445. make -j${SYSTEM_THREADS}
  446. WORKING_DIRECTORY
  447. ${FFmpeg_BUILD_DIR}
  448. )
  449. # ALL makes this custom target build every time
  450. # but it won't actually build if the DEPENDS parameter is up to date
  451. add_custom_target(ffmpeg-configure ALL DEPENDS ${FFmpeg_MAKEFILE})
  452. add_custom_target(ffmpeg-build ALL DEPENDS ${FFmpeg_LIBRARIES} ffmpeg-configure)
  453. if (FFmpeg_FOUND)
  454. message(STATUS "Found FFmpeg version ${FFmpeg_VERSION}")
  455. else()
  456. message(FATAL_ERROR "FFmpeg not found")
  457. endif()
  458. else() # WIN32
  459. # Use yuzu FFmpeg binaries
  460. set(FFmpeg_EXT_NAME "ffmpeg-4.3.1")
  461. set(FFmpeg_PATH "${CMAKE_BINARY_DIR}/externals/${FFmpeg_EXT_NAME}")
  462. download_bundled_external("ffmpeg/" ${FFmpeg_EXT_NAME} "")
  463. set(FFmpeg_FOUND YES)
  464. set(FFmpeg_INCLUDE_DIR "${FFmpeg_PATH}/include" CACHE PATH "Path to FFmpeg headers" FORCE)
  465. set(FFmpeg_LIBRARY_DIR "${FFmpeg_PATH}/bin" CACHE PATH "Path to FFmpeg library directory" FORCE)
  466. set(FFmpeg_DLL_DIR "${FFmpeg_PATH}/bin" CACHE PATH "Path to FFmpeg dll's" FORCE)
  467. set(FFmpeg_LIBRARIES
  468. ${FFmpeg_LIBRARY_DIR}/swscale.lib
  469. ${FFmpeg_LIBRARY_DIR}/avcodec.lib
  470. ${FFmpeg_LIBRARY_DIR}/avutil.lib
  471. CACHE PATH "Paths to FFmpeg libraries" FORCE)
  472. endif()
  473. endif()
  474. unset(FFmpeg_COMPONENTS)
  475. # Prefer the -pthread flag on Linux.
  476. set(THREADS_PREFER_PTHREAD_FLAG ON)
  477. find_package(Threads REQUIRED)
  478. # Platform-specific library requirements
  479. # ======================================
  480. if (APPLE)
  481. # Umbrella framework for everything GUI-related
  482. find_library(COCOA_LIBRARY Cocoa)
  483. set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  484. elseif (WIN32)
  485. # WSAPoll and SHGetKnownFolderPath (AppData/Roaming) didn't exist before WinNT 6.x (Vista)
  486. add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600)
  487. set(PLATFORM_LIBRARIES winmm ws2_32)
  488. if (MINGW)
  489. # PSAPI is the Process Status API
  490. set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} psapi imm32 version)
  491. endif()
  492. elseif (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
  493. set(PLATFORM_LIBRARIES rt)
  494. endif()
  495. # Setup a custom clang-format target (if clang-format can be found) that will run
  496. # against all the src files. This should be used before making a pull request.
  497. # =======================================================================
  498. set(CLANG_FORMAT_POSTFIX "-10")
  499. find_program(CLANG_FORMAT
  500. NAMES clang-format${CLANG_FORMAT_POSTFIX}
  501. clang-format
  502. PATHS ${PROJECT_BINARY_DIR}/externals)
  503. # if find_program doesn't find it, try to download from externals
  504. if (NOT CLANG_FORMAT)
  505. if (WIN32 AND NOT CMAKE_CROSSCOMPILING)
  506. message(STATUS "Clang format not found! Downloading...")
  507. set(CLANG_FORMAT "${PROJECT_BINARY_DIR}/externals/clang-format${CLANG_FORMAT_POSTFIX}.exe")
  508. file(DOWNLOAD
  509. https://github.com/yuzu-emu/ext-windows-bin/raw/master/clang-format${CLANG_FORMAT_POSTFIX}.exe
  510. "${CLANG_FORMAT}" SHOW_PROGRESS
  511. STATUS DOWNLOAD_SUCCESS)
  512. if (NOT DOWNLOAD_SUCCESS EQUAL 0)
  513. message(WARNING "Could not download clang format! Disabling the clang format target")
  514. file(REMOVE ${CLANG_FORMAT})
  515. unset(CLANG_FORMAT)
  516. endif()
  517. else()
  518. message(WARNING "Clang format not found! Disabling the clang format target")
  519. endif()
  520. endif()
  521. if (CLANG_FORMAT)
  522. set(SRCS ${PROJECT_SOURCE_DIR}/src)
  523. set(CCOMMENT "Running clang format against all the .h and .cpp files in src/")
  524. if (WIN32)
  525. add_custom_target(clang-format
  526. COMMAND powershell.exe -Command "Get-ChildItem '${SRCS}/*' -Include *.cpp,*.h -Recurse | Foreach {&'${CLANG_FORMAT}' -i $_.fullname}"
  527. COMMENT ${CCOMMENT})
  528. elseif(MINGW)
  529. add_custom_target(clang-format
  530. COMMAND find `cygpath -u ${SRCS}` -iname *.h -o -iname *.cpp | xargs `cygpath -u ${CLANG_FORMAT}` -i
  531. COMMENT ${CCOMMENT})
  532. else()
  533. add_custom_target(clang-format
  534. COMMAND find ${SRCS} -iname *.h -o -iname *.cpp | xargs ${CLANG_FORMAT} -i
  535. COMMENT ${CCOMMENT})
  536. endif()
  537. unset(SRCS)
  538. unset(CCOMMENT)
  539. endif()
  540. # Include source code
  541. # ===================
  542. # This function should be passed a list of all files in a target. It will automatically generate
  543. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  544. # one in the filesystem.
  545. function(create_target_directory_groups target_name)
  546. # Place any files that aren't in the source list in a separate group so that they don't get in
  547. # the way.
  548. source_group("Other Files" REGULAR_EXPRESSION ".")
  549. get_target_property(target_sources "${target_name}" SOURCES)
  550. foreach(file_name IN LISTS target_sources)
  551. get_filename_component(dir_name "${file_name}" PATH)
  552. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  553. string(REPLACE "/" "\\" group_name "${dir_name}")
  554. source_group("${group_name}" FILES "${file_name}")
  555. endforeach()
  556. endfunction()
  557. # Prevent boost from linking against libs when building
  558. add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY
  559. -DBOOST_SYSTEM_NO_LIB
  560. -DBOOST_DATE_TIME_NO_LIB
  561. -DBOOST_REGEX_NO_LIB
  562. )
  563. enable_testing()
  564. add_subdirectory(externals)
  565. add_subdirectory(src)
  566. # Set yuzu project or yuzu-cmd project as default StartUp Project in Visual Studio depending on whether QT is enabled or not
  567. if(ENABLE_QT)
  568. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu)
  569. else()
  570. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu-cmd)
  571. endif()
  572. # Installation instructions
  573. # =========================
  574. # Install freedesktop.org metadata files, following those specifications:
  575. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  576. # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  577. # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  578. if(ENABLE_QT AND UNIX AND NOT APPLE)
  579. install(FILES "${PROJECT_SOURCE_DIR}/dist/yuzu.desktop"
  580. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
  581. install(FILES "${PROJECT_SOURCE_DIR}/dist/yuzu.svg"
  582. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/scalable/apps")
  583. install(FILES "${PROJECT_SOURCE_DIR}/dist/yuzu.xml"
  584. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/mime/packages")
  585. endif()