[CMake] Are the current scoping rules for functions intended and documented?

Mario Werner mario.werner at iaik.tugraz.at
Mon Nov 14 10:29:45 EST 2016


Hi,

I was just surprised by the scoping rules of CMake when it comes to
function definitions and sub scopes (i.e., subdirectories). I expected
that function definitions adhere to the same scoping rules as variables.
More concretely, I assumed that function definitions in a sub scope are
only valid in the same (or deeper) scope(s) but do not influence
definitions in potential parent scopes.

However, as it turns out, (at least CMake 3.5.1) this is not the case.
CMake seems to use the last definition of a function it encounters,
independent of current scope.

I uploaded a quick minimal working example which shows what I mean under
[1]. I just discovered this behaviour by hunting down a bug in our build
system which was caused by the fact that a function definition in a sub
project overwrote an identically named function in the parent project.

Now I am wondering if this behaviour is actually intended or if it is a
corner case which just never surfaced before. The documentation on
functions [2] unfortunately also omits how this is supposed to behave.

Any pointers would be appreciated.

Regards,
Mario

[1] https://github.com/niosHD/mwe-cmake-function-scopes
[2] https://cmake.org/cmake/help/v3.7/command/function.html



More information about the CMake mailing list