diff --git a/CMakeLists.txt b/CMakeLists.txt index 825fc453..ab3248ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,20 @@ cmake_dependent_option(GLFW_USE_WAYLAND "Use Wayland for window creation" OFF cmake_dependent_option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON "MSVC" OFF) -if (BUILD_SHARED_LIBS AND UNIX) +set(GLFW_LIBRARY_TYPE "${GLFW_LIBRARY_TYPE}" CACHE STRING + "Library type override for GLFW (SHARED, STATIC, OBJECT, or empty to follow BUILD_SHARED_LIBS)") + +if (GLFW_LIBRARY_TYPE) + if (GLFW_LIBRARY_TYPE STREQUAL "SHARED") + set(GLFW_BUILD_SHARED_LIBRARY TRUE) + else() + set(GLFW_BUILD_SHARED_LIBRARY FALSE) + endif() +else() + set(GLFW_BUILD_SHARED_LIBRARY ${BUILD_SHARED_LIBS}) +endif() + +if (UNIX AND GLFW_BUILD_SHARED_LIBRARY) # On Unix-like systems, shared libraries can use the soname system. set(GLFW_LIB_NAME glfw) else() @@ -49,7 +62,7 @@ else() endif() if (GLFW_VULKAN_STATIC) - if (BUILD_SHARED_LIBS) + if (GLFW_BUILD_SHARED_LIBRARY) # If you absolutely must do this, remove this line and add the Vulkan # loader static library via the CMAKE_SHARED_LINKER_FLAGS message(FATAL_ERROR "You are trying to link the Vulkan loader static library into the GLFW shared library") diff --git a/README.md b/README.md index 3e529076..98b49733 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,8 @@ information on what to include when reporting a bug. values to select ANGLE backend (#1380) - Added `GLFW_X11_XCB_VULKAN_SURFACE` init hint for selecting X11 Vulkan surface extension (#1793) + - Added `GLFW_LIBRARY_TYPE` CMake variable for overriding the library type + (#279,#1307,#1497,#1574,#1928) - Made joystick subsystem initialize at first use (#1284,#1646) - Made `GLFW_DOUBLEBUFFER` a read-only window attribute - Updated the minimum required CMake version to 3.1 diff --git a/docs/compile.dox b/docs/compile.dox index a5367110..d5b839d6 100644 --- a/docs/compile.dox +++ b/docs/compile.dox @@ -239,9 +239,18 @@ cmake -S path/to/glfw -B path/to/build -D BUILD_SHARED_LIBS=ON @subsection compile_options_shared Shared CMake options @anchor BUILD_SHARED_LIBS -__BUILD_SHARED_LIBS__ determines whether GLFW is built as a static -library or as a DLL / shared library / dynamic library. This is disabled by -default, producing a static GLFW library. +__BUILD_SHARED_LIBS__ determines whether GLFW is built as a static library or as +a DLL / shared library / dynamic library. This is disabled by default, +producing a static GLFW library. This variable has no `GLFW_` prefix because it +is defined by CMake. If you want to change the library only for GLFW when it is +part of a larger project, see @ref GLFW_LIBRARY_TYPE. + +@anchor GLFW_LIBRARY_TYPE +__GLFW_LIBRARY_TYPE__ allows you to override @ref BUILD_SHARED_LIBS only for +GLFW, without affecting other libraries in a larger project. When set, the +value of this option must be a valid CMake library type. Set it to `STATIC` to +build GLFW as a static library, `SHARED` to build it as a shared library +/ dynamic library / DLL, or `OBJECT` to make GLFW a CMake object library. @anchor GLFW_BUILD_EXAMPLES __GLFW_BUILD_EXAMPLES__ determines whether the GLFW examples are built diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d15838e0..ea16f195 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ -add_library(glfw "${GLFW_SOURCE_DIR}/include/GLFW/glfw3.h" +add_library(glfw ${GLFW_LIBRARY_TYPE} + "${GLFW_SOURCE_DIR}/include/GLFW/glfw3.h" "${GLFW_SOURCE_DIR}/include/GLFW/glfw3native.h" internal.h mappings.h context.c init.c input.c monitor.c vulkan.c window.c) @@ -96,7 +97,7 @@ if (_GLFW_WAYLAND) "${GLFW_BINARY_DIR}/src/wayland-idle-inhibit-unstable-v1-client-protocol") endif() -if (WIN32 AND BUILD_SHARED_LIBS) +if (WIN32 AND GLFW_BUILD_SHARED_LIBRARY) configure_file(glfw.rc.in glfw.rc @ONLY) target_sources(glfw PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/glfw.rc") endif() @@ -201,7 +202,7 @@ if (_GLFW_X11 OR _GLFW_WAYLAND OR _GLFW_OSMESA) endif() endif() -if (BUILD_SHARED_LIBS) +if (GLFW_BUILD_SHARED_LIBRARY) if (WIN32) if (MINGW) # Remove the dependency on the shared version of libgcc