Cmake:几个使用自定义目标进行测试的项目
Cmake : several projects using custom target to make test
假设我有一个库 ProjectA
,我在我的 CMakefile 中定义了一个自定义目标 build-tests
来构建我的测试,以便我可以在之后做 make test
。 (参见 CMake & CTest : make test doesn't build tests)。
然后,我有第二个依赖于 ProjectA
的库 ProjectB
。我使用 git 子模块,这样我就可以在 ProjectB
的 CMakefile 中执行 add_subdirectory(ProjectA)
。问题是,我想做同样的事情:定义一个自定义目标 build-tests
来构建 ProjectB
的测试,但我不能,因为它已经在 ProjectA
中定义了...
我的问题与 and 类似,但他们可以定义一个目标来避免这种情况,而我希望能够在 ProjectA
和 [=13= 中执行 make build-tests
].
那么,有没有办法在本地为 cmake 项目定义自定义目标?还是有一种聪明的方法来做我想做的事? (这看起来很自然)
So, is there a way to define a custom target locally to a cmake project for example ? or is there a clever way to do what I would like to do ? (which seems quite natural)
因为您正在使用自定义目标,可能 ALLOW_DUPLICATE_CUSTOM_TARGETS
可以完成这项工作。
文档确实很清楚:
Allow duplicate custom targets to be created.
如果你走得更远,你会发现:
The property allows multiple add_custom_target
command calls in different directories to specify the same target name.
不幸的是它也有一些限制:
[...] setting this property will cause non-Makefile generators to produce an error and refuse to generate the project.
仔细阅读文档(参见上面的 link)以了解它是否适合您的目的。
我想我找到了更好的解决方案。让我们假设:
- 您可以控制两个项目中的
CMakeLists.txt
文件。
- 共享目标应该配置相同。
然后你可以这样做:
if(NOT TARGET build-tests)
add_custom_target(build-tests ALL)
endif()
add_custom_target(build-tests.ProjectA ...)
add_dependencies(build-tests build-tests.ProjectA)
build-tests
目标实质上将成为每个项目的 build-tests
目标的联合。
假设我有一个库 ProjectA
,我在我的 CMakefile 中定义了一个自定义目标 build-tests
来构建我的测试,以便我可以在之后做 make test
。 (参见 CMake & CTest : make test doesn't build tests)。
然后,我有第二个依赖于 ProjectA
的库 ProjectB
。我使用 git 子模块,这样我就可以在 ProjectB
的 CMakefile 中执行 add_subdirectory(ProjectA)
。问题是,我想做同样的事情:定义一个自定义目标 build-tests
来构建 ProjectB
的测试,但我不能,因为它已经在 ProjectA
中定义了...
我的问题与 ProjectA
和 [=13= 中执行 make build-tests
].
那么,有没有办法在本地为 cmake 项目定义自定义目标?还是有一种聪明的方法来做我想做的事? (这看起来很自然)
So, is there a way to define a custom target locally to a cmake project for example ? or is there a clever way to do what I would like to do ? (which seems quite natural)
因为您正在使用自定义目标,可能 ALLOW_DUPLICATE_CUSTOM_TARGETS
可以完成这项工作。
文档确实很清楚:
Allow duplicate custom targets to be created.
如果你走得更远,你会发现:
The property allows multiple
add_custom_target
command calls in different directories to specify the same target name.
不幸的是它也有一些限制:
[...] setting this property will cause non-Makefile generators to produce an error and refuse to generate the project.
仔细阅读文档(参见上面的 link)以了解它是否适合您的目的。
我想我找到了更好的解决方案。让我们假设:
- 您可以控制两个项目中的
CMakeLists.txt
文件。 - 共享目标应该配置相同。
然后你可以这样做:
if(NOT TARGET build-tests)
add_custom_target(build-tests ALL)
endif()
add_custom_target(build-tests.ProjectA ...)
add_dependencies(build-tests build-tests.ProjectA)
build-tests
目标实质上将成为每个项目的 build-tests
目标的联合。