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.
This commit is contained in:
iphydf 2023-12-26 00:20:57 +00:00
parent c58928cc89
commit 00ff078f91
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
7 changed files with 69 additions and 76 deletions

View File

@ -405,7 +405,12 @@ endif()
add_module(toxcore ${toxcore_SOURCES}) add_module(toxcore ${toxcore_SOURCES})
# Link it to all dependencies. # Link it to all dependencies.
target_link_modules(toxcore ${toxcore_LINK_MODULES}) if(TARGET toxcore_static)
target_link_libraries(toxcore_static PRIVATE ${toxcore_LINK_MODULES})
endif()
if(TARGET toxcore_shared)
target_link_libraries(toxcore_shared PRIVATE ${toxcore_LINK_MODULES})
endif()
# Make version script (on systems that support it) to limit symbol visibility. # Make version script (on systems that support it) to limit symbol visibility.
make_version_script(toxcore ${toxcore_API_HEADERS}) make_version_script(toxcore ${toxcore_API_HEADERS})
@ -422,7 +427,11 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)
function(unit_test subdir target) function(unit_test subdir target)
add_executable(unit_${target}_test ${subdir}/${target}_test.cc) add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
target_link_modules(unit_${target}_test toxcore) if(TARGET toxcore_static)
target_link_libraries(unit_${target}_test PRIVATE toxcore_static)
else()
target_link_libraries(unit_${target}_test PRIVATE toxcore_shared)
endif()
target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main) target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main)
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}") set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test) add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
@ -469,7 +478,12 @@ if(DHT_BOOTSTRAP)
add_executable(DHT_bootstrap add_executable(DHT_bootstrap
other/DHT_bootstrap.c other/DHT_bootstrap.c
other/bootstrap_node_packets.c) other/bootstrap_node_packets.c)
target_link_modules(DHT_bootstrap toxcore misc_tools) if(TARGET toxcore_static)
target_link_libraries(DHT_bootstrap PRIVATE toxcore_static)
else()
target_link_libraries(DHT_bootstrap PRIVATE toxcore_shared)
endif()
target_link_libraries(DHT_bootstrap PRIVATE misc_tools)
install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin) install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin)
endif() endif()

View File

@ -3,12 +3,22 @@ set(TEST_TIMEOUT_SECONDS "" CACHE STRING "Limit runtime of each test to the numb
add_library(auto_test_support add_library(auto_test_support
auto_test_support.c auto_test_support.c
auto_test_support.h) auto_test_support.h)
target_link_modules(auto_test_support toxcore misc_tools) target_link_libraries(auto_test_support PRIVATE misc_tools)
if(TARGET toxcore_static)
target_link_libraries(auto_test_support PRIVATE toxcore_static)
else()
target_link_libraries(auto_test_support PRIVATE toxcore_shared)
endif()
function(auto_test target) function(auto_test target)
if(AUTOTEST AND NOT (MSVC AND ARGV1 STREQUAL "MSVC_DONT_BUILD")) if(AUTOTEST AND NOT (MSVC AND ARGV1 STREQUAL "MSVC_DONT_BUILD"))
add_executable(auto_${target}_test ${target}_test.c) add_executable(auto_${target}_test ${target}_test.c)
target_link_modules(auto_${target}_test toxcore misc_tools auto_test_support) target_link_libraries(auto_${target}_test PRIVATE misc_tools auto_test_support)
if(TARGET toxcore_static)
target_link_libraries(auto_${target}_test PRIVATE toxcore_static)
else()
target_link_libraries(auto_${target}_test PRIVATE toxcore_shared)
endif()
if(NOT ARGV1 STREQUAL "DONT_RUN") if(NOT ARGV1 STREQUAL "DONT_RUN")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")

View File

@ -70,7 +70,7 @@ function(add_module lib)
endfunction() endfunction()
function(install_module lib) function(install_module lib)
if(ENABLE_SHARED) if(TARGET ${lib}_shared)
set_target_properties(${lib}_shared PROPERTIES set_target_properties(${lib}_shared PROPERTIES
VERSION ${SOVERSION} VERSION ${SOVERSION}
SOVERSION ${SOVERSION_MAJOR} SOVERSION ${SOVERSION_MAJOR}
@ -80,7 +80,7 @@ function(install_module lib)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif() endif()
if(ENABLE_STATIC) if(TARGET ${lib}_static)
install(TARGETS ${lib}_static install(TARGETS ${lib}_static
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@ -113,59 +113,3 @@ function(install_module lib)
install(FILES ${header} ${ARGN}) install(FILES ${header} ${ARGN})
endforeach() endforeach()
endfunction() endfunction()
function(target_link_modules target)
# If the target we're adding dependencies to is a shared library, add it to
# the set of targets.
if(TARGET ${target}_shared)
set(_targets ${_targets} ${target}_shared)
# Shared libraries should first try to link against other shared libraries.
set(${target}_shared_primary shared)
# If that fails (because the shared target doesn't exist), try linking
# against the static library. This requires the static library's objects to
# be PIC.
set(${target}_shared_secondary static)
endif()
# It can also be a static library at the same time.
if(TARGET ${target}_static)
set(_targets ${_targets} ${target}_static)
# Static libraries aren't actually linked, but their dependencies are
# recorded by "linking" them. If we link an executable to a static library,
# we want to also link statically against its transitive dependencies.
set(${target}_static_primary static)
# If a dependency doesn't exist as static library, we link against the
# shared one.
set(${target}_static_secondary shared)
endif()
# If it's neither, then it's an executable.
if(NOT _targets)
set(_targets ${_targets} ${target})
# Executables preferably link against static libraries, so they are
# standalone and can be shipped without any external dependencies. As a
# frame of reference: tests become roughly 600-800K binaries instead of
# 50-100K on x86_64 Linux.
set(${target}_primary static)
set(${target}_secondary shared)
endif()
foreach(dep ${ARGN})
foreach(_target ${_targets})
if(TARGET ${dep}_${${_target}_primary})
target_link_libraries(${_target} PRIVATE ${dep}_${${_target}_primary})
elseif(TARGET ${dep}_${${_target}_secondary})
target_link_libraries(${_target} PRIVATE ${dep}_${${_target}_secondary})
else()
# We record the modules linked to this target, so that we can collect
# them later when linking a composed module.
list(FIND LINK_MODULES ${dep} _index)
if(_index EQUAL -1)
set(LINK_MODULES ${LINK_MODULES} ${dep})
endif()
target_link_libraries(${_target} PRIVATE ${dep})
endif()
endforeach()
endforeach()
set(${target}_LINK_MODULES ${${target}_LINK_MODULES} ${LINK_MODULES} PARENT_SCOPE)
endfunction()

View File

@ -19,6 +19,11 @@ add_executable(tox-bootstrapd
src/tox-bootstrapd.c src/tox-bootstrapd.c
../bootstrap_node_packets.c ../bootstrap_node_packets.c
../bootstrap_node_packets.h) ../bootstrap_node_packets.h)
target_link_modules(tox-bootstrapd toxcore ${LIBCONFIG_LIBRARIES}) target_link_libraries(tox-bootstrapd PRIVATE ${LIBCONFIG_LIBRARIES})
if(TARGET toxcore_static)
target_link_libraries(tox-bootstrapd PRIVATE toxcore_static)
else()
target_link_libraries(tox-bootstrapd PRIVATE toxcore_shared)
endif()
install(TARGETS tox-bootstrapd RUNTIME DESTINATION bin) install(TARGETS tox-bootstrapd RUNTIME DESTINATION bin)
install(FILES bash-completion/completions/tox-bootstrapd DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions") install(FILES bash-completion/completions/tox-bootstrapd DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions")

View File

@ -1 +1 @@
1550b285e7d2f85a340fbde449dfbab3d49958794c918aebdb486ffc1b77c68c /usr/local/bin/tox-bootstrapd f0bff9fe04d56543d95a457afd76c618139eef99a4302337c66d07759d108e8b /usr/local/bin/tox-bootstrapd

View File

@ -1,30 +1,41 @@
function(target_link_toxcore target)
if(TARGET toxcore_static)
target_link_libraries(${target} PRIVATE toxcore_static)
else()
target_link_libraries(${target} PRIVATE toxcore_shared)
endif()
endfunction()
add_executable(save-generator save-generator.c) add_executable(save-generator save-generator.c)
target_link_modules(save-generator toxcore misc_tools) target_link_libraries(save-generator PRIVATE misc_tools)
target_link_toxcore(save-generator)
add_executable(strkey strkey.c) add_executable(strkey strkey.c)
target_link_modules(strkey toxcore ${LIBSODIUM_LIBRARIES}) target_link_libraries(strkey PRIVATE ${LIBSODIUM_LIBRARIES})
target_link_toxcore(strkey)
add_executable(create_bootstrap_keys create_bootstrap_keys.c) add_executable(create_bootstrap_keys create_bootstrap_keys.c)
target_link_modules(create_bootstrap_keys ${LIBSODIUM_LIBRARIES}) target_link_libraries(create_bootstrap_keys PRIVATE ${LIBSODIUM_LIBRARIES})
target_link_toxcore(create_bootstrap_keys)
add_executable(create_minimal_savedata create_minimal_savedata.c) add_executable(create_minimal_savedata create_minimal_savedata.c)
target_link_modules(create_minimal_savedata ${LIBSODIUM_LIBRARIES}) target_link_libraries(create_minimal_savedata PRIVATE ${LIBSODIUM_LIBRARIES})
add_executable(create_savedata create_savedata.c) add_executable(create_savedata create_savedata.c)
target_link_modules(create_savedata toxcore ${LIBSODIUM_LIBRARIES}) target_link_libraries(create_savedata PRIVATE ${LIBSODIUM_LIBRARIES})
target_link_toxcore(create_savedata)
add_executable(sign sign.c) add_executable(sign sign.c)
target_link_modules(sign ${LIBSODIUM_LIBRARIES} misc_tools) target_link_libraries(sign PRIVATE ${LIBSODIUM_LIBRARIES} misc_tools)
add_executable(cracker_simple cracker_simple.c) add_executable(cracker_simple cracker_simple.c)
target_link_modules(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools) target_link_libraries(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools)
# MSVC doesn't support OpenMP # MSVC doesn't support OpenMP
if(NOT MSVC) if(NOT MSVC)
find_package(OpenMP) find_package(OpenMP)
if(OpenMP_C_FOUND) if(OpenMP_C_FOUND)
add_executable(cracker cracker.c) add_executable(cracker cracker.c)
target_link_modules(cracker ${LIBSODIUM_LIBRARIES}) target_link_libraries(cracker PRIVATE OpenMP::OpenMP_C ${LIBSODIUM_LIBRARIES})
target_link_libraries(cracker PRIVATE OpenMP::OpenMP_C)
endif() endif()
endif() endif()

View File

@ -3,7 +3,11 @@ set(misc_tools_SOURCES
misc_tools.c misc_tools.c
misc_tools.h) misc_tools.h)
add_library(misc_tools STATIC ${misc_tools_SOURCES}) add_library(misc_tools STATIC ${misc_tools_SOURCES})
target_link_modules(misc_tools toxcore) if(TARGET toxcore_static)
target_link_libraries(misc_tools PRIVATE toxcore_static)
else()
target_link_libraries(misc_tools PRIVATE toxcore_shared)
endif()
################################################################################ ################################################################################
# #
@ -13,5 +17,10 @@ target_link_modules(misc_tools toxcore)
if(BUILD_MISC_TESTS) if(BUILD_MISC_TESTS)
add_executable(Messenger_test Messenger_test.c) add_executable(Messenger_test Messenger_test.c)
target_link_modules(Messenger_test toxcore misc_tools) target_link_libraries(Messenger_test PRIVATE misc_tools)
if(TARGET toxcore_static)
target_link_libraries(Messenger_test PRIVATE toxcore_static)
else()
target_link_libraries(Messenger_test PRIVATE toxcore_shared)
endif()
endif() endif()