return(PROPAGATE) leaks variables when add_subdirectory() is called inside a block()
Consider the following minimal project:
CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(example)
set(topvar "Initial value")
# top block
block()
add_subdirectory(subber)
message("inside top block: topvar = ${topvar}")
endblock()
message("outside top block: topvar = ${topvar}")
subber/CMakeLists.txt
# sub block
block()
set(topvar "from subber")
return(PROPAGATE topvar)
endblock()
The output from the above looks like this:
inside top block: topvar = from subber
outside top block: topvar = from subber
I expected topvar
to still have the value Initial value
at the end of the top-most CMakeLists.txt
file (after the top block), not from subber
. The more intuitive behavior would be for PROPAGATE
to propagate the variable to the scope in which the subdirectory was added from, which would be inside the top block.
The documented behavior for return()
is inaccurate for the above situation. It says the variable will propagate to the "parent directory or function caller scope". There's no function call in this case, so it would have to be the parent directory scope. That is either inside or outside the top block, depending on how you interpret that statement, but it shouldn't be both.