Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 rivejä
14 KiB

5 vuotta sitten
  1. set(CMAKE_LEGACY_CYGWIN_WIN32 OFF)
  2. project(GLFW C)
  3. cmake_minimum_required(VERSION 2.8.12)
  4. if (NOT CMAKE_VERSION VERSION_LESS "3.0")
  5. # Until all major package systems have moved to CMake 3,
  6. # we stick with the older INSTALL_NAME_DIR mechanism
  7. cmake_policy(SET CMP0042 OLD)
  8. endif()
  9. set(GLFW_VERSION_MAJOR "3")
  10. set(GLFW_VERSION_MINOR "2")
  11. set(GLFW_VERSION_PATCH "1")
  12. set(GLFW_VERSION_EXTRA "")
  13. set(GLFW_VERSION "${GLFW_VERSION_MAJOR}.${GLFW_VERSION_MINOR}")
  14. set(GLFW_VERSION_FULL "${GLFW_VERSION}.${GLFW_VERSION_PATCH}${GLFW_VERSION_EXTRA}")
  15. set(LIB_SUFFIX "" CACHE STRING "Takes an empty string or 64. Directory where lib will be installed: lib or lib64")
  16. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  17. option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
  18. option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ON)
  19. option(GLFW_BUILD_TESTS "Build the GLFW test programs" ON)
  20. option(GLFW_BUILD_DOCS "Build the GLFW documentation" ON)
  21. option(GLFW_INSTALL "Generate installation target" ON)
  22. option(GLFW_VULKAN_STATIC "Use the Vulkan loader statically linked into application" OFF)
  23. option(GLFW_DOCUMENT_INTERNALS "Include internals in documentation" OFF)
  24. if (WIN32)
  25. option(GLFW_USE_HYBRID_HPG "Force use of high-performance GPU on hybrid systems" OFF)
  26. endif()
  27. if (APPLE)
  28. option(GLFW_USE_CHDIR "Make glfwInit chdir to Contents/Resources" ON)
  29. option(GLFW_USE_MENUBAR "Populate the menu bar on first window creation" ON)
  30. option(GLFW_USE_RETINA "Use the full resolution of Retina displays" ON)
  31. endif()
  32. if (UNIX AND NOT APPLE)
  33. option(GLFW_USE_WAYLAND "Use Wayland for window creation" OFF)
  34. option(GLFW_USE_MIR "Use Mir for window creation" OFF)
  35. endif()
  36. if (MSVC)
  37. option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON)
  38. endif()
  39. if (BUILD_SHARED_LIBS)
  40. set(_GLFW_BUILD_DLL 1)
  41. endif()
  42. if (BUILD_SHARED_LIBS AND UNIX)
  43. # On Unix-like systems, shared libraries can use the soname system.
  44. set(GLFW_LIB_NAME glfw)
  45. else()
  46. set(GLFW_LIB_NAME glfw3)
  47. endif()
  48. if (GLFW_VULKAN_STATIC)
  49. set(_GLFW_VULKAN_STATIC 1)
  50. endif()
  51. list(APPEND CMAKE_MODULE_PATH "${GLFW_SOURCE_DIR}/CMake/modules")
  52. find_package(Threads REQUIRED)
  53. find_package(Vulkan)
  54. if (GLFW_BUILD_DOCS)
  55. set(DOXYGEN_SKIP_DOT TRUE)
  56. find_package(Doxygen)
  57. endif()
  58. #--------------------------------------------------------------------
  59. # Set compiler specific flags
  60. #--------------------------------------------------------------------
  61. if (MSVC)
  62. if (NOT USE_MSVC_RUNTIME_LIBRARY_DLL)
  63. foreach (flag CMAKE_C_FLAGS
  64. CMAKE_C_FLAGS_DEBUG
  65. CMAKE_C_FLAGS_RELEASE
  66. CMAKE_C_FLAGS_MINSIZEREL
  67. CMAKE_C_FLAGS_RELWITHDEBINFO)
  68. if (${flag} MATCHES "/MD")
  69. string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
  70. endif()
  71. if (${flag} MATCHES "/MDd")
  72. string(REGEX REPLACE "/MDd" "/MTd" ${flag} "${${flag}}")
  73. endif()
  74. endforeach()
  75. endif()
  76. endif()
  77. if (MINGW)
  78. # Workaround for legacy MinGW not providing XInput and DirectInput
  79. include(CheckIncludeFile)
  80. check_include_file(dinput.h DINPUT_H_FOUND)
  81. check_include_file(xinput.h XINPUT_H_FOUND)
  82. if (NOT DINPUT_H_FOUND OR NOT XINPUT_H_FOUND)
  83. list(APPEND glfw_INCLUDE_DIRS "${GLFW_SOURCE_DIR}/deps/mingw")
  84. endif()
  85. # Enable link-time exploit mitigation features enabled by default on MSVC
  86. include(CheckCCompilerFlag)
  87. # Compatibility with data execution prevention (DEP)
  88. set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
  89. check_c_compiler_flag("" _GLFW_HAS_DEP)
  90. if (_GLFW_HAS_DEP)
  91. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--nxcompat ${CMAKE_SHARED_LINKER_FLAGS}")
  92. endif()
  93. # Compatibility with address space layout randomization (ASLR)
  94. set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
  95. check_c_compiler_flag("" _GLFW_HAS_ASLR)
  96. if (_GLFW_HAS_ASLR)
  97. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--dynamicbase ${CMAKE_SHARED_LINKER_FLAGS}")
  98. endif()
  99. # Compatibility with 64-bit address space layout randomization (ASLR)
  100. set(CMAKE_REQUIRED_FLAGS "-Wl,--high-entropy-va")
  101. check_c_compiler_flag("" _GLFW_HAS_64ASLR)
  102. if (_GLFW_HAS_64ASLR)
  103. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--high-entropy-va ${CMAKE_SHARED_LINKER_FLAGS}")
  104. endif()
  105. endif()
  106. #--------------------------------------------------------------------
  107. # Detect and select backend APIs
  108. #--------------------------------------------------------------------
  109. if (WIN32)
  110. set(_GLFW_WIN32 1)
  111. message(STATUS "Using Win32 for window creation")
  112. elseif (APPLE)
  113. set(_GLFW_COCOA 1)
  114. message(STATUS "Using Cocoa for window creation")
  115. elseif (UNIX)
  116. if (GLFW_USE_WAYLAND)
  117. set(_GLFW_WAYLAND 1)
  118. message(STATUS "Using Wayland for window creation")
  119. elseif (GLFW_USE_MIR)
  120. set(_GLFW_MIR 1)
  121. message(STATUS "Using Mir for window creation")
  122. else()
  123. set(_GLFW_X11 1)
  124. message(STATUS "Using X11 for window creation")
  125. endif()
  126. else()
  127. message(FATAL_ERROR "No supported platform was detected")
  128. endif()
  129. #--------------------------------------------------------------------
  130. # Add Vulkan static library if requested
  131. #--------------------------------------------------------------------
  132. if (GLFW_VULKAN_STATIC)
  133. if (VULKAN_FOUND AND VULKAN_STATIC_LIBRARY)
  134. list(APPEND glfw_LIBRARIES ${VULKAN_STATIC_LIBRARY})
  135. else()
  136. if (BUILD_SHARED_LIBS OR GLFW_BUILD_EXAMPLES OR GLFW_BUILD_TESTS)
  137. message(FATAL_ERROR "Vulkan loader static library not found")
  138. else()
  139. message(WARNING "Vulkan loader static library not found")
  140. endif()
  141. endif()
  142. endif()
  143. #--------------------------------------------------------------------
  144. # Find and add Unix math and time libraries
  145. #--------------------------------------------------------------------
  146. if (UNIX AND NOT APPLE)
  147. find_library(RT_LIBRARY rt)
  148. mark_as_advanced(RT_LIBRARY)
  149. if (RT_LIBRARY)
  150. list(APPEND glfw_LIBRARIES "${RT_LIBRARY}")
  151. list(APPEND glfw_PKG_LIBS "-lrt")
  152. endif()
  153. find_library(MATH_LIBRARY m)
  154. mark_as_advanced(MATH_LIBRARY)
  155. if (MATH_LIBRARY)
  156. list(APPEND glfw_LIBRARIES "${MATH_LIBRARY}")
  157. list(APPEND glfw_PKG_LIBS "-lm")
  158. endif()
  159. if (CMAKE_DL_LIBS)
  160. list(APPEND glfw_LIBRARIES "${CMAKE_DL_LIBS}")
  161. list(APPEND glfw_PKG_LIBS "-l${CMAKE_DL_LIBS}")
  162. endif()
  163. endif()
  164. #--------------------------------------------------------------------
  165. # Use Win32 for window creation
  166. #--------------------------------------------------------------------
  167. if (_GLFW_WIN32)
  168. list(APPEND glfw_PKG_LIBS "-lgdi32")
  169. if (GLFW_USE_HYBRID_HPG)
  170. set(_GLFW_USE_HYBRID_HPG 1)
  171. endif()
  172. endif()
  173. #--------------------------------------------------------------------
  174. # Use X11 for window creation
  175. #--------------------------------------------------------------------
  176. if (_GLFW_X11)
  177. find_package(X11 REQUIRED)
  178. list(APPEND glfw_PKG_DEPS "x11")
  179. # Set up library and include paths
  180. list(APPEND glfw_INCLUDE_DIRS "${X11_X11_INCLUDE_PATH}")
  181. list(APPEND glfw_LIBRARIES "${X11_X11_LIB}" "${CMAKE_THREAD_LIBS_INIT}")
  182. # Check for XRandR (modern resolution switching and gamma control)
  183. if (NOT X11_Xrandr_FOUND)
  184. message(FATAL_ERROR "The RandR library and headers were not found")
  185. endif()
  186. list(APPEND glfw_INCLUDE_DIRS "${X11_Xrandr_INCLUDE_PATH}")
  187. list(APPEND glfw_LIBRARIES "${X11_Xrandr_LIB}")
  188. list(APPEND glfw_PKG_DEPS "xrandr")
  189. # Check for Xinerama (legacy multi-monitor support)
  190. if (NOT X11_Xinerama_FOUND)
  191. message(FATAL_ERROR "The Xinerama library and headers were not found")
  192. endif()
  193. list(APPEND glfw_INCLUDE_DIRS "${X11_Xinerama_INCLUDE_PATH}")
  194. list(APPEND glfw_LIBRARIES "${X11_Xinerama_LIB}")
  195. list(APPEND glfw_PKG_DEPS "xinerama")
  196. # Check for Xf86VidMode (fallback gamma control)
  197. if (X11_xf86vmode_FOUND)
  198. list(APPEND glfw_INCLUDE_DIRS "${X11_xf86vmode_INCLUDE_PATH}")
  199. list(APPEND glfw_PKG_DEPS "xxf86vm")
  200. if (X11_Xxf86vm_LIB)
  201. list(APPEND glfw_LIBRARIES "${X11_Xxf86vm_LIB}")
  202. else()
  203. # Backwards compatibility (see CMake bug 0006976)
  204. list(APPEND glfw_LIBRARIES Xxf86vm)
  205. endif()
  206. set(_GLFW_HAS_XF86VM TRUE)
  207. endif()
  208. # Check for Xkb (X keyboard extension)
  209. if (NOT X11_Xkb_FOUND)
  210. message(FATAL_ERROR "The X keyboard extension headers were not found")
  211. endif()
  212. list(APPEND glfw_INCLUDE_DIR "${X11_Xkb_INCLUDE_PATH}")
  213. # Check for Xcursor
  214. if (NOT X11_Xcursor_FOUND)
  215. message(FATAL_ERROR "The Xcursor libraries and headers were not found")
  216. endif()
  217. list(APPEND glfw_INCLUDE_DIR "${X11_Xcursor_INCLUDE_PATH}")
  218. list(APPEND glfw_LIBRARIES "${X11_Xcursor_LIB}")
  219. list(APPEND glfw_PKG_DEPS "xcursor")
  220. endif()
  221. #--------------------------------------------------------------------
  222. # Use Wayland for window creation
  223. #--------------------------------------------------------------------
  224. if (_GLFW_WAYLAND)
  225. find_package(ECM REQUIRED NO_MODULE)
  226. list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
  227. find_package(Wayland REQUIRED)
  228. find_package(WaylandScanner REQUIRED)
  229. find_package(WaylandProtocols 1.1 REQUIRED)
  230. list(APPEND glfw_PKG_DEPS "wayland-egl")
  231. list(APPEND glfw_INCLUDE_DIRS "${Wayland_INCLUDE_DIR}")
  232. list(APPEND glfw_LIBRARIES "${Wayland_LIBRARIES}" "${CMAKE_THREAD_LIBS_INIT}")
  233. find_package(XKBCommon REQUIRED)
  234. list(APPEND glfw_PKG_DEPS "xkbcommon")
  235. list(APPEND glfw_INCLUDE_DIRS "${XKBCOMMON_INCLUDE_DIRS}")
  236. list(APPEND glfw_LIBRARIES "${XKBCOMMON_LIBRARY}")
  237. endif()
  238. #--------------------------------------------------------------------
  239. # Use Mir for window creation
  240. #--------------------------------------------------------------------
  241. if (_GLFW_MIR)
  242. find_package(Mir REQUIRED)
  243. list(APPEND glfw_PKG_DEPS "mirclient")
  244. list(APPEND glfw_INCLUDE_DIRS "${MIR_INCLUDE_DIR}")
  245. list(APPEND glfw_LIBRARIES "${MIR_LIBRARIES}" "${CMAKE_THREAD_LIBS_INIT}")
  246. find_package(XKBCommon REQUIRED)
  247. list(APPEND glfw_PKG_DEPS "xkbcommon")
  248. list(APPEND glfw_INCLUDE_DIRS "${XKBCOMMON_INCLUDE_DIRS}")
  249. list(APPEND glfw_LIBRARIES "${XKBCOMMON_LIBRARY}")
  250. endif()
  251. #--------------------------------------------------------------------
  252. # Use Cocoa for window creation and NSOpenGL for context creation
  253. #--------------------------------------------------------------------
  254. if (_GLFW_COCOA)
  255. if (GLFW_USE_MENUBAR)
  256. set(_GLFW_USE_MENUBAR 1)
  257. endif()
  258. if (GLFW_USE_CHDIR)
  259. set(_GLFW_USE_CHDIR 1)
  260. endif()
  261. if (GLFW_USE_RETINA)
  262. set(_GLFW_USE_RETINA 1)
  263. endif()
  264. # Set up library and include paths
  265. find_library(COCOA_FRAMEWORK Cocoa)
  266. find_library(IOKIT_FRAMEWORK IOKit)
  267. find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation)
  268. find_library(CORE_VIDEO_FRAMEWORK CoreVideo)
  269. mark_as_advanced(COCOA_FRAMEWORK
  270. IOKIT_FRAMEWORK
  271. CORE_FOUNDATION_FRAMEWORK
  272. CORE_VIDEO_FRAMEWORK)
  273. list(APPEND glfw_LIBRARIES "${COCOA_FRAMEWORK}"
  274. "${IOKIT_FRAMEWORK}"
  275. "${CORE_FOUNDATION_FRAMEWORK}"
  276. "${CORE_VIDEO_FRAMEWORK}")
  277. set(glfw_PKG_DEPS "")
  278. set(glfw_PKG_LIBS "-framework Cocoa -framework IOKit -framework CoreFoundation -framework CoreVideo")
  279. endif()
  280. #--------------------------------------------------------------------
  281. # Export GLFW library dependencies
  282. #--------------------------------------------------------------------
  283. foreach(arg ${glfw_PKG_DEPS})
  284. set(GLFW_PKG_DEPS "${GLFW_PKG_DEPS} ${arg}")
  285. endforeach()
  286. foreach(arg ${glfw_PKG_LIBS})
  287. set(GLFW_PKG_LIBS "${GLFW_PKG_LIBS} ${arg}")
  288. endforeach()
  289. #--------------------------------------------------------------------
  290. # Create generated files
  291. #--------------------------------------------------------------------
  292. include(CMakePackageConfigHelpers)
  293. set(GLFW_CONFIG_PATH "lib${LIB_SUFFIX}/cmake/glfw3")
  294. configure_package_config_file(src/glfw3Config.cmake.in
  295. src/glfw3Config.cmake
  296. INSTALL_DESTINATION "${GLFW_CONFIG_PATH}"
  297. NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  298. write_basic_package_version_file(src/glfw3ConfigVersion.cmake
  299. VERSION ${GLFW_VERSION_FULL}
  300. COMPATIBILITY SameMajorVersion)
  301. configure_file(src/glfw_config.h.in src/glfw_config.h @ONLY)
  302. configure_file(src/glfw3.pc.in src/glfw3.pc @ONLY)
  303. #--------------------------------------------------------------------
  304. # Add subdirectories
  305. #--------------------------------------------------------------------
  306. add_subdirectory(src)
  307. if (GLFW_BUILD_EXAMPLES)
  308. add_subdirectory(examples)
  309. endif()
  310. if (GLFW_BUILD_TESTS)
  311. add_subdirectory(tests)
  312. endif()
  313. if (DOXYGEN_FOUND AND GLFW_BUILD_DOCS)
  314. add_subdirectory(docs)
  315. endif()
  316. #--------------------------------------------------------------------
  317. # Install files other than the library
  318. # The library is installed by src/CMakeLists.txt
  319. #--------------------------------------------------------------------
  320. if (GLFW_INSTALL)
  321. install(DIRECTORY include/GLFW DESTINATION include
  322. FILES_MATCHING PATTERN glfw3.h PATTERN glfw3native.h)
  323. install(FILES "${GLFW_BINARY_DIR}/src/glfw3Config.cmake"
  324. "${GLFW_BINARY_DIR}/src/glfw3ConfigVersion.cmake"
  325. DESTINATION "${GLFW_CONFIG_PATH}")
  326. install(EXPORT glfwTargets FILE glfw3Targets.cmake
  327. EXPORT_LINK_INTERFACE_LIBRARIES
  328. DESTINATION "${GLFW_CONFIG_PATH}")
  329. install(FILES "${GLFW_BINARY_DIR}/src/glfw3.pc"
  330. DESTINATION "lib${LIB_SUFFIX}/pkgconfig")
  331. # Only generate this target if no higher-level project already has
  332. if (NOT TARGET uninstall)
  333. configure_file(cmake_uninstall.cmake.in
  334. cmake_uninstall.cmake IMMEDIATE @ONLY)
  335. add_custom_target(uninstall
  336. "${CMAKE_COMMAND}" -P
  337. "${GLFW_BINARY_DIR}/cmake_uninstall.cmake")
  338. endif()
  339. endif()