Skip to content

Commit 00ff078

Browse files
committed
cleanup: Use target_link_libraries directly in cmake.
Instead of using `target_link_modules`, which does magic that we no longer need, because we only have 1 library we install, and all binaries we build link statically because they need access to internal symbols.
1 parent c58928c commit 00ff078

File tree

7 files changed

+69
-76
lines changed

7 files changed

+69
-76
lines changed

CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,12 @@ endif()
405405
add_module(toxcore ${toxcore_SOURCES})
406406

407407
# Link it to all dependencies.
408-
target_link_modules(toxcore ${toxcore_LINK_MODULES})
408+
if(TARGET toxcore_static)
409+
target_link_libraries(toxcore_static PRIVATE ${toxcore_LINK_MODULES})
410+
endif()
411+
if(TARGET toxcore_shared)
412+
target_link_libraries(toxcore_shared PRIVATE ${toxcore_LINK_MODULES})
413+
endif()
409414

410415
# Make version script (on systems that support it) to limit symbol visibility.
411416
make_version_script(toxcore ${toxcore_API_HEADERS})
@@ -422,7 +427,11 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)
422427

423428
function(unit_test subdir target)
424429
add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
425-
target_link_modules(unit_${target}_test toxcore)
430+
if(TARGET toxcore_static)
431+
target_link_libraries(unit_${target}_test PRIVATE toxcore_static)
432+
else()
433+
target_link_libraries(unit_${target}_test PRIVATE toxcore_shared)
434+
endif()
426435
target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main)
427436
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
428437
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
@@ -469,7 +478,12 @@ if(DHT_BOOTSTRAP)
469478
add_executable(DHT_bootstrap
470479
other/DHT_bootstrap.c
471480
other/bootstrap_node_packets.c)
472-
target_link_modules(DHT_bootstrap toxcore misc_tools)
481+
if(TARGET toxcore_static)
482+
target_link_libraries(DHT_bootstrap PRIVATE toxcore_static)
483+
else()
484+
target_link_libraries(DHT_bootstrap PRIVATE toxcore_shared)
485+
endif()
486+
target_link_libraries(DHT_bootstrap PRIVATE misc_tools)
473487
install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin)
474488
endif()
475489

auto_tests/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ set(TEST_TIMEOUT_SECONDS "" CACHE STRING "Limit runtime of each test to the numb
33
add_library(auto_test_support
44
auto_test_support.c
55
auto_test_support.h)
6-
target_link_modules(auto_test_support toxcore misc_tools)
6+
target_link_libraries(auto_test_support PRIVATE misc_tools)
7+
if(TARGET toxcore_static)
8+
target_link_libraries(auto_test_support PRIVATE toxcore_static)
9+
else()
10+
target_link_libraries(auto_test_support PRIVATE toxcore_shared)
11+
endif()
712

813
function(auto_test target)
914
if(AUTOTEST AND NOT (MSVC AND ARGV1 STREQUAL "MSVC_DONT_BUILD"))
1015
add_executable(auto_${target}_test ${target}_test.c)
11-
target_link_modules(auto_${target}_test toxcore misc_tools auto_test_support)
16+
target_link_libraries(auto_${target}_test PRIVATE misc_tools auto_test_support)
17+
if(TARGET toxcore_static)
18+
target_link_libraries(auto_${target}_test PRIVATE toxcore_static)
19+
else()
20+
target_link_libraries(auto_${target}_test PRIVATE toxcore_shared)
21+
endif()
1222
if(NOT ARGV1 STREQUAL "DONT_RUN")
1323
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
1424
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")

cmake/ModulePackage.cmake

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function(add_module lib)
7070
endfunction()
7171

7272
function(install_module lib)
73-
if(ENABLE_SHARED)
73+
if(TARGET ${lib}_shared)
7474
set_target_properties(${lib}_shared PROPERTIES
7575
VERSION ${SOVERSION}
7676
SOVERSION ${SOVERSION_MAJOR}
@@ -80,7 +80,7 @@ function(install_module lib)
8080
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
8181
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
8282
endif()
83-
if(ENABLE_STATIC)
83+
if(TARGET ${lib}_static)
8484
install(TARGETS ${lib}_static
8585
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
8686
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -113,59 +113,3 @@ function(install_module lib)
113113
install(FILES ${header} ${ARGN})
114114
endforeach()
115115
endfunction()
116-
117-
function(target_link_modules target)
118-
# If the target we're adding dependencies to is a shared library, add it to
119-
# the set of targets.
120-
if(TARGET ${target}_shared)
121-
set(_targets ${_targets} ${target}_shared)
122-
# Shared libraries should first try to link against other shared libraries.
123-
set(${target}_shared_primary shared)
124-
# If that fails (because the shared target doesn't exist), try linking
125-
# against the static library. This requires the static library's objects to
126-
# be PIC.
127-
set(${target}_shared_secondary static)
128-
endif()
129-
# It can also be a static library at the same time.
130-
if(TARGET ${target}_static)
131-
set(_targets ${_targets} ${target}_static)
132-
# Static libraries aren't actually linked, but their dependencies are
133-
# recorded by "linking" them. If we link an executable to a static library,
134-
# we want to also link statically against its transitive dependencies.
135-
set(${target}_static_primary static)
136-
# If a dependency doesn't exist as static library, we link against the
137-
# shared one.
138-
set(${target}_static_secondary shared)
139-
endif()
140-
# If it's neither, then it's an executable.
141-
if(NOT _targets)
142-
set(_targets ${_targets} ${target})
143-
# Executables preferably link against static libraries, so they are
144-
# standalone and can be shipped without any external dependencies. As a
145-
# frame of reference: tests become roughly 600-800K binaries instead of
146-
# 50-100K on x86_64 Linux.
147-
set(${target}_primary static)
148-
set(${target}_secondary shared)
149-
endif()
150-
151-
foreach(dep ${ARGN})
152-
foreach(_target ${_targets})
153-
if(TARGET ${dep}_${${_target}_primary})
154-
target_link_libraries(${_target} PRIVATE ${dep}_${${_target}_primary})
155-
elseif(TARGET ${dep}_${${_target}_secondary})
156-
target_link_libraries(${_target} PRIVATE ${dep}_${${_target}_secondary})
157-
else()
158-
# We record the modules linked to this target, so that we can collect
159-
# them later when linking a composed module.
160-
list(FIND LINK_MODULES ${dep} _index)
161-
if(_index EQUAL -1)
162-
set(LINK_MODULES ${LINK_MODULES} ${dep})
163-
endif()
164-
165-
target_link_libraries(${_target} PRIVATE ${dep})
166-
endif()
167-
endforeach()
168-
endforeach()
169-
170-
set(${target}_LINK_MODULES ${${target}_LINK_MODULES} ${LINK_MODULES} PARENT_SCOPE)
171-
endfunction()

other/bootstrap_daemon/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ add_executable(tox-bootstrapd
1919
src/tox-bootstrapd.c
2020
../bootstrap_node_packets.c
2121
../bootstrap_node_packets.h)
22-
target_link_modules(tox-bootstrapd toxcore ${LIBCONFIG_LIBRARIES})
22+
target_link_libraries(tox-bootstrapd PRIVATE ${LIBCONFIG_LIBRARIES})
23+
if(TARGET toxcore_static)
24+
target_link_libraries(tox-bootstrapd PRIVATE toxcore_static)
25+
else()
26+
target_link_libraries(tox-bootstrapd PRIVATE toxcore_shared)
27+
endif()
2328
install(TARGETS tox-bootstrapd RUNTIME DESTINATION bin)
2429
install(FILES bash-completion/completions/tox-bootstrapd DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1550b285e7d2f85a340fbde449dfbab3d49958794c918aebdb486ffc1b77c68c /usr/local/bin/tox-bootstrapd
1+
f0bff9fe04d56543d95a457afd76c618139eef99a4302337c66d07759d108e8b /usr/local/bin/tox-bootstrapd

other/fun/CMakeLists.txt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
1+
function(target_link_toxcore target)
2+
if(TARGET toxcore_static)
3+
target_link_libraries(${target} PRIVATE toxcore_static)
4+
else()
5+
target_link_libraries(${target} PRIVATE toxcore_shared)
6+
endif()
7+
endfunction()
8+
19
add_executable(save-generator save-generator.c)
2-
target_link_modules(save-generator toxcore misc_tools)
10+
target_link_libraries(save-generator PRIVATE misc_tools)
11+
target_link_toxcore(save-generator)
312

413
add_executable(strkey strkey.c)
5-
target_link_modules(strkey toxcore ${LIBSODIUM_LIBRARIES})
14+
target_link_libraries(strkey PRIVATE ${LIBSODIUM_LIBRARIES})
15+
target_link_toxcore(strkey)
616

717
add_executable(create_bootstrap_keys create_bootstrap_keys.c)
8-
target_link_modules(create_bootstrap_keys ${LIBSODIUM_LIBRARIES})
18+
target_link_libraries(create_bootstrap_keys PRIVATE ${LIBSODIUM_LIBRARIES})
19+
target_link_toxcore(create_bootstrap_keys)
920

1021
add_executable(create_minimal_savedata create_minimal_savedata.c)
11-
target_link_modules(create_minimal_savedata ${LIBSODIUM_LIBRARIES})
22+
target_link_libraries(create_minimal_savedata PRIVATE ${LIBSODIUM_LIBRARIES})
1223

1324
add_executable(create_savedata create_savedata.c)
14-
target_link_modules(create_savedata toxcore ${LIBSODIUM_LIBRARIES})
25+
target_link_libraries(create_savedata PRIVATE ${LIBSODIUM_LIBRARIES})
26+
target_link_toxcore(create_savedata)
1527

1628
add_executable(sign sign.c)
17-
target_link_modules(sign ${LIBSODIUM_LIBRARIES} misc_tools)
29+
target_link_libraries(sign PRIVATE ${LIBSODIUM_LIBRARIES} misc_tools)
1830

1931
add_executable(cracker_simple cracker_simple.c)
20-
target_link_modules(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools)
32+
target_link_libraries(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools)
2133

2234
# MSVC doesn't support OpenMP
2335
if(NOT MSVC)
2436
find_package(OpenMP)
2537
if(OpenMP_C_FOUND)
2638
add_executable(cracker cracker.c)
27-
target_link_modules(cracker ${LIBSODIUM_LIBRARIES})
28-
target_link_libraries(cracker PRIVATE OpenMP::OpenMP_C)
39+
target_link_libraries(cracker PRIVATE OpenMP::OpenMP_C ${LIBSODIUM_LIBRARIES})
2940
endif()
3041
endif()

testing/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ set(misc_tools_SOURCES
33
misc_tools.c
44
misc_tools.h)
55
add_library(misc_tools STATIC ${misc_tools_SOURCES})
6-
target_link_modules(misc_tools toxcore)
6+
if(TARGET toxcore_static)
7+
target_link_libraries(misc_tools PRIVATE toxcore_static)
8+
else()
9+
target_link_libraries(misc_tools PRIVATE toxcore_shared)
10+
endif()
711

812
################################################################################
913
#
@@ -13,5 +17,10 @@ target_link_modules(misc_tools toxcore)
1317

1418
if(BUILD_MISC_TESTS)
1519
add_executable(Messenger_test Messenger_test.c)
16-
target_link_modules(Messenger_test toxcore misc_tools)
20+
target_link_libraries(Messenger_test PRIVATE misc_tools)
21+
if(TARGET toxcore_static)
22+
target_link_libraries(Messenger_test PRIVATE toxcore_static)
23+
else()
24+
target_link_libraries(Messenger_test PRIVATE toxcore_shared)
25+
endif()
1726
endif()

0 commit comments

Comments
 (0)