From 65b38b28287c7da2247d79a9f6187b5a8455bea8 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Fri, 23 Jan 2026 16:54:45 +0000 Subject: [PATCH] ENH: Add example project which used ITK Modules and FetchContent By use FetchContent for ITK, along with specific ITK module interface library linking enable to build only the targets needed in ITK for a project. While the initial configuration time is longer due to downloading and configuring ITK, the CMake project structure is simpler than a Superbuild, and the build is more efficient. --- Examples/HelloFetchITK/CMakeLists.txt | 14 ++++++ Examples/HelloFetchITK/FetchWorld.cxx | 32 +++++++++++++ Examples/HelloFetchITK/ITKFetchContents.cmake | 48 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 Examples/HelloFetchITK/CMakeLists.txt create mode 100644 Examples/HelloFetchITK/FetchWorld.cxx create mode 100644 Examples/HelloFetchITK/ITKFetchContents.cmake diff --git a/Examples/HelloFetchITK/CMakeLists.txt b/Examples/HelloFetchITK/CMakeLists.txt new file mode 100644 index 00000000000..cc96048108c --- /dev/null +++ b/Examples/HelloFetchITK/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.22.1...3.29.0 FATAL_ERROR) + +# This project is designed to be built outside the Insight source tree. +project(HelloFetchITK) + +# Use an existing ITK installation if specified with ITK_DIR, or fetch and configure ITK. +include(ITKFetchContents.cmake) + +itk_generate_factory_registration() + +add_executable(HelloFetchITK FetchWorld.cxx) + +# If ITK was fetched and configure, only the required modules are built. +target_link_libraries(HelloFetchITK ITK::ITKCommonModule) diff --git a/Examples/HelloFetchITK/FetchWorld.cxx b/Examples/HelloFetchITK/FetchWorld.cxx new file mode 100644 index 00000000000..f46b190e17a --- /dev/null +++ b/Examples/HelloFetchITK/FetchWorld.cxx @@ -0,0 +1,32 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#include "itkImage.h" +#include + +int +main() +{ + using ImageType = itk::Image; + + auto image = ImageType::New(); + + std::cout << "ITK Hello World !" << std::endl; + + return EXIT_SUCCESS; +} diff --git a/Examples/HelloFetchITK/ITKFetchContents.cmake b/Examples/HelloFetchITK/ITKFetchContents.cmake new file mode 100644 index 00000000000..0c3e840876c --- /dev/null +++ b/Examples/HelloFetchITK/ITKFetchContents.cmake @@ -0,0 +1,48 @@ +#----------------------------------------------------------------------------- +# Get and build ITK using FetchContent + +include(FetchContent) + +# Set ITK Git repository and tag +set(ITK_GIT_REPOSITORY "https://github.com/blowekamp/ITK.git") + +set(ITK_GIT_TAG "cmake_interface_module") + +# Set ITK build options +set(ITK_BUILD_DEFAULT_MODULES ON) +set(ITK_USE_KWSTYLE OFF) +set(BUILD_TESTING OFF) +set(BUILD_EXAMPLES OFF) + +FetchContent_Declare( + ITK + GIT_REPOSITORY "${ITK_GIT_REPOSITORY}" + GIT_TAG "${ITK_GIT_TAG}" + EXCLUDE_FROM_ALL + FIND_PACKAGE_ARGS + NAMES + ITK +) + +FetchContent_MakeAvailable(ITK) + +# Check if FetchContent used find_package() or fetched from source +FetchContent_GetProperties(ITK) +if(ITK_SOURCE_DIR) + message(STATUS "ITK fetched from repository and built from source") + message(STATUS " Source directory: ${ITK_SOURCE_DIR}") + message(STATUS " Binary directory: ${ITK_BINARY_DIR}") + set(ITK_DIR "${ITK_BINARY_DIR}") + + include(${ITK_DIR}/ITKConfig.cmake) +elseif(DEFINED ITK_FOUND) + message(STATUS "ITK found via find_package()") + # ITK_DIR should already be set by find_package() +else() + message(FATAL_ERROR "ITK configuration failed - no targets available") +endif() + +# These ITK option conflict with SimpleITK. +# Allow a user's cache variable to be respected. +unset(BUILD_TESTING) +unset(BUILD_EXAMPLES)