FetchContent_MakeAvailable in export configuration

Hello,

Say I want to create an export configuration for a library with a dependency provided by FetchContent_MakeAvailable, like the following:

Include(FetchContent)

FetchContent_Declare(
    fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt.git
    GIT_TAG        10.2.1 # or a later release
)

FetchContent_MakeAvailable(fmt)

...

target_link_libraries(${module_name} PRIVATE fmt::fmt)

How should I write my export configuration file? Is it good practice to simply use FetchContent_MakeAvailable also in the config file like this:

@PACKAGE_INIT@
Include(FetchContent)

FetchContent_Declare(
    fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt.git
    GIT_TAG        10.2.1 # or a later release
)

FetchContent_MakeAvailable(fmt)

include(${CMAKE_CURRENT_LIST_DIR}/mylib-config-version.cmake)

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

?

My worry is that the fetched repository version for the exported configuration may be different that the original one used for building my library (if the repository was updated between the original build of my library and its import in another project). Is there a solution / another way to handle this?

Thanks

I may do it like at the ModernCppStarter project.

Actually, the correct solution seems more to install the dependency like any other target like this:

install(TARGETS fmt EXPORT ${project}-targets
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

According to https://stackoverflow.com/questions/71541589/how-to-exporting-targets-fetched-with-fetchcontent

But I get an

The following imported targets are referenced, but are missing:
  fmt::fmt-header-only

when I try to use my package in another project.

CPMAddPackage(
  NAME fmt
  GIT_TAG 10.2.1
  GITHUB_REPOSITORY fmtlib/fmt
  SYSTEM ON # used in case of cmake v3.25
  OPTIONS "FMT_INSTALL YES" # create an installable target
)

Most projects does install itself only configured as subproject!

Thanks, but for some reason, even using CPMAddPackage, I still got the following error when I try to import my package in another project:

CMake Error at CMakeLists.txt:33 (find_package):
  Found package configuration file:

    ......./install/lib/cmake/mylib/mylib-config.cmake

  but it set mylib_FOUND to FALSE so package "mylib" is considered
  to be NOT FOUND.  Reason given by package:

  The following imported targets are referenced, but are missing: fmt::fmt

Actually, an installable target is indeed created for fmt, in install/cmake/lib/cmake/fmt, along with install/cmake/lib/cmake/mylib.

But in a new project, I would expect that find_package(mylib) would handle this dependency. Perhaps I am doing something wrong in the install config of mylib?

It works, try it out please:

cmake --worklow --preset default
pushd test
cmake --worklow --preset default

Where is your fmt package installed?