CMake transitive LINK_LIBRARIES

This commit is contained in:
Luís Murta 2025-08-05 23:30:57 +01:00
commit 0e1cb50299
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94
7 changed files with 74 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.cache
build

65
CMakeLists.txt Normal file
View File

@ -0,0 +1,65 @@
cmake_minimum_required(VERSION 3.12...4.1)
project(link-libraries)
# Added in 4.1, enables transitive link libraries on LINK_LIBRARIES property
# https://cmake.org/cmake/help/v4.1/policy/CMP0189.html
# https://gitlab.kitware.com/cmake/cmake/-/issues/26709
# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10391
cmake_policy(SET CMP0189 NEW)
add_library(shared1 SHARED dummy.cpp)
add_library(shared2 SHARED dummy.cpp)
add_library(shared3 SHARED dummy.cpp)
add_library(interface1 INTERFACE)
add_library(interface2 INTERFACE)
add_library(interface3 INTERFACE)
add_library(interface4 INTERFACE)
add_library(static1 STATIC dummy.cpp)
add_library(static2 STATIC dummy.cpp)
add_library(lib1 STATIC dummy.cpp)
target_link_libraries(
lib1
PUBLIC shared1 interface1 static1
PRIVATE shared2 interface2 static2
INTERFACE interface3)
add_library(lib2 SHARED dummy.cpp)
target_link_libraries(lib2 shared3 interface3)
add_executable(exe1 dummy.cpp)
target_link_libraries(exe1 PRIVATE lib1 lib2 interface4)
add_executable(exe2 dummy.cpp)
target_link_libraries(exe2 lib1 lib2 interface4)
get_target_property(lib1_LINK_LIBRARIES lib1 LINK_LIBRARIES)
get_target_property(lib1_INTERFACE_LINK_LIBRARIES lib1 INTERFACE_LINK_LIBRARIES)
message(STATUS "lib1_LINK_LIBRARIES: ${lib1_LINK_LIBRARIES}")
message(STATUS "lib1_INTERFACE_LINK: ${lib1_INTERFACE_LINK_LIBRARIES}")
get_target_property(exe1_LINK_LIBRARIES exe1 LINK_LIBRARIES)
get_target_property(exe1_INTERFACE_LINK_LIBRARIES exe1 INTERFACE_LINK_LIBRARIES)
message(STATUS "exe1_LINK_LIBRARIES: ${exe1_LINK_LIBRARIES}")
message(STATUS "exe1_INTERFACE_LINK: ${exe1_INTERFACE_LINK_LIBRARIES}")
get_target_property(exe2_LINK_LIBRARIES exe2 LINK_LIBRARIES)
get_target_property(exe2_INTERFACE_LINK_LIBRARIES exe1 INTERFACE_LINK_LIBRARIES)
message(STATUS "exe2_LINK_LIBRARIES: ${exe2_LINK_LIBRARIES}")
message(STATUS "exe2_INTERFACE_LINK: ${exe2_INTERFACE_LINK_LIBRARIES}")
# https://stackoverflow.com/questions/51353110/how-do-i-output-the-result-of-a-generator-expression-in-cmake
file(
GENERATE
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/lib1_link_libraries.txt
CONTENT "$<TARGET_PROPERTY:lib1,LINK_LIBRARIES>")
file(
GENERATE
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/exe1_link_libraries.txt
CONTENT "$<TARGET_PROPERTY:exe1,LINK_LIBRARIES>")
file(
GENERATE
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/exe2_link_libraries.txt
CONTENT "$<TARGET_PROPERTY:exe2,LINK_LIBRARIES>")
# set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# CMake transitive LINK_LIBRARIES
Small test to check if the target property LINK_LIBRARIES was transitive. It seems it was only added
as the result of the generator expression $<TARGET_PROPERTY:tgt,LINK_LIBRARIES> in CMake 4.1.

0
dummy.cpp Normal file
View File

1
exe1_link_libraries.txt Normal file
View File

@ -0,0 +1 @@
lib1;lib2;interface4;shared1;interface1;static1;shared2;interface2;static2;interface3;shared3;interface3

1
exe2_link_libraries.txt Normal file
View File

@ -0,0 +1 @@
lib1;lib2;interface4;shared1;interface1;static1;shared2;interface2;static2;interface3;shared3;interface3

1
lib1_link_libraries.txt Normal file
View File

@ -0,0 +1 @@
shared1;interface1;static1;shared2;interface2;static2