Skip to content

Commit

Permalink
cleanup: Use target_link_libraries directly in cmake.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iphydf committed Dec 25, 2023
1 parent 895a6af commit 1e432e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 84 deletions.
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,12 @@ endif()
add_module(toxcore ${toxcore_SOURCES})

# 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(toxcore ${toxcore_API_HEADERS})
Expand All @@ -422,8 +427,8 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)

function(unit_test subdir target)
add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
target_link_modules(unit_${target}_test toxcore)
target_link_libraries(unit_${target}_test GTest::GTest GTest::Main)
target_link_libraries(unit_${target}_test toxcore_static)
target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main)
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw")
Expand Down Expand Up @@ -469,7 +474,7 @@ if(DHT_BOOTSTRAP)
add_executable(DHT_bootstrap
other/DHT_bootstrap.c
other/bootstrap_node_packets.c)
target_link_modules(DHT_bootstrap toxcore misc_tools)
target_link_libraries(DHT_bootstrap toxcore_static misc_tools)
install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin)
endif()

Expand Down
19 changes: 8 additions & 11 deletions auto_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ set(TEST_TIMEOUT_SECONDS "" CACHE STRING "Limit runtime of each test to the numb
add_library(auto_test_support
auto_test_support.c
auto_test_support.h)
target_link_modules(auto_test_support toxcore misc_tools)
target_link_libraries(auto_test_support PRIVATE toxcore_static misc_tools)

function(auto_test target)
if(AUTOTEST AND NOT (MSVC AND ARGV1 STREQUAL "MSVC_DONT_BUILD"))
if(AUTOTEST)
add_executable(auto_${target}_test ${target}_test.c)
target_link_modules(auto_${target}_test toxcore misc_tools auto_test_support)
if(NOT ARGV1 STREQUAL "DONT_RUN")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
# add the source dir as environment variable, so the testdata can be found
set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endif()
target_link_libraries(auto_${target}_test PRIVATE toxcore_static misc_tools auto_test_support)
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
# add the source dir as environment variable, so the testdata can be found
set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endif()
endfunction()

Expand Down Expand Up @@ -75,12 +73,11 @@ if(NON_HERMETIC_TESTS)
endif()

if(BUILD_TOXAV)
auto_test(conference_av MSVC_DONT_BUILD)
auto_test(conference_av)
auto_test(toxav_basic)
auto_test(toxav_many)
endif()


if(PROXY_TEST)
auto_test(proxy)
endif()
62 changes: 5 additions & 57 deletions cmake/ModulePackage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ find_package(PkgConfig)
function(pkg_use_module mod pkgs)
foreach(pkg IN ITEMS ${pkgs})
if(PKG_CONFIG_FOUND)
pkg_search_module(${mod} ${pkg})
pkg_search_module(${mod} ${pkg} IMPORTED_TARGET)
endif()
if(NOT ${mod}_FOUND)
find_package(${pkg} QUIET)
Expand Down Expand Up @@ -62,10 +62,14 @@ function(add_module lib)
if(ENABLE_SHARED)
add_library(${lib}_shared SHARED ${ARGN})
set_target_properties(${lib}_shared PROPERTIES OUTPUT_NAME ${lib})
add_library(${lib} ALIAS ${lib}_shared)
endif()
if(ENABLE_STATIC)
add_library(${lib}_static STATIC ${ARGN})
set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
if(NOT ENABLE_SHARED)
add_library(${lib} ALIAS ${lib}_static)
endif()
endif()
endfunction()

Expand Down Expand Up @@ -113,59 +117,3 @@ function(install_module lib)
install(FILES ${header} ${ARGN})
endforeach()
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} ${dep}_${${_target}_primary})
elseif(TARGET ${dep}_${${_target}_secondary})
target_link_libraries(${_target} ${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} ${dep})
endif()
endforeach()
endforeach()

set(${target}_LINK_MODULES ${${target}_LINK_MODULES} ${LINK_MODULES} PARENT_SCOPE)
endfunction()
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ add_executable(tox-bootstrapd
src/tox-bootstrapd.c
../bootstrap_node_packets.c
../bootstrap_node_packets.h)
target_link_modules(tox-bootstrapd toxcore ${LIBCONFIG_LIBRARIES})
target_link_libraries(tox-bootstrapd PRIVATE toxcore_static ${LIBCONFIG_LIBRARIES})
install(TARGETS tox-bootstrapd RUNTIME DESTINATION bin)
install(FILES bash-completion/completions/tox-bootstrapd DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions")
17 changes: 8 additions & 9 deletions other/fun/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
add_executable(save-generator save-generator.c)
target_link_modules(save-generator toxcore misc_tools)
target_link_libraries(save-generator PRIVATE toxcore_static misc_tools)

add_executable(strkey strkey.c)
target_link_modules(strkey toxcore ${LIBSODIUM_LIBRARIES})
target_link_libraries(strkey PRIVATE toxcore_static ${LIBSODIUM_LIBRARIES})

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})

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)
target_link_modules(create_savedata toxcore ${LIBSODIUM_LIBRARIES})
target_link_libraries(create_savedata PRIVATE toxcore_static ${LIBSODIUM_LIBRARIES})

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)
target_link_modules(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools)
target_link_libraries(cracker_simple PRIVATE ${LIBSODIUM_LIBRARIES} misc_tools)

# MSVC doesn't support OpenMP
if(NOT MSVC)
find_package(OpenMP)
if(OpenMP_C_FOUND)
add_executable(cracker cracker.c)
target_link_modules(cracker ${LIBSODIUM_LIBRARIES})
target_link_libraries(cracker OpenMP::OpenMP_C)
target_link_libraries(cracker PRIVATE ${LIBSODIUM_LIBRARIES} OpenMP::OpenMP_C)
endif()
endif()
4 changes: 2 additions & 2 deletions testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set(misc_tools_SOURCES
misc_tools.c
misc_tools.h)
add_library(misc_tools STATIC ${misc_tools_SOURCES})
target_link_modules(misc_tools toxcore)
target_link_libraries(misc_tools PRIVATE toxcore_static)

################################################################################
#
Expand All @@ -13,5 +13,5 @@ target_link_modules(misc_tools toxcore)

if(BUILD_MISC_TESTS)
add_executable(Messenger_test Messenger_test.c)
target_link_modules(Messenger_test toxcore misc_tools)
target_link_libraries(Messenger_test PRIVATE toxcore_static misc_tools)
endif()

0 comments on commit 1e432e8

Please sign in to comment.