]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: omit linking duplicate/unnecessary libs to tests & examples
authorViktor Szakats <commit@vsz.me>
Sat, 21 Jun 2025 18:05:39 +0000 (20:05 +0200)
committerViktor Szakats <commit@vsz.me>
Mon, 28 Jul 2025 09:28:15 +0000 (11:28 +0200)
Before this patch we explicitly linked the full list of libcurl
dependency libs to tests and examples via `CURL_LIBS`. This was
redundant, because test and example code do not directly use these
dependency libs and for indirect use they are implicitly passed
via libcurl as needed. After this patch, tests and examples only link
explicitly to system libs (e.g. socket).

Also bringing it closer to how `./configure` does this.

Borrow the variable name `CURL_NETWORK_AND_TIME_LIBS` from
`./configure`. However, its content is not exactly the same. With cmake
it also holds `pthread`, but doesn't hold AmiSSL.

Closes #17696

CMakeLists.txt
docs/examples/CMakeLists.txt
tests/client/CMakeLists.txt
tests/libtest/CMakeLists.txt
tests/server/CMakeLists.txt

index d9d4acf1609fb8acf3150b6ef1c14594504444ac..d113989a5cc0ffdc10376a1eb015e41b8da9df35 100644 (file)
@@ -362,6 +362,7 @@ endif()
 option(CURL_USE_PKGCONFIG "Enable pkg-config to detect dependencies" ${_curl_use_pkgconfig_default})
 
 # Initialize variables collecting dependency libs, paths, pkg-config names.
+set(CURL_NETWORK_AND_TIME_LIBS "")
 set(CURL_LIBS "")
 set(CURL_LIBDIRS "")
 set(LIBCURL_PC_REQUIRES_PRIVATE "")
@@ -544,9 +545,9 @@ endif()
 
 # If we are on Haiku, make sure that the network library is brought in.
 if(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
-  list(APPEND CURL_LIBS "network")
+  list(APPEND CURL_NETWORK_AND_TIME_LIBS "network")
 elseif(AMIGA)
-  list(APPEND CURL_LIBS "net" "m" "atomic")
+  list(APPEND CURL_NETWORK_AND_TIME_LIBS "net" "m" "atomic")
   list(APPEND CMAKE_REQUIRED_LIBRARIES "net" "m" "atomic")
 endif()
 
@@ -591,7 +592,7 @@ if(ENABLE_THREADED_RESOLVER)
     find_package(Threads REQUIRED)
     set(USE_THREADS_POSIX ${CMAKE_USE_PTHREADS_INIT})
     set(HAVE_PTHREAD_H ${CMAKE_USE_PTHREADS_INIT})
-    list(APPEND CURL_LIBS ${CMAKE_THREAD_LIBS_INIT})
+    list(APPEND CURL_NETWORK_AND_TIME_LIBS ${CMAKE_THREAD_LIBS_INIT})
   endif()
 endif()
 
@@ -614,7 +615,7 @@ elseif(DOS)
   if(WATT_ROOT)
     set(USE_WATT32 ON)
     # FIXME upstream: must specify the full path to avoid CMake converting "watt" to "watt.lib"
-    list(APPEND CURL_LIBS "${WATT_ROOT}/lib/libwatt.a")
+    list(APPEND CURL_NETWORK_AND_TIME_LIBS "${WATT_ROOT}/lib/libwatt.a")
     include_directories(SYSTEM "${WATT_ROOT}/inc")
     list(APPEND CMAKE_REQUIRED_INCLUDES "${WATT_ROOT}/inc")
   else()
@@ -634,7 +635,7 @@ elseif(AMIGA)
 elseif(NOT APPLE)
   check_library_exists("socket" "connect" "" HAVE_LIBSOCKET)
   if(HAVE_LIBSOCKET)
-    set(CURL_LIBS "socket" ${CURL_LIBS})
+    set(CURL_NETWORK_AND_TIME_LIBS "socket" ${CURL_NETWORK_AND_TIME_LIBS})
   endif()
 endif()
 
@@ -1944,9 +1945,9 @@ include(CMake/OtherTests.cmake)
 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "HAVE_CONFIG_H")
 
 if(WIN32)
-  list(APPEND CURL_LIBS "${_win32_winsock}")
+  list(APPEND CURL_NETWORK_AND_TIME_LIBS "${_win32_winsock}")
   if(NOT WINCE AND NOT WINDOWS_STORE)
-    list(APPEND CURL_LIBS "iphlpapi")
+    list(APPEND CURL_NETWORK_AND_TIME_LIBS "iphlpapi")
   endif()
   if(NOT WINCE)
     list(APPEND CURL_LIBS "bcrypt")
@@ -1973,6 +1974,8 @@ if(WIN32)
   endif()
 endif()
 
+list(APPEND CURL_LIBS ${CURL_NETWORK_AND_TIME_LIBS})
+
 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")  # MSVC but exclude clang-cl
   set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "-MP")  # Parallel compilation
 endif()
index 595439301756488694118506b344721f3e43059d..0963c62d9a70b5c710e9cbfb3f73f68e4c3c2b47 100644 (file)
@@ -32,7 +32,7 @@ foreach(_target IN LISTS check_PROGRAMS)
   set(_target_name "curl-example-${_target}")
   add_executable(${_target_name} EXCLUDE_FROM_ALL "${_target}.c")
   add_dependencies(curl-examples ${_target_name})
-  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_LIBS})
+  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS})
   target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" "$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_DEPRECATE>")
   set_target_properties(${_target_name} PROPERTIES OUTPUT_NAME "${_target}" PROJECT_LABEL "Example ${_target}" UNITY_BUILD OFF)
 endforeach()
index 0d8af5683279b6918d79bb797d3598e6667f8149..c8cec6f2029b6e264a5d09fb134b6bbf3f68b11a 100644 (file)
@@ -40,7 +40,7 @@ add_custom_command(OUTPUT "${BUNDLE}.c"
 
 add_executable(${BUNDLE} EXCLUDE_FROM_ALL "${BUNDLE}.c")
 add_dependencies(testdeps ${BUNDLE})
-target_link_libraries(${BUNDLE} ${LIB_SELECTED} ${CURL_LIBS})
+target_link_libraries(${BUNDLE} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS})
 target_include_directories(${BUNDLE} PRIVATE
   "${PROJECT_BINARY_DIR}/lib"            # for "curl_config.h"
   "${PROJECT_SOURCE_DIR}/lib"            # for "curl_setup.h", curlx
index 859f29447d1a20652fea66939ee59bea6c3824c5..77b69a0a3038042668d95f569fccc6e783b2d364 100644 (file)
@@ -49,7 +49,7 @@ add_custom_command(OUTPUT "${BUNDLE}.c"
 
 add_executable(${BUNDLE} EXCLUDE_FROM_ALL "${BUNDLE}.c")
 add_dependencies(testdeps ${BUNDLE})
-target_link_libraries(${BUNDLE} ${LIB_SELECTED} ${CURL_LIBS})
+target_link_libraries(${BUNDLE} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS})
 target_include_directories(${BUNDLE} PRIVATE
   "${PROJECT_BINARY_DIR}/lib"            # for "curl_config.h"
   "${PROJECT_SOURCE_DIR}/lib"            # for "curl_setup.h", curlx
index 549b5536fcf5995405c688ff7705e716c442c35c..887f8c72ec2a5bd22b2dca49e0a38dcd533c6995 100644 (file)
@@ -36,7 +36,7 @@ add_custom_command(OUTPUT "${BUNDLE}.c"
 
 add_executable(${BUNDLE} EXCLUDE_FROM_ALL "${BUNDLE}.c")
 add_dependencies(testdeps ${BUNDLE})
-target_link_libraries(${BUNDLE} ${CURL_LIBS})
+target_link_libraries(${BUNDLE} ${CURL_NETWORK_AND_TIME_LIBS})
 target_include_directories(${BUNDLE} PRIVATE
   "${PROJECT_BINARY_DIR}/lib"            # for "curl_config.h"
   "${PROJECT_SOURCE_DIR}/lib"            # for "curl_setup.h", curlx