如何检查CMake中变量是否在父范围中设置(而不是从祖父母那里继承)?
在理想世界中,我想为DEFINED_IN_PARENT_SCOPE
构造使用某种if
谓词,因此在下面的代码中
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()
会产量
Parent has defined MYVAR
Parent has defined MYVAR
Parent has inherited MYVAR from other function up in the call stack
No MYVAR defined
如果我知道如何解决How to write a variable to the parents' parent scope in CMake?,那么我可以解决这个问题。