Allow functions to also have dynamic scope
At the moment, functions themselves are always defined within the global scope.
Since no warning is given when overriding an existing function name, in big projects with superbuilds spanning across hundreds of folders, it is possible that the same function name is defined more than once (e.g. "function(build_tests)"), if it is common enough. E.g.:
# in folder A
include(B.cmake)
add_subdirectory(C)
f() # prints C
# module B.cmake
function(f)
message(STATUS "B")
endfunction(f)
# in C/CMakeLists.txt
function(f)
message(STATUS "C")
endfunction(f)
In case the signature is compatible, there will be no error. Additionally, it might not be possible to change some of the projects to use a new function name.
This is potentially disruptive. We have a huge codebase with hundreds of CMake files, and fixing them all / keeping track of the order they are loaded at runtime (which might change) is really hard. It is also hard to stop one team working on some component to define a function used by another team in another component.
Therefore, it would be really beneficial if functions could use the same dynamic scope as variables, or at least be defined on the top of a stack that is popped when going out of a subdirectory.
This would require a new CMake policy, of course.