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

Mario Werner mario.werner at iaik.tugraz.at
Tue Nov 15 07:49:16 EST 2016


Hi Brad,

thank you very much for the clarification and the background information!

I definitely have to scrutinize our CMake code to make sure that no
additional problems are lurking in our build system. For now, I am going
to file a feature request to keep track of the idea of properly scoping
function and macro definitions. As soon as I have more time on my hands
I may come back to it and give it try.

Cheers,
Mario

On 2016-11-14 19:47, Brad King wrote:
> On 11/14/2016 10:29 AM, Mario Werner wrote:
>> 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.
> 
> That would be nice behavior.
> 
>> 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.
> 
> Commands (builtin+functions+macros) are currently scoped globally.
> 
>> Now I am wondering if this behaviour is actually intended or if it is a
>> corner case which just never surfaced before.
> 
> It just happened to be implemented that way from the beginning back in
> our wild west days without much thought to scoping.  It is indeed not
> documented AFAIK, but a good place to add this to the docs might be here:
> 
>  https://cmake.org/cmake/help/v3.7/manual/cmake-language.7.html#command-definitions
> 
> It is also something that could be changed by the introduction of a
> new CMake Policy, though it would be a bit of work to get right.
> 
> -Brad
> 




More information about the CMake mailing list