]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: sync code between test/example targets
authorViktor Szakats <commit@vsz.me>
Thu, 22 Aug 2024 07:10:07 +0000 (09:10 +0200)
committerViktor Szakats <commit@vsz.me>
Fri, 23 Aug 2024 16:42:56 +0000 (18:42 +0200)
- reuse local variable names.
- sync `PROJECT_LABEL`, add where missing.
- namespace all target names.
- bind header directories to each target.
- tests/server: limit `CURL_STATICLIB` to Windows (as in autotools.)
- drop functions with a single caller.

Closes #14660

docs/examples/CMakeLists.txt
tests/http/clients/CMakeLists.txt
tests/libtest/CMakeLists.txt
tests/server/CMakeLists.txt
tests/unit/CMakeLists.txt

index 2dc92f3f255afdd88d2d11c8f851eab12f90245e..75784ab501891fb64fb0b8e694d785ecc24c3a00 100644 (file)
 transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
 include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
 
-foreach(_example IN LISTS check_PROGRAMS)
-  set(_example_target "curl-example-${_example}")
-  add_executable(${_example_target} "${_example}.c")
-  target_link_libraries(${_example_target} ${LIB_SELECTED} ${CURL_LIBS})
-  target_compile_definitions(${_example_target} PRIVATE "CURL_NO_OLDIES")
+foreach(_target IN LISTS check_PROGRAMS)
+  set(_target_name "curl-example-${_target}")
+  add_executable(${_target_name} "${_target}.c")
+  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_LIBS})
+  target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES")
   if(LIB_SELECTED STREQUAL LIB_STATIC AND WIN32)
-    set_property(TARGET ${_example_target} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
+    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
   endif()
   if(MINGW)
     # For mingw-w64 7.0.0 and earlier to avoid '-Wformat='
-    set_property(TARGET ${_example_target} APPEND PROPERTY COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
+    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
   endif()
-  set_target_properties(${_example_target} PROPERTIES
-    OUTPUT_NAME "${_example}" UNITY_BUILD OFF)
+  set_target_properties(${_target_name} PROPERTIES
+    OUTPUT_NAME "${_target}" UNITY_BUILD OFF
+    PROJECT_LABEL "Example ${_target}")
 endforeach()
index 0747a1926f30fb5dd9fe8a3a3ca38d1cc0f0a254..8ce1edb4fc6e16b0cf672b0e540713863ea27fa3 100644 (file)
 transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
 include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
 
-foreach(_test_name IN LISTS check_PROGRAMS)
-  set(_test_target "curl-test-client-${_test_name}")
-  add_executable(${_test_target} EXCLUDE_FROM_ALL "${_test_name}.c")
-  add_dependencies(testdeps ${_test_target})
-  target_include_directories(${_test_target} PRIVATE
+foreach(_target IN LISTS check_PROGRAMS)
+  set(_target_name "curl-test-client-${_target}")
+  add_executable(${_target_name} EXCLUDE_FROM_ALL "${_target}.c")
+  add_dependencies(testdeps ${_target_name})
+  target_include_directories(${_target_name} PRIVATE
     "${CURL_BINARY_DIR}/lib"  # for "curl_config.h"
     "${CURL_SOURCE_DIR}/lib"  # for "curl_setup.h"
   )
-  target_link_libraries(${_test_target} ${LIB_SELECTED} ${CURL_LIBS})
-  target_compile_definitions(${_test_target} PRIVATE "CURL_NO_OLDIES")
+  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_LIBS})
+  target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES")
   if(LIB_SELECTED STREQUAL LIB_STATIC AND WIN32)
-    set_property(TARGET ${_test_target} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
+    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
   endif()
   if(MINGW)
     # For mingw-w64 7.0.0 and earlier to avoid '-Wformat='
-    set_property(TARGET ${_test_target} APPEND PROPERTY COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
+    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
   endif()
-  set_target_properties(${_test_target} PROPERTIES
-    OUTPUT_NAME "${_test_name}" UNITY_BUILD OFF
-    PROJECT_LABEL "Test client ${_test_target}")
+  set_target_properties(${_target_name} PROPERTIES
+    OUTPUT_NAME "${_target}" UNITY_BUILD OFF
+    PROJECT_LABEL "Test client ${_target}")
 endforeach()
index 5715bf80556b927db42a19600ae977d1a30402f3..82e124ac78765b3d7eec132412ba83a583648168 100644 (file)
 # SPDX-License-Identifier: curl
 #
 ###########################################################################
-function(setup_test _test_name)  # ARGN are the files in the test
+
+# Get 'noinst_PROGRAMS', '*_SOURCES', WARNLESS, MULTIBYTE, TIMEDIFF variables
+transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
+include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
+
+foreach(_target IN LISTS noinst_PROGRAMS)
+  if(DEFINED ${_target}_SOURCES)
+    set(_sources ${${_target}_SOURCES})
+  else()
+    set(_sources ${nodist_${_target}_SOURCES})
+  endif()
 
   if(LIB_SELECTED STREQUAL LIB_STATIC)
     # These are part of the libcurl static lib. Do not compile/link them again.
-    list(REMOVE_ITEM ARGN ${WARNLESS} ${MULTIBYTE} ${TIMEDIFF})
+    list(REMOVE_ITEM _sources ${WARNLESS} ${MULTIBYTE} ${TIMEDIFF})
   endif()
 
-  add_executable(${_test_name} EXCLUDE_FROM_ALL ${ARGN})
-  add_dependencies(testdeps ${_test_name})
-  string(TOUPPER ${_test_name} _upper_test_name)
-
-  include_directories(
+  string(TOUPPER ${_target} _upper_target)
+  set(_target_name "curl-test-${_target}")
+  add_executable(${_target_name} EXCLUDE_FROM_ALL ${_sources})
+  add_dependencies(testdeps ${_target_name})
+  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_LIBS})
+  target_include_directories(${_target_name} PRIVATE
     "${CURL_BINARY_DIR}/lib"            # for "curl_config.h"
     "${CURL_SOURCE_DIR}/lib"            # for "curl_setup.h"
     "${CURL_SOURCE_DIR}/tests/libtest"  # to be able to build generated tests
   )
-
-  target_link_libraries(${_test_name} ${LIB_SELECTED} ${CURL_LIBS})
-
-  set_target_properties(${_test_name} PROPERTIES
-    COMPILE_DEFINITIONS ${_upper_test_name}
-    PROJECT_LABEL "Test ${_test_name}")
-endfunction()
-
-# Get 'noinst_PROGRAMS', '*_SOURCES', WARNLESS, MULTIBYTE, TIMEDIFF variables
-transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
-include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
-
-foreach(_test_name IN LISTS noinst_PROGRAMS)
-  if(DEFINED ${_test_name}_SOURCES)
-    setup_test(${_test_name} ${${_test_name}_SOURCES})
-  else()
-    setup_test(${_test_name} ${nodist_${_test_name}_SOURCES})
-  endif()
+  set_target_properties(${_target_name} PROPERTIES
+    COMPILE_DEFINITIONS ${_upper_target}
+    OUTPUT_NAME "${_target}"
+    PROJECT_LABEL "Test libtest ${_target}")
 endforeach()
 
 # Allows for hostname override to make tests machine independent.
@@ -62,6 +59,10 @@ endforeach()
 if(NOT WIN32)
   add_library(hostname MODULE EXCLUDE_FROM_ALL "sethostname.c")
   add_dependencies(testdeps hostname)
+  target_include_directories(hostname PRIVATE
+    "${CURL_BINARY_DIR}/lib"  # for "curl_config.h"
+    "${CURL_SOURCE_DIR}/lib"  # for "curl_setup.h"
+  )
   # Output to .libs for compatibility with autotools, the test data expects a
   # library at (tests)/libtest/.libs/libhostname.so
   set_target_properties(hostname PROPERTIES
index fb3482b22cb9d17a5a64c7ebe946644122fec5e7..27164a8a6a34527f641b6a05038ec83b2926b0dd 100644 (file)
 # SPDX-License-Identifier: curl
 #
 ###########################################################################
-function(setup_executable _test_name)  # ARGN are the files in the test
-  add_executable(${_test_name} EXCLUDE_FROM_ALL ${ARGN})
-  add_dependencies(testdeps ${_test_name})
 
-  include_directories(
+# Get 'noinst_PROGRAMS', '<target>_SOURCES' variables
+transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
+include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
+
+foreach(_target IN LISTS noinst_PROGRAMS)
+  set(_target_name "curl-test-server-${_target}")
+  add_executable(${_target_name} EXCLUDE_FROM_ALL ${${_target}_SOURCES})
+  add_dependencies(testdeps ${_target_name})
+  target_include_directories(${_target_name} PRIVATE
     "${CURL_BINARY_DIR}/lib"  # for "curl_config.h"
     "${CURL_SOURCE_DIR}/lib"  # for "curl_setup.h"
     "${CURL_SOURCE_DIR}/src"  # for "tool_xattr.h" in disabled_SOURCES
   )
-
-  target_link_libraries(${_test_name} ${CURL_LIBS})
-
+  target_link_libraries(${_target_name} ${CURL_LIBS})
   # Test servers simply are standalone programs that do not use libcurl
   # library.  For convenience and to ease portability of these servers,
   # some source code files from the libcurl subdirectory are also used
   # to build the servers.  In order to achieve proper linkage of these
   # files on Windows targets it is necessary to build the test servers
   # with CURL_STATICLIB defined, independently of how libcurl is built.
-  set_target_properties(${_test_name} PROPERTIES
-    COMPILE_DEFINITIONS "CURL_STATICLIB"
-    PROJECT_LABEL "Test server ${_test_name}")
-endfunction()
-
-# Get 'noinst_PROGRAMS', '<target>_SOURCES' variables
-transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
-include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
-
-foreach(_executable_name IN LISTS noinst_PROGRAMS)
-  setup_executable(${_executable_name} ${${_executable_name}_SOURCES})
+  if(WIN32)
+    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
+  endif()
+  set_target_properties(${_target_name} PROPERTIES
+    OUTPUT_NAME "${_target}"
+    PROJECT_LABEL "Test server ${_target}")
 endforeach()
index 386d6e92f628a1605d713107150d2a4a655bd314..23148228cd4d28d6b5f70d2dcb2cc6c921599828 100644 (file)
 transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
 include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
 
-include_directories(
-  "${CURL_BINARY_DIR}/lib"            # for "curl_config.h"
-  "${CURL_SOURCE_DIR}/lib"            # for "curl_setup.h"
-  "${CURL_SOURCE_DIR}/src"
-  "${CURL_SOURCE_DIR}/tests/libtest"
-)
-
-foreach(_test_name IN LISTS UNITPROGS)
-  add_executable(${_test_name} EXCLUDE_FROM_ALL "${_test_name}.c" ${UNITFILES})
-  add_dependencies(testdeps ${_test_name})
-  target_link_libraries(${_test_name} curltool curlu)
-  set_target_properties(${_test_name} PROPERTIES
-    PROJECT_LABEL "Test unit ${_test_name}")
+foreach(_target IN LISTS UNITPROGS)
+  set(_target_name "curl-test-${_target}")
+  add_executable(${_target_name} EXCLUDE_FROM_ALL "${_target}.c" ${UNITFILES})
+  add_dependencies(testdeps ${_target_name})
+  target_link_libraries(${_target_name} curltool curlu)
+  target_include_directories(${_target_name} PRIVATE
+    "${CURL_BINARY_DIR}/lib"            # for "curl_config.h"
+    "${CURL_SOURCE_DIR}/lib"            # for "curl_setup.h"
+    "${CURL_SOURCE_DIR}/src"
+    "${CURL_SOURCE_DIR}/tests/libtest"
+  )
+  set_target_properties(${_target_name} PROPERTIES
+    OUTPUT_NAME "${_target}"
+    PROJECT_LABEL "Test unit ${_target}")
 endforeach()