CPack:从子目录(googletest 目录)中排除 INSTALL 命令

2021-12-23 00:00:00 installation cmake cpack c++ googletest

我在一个项目中使用 CMake,在我的测试用例中使用 googletest.环顾互联网,将 googletest 源代码复制到存储库的子文件夹中并将其包含在add_subdirectory(googletest)"中似乎是常见做法.我就是这么做的.

I'm using CMake for a project and googletest for my test cases. Looking around the internet, it seems to be common practise to just copy the googletest source into a subfolder of your repository and include it with "add_subdirectory(googletest)". I did that.

现在我使用 CPack 为我的项目生成 debian 包.不幸的是,CPack 生成的包与我的项目一起安装了 googletest.这当然不是我想要的.

Now I'm using CPack to generate debian packages for my project. Unfortunately, the packages generated by CPack install googletest alongside with my project. This is of course not what I want.

查看 googletest 目录,我在那里发现了一些 INSTALL cmake 命令,所以很清楚,为什么会这样.现在的问题是 - 我怎样才能避免它?我不喜欢从 googletest 修改 CMakeLists.txt 文件,因为我必须记住在更新时重新应用我的修改.还有其他方法可以在 CPack 中禁用这些安装吗?

Looking in the googletest directory, I found some INSTALL cmake commands there, so it is clear, why it happens. The question is now - how can I avoid it? I don't like modifying the CMakeLists.txt files from googletest, because I would have to remember re-applying my modifications on an update. Is there another way to disable these installs in CPack?

推荐答案

更新:如中所述其他答案,似乎EXCLUDE_FROM_ALL选项是在子目录下的子项目中禁用install的最直接和正确的方法:

Updated: As noted in the other answer, it seems that EXCLUDE_FROM_ALL option is the most direct and correct way for disable install in the subproject in the subdirectory:

add_subdirectory(googletest EXCLUDE_FROM_ALL)


以前的解决方案

如果您的项目发布中不需要测试(您希望通过 CPack 交付),则有条件地包含 googletest 子目录,并在打包时将条件设置为 false:


Previous solutions

If you don't need tests in your project's release (which you want to deliver with CPack), then include googletest subdirectory conditionally, and set conditional to false when packaging:

...
if(NOT DISABLE_TESTS)
    add_subdirectory(googletest)
endif()

包装

cmake -DDISABLE_TESTS=ON <source-dir>
cpack

或者,如果你想要测试,但不想安装测试基础设施,你可以通过定义同名的宏或函数来禁用install命令:

Alternatively, if you want tests, but don't want to install testing infrastructure, you may disable install command via defining macro or function with same name:

# Replace install() to do-nothing macro.
macro(install)
endmacro()
# Include subproject (or any other CMake code) with "disabled" install().
add_subdirectory(googletest)
# Restore original install() behavior.
macro(install)
    _install(${ARGN})
endmacro()

CMake 邮件中也建议了这种方法.

根据评论,这种替换 CMake 命令的方式非常棘手,并且在某些情况下可能不起作用:传递给修改后的 install 的参数被错误解析或恢复 install 不起作用,甚至以下 install s 都被禁用.

According to the comments, that way with replacing CMake command is very tricky one and may to not work in some cases: either parameters passed to the modified install are parsed incorrectly or restoring install is not work and even following installs are disabled.

相关文章