Skip to content

Commit 65b38b2

Browse files
committed
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.
1 parent 8c6e9b7 commit 65b38b2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.22.1...3.29.0 FATAL_ERROR)
2+
3+
# This project is designed to be built outside the Insight source tree.
4+
project(HelloFetchITK)
5+
6+
# Use an existing ITK installation if specified with ITK_DIR, or fetch and configure ITK.
7+
include(ITKFetchContents.cmake)
8+
9+
itk_generate_factory_registration()
10+
11+
add_executable(HelloFetchITK FetchWorld.cxx)
12+
13+
# If ITK was fetched and configure, only the required modules are built.
14+
target_link_libraries(HelloFetchITK ITK::ITKCommonModule)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
19+
#include "itkImage.h"
20+
#include <iostream>
21+
22+
int
23+
main()
24+
{
25+
using ImageType = itk::Image<unsigned short, 3>;
26+
27+
auto image = ImageType::New();
28+
29+
std::cout << "ITK Hello World !" << std::endl;
30+
31+
return EXIT_SUCCESS;
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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/blowekamp/ITK.git")
8+
9+
set(ITK_GIT_TAG "cmake_interface_module")
10+
11+
# Set ITK build options
12+
set(ITK_BUILD_DEFAULT_MODULES ON)
13+
set(ITK_USE_KWSTYLE OFF)
14+
set(BUILD_TESTING OFF)
15+
set(BUILD_EXAMPLES OFF)
16+
17+
FetchContent_Declare(
18+
ITK
19+
GIT_REPOSITORY "${ITK_GIT_REPOSITORY}"
20+
GIT_TAG "${ITK_GIT_TAG}"
21+
EXCLUDE_FROM_ALL
22+
FIND_PACKAGE_ARGS
23+
NAMES
24+
ITK
25+
)
26+
27+
FetchContent_MakeAvailable(ITK)
28+
29+
# Check if FetchContent used find_package() or fetched from source
30+
FetchContent_GetProperties(ITK)
31+
if(ITK_SOURCE_DIR)
32+
message(STATUS "ITK fetched from repository and built from source")
33+
message(STATUS " Source directory: ${ITK_SOURCE_DIR}")
34+
message(STATUS " Binary directory: ${ITK_BINARY_DIR}")
35+
set(ITK_DIR "${ITK_BINARY_DIR}")
36+
37+
include(${ITK_DIR}/ITKConfig.cmake)
38+
elseif(DEFINED ITK_FOUND)
39+
message(STATUS "ITK found via find_package()")
40+
# ITK_DIR should already be set by find_package()
41+
else()
42+
message(FATAL_ERROR "ITK configuration failed - no targets available")
43+
endif()
44+
45+
# These ITK option conflict with SimpleITK.
46+
# Allow a user's cache variable to be respected.
47+
unset(BUILD_TESTING)
48+
unset(BUILD_EXAMPLES)

0 commit comments

Comments
 (0)