Looping over files matching wildcard in CMake
I want to use CMake to run some tests. One of the test开发者_运维技巧 should call a validator script on all files matching fixtures/*.ext
. How can transform the following pseudo-CMake into real CMake?
i=0
for file in fixtures/*.ext; do
ADD_TEST(validate_${i}, "validator", $file)
let "i=i+1"
done
Like this:
file(GLOB files "fixtures/*.ext")
foreach(file ${files})
... calculate ${i} to get the test name
add_test(validate_${i}, "validator", ${file})
endforeach()
But this does not calculate i
for you. Is it important that you have an integer suffix to the test? Otherwise you could use file(...)
to extract the filename (without extension) to use.
You need enable_testing
to activate CMake's test machinery. The add_test
function needs the name of the test and the command to run and the syntax is as below.
To add a counter, you can use the math()
function. The following will let you also run out of source builds by specifying the complete path to the inputs.
cmake_minimum_required(VERSION 2.6)
enable_testing()
file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/fixtures/*.ext")
set(validator ${CMAKE_CURRENT_SOURCE_DIR}/validator)
set(i 0)
foreach(filename ${files})
add_test(NAME "validate_${i}"
COMMAND "${validator}" ${filename})
math(EXPR i "${i} + 1")
endforeach()
精彩评论