Skip to content

Commit d0bd6aa

Browse files
committed
refactor: Replace find_package(ITK) with FetchContent integration
- Replace direct find_package(ITK) with CMake/ITKFetchContent.cmake - FetchContent intelligently finds pre-built ITK via ITK_DIR or downloads - Fix itk_module_impl condition: use itk-module variable instead of COMMAND to properly distinguish between superbuild/module context vs standalone
1 parent 8d48e77 commit d0bd6aa

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

CMake/ITKFetchContent.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#-----------------------------------------------------------------------------
2+
# Get and build ITK using FetchContent
3+
4+
include(FetchContent)
5+
6+
# Set ITK Git repository and tag
7+
set(ITK_GIT_REPOSITORY "https://github.com/InsightSoftwareConsortium/ITK.git")
8+
set(ITK_GIT_TAG "main")
9+
10+
if(NOT DEFINED ITK_BUILD_DEFAULT_MODULES)
11+
set(ITK_BUILD_DEFAULT_MODULES ON)
12+
endif()
13+
14+
# Configure ITK-specific options that need to be set before FetchContent_Declare
15+
if(NOT ${BUILD_SHARED_LIBS})
16+
set(CMAKE_C_VISIBILITY_PRESET "hidden")
17+
set(CMAKE_CXX_VISIBILITY_PRESET "hidden")
18+
set(ITK_TEMPLATE_VISIBILITY_DEFAULT OFF)
19+
endif()
20+
21+
set(Modile_ITKReview ON CACHE INTERNAL "")
22+
set(Module_SphinxExamples OFF CACHE INTERNAL "")
23+
24+
# Set ITK build options
25+
set(ITK_USE_KWSTYLE OFF)
26+
set(BUILD_TESTING OFF)
27+
set(BUILD_EXAMPLES OFF)
28+
29+
FetchContent_Declare(
30+
ITK
31+
GIT_REPOSITORY "${ITK_GIT_REPOSITORY}"
32+
GIT_TAG "${ITK_GIT_TAG}"
33+
EXCLUDE_FROM_ALL
34+
FIND_PACKAGE_ARGS
35+
NAMES
36+
ITK
37+
)
38+
39+
FetchContent_MakeAvailable(ITK)
40+
41+
# Check if FetchContent used find_package() or fetched from source
42+
FetchContent_GetProperties(ITK)
43+
if(ITK_SOURCE_DIR)
44+
message(STATUS "ITK fetched from repository and built from source")
45+
message(STATUS " Source directory: ${ITK_SOURCE_DIR}")
46+
message(STATUS " Binary directory: ${ITK_BINARY_DIR}")
47+
set(ITK_DIR "${ITK_BINARY_DIR}")
48+
49+
include(${ITK_DIR}/ITKConfig.cmake)
50+
elseif(DEFINED ITK_FOUND)
51+
message(STATUS "ITK found via find_package()")
52+
# ITK_DIR should already be set by find_package()
53+
else()
54+
message(FATAL_ERROR "ITK configuration failed - no targets available")
55+
endif()
56+
57+
# These ITK options conflict with ITKSphinxExamples build options.
58+
# Allow ITKSphinxExamples' cache variables to be respected.
59+
unset(BUILD_TESTING)
60+
unset(BUILD_EXAMPLES)

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ endif()
7979
#Optional if we are building as an ITK module, these variables will be pre-defined
8080
if(NOT ITK_VERSION)
8181
# To get the ITK version information from external package when building
82-
# stand alone, ITK is required.
83-
find_package(ITK 5.4.2 REQUIRED)
82+
# stand alone, ITK is required. ITK can be provided via ITK_DIR or fetched
83+
# from source using FetchContent.
84+
include(${CMAKE_CURRENT_SOURCE_DIR}/CMake/ITKFetchContent.cmake)
8485
endif()
8586
set(ITKSphinxExamples_VERSION_MAJOR ${ITK_VERSION_MAJOR})
8687
set(ITKSphinxExamples_VERSION_MINOR ${ITK_VERSION_MINOR})
@@ -308,7 +309,8 @@ configure_file("${ITKSphinxExamples_SOURCE_DIR}/Utilities/CreateNewExample.py.in
308309

309310
# Build the example executables?
310311
option(BUILD_EXECUTABLES "Build the example executables." ON)
311-
if(COMMAND itk_module_impl)
312+
# Use itk-module (set by ITK's module system) to detect if we are building as an ITK module or stand alone.
313+
if(itk-module)
312314
itk_module_impl()
313315
else()
314316
if(BUILD_EXECUTABLES)

0 commit comments

Comments
 (0)