1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-08-01 11:26:53 +03:00

cmake: tidy-up foreach() syntax

Use `IN LISTS` and `IN ITEMS`. This appears to be the preferred way
within CMake's own source code and possibly improves readability.

Fixup a side-effect of `IN LISTS`, where it retains empty values at
the end of the list, as opposed to the syntax used before, which
dropped it. In our case this happened with lines read from a text
file via `file(READ)`.

https://cmake.org/cmake/help/v3.7/command/foreach.html

Closes #1180
This commit is contained in:
Viktor Szakats
2023-08-26 09:48:34 +00:00
parent 5754fed686
commit 4a64ca1430
6 changed files with 21 additions and 22 deletions

View File

@ -65,7 +65,7 @@ function(check_function_exists_may_need_library function variable)
check_function_exists(${function} ${variable}) check_function_exists(${function} ${variable})
if(NOT ${variable}) if(NOT ${variable})
foreach(lib ${ARGN}) foreach(lib IN LISTS ARGN)
string(TOUPPER ${lib} UP_LIB) string(TOUPPER ${lib} UP_LIB)
# Use new variable to prevent cache from previous step shortcircuiting # Use new variable to prevent cache from previous step shortcircuiting
# new test # new test

View File

@ -52,12 +52,11 @@ function(ADD_TARGET_TO_COPY_DEPENDENCIES)
# parallel builds trying to kick off the commands at the same time # parallel builds trying to kick off the commands at the same time
add_custom_target(${COPY_TARGET}) add_custom_target(${COPY_TARGET})
foreach(target ${COPY_BEFORE_TARGETS}) foreach(target IN LISTS COPY_BEFORE_TARGETS)
add_dependencies(${target} ${COPY_TARGET}) add_dependencies(${target} ${COPY_TARGET})
endforeach() endforeach()
foreach(dependency ${COPY_DEPENDENCIES}) foreach(dependency IN LISTS COPY_DEPENDENCIES)
add_custom_command( add_custom_command(
TARGET ${COPY_TARGET} TARGET ${COPY_TARGET}
DEPENDS ${dependency} DEPENDS ${dependency}
@ -68,7 +67,5 @@ function(ADD_TARGET_TO_COPY_DEPENDENCIES)
COMMAND ${CMAKE_COMMAND} COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${dependency} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR} ARGS -E copy ${dependency} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
VERBATIM) VERBATIM)
endforeach() endforeach()
endfunction() endfunction()

View File

@ -189,11 +189,11 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
unset(WPICKY) unset(WPICKY)
foreach(_CCOPT ${WPICKY_ENABLE}) foreach(_CCOPT IN LISTS WPICKY_ENABLE)
set(WPICKY "${WPICKY} ${_CCOPT}") set(WPICKY "${WPICKY} ${_CCOPT}")
endforeach() endforeach()
foreach(_CCOPT ${WPICKY_DETECT}) foreach(_CCOPT IN LISTS WPICKY_DETECT)
# surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
# test result in. # test result in.
string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname) string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)

View File

@ -45,7 +45,7 @@ transform_makefile_inc("Makefile.am" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cm
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cmake")
set(EXAMPLES ${noinst_PROGRAMS}) set(EXAMPLES ${noinst_PROGRAMS})
foreach(example ${EXAMPLES}) foreach(example IN LISTS EXAMPLES)
add_executable(${example} "${example}.c") add_executable(${example} "${example}.c")
list(APPEND EXAMPLE_TARGETS ${example}) list(APPEND EXAMPLE_TARGETS ${example})
# to find generated header # to find generated header

View File

@ -74,7 +74,7 @@ add_library(runner STATIC ${librunner_la_SOURCES})
target_compile_definitions(runner PRIVATE "${CRYPTO_BACKEND_DEFINE}") target_compile_definitions(runner PRIVATE "${CRYPTO_BACKEND_DEFINE}")
target_include_directories(runner PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" "../src" "../include" "${CRYPTO_BACKEND_INCLUDE_DIR}") target_include_directories(runner PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" "../src" "../include" "${CRYPTO_BACKEND_INCLUDE_DIR}")
foreach(test ${DOCKER_TESTS} ${STANDALONE_TESTS} ${SSHD_TESTS}) foreach(test IN LISTS DOCKER_TESTS STANDALONE_TESTS SSHD_TESTS)
if(NOT ";${DOCKER_TESTS_STATIC};${STANDALONE_TESTS_STATIC};" MATCHES ";${test};") if(NOT ";${DOCKER_TESTS_STATIC};${STANDALONE_TESTS_STATIC};" MATCHES ";${test};")
set(LIB_FOR_TESTS ${LIB_SELECTED}) set(LIB_FOR_TESTS ${LIB_SELECTED})
elseif(TARGET ${LIB_STATIC}) elseif(TARGET ${LIB_STATIC})
@ -107,7 +107,7 @@ endforeach()
option(RUN_DOCKER_TESTS "Run tests requiring Docker" ON) option(RUN_DOCKER_TESTS "Run tests requiring Docker" ON)
if(RUN_DOCKER_TESTS) if(RUN_DOCKER_TESTS)
foreach(test ${DOCKER_TESTS}) foreach(test IN LISTS DOCKER_TESTS)
if(TARGET ${test}) if(TARGET ${test})
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>") add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>")
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
@ -115,7 +115,7 @@ if(RUN_DOCKER_TESTS)
endforeach() endforeach()
endif() endif()
foreach(test ${STANDALONE_TESTS}) foreach(test IN LISTS STANDALONE_TESTS)
if(TARGET ${test}) if(TARGET ${test})
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>") add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>")
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
@ -124,7 +124,7 @@ endforeach()
if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE) if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE)
unset(sshd_test_targets) unset(sshd_test_targets)
foreach(test ${SSHD_TESTS}) foreach(test IN LISTS SSHD_TESTS)
if(TARGET ${test}) if(TARGET ${test})
set(sshd_test_targets "${sshd_test_targets} $<TARGET_FILE:${test}>") set(sshd_test_targets "${sshd_test_targets} $<TARGET_FILE:${test}>")
endif() endif()
@ -141,7 +141,8 @@ if(RUN_DOCKER_TESTS)
# CRYPT/MAC algo tests # CRYPT/MAC algo tests
file(READ "test_read_algos.txt" ALGO_TESTS) file(READ "test_read_algos.txt" ALGO_TESTS)
string(REGEX REPLACE "\\\n" ";" ALGO_TESTS ${ALGO_TESTS}) string(REGEX REPLACE "\\\n" ";" ALGO_TESTS ${ALGO_TESTS})
foreach(test ${ALGO_TESTS}) foreach(test IN LISTS ALGO_TESTS)
if(test)
set(testname "test_read-${test}") set(testname "test_read-${test}")
add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>") add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
@ -150,6 +151,7 @@ if(RUN_DOCKER_TESTS)
else() else()
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}") set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}")
endif() endif()
endif()
endforeach() endforeach()
endif() endif()

View File

@ -20,7 +20,7 @@ if(TEST_INTEGRATION_MODE STREQUAL "find_package" OR
DOWNLOAD_EXTRACT_TIMESTAMP ON) DOWNLOAD_EXTRACT_TIMESTAMP ON)
endif() endif()
find_package(libssh2 REQUIRED CONFIG) find_package(libssh2 REQUIRED CONFIG)
foreach(result_var libssh2_FOUND libssh2_VERSION) foreach(result_var IN ITEMS libssh2_FOUND libssh2_VERSION)
if(${result_var}) if(${result_var})
message(STATUS "${result_var}: |${${result_var}}|") message(STATUS "${result_var}: |${${result_var}}|")
else() else()