Skip to content

Commit 35050d8

Browse files
authored
Merge pull request #467 from blowekamp/use_itk_fetchcontent
Use FetchContent to build ITK
2 parents 56b3ce0 + d536858 commit 35050d8

7 files changed

Lines changed: 75 additions & 108 deletions

File tree

.github/workflows/build-test-publish.yml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,6 @@ jobs:
6464
key: externaldata-v1-${{ hashFiles('Ex/src/**/*.cid') }}
6565
restore-keys: |
6666
externaldata-v1-
67-
68-
- name: Build ITK
69-
if: matrix.os != 'windows-2022'
70-
run: |
71-
cd ..
72-
mkdir ITK-build
73-
cd ITK-build
74-
cmake -DCMAKE_C_COMPILER:FILEPATH="${{ matrix.c-compiler }}" -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_CXX_COMPILER="${{ matrix.cxx-compiler }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.cmake-build-type }} -DBUILD_TESTING:BOOL=OFF -GNinja ../ITK
75-
ninja
76-
- name: Build ITK
77-
if: matrix.os == 'windows-2022'
78-
run: |
79-
cd ..
80-
mkdir ITK-build
81-
cd ITK-build
82-
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
83-
cmake -DCMAKE_C_COMPILER:FILEPATH="${{ matrix.c-compiler }}" -DBUILD_SHARED_LIBS:BOOL=OFF -DCMAKE_CXX_COMPILER="${{ matrix.cxx-compiler }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.cmake-build-type }} -DBUILD_TESTING:BOOL=OFF -GNinja ../ITK
84-
ninja
85-
shell: cmd
86-
8767
- name: Fetch CTest driver script
8868
run: |
8969
curl -L https://raw.githubusercontent.com/InsightSoftwareConsortium/ITK/dashboard/itk_common.cmake -O

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)

Formatting/conf.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ release = '@ITKSphinxExamples_RELEASE_VERSION@'
9090

9191
# List of patterns, relative to source directory, that match files and
9292
# directories to ignore when looking for source files.
93-
exclude_patterns = ['Utilities', 'html']
93+
exclude_patterns = ['Utilities', 'html', '_deps']
9494

9595
# The reST default role (used for this markup: `text`) to use for all documents.
9696
#default_role = None

Superbuild/External-ITK.cmake

Lines changed: 0 additions & 74 deletions
This file was deleted.

Superbuild/Superbuild.cmake

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ else()
2929
endif()
3030

3131
set(ITKSphinxExamples_DEPENDENCIES )
32-
set(ITK_DEPENDENCIES )
3332

3433
option(ITKSphinxExamples_USE_VTK "Add VTK to the superbuild." OFF)
3534

3635
option(ITKSphinxExamples_USE_OpenCV "Add OpenCV to the superbuild" OFF)
3736
if(NOT OpenCV_DIR AND ITKSphinxExamples_USE_OpenCV)
3837
include(${CMAKE_SOURCE_DIR}/External-OpenCV.cmake)
3938
list(APPEND ITKSphinxExamples_DEPENDENCIES OpenCV)
40-
list(APPEND ITK_DEPENDENCIES OpenCV)
4139
endif()
4240

4341

@@ -55,13 +53,11 @@ endif()
5553
if(NOT VTK_DIR AND ITKSphinxExamples_USE_VTK)
5654
include(${CMAKE_SOURCE_DIR}/External-VTK.cmake)
5755
list(APPEND ITKSphinxExamples_DEPENDENCIES VTK)
58-
list(APPEND ITK_DEPENDENCIES VTK)
5956
endif()
6057

61-
if(NOT ITK_DIR)
62-
include(${CMAKE_SOURCE_DIR}/External-ITK.cmake)
63-
list(APPEND ITKSphinxExamples_DEPENDENCIES ITK)
64-
endif()
58+
# ITK is no longer fetched by the Superbuild. It is acquired via FetchContent
59+
# in the root CMakeLists.txt. Pass ITK_DIR through if provided by the user
60+
# so the inner build can use a pre-built ITK via find_package.
6561

6662
option(BUILD_DOCUMENTATION "Build documentation" OFF)
6763
set(_build_doc_args)
@@ -108,7 +104,7 @@ ExternalProject_Add(ITKSphinxExamples
108104
${_build_doc_args}
109105
${_python_args}
110106
-DBUILD_SHARED_LIBS:BOOL=FALSE
111-
# ITK
107+
# ITK — pass through ITK_DIR if provided, otherwise FetchContent handles it
112108
-DITK_DIR:PATH=${ITK_DIR}
113109
-DBUILD_TESTING:BOOL=${BUILD_TESTING}
114110
-DBUILD_DOCUMENTATION:BOOL=${BUILD_DOCUMENTATION}

src/Filtering/MathematicalMorphology/DilateUsingFunctionalGrayscale/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ find_package( ITK REQUIRED
99
ITKMathematicalMorphology
1010
ITKIOPNG
1111
)
12-
include( ${ITK_USE_FILE} )
1312

1413
add_executable( DilateUsingFunctionalGrayscale Code.cxx )
15-
target_link_libraries( DilateUsingFunctionalGrayscale ${ITK_LIBRARIES} )
14+
target_link_libraries(${PROJECT_NAME}
15+
PRIVATE
16+
ITK::ITKImageIO
17+
ITK::ITKCommonModule
18+
ITK::ITKMathematicalMorphologyModule
19+
)
1620

1721
install( TARGETS DilateUsingFunctionalGrayscale
1822
DESTINATION bin/ITKSphinxExamples/Filtering/MathematicalMorphology

0 commit comments

Comments
 (0)