From 31a2c26440b2ca54febd0308c1a3533787d9449d Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Mon, 13 Jul 2026 20:45:20 +0000 Subject: [PATCH] COMP: Use single export() call to fix namespace corruption Replacing three APPEND export() calls with one non-APPEND call fixes a namespace corruption bug where CMake prepends the DCMTK:: namespace to dependency targets (e.g. ITK codec targets) that belong to other export sets. CMake's HandleMissingTarget only searches other registered export sets when not in append mode. In append mode it blindly prepends the current namespace, producing DCMTK::ITK::ITKZLIBModule instead of the correct ITK::ITKZLIBModule in DCMTKTargets.cmake. The file(WRITE)+3xAPPEND pattern was used only to accumulate targets across multiple calls; combining them into a single export() call is equivalent and avoids the append-mode shortcut. --- CMake/GenerateCMakeExports.cmake | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/CMake/GenerateCMakeExports.cmake b/CMake/GenerateCMakeExports.cmake index b4f44e460c..2fff699f88 100644 --- a/CMake/GenerateCMakeExports.cmake +++ b/CMake/GenerateCMakeExports.cmake @@ -14,19 +14,22 @@ # DCMTKConfigVersion.cmake provides checking of DCMTK version compatibility # DCMTKConfig.cmake will contain options used to build this DCMTK package -# Start with clean DCMTKTargets.cmake and fill it by appending -file(WRITE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" "") - -# Get and store all executable targets to DCMTKTargets.cmake within build's main dir +# Get all targets to export into a single list so that export() runs without +# APPEND. A single non-APPEND call allows CMake to resolve dependency targets +# that live in other export sets (e.g. ITK codec targets) using those sets' +# own namespaces, rather than blindly prepending the DCMTK:: namespace. get_property(DCMTK_EXECUTABLE_TARGETS GLOBAL PROPERTY DCMTK_EXECUTABLE_TARGETS) -export(TARGETS ${DCMTK_EXECUTABLE_TARGETS} APPEND FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" NAMESPACE DCMTK::) - -# Get and store libraries to DCMTKTargets.cmake within the build's main dir get_property(DCMTK_LIBRARY_TARGETS GLOBAL PROPERTY DCMTK_LIBRARY_TARGETS) -export(TARGETS config ${DCMTK_LIBRARY_TARGETS} APPEND FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" NAMESPACE DCMTK::) - -# Add interface library for conveniently linking to all libraries via DCMTK::DCMTK -export(TARGETS DCMTK APPEND FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" NAMESPACE DCMTK::) +set(_dcmtk_all_export_targets + config + ${DCMTK_LIBRARY_TARGETS} + DCMTK + ${DCMTK_EXECUTABLE_TARGETS} +) +export(TARGETS ${_dcmtk_all_export_targets} + FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" + NAMESPACE DCMTK::) +unset(_dcmtk_all_export_targets) # Create DCMTKConfigVersion.cmake with basic DCMTK version information (build tree) set(DCMTK_CONFIG_VERSION "${CMAKE_BINARY_DIR}/DCMTKConfigVersion.cmake")