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 $<TARGET_OBJECTS:glfw_objlib> as part of the source.
This commit is contained in:
Ahmad Fatoum 2018-07-29 21:48:41 +02:00
parent 9a9deb3147
commit a23f824878
2 changed files with 19 additions and 13 deletions

View File

@ -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

View File

@ -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 $<TARGET_OBJECTS:glfw_objlib>)
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
"$<$<C_COMPILER_ID:AppleClang>:-std=c99>"
"$<$<C_COMPILER_ID:Clang>:-std=c99>"
"$<$<C_COMPILER_ID:GNU>:-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
"$<BUILD_INTERFACE:${GLFW_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_FULL_INCLUDEDIR}>")
target_include_directories(glfw PUBLIC
"$<BUILD_INTERFACE:${GLFW_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_include_directories(glfw PRIVATE
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_FULL_INCLUDEDIR}>")
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
"$<$<BOOL:${MINGW}>: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
"$<$<C_COMPILER_ID:AppleClang>:-Wall>"
"$<$<C_COMPILER_ID:Clang>:-Wall>"
"$<$<C_COMPILER_ID:GNU>:-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)