From 4e5ff510f8234a6e85b86a9e2c96f3a07d2af16b Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 22 Jul 2026 11:24:25 -0700 Subject: [PATCH] Simplify build-tree export and rename INSTALL_TARGETS Rename INSTALL_TARGETS to FMT_TARGETS since the list is now used for both installation and the build-tree export, and move INSTALL_FILE_SET into the FMT_INSTALL block as only install(TARGETS) uses it. Replace the export-test static library and export-lib.cc with an INTERFACE library, which reproduces the same CMake export dependency check (#4806). --- CMakeLists.txt | 22 +++++++++++----------- test/add-subdirectory-test/CMakeLists.txt | 8 +++----- test/add-subdirectory-test/export-lib.cc | 3 --- 3 files changed, 14 insertions(+), 19 deletions(-) delete mode 100644 test/add-subdirectory-test/export-lib.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fd7662d..3b317942 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -442,13 +442,10 @@ set_target_properties(fmt-c PROPERTIES PUBLIC_HEADER include/fmt/fmt-c.h) # Install targets. set(targets_export_name fmt-targets) -set(INSTALL_TARGETS fmt fmt-header-only fmt-c) +set(FMT_TARGETS fmt fmt-header-only fmt-c) if (FMT_MODULE) - list(APPEND INSTALL_TARGETS fmt-module) - if (FMT_USE_CMAKE_MODULES) - set(INSTALL_FILE_SET FILE_SET fmt DESTINATION ${FMT_INC_DIR}/fmt) - endif () + list(APPEND FMT_TARGETS fmt-module) endif () if (FMT_INSTALL) @@ -495,8 +492,11 @@ if (FMT_INSTALL) INSTALL_DESTINATION ${FMT_CMAKE_DIR}) # Install the library and headers. + if (FMT_MODULE AND FMT_USE_CMAKE_MODULES) + set(INSTALL_FILE_SET FILE_SET fmt DESTINATION ${FMT_INC_DIR}/fmt) + endif () install( - TARGETS ${INSTALL_TARGETS} + TARGETS ${FMT_TARGETS} COMPONENT fmt_core EXPORT ${targets_export_name} LIBRARY DESTINATION ${FMT_LIB_DIR} @@ -521,12 +521,12 @@ if (FMT_INSTALL) COMPONENT fmt_core) endif () -# Generate an export file in the build tree so that fmt can be used by -# add_subdirectory() or FetchContent without being installed. Use a -# namespace because CMake provides better diagnostics for namespaced -# imported targets. +# Generate a build-tree export so that targets depending on fmt can also be +# exported without installing fmt, including when fmt is provided through +# add_subdirectory() or FetchContent. Use a namespace because CMake provides +# better diagnostics for namespaced imported targets. export( - TARGETS ${INSTALL_TARGETS} + TARGETS ${FMT_TARGETS} NAMESPACE fmt:: FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake) diff --git a/test/add-subdirectory-test/CMakeLists.txt b/test/add-subdirectory-test/CMakeLists.txt index 65be2525..ce4993eb 100644 --- a/test/add-subdirectory-test/CMakeLists.txt +++ b/test/add-subdirectory-test/CMakeLists.txt @@ -16,11 +16,9 @@ if (TARGET fmt::fmt-header-only) target_link_libraries(header-only-test fmt::fmt-header-only) endif () -# Test that a target publicly depending on fmt can itself be exported even -# though fmt is added via add_subdirectory and FMT_INSTALL is off (#4806). -add_library(export-test STATIC export-lib.cc) -target_link_libraries(export-test PUBLIC fmt::fmt) +# Test that a target depending on fmt can itself be exported (#4806). +add_library(export-test INTERFACE) +target_link_libraries(export-test INTERFACE fmt::fmt) export( TARGETS export-test - NAMESPACE test:: FILE ${CMAKE_CURRENT_BINARY_DIR}/export-test-targets.cmake) diff --git a/test/add-subdirectory-test/export-lib.cc b/test/add-subdirectory-test/export-lib.cc deleted file mode 100644 index e8adbb1c..00000000 --- a/test/add-subdirectory-test/export-lib.cc +++ /dev/null @@ -1,3 +0,0 @@ -#include "fmt/base.h" - -void greet(const char* name) { fmt::print("Hello, {}!\n", name); }