问题
How to check if the variable is set in the parent scope, (as opposed to being inherited from grandparents) in CMake?
In the ideal world, I would fancy having some sort of DEFINED_IN_PARENT_SCOPE predicate for the if construct, so in the following code
function(child)
if(DEFINED MYVAR) #First check if the variable is defined in any of the parents
if(DEFINED_IN_PARENT_SCOPE MYVAR)
message("Parent has defined MYVAR")
else()
message("Parent has inherited MYVAR from other function up in the call stack")
endif()
else()
message("No MYVAR defined")
endif()
endfunction()
function(parent1)
set(MYVAR 1)
child()
endfunction()
function(parent2)
child()
endfunction()
function(grandparent1A)
set(MYVAR 1)
parent1()
endfunction()
function(grandparent1B)
parent1()
endfunction()
function(grandparent2A)
set(MYVAR 1)
parent2()
endfunction()
function(grandparent2B)
parent2()
endfunction()
grandparent1A()
grandparent1B()
grandparent2A()
grandparent2B()
would yield
Parent has defined MYVAR
Parent has defined MYVAR
Parent has inherited MYVAR from other function up in the call stack
No MYVAR defined
I can walk around this problem if I knew how to solve How to write a variable to the parents' parent scope in CMake? .
来源:https://stackoverflow.com/questions/56295623/how-to-check-if-the-variable-is-set-in-the-parent-scope-as-opposed-to-being-in