Search code examples
unit-testingcmakectest

pass ARGN to custom script in add_test() of CMake


i want to pass ARGN to a custom run_test.cmake in unit tests. What i do in CMake script is:

# 1:
message("ARGN: ${ARGN}")
add_test(NAME ${_category}/${_test_name}_mpi${_nmpi}
 WORKING_DIRECTORY "${_target_dir}"
 COMMAND "${CMAKE_COMMAND}"
 -DNMPI=${_nmpi}
 -DTEST_PROG=${_target_dir}/${_target_exec}
 -DTEST_ARGN=${ARGN}
 -Doutput_blessed=${CMAKE_SOURCE_DIR}/unit_tests/${_category}/${_test_name}.output
 -Doutput_test=${_target_dir}/output
 -P ${CMAKE_SOURCE_DIR}/unit_tests/run_test.cmake
 )  

where

  # 2 (part of run_test.cmake):
  message("TEST_ARGN: ${TEST_ARGN}")
  # run the executable
  execute_process(COMMAND mpirun -np ${NMPI} ${TEST_PROG} ${TEST_ARGN}
            RESULT_VARIABLE HAD_ERROR
            OUTPUT_VARIABLE output
            ERROR_VARIABLE output)

the problem is that (1) gets all the arguments, during configure message prints ARGN: ARG1;ARG2;ARG3, whereas (2) gets only the first one, during run-time message prints TEST_ARGN: ARG1.

It is definitely my lack of understanding Cmake, but i would love to know what am I doing wrong.


Solution

  • As ${ARGN} contains ';' character(which delimits element in the list), you should put all usages of it into ":

    "-DTEST_ARGN=${ARGN}"
    

    Otherwise CMake interpret it as several parameters to the command:

    -DTEST_ARGN=ARG1 ARG2 ARG3