I have the following simple CMake script:
cmake_minimum_required(VERSION 3.26)
project(subway LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(boost ...)
FetchContent_MakeAvailable(boost) # this line adds install rules for boost
add_library(heavy-rail heavy-rail.cpp)
install(TARGETS heavy-rail COMPONENT heavy-rail)
add_library(bus bus.cpp)
install(TARGETS bus COMPONENT bus)
How can I do the following:
cmake_install.cmake
install script?While this is undocumented behavior, you can use install(CODE "set(CMAKE_INSTALL_LOCAL_ONLY TRUE)" ALL_COMPONENTS)
.
In the CMake installation script cmake_install.cmake
, dependency install rules are added by including the subdirectory installation script. That include
statement is guarded by a check to the CMAKE_INSTALL_LOCAL_ONLY
variable, and only runs if the variable is false:
...
if(NOT CMAKE_INSTALL_LOCAL_ONLY)
# Include the install script for the subdirectory.
include("/home/luka/subway/cmake-build-debug/_deps/boost-build/cmake_install.cmake")
endif()
...
We can set the variable in the install script by using a CODE
install rule. This prevents the Boost install script from ever running:
install(CODE "set(CMAKE_INSTALL_LOCAL_ONLY TRUE)")
However, in the component-based install, our CODE
install statement won't run (because it isn't associated with any components). It is unlikely Boost install components will overlap with our component names. If we still want to avoid calling the Boost install script, we can use the ALL_COMPONENTS
modifier (only available with CODE
install rules):
install(CODE "set(CMAKE_INSTALL_LOCAL_ONLY TRUE)" ALL_COMPONENTS)
Final script:
cmake_minimum_required(VERSION 3.26)
project(subway LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(boost ...)
# Make sure this is added before the MakeAvailable call
install(CODE "set(CMAKE_INSTALL_LOCAL_ONLY TRUE)" ALL_COMPONENTS)
FetchContent_MakeAvailable(boost) # this line adds install rules for boost
add_library(heavy-rail heavy-rail.cpp)
install(TARGETS heavy-rail COMPONENT heavy-rail)
add_library(bus bus.cpp)
install(TARGETS bus COMPONENT bus)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With