|
|
Line 1: |
Line 1: |
| == How to emulate GNU Autotools 'make check' ==
| | {{CMake/Template/Moved}} |
|
| |
|
| Those of you familiar with the GNU Autotools probably know that the <tt>all</tt> target in a Makefile generated by autoconf/automake does not cause a (re)build of your test programs (assuming you've listed them as <tt>check_PROGRAMS</tt>). This is IMHO a very nice feature, because the normal build is not bogged down by the compilation of many test programs, or worse, comes to a grinding halt due to a compilation error in one of the test programs you didn't have time to fix yet. When the time has come to run your test suite, you simply type <tt>make check</tt> and your test programs will be (re)build and run.
| | This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/EmulateMakeCheck here]. |
| | |
| This feature can be emulated in CMake.
| |
| | |
| ==== Define a custom target ====
| |
| First you need to define a custom target <tt>check</tt>. You only need to do this once, so doing this in your toplevel <tt>CMakeLists.txt</tt> file is probably a good idea.
| |
| | |
| <pre>
| |
| add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
| |
| </pre>
| |
| | |
| eg, you could add set(CMAKE_CTEST_COMMAND ctest -V) to see the printouts from the tests.
| |
| | |
| ==== Add a test program ====
| |
| To add a test program <tt><testprog></tt>, use the following commands
| |
| | |
| <pre>
| |
| add_executable(<testprog> EXCLUDE_FROM_ALL ...)
| |
| add_test(<testprog> <testprog>)
| |
| add_dependencies(check <testprog>)
| |
| </pre>
| |
| | |
| The option <tt>EXCLUDE_FROM_ALL</tt> is essential here; it causes <tt><testprog></tt> to be ignored when the <tt>all</tt> target is built.
| |
| | |
| ===== Note =====
| |
| If you organize all your test programs in one directory, you can use the <tt>EXCLUDE_FROM_ALL</tt> option with the <tt>add_subdirectory</tt> command. That way, you don't need to specify this option with every <tt>add_executable</tt> command.
| |
| | |
| {{CMake/Template/Footer}}
| |
Latest revision as of 15:41, 30 April 2018