This is a follow up of Correctly exporting configuration.
In first question, I focused on the installation part.
In this part I’d like advices on the creation and installation of the export configuration files.
So far I’m not following what is done in https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20Export%20Configuration.html.
Let’s give details.
In my CMakeLists, I’m doing:
set(MY_REQUIRED_DEPENDENCIES)
...
#for each dependency LibName
find_package(<LibName> REQUIRED)
list(APPEND MY_REQUIRED_DEPENDENCIES <LibName>)
target_link_libraries(${TARGET_NAME} <PUBLIC|PRIVATE|INTERFACE> <LibName>::<LibName>)
# actually there is more code to check if <LibName>::<LibName>exists and to create it on the fly if <LibName> is found but not defining <LibName>::<LibName> target
...
configure_file("<Path_To_A_Template_Of_Config_File>/MyConfig.cmake.in" ${TARGET_NAME}Config.cmake @ONLY)
unset(MY_REQUIRED_DEPENDENCIES)
...
# Intall the target: SEE ALSO [FIRST PART OF THE QUESTION](https://discourse.cmake.org/t/correctly-exporting-configuration/11042)
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: SEE ALSO [FIRST PART OF THE QUESTION](https://discourse.cmake.org/t/correctly-exporting-configuration/11042)
install (EXPORT ${TARGET_NAME}Targets
NAMESPACE ${TARGET_NAME}::
DESTINATION "${COMPUTED_ABSOLUTE_PATH_TO_A_DEDICATED_DIRECTORY}"
)
# Install the ${TARGET_NAME}Config.cmake: SEE ALSO [FIRST PART OF THE QUESTION](https://discourse.cmake.org/t/correctly-exporting-configuration/11042)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.cmake"
DESTINATION "${COMPUTED_ABSOLUTE_PATH_TO_A_DEDICATED_DIRECTORY}"
)
...
Here is MyConfig.cmake.in
:
include(CMakeFindDependencyMacro)
foreach(_lib_ IN ITEMS @MY_REQUIRED_DEPENDENCIES@)
find_dependency(${_lib_} REQUIRED)
endforeach()
# include the generated target file
include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_NAME@Targets.cmake")
Obviously, I’m not following at all the tutorial and this procedure has several issues:
1- I’m not sure it produces properly relocatable, packagable files, though, up to know I didn’t have issues to clone, configure, and build my libraries on other machines, os and locations (I didn’t try direct packaging of binaries and installation from the package)
2- I’m not sure to handle properly <LibName>::<CompName>
(what is the proper wording in cmake? namespace::component? I’m not sure), especially if I’m creating <LibName>::<LibName>
, which was the case, for instance, for opencv (yet it worked so far, but I linked, in this case, against all opencv modules, which is overkill)
3- It does not propagate special logic for dependency detection (see Calling CMake "default" Find<Module> from a user defined one for details on this issue)
How can the tutorial and/or my procedure be fixed?
Regards
A.
bonus question: with this procedure <Target>Targest-<config>.cmake
files are automaticaly created whereas I don’t see, in my procedure, the -<config>
part, is it automated? At what stage?