From a23f824878a4c543c45e82db3c0a2acdc6039341 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sun, 29 Jul 2018 21:48:41 +0200 Subject: [PATCH] Add CMake object library target as well Some projects (e.g. raylib, nanogui) ship a static library that has GLFW embedded. Unfortunately, it's not possible to do so portably with the current CMake setup. The proper way seems to be to define a CMake OBJECT library target and use that as source for the glfw target. User code that is interested in the glfw target is unaffected and can keep using it normally. User code that wants the objects to link into a static library just needs to supply $ as part of the source. --- README.md | 2 ++ src/CMakeLists.txt | 30 +++++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3898f5bd9..8b849f1a8 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,8 @@ information on what to include when reporting a bug. - [NSGL] Removed enforcement of forward-compatible flag for core contexts - Export CMake `GLFW_PKG_DEPS` and `GLFW_PKG_LIBS` to parent scope for use in client pkg-configs (#1307) +- Added a `glfw_objlib` CMake OBJECT library target for embedding into static + libraries (#1307) ## Contact diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6cbeed6d6..8db786eaf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -92,31 +92,35 @@ if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR COMPILE_FLAGS -Wdeclaration-after-statement) endif() -add_library(glfw ${glfw_SOURCES} ${glfw_HEADERS}) +add_library(glfw_objlib OBJECT ${glfw_SOURCES} ${glfw_HEADERS}) +add_library(glfw $) +set_target_properties(glfw_objlib PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(glfw PROPERTIES OUTPUT_NAME ${GLFW_LIB_NAME} VERSION ${GLFW_VERSION_MAJOR}.${GLFW_VERSION_MINOR} SOVERSION ${GLFW_VERSION_MAJOR} - POSITION_INDEPENDENT_CODE ON FOLDER "GLFW3") if (${CMAKE_VERSION} VERSION_EQUAL "3.1.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.1.0") - set_target_properties(glfw PROPERTIES C_STANDARD 99) + set_target_properties(glfw_objlib PROPERTIES C_STANDARD 99) else() # Remove this fallback when removing support for CMake version less than 3.1 - target_compile_options(glfw PRIVATE + target_compile_options(glfw_objlib PRIVATE "$<$:-std=c99>" "$<$:-std=c99>" "$<$:-std=c99>") endif() -target_compile_definitions(glfw PRIVATE _GLFW_USE_CONFIG_H) +target_compile_definitions(glfw_objlib PRIVATE _GLFW_USE_CONFIG_H) +target_include_directories(glfw_objlib PUBLIC + "$" + "$") target_include_directories(glfw PUBLIC "$" - "$") -target_include_directories(glfw PRIVATE + "$") +target_include_directories(glfw_objlib PRIVATE "${GLFW_SOURCE_DIR}/src" "${GLFW_BINARY_DIR}/src" ${glfw_INCLUDE_DIRS}) @@ -125,11 +129,11 @@ target_include_directories(glfw PRIVATE # the inclusion of stddef.h (by glfw3.h), which is itself included before # win32_platform.h. We define them here until a saner solution can be found # NOTE: MinGW-w64 and Visual C++ do /not/ need this hack. -target_compile_definitions(glfw PRIVATE +target_compile_definitions(glfw_objlib PRIVATE "$<$:UNICODE;WINVER=0x0501>") # Enable a reasonable set of warnings (no, -Wextra is not reasonable) -target_compile_options(glfw PRIVATE +target_compile_options(glfw_objlib PRIVATE "$<$:-Wall>" "$<$:-Wall>" "$<$:-Wall>") @@ -151,10 +155,10 @@ if (BUILD_SHARED_LIBS) set_target_properties(glfw PROPERTIES IMPORT_SUFFIX "dll.lib") endif() - target_compile_definitions(glfw INTERFACE GLFW_DLL) + target_compile_definitions(glfw_objlib INTERFACE GLFW_DLL) elseif (APPLE) # Add -fno-common to work around a bug in Apple's GCC - target_compile_options(glfw PRIVATE "-fno-common") + target_compile_options(glfw_objlib PRIVATE "-fno-common") set_target_properties(glfw PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_LIBDIR}") @@ -162,7 +166,7 @@ if (BUILD_SHARED_LIBS) if (UNIX) # Hide symbols not explicitly tagged for export from the shared library - target_compile_options(glfw PRIVATE "-fvisibility=hidden") + target_compile_options(glfw_objlib PRIVATE "-fvisibility=hidden") endif() target_link_libraries(glfw PRIVATE ${glfw_LIBRARIES}) @@ -171,7 +175,7 @@ else() endif() if (MSVC) - target_compile_definitions(glfw PRIVATE _CRT_SECURE_NO_WARNINGS) + target_compile_definitions(glfw_objlib PRIVATE _CRT_SECURE_NO_WARNINGS) endif() if (GLFW_INSTALL)