From d83849792b0cf3e40c402f299c867ac6e1fd0e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 6 Jul 2021 23:12:05 +0200 Subject: [PATCH] Add override variable for CMake library type This adds the GLFW_LIBRARY_TYPE CMake cache variable, which allows users and higher-level projects to set what type of library GLFW is built as. When not empty, this value overrides the standard BUILD_SHARED_LIBS option for GLFW while still allowing it to control the type of other libraries in a larger project. This also allows building GLFW as an object library without adding dummy source files (as required by Xcode) or producing unused library binaries. Projects using CMake 3.12 or later can link the resulting GLFW object library normally using target_link_libraries. Fixes #279. Related to #1307. Closes #1497. Closes #1574. Closes #1928. --- CMakeLists.txt | 17 +++++++++++++++-- README.md | 2 ++ docs/compile.dox | 15 ++++++++++++--- src/CMakeLists.txt | 7 ++++--- 4 files changed, 33 insertions(+), 8 deletions(-) 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