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