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