You are not logged in.

#1 2011-03-09 15:00:57

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

CMake: functions, macro and variables scope

Hi, I wrote a CMake function since I need local scope for some variables. But this function has to call a macro which sets some other global variables: calling the macro inside the function has the result that the macro sets the variables in the function scope, not in the global one...
Is it possible to specify the scope to create the variables? I tried PARENT_SCOPE, but this does not work since the function may be called recursively so the PARENT_SCOPE may be the n-1 recursive step and not the global scope. I would need something like this inside my macro:

set(varName value GLOBAL_SCOPE)

which sadly does not exist. Does anyone know a neat trick to deal with this?
Thanks

Offline

#2 2011-03-09 20:28:40

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: CMake: functions, macro and variables scope

Does anyone know a neat trick to deal with this?

Store it in cache this way:

set(varName value INTERNAL CACHE STRING "" FORCE)

The INTERNAL prevent the variable to be visible by the user, the FORCE prevents the use to change
the variable.

See cmake doc for more information.

The drawback is of course that may have to delete your cache ....

Just out of curiosity, why do you need recursive cmake calls for?

Offline

#3 2011-03-10 09:17:15

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: CMake: functions, macro and variables scope

Thanks Yannick, I already found a way to fix the things but I'll give a try to your solution which seems more polite than my dirty trick smile
For your curiosity, my function searches for packages upon which a project depends. These packages might depend on other packages as well, so my package-search function should recursivley call itself until it reaches the leaf packages. Each recursion level sets in its parent scope a variable with the number of found packages at that recursion level, so that I can check if all the packages have been found: this is why I need local scope (generally, local scope is essential when using recursive functions).
The problem is that the Find<package>.cmake called by my function to find packages also set variables to configure the project build, so I don't want these to be limited to the function scope but rather be global. I'm dealing with a third-party software so I have to live with this desired behavior, and this is the origin of my headaches... smile

Last edited by snack (2011-03-10 09:20:08)

Offline

Board footer

Powered by FluxBB