Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over files matching wildcard in CMake

Tags:

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 image 255
Manuel Avatar asked Feb 02 '10 10:02

Manuel


2 Answers

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.

like image 70
JesperE Avatar answered Nov 14 '22 19:11

JesperE


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()
like image 33
richq Avatar answered Nov 14 '22 18:11

richq