Let’s assume there is a project A with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.27)
project(A VERSION 1.0.0 LANGUAGES CXX)
set(library_name ProjectA)
add_library(${library_name} INTERFACE)
add_library(My::ProjectA ALIAS ${library_name})
target_sources(${library_name}
INTERFACE
FILE_SET HEADERS
BASE_DIRS
include
FILES
include/header.h
)
target_include_directories(${library_name}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
install(TARGETS ${library_name} EXPORT ProjectA
FILE_SET HEADERS
DESTINATION lib/subdir/include/another/dir
INCLUDES DESTINATION lib/subdir/include
)
install(EXPORT ProjectA DESTINATION cmake NAMESPACE My::)
export(EXPORT ProjectA NAMESPACE My::)
This project has the header files in the directory include
, but installs them to another directory lib/subdir/include/another/dir
and adds the directory lib/subdir/include
as include directory.
If another project fetches the source code with FetchContent, is there a way to automatically figure out in a CMakeLists.txt to which destination the header files are installed?
At the moment, I have other projects B and C, which use the install tree and expect the headers to be in another/dir
, but there is also a test project T that fetches the source code of A, B, and C directly and manually copies the header files around. Therefore, I was wondering what might be the best way to have the headers in the right location when building T and the dependencies without changing the layout in Project A, B, and C.