Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt
3153 views
1
cmake_minimum_required(VERSION 3.10)
2
3
if(POLICY CMP0129)
4
cmake_policy(SET CMP0129 NEW)
5
endif()
6
7
project(target_link_libraries)
8
9
file(WRITE
10
"${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
11
"int main() { return 0; }
12
"
13
)
14
15
add_executable(
16
target_link_libraries
17
"${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
18
)
19
20
macro(ASSERT_PROPERTY _target _property _value)
21
get_target_property(_out ${_target} ${_property})
22
if (NOT _out)
23
set(_out "")
24
endif()
25
if (NOT "${_out}" STREQUAL "${_value}")
26
message(SEND_ERROR "Target ${_target} does not have property ${_property} with value ${_value}. Actual value: ${_out}")
27
endif()
28
endmacro()
29
30
include(GenerateExportHeader)
31
set(CMAKE_INCLUDE_CURRENT_DIR ON)
32
33
add_library(depA SHARED depA.cpp)
34
generate_export_header(depA)
35
36
add_library(depB SHARED depB.cpp)
37
generate_export_header(depB)
38
39
target_link_libraries(depB LINK_PRIVATE depA LINK_PRIVATE depA)
40
41
add_library(libgenex SHARED libgenex.cpp)
42
generate_export_header(libgenex)
43
44
set_property(TARGET depB APPEND PROPERTY
45
LINK_LIBRARIES $<1:libgenex>
46
)
47
48
add_library(depC SHARED depC.cpp)
49
generate_export_header(depC)
50
51
target_link_libraries(depC LINK_PUBLIC depA LINK_PUBLIC depA)
52
53
assert_property(depA INTERFACE_LINK_LIBRARIES "")
54
assert_property(depB INTERFACE_LINK_LIBRARIES "")
55
assert_property(depC INTERFACE_LINK_LIBRARIES "depA;depA")
56
57
add_executable(targetA targetA.cpp)
58
59
target_link_libraries(targetA LINK_INTERFACE_LIBRARIES depA depB)
60
61
assert_property(targetA INTERFACE_LINK_LIBRARIES "depA;depB")
62
63
set_target_properties(targetA PROPERTIES INTERFACE_LINK_LIBRARIES "")
64
65
assert_property(targetA INTERFACE_LINK_LIBRARIES "")
66
67
add_subdirectory(subdir)
68
target_link_libraries(targetA subdirlib)
69
70
target_link_libraries(targetA depB depC)
71
72
assert_property(targetA INTERFACE_LINK_LIBRARIES ";subdirlib;depB;depC")
73
74
# Exclude depIfaceOnly from ALL so that it will only be built if something
75
# depends on it. As it is in the link interface of depB, targetA
76
# will depend on it. That dependency is what is being tested here.
77
add_library(depIfaceOnly SHARED EXCLUDE_FROM_ALL depIfaceOnly.cpp)
78
generate_export_header(depIfaceOnly)
79
set_property(TARGET depB APPEND PROPERTY INTERFACE_LINK_LIBRARIES depIfaceOnly)
80
81
add_library(depD SHARED depD.cpp)
82
generate_export_header(depD)
83
set_property(TARGET depD APPEND PROPERTY
84
INTERFACE_LINK_LIBRARIES
85
$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:depA>
86
)
87
88
add_executable(targetB targetB.cpp)
89
target_link_libraries(targetB depD)
90
91
macro(create_header _name)
92
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_name}")
93
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_name}/${_name}.h" "//${_name}.h\n")
94
endmacro()
95
96
create_header(foo)
97
create_header(bar)
98
99
add_library(depG SHARED depG.cpp)
100
generate_export_header(depG)
101
target_include_directories(depG INTERFACE
102
"${CMAKE_CURRENT_BINARY_DIR}/foo"
103
"${CMAKE_CURRENT_BINARY_DIR}/bar"
104
)
105
target_compile_definitions(depG INTERFACE
106
TEST_DEF
107
)
108
109
110
add_executable(targetC targetC.cpp)
111
if(NOT BORLAND AND NOT WATCOM)
112
# Linking to a target containing a + should be non-fatal, though it does
113
# not work at all on Borland or watcom
114
add_library(wrapc++ empty.cpp)
115
target_link_libraries(targetC wrapc++)
116
endif()
117
# The TARGET_PROPERTY expression is duplicated below to test that there is no
118
# shortcutting of the evaluation by returning an empty string.
119
set(_exe_test $<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>)
120
target_link_libraries(targetC $<$<AND:${_exe_test},${_exe_test}>:depG>)
121
122
add_library(libConsumer empty.cpp)
123
# This line causes $<$<CONFIG:Debug>:depA> to be used when
124
# determining the include directories for libConsumer based on the
125
# interface properties of its LINK_LIBRARIES. Because the above expression
126
# evaluates to the empty string in non-Debug cases, ensure that that causes
127
# no problems.
128
target_link_libraries(libConsumer debug depA)
129
130
add_subdirectory(cmp0022)
131
132
add_executable(newsignature1 newsignature1.cpp)
133
target_link_libraries(newsignature1 PRIVATE depC INTERFACE depD PUBLIC depB PRIVATE subdirlib INTERFACE INTERFACE PUBLIC)
134
135
assert_property(newsignature1 INTERFACE_LINK_LIBRARIES "depD;depB")
136
assert_property(newsignature1 LINK_LIBRARIES "depC;depB;subdirlib")
137
138
#----------------------------------------------------------------------------
139
# Test cross-directory linking.
140
cmake_policy(PUSH)
141
cmake_policy(SET CMP0022 NEW)
142
cmake_policy(SET CMP0079 NEW)
143
add_executable(TopDir TopDir.c)
144
add_library(TopDirInterface INTERFACE)
145
target_link_libraries(TopDir PRIVATE TopDirInterface)
146
add_subdirectory(SubDirA)
147
add_subdirectory(SubDirB)
148
target_link_libraries(SubDirB TopDirImported)
149
add_subdirectory(SubDirC)
150
target_link_libraries(SubDirC PRIVATE SubDirC2)
151
target_link_libraries(TopDir PRIVATE SubDirC)
152
add_library(TopDirImported IMPORTED INTERFACE)
153
target_compile_definitions(TopDirImported INTERFACE DEF_TopDirImported)
154
cmake_policy(POP)
155
156
#----------------------------------------------------------------------------
157
# Test $<COMPILE_ONLY:> genex.
158
cmake_policy(SET CMP0099 NEW)
159
add_library(dont_link_too SHARED compile_only.cpp)
160
target_compile_definitions(dont_link_too PUBLIC USE_EXAMPLE PRIVATE HAVE_FUNCTION)
161
target_link_options(dont_link_too INTERFACE invalid_link_option)
162
target_link_libraries(dont_link_too INTERFACE invalid_link_library)
163
164
add_library(uses_compile_only_genex SHARED compile_only.cpp)
165
target_link_libraries(uses_compile_only_genex PUBLIC $<COMPILE_ONLY:dont_link_too>)
166
167
add_library(uses_compile_only_genex_static STATIC compile_only.cpp)
168
target_link_libraries(uses_compile_only_genex_static PRIVATE $<COMPILE_ONLY:dont_link_too>)
169
add_executable(uses_via_static_linking main.cxx)
170
target_link_libraries(uses_via_static_linking PRIVATE uses_compile_only_genex_static)
171
172
add_library(uses_compile_only_genex_obj SHARED compile_only.cpp)
173
target_compile_definitions(uses_compile_only_genex_obj PRIVATE HAVE_FUNCTION)
174
target_link_libraries(uses_compile_only_genex_obj PRIVATE $<COMPILE_ONLY:dont_link_too>)
175
176
cmake_policy(SET CMP0131 NEW)
177
add_library(only_link_too OBJECT link_only.cpp)
178
target_compile_options(only_link_too INTERFACE INVALID_COMPILER_FLAG_ARRRRGH)
179
180
add_executable(uses_link_only_genex_obj compile_only.cpp)
181
target_compile_definitions(uses_link_only_genex_obj PUBLIC USE_EXAMPLE)
182
target_link_libraries(uses_link_only_genex_obj PRIVATE $<LINK_ONLY:only_link_too>)
183
184