如何检测当前范围在 CMake 中是否有父级? [英] How to detect if current scope has a parent in CMake?

查看:15
本文介绍了如何检测当前范围在 CMake 中是否有父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测当前作用域是否有父作用域?

Is there any way to detect if the current scope has a parent?

我有一个项目,它可以是独立项目,也可以是另一个项目的子项目.为了允许子项目案例,我使用 PARENT_SCOPE 标志来 set() 将事情推送给父项.但是,当作为独立项目构建时,我收到当前范围没有父级"警告.我想通过检测是否有父项并将 set() 调用包含在 if 语句中来避免该错误.还是只有在有父级时才可以在父级范围内设置变量?

I have a project that can either be a standalone project or a sub-project of another. To allow the sub project case, I use the PARENT_SCOPE flag to set() to push things up to the parent. However, when build as a standalone project I get a "current scope has no parent" warning. I would like to avoid that error by detecting if there is a parent and enclosing the set() calls in an if statement. Or is there another way to set a variable at parent scope only if there is a parent?

推荐答案

我认为最可靠的方法是使用 PARENT_DIRECTORY 目录属性.

I think the most robust approach is to use the PARENT_DIRECTORY directory property.

无论是在 project 命令之前还是之后调用它,无论父级和子级是否具有相同的项目名称,这都会产生正确的答案.

This will yield the correct answer regardless of whether it's called before or after the project command, and regardless of whether the parent and child both have the same project name.

get_directory_property(hasParent PARENT_DIRECTORY)
if(hasParent)
  message(STATUS "Has a parent scope.")
else()
  message(STATUS "Doesn't have a parent scope.")
endif()

这篇关于如何检测当前范围在 CMake 中是否有父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆