CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # CMake 2.8.11 required for Qt5 settings to be applied automatically on
  2. # dependent libraries.
  3. cmake_minimum_required(VERSION 2.8.11)
  4. function(download_bundled_external remote_path lib_name prefix_var)
  5. set(prefix "${CMAKE_BINARY_DIR}/externals/${lib_name}")
  6. if (NOT EXISTS "${prefix}")
  7. message(STATUS "Downloading binaries for ${lib_name}...")
  8. file(DOWNLOAD
  9. https://github.com/citra-emu/ext-windows-bin/raw/master/${remote_path}${lib_name}.7z
  10. "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z" SHOW_PROGRESS)
  11. execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z"
  12. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/externals")
  13. endif()
  14. message(STATUS "Using bundled binaries at ${prefix}")
  15. set(${prefix_var} "${prefix}" PARENT_SCOPE)
  16. endfunction()
  17. include(CheckSymbolExists)
  18. function(detect_architecture symbol arch)
  19. if (NOT DEFINED ARCHITECTURE)
  20. set(CMAKE_REQUIRED_QUIET 1)
  21. check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
  22. unset(CMAKE_REQUIRED_QUIET)
  23. # The output variable needs to be unique across invocations otherwise
  24. # CMake's crazy scope rules will keep it defined
  25. if (ARCHITECTURE_${arch})
  26. set(ARCHITECTURE "${arch}" PARENT_SCOPE)
  27. set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
  28. add_definitions(-DARCHITECTURE_${arch}=1)
  29. endif()
  30. endif()
  31. endfunction()
  32. project(citra)
  33. if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit)
  34. message(STATUS "Copying pre-commit hook")
  35. file(COPY hooks/pre-commit
  36. DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks)
  37. endif()
  38. if (MSVC)
  39. detect_architecture("_M_AMD64" x86_64)
  40. detect_architecture("_M_IX86" x86)
  41. detect_architecture("_M_ARM" ARM)
  42. else()
  43. detect_architecture("__x86_64__" x86_64)
  44. detect_architecture("__i386__" x86)
  45. detect_architecture("__arm__" ARM)
  46. endif()
  47. if (NOT DEFINED ARCHITECTURE)
  48. set(ARCHITECTURE "GENERIC")
  49. set(ARCHITECTURE_GENERIC 1)
  50. add_definitions(-DARCHITECTURE_GENERIC=1)
  51. endif()
  52. message(STATUS "Target architecture: ${ARCHITECTURE}")
  53. if (NOT MSVC)
  54. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-attributes -pthread")
  55. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
  56. if (ARCHITECTURE_x86_64)
  57. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")
  58. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1")
  59. endif()
  60. else()
  61. # Silence "deprecation" warnings
  62. add_definitions(/D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE)
  63. # Avoid windows.h junk
  64. add_definitions(/DNOMINMAX)
  65. # set up output paths for executable binaries (.exe-files, and .dll-files on DLL-capable platforms)
  66. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  67. set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
  68. # Tweak optimization settings
  69. # As far as I can tell, there's no way to override the CMake defaults while leaving user
  70. # changes intact, so we'll just clobber everything and say sorry.
  71. message(STATUS "Cache compiler flags ignored, please edit CMakeLists.txt to change the flags.")
  72. # /W3 - Level 3 warnings
  73. # /MP - Multi-threaded compilation
  74. # /Zi - Output debugging information
  75. # /Zo - enahnced debug info for optimized builds
  76. set(CMAKE_C_FLAGS "/W3 /MP /Zi /Zo" CACHE STRING "" FORCE)
  77. # /GR- - Disable RTTI
  78. # /EHsc - C++-only exception handling semantics
  79. set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} /GR- /EHsc" CACHE STRING "" FORCE)
  80. # /MDd - Multi-threaded Debug Runtime DLL
  81. set(CMAKE_C_FLAGS_DEBUG "/Od /MDd" CACHE STRING "" FORCE)
  82. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING "" FORCE)
  83. # /O2 - Optimization level 2
  84. # /GS- - No stack buffer overflow checks
  85. # /MD - Multi-threaded runtime DLL
  86. set(CMAKE_C_FLAGS_RELEASE "/O2 /GS- /MD" CACHE STRING "" FORCE)
  87. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
  88. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG" CACHE STRING "" FORCE)
  89. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/DEBUG /INCREMENTAL:NO /OPT:REF,ICF" CACHE STRING "" FORCE)
  90. endif()
  91. add_definitions(-DSINGLETHREADED)
  92. # CMake seems to only define _DEBUG on Windows
  93. set_property(DIRECTORY APPEND PROPERTY
  94. COMPILE_DEFINITIONS $<$<CONFIG:Debug>:_DEBUG> $<$<NOT:$<CONFIG:Debug>>:NDEBUG>)
  95. find_package(PNG QUIET)
  96. if (PNG_FOUND)
  97. add_definitions(-DHAVE_PNG)
  98. else()
  99. message(STATUS "libpng not found. Some debugging features have been disabled.")
  100. endif()
  101. find_package(Boost 1.57.0 QUIET)
  102. if (Boost_FOUND)
  103. include_directories(${Boost_INCLUDE_DIRS})
  104. else()
  105. message(STATUS "Boost 1.57.0 or newer not found, falling back to externals")
  106. include_directories(externals/boost)
  107. endif()
  108. # Include bundled CMake modules
  109. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/externals/cmake-modules")
  110. find_package(OpenGL REQUIRED)
  111. include_directories(${OPENGL_INCLUDE_DIR})
  112. option(ENABLE_GLFW "Enable the GLFW frontend" ON)
  113. option(CITRA_USE_BUNDLED_GLFW "Download bundled GLFW binaries" OFF)
  114. if (ENABLE_GLFW)
  115. if (CITRA_USE_BUNDLED_GLFW)
  116. # Detect toolchain and platform
  117. if (MSVC14 AND ARCHITECTURE_x86_64)
  118. set(GLFW_VER "glfw-3.1.1-msvc2015_64")
  119. elseif (MSVC12 AND ARCHITECTURE_x86_64)
  120. set(GLFW_VER "glfw-3.1.1-msvc2013_64")
  121. else()
  122. message(FATAL_ERROR "No bundled GLFW binaries for your toolchain. Disable CITRA_USE_BUNDLED_GLFW and provide your own.")
  123. endif()
  124. if (DEFINED GLFW_VER)
  125. download_bundled_external("glfw/" ${GLFW_VER} GLFW_PREFIX)
  126. endif()
  127. set(GLFW_INCLUDE_DIRS "${GLFW_PREFIX}/include" CACHE PATH "Path to GLFW3 headers")
  128. set(GLFW_LIBRARY_DIRS "${GLFW_PREFIX}/lib" CACHE PATH "Path to GLFW3 libraries")
  129. set(GLFW_LIBRARIES glfw3)
  130. else()
  131. find_package(PkgConfig REQUIRED)
  132. pkg_search_module(GLFW REQUIRED glfw3)
  133. endif()
  134. endif()
  135. IF (APPLE)
  136. FIND_LIBRARY(COCOA_LIBRARY Cocoa) # Umbrella framework for everything GUI-related
  137. FIND_LIBRARY(IOKIT_LIBRARY IOKit) # GLFW dependency
  138. FIND_LIBRARY(COREVIDEO_LIBRARY CoreVideo) # GLFW dependency
  139. set(PLATFORM_LIBRARIES iconv ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
  140. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  141. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
  142. ELSEIF(MINGW)
  143. # GCC does not support codecvt, so use iconv instead
  144. # PSAPI is the Process Status API
  145. set(PLATFORM_LIBRARIES winmm ws2_32 psapi iconv)
  146. # WSAPoll functionality doesn't exist before WinNT 6.x (Vista and up)
  147. add_definitions(-D_WIN32_WINNT=0x0600)
  148. ELSEIF(WIN32)
  149. set(PLATFORM_LIBRARIES winmm ws2_32)
  150. ELSE()
  151. set(PLATFORM_LIBRARIES rt)
  152. ENDIF (APPLE)
  153. option(ENABLE_QT "Enable the Qt frontend" ON)
  154. option(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" OFF)
  155. option(CITRA_FORCE_QT4 "Use Qt4 even if Qt5 is available." OFF)
  156. if (ENABLE_QT)
  157. if (CITRA_USE_BUNDLED_QT)
  158. if (MSVC14 AND ARCHITECTURE_x86_64)
  159. set(QT_VER qt-5.5-msvc2015_64)
  160. else()
  161. message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable CITRA_USE_BUNDLED_QT and provide your own.")
  162. endif()
  163. if (DEFINED QT_VER)
  164. download_bundled_external("qt/" ${QT_VER} QT_PREFIX)
  165. endif()
  166. set(QT_PREFIX_HINT HINTS "${QT_PREFIX}")
  167. else()
  168. # Passing an empty HINTS seems to cause default system paths to get ignored in CMake 2.8 so
  169. # make sure to not pass anything if we don't have one.
  170. set(QT_PREFIX_HINT)
  171. endif()
  172. if (NOT CITRA_FORCE_QT4)
  173. find_package(Qt5 COMPONENTS Widgets OpenGL ${QT_PREFIX_HINT})
  174. set(CITRA_QT_LIBS Qt5::Widgets Qt5::OpenGL)
  175. endif()
  176. if (CITRA_FORCE_QT4 OR NOT Qt5_FOUND)
  177. # Try to fallback to Qt4
  178. find_package(Qt4 REQUIRED COMPONENTS QtGui QtOpenGL ${QT_PREFIX_HINT})
  179. set(CITRA_QT_LIBS Qt4::QtGui Qt4::QtOpenGL)
  180. endif()
  181. endif()
  182. # This function should be passed a list of all files in a target. It will automatically generate
  183. # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
  184. # one in the filesystem.
  185. function(create_directory_groups)
  186. # Place any files that aren't in the source list in a separate group so that they don't get in
  187. # the way.
  188. source_group("Other Files" REGULAR_EXPRESSION ".")
  189. foreach(file_name ${ARGV})
  190. get_filename_component(dir_name "${file_name}" PATH)
  191. # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
  192. string(REPLACE "/" "\\" group_name "${dir_name}")
  193. source_group("${group_name}" FILES "${file_name}")
  194. endforeach()
  195. endfunction()
  196. # generate git revision information
  197. include(GetGitRevisionDescription)
  198. get_git_head_revision(GIT_REF_SPEC GIT_REV)
  199. git_describe(GIT_DESC --always --long --dirty)
  200. git_branch_name(GIT_BRANCH)
  201. set(INI_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/externals/inih")
  202. include_directories(${INI_PREFIX})
  203. add_subdirectory(${INI_PREFIX})
  204. add_subdirectory(externals/glad)
  205. include_directories(externals/microprofile)
  206. include_directories(externals/nihstro/include)
  207. if (MSVC)
  208. add_subdirectory(externals/getopt)
  209. endif()
  210. # process subdirectories
  211. if(ENABLE_QT)
  212. include_directories(externals/qhexedit)
  213. add_subdirectory(externals/qhexedit)
  214. endif()
  215. add_subdirectory(src)
  216. # Install freedesktop.org metadata files, following those specifications:
  217. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
  218. # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  219. # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
  220. if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|OpenBSD|NetBSD")
  221. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.desktop"
  222. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
  223. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.svg"
  224. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/pixmaps")
  225. install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.xml"
  226. DESTINATION "${CMAKE_INSTALL_PREFIX}/share/mime/packages")
  227. endif()