CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. cmake_minimum_required(VERSION 3.0)
  2. project(GLFW VERSION 3.4.0 LANGUAGES C)
  3. set(CMAKE_LEGACY_CYGWIN_WIN32 OFF)
  4. if (POLICY CMP0054)
  5. cmake_policy(SET CMP0054 NEW)
  6. endif()
  7. if (POLICY CMP0077)
  8. cmake_policy(SET CMP0077 NEW)
  9. endif()
  10. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  11. if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
  12. set(GLFW_STANDALONE TRUE)
  13. endif()
  14. option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
  15. option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ${GLFW_STANDALONE})
  16. option(GLFW_BUILD_TESTS "Build the GLFW test programs" ${GLFW_STANDALONE})
  17. option(GLFW_BUILD_DOCS "Build the GLFW documentation" ON)
  18. option(GLFW_INSTALL "Generate installation target" ON)
  19. option(GLFW_VULKAN_STATIC "Assume the Vulkan loader is linked with the application" OFF)
  20. include(GNUInstallDirs)
  21. include(CMakeDependentOption)
  22. cmake_dependent_option(GLFW_USE_OSMESA "Use OSMesa for offscreen context creation" OFF
  23. "UNIX" OFF)
  24. cmake_dependent_option(GLFW_USE_HYBRID_HPG "Force use of high-performance GPU on hybrid systems" OFF
  25. "WIN32" OFF)
  26. cmake_dependent_option(GLFW_USE_WAYLAND "Use Wayland for window creation" OFF
  27. "UNIX;NOT APPLE" OFF)
  28. cmake_dependent_option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON
  29. "MSVC" OFF)
  30. if (BUILD_SHARED_LIBS)
  31. set(_GLFW_BUILD_DLL 1)
  32. endif()
  33. if (BUILD_SHARED_LIBS AND UNIX)
  34. # On Unix-like systems, shared libraries can use the soname system.
  35. set(GLFW_LIB_NAME glfw)
  36. else()
  37. set(GLFW_LIB_NAME glfw3)
  38. endif()
  39. if (GLFW_VULKAN_STATIC)
  40. if (BUILD_SHARED_LIBS)
  41. # If you absolutely must do this, remove this line and add the Vulkan
  42. # loader static library via the CMAKE_SHARED_LINKER_FLAGS
  43. message(FATAL_ERROR "You are trying to link the Vulkan loader static library into the GLFW shared library")
  44. endif()
  45. set(_GLFW_VULKAN_STATIC 1)
  46. endif()
  47. list(APPEND CMAKE_MODULE_PATH "${GLFW_SOURCE_DIR}/CMake/modules")
  48. find_package(Threads REQUIRED)
  49. if (GLFW_BUILD_DOCS)
  50. set(DOXYGEN_SKIP_DOT TRUE)
  51. find_package(Doxygen)
  52. endif()
  53. #--------------------------------------------------------------------
  54. # Set compiler specific flags
  55. #--------------------------------------------------------------------
  56. if (MSVC)
  57. if (MSVC90)
  58. # Workaround for VS 2008 not shipping with the DirectX 9 SDK
  59. include(CheckIncludeFile)
  60. check_include_file(dinput.h DINPUT_H_FOUND)
  61. if (NOT DINPUT_H_FOUND)
  62. message(FATAL_ERROR "DirectX 9 SDK not found")
  63. endif()
  64. # Workaround for VS 2008 not shipping with stdint.h
  65. list(APPEND glfw_INCLUDE_DIRS "${GLFW_SOURCE_DIR}/deps/vs2008")
  66. endif()
  67. if (NOT USE_MSVC_RUNTIME_LIBRARY_DLL)
  68. foreach (flag CMAKE_C_FLAGS
  69. CMAKE_C_FLAGS_DEBUG
  70. CMAKE_C_FLAGS_RELEASE
  71. CMAKE_C_FLAGS_MINSIZEREL
  72. CMAKE_C_FLAGS_RELWITHDEBINFO)
  73. if (${flag} MATCHES "/MD")
  74. string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
  75. endif()
  76. if (${flag} MATCHES "/MDd")
  77. string(REGEX REPLACE "/MDd" "/MTd" ${flag} "${${flag}}")
  78. endif()
  79. endforeach()
  80. endif()
  81. endif()
  82. if (MINGW)
  83. # Workaround for legacy MinGW not providing XInput and DirectInput
  84. include(CheckIncludeFile)
  85. check_include_file(dinput.h DINPUT_H_FOUND)
  86. check_include_file(xinput.h XINPUT_H_FOUND)
  87. if (NOT DINPUT_H_FOUND OR NOT XINPUT_H_FOUND)
  88. list(APPEND glfw_INCLUDE_DIRS "${GLFW_SOURCE_DIR}/deps/mingw")
  89. endif()
  90. # Enable link-time exploit mitigation features enabled by default on MSVC
  91. include(CheckCCompilerFlag)
  92. # Compatibility with data execution prevention (DEP)
  93. set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
  94. check_c_compiler_flag("" _GLFW_HAS_DEP)
  95. if (_GLFW_HAS_DEP)
  96. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--nxcompat ${CMAKE_SHARED_LINKER_FLAGS}")
  97. endif()
  98. # Compatibility with address space layout randomization (ASLR)
  99. set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
  100. check_c_compiler_flag("" _GLFW_HAS_ASLR)
  101. if (_GLFW_HAS_ASLR)
  102. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--dynamicbase ${CMAKE_SHARED_LINKER_FLAGS}")
  103. endif()
  104. # Compatibility with 64-bit address space layout randomization (ASLR)
  105. set(CMAKE_REQUIRED_FLAGS "-Wl,--high-entropy-va")
  106. check_c_compiler_flag("" _GLFW_HAS_64ASLR)
  107. if (_GLFW_HAS_64ASLR)
  108. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--high-entropy-va ${CMAKE_SHARED_LINKER_FLAGS}")
  109. endif()
  110. endif()
  111. #--------------------------------------------------------------------
  112. # Detect and select backend APIs
  113. #--------------------------------------------------------------------
  114. if (GLFW_USE_WAYLAND)
  115. set(_GLFW_WAYLAND 1)
  116. message(STATUS "Using Wayland for window creation")
  117. elseif (GLFW_USE_OSMESA)
  118. set(_GLFW_OSMESA 1)
  119. message(STATUS "Using OSMesa for headless context creation")
  120. elseif (WIN32)
  121. set(_GLFW_WIN32 1)
  122. message(STATUS "Using Win32 for window creation")
  123. elseif (APPLE)
  124. set(_GLFW_COCOA 1)
  125. message(STATUS "Using Cocoa for window creation")
  126. elseif (UNIX)
  127. set(_GLFW_X11 1)
  128. message(STATUS "Using X11 for window creation")
  129. else()
  130. message(FATAL_ERROR "No supported platform was detected")
  131. endif()
  132. #--------------------------------------------------------------------
  133. # Find and add Unix math and time libraries
  134. #--------------------------------------------------------------------
  135. if (UNIX AND NOT APPLE)
  136. find_library(RT_LIBRARY rt)
  137. mark_as_advanced(RT_LIBRARY)
  138. if (RT_LIBRARY)
  139. list(APPEND glfw_LIBRARIES "${RT_LIBRARY}")
  140. list(APPEND glfw_PKG_LIBS "-lrt")
  141. endif()
  142. find_library(MATH_LIBRARY m)
  143. mark_as_advanced(MATH_LIBRARY)
  144. if (MATH_LIBRARY)
  145. list(APPEND glfw_LIBRARIES "${MATH_LIBRARY}")
  146. list(APPEND glfw_PKG_LIBS "-lm")
  147. endif()
  148. if (CMAKE_DL_LIBS)
  149. list(APPEND glfw_LIBRARIES "${CMAKE_DL_LIBS}")
  150. list(APPEND glfw_PKG_LIBS "-l${CMAKE_DL_LIBS}")
  151. endif()
  152. endif()
  153. #--------------------------------------------------------------------
  154. # Use Win32 for window creation
  155. #--------------------------------------------------------------------
  156. if (_GLFW_WIN32)
  157. list(APPEND glfw_PKG_LIBS "-lgdi32")
  158. if (GLFW_USE_HYBRID_HPG)
  159. set(_GLFW_USE_HYBRID_HPG 1)
  160. endif()
  161. endif()
  162. #--------------------------------------------------------------------
  163. # Use X11 for window creation
  164. #--------------------------------------------------------------------
  165. if (_GLFW_X11)
  166. find_package(X11 REQUIRED)
  167. list(APPEND glfw_PKG_DEPS "x11")
  168. # Set up library and include paths
  169. list(APPEND glfw_INCLUDE_DIRS "${X11_X11_INCLUDE_PATH}")
  170. list(APPEND glfw_LIBRARIES "${X11_X11_LIB}" "${CMAKE_THREAD_LIBS_INIT}")
  171. # Check for XRandR (modern resolution switching and gamma control)
  172. if (NOT X11_Xrandr_INCLUDE_PATH)
  173. message(FATAL_ERROR "The RandR headers were not found")
  174. endif()
  175. # Check for Xinerama (legacy multi-monitor support)
  176. if (NOT X11_Xinerama_INCLUDE_PATH)
  177. message(FATAL_ERROR "The Xinerama headers were not found")
  178. endif()
  179. # Check for Xkb (X keyboard extension)
  180. if (NOT X11_Xkb_INCLUDE_PATH)
  181. message(FATAL_ERROR "The X keyboard extension headers were not found")
  182. endif()
  183. # Check for Xcursor (cursor creation from RGBA images)
  184. if (NOT X11_Xcursor_INCLUDE_PATH)
  185. message(FATAL_ERROR "The Xcursor headers were not found")
  186. endif()
  187. # Check for XInput (modern HID input)
  188. if (NOT X11_Xi_INCLUDE_PATH)
  189. message(FATAL_ERROR "The XInput headers were not found")
  190. endif()
  191. list(APPEND glfw_INCLUDE_DIRS "${X11_Xrandr_INCLUDE_PATH}"
  192. "${X11_Xinerama_INCLUDE_PATH}"
  193. "${X11_Xkb_INCLUDE_PATH}"
  194. "${X11_Xcursor_INCLUDE_PATH}"
  195. "${X11_Xi_INCLUDE_PATH}")
  196. endif()
  197. #--------------------------------------------------------------------
  198. # Use Wayland for window creation
  199. #--------------------------------------------------------------------
  200. if (_GLFW_WAYLAND)
  201. find_package(ECM REQUIRED NO_MODULE)
  202. list(APPEND CMAKE_MODULE_PATH "${ECM_MODULE_PATH}")
  203. find_package(Wayland REQUIRED Client Cursor Egl)
  204. find_package(WaylandScanner REQUIRED)
  205. find_package(WaylandProtocols 1.15 REQUIRED)
  206. list(APPEND glfw_PKG_DEPS "wayland-egl")
  207. list(APPEND glfw_INCLUDE_DIRS "${Wayland_INCLUDE_DIRS}")
  208. list(APPEND glfw_LIBRARIES "${Wayland_LIBRARIES}" "${CMAKE_THREAD_LIBS_INIT}")
  209. find_package(XKBCommon REQUIRED)
  210. list(APPEND glfw_INCLUDE_DIRS "${XKBCOMMON_INCLUDE_DIRS}")
  211. include(CheckIncludeFiles)
  212. include(CheckFunctionExists)
  213. check_include_files(xkbcommon/xkbcommon-compose.h HAVE_XKBCOMMON_COMPOSE_H)
  214. check_function_exists(memfd_create HAVE_MEMFD_CREATE)
  215. if (NOT ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux"))
  216. find_package(EpollShim)
  217. if (EPOLLSHIM_FOUND)
  218. list(APPEND glfw_INCLUDE_DIRS "${EPOLLSHIM_INCLUDE_DIRS}")
  219. list(APPEND glfw_LIBRARIES "${EPOLLSHIM_LIBRARIES}")
  220. endif()
  221. endif()
  222. endif()
  223. #--------------------------------------------------------------------
  224. # Use OSMesa for offscreen context creation
  225. #--------------------------------------------------------------------
  226. if (_GLFW_OSMESA)
  227. find_package(OSMesa REQUIRED)
  228. list(APPEND glfw_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
  229. endif()
  230. #--------------------------------------------------------------------
  231. # Use Cocoa for window creation and NSOpenGL for context creation
  232. #--------------------------------------------------------------------
  233. if (_GLFW_COCOA)
  234. list(APPEND glfw_LIBRARIES
  235. "-framework Cocoa"
  236. "-framework IOKit"
  237. "-framework CoreFoundation"
  238. "-framework CoreVideo")
  239. set(glfw_PKG_DEPS "")
  240. set(glfw_PKG_LIBS "-framework Cocoa -framework IOKit -framework CoreFoundation -framework CoreVideo")
  241. endif()
  242. #--------------------------------------------------------------------
  243. # Add the Vulkan loader as a dependency if necessary
  244. #--------------------------------------------------------------------
  245. if (GLFW_VULKAN_STATIC)
  246. list(APPEND glfw_PKG_DEPS "vulkan")
  247. endif()
  248. #--------------------------------------------------------------------
  249. # Export GLFW library dependencies
  250. #--------------------------------------------------------------------
  251. foreach(arg ${glfw_PKG_DEPS})
  252. set(GLFW_PKG_DEPS "${GLFW_PKG_DEPS} ${arg}")
  253. endforeach()
  254. foreach(arg ${glfw_PKG_LIBS})
  255. set(GLFW_PKG_LIBS "${GLFW_PKG_LIBS} ${arg}")
  256. endforeach()
  257. #--------------------------------------------------------------------
  258. # Create generated files
  259. #--------------------------------------------------------------------
  260. include(CMakePackageConfigHelpers)
  261. set(GLFW_CONFIG_PATH "${CMAKE_INSTALL_LIBDIR}/cmake/glfw3")
  262. configure_package_config_file(src/glfw3Config.cmake.in
  263. src/glfw3Config.cmake
  264. INSTALL_DESTINATION "${GLFW_CONFIG_PATH}"
  265. NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  266. write_basic_package_version_file(src/glfw3ConfigVersion.cmake
  267. VERSION ${GLFW_VERSION}
  268. COMPATIBILITY SameMajorVersion)
  269. configure_file(src/glfw_config.h.in src/glfw_config.h @ONLY)
  270. configure_file(src/glfw3.pc.in src/glfw3.pc @ONLY)
  271. #--------------------------------------------------------------------
  272. # Add subdirectories
  273. #--------------------------------------------------------------------
  274. add_subdirectory(src)
  275. if (GLFW_BUILD_EXAMPLES)
  276. add_subdirectory(examples)
  277. endif()
  278. if (GLFW_BUILD_TESTS)
  279. add_subdirectory(tests)
  280. endif()
  281. if (DOXYGEN_FOUND AND GLFW_BUILD_DOCS)
  282. add_subdirectory(docs)
  283. endif()
  284. #--------------------------------------------------------------------
  285. # Install files other than the library
  286. # The library is installed by src/CMakeLists.txt
  287. #--------------------------------------------------------------------
  288. if (GLFW_INSTALL)
  289. install(DIRECTORY include/GLFW DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  290. FILES_MATCHING PATTERN glfw3.h PATTERN glfw3native.h)
  291. install(FILES "${GLFW_BINARY_DIR}/src/glfw3Config.cmake"
  292. "${GLFW_BINARY_DIR}/src/glfw3ConfigVersion.cmake"
  293. DESTINATION "${GLFW_CONFIG_PATH}")
  294. install(EXPORT glfwTargets FILE glfw3Targets.cmake
  295. EXPORT_LINK_INTERFACE_LIBRARIES
  296. DESTINATION "${GLFW_CONFIG_PATH}")
  297. install(FILES "${GLFW_BINARY_DIR}/src/glfw3.pc"
  298. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  299. # Only generate this target if no higher-level project already has
  300. if (NOT TARGET uninstall)
  301. configure_file(CMake/cmake_uninstall.cmake.in
  302. cmake_uninstall.cmake IMMEDIATE @ONLY)
  303. add_custom_target(uninstall
  304. "${CMAKE_COMMAND}" -P
  305. "${GLFW_BINARY_DIR}/cmake_uninstall.cmake")
  306. set_target_properties(uninstall PROPERTIES FOLDER "GLFW3")
  307. endif()
  308. endif()