CMakeLists.txt 13 KB

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