Skip to content

Commit c806375

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 c806375

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

CMake/ITKFetchContent.cmake

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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(Module_ITKReview ON CACHE INTERNAL "")
22+
23+
# Set ITK build options
24+
set(ITK_USE_KWSTYLE OFF)
25+
set(BUILD_TESTING OFF)
26+
set(BUILD_EXAMPLES OFF)
27+
28+
FetchContent_Declare(
29+
ITK
30+
GIT_REPOSITORY "${ITK_GIT_REPOSITORY}"
31+
GIT_TAG "${ITK_GIT_TAG}"
32+
EXCLUDE_FROM_ALL
33+
FIND_PACKAGE_ARGS
34+
NAMES
35+
ITK
36+
)
37+
38+
FetchContent_MakeAvailable(ITK)
39+
40+
# Check if FetchContent used find_package() or fetched from source
41+
FetchContent_GetProperties(ITK)
42+
if(ITK_SOURCE_DIR)
43+
message(STATUS "ITK fetched from repository and built from source")
44+
message(STATUS " Source directory: ${ITK_SOURCE_DIR}")
45+
message(STATUS " Binary directory: ${ITK_BINARY_DIR}")
46+
set(ITK_DIR "${ITK_BINARY_DIR}")
47+
48+
include(${ITK_DIR}/ITKConfig.cmake)
49+
elseif(DEFINED ITK_FOUND)
50+
message(STATUS "ITK found via find_package()")
51+
# ITK_DIR should already be set by find_package()
52+
else()
53+
message(FATAL_ERROR "ITK configuration failed - no targets available")
54+
endif()
55+
56+
# These ITK options conflict with ITKSphinxExamples build options.
57+
# Allow ITKSphinxExamples' cache variables to be respected.
58+
unset(BUILD_TESTING)
59+
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)