如何在CMAKE中获取父级项目的名称?
请参阅以下最小示例:
├───CMakeLists.txt
├───bar
│ ├───CMakeLists.txt
cmakelists.txt
cmake_minimum_required(VERSION 3.20)
project(foo)
add_subdirectory(bar)
bar/cmakelists.txt
project(bar)
cmake_path(GET CMAKE_CURRENT_LIST_DIR PARENT_PATH BAR_PARENT_DIR)
# how can I get `foo` given ${BAR_PARENT_DIR}?
# or is there a better way to get `foo`?
真正的用例是,最初是foo
是通过$ {cmake_project_name}
提取的,但是最近有需要使repo subpo兼容。一旦将此存储库用作子模块,$ {cmake_project_name}
将不再等于foo
。此外,bar
是foo
的子模块,因此我们不允许使用硬代码foo
进入bar/cmakelists.txt,因为那会破裂使用bar
作为子模块的其他存储库。
有没有办法从父目录中提取cmakelists.txt的项目名称?
编辑:我正在寻找一种解决方案,以使以下方案起作用。含义foo
由另一个项目种类。例如
baz
├───CMakeLists.txt
├───foo
│ ├───CMakeLists.txt
│ ├───bar
│ ├───CMakeLists.txt
Please see the below minimal example:
├───CMakeLists.txt
├───bar
│ ├───CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(foo)
add_subdirectory(bar)
bar/CMakeLists.txt
project(bar)
cmake_path(GET CMAKE_CURRENT_LIST_DIR PARENT_PATH BAR_PARENT_DIR)
# how can I get `foo` given ${BAR_PARENT_DIR}?
# or is there a better way to get `foo`?
The real use case is that originally foo
was extracted via ${CMAKE_PROJECT_NAME}
, but recently there's a need to make the repo submodule compatible. Once this repo is being used as a submodule, ${CMAKE_PROJECT_NAME}
won't be equivalent to foo
anymore. Additionally, bar
is a submodule of foo
, so we aren't allowed to hard code foo
into bar/CMakeLists.txt because that would break other repos that are using bar
as a submodule.
Is there a way to extract the project name of a CMakeLists.txt from a parent directory?
Edit: I am looking for a solution that will make the below scenario work. Meaning foo
is submoduled by another project. e.g.
baz
├───CMakeLists.txt
├───foo
│ ├───CMakeLists.txt
│ ├───bar
│ ├───CMakeLists.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不难。
project()
命令始终在调用时设置project_name
变量。因此,在 you 呼叫project()
之前,该变量的值是直接父母的名称。这没有任何标准,但是实施是微不足道的:
This is not so hard. The
project()
command always sets thePROJECT_NAME
variable when it is called. So the value of that variable just before you callproject()
is the name of the immediate parent.There's nothing standard for this, but it's trivial to implement: