I have an INTERFACE called exi and I want its INTERFACE_SOURCES to be with the source code directory during build then later during install I would copy the source files elsewhere and set INTERFACE_SOURCES to that install directory
I have the following CMakeList.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
# Specify C sources and HEADER_PUBLIC directories
set(SOURCE_C
file1.c
file2.c
)
set(HEADER_PUBLIC
src/header1.h
include/header2.h
)
#set a variable with source code install dir
set(SRC_INSTALL_DIR share/src)
# Construct List with Full path for the build interface
set(SOURCE_C_BUILD "")
foreach(file ${SOURCE_C})
list(APPEND SOURCE_C_BUILD "${CMAKE_CURRENT_SOURCE_DIR}/src/${file}")
endforeach()
# Construct List with Full path for the install interface
set(SOURCE_C_INSTALL "")
foreach(file ${SOURCE_C})
list(APPEND SOURCE_C_INSTALL "${SRC_INSTALL_DIR}/${file}")
endforeach()
# Create an Interface
add_library(exi INTERFACE)
target_sources(exi INTERFACE
$<BUILD_INTERFACE:${SOURCE_C_BUILD}>
$<INSTALL_INTERFACE:${SOURCE_C_INSTALL}>
)
target_include_directories(exi INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>
$<INSTALL_INTERFACE:${SRC_INSTALL_DIR}>
)
# install source code
install(FILES ${SOURCE_C_BUILD} DESTINATION ${SRC_INSTALL_DIR})
# Install Header files
install(FILES ${HEADER_PUBLIC} DESTINATION include)
and in a higher level CMakeLists.txt exi is exported with different configs in build than install with these lines
install(
TARGETS exi
EXPORT exi_targets
DESTINATION lib/exi
)
install(EXPORT exi_targets DESTINATION lib/exi FILE exiConfig.cmake)
export(TARGETS exi FILE exiConfig.cmake)
OUTPUT
The output is correct in both exiConfig.cmake files
- build interface exiConfig.cmake
# Create imported target exi
add_library(exi INTERFACE IMPORTED)
set_target_properties(exi PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/workspaces/Project/inf/exi/include;/workspaces/Project/inf/exi/src"
INTERFACE_SOURCES "/workspaces/Project/inf/exi/src/file1.c;/workspaces/Project/inf/exi/src/file2.c"
)
- install interface exiConfig.cmake
# Create imported target exi
add_library(exi INTERFACE IMPORTED)
set_target_properties(exi PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/share/src"
INTERFACE_SOURCES "${_IMPORT_PREFIX}/share/src/file1.c;${_IMPORT_PREFIX}/share/src/file2.c"
)
WARNING
but I am getting this warning
Policy CMP0076 is not set: target_sources() command converts relative paths
to absolute. Run "cmake --help-policy CMP0076" for policy details. Use
the cmake_policy command to set the policy and suppress this warning.
An interface source of target "exi" has a relative path.
This warning is for project developers. Use -Wno-dev to suppress it.
as you can see ${SOURCE_C_BUILD} is not relative path and if I comment the line with ${SOURCE_C_INSTALL} then I would still get the same warning.