I have the following (simplified) directory structure:
|-- doc/
|-- subdir1/
|-- subdir2/
|-- CMakeLists.txt
|-- src
|-- subdir3/
|-- subdir4/
|-- CMakeLists.txt
|-- test
|-- subdir5/
|-- subdir6/
|-- CMakeLists.txt
|-- CMakeLists.txt
The top level CMakeLists.txt
contains something like:
add_subdirectory(doc)
add_subdirectory(src)
add_subdirectory(test)
ONLY inside the src/CMakeLists.txt
I want to use my own version of CMake command
add_subdirectory(), so I use a CMake macro() to "overwrite" the command like this:
##src/CMakeLists.txt
macro(add_subdirectory)
message(STATUS "My own 'add_subdirectory()'")
_add_subdirectory(${ARGV})
endmacro(add_subdirectory)
# Call specialized version of 'add_subdirectory()'
add_subdirectory(subdir3)
add_subdirectory(subdir4)
The top level CMakeLists.txt
contains something like:
add_subdirectory(doc) #Uses CMake standard command 'add_subdirectory()'
add_subdirectory(src) #Uses overwritten 'add_subdirectory()'
add_subdirectory(test) #Should use CMake standard command 'add_subdirectory()'
For directory 'doc' and 'src' the behaviour is as expected. But inside the 'test' directory the
overwritten add_subdirectory()
still applies, which is not what I expected. I was under the
impression that due to scoping rules the default behavior of add_subdirectory()
would be used.
Trying to restore the default behavior by adding
macro(add_subdirectory)
_add_subdirectory(${ARGV})
endmacro(add_subdirectory)
at the end of 'src/CMakeLists.txt' results in a segmentation fault when runinng CMake, which I
assume is the result of an infinite recursive call. Something similar was suggested here:
https://cmake.org/pipermail/cmake/2012-April/050023.html
Using a CMake function() instead of a macro()
seems to result in the same behavior.
So my question is: Is it possible to restore the default behavior of a CMake command after it has
been overwritten?
I'm using CMake version 3.5.2.