I've found another (better) way to solve the actual question.
TL;DR
Add to your CMakeLists.txt file:
configure_file(${CMAKE_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_BINARY_DIR} @ONLY)
Add do your CTestCustom.cmake
file in your source code:
file (STRINGS "@CMAKE_BINARY_DIR@/CTestTestfile.cmake" LINES)
# overwrite the file....
file(WRITE "@CMAKE_BINARY_DIR@/CTestTestfile.cmake" "")
# loop through the lines,
foreach(LINE IN LISTS LINES)
# remove unwanted parts
string(REGEX REPLACE ".*directory_to_remove.*" "" STRIPPED "${LINE}")
# and write the (changed) line ...
file(APPEND "@CMAKE_BINARY_DIR@/CTestTestfile.cmake" "${STRIPPED}\n")
endforeach()
(See https://mcmap.net/q/745520/-how-to-remove-a-line-of-text-in-a-string-in-cmake-working-around-cmake-39-s-lack-of-line-based-regex-matching for a function form of removing a line)
Explanation:
The excluding per test method has many downsides, including dynamically (via a macro) added tests, wasted time, etc...
This was tested on CMake 3.x: When a CMake project with tests enabled is built, a CTestTestfile.cmake
file is generate in the CMAKE_BINARY_DIR
. Essentially every time add_subdirectory
is called, an equivalent subdirs
command is added to the CTestTestfile.cmake
file. The CTestTestfile.cmake
files will mimic an equivalent directory structure as the CMakeLists.txt
files. (So if you need to remove a sub directory in another directory, that's the CTestTestfile.cmake
you need to edit).
The CTestCustom.cmake
is executed before the CTestTestfile.cmake
, so all CTestCustom.cmake
needs to do is remove the offending line from the CTestTestfile.cmake
file. This could be done with a sed
, but in the spirit of cross compatibility, it's all pure CMake now. The CTestCustom.cmake
does not have CMAKE_BINARY_DIR
set, so we use @
substitution to get the value of CMAKE_BINARY_DIR
at CMake generate time, and replace the values that will be in the CTestCustom.cmake
copy in the build directory, using @ONLY
replacement, to leave the other variables $
alone.