Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions CMake/ElastixITKDeps.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#
# ElastixITKDeps.cmake
#
# Provides a helper macro and convenience variables for linking ITK dependencies
# using modern CMake interface library targets (ITK 6+) or the legacy
# ${ITK_LIBRARIES} variable (ITK 5.x), maintaining backward compatibility.
#
# Must be included AFTER find_package(ITK ...) and, for ITK 6+, after
# itk_generate_factory_registration() has been called so that factory
# meta-module target properties are properly configured.
#
# Variables set by this file
# --------------------------
#
# ELASTIX_ITK_EXECUTABLE_LIBRARIES
# For executables that already carry ITK processing module dependencies
# transitively via elxCommon (e.g. elastix_exe, transformix_exe, GTest targets).
# ITK 6+: IO factory meta-module targets (ITK::ITKImageIO, etc.) that provide
# the factory registration include directories and compile definitions.
# ITK 5.x: ${ITK_LIBRARIES} (factory registration handled globally by UseITK.cmake).
#
# ELASTIX_ITK_ALL_LIBRARIES
# For standalone executables that do NOT link elxCommon/elxCore and therefore
# need explicit ITK processing module dependencies as well as IO factories
# (e.g. elxComputeOverlap, elxImageCompare, external project examples).
# ITK 6+: ${ITK_INTERFACE_LIBRARIES} (all requested module interface libs)
# plus IO factory meta-module targets.
# ITK 5.x: ${ITK_LIBRARIES}.
#
# Macro provided
# --------------
#
# elastix_link_itk(<target> [PUBLIC|PRIVATE|INTERFACE] MODULES <mod1> [<mod2> ...])
#
# Links <target> to specific ITK modules using:
# ITK 6+: ITK::<mod>Module interface library targets
# ITK 5.x: ${ITK_LIBRARIES} (all loaded modules)
#
# The optional scope keyword (PUBLIC / PRIVATE / INTERFACE) controls CMake
# link visibility and may be omitted for old-style plain linkage.
#
# Example (library that exposes ITK types in its public headers):
# elastix_link_itk(elxCommon PUBLIC MODULES
# ITKCommon ITKTransform ITKOptimizers ITKRegistrationCommon)
#
# Example (internal library with no public ITK API):
# elastix_link_itk(param MODULES ITKCommon)
#

if(ITK_VERSION_MAJOR GREATER_EQUAL 6)
# Collect IO factory meta-module targets configured by itk_generate_factory_registration().
# These carry INTERFACE_INCLUDE_DIRECTORIES pointing to the generated factory
# registration headers and INTERFACE_COMPILE_DEFINITIONS enabling the managers.
set(ELASTIX_ITK_EXECUTABLE_LIBRARIES "")
foreach(_elx_meta ITKImageIO ITKTransformIO ITKMeshIO)
if(TARGET ITK::${_elx_meta})
list(APPEND ELASTIX_ITK_EXECUTABLE_LIBRARIES ITK::${_elx_meta})
endif()
endforeach()
unset(_elx_meta)

# ITKTestKernel provides test utilities (itkTestingComparisonImageFilter.h, etc.).
# Linked only to test targets; not added to production libraries.
if(TARGET ITK::ITKTestKernelModule)
set(ELASTIX_ITK_TEST_LIBRARIES ITK::ITKTestKernelModule)
else()
set(ELASTIX_ITK_TEST_LIBRARIES "")
endif()

# Full ITK: all requested module interface libraries + IO factory meta-modules.
# ITK_INTERFACE_LIBRARIES is set by itk_module_config() in ITKConfig.cmake
# and contains one ITK::<mod>Module target per loaded ITK module.
set(ELASTIX_ITK_ALL_LIBRARIES ${ITK_INTERFACE_LIBRARIES} ${ELASTIX_ITK_EXECUTABLE_LIBRARIES})

else()
# ITK 5.x: the single ${ITK_LIBRARIES} variable covers all requested modules
# and IO factories (factory registration is done globally by UseITK.cmake).
set(ELASTIX_ITK_EXECUTABLE_LIBRARIES ${ITK_LIBRARIES})
set(ELASTIX_ITK_TEST_LIBRARIES "")
set(ELASTIX_ITK_ALL_LIBRARIES ${ITK_LIBRARIES})
endif()

#
# elastix_link_itk(<target> [PUBLIC|PRIVATE|INTERFACE] MODULES <mod1> [<mod2> ...])
#
macro(elastix_link_itk _elt_target)
cmake_parse_arguments(_elt "" "" "MODULES" ${ARGN})
# _elt_UNPARSED_ARGUMENTS holds an optional scope keyword (PUBLIC/PRIVATE/INTERFACE).
if(ITK_VERSION_MAJOR GREATER_EQUAL 6)
set(_elt_itk_link_targets "")
foreach(_elt_mod ${_elt_MODULES})
list(APPEND _elt_itk_link_targets ITK::${_elt_mod}Module)
endforeach()
target_link_libraries(${_elt_target} ${_elt_UNPARSED_ARGUMENTS} ${_elt_itk_link_targets})
unset(_elt_itk_link_targets)
unset(_elt_mod)
else()
target_link_libraries(${_elt_target} ${_elt_UNPARSED_ARGUMENTS} ${ITK_LIBRARIES})
endif()
unset(_elt_MODULES)
unset(_elt_UNPARSED_ARGUMENTS)
endmacro()
19 changes: 18 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ find_package(ITK 5.4.1 REQUIRED COMPONENTS
${_GPU_depends}
${_ITK_io_depends}
)
include(${ITK_USE_FILE})

if(ITK_VERSION_MAJOR GREATER_EQUAL 6)
# ITK 6+: use modern interface library targets.
# itk_generate_factory_registration() configures the factory registration
# headers and sets INTERFACE properties on the factory meta-module targets
# (ITK::ITKImageIO, ITK::ITKTransformIO, ITK::ITKMeshIO).
# Must be called before any add_subdirectory() so the target properties are
# ready when subdirectory CMakeLists.txt files reference them.
itk_generate_factory_registration()
else()
# ITK 5.x: UseITK.cmake sets global include/link directories and calls
# itk_generate_factory_registration() internally.
include(${ITK_USE_FILE})
endif()

#---------------------------------------------------------------------
# Add the CMake dir as an additional module path,
Expand All @@ -100,6 +113,10 @@ include(elastixVersion)
# Function for exporting targets
include(elastixExportTarget)

# ITK dependency helper: provides elastix_link_itk() macro and
# ELASTIX_ITK_EXECUTABLE_LIBRARIES / ELASTIX_ITK_ALL_LIBRARIES variables.
include(ElastixITKDeps)

if (MSVC)
add_compile_options(/W3)
else()
Expand Down
28 changes: 26 additions & 2 deletions Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,32 @@ endif()
#---------------------------------------------------------------------
# Link against other libraries.

target_link_libraries(elxCommon
${ITK_LIBRARIES}
elastix_link_itk(elxCommon PUBLIC MODULES
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
)

target_include_directories(elxCommon PUBLIC
Expand Down
4 changes: 2 additions & 2 deletions Common/GTesting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ if (ITK_VERSION VERSION_LESS_EQUAL 5.4.5)
target_link_libraries(CommonGTest
GTest::GTest
GTest::Main
${ITK_LIBRARIES}
${ELASTIX_ITK_EXECUTABLE_LIBRARIES}
elastix_lib
)
else()
target_link_libraries(CommonGTest
GTest::gtest
GTest::gtest_main
${ITK_LIBRARIES}
${ELASTIX_ITK_EXECUTABLE_LIBRARIES}
elastix_lib
)
endif()
Expand Down
6 changes: 5 additions & 1 deletion Common/MevisDicomTiff/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ if(NOT ELASTIX_NO_INSTALL_DEVELOPMENT)
COMPONENT Development)
endif()

target_link_libraries(mevisdcmtiff ${ITK_LIBRARIES})
elastix_link_itk(mevisdcmtiff MODULES
ITKCommon
ITKGDCM
ITKIOImageBase
)
target_include_directories(mevisdcmtiff PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${ELASTIX_INCLUDE_DIR}/Common/MevisDicomTiff>)
Expand Down
11 changes: 8 additions & 3 deletions Common/OpenCL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ set(OpenCL_Files
add_library(elxOpenCL STATIC ${OpenCL_Files})

# Link it against the necessary libraries.
target_link_libraries(elxOpenCL
${ITK_LIBRARIES}
${OPENCL_LIBRARIES}
elastix_link_itk(elxOpenCL MODULES
ITKCommon
ITKGPUCommon
ITKTransform
ITKImageGrid
ITKSmoothing
ITKImageFunction
)
target_link_libraries(elxOpenCL ${OPENCL_LIBRARIES})

target_include_directories(elxOpenCL PUBLIC ${OPENCL_INCLUDE_DIRS})
target_compile_definitions(elxOpenCL PUBLIC ELASTIX_USE_OPENCL)
Expand Down
4 changes: 3 additions & 1 deletion Common/ParameterFileParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ if(NOT ELASTIX_NO_INSTALL_DEVELOPMENT)
COMPONENT Development)
endif()

target_link_libraries(param ${ITK_LIBRARIES})
elastix_link_itk(param MODULES
ITKCommon
)
target_include_directories(param PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${ELASTIX_INCLUDE_DIR}/Common/ParameterFileParser>)
Expand Down
20 changes: 16 additions & 4 deletions Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ project(elxComponents)

#---------------------------------------------------------------------
# Set the libraries to be linked.

set(elxLinkLibs
elxCore
${ITK_LIBRARIES})
#
# For ITK 6+, component libraries inherit all ITK processing module dependencies
# transitively via elxCore -> elxCommon (PUBLIC link). No additional direct
# ITK module link is needed here.
# For ITK 5.x, ${ITK_LIBRARIES} provides full coverage (factory registration
# is handled globally by UseITK.cmake).
if(ITK_VERSION_MAJOR GREATER_EQUAL 6)
set(elxLinkLibs
elxCore
)
else()
set(elxLinkLibs
elxCore
${ITK_LIBRARIES}
)
endif()

#--------------------------------------------------------------------
# Prepare elxInstallComponentFunctionDeclarations.h and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ add_subdirectory(ann_1.1)
add_library(KNNlib STATIC ${KNN_Files})

# Link it against the necessary libraries.
target_link_libraries(KNNlib ANNlib ${ITK_LIBRARIES})
elastix_link_itk(KNNlib MODULES
ITKCommon
ITKStatistics
ITKSpatialObjects
)
target_link_libraries(KNNlib ANNlib)

# Group in IDE's like Visual Studio
set_property(TARGET KNNlib PROPERTY FOLDER "libraries")
9 changes: 8 additions & 1 deletion Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,20 @@ elastix_export_target(param)
# Define the mevis dcm tiff lib to which we should link.
set(mevisdcmtifflib mevisdcmtiff)

# For ITK 6+: executables link to the IO factory meta-module targets which carry
# INTERFACE_INCLUDE_DIRECTORIES pointing to the generated factory registration
# headers (itkImageIOFactoryRegisterManager.h, etc.) and INTERFACE_COMPILE_DEFINITIONS
# enabling the registration managers. ELASTIX_ITK_EXECUTABLE_LIBRARIES is set
# by CMake/ElastixITKDeps.cmake (included from the top-level CMakeLists.txt).
# For ITK 5.x: ${ITK_LIBRARIES} is used (factory registration handled globally
# by UseITK.cmake via directory-scoped COMPILE_DEFINITIONS).
set(ELASTIX_TARGET_LINK_LIBRARIES
param
elxCommon
elxCore
${mevisdcmtifflib}
${AllComponentLibs}
${ITK_LIBRARIES}
${ELASTIX_ITK_EXECUTABLE_LIBRARIES}
)
if(ELASTIX_USE_OPENCL)
set(ELASTIX_TARGET_LINK_LIBRARIES ${ELASTIX_TARGET_LINK_LIBRARIES} elxOpenCL)
Expand Down
4 changes: 2 additions & 2 deletions Core/Main/GTesting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ if (ITK_VERSION VERSION_LESS_EQUAL 5.4.5)
GTest::Main
elastix_lib
transformix_lib
${ITK_LIBRARIES}
${ELASTIX_ITK_EXECUTABLE_LIBRARIES}
)
else()
target_link_libraries(ElastixLibGTest
GTest::gtest
GTest::gtest_main
elastix_lib
transformix_lib
${ITK_LIBRARIES}
${ELASTIX_ITK_EXECUTABLE_LIBRARIES}
)
endif()

Expand Down
10 changes: 10 additions & 0 deletions ElastixConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ if(NOT ITK_CONFIG_TARGETS_FILE)
find_package(ITK "@ITK_VERSION@" EXACT REQUIRED)
endif()

# For ITK 6+: generate factory registration headers and configure factory
# meta-module target properties in the downstream project's build scope.
# Guards against double-invocation if the downstream project also calls this.
if(NOT ELASTIX_ITK_FACTORY_REGISTRATION_DONE)
set(ELASTIX_ITK_FACTORY_REGISTRATION_DONE TRUE)
if(ITK_VERSION_MAJOR GREATER_EQUAL 6 AND COMMAND itk_generate_factory_registration)
itk_generate_factory_registration()
endif()
endif()


# Import ELASTIX targets.
set( ELASTIX_CONFIG_TARGETS_FILE "${_ELASTIXConfig_DIR}/ElastixTargets.cmake")
Expand Down
11 changes: 6 additions & 5 deletions Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ macro(elx_add_test_core executable_name source_name test_name group)
param # some test use the CommandLineArgumentParser
elxCommon # provides Common headers (Transforms, CostFunctions, etc.)
${mevisdcmtifflib} # is empty if not selected in CMake
${ITK_LIBRARIES}
${ELASTIX_ITK_EXECUTABLE_LIBRARIES}
${ELASTIX_ITK_TEST_LIBRARIES}
)

if(ELASTIX_USE_OPENCL)
Expand Down Expand Up @@ -420,22 +421,22 @@ endmacro()

# Create elxComputeOverlap
add_executable(elxComputeOverlap elxComputeOverlap.cxx itkCommandLineArgumentParser.cxx)
target_link_libraries(elxComputeOverlap ${ITK_LIBRARIES})
target_link_libraries(elxComputeOverlap ${ELASTIX_ITK_ALL_LIBRARIES})
set_property(TARGET elxComputeOverlap PROPERTY FOLDER "tests/Executable")

# Create elxImageCompare
add_executable(elxImageCompare elxImageCompare.cxx itkCommandLineArgumentParser.cxx)
target_link_libraries(elxImageCompare ${ITK_LIBRARIES})
target_link_libraries(elxImageCompare ${ELASTIX_ITK_ALL_LIBRARIES})
set_property(TARGET elxImageCompare PROPERTY FOLDER "tests/Executable")

# Create elxTransformParametersCompare
add_executable(elxTransformParametersCompare elxTransformParametersCompare.cxx itkCommandLineArgumentParser.cxx)
target_link_libraries(elxTransformParametersCompare elxCore param ${ITK_LIBRARIES})
target_link_libraries(elxTransformParametersCompare elxCore param ${ELASTIX_ITK_EXECUTABLE_LIBRARIES})
set_property(TARGET elxTransformParametersCompare PROPERTY FOLDER "tests/Executable")

# Create elxInvertTransform
add_executable(elxInvertTransform elxInvertTransform.cxx itkCommandLineArgumentParser.cxx)
target_link_libraries(elxInvertTransform elxCore param ${ITK_LIBRARIES})
target_link_libraries(elxInvertTransform elxCore param ${ELASTIX_ITK_EXECUTABLE_LIBRARIES})
set_property(TARGET elxInvertTransform PROPERTY FOLDER "tests/Executable")

#---------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions UseElastix.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
# find_package( Elastix REQUIRED )
# include( ${ELASTIX_USE_FILE} )
#
# Note: for ITK 6+, prefer linking directly to the 'elastix_lib' or
# 'transformix_lib' CMake targets instead of using this file. Modern
# targets carry include directories, compile definitions, and all ITK
# interface library dependencies as transitive INTERFACE properties,
# so include_directories() and link_directories() are no longer needed.
#

# Add include dirs
include_directories( ${ELASTIX_INCLUDE_DIRS} )
Expand Down
Loading
Loading