英文:
How can I prevent targets from added subdirectories of external dependencies from being built in CMake?
问题
The structure of my project:
- CMakeLists.txt
- my_src
- extern
- CMakeLists.txt
- dir1
- dir2
"extern" is from a third-party repo. "extern/CMakeLists.txt" added "dir1" and "dir2" with "add_subdirectory()" command.
Now I want to remove "dir1" from being built. But "extern/CMakeList.txt" doesn't provide a CMake option/switch for adding "dir1" or not.
So is there a better or formal solution for this situation?
I searched with Google, the best solution I got is to patch the "extern" project, which is not a solution I prefer, since in this project, the extern directory is not tracked in version control and needs to be set up manually, and I don't want to add such extra steps for other users (team members and customers) trying to build the project.
英文:
The structure of my project:
- CMakeLists.txt
- my_src
- extern
- CMakeLists.txt
- dir1
- dir2
"extern" is from a third_party repo. "extern/CMakeList.txt" added "dir1" and "dir2" with "add_subdirectory()" command.
Now I want to remove "dir1" from being built. But "extern/CMakeList.txt" doesn't provides cmake option/switch for adding "dir1" or not.
So is there a better or formal solution for this situation?
I searched with google, the best solution I got is to patch the "extern" project, which is not a solution I prefer, since in this project, the extern directory is not tracked in version control and needs to be setup up manually, and I don't want to add such extra steps for other users (team members and customers) trying to build the project.
答案1
得分: 1
如果您的目标实际上是防止这些子目录被添加(这将阻止在这些子目录的CMakeLists.txt文件中添加的目标生成构建配置),除了在生成构建系统之前自行修改外部项目的CMakeLists.txt文件,和/或要求外部项目的维护者添加这样的选项来配置是否添加其子目录,我认为您没有其他可以做的事情。
但根据我阅读的内容(您说“现在我想要移除“dir1”不要进行构建。”(术语方面有些模糊)),您的目标只是防止在这些子目录中添加的目标被构建。假设它们只是被构建,因为您正在运行一个不特别选择要构建的目标的构建命令(即,假设您正在构建“all”目标),在这种情况下,我认为您可以为这些子目录设置EXCLUDE_FROM_ALL
目录属性的真值,用于您不希望使用“all”目标进行构建的每个目标(或单独为这些子目录中添加的目标设置EXCLUDE_FROM_ALL
目标属性)。
add_subdirectory(extern)
set_property(DIRECTORY extern/dir1 PROPERTY EXCLUDE_FROM_ALL YES)
set_property(DIRECTORY extern/dir2 PROPERTY EXCLUDE_FROM_ALL YES)
请注意,add_subdirectory
本身具有一个EXCLUDE_FROM_ALL
参数,这只是一种方便的方法,用于设置添加的子目录的相应目录属性,但我不确定这是否在这里有用。
英文:
If your goal is actually to prevent those subdirectories from getting added (which would prevent targets added in those subdirectories' CMakeLists.txt files from getting build configuration generated), aside from modifying the CMakeLists.txt file of the external project yourself before generating the buildsystem, and/or asking the maintainer of that external project to add such options to configure whether those subdirectories of its get added, I don't think there's anything you can do.
But from what I read (you said "Now I want to remove "dir1" from being built." (which is a little ambiguous in terms of terminology)), your goal is just to prevent the targets added in those subdirectories from getting built. Assuming they're just getting built because you're running a build command that doesn't pick specifically which targets to build (I.e. assuming you're building the "all" target), in that case, I think what you can do is set the EXCLUDE_FROM_ALL
directory property for those subirectories to a truthy value for each target that you don't want to get built with the "all" target (or individually set the EXCLUDE_FROM_ALL
target property for those targets added in those subdirectories). Ex.
add_subdirectory(extern)
set_property(DIRECTORY extern/dir1 PROPERTY EXCLUDE_FROM_ALL YES)
set_property(DIRECTORY extern/dir2 PROPERTY EXCLUDE_FROM_ALL YES)
Note that add_subdirectory
itself has an EXCLUDE_FROM_ALL
argument that is just a convenience way to set the corresponding directory property for the added subdirectory, but I'm not sure if that's uesful here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论