I asked a questions about cmake and passing variables here. I can make it work, but only if I name my variable in the function differently than the variable in parent scope I call the function for. So in essence:
function(strange name)
message(STATUS ${name})
message(STATUS ${${name}})
endfunction()
set(name foo)
set(anothername foo)
strange(name)
strange(anothername)
Which results in:
-- name (message(STATUS ${name}) for var "name")
-- name (message(STATUS ${${name}}) for var "name")
-- anothername message(STATUS ${name}) for var "anothername")
-- foo (message(STATUS ${${name}}) for var "anothername")
Isn’t that a little weird? What’s happening?
I think the behaviour of a function should not depend on the naming of variable in the parent scope – should it?!
Any clarification is much appreciated!
Unfortunately, the language of CMake is extremely primitive. In this case, the naming of local variables may interact with variables with the same name in outer scopes.
Concretely, in the two calls
${${name}}expands to${name}and${anothername}, respectively. In the formernameis the name of a local variable whose value is used. In the latter, theanothernamefrom the outer scope is used.I don’t know of any way around this, except using obfuscated variable names in function accept variable names as arguments.
Maybe we should petition for a
${name:PARENT_SCOPE}from the CMake team?