cmake: sync code between test/example targets

- 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
This commit is contained in:
Viktor Szakats 2024-08-22 09:10:07 +02:00
parent f73f6bf9f8
commit a2ef5d36b3
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
5 changed files with 80 additions and 79 deletions

View file

@ -21,33 +21,31 @@
# 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()