Skip to content

Fix OpenJPEG target handling in DCMTKConfig.cmake#145

Open
lassoan wants to merge 1 commit intoDCMTK:masterfrom
lassoan:fix-dcmtkconfig-with-openjpeg
Open

Fix OpenJPEG target handling in DCMTKConfig.cmake#145
lassoan wants to merge 1 commit intoDCMTK:masterfrom
lassoan:fix-dcmtkconfig-with-openjpeg

Conversation

@lassoan
Copy link
Copy Markdown

@lassoan lassoan commented Apr 3, 2026

When DCMTK is built with OpenJPEG support, DCMTKTargets.cmake contains
INTERFACE_LINK_LIBRARIES entries that reference the imported target
openjp2. If a consuming project includes DCMTKConfig.cmake without
previously finding OpenJPEG, CMake errors out because the openjp2
target does not exist.

Use CMakeFindDependencyMacro to conditionally call find_dependency(OpenJPEG)
when DCMTK_WITH_OPENJPEG is enabled. This guarantees that the openjp2
target is defined before DCMTKTargets.cmake is processed,
while avoiding redundant lookups in projects that have already found
OpenJPEG themselves.

@michaelonken
Copy link
Copy Markdown
Member

@eichelberg This is probably something for you.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a CMake package-consumption issue when DCMTK is built with OpenJPEG support: importing DCMTKTargets.cmake can fail if the consuming project hasn’t already created the openjp2 imported target.

Changes:

  • Add a guarded find_package(OpenJPEG ...) call in DCMTKConfig.cmake generation so openjp2 is defined before importing DCMTK targets.
  • Only perform the OpenJPEG lookup when DCMTK was built with OpenJPEG and the openjp2 target does not already exist.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CMake/DCMTKConfig.cmake.in Outdated
set(DCMTK_INCLUDE_DIR ${DCMTK_INCLUDE_DIRS})

if(DCMTK_WITH_OPENJPEG AND NOT TARGET openjp2)
find_package(OpenJPEG CONFIG QUIET)
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_package(OpenJPEG CONFIG QUIET) suppresses why OpenJPEG wasn’t found; if this call fails, include(DCMTKTargets.cmake) will still error later due to a missing openjp2 target, but with a less actionable message. Consider using REQUIRED or checking the result and emitting a clearer FATAL_ERROR that suggests setting OpenJPEG_DIR when DCMTK was built with OpenJPEG support.

Suggested change
find_package(OpenJPEG CONFIG QUIET)
find_package(OpenJPEG CONFIG QUIET)
if(NOT TARGET openjp2)
message(FATAL_ERROR
"DCMTK was built with OpenJPEG support, but the OpenJPEG package "
"configuration could not be found. Please make OpenJPEG available to "
"CMake, for example by setting OpenJPEG_DIR to the directory "
"containing OpenJPEGConfig.cmake or by adjusting CMAKE_PREFIX_PATH.")
endif()

Copilot uses AI. Check for mistakes.
When DCMTK is built with OpenJPEG support, DCMTKTargets.cmake contains
INTERFACE_LINK_LIBRARIES entries that reference the imported target
openjp2. If a consuming project includes DCMTKConfig.cmake without
previously finding OpenJPEG, CMake errors out because the openjp2
target does not exist.

Use CMakeFindDependencyMacro to conditionally call find_dependency(OpenJPEG)
when DCMTK_WITH_OPENJPEG is enabled. This guarantees that the openjp2
target is defined before DCMTKTargets.cmake is processed,
while avoiding redundant lookups in projects that have already found
OpenJPEG themselves.
@lassoan lassoan force-pushed the fix-dcmtkconfig-with-openjpeg branch from 7f051c5 to 6b73475 Compare April 14, 2026 00:34
@lassoan
Copy link
Copy Markdown
Author

lassoan commented Apr 14, 2026

@eichelberg please have a look. Without this, downstream projects need to add find_package calls depending on what DCMTK modules are enabled.

Note that the same problem is fixed for OpenSSL and zlib packages here: #67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants