How to completely ignore BUILD_INTERFACE libraries in installation?

Through that, I link private libraries with BUILD_INTERFACE genexpr and they wouldn’t be in the exported targets, but the libraries’ linked libraries will be and unfortunately when we run the install rule they will be installed. And i don’t have access to those libraries’ code to change. How can I remove those targets from being installed?

For testing:

TestingExports.text

cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(TestinExports CXX)

add_subdirectory(ThatThing)

file(WRITE main.cc "int main() { }")
add_executable(${PROJECT_NAME} main.cc)
target_link_libraries(${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:ThatThing>)

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config)
install(EXPORT ${PROJECT_NAME}Config DESTINATION lib/cmake)

ThatThing/CMakeLists.txt

cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(ThatThing CXX)

add_subdirectory(ThoseLibraries)

file(WRITE that_thing.cc "void Function() { }")
add_library(${PROJECT_NAME} STATIC that_thing.cc)
target_link_libraries(${PROJECT_NAME} PRIVATE ThoseLibraries)

ThatThing/ThoseLibraries/CMakeLists.txt

cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(ThoseLibraries CXX)

file(WRITE those_libraries.cc "void AnotherFunction() { }")
add_library(${PROJECT_NAME} STATIC those_libraries.cc)

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config)
install(EXPORT ${PROJECT_NAME}Config DESTINATION lib/cmake)

After we build and install we can see that ThoseLibraries is also installed:

$ cmake -B build -DCMAKE_INSTALL_PREFIX=$PWD/install
$ cmake --build build
$ cmake --install build
-- Install configuration: ""
-- Installing: /tmp/export_set/install/lib/libThoseLibraries.a
-- Installing: /tmp/export_set/install/lib/cmake/ThoseLibrariesConfig.cmake
-- Installing: /tmp/export_set/install/lib/cmake/ThoseLibrariesConfig-noconfig.cmake
-- Installing: /tmp/export_set/install/bin/TestinExports
-- Installing: /tmp/export_set/install/lib/cmake/TestinExportsConfig.cmake
-- Installing: /tmp/export_set/install/lib/cmake/TestinExportsConfig-noconfig.cmake

You’ll need to mask these install() commands somehow.

For me the most successful strategy has been to always use components for my install commands, then set CPack variables so that only my components would get installed.

1 Like

I handle this case by adding EXCLUDE_FROM_ALL to add_subdirectory().

add_subdirectory(ThoseLibraries EXCLUDE_FROM_ALL)
1 Like