2

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.

PiJ
  • 151
  • 1
  • 12

2 Answers2

3

You can activate it via an additional variable defined outside the macro's scope:

set(MY_ADD_SUBDIRECTORY TRUE)

macro(add_subdirectory)
    if (MY_ADD_SUBDIRECTORY)
        message(STATUS "My own 'add_subdirectory()'")       
    endif()
    _add_subdirectory(${ARGV})
endmacro(add_subdirectory)

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149
1

A simple solution would be to name your maceo something else, like my_add_subdirectory and use that where you need the special behaviour.

That way you don't have to bother with trying to "restore" the overwritten add_command (if that is even possible) and things should just work.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70