When executing cmake
to generate ctest
inputs, how can I directly specify the destination directory for the cmake
-generated CTestTestfile.cmake
? Specifically, my test suite is an independent cmake project. I currently add it to the main cmake
project with include()
, but the more appropriate method is to use add_subdirectory()
. However, add_subdirectory()
installs CTestTestfile.cmake
in the subproject's directory in the build
directory, but I need it in build/
for ctest
to find it.
I set up the test macros in a file CTestList.cmake
which lives in the test_project. This file is included in the build in test_project/CMakeLists.txt
:
include(CTest)
include(${CMAKE_CURRENT_LIST_DIR}/CTestList.cmake)
With include()
:
project/
|-- CMakeLists.txt
|-- src/
|-- test_project/
`-- build/
|-- CTestTestfile.cmake
`-- test_project/
With add_subdirectory()
:
project/
|-- CMakeLists.txt
|-- src/
|-- test_project/
`-- build/
`-- test_project/
`-- CTestTestfile.cmake
The include
and the add_subdirectory
commands of CMake
are very different.
While the include
command works like #include
of C and C++ – loads the content of included file into the file where it appears –, the add_subdirectory
adds a subdirectory to the build. This means that when you use the add_subdirectory
the variables inside the subdirectory's CMakeLists.txt
will be the part of a different scope and the value of CMAKE_CURRENT_BINARY_DIR
variable will be different too. Just add the following line to the test_project/CMakeLists.txt
and try either to include
it or add it by add_subdirectory
:
message(STATUS "The files will be generated into: " ${CMAKE_CURRENT_BINARY_DIR})
The enable_testing
enables testing for current directory and below. So, when you used the add_subdirecory
instead of include
the current directory meant build/test_project/
– as explained above.
To make sure the CTestTestfile.cmake
will be created in build
directory too, simply add an extra enable_testing
command to the top-level CMakeLists.txt
.