From dabe56de58a32768f0dff740f83539ab8337b132 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Thu, 10 Mar 2022 16:54:43 +0100 Subject: genex-LINK_LIBRARY: Add feature WHOLE_ARCHIVE --- Help/manual/cmake-generator-expressions.7.rst | 4 +-- Help/release/dev/LINK_LIBRARY-WHOLE_ARCHIVE.rst | 14 +++++++++ Help/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.txt | 18 ++++++------ Help/variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt | 18 ++++++++++++ Modules/Platform/CYGWIN-GNU.cmake | 28 ++++++++++++++++++ Modules/Platform/Darwin.cmake | 8 +++-- Modules/Platform/FreeBSD.cmake | 27 +++++++++++++++++ Modules/Platform/Linux.cmake | 25 ++++++++++++++++ Modules/Platform/NetBSD.cmake | 27 +++++++++++++++++ Modules/Platform/SunOS.cmake | 14 +++++++++ Modules/Platform/Windows-Clang.cmake | 7 +++++ Modules/Platform/Windows-GNU.cmake | 25 ++++++++++++++++ Modules/Platform/Windows-MSVC.cmake | 9 ++++++ .../RunCMakeTest.cmake | 14 +++++++-- .../feature-WHOLE_ARCHIVE.cmake | 11 +++++++ .../load_archive.cmake | 34 ++++++++++++++++++++++ .../whole_archive.cmake | 34 ---------------------- 17 files changed, 268 insertions(+), 49 deletions(-) create mode 100644 Help/release/dev/LINK_LIBRARY-WHOLE_ARCHIVE.rst create mode 100644 Tests/RunCMake/target_link_libraries-LINK_LIBRARY/feature-WHOLE_ARCHIVE.cmake create mode 100644 Tests/RunCMake/target_link_libraries-LINK_LIBRARY/load_archive.cmake delete mode 100644 Tests/RunCMake/target_link_libraries-LINK_LIBRARY/whole_archive.cmake diff --git a/Help/manual/cmake-generator-expressions.7.rst b/Help/manual/cmake-generator-expressions.7.rst index 2394eb2..b79cf3a 100644 --- a/Help/manual/cmake-generator-expressions.7.rst +++ b/Help/manual/cmake-generator-expressions.7.rst @@ -1125,9 +1125,9 @@ Output-Related Expressions add_library(lib1 STATIC ...) add_library(lib2 ...) - target_link_libraries(lib2 PRIVATE "$") + target_link_libraries(lib2 PRIVATE "$") - This specify to use the ``lib1`` target with feature ``whole_archive`` for + This specify to use the ``lib1`` target with feature ``load_archive`` for linking target ``lib2``. The feature must have be defined by :variable:`CMAKE__LINK_LIBRARY_USING_` variable or, if :variable:`CMAKE__LINK_LIBRARY_USING__SUPPORTED` is false, diff --git a/Help/release/dev/LINK_LIBRARY-WHOLE_ARCHIVE.rst b/Help/release/dev/LINK_LIBRARY-WHOLE_ARCHIVE.rst new file mode 100644 index 0000000..abaf3a9 --- /dev/null +++ b/Help/release/dev/LINK_LIBRARY-WHOLE_ARCHIVE.rst @@ -0,0 +1,14 @@ +LINK_LIBRARY-WHOLE_ARCHIVE +-------------------------- + +* The :genex:`LINK_LIBRARY` generator expression gained the feature + ``WHOLE_ARCHIVE`` to force load of all members in a static library. This + feature is supported on the following target platforms: + + * all ``Apple`` variants + * ``Linux`` + * all ``BSD`` variants + * ``SunOS`` + * ``Windows`` + * ``CYGWIN`` + * ``MSYS`` diff --git a/Help/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.txt b/Help/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.txt index ad393b0..ec293d3 100644 --- a/Help/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.txt +++ b/Help/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.txt @@ -46,27 +46,27 @@ is offered by various environments but with a specific syntax: .. code-block:: cmake - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive_SUPPORTED TRUE) + set(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED TRUE) if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang") - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "-force_load ") + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "-force_load ") elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux") - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "LINKER:--push-state,--whole-archive" - "" - "LINKER:--pop-state") + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "/WHOLEARCHIVE:") + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "/WHOLEARCHIVE:") else() # feature not yet supported for the other environments - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive_SUPPORTED FALSE) + set(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED FALSE) endif() add_library(lib1 STATIC ...) add_library(lib2 SHARED ...) - if(CMAKE_C_LINK_LIBRARY_USING_whole_archive_SUPPORTED) + if(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED) target_link_libraries(lib2 PRIVATE - "$,libexternal.a,external>>") + "$,libexternal.a,external>>") else() target_link_libraries(lib2 PRIVATE lib1 external) endif() diff --git a/Help/variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt b/Help/variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt index 936bd31..e4fa0ed8 100644 --- a/Help/variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt +++ b/Help/variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt @@ -5,6 +5,24 @@ useful with :prop_tgt:`LINK_LIBRARY_OVERRIDE` and :prop_tgt:`LINK_LIBRARY_OVERRIDE_` target properties. +**Features available for a subset of environments** + +``WHOLE_ARCHIVE`` + Force load of all members in a static library. + + Target platforms supported: all ``Apple`` variants, ``Linux``, all ``BSD`` + variants, ``SunOS``, ``Windows``, ``CYGWIN``, and ``MSYS``. + + Platform-specific notes: + + * On Apple platforms, the library must be specified as a CMake target name, a + library file name (such as ``libfoo.a``), or a library file path (such as + ``/path/to/libfoo.a``). It cannot be specified as a plain library name + (such as ``foo``, where ``foo`` is not CMake target), due to a limitation + in the Apple linker. + * On Windows platforms, for ``MSVC`` or MSVC-like toolchains, the version + must be greater than ``1900``. + **Features available in Apple environments** It is assumed that the linker used is the one provided by `XCode` or is diff --git a/Modules/Platform/CYGWIN-GNU.cmake b/Modules/Platform/CYGWIN-GNU.cmake index b81bd4d..ef64012 100644 --- a/Modules/Platform/CYGWIN-GNU.cmake +++ b/Modules/Platform/CYGWIN-GNU.cmake @@ -14,6 +14,34 @@ string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -Wl,--enable-auto-import") set(CMAKE_GNULD_IMAGE_VERSION "-Wl,--major-image-version,,--minor-image-version,") set(CMAKE_GENERATOR_RC windres) + + +# Features for LINK_LIBRARY generator expression +## check linker capabilities +if(NOT DEFINED _CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + execute_process(COMMAND "${CMAKE_LINKER}" --help + OUTPUT_VARIABLE __linker_help + ERROR_VARIABLE __linker_help) + if(__linker_help MATCHES "--push-state" AND __linker_help MATCHES "--pop-state") + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED TRUE CACHE INTERNAL "linker supports push/pop state") + else() + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED FALSE CACHE INTERNAL "linker supports push/pop state") + endif() + unset(__linker_help) +endif() +## WHOLE_ARCHIVE: Force loading all members of an archive +if(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") +else() + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") +endif() +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + + macro(__cygwin_compiler_gnu lang) # Binary link rules. set(CMAKE_${lang}_CREATE_SHARED_MODULE diff --git a/Modules/Platform/Darwin.cmake b/Modules/Platform/Darwin.cmake index a27bb36..ec88a37 100644 --- a/Modules/Platform/Darwin.cmake +++ b/Modules/Platform/Darwin.cmake @@ -108,7 +108,7 @@ foreach(lang C CXX Fortran OBJC OBJCXX) set(CMAKE_${lang}_FRAMEWORK_SEARCH_FLAG -F) endforeach() -# Defines link features for frameworks +# Defines LINK_LIBRARY features for frameworks set(CMAKE_LINK_LIBRARY_USING_FRAMEWORK "LINKER:-framework,") set(CMAKE_LINK_LIBRARY_USING_FRAMEWORK_SUPPORTED TRUE) @@ -121,7 +121,7 @@ set(CMAKE_LINK_LIBRARY_USING_REEXPORT_FRAMEWORK_SUPPORTED TRUE) set(CMAKE_LINK_LIBRARY_USING_WEAK_FRAMEWORK "LINKER:-weak_framework,") set(CMAKE_LINK_LIBRARY_USING_WEAK_FRAMEWORK_SUPPORTED TRUE) -# Defines link features for libraries +# Defines LINK_LIBRARY features for libraries set(CMAKE_LINK_LIBRARY_USING_NEEDED_LIBRARY "PATH{LINKER:-needed_library }NAME{LINKER:-needed-l}") set(CMAKE_LINK_LIBRARY_USING_NEEDED_LIBRARY_SUPPORTED TRUE) @@ -131,6 +131,10 @@ set(CMAKE_LINK_LIBRARY_USING_REEXPORT_LIBRARY_SUPPORTED TRUE) set(CMAKE_LINK_LIBRARY_USING_WEAK_LIBRARY "PATH{LINKER:-weak_library }NAME{LINKER:-weak-l}") set(CMAKE_LINK_LIBRARY_USING_WEAK_LIBRARY_SUPPORTED TRUE) +# Defines LINK_LIBRARY feature to Force loading of all members of an archive +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:-force_load ") +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + # default to searching for frameworks first if(NOT DEFINED CMAKE_FIND_FRAMEWORK) set(CMAKE_FIND_FRAMEWORK FIRST) diff --git a/Modules/Platform/FreeBSD.cmake b/Modules/Platform/FreeBSD.cmake index 4a4c00d..4c8cb6a 100644 --- a/Modules/Platform/FreeBSD.cmake +++ b/Modules/Platform/FreeBSD.cmake @@ -26,4 +26,31 @@ foreach(type SHARED_LIBRARY SHARED_MODULE EXE) set(CMAKE_${type}_LINK_DYNAMIC_C_FLAGS "-Wl,-Bdynamic") endforeach() + +# Features for LINK_LIBRARY generator expression +## check linker capabilities +if(NOT DEFINED _CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + execute_process(COMMAND "${CMAKE_LINKER}" --help + OUTPUT_VARIABLE __linker_help + ERROR_VARIABLE __linker_help) + if(__linker_help MATCHES "--push-state" AND __linker_help MATCHES "--pop-state") + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED TRUE CACHE INTERNAL "linker supports push/pop state") + else() + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED FALSE CACHE INTERNAL "linker supports push/pop state") + endif() + unset(__linker_help) +endif() +## WHOLE_ARCHIVE: Force loading all members of an archive +if(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") +else() + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") +endif() +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + + include(Platform/UnixPaths) diff --git a/Modules/Platform/Linux.cmake b/Modules/Platform/Linux.cmake index 95102e1..a7e58ab 100644 --- a/Modules/Platform/Linux.cmake +++ b/Modules/Platform/Linux.cmake @@ -20,6 +20,31 @@ foreach(type SHARED_LIBRARY SHARED_MODULE EXE) endforeach() +# Features for LINK_LIBRARY generator expression +## check linker capabilities +if(NOT DEFINED _CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + execute_process(COMMAND "${CMAKE_LINKER}" --help + OUTPUT_VARIABLE __linker_help + ERROR_VARIABLE __linker_help) + if(__linker_help MATCHES "--push-state" AND __linker_help MATCHES "--pop-state") + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED TRUE CACHE INTERNAL "linker supports push/pop state") + else() + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED FALSE CACHE INTERNAL "linker supports push/pop state") + endif() + unset(__linker_help) +endif() +## WHOLE_ARCHIVE: Force loading all members of an archive +if(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") +else() + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") +endif() +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + # Features for LINK_GROUP generator expression ## RESCAN: request the linker to rescan static libraries until there is ## no pending undefined symbols diff --git a/Modules/Platform/NetBSD.cmake b/Modules/Platform/NetBSD.cmake index d99cb4a..8774159 100644 --- a/Modules/Platform/NetBSD.cmake +++ b/Modules/Platform/NetBSD.cmake @@ -12,4 +12,31 @@ set(CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG "-Wl,-rpath-link,") set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-soname,") set(CMAKE_EXE_EXPORTS_C_FLAG "-Wl,--export-dynamic") + +# Features for LINK_LIBRARY generator expression +## check linker capabilities +if(NOT DEFINED _CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + execute_process(COMMAND "${CMAKE_LINKER}" --help + OUTPUT_VARIABLE __linker_help + ERROR_VARIABLE __linker_help) + if(__linker_help MATCHES "--push-state" AND __linker_help MATCHES "--pop-state") + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED TRUE CACHE INTERNAL "linker supports push/pop state") + else() + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED FALSE CACHE INTERNAL "linker supports push/pop state") + endif() + unset(__linker_help) +endif() +## WHOLE_ARCHIVE: Force loading all members of an archive +if(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") +else() + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") +endif() +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + + include(Platform/UnixPaths) diff --git a/Modules/Platform/SunOS.cmake b/Modules/Platform/SunOS.cmake index e01e892..b8a302c 100644 --- a/Modules/Platform/SunOS.cmake +++ b/Modules/Platform/SunOS.cmake @@ -8,6 +8,20 @@ if(CMAKE_SYSTEM MATCHES "SunOS-4") endif() +# Features for LINK_LIBRARY generator expression +## WHOLE_ARCHIVE: Force loading all members of an archive +if (CMAKE_SYSTEM_VERSION VERSION_GREATER "5.10") + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") +else() + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:-z,allextract" + "" + "LINKER:-z,defaultextract") +endif() +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + + # Features for LINK_GROUP generator expression if (CMAKE_SYSTEM_VERSION VERSION_GREATER "5.9") ## RESCAN: request the linker to rescan static libraries until there is diff --git a/Modules/Platform/Windows-Clang.cmake b/Modules/Platform/Windows-Clang.cmake index e7e975d..7600c8d 100644 --- a/Modules/Platform/Windows-Clang.cmake +++ b/Modules/Platform/Windows-Clang.cmake @@ -113,6 +113,13 @@ macro(__windows_compiler_clang_gnu lang) string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_LOWER) set(CMAKE_${lang}_STANDARD_LIBRARIES_INIT "-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames") + # Features for LINK_LIBRARY generator expression + if(MSVC_VERSION GREATER "1900") + ## WHOLE_ARCHIVE: Force loading all members of an archive + set(CMAKE_${lang}_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:/WHOLEARCHIVE:") + set(CMAKE_${lang}_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + endif() + enable_language(RC) endmacro() diff --git a/Modules/Platform/Windows-GNU.cmake b/Modules/Platform/Windows-GNU.cmake index 3c24c21..b464169 100644 --- a/Modules/Platform/Windows-GNU.cmake +++ b/Modules/Platform/Windows-GNU.cmake @@ -45,6 +45,31 @@ if("${_help}" MATCHES "GNU ld .* 2\\.1[1-6]") endif() +# Features for LINK_LIBRARY generator expression +## check linker capabilities +if(NOT DEFINED _CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + execute_process(COMMAND "${CMAKE_LINKER}" --help + OUTPUT_VARIABLE __linker_help + ERROR_VARIABLE __linker_help) + if(__linker_help MATCHES "--push-state" AND __linker_help MATCHES "--pop-state") + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED TRUE CACHE INTERNAL "linker supports push/pop state") + else() + set(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED FALSE CACHE INTERNAL "linker supports push/pop state") + endif() + unset(__linker_help) +endif() +## WHOLE_ARCHIVE: Force loading all members of an archive +if(_CMAKE_LINKER_PUSHPOP_STATE_SUPPORTED) + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") +else() + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") +endif() +set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) + # Features for LINK_GROUP generator expression ## RESCAN: request the linker to rescan static libraries until there is ## no pending undefined symbols diff --git a/Modules/Platform/Windows-MSVC.cmake b/Modules/Platform/Windows-MSVC.cmake index b2cc6f4..e74ec9e 100644 --- a/Modules/Platform/Windows-MSVC.cmake +++ b/Modules/Platform/Windows-MSVC.cmake @@ -331,6 +331,15 @@ else() endif() unset(__WINDOWS_MSVC_CMP0091) + +# Features for LINK_LIBRARY generator expression +if(MSVC_VERSION GREATER "1900") + ## WHOLE_ARCHIVE: Force loading all members of an archive + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "/WHOLEARCHIVE:") + set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE) +endif() + + macro(__windows_compiler_msvc lang) if(NOT MSVC_VERSION LESS 1400) # for 2005 make sure the manifest is put in the dll with mt diff --git a/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/RunCMakeTest.cmake b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/RunCMakeTest.cmake index 349310f..021de41 100644 --- a/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/RunCMakeTest.cmake +++ b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/RunCMakeTest.cmake @@ -67,8 +67,8 @@ if ((RunCMake_GENERATOR MATCHES "Makefiles|Ninja|Xcode" if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" OR (CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER "1900") OR (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")) - run_cmake(whole_archive) - run_cmake_target(whole_archive link-exe main) + run_cmake(load_archive) + run_cmake_target(load_archive link-exe main) endif() if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang") run_cmake(weak_library) @@ -110,3 +110,13 @@ endif() if (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_C_COMPILER_VERSION GREATER_EQUAL "12") run_cmake_target(apple_library needed_library main-needed_library) endif() + +# WHOLE_ARCHIVE feature +if ((CMAKE_SYSTEM_NAME STREQUAL "Windows" AND + ((DEFINED MSVC_VERSION AND MSVC_VERSION GREATER "1900") OR (CMAKE_C_COMPILER_ID MATCHES "GNU|Clang" AND NOT CMAKE_C_SIMULATE_ID STREQUAL "MSVC"))) + OR (CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND + (NOT CMAKE_C_COMPILER_ID STREQUAL "SunPro" OR CMAKE_C_COMPILER_VERSION GREATER "5.9")) + OR CMAKE_SYSTEM_NAME MATCHES "Darwin|iOS|tvOS|watchOS|Linux|BSD|MSYS|CYGWIN") + run_cmake(feature-WHOLE_ARCHIVE) + run_cmake_target(feature-WHOLE_ARCHIVE link-exe main) +endif() diff --git a/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/feature-WHOLE_ARCHIVE.cmake b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/feature-WHOLE_ARCHIVE.cmake new file mode 100644 index 0000000..e525325 --- /dev/null +++ b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/feature-WHOLE_ARCHIVE.cmake @@ -0,0 +1,11 @@ + +enable_language(C) + +add_library(base STATIC base.c unref.c) +target_compile_definitions(base PUBLIC STATIC_BASE) + +add_library(lib SHARED lib.c) +target_link_libraries(lib PRIVATE "$") + +add_executable(main main.c) +target_link_libraries(main PRIVATE lib) diff --git a/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/load_archive.cmake b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/load_archive.cmake new file mode 100644 index 0000000..a0bbb43 --- /dev/null +++ b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/load_archive.cmake @@ -0,0 +1,34 @@ + +enable_language(C) + +set(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED TRUE) +if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang") + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "-force_load ") +elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux") + execute_process(COMMAND "${CMAKE_LINKER}" --help + OUTPUT_VARIABLE linker_help + ERROR_VARIABLE linker_help) + if(linker_help MATCHES "--push-state" AND linker_help MATCHES "--pop-state") + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "LINKER:--push-state,--whole-archive" + "" + "LINKER:--pop-state") + else() + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "LINKER:--whole-archive" + "" + "LINKER:--no-whole-archive") + endif() +elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") + set(CMAKE_C_LINK_LIBRARY_USING_load_archive "/WHOLEARCHIVE:") +else() + # feature not yet supported for the other environments + set(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED FALSE) +endif() + +add_library(base STATIC base.c unref.c) +target_compile_definitions(base PUBLIC STATIC_BASE) + +add_library(lib SHARED lib.c) +target_link_libraries(lib PRIVATE "$") + +add_executable(main main.c) +target_link_libraries(main PRIVATE lib) diff --git a/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/whole_archive.cmake b/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/whole_archive.cmake deleted file mode 100644 index 93082a4..0000000 --- a/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/whole_archive.cmake +++ /dev/null @@ -1,34 +0,0 @@ - -enable_language(C) - -set(CMAKE_C_LINK_LIBRARY_USING_whole_archive_SUPPORTED TRUE) -if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang") - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "-force_load ") -elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux") - execute_process(COMMAND "${CMAKE_LINKER}" --help - OUTPUT_VARIABLE linker_help - ERROR_VARIABLE linker_help) - if(linker_help MATCHES "--push-state" AND linker_help MATCHES "--pop-state") - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "LINKER:--push-state,--whole-archive" - "" - "LINKER:--pop-state") - else() - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "LINKER:--whole-archive" - "" - "LINKER:--no-whole-archive") - endif() -elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive "/WHOLEARCHIVE:") -else() - # feature not yet supported for the other environments - set(CMAKE_C_LINK_LIBRARY_USING_whole_archive_SUPPORTED FALSE) -endif() - -add_library(base STATIC base.c unref.c) -target_compile_definitions(base PUBLIC STATIC_BASE) - -add_library(lib SHARED lib.c) -target_link_libraries(lib PRIVATE "$") - -add_executable(main main.c) -target_link_libraries(main PRIVATE lib) -- cgit v0.12