dimanche 26 novembre 2017

CMake "RUN_TESTS" or "test" target

In my "plange_superbuild" CMakeLists.txt, I have:

add_test(plange_test ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/prefix/src/plange-build --target test)

which should invoke the "plange" sub-project's test target, and works fine when using the Makefile generator. However, the CMake Testing documentation explains:

[ enable_testing() ] adds another build target, which is test for Makefile generators, or RUN_TESTS for integrated development environments (like Visual Studio).

So, my first snippet doesn't work on builds with an IDE because I'm just saying "test". I've thought of some possible work around options. One is to use an existing CMake variable, should it exist, to get the name of the test target. That is a variable (lets call it CMAKE_TEST_TARGET) that would be equal to test except in IDE builds where it would be equal to RUN_TESTS, allowing the following:

add_test(plange_test ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/prefix/src/plange-build --target ${CMAKE_TEST_TARGET})

I'm not aware of such a variable though. Perhaps I could detect IDE based builds via the CMAKE_GENERATOR variable (or MSVC and similar) and manually set the so-called CMAKE_TEST_TARGET variable. That would require knowing (and maintaining) the exact logic that CMake uses to determine "test" or "RUN_TESTS" in my CMakeLists.txt, which would be unreliable.

My other thought is to invoke ctest directly using a variable (let's call is CTEST_COMMAND) that's similar to CMAKE_COMMAND, but points to the ctest executable. Again, I'm not aware of such a variable. Perhaps this variable could be derived from CMAKE_COMMAND, using get_filename_component() to extract the bin directory. This would allow:

add_test(plange_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/prefix/src/plange-build ${CTEST_COMMAND})

How can I make the first command also work in IDE builds? Are one of these common practice? Is there some superior approach that I haven't considered?

Aucun commentaire:

Enregistrer un commentaire