How to use CMakePackageConfigHelpers right?

When I try to use it like here https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html#example-generating-package-files I run into problems.

Using cmake version 3.16.3 on OSX and debian, same result.

I have an project cxx_simplelog, that requires spdlog to be installed.

spdlog depends on fmt lib to be installed too.
All that is true, but wenn I install my project und try to build an example using

find_package(cxx_simplelog REQUIRED)

my package is found, but the dependent libs are not imported?

project-config.cmake.in

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/cxx_simplelog-targets.cmake")

check_required_components(cxx_simplelog)

# TODO: with this it works?
# include(CMakeFindDependencyMacro)
# find_dependency(spdlog @SPDLOG_MIN_VERSION@ CONFIG)
'''

Also I am not sure, what is the right directory to install a cmake config files?

lib/packagename/cmake

or

lib/cmake/packagename

Both is found in cmake documentation?

What is about this tutorial page, is this still valid?
https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/How-to-create-a-ProjectConfig.cmake-file

Both are found in the documentation because both are acceptable. There isn’t really a fixed, strict standard for where the .cmake files should go in the installation directory.

Personally, I usually put them in lib/cmake/<package>.

That is also my preference and seems most often used.
At least on my host:

bash-5.0$ tree -d /usr/local/lib/*/cmake
/usr/local/lib/*/cmake [error opening dir]

0 directories
bash-5.0$ tree -d /usr/local/*/cmake
/usr/local/lib/cmake
├── Example
├── Poco
├── Snappy
├── asio
├── cppunit
├── cxx_simplelog
├── doctest
├── fmt
├── gsl-lite
├── spdlog
├── tftpd
└── zlib

12 directories
bash-5.0$ tree -d /opt/local/lib/*/cmake
/opt/local/lib/*/cmake [error opening dir]

0 directories
bash-5.0$ tree -d /opt/local/lib/cmake
/opt/local/lib/cmake
├── DBus1
├── GTest
├── cmocka
├── harfbuzz
├── jsoncpp
├── libssh
└── libxml2

7 directories
bash-5.0$ 

You might find the following issue/discussion relevant:

https://gitlab.kitware.com/cmake/cmake/issues/18453

The tutorial seems to be outdated (cmake version 2.8)!

get_property(TARGETS
    DIRECTORY ./
    PROPERTY BUILDSYSTEM_TARGETS
    )

install(
  EXPORT
    messageTargets
  NAMESPACE
    "message::"
  DESTINATION
    ${INSTALL_CMAKEDIR}
  COMPONENT
    dev
  )

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  ${CMAKE_CURRENT_BINARY_DIR}/messageConfigVersion.cmake
  VERSION ${PROJECT_VERSION}
  COMPATIBILITY SameMajorVersion
  )

configure_package_config_file(
  ${PROJECT_SOURCE_DIR}/cmake/messageConfig.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/messageConfig.cmake
  INSTALL_DESTINATION ${INSTALL_CMAKEDIR}
  )

install(
  FILES
    ${CMAKE_CURRENT_BINARY_DIR}/messageConfig.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/messageConfigVersion.cmake
  DESTINATION
    ${INSTALL_CMAKEDIR}
  )

this seems to be better!
using cmake/messageConfig.cmake.in

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/messageTargets.cmake")
check_required_components(
  @TARGETS@
  )

# Remove dependency on UUID if on Windows
if(NOT WIN32)
  if(NOT TARGET PkgConfig::UUID)
    find_package(PkgConfig REQUIRED QUIET)
    pkg_search_module(UUID REQUIRED uuid IMPORTED_TARGET)
  endif()
endif()

But here find_package() part is also duplicated code and dependent from target_link_libraries() at current CMakeLists.txt and should be generated!

Are you asking for CMake to generate the find_dependency(fmt) call in the generated file? That is a hard problem. The targets which use fmt may be guarded by COMPONENTS of your config.cmake file.

In your example, the UUID target should set _FOUND for the relevant component (setting the package-wide _FOUND variable if it is a required component).

I have learned, there is no generic way to do it.

But I found this block: https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

One more way to do it:

get_filename_component(JSONUTILS_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
#
# NOTE: We had to use find_package because find_dependency does not support
# COMPONENTS or MODULE until 3.15.0? CK
#
list(APPEND CMAKE_MODULE_PATH ${JSONUTILS_CMAKE_DIR})
# NOTE: to find FindRapidJSON.cmake
find_package(RAPIDJSON @MIN_RAPIDJSON_VERSION@ REQUIRED MODULE)
list(REMOVE_AT CMAKE_MODULE_PATH -1)

include(CMakeFindDependencyMacro)
find_dependency(boost_regex @MIN_BOOST_VERSION@ REQUIRED CONFIG)

if(NOT TARGET JSONUtils::JSONUtils)
    include("${JSONUTILS_CMAKE_DIR}/JSONUtilsTargets.cmake")
endif()

set(JSONUTILS_lIBRARIES JSONUtils::JSONUtils)