CMakeLists.txt 9.9 KB

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