Skip to content

CMAKE: Migrate to ITK 6 modern interface library targets#1435

Draft
blowekamp wants to merge 1 commit intoSuperElastix:mainfrom
blowekamp:itk6-modern-cmake-interface-libs
Draft

CMAKE: Migrate to ITK 6 modern interface library targets#1435
blowekamp wants to merge 1 commit intoSuperElastix:mainfrom
blowekamp:itk6-modern-cmake-interface-libs

Conversation

@blowekamp
Copy link
Copy Markdown
Contributor

Replace legacy include(${ITK_USE_FILE}) and ${ITK_LIBRARIES} patterns with modern CMake target-based dependencies using ITK::Module interface library targets. Backward compatibility with ITK 5.4.1+ is preserved via version-conditional logic.

New file: CMake/ElastixITKDeps.cmake

  • elastix_link_itk() macro: expands to ITK::Module targets (ITK 6+) or ${ITK_LIBRARIES} (ITK 5.x).
  • ELASTIX_ITK_EXECUTABLE_LIBRARIES: IO factory meta-module targets (ITK::ITKImageIO, ITK::ITKTransformIO, ITK::ITKMeshIO) that carry factory registration include dirs and compile definitions via INTERFACE properties.
  • ELASTIX_ITK_TEST_LIBRARIES: ITK::ITKTestKernelModule for test-only utilities.
  • ELASTIX_ITK_ALL_LIBRARIES: full ${ITK_INTERFACE_LIBRARIES} + factory meta-modules, for standalone executables without elxCommon transitive deps.

Top-level CMakeLists.txt:

  • ITK 6+: call itk_generate_factory_registration() before add_subdirectory().
  • ITK 5.x: keep include(${ITK_USE_FILE}).
  • include(ElastixITKDeps) to load the helper.

Library target updates (elastix_link_itk() with WhatModulesITK.py-verified lists):

  • param: ITKCommon
  • elxCommon: 25 specific ITK::*Module targets (ITKCommon, ITKDisplacementField, ITKDistanceMap, ITKImageCompose, ITKImageFilterBase, ITKImageFunction, ITKImageGradient, ITKImageGrid, ITKImageIntensity, ITKImageStatistics, ITKIOImageBase, ITKIOMeshBase, ITKIOMeta, ITKIOTransformBase, ITKIOXML, ITKMathematicalMorphology, ITKMesh, ITKOptimizers, ITKRegistrationCommon, ITKSmoothing, ITKSpatialObjects, ITKStatistics, ITKThresholding, ITKTransform, ITKTransformFactory)
  • mevisdcmtiff: ITKCommon, ITKGDCM, ITKIOImageBase
  • elxOpenCL: ITKCommon, ITKGPUCommon, ITKTransform, ITKImageGrid, ITKSmoothing, ITKImageFunction
  • KNNlib: ITKCommon, ITKStatistics, ITKSpatialObjects

Components/Core/Tests:

  • elxLinkLibs: ITK 6+ = elxCore only (all ITK deps come transitively via elxCommon PUBLIC link chain); ITK 5.x = elxCore + ${ITK_LIBRARIES}.
  • ELASTIX_TARGET_LINK_LIBRARIES: uses ELASTIX_ITK_EXECUTABLE_LIBRARIES.
  • Test macro and utility targets: ELASTIX_ITK_EXECUTABLE_LIBRARIES + ELASTIX_ITK_TEST_LIBRARIES. GTest targets: ELASTIX_ITK_EXECUTABLE_LIBRARIES.

Config/downstream:

  • ElastixConfig.cmake.in: calls itk_generate_factory_registration() for ITK 6+.
  • UseElastix.cmake.in: note recommending direct target linking for ITK 6+.
  • dox/externalproject: ITK 6+ links only elastix_lib (all ITK deps transitive).

Verified with ITK 6.0.0: 159/163 tests pass (4 pre-existing failures unrelated to this change: numerical baseline mismatches vs ITK 5 baselines, missing Python itk module in test environment).

Replace legacy include(${ITK_USE_FILE}) and ${ITK_LIBRARIES} patterns
with modern CMake target-based dependencies using ITK::<Module>Module interface
library targets. Backward compatibility with ITK 5.4.1+ is preserved via
version-conditional logic.

New file: CMake/ElastixITKDeps.cmake
- elastix_link_itk() macro: expands to ITK::<Mod>Module targets (ITK 6+)
  or ${ITK_LIBRARIES} (ITK 5.x).
- ELASTIX_ITK_EXECUTABLE_LIBRARIES: IO factory meta-module targets
  (ITK::ITKImageIO, ITK::ITKTransformIO, ITK::ITKMeshIO) that carry factory
  registration include dirs and compile definitions via INTERFACE properties.
- ELASTIX_ITK_TEST_LIBRARIES: ITK::ITKTestKernelModule for test-only utilities.
- ELASTIX_ITK_ALL_LIBRARIES: full ${ITK_INTERFACE_LIBRARIES} + factory
  meta-modules, for standalone executables without elxCommon transitive deps.

Top-level CMakeLists.txt:
- ITK 6+: call itk_generate_factory_registration() before add_subdirectory().
- ITK 5.x: keep include(${ITK_USE_FILE}).
- include(ElastixITKDeps) to load the helper.

Library target updates (elastix_link_itk() with WhatModulesITK.py-verified lists):
- param: ITKCommon
- elxCommon: 25 specific ITK::*Module targets (ITKCommon, ITKDisplacementField,
  ITKDistanceMap, ITKImageCompose, ITKImageFilterBase, ITKImageFunction,
  ITKImageGradient, ITKImageGrid, ITKImageIntensity, ITKImageStatistics,
  ITKIOImageBase, ITKIOMeshBase, ITKIOMeta, ITKIOTransformBase, ITKIOXML,
  ITKMathematicalMorphology, ITKMesh, ITKOptimizers, ITKRegistrationCommon,
  ITKSmoothing, ITKSpatialObjects, ITKStatistics, ITKThresholding,
  ITKTransform, ITKTransformFactory)
- mevisdcmtiff: ITKCommon, ITKGDCM, ITKIOImageBase
- elxOpenCL: ITKCommon, ITKGPUCommon, ITKTransform, ITKImageGrid,
  ITKSmoothing, ITKImageFunction
- KNNlib: ITKCommon, ITKStatistics, ITKSpatialObjects

Components/Core/Tests:
- elxLinkLibs: ITK 6+ = elxCore only (all ITK deps come transitively via
  elxCommon PUBLIC link chain); ITK 5.x = elxCore + ${ITK_LIBRARIES}.
- ELASTIX_TARGET_LINK_LIBRARIES: uses ELASTIX_ITK_EXECUTABLE_LIBRARIES.
- Test macro and utility targets: ELASTIX_ITK_EXECUTABLE_LIBRARIES +
  ELASTIX_ITK_TEST_LIBRARIES. GTest targets: ELASTIX_ITK_EXECUTABLE_LIBRARIES.

Config/downstream:
- ElastixConfig.cmake.in: calls itk_generate_factory_registration() for ITK 6+.
- UseElastix.cmake.in: note recommending direct target linking for ITK 6+.
- dox/externalproject: ITK 6+ links only elastix_lib (all ITK deps transitive).

Verified with ITK 6.0.0: 159/163 tests pass (4 pre-existing failures unrelated
to this change: numerical baseline mismatches vs ITK 5 baselines, missing Python
itk module in test environment).
@blowekamp
Copy link
Copy Markdown
Contributor Author

Relates to: SimpleITK/SimpleITK#2569

@blowekamp
Copy link
Copy Markdown
Contributor Author

@N-Dekker This is a AI driven patch. Please let me know what you think of the general approach of adding the cmake function to fill in the gap between the ITK versions or other opinions on the app read before I look at things more closely.

@blowekamp
Copy link
Copy Markdown
Contributor Author

@N-Dekker Any thought on this PR, and how you might want this type if change implemented?

@N-Dekker
Copy link
Copy Markdown
Member

Thanks @blowekamp I'll have a look!

I very much appreciate your help with CMake. And I realize that the CMake code of elastix may be outdated.

Would it be possible to do such a migration with less lines of code? I see now that your draft adds 200+ lines. In general, I would expect modernization to reduce the number of lines of code, instead. 🤷

@blowekamp
Copy link
Copy Markdown
Contributor Author

Would it be possible to do such a migration with less lines of code? I see now that your draft adds 200+ lines. In general, I would expect modernization to reduce the number of lines of code, instead. 🤷

There are two primary things done.

  • Each library can link against the specific ITK targets or modules that is used. By doing the include paths, defines and library linking is more specific and efficient. This does add some code for each library with this additional specification. This additional usage specification is a good thing, but Elastix could still like to ALL ITK libraries with by using something like ITK_INTERFACE_LIBRARIES.
  • A function is added to provide compatibility with v6. This does add a 100+ lines of code ( 50 comments which could be deleted/reduced) I think this is the best way to provide interoperability with ITKv6 and prior version. However, an v6+ only version would not need this function.

There some other AI written comment/documentation that could be reduced to. I'm not sure if they help or hinder the review process.

@blowekamp
Copy link
Copy Markdown
Contributor Author

@N-Dekker Do you have any preference or specific requests to move this PR forward?

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.

2 participants