How to check if the variable is set in the parent scope, (as opposed to being inherited from grandparents) in CMake?

倖福魔咒の 提交于 2019-12-11 14:53:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!