Reputation: 6083
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?
Upvotes: 12
Views: 9589
Reputation: 96556
So there is the macro option @Tsyvarev mentioned that was originally suggested here (code below) but I never got it 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.
Non-working macro solution for posterity:
# 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.
Upvotes: 20
Reputation: 65928
NOTE: Approach below is the last thing which you should try.
While it works by itself, it prevents your project to be integrated with other projects, which redefines install
command, and prevents your project to be configured with some toolchains, which do such redefinition.
The main idea: redefine install
command so it will install things only when a specific variable is not TRUE. That variable could be set to TRUE when you want to include subdirectory and disable installation commands inside it.
In the top-level CMakeLists.txt place these commands. They doesn't disable installation, but allows further code to do that in a controllable manner.
# By using such 'if' we allow integration of our project
# with other projects, which use the **same approach**
# for disabling installation.
if(NOT DEFINED _DISABLE_INSTALLATION)
# This variable is responsible for installation disabling.
set(_DISABLE_INSTALLATION FALSE)
# Replace install() with conditional installation.
macro(install)
if (NOT _DISABLE_INSTALLATION)
_install(${ARGN})
endif()
endmacro()
endif()
When need to include googletest (or other project) with disabled installation, wrap it in the following manner:
# Store old value of flag for the case the external project already disables installation of current one.
set(_DISABLE_INSTALLATION_OLD ${_DISABLE_INSTALLATION})
set(_DISABLE_INSTALLATION TRUE)
add_subdirectory(googletest)
# Restore original install() behavior.
set(_DISABLE_INSTALLATION ${_DISABLE_INSTALLATION_OLD})
Approach with install
redifinition has been suggested in CMake mailing. But the code above avoids double redefinition of install
, in which case original install
behavior will be lost (_install
will refer to the first redefinition).
Upvotes: 13
Reputation: 166
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.
Upvotes: 9