Correctly exporting configuration: second part

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?

It depend on the type of your Target:
If it is an INTERFACE library, there will be no <Target>Targest-<config>.cmake files written.
Too, AFAIK not for executables.

Is a good example to handle complexity and portability witch CMake: see
cmake/OpenCVGenConfig.cmake

It writes only this files!

/usr/local/lib/cmake/opencv4
|-- OpenCVConfig-version.cmake
|-- OpenCVConfig.cmake
|-- OpenCVModules-release.cmake
`-- OpenCVModules.cmake
/usr/local/lib/cmake/openjpeg-2.5
|-- OpenJPEGConfig.cmake
|-- OpenJPEGConfigVersion.cmake
|-- OpenJPEGTargets-release.cmake
`-- OpenJPEGTargets.cmake
/usr/local/lib/cmake/openvino2024.1.0
|-- OpenVINOConfig-version.cmake
|-- OpenVINOConfig.cmake
|-- OpenVINOTargets-release.cmake
`-- OpenVINOTargets.cmake

3 directories, 12 files

in contrast to boost:

bash-5.2$ tree -d /usr/local/lib/cmake/boost*
/usr/local/lib/cmake/boost_atomic-1.85.0
/usr/local/lib/cmake/boost_charconv-1.85.0
/usr/local/lib/cmake/boost_chrono-1.85.0
/usr/local/lib/cmake/boost_container-1.85.0
/usr/local/lib/cmake/boost_context-1.85.0
/usr/local/lib/cmake/boost_contract-1.85.0
/usr/local/lib/cmake/boost_coroutine-1.85.0
/usr/local/lib/cmake/boost_date_time-1.85.0
/usr/local/lib/cmake/boost_exception-1.85.0
/usr/local/lib/cmake/boost_fiber-1.85.0
/usr/local/lib/cmake/boost_filesystem-1.85.0
/usr/local/lib/cmake/boost_graph-1.85.0
/usr/local/lib/cmake/boost_graph_parallel-1.85.0
/usr/local/lib/cmake/boost_headers-1.85.0
/usr/local/lib/cmake/boost_iostreams-1.85.0
/usr/local/lib/cmake/boost_json-1.85.0
/usr/local/lib/cmake/boost_locale-1.85.0
/usr/local/lib/cmake/boost_log-1.85.0
/usr/local/lib/cmake/boost_log_setup-1.85.0
/usr/local/lib/cmake/boost_math_c99-1.85.0
/usr/local/lib/cmake/boost_math_c99f-1.85.0
/usr/local/lib/cmake/boost_math_c99l-1.85.0
/usr/local/lib/cmake/boost_math_tr1-1.85.0
/usr/local/lib/cmake/boost_math_tr1f-1.85.0
/usr/local/lib/cmake/boost_math_tr1l-1.85.0
/usr/local/lib/cmake/boost_nowide-1.85.0
/usr/local/lib/cmake/boost_prg_exec_monitor-1.85.0
/usr/local/lib/cmake/boost_program_options-1.85.0
/usr/local/lib/cmake/boost_random-1.85.0
/usr/local/lib/cmake/boost_regex-1.85.0
/usr/local/lib/cmake/boost_serialization-1.85.0
/usr/local/lib/cmake/boost_stacktrace_addr2line-1.85.0
/usr/local/lib/cmake/boost_stacktrace_basic-1.85.0
/usr/local/lib/cmake/boost_stacktrace_noop-1.85.0
/usr/local/lib/cmake/boost_system-1.85.0
/usr/local/lib/cmake/boost_test_exec_monitor-1.85.0
/usr/local/lib/cmake/boost_thread-1.85.0
/usr/local/lib/cmake/boost_timer-1.85.0
/usr/local/lib/cmake/boost_type_erasure-1.85.0
/usr/local/lib/cmake/boost_unit_test_framework-1.85.0
/usr/local/lib/cmake/boost_url-1.85.0
/usr/local/lib/cmake/boost_wave-1.85.0
/usr/local/lib/cmake/boost_wserialization-1.85.0