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)