CMakeLists.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. 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. option(ENABLE_QT "Enable the Qt frontend" ON)
  13. CMAKE_DEPENDENT_OPTION(YUZU_USE_BUNDLED_QT "Download bundled Qt binaries" ON "ENABLE_SDL2;MSVC" OFF)
  14. if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
  15. message(STATUS "Copying pre-commit hook")
  16. file(COPY hooks/pre-commit
  17. DESTINATION ${CMAKE_SOURCE_DIR}/.git/hooks)
  18. endif()
  19. # Sanity check : Check that all submodules are present
  20. # =======================================================================
  21. function(check_submodules_present)
  22. file(READ "${CMAKE_SOURCE_DIR}/.gitmodules" gitmodules)
  23. string(REGEX MATCHALL "path *= *[^ \t\r\n]*" gitmodules ${gitmodules})
  24. foreach(module ${gitmodules})
  25. string(REGEX REPLACE "path *= *" "" module ${module})
  26. if (NOT EXISTS "${CMAKE_SOURCE_DIR}/${module}/.git")
  27. message(FATAL_ERROR "Git submodule ${module} not found. "
  28. "Please run: git submodule update --init --recursive")
  29. endif()
  30. endforeach()
  31. endfunction()
  32. check_submodules_present()
  33. # Detect current compilation architecture and create standard definitions
  34. # =======================================================================
  35. include(CheckSymbolExists)
  36. function(detect_architecture symbol arch)
  37. if (NOT DEFINED ARCHITECTURE)
  38. set(CMAKE_REQUIRED_QUIET 1)
  39. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  40. unset(CMAKE_REQUIRED_QUIET)
  41. # The output variable needs to be unique across invocations otherwise
  42. # CMake's crazy scope rules will keep it defined
  43. if (ARCHITECTURE_${arch})
  44. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  45. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  46. add_definitions(-DARCHITECTURE_${arch}=1)
  47. endif()
  48. endif()
  49. endfunction()
  50. if (NOT ENABLE_GENERIC)
  51. if (MSVC)
  52. detect_architecture("_M_AMD64" x86_64)
  53. detect_architecture("_M_IX86" x86)
  54. detect_architecture("_M_ARM" ARM)
  55. else()
  56. detect_architecture("__x86_64__" x86_64)
  57. detect_architecture("__i386__" x86)
  58. detect_architecture("__arm__" ARM)
  59. endif()
  60. endif()
  61. if (NOT DEFINED ARCHITECTURE)
  62. set(ARCHITECTURE "GENERIC")
  63. set(ARCHITECTURE_GENERIC 1)
  64. add_definitions(-DARCHITECTURE_GENERIC=1)
  65. endif()
  66. message(STATUS "Target architecture: ${ARCHITECTURE}")
  67. # Configure compilation flags
  68. # ===========================
  69. set(CMAKE_CXX_STANDARD 17)
  70. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  71. if (NOT MSVC)
  72. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-attributes")
  73. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  74. if (MINGW)
  75. add_definitions(-DMINGW_HAS_SECURE_API)
  76. if (MINGW_STATIC_BUILD)
  77. add_definitions(-DQT_STATICPLUGIN)
  78. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
  79. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
  80. endif()
  81. endif()
  82. else()
  83. # Silence "deprecation" warnings
  84. add_definitions(/D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE /D_SCL_SECURE_NO_WARNINGS)
  85. # Avoid windows.h junk
  86. add_definitions(/DNOMINMAX)
  87. # Avoid windows.h from including some usually unused libs like winsocks.h, since this might cause some redefinition errors.
  88. add_definitions(/DWIN32_LEAN_AND_MEAN)
  89. # set up output paths for executable binaries (.exe-files, and .dll-files on DLL-capable platforms)
  90. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  91. set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
  92. # Tweak optimization settings
  93. # As far as I can tell, there's no way to override the CMake defaults while leaving user
  94. # changes intact, so we'll just clobber everything and say sorry.
  95. message(STATUS "Cache compiler flags ignored, please edit CMakeLists.txt to change the flags.")
  96. # /W3 - Level 3 warnings
  97. # /MP - Multi-threaded compilation
  98. # /Zi - Output debugging information
  99. # /Zo - enhanced debug info for optimized builds
  100. # /permissive- - enables stricter C++ standards conformance checks
  101. set(CMAKE_C_FLAGS "/W3 /MP /Zi /Zo /permissive-" CACHE STRING "" FORCE)
  102. # /EHsc - C++-only exception handling semantics
  103. # /Zc:throwingNew - let codegen assume `operator new` will never return null
  104. # /Zc:inline - let codegen omit inline functions in object files
  105. set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} /EHsc /std:c++latest /Zc:throwingNew,inline" CACHE STRING "" FORCE)
  106. # /MDd - Multi-threaded Debug Runtime DLL
  107. set(CMAKE_C_FLAGS_DEBUG "/Od /MDd" CACHE STRING "" FORCE)
  108. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING "" FORCE)
  109. # /O2 - Optimization level 2
  110. # /GS- - No stack buffer overflow checks
  111. # /MD - Multi-threaded runtime DLL
  112. set(CMAKE_C_FLAGS_RELEASE "/O2 /GS- /MD" CACHE STRING "" FORCE)
  113. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
  114. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG /MANIFEST:NO" CACHE STRING "" FORCE)
  115. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/DEBUG /MANIFEST:NO /INCREMENTAL:NO /OPT:REF,ICF" CACHE STRING "" FORCE)
  116. endif()
  117. # Fix GCC C++17 and Boost.ICL incompatibility (needed to build dynarmic)
  118. # See https://bugzilla.redhat.com/show_bug.cgi?id=1485641#c1
  119. if (CMAKE_COMPILER_IS_GNUCC)
  120. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-new-ttp-matching")
  121. endif()
  122. # Set file offset size to 64 bits.
  123. #
  124. # On modern Unixes, this is typically already the case. The lone exception is
  125. # glibc, which may default to 32 bits. glibc allows this to be configured
  126. # by setting _FILE_OFFSET_BITS.
  127. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  128. add_definitions(-D_FILE_OFFSET_BITS=64)
  129. endif()
  130. # CMake seems to only define _DEBUG on Windows
  131. set_property(DIRECTORY APPEND PROPERTY
  132. COMPILE_DEFINITIONS $<$<CONFIG:Debug>:_DEBUG> $<$<NOT:$<CONFIG:Debug>>:NDEBUG>)
  133. # System imported libraries
  134. # ======================
  135. find_package(Boost 1.63.0 QUIET)
  136. if (NOT Boost_FOUND)
  137. message(STATUS "Boost 1.63.0 or newer not found, falling back to externals")
  138. set(BOOST_ROOT "${CMAKE_SOURCE_DIR}/externals/boost")
  139. set(Boost_NO_SYSTEM_PATHS OFF)
  140. find_package(Boost QUIET REQUIRED)
  141. endif()
  142. # Output binaries to bin/
  143. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  144. # Prefer the -pthread flag on Linux.
  145. set(THREADS_PREFER_PTHREAD_FLAG ON)
  146. find_package(Threads REQUIRED)
  147. if (ENABLE_SDL2)
  148. if (YUZU_USE_BUNDLED_SDL2)
  149. # Detect toolchain and platform
  150. if (MSVC14 AND ARCHITECTURE_x86_64)
  151. set(SDL2_VER "SDL2-2.0.5")
  152. else()
  153. message(FATAL_ERROR "No bundled SDL2 binaries for your toolchain. Disable YUZU_USE_BUNDLED_SDL2 and provide your own.")
  154. endif()
  155. if (DEFINED SDL2_VER)
  156. download_bundled_external("sdl2/" ${SDL2_VER} SDL2_PREFIX)
  157. endif()
  158. set(SDL2_FOUND YES)
  159. set(SDL2_INCLUDE_DIR "${SDL2_PREFIX}/include" CACHE PATH "Path to SDL2 headers")
  160. set(SDL2_LIBRARY "${SDL2_PREFIX}/lib/x64/SDL2.lib" CACHE PATH "Path to SDL2 library")
  161. set(SDL2_DLL_DIR "${SDL2_PREFIX}/lib/x64/" CACHE PATH "Path to SDL2.dll")
  162. else()
  163. find_package(SDL2 REQUIRED)
  164. endif()
  165. if (SDL2_FOUND)
  166. # TODO(yuriks): Make FindSDL2.cmake export an IMPORTED library instead
  167. add_library(SDL2 INTERFACE)
  168. target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARY}")
  169. target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}")
  170. endif()
  171. else()
  172. set(SDL2_FOUND NO)
  173. endif()
  174. # If unicorn isn't found, msvc -> download bundled unicorn; everyone else -> build external
  175. find_package(Unicorn QUIET)
  176. if (NOT UNICORN_FOUND)
  177. if (MSVC)
  178. message(STATUS "unicorn not found, falling back to bundled")
  179. # Detect toolchain and platform
  180. if (MSVC14 AND ARCHITECTURE_x86_64)
  181. set(UNICORN_VER "unicorn-yuzu")
  182. else()
  183. message(FATAL_ERROR "No bundled Unicorn binaries for your toolchain. Disable YUZU_USE_BUNDLED_UNICORN and provide your own.")
  184. endif()
  185. if (DEFINED UNICORN_VER)
  186. download_bundled_external("unicorn/" ${UNICORN_VER} UNICORN_PREFIX)
  187. endif()
  188. if (DEFINED UNICORN_VER)
  189. download_bundled_external("unicorn/" ${UNICORN_VER} UNICORN_PREFIX)
  190. endif()
  191. set(UNICORN_FOUND YES)
  192. set(LIBUNICORN_INCLUDE_DIR "${UNICORN_PREFIX}/include" CACHE PATH "Path to Unicorn headers" FORCE)
  193. set(LIBUNICORN_LIBRARY "${UNICORN_PREFIX}/lib/x64/unicorn_dynload.lib" CACHE PATH "Path to Unicorn library" FORCE)
  194. set(UNICORN_DLL_DIR "${UNICORN_PREFIX}/lib/x64/" CACHE PATH "Path to unicorn.dll" FORCE)
  195. else()
  196. message(STATUS "unicorn not found, falling back to externals")
  197. if (MINGW)
  198. set(UNICORN_LIB_NAME "unicorn.a")
  199. else()
  200. set(UNICORN_LIB_NAME "libunicorn.a")
  201. endif()
  202. set(UNICORN_FOUND YES)
  203. set(UNICORN_PREFIX ${CMAKE_SOURCE_DIR}/externals/unicorn)
  204. set(LIBUNICORN_LIBRARY "${UNICORN_PREFIX}/${UNICORN_LIB_NAME}" CACHE PATH "Path to Unicorn library" FORCE)
  205. set(LIBUNICORN_INCLUDE_DIR "${UNICORN_PREFIX}/include" CACHE PATH "Path to Unicorn headers" FORCE)
  206. set(UNICORN_DLL_DIR "${UNICORN_PREFIX}/" CACHE PATH "Path to unicorn dynamic library" FORCE)
  207. find_package(PythonInterp 2.7 REQUIRED)
  208. add_custom_command(OUTPUT ${LIBUNICORN_LIBRARY}
  209. COMMAND ${CMAKE_COMMAND} -E env UNICORN_ARCHS="aarch64" PYTHON="${PYTHON_EXECUTABLE}" /bin/sh make.sh
  210. WORKING_DIRECTORY ${UNICORN_PREFIX}
  211. )
  212. # ALL makes this custom target build every time
  213. # but it won't actually build if LIBUNICORN_LIBRARY is up to date
  214. add_custom_target(unicorn-build ALL
  215. DEPENDS ${LIBUNICORN_LIBRARY}
  216. )
  217. unset(UNICORN_LIB_NAME)
  218. endif()
  219. endif()
  220. if (UNICORN_FOUND)
  221. add_library(unicorn INTERFACE)
  222. target_link_libraries(unicorn INTERFACE "${LIBUNICORN_LIBRARY}")
  223. target_include_directories(unicorn INTERFACE "${LIBUNICORN_INCLUDE_DIR}")
  224. else()
  225. message(FATAL_ERROR "Could not find or build unicorn which is required.")
  226. endif()
  227. if (ENABLE_QT)
  228. if (YUZU_USE_BUNDLED_QT)
  229. if (MSVC14 AND ARCHITECTURE_x86_64)
  230. set(QT_VER qt-5.7-msvc2015_64)
  231. else()
  232. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable YUZU_USE_BUNDLED_QT and provide your own.")
  233. endif()
  234. if (DEFINED QT_VER)
  235. download_bundled_external("qt/" ${QT_VER} QT_PREFIX)
  236. endif()
  237. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  238. else()
  239. # Passing an empty HINTS seems to cause default system paths to get ignored in CMake 2.8 so
  240. # make sure to not pass anything if we don't have one.
  241. set(QT_PREFIX_HINT)
  242. endif()
  243. find_package(Qt5 REQUIRED COMPONENTS Widgets OpenGL ${QT_PREFIX_HINT})
  244. endif()
  245. # Platform-specific library requirements
  246. # ======================================
  247. IF (APPLE)
  248. FIND_LIBRARY(COCOA_LIBRARY Cocoa) # Umbrella framework for everything GUI-related
  249. set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  250. if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  251. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  252. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
  253. endif()
  254. ELSEIF (WIN32)
  255. # WSAPoll and SHGetKnownFolderPath (AppData/Roaming) didn't exist before WinNT 6.x (Vista)
  256. add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600)
  257. set(PLATFORM_LIBRARIES winmm ws2_32)
  258. IF (MINGW)
  259. # PSAPI is the Process Status API
  260. set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} psapi imm32 version)
  261. ENDIF (MINGW)
  262. ELSEIF (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
  263. set(PLATFORM_LIBRARIES rt)
  264. ENDIF (APPLE)
  265. # MINGW: GCC does not support codecvt, so use iconv instead
  266. if (UNIX OR MINGW)
  267. find_library(ICONV_LIBRARY NAMES iconv)
  268. if (ICONV_LIBRARY)
  269. list(APPEND PLATFORM_LIBRARIES ${ICONV_LIBRARY})
  270. endif()
  271. endif()
  272. # Setup a custom clang-format target (if clang-format can be found) that will run
  273. # against all the src files. This should be used before making a pull request.
  274. # =======================================================================
  275. set(CLANG_FORMAT_POSTFIX "-6.0")
  276. find_program(CLANG_FORMAT
  277. NAMES clang-format${CLANG_FORMAT_POSTFIX}
  278. clang-format
  279. PATHS ${CMAKE_BINARY_DIR}/externals)
  280. # if find_program doesn't find it, try to download from externals
  281. if (NOT CLANG_FORMAT)
  282. if (WIN32)
  283. message(STATUS "Clang format not found! Downloading...")
  284. set(CLANG_FORMAT "${CMAKE_BINARY_DIR}/externals/clang-format${CLANG_FORMAT_POSTFIX}.exe")
  285. file(DOWNLOAD
  286. https://github.com/yuzu-emu/ext-windows-bin/raw/master/clang-format${CLANG_FORMAT_POSTFIX}.exe
  287. "${CLANG_FORMAT}" SHOW_PROGRESS
  288. STATUS DOWNLOAD_SUCCESS)
  289. if (NOT DOWNLOAD_SUCCESS EQUAL 0)
  290. message(WARNING "Could not download clang format! Disabling the clang format target")
  291. file(REMOVE ${CLANG_FORMAT})
  292. unset(CLANG_FORMAT)
  293. endif()
  294. else()
  295. message(WARNING "Clang format not found! Disabling the clang format target")
  296. endif()
  297. endif()
  298. if (CLANG_FORMAT)
  299. set(SRCS ${CMAKE_SOURCE_DIR}/src)
  300. set(CCOMMENT "Running clang format against all the .h and .cpp files in src/")
  301. if (WIN32)
  302. add_custom_target(clang-format
  303. COMMAND powershell.exe -Command "${CLANG_FORMAT} -i @(Get-ChildItem -Recurse ${SRCS}/* -Include \'*.h\', \'*.cpp\')"
  304. COMMENT ${CCOMMENT})
  305. elseif(MINGW)
  306. add_custom_target(clang-format
  307. COMMAND find `cygpath -u ${SRCS}` -iname *.h -o -iname *.cpp | xargs `cygpath -u ${CLANG_FORMAT}` -i
  308. COMMENT ${CCOMMENT})
  309. else()
  310. add_custom_target(clang-format
  311. COMMAND find ${SRCS} -iname *.h -o -iname *.cpp | xargs ${CLANG_FORMAT} -i
  312. COMMENT ${CCOMMENT})
  313. endif()
  314. unset(SRCS)
  315. unset(CCOMMENT)
  316. endif()
  317. # Include source code
  318. # ===================
  319. # This function should be passed a list of all files in a target. It will automatically generate
  320. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  321. # one in the filesystem.
  322. function(create_target_directory_groups target_name)
  323. # Place any files that aren't in the source list in a separate group so that they don't get in
  324. # the way.
  325. source_group("Other Files" REGULAR_EXPRESSION ".")
  326. get_target_property(target_sources "${target_name}" SOURCES)
  327. foreach(file_name IN LISTS target_sources)
  328. get_filename_component(dir_name "${file_name}" PATH)
  329. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  330. string(REPLACE "/" "\\" group_name "${dir_name}")
  331. source_group("${group_name}" FILES "${file_name}")
  332. endforeach()
  333. endfunction()
  334. # Gets a UTC timstamp and sets the provided variable to it
  335. function(get_timestamp _var)
  336. string(TIMESTAMP timestamp UTC)
  337. set(${_var} "${timestamp}" PARENT_SCOPE)
  338. endfunction()
  339. # generate git/build information
  340. include(GetGitRevisionDescription)
  341. get_git_head_revision(GIT_REF_SPEC GIT_REV)
  342. git_describe(GIT_DESC --always --long --dirty)
  343. git_branch_name(GIT_BRANCH)
  344. get_timestamp(BUILD_DATE)
  345. enable_testing()
  346. add_subdirectory(externals)
  347. add_subdirectory(src)
  348. # Installation instructions
  349. # =========================
  350. # Install freedesktop.org metadata files, following those specifications:
  351. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  352. # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  353. # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  354. if(ENABLE_QT AND UNIX AND NOT APPLE)
  355. install(FILES "${CMAKE_SOURCE_DIR}/dist/yuzu.desktop"
  356. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
  357. install(FILES "${CMAKE_SOURCE_DIR}/dist/yuzu.svg"
  358. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/pixmaps")
  359. install(FILES "${CMAKE_SOURCE_DIR}/dist/yuzu.xml"
  360. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/mime/packages")
  361. endif()