Skip to content

Commit

Permalink
cmake: tidy-up foreach() syntax
Browse files Browse the repository at this point in the history
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 libssh2#1180
  • Loading branch information
vszakats authored and agreppin committed Jul 14, 2024
1 parent f50ec9c commit b5ba203
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmake/CheckFunctionExistsMayNeedLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function(check_function_exists_may_need_library function variable)
check_function_exists(${function} ${variable})

if(NOT ${variable})
foreach(lib ${ARGN})
foreach(lib IN LISTS ARGN)
string(TOUPPER ${lib} UP_LIB)
# Use new variable to prevent cache from previous step shortcircuiting
# new test
Expand Down
7 changes: 2 additions & 5 deletions cmake/CopyRuntimeDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ function(ADD_TARGET_TO_COPY_DEPENDENCIES)
# parallel builds trying to kick off the commands at the same time
add_custom_target(${COPY_TARGET})

foreach(target ${COPY_BEFORE_TARGETS})
foreach(target IN LISTS COPY_BEFORE_TARGETS)
add_dependencies(${target} ${COPY_TARGET})
endforeach()

foreach(dependency ${COPY_DEPENDENCIES})

foreach(dependency IN LISTS COPY_DEPENDENCIES)
add_custom_command(
TARGET ${COPY_TARGET}
DEPENDS ${dependency}
Expand All @@ -68,7 +67,5 @@ function(ADD_TARGET_TO_COPY_DEPENDENCIES)
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${dependency} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
VERBATIM)

endforeach()

endfunction()
4 changes: 2 additions & 2 deletions cmake/max_warnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I

unset(WPICKY)

foreach(_CCOPT ${WPICKY_ENABLE})
foreach(_CCOPT IN LISTS WPICKY_ENABLE)
set(WPICKY "${WPICKY} ${_CCOPT}")
endforeach()

foreach(_CCOPT ${WPICKY_DETECT})
foreach(_CCOPT IN LISTS WPICKY_DETECT)
# surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
# test result in.
string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ transform_makefile_inc("Makefile.am" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cm
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cmake")
set(EXAMPLES ${noinst_PROGRAMS})

foreach(example ${EXAMPLES})
foreach(example IN LISTS EXAMPLES)
add_executable(${example} "${example}.c")
list(APPEND EXAMPLE_TARGETS ${example})
# to find generated header
Expand Down
26 changes: 14 additions & 12 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ add_library(runner STATIC ${librunner_la_SOURCES})
target_compile_definitions(runner PRIVATE "${CRYPTO_BACKEND_DEFINE}")
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};")
set(LIB_FOR_TESTS ${LIB_SELECTED})
elseif(TARGET ${LIB_STATIC})
Expand Down Expand Up @@ -107,15 +107,15 @@ endforeach()
option(RUN_DOCKER_TESTS "Run tests requiring Docker" ON)

if(RUN_DOCKER_TESTS)
foreach(test ${DOCKER_TESTS})
foreach(test IN LISTS DOCKER_TESTS)
if(TARGET ${test})
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>")
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endif()
endforeach()
endif()

foreach(test ${STANDALONE_TESTS})
foreach(test IN LISTS STANDALONE_TESTS)
if(TARGET ${test})
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>")
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
Expand All @@ -124,7 +124,7 @@ endforeach()

if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE)
unset(sshd_test_targets)
foreach(test ${SSHD_TESTS})
foreach(test IN LISTS SSHD_TESTS)
if(TARGET ${test})
set(sshd_test_targets "${sshd_test_targets} $<TARGET_FILE:${test}>")
endif()
Expand All @@ -141,14 +141,16 @@ if(RUN_DOCKER_TESTS)
# CRYPT/MAC algo tests
file(READ "test_read_algos.txt" ALGO_TESTS)
string(REGEX REPLACE "\\\n" ";" ALGO_TESTS ${ALGO_TESTS})
foreach(test ${ALGO_TESTS})
set(testname "test_read-${test}")
add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
if(test MATCHES "mac-")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_MAC=${test}")
else()
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}")
foreach(test IN LISTS ALGO_TESTS)
if(test)
set(testname "test_read-${test}")
add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
if(test MATCHES "mac-")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_MAC=${test}")
else()
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}")
endif()
endif()
endforeach()
endif()
Expand Down
2 changes: 1 addition & 1 deletion tests/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if(TEST_INTEGRATION_MODE STREQUAL "find_package" OR
DOWNLOAD_EXTRACT_TIMESTAMP ON)
endif()
find_package(libssh2 REQUIRED CONFIG)
foreach(result_var libssh2_FOUND libssh2_VERSION)
foreach(result_var IN ITEMS libssh2_FOUND libssh2_VERSION)
if(${result_var})
message(STATUS "${result_var}: |${${result_var}}|")
else()
Expand Down

0 comments on commit b5ba203

Please sign in to comment.