Skip to content

Factory registration include directory collides between targets in one project #6860

Open

Description

@hjmjohnson

itk_generate_factory_registration() stores the generated header's include directory on the shared ITK::ITKImageIO target, so in a project with more than one target requesting different IO components the last call wins and the other targets compile against the wrong registration header.

Reproducer

# CMakeLists.txt
cmake_minimum_required(VERSION 3.22.1)
project(FactoryRegistrationCollision CXX)
find_package(ITK 6 REQUIRED)       # aggregate project finds ITK once
add_subdirectory(png_example)
add_subdirectory(meta_example)
# png_example/CMakeLists.txt   (meta_example is identical with ITKIOMeta)
project(png_example CXX)
find_package(ITK REQUIRED COMPONENTS ITKCommon ITKIOImageBase ITKIOPNG)
itk_generate_factory_registration()
add_executable(png_example prog.cxx)
target_link_libraries(png_example PRIVATE ITK::ITKCommonModule ITK::ITKImageIO)

prog.cxx writes a 4x4 image to argv[1] with itk::WriteImage.

Observed

target components requested factories in binary runtime
png_example ITKIOPNG Meta fails to write .png
meta_example ITKIOMeta Meta writes .mha

png_example fails with Could not create IO object for writing file, because it was compiled against meta_example's registration header.

Expected

Each target registers the factories for the components it requested.

Cause

_itk_configure_FactoryRegisterManager in CMake/ITKFactoryRegistration.cmake writes the header to ${CMAKE_CURRENT_BINARY_DIR}/ITKFactoryRegistration and then points the meta-module target at it:

set_property(
  TARGET ${_meta_module}
  PROPERTY INTERFACE_INCLUDE_DIRECTORIES
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/ITKFactoryRegistration>"
)

The property is overwritten on each call. Two sibling subdirectories that each call find_package(ITK) themselves are unaffected, because each gets its own imported target; the collision appears once a parent scope calls find_package(ITK) and the subdirectories inherit that target.

Impact

Per-target narrowing of IO components is not usable in a single-project build. In ITKSphinxExamples, narrowing each example to the formats it reads reduces the built examples from 5.26 GB to 0.72 GB, but only works when each example is configured as its own project; in the aggregate build 62 of 558 tests fail this way.

Verified against ITK main (6.0.0) on macOS with AppleClang.

Activity

  1. blowekamp commented on Sep 14, 2026

    @blowekamp
    Member

    The ITK::ITKImageIO is a CMake target which are essentially global. So I don't think there is a way to do per directory configuration with the target.

    Some of the functions may need to be manually called, and new options or features added around here:

    if(ITK_NO_${factory_uc}_FACTORY_REGISTER_MANAGER)
    # pass generation this factory registration
    elseif(_factory_name MATCHES "IO" AND ITK_NO_IO_FACTORY_REGISTER_MANAGER)
    message(
    WARNING
    "ITK_NO_IO_FACTORY_REGISTER_MANAGER CMake variable is "
    "deprecated. Use ITK_NO_${factory_uc}_FACTORY_REGISTER_MANAGER"
    )
    else()
    _itk_configure_factoryregistermanager("${_factory_name}" "${LIST_OF_${factory_uc}_FORMATS}")
    endif()

    Alternatively the example could explicitly do the registration or create the ImageIO themselves.

  2. dzenanz commented on Sep 14, 2026

    @dzenanz
    Member

    Does this problem affect Slicer? It has many modules which use ITK, some of which are probably providing a list of modules they want.

  3. blowekamp commented on Sep 14, 2026

    @blowekamp
    Member

    Does this problem affect Slicer? It has many modules which use ITK, some of which are probably providing a list of modules they want.

    Was it possible in ITKv5 to specify which ImageIOs to use in different parts of the project? And was is respected?

    Is this a regression or a new feature/issue?

  4. hjmjohnson commented on Sep 15, 2026

    @hjmjohnson
    MemberAuthor

    Once this is fixed, revisit per-format IO narrowing in ITKSphinxExamples (InsightSoftwareConsortium/ITKSphinxExamples#481, now closed). Narrowing examples to the IO formats they use cut the built examples from 5.26 GB to 0.72 GB, but hit this bug in the aggregate build.

  5. hjmjohnson commented on Sep 16, 2026

    @hjmjohnson
    MemberAuthor

    Is this a regression or a new feature/issue

    I think it is a longstanding issue that would be nice to have, and would have been the expected behavior for someone trying to narrowly limit exposure in a binary, but not often encountered.

  6. blowekamp commented on Sep 16, 2026

    @blowekamp
    Member

    Is this a regression or a new feature/issue

    I think it is a longstanding issue that would be nice to have, and would have been the expected behavior for someone trying to narrowly limit exposure in a binary, but not often encountered.

    To be clear, ITK::ITKImageIO and other meta-modules for factories are global, beyond even the project scope. This is a hard constrain based on how CMake works. The way I interpret the request is that the "ITKImageIO" should change with different find_package calls, is not achievable.

    Would an explicit call to a cmake function for this special case meet the requirements?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions