我如何在CMakeLists.txt中知道它是否与add_subdirectory()一起使用? [英] How can I tell, within a CMakeLists.txt, whether it's used with add_subdirectory()?

查看:565
本文介绍了我如何在CMakeLists.txt中知道它是否与add_subdirectory()一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码项目,可以独立构建,也可以作为较大存储库的子项目构建(将其作为子存储库检查)。在后一种情况下,我为主要项目有一个顶级 CMakeLists.txt

I have this code project can be built either independently or as a subproject of a larger repository (checking it our as a sub-repository). In the latter case, I have a top-level CMakeLists.txt for the main project which has

add_subdirectory(${MY_SUBPROJ_SUBDIR})

现在,我希望该子项目如果通过 add_directory()使用它,其行为会有所不同。显然,我将使用大型 if 指令。但是我要检查什么情况?在运行某些 CMakeLists.txt 时,CMake如何告诉它是子目录文件还是主文件?

now, I want the subproject to behave somewhat differently in case it's used via the add_directory(). Obviously, I would be using a large if instruction. But what condition do I check? How can CMake "tell", when running for some CMakeLists.txt, whether it's a subdir file or the main file?

推荐答案


  1. 之后 project()调用中项目的 CMakeLists.txt ,并且可以在项目的子目录中使用:

  1. After the project() call in project's CMakeLists.txt and in the project's subdirectories you may use:

 if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
     # I am top-level project.
 else()
     # I am called from other project with add_subdirectory().
 endif()

注意:如评论中所述,此方法可能不能在Windows上运行,其中 CMAKE_SOURCE_DIR 包含小写版本的 PROJECT_SOURCE_DIR ,因此无法直接比较相等性。对于这种情况,请检查 PARENT_DIRECTORY 属性,如该答案所述对于重复的问题,似乎更可靠。

NOTE: As stated in the comments, this approach may to not work on Windows, where CMAKE_SOURCE_DIR contains lower-case version of PROJECT_SOURCE_DIR and thus cannot be compared directly for equality. For that case approach with checking PARENT_DIRECTORY property, as described in that answer to the duplicate question, seems to be more robust.

在<$ c $之前之前 c> project()调用:

Alternative for use before the project() call:

 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
     # I am top-level project.
 else()
     # I am called from other project with add_subdirectory().
 endif()

此替代方案也可以在项目 CMakeLists.txt中的任何位置使用(但不在子目录中)。

This alternative can also be used anywhere in project's CMakeLists.txt (but not in subdirectories).




您有一个项目 A ,其中有两个 CMakeLists.txt :一个在项目目录中,另一个在子目录 src / 。上述使用方法的方案:


Assume you have a project A with two CMakeLists.txt: one in the project's directory, and one in subdirectory src/. Scheme for use approaches described above:

CMakeLists.txt

cmake_minimum_required(...)
...
<only approach *2* can be used there>
...
project(A)
...
<approach *1* or *2* can be used there>
...
add_subdirectory(src)
...

src / CMakeLists.txt

...
<only approach *1* can be used there>
...

在给定的方案下,项目 A 可以检测到内置的独立项目(顶级项目)或作为另一个项目 B 的一部分。

With given scheme project A may detect, whether it is built standalone (top-level project) or as a part of another project B.

这篇关于我如何在CMakeLists.txt中知道它是否与add_subdirectory()一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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