When looking closely at https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20Export%20Configuration.html, I realize that I don’t seem to follow the logic of this tutorial. I’d appreciate a review of my procedure (which must come, at some time, from an example found on the net). NB in order to be clear, my procedure seems to work (tested on several machines, os and with various directory locations.
Also, CMAKE_INSTALL_PREFIX
is set.
# Intall the target
install (TARGETS ${TARGET_NAME}
EXPORT ${TARGET_NAME}Targets
LIBRARY DESTINATION "${INSTALL_LIB_PATH}/${CONFIG}"
ARCHIVE DESTINATION "${INSTALL_LIB_PATH}/${CONFIG}"
RUNTIME DESTINATION "${INSTALL_BIN_PATH}/${CONFIG}"
PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_PATH}"
)
# Install the target export
install (EXPORT ${TARGET_NAME}Targets
NAMESPACE ${TARGET_NAME}::
DESTINATION "${COMPUTED_ABSOLUTE_PATH_TO_A_DEDICATED_DIRECTORY}"
)
# Install the ${TARGET_NAME}Config.cmake
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.cmake"
DESTINATION "${COMPUTED_ABSOLUTE_PATH_TO_A_DEDICATED_DIRECTORY}"
)
AFAIU, the first install
tells to create a ${TARGET_NAME}Targets.cmake
file in the build directory.
Paths are given relative to CMAKE_INSTALL_PREFIX
.
The second one creates a namespace to wrap my target into it and copies the resulting ${TARGET_NAME}Targets.cmake
into COMPUTED_ABSOLUTE_PATH_TO_A_DEDICATED_DIRECTORY
NB why don’t I need a install(EXPORT ${TARGET_NAME}Targets FILES ${TARGET_NAME}Targets.cmake DESTINATION "${COMPUTED_ABSOLUTE_PATH_TO_A_DEDICATED_DIRECTORY}")
, as in the tutorial?
The last install
is copying the remaining configuration files into the destination.
NB ${TARGET_NAME}Targets-Debug.cmake
and ${TARGET_NAME}Targets-Release.cmake
are also copied at the right place but I don’t know by what magic.
Eventually, the tutorial indicates the need for
target_include_directories(${TARGET_NAME}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
but I don’t have it and I didn’t observe any issue (so far)
What may be the issues with my procedure and how should I fix them.
A guess is that the generated configuration files are full of absolute paths to the binaries and headers making them not compliant with https://cmake.org/cmake/help/latest/guide/tutorial/Packaging%20an%20Installer.html.
(yet I’d like, in the near future, the possibility to install my artefacts, without the need to configure and build them locally).
Regards
A.
[TEASER] I will make a separate question about the proper way to create configuration files. The procedure I found is also quite different from the tutorial and, in this case, IMHO, the tutorial is lacking information about how to declare and propagate dependencies through the configuration file.