Skip to content

Commit b5ba203

Browse files
vszakatsagreppin
authored andcommitted
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 libssh2#1180
1 parent f50ec9c commit b5ba203

6 files changed

+21
-22
lines changed

cmake/CheckFunctionExistsMayNeedLibrary.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function(check_function_exists_may_need_library function variable)
6565
check_function_exists(${function} ${variable})
6666

6767
if(NOT ${variable})
68-
foreach(lib ${ARGN})
68+
foreach(lib IN LISTS ARGN)
6969
string(TOUPPER ${lib} UP_LIB)
7070
# Use new variable to prevent cache from previous step shortcircuiting
7171
# new test

cmake/CopyRuntimeDependencies.cmake

+2-5
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ function(ADD_TARGET_TO_COPY_DEPENDENCIES)
5252
# parallel builds trying to kick off the commands at the same time
5353
add_custom_target(${COPY_TARGET})
5454

55-
foreach(target ${COPY_BEFORE_TARGETS})
55+
foreach(target IN LISTS COPY_BEFORE_TARGETS)
5656
add_dependencies(${target} ${COPY_TARGET})
5757
endforeach()
5858

59-
foreach(dependency ${COPY_DEPENDENCIES})
60-
59+
foreach(dependency IN LISTS COPY_DEPENDENCIES)
6160
add_custom_command(
6261
TARGET ${COPY_TARGET}
6362
DEPENDS ${dependency}
@@ -68,7 +67,5 @@ function(ADD_TARGET_TO_COPY_DEPENDENCIES)
6867
COMMAND ${CMAKE_COMMAND}
6968
ARGS -E copy ${dependency} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
7069
VERBATIM)
71-
7270
endforeach()
73-
7471
endfunction()

cmake/max_warnings.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
189189

190190
unset(WPICKY)
191191

192-
foreach(_CCOPT ${WPICKY_ENABLE})
192+
foreach(_CCOPT IN LISTS WPICKY_ENABLE)
193193
set(WPICKY "${WPICKY} ${_CCOPT}")
194194
endforeach()
195195

196-
foreach(_CCOPT ${WPICKY_DETECT})
196+
foreach(_CCOPT IN LISTS WPICKY_DETECT)
197197
# surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
198198
# test result in.
199199
string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)

example/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ transform_makefile_inc("Makefile.am" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cm
4545
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cmake")
4646
set(EXAMPLES ${noinst_PROGRAMS})
4747

48-
foreach(example ${EXAMPLES})
48+
foreach(example IN LISTS EXAMPLES)
4949
add_executable(${example} "${example}.c")
5050
list(APPEND EXAMPLE_TARGETS ${example})
5151
# to find generated header

tests/CMakeLists.txt

+14-12
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ add_library(runner STATIC ${librunner_la_SOURCES})
7474
target_compile_definitions(runner PRIVATE "${CRYPTO_BACKEND_DEFINE}")
7575
target_include_directories(runner PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" "../src" "../include" "${CRYPTO_BACKEND_INCLUDE_DIR}")
7676

77-
foreach(test ${DOCKER_TESTS} ${STANDALONE_TESTS} ${SSHD_TESTS})
77+
foreach(test IN LISTS DOCKER_TESTS STANDALONE_TESTS SSHD_TESTS)
7878
if(NOT ";${DOCKER_TESTS_STATIC};${STANDALONE_TESTS_STATIC};" MATCHES ";${test};")
7979
set(LIB_FOR_TESTS ${LIB_SELECTED})
8080
elseif(TARGET ${LIB_STATIC})
@@ -107,15 +107,15 @@ endforeach()
107107
option(RUN_DOCKER_TESTS "Run tests requiring Docker" ON)
108108

109109
if(RUN_DOCKER_TESTS)
110-
foreach(test ${DOCKER_TESTS})
110+
foreach(test IN LISTS DOCKER_TESTS)
111111
if(TARGET ${test})
112112
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>")
113113
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
114114
endif()
115115
endforeach()
116116
endif()
117117

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

125125
if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE)
126126
unset(sshd_test_targets)
127-
foreach(test ${SSHD_TESTS})
127+
foreach(test IN LISTS SSHD_TESTS)
128128
if(TARGET ${test})
129129
set(sshd_test_targets "${sshd_test_targets} $<TARGET_FILE:${test}>")
130130
endif()
@@ -141,14 +141,16 @@ if(RUN_DOCKER_TESTS)
141141
# CRYPT/MAC algo tests
142142
file(READ "test_read_algos.txt" ALGO_TESTS)
143143
string(REGEX REPLACE "\\\n" ";" ALGO_TESTS ${ALGO_TESTS})
144-
foreach(test ${ALGO_TESTS})
145-
set(testname "test_read-${test}")
146-
add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>")
147-
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
148-
if(test MATCHES "mac-")
149-
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_MAC=${test}")
150-
else()
151-
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}")
144+
foreach(test IN LISTS ALGO_TESTS)
145+
if(test)
146+
set(testname "test_read-${test}")
147+
add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>")
148+
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
149+
if(test MATCHES "mac-")
150+
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_MAC=${test}")
151+
else()
152+
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}")
153+
endif()
152154
endif()
153155
endforeach()
154156
endif()

tests/cmake/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if(TEST_INTEGRATION_MODE STREQUAL "find_package" OR
2020
DOWNLOAD_EXTRACT_TIMESTAMP ON)
2121
endif()
2222
find_package(libssh2 REQUIRED CONFIG)
23-
foreach(result_var libssh2_FOUND libssh2_VERSION)
23+
foreach(result_var IN ITEMS libssh2_FOUND libssh2_VERSION)
2424
if(${result_var})
2525
message(STATUS "${result_var}: |${${result_var}}|")
2626
else()

0 commit comments

Comments
 (0)