CPack: Exclude INSTALL commands from subdirectory (googletest directory)

元气小坏坏 提交于 2019-12-17 14:50:13

问题


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.

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.

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?


回答1:


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()

packaging with

cmake -DDISABLE_TESTS=ON <source-dir>
cpack

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()

This approach has also been suggested in CMake mailing.




回答2:


So there is the macro option @Tsyvarev mentioned that was originally suggested here:

# overwrite install() command with a dummy macro that is a nop
macro (install)
endmacro ()

# configure build system for external libraries
add_subdirectory(external)

# replace install macro by one which simply invokes the CMake
install() function with the given arguments
macro (install)
  _install(${ARGV})
endmacro(install)

Note ${ARGV} and ${ARGN} are the same but the docs currently suggest using ${ARGN}. Also the fact that macro-overwriting prepends _ to the original macro name is not documented, but it is still the behaviour. See the code here.

However, I never got the above code to work properly. It does really weird things and often calls install() twice.

An alternative - also undocumented - is to use EXCLUDE_FROM_ALL:

add_subdirectory(external EXCLUDE_FROM_ALL)

According to some comment I found somewhere this disables install() for that subdirectory. I think what it actually does is set EXCLUDE_FROM_ALL by default for all the install() commands which also probably does what you want. I haven't really tested it, worth a shot though.




回答3:


A bit late reply, but I just spent too long a time figuring this out.

In the specific case of googletests, specifying this in your top level CMakeLists.txt does the trick.

option(INSTALL_GMOCK "Install Googletest's GMock?" OFF)
option(INSTALL_GTEST "Install Googletest's GTest?" OFF)
add_subdirectory(googletest)

I read on (I think) the CMake mailing list that making installation conditional on a INSTALL_<package name> inside your package is sort of a defacto standard (and one I'm certainly going to follow from now on!). But I can't find that link now.



来源:https://stackoverflow.com/questions/35344132/cpack-exclude-install-commands-from-subdirectory-googletest-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!