Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude dependencies in CMake install rules run for all components

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:

  1. Exclude boost install rules from the resulting cmake_install.cmake install script?
  2. Exclude boost install rules no matter what the component is?
like image 524
Luka Govedič Avatar asked Aug 31 '25 03:08

Luka Govedič


1 Answers

Answer

While this is undocumented behavior, you can use install(CODE "set(CMAKE_INSTALL_LOCAL_ONLY TRUE)" ALL_COMPONENTS).

Explanation

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)
like image 166
Luka Govedič Avatar answered Sep 02 '25 23:09

Luka Govedič