Issue
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?
Solution
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).
Answered By - Tsyvarev Answer Checked By - Mary Flores (WPSolving Volunteer)