[CMake] Pass an array type argument to a macro

Laszlo Papp lpapp at kde.org
Tue Jul 19 04:57:55 EDT 2011


Hi,

Is there a way of passing array arguments to a macro with cmake ?

I would like to write a generic test generation macro, like this:
MACRO(UNIT_TESTS modulename libraries)
      FOREACH(testname ${ARGN})
              qt4_automoc(${testname}.cpp)
              add_executable("${modulename}-${testname}" ${testname}.cpp)
              target_link_libraries("${modulename}-${testname}"
${libraries} ${QT_QTTEST_LIBRARY})
              add_test("${modulename}-${testname}" ${modulename}-${testname})
              if(WINCE)
               target_link_libraries("${modulename}-${testname}"
${WCECOMPAT_LIBRARIES})
              endif(WINCE)
      ENDFOREACH(testname)
ENDMACRO(UNIT_TESTS)

The problem is that when I would like to use the aforementioned macro
like this:
UNIT_TESTS(
   ${CORE_LIBS}# libraries argument
   core # modulename argument
   objecttest
)

The "libraries" argument contains only the first part of the
"${CORE_LIBS}". For instance there is this inside the ${CORE_LIBS}:
/usr/lib/libQtCore.so;/usr/lib/libQtGui.so;/usr/lib/libQtScript.so
The "libraries" will contain: /usr/lib/libQtCore.so
The problem is that if I would like to use such a test generation
macro from each submodule of a really huge project, I would duplicate,
triplicate and so forth this macro in each submodule instead of having
it nicely just inside the main CMakeLists.txt or at least inside some
core CMakeLists.txt. Continous copy paste of this macro with hard
coded submodule specific values do not seem to be a good solution
or/and approach.

The workaround can be that I determine an internal variable that can
be filled out with the proper content and use that internally inside
the macro, but I do not consider that nice solution. The best would be
if we can somehow pass array type arguments to the cmake macro. Is
that possible ? Does it already somehow work like that ?

Thank you in advance!

Best Regards,
Laszlo Papp


More information about the CMake mailing list