diff --git a/CMakeLists.txt b/CMakeLists.txt index 30e9507b2..f0e034ae3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ include(CheckEnvironment) include(ExternalProject) include(ConfigureExternalProjectVariables) +include(EP_GeneratePatchCommand) #Variables for dependencies and update custom, updated in external projects set(Anima_DEPS "") diff --git a/superbuild/ConfigureExternalProjectVariables.cmake b/superbuild/ConfigureExternalProjectVariables.cmake index 4a11e1247..ef86c294d 100644 --- a/superbuild/ConfigureExternalProjectVariables.cmake +++ b/superbuild/ConfigureExternalProjectVariables.cmake @@ -35,6 +35,8 @@ set(common_cache_args ${MACOSX_RPATH_OPTION} ) +set(EP_PATH_SOURCE ${PROJECT_SOURCE_DIR}/External-Projects) + if (CMAKE_EXTRA_GENERATOR) set(cmake_gen "${CMAKE_EXTRA_GENERATOR} -G ${CMAKE_GENERATOR}") else() diff --git a/superbuild/EP_GeneratePatchCommand.cmake b/superbuild/EP_GeneratePatchCommand.cmake new file mode 100644 index 000000000..db18e0d28 --- /dev/null +++ b/superbuild/EP_GeneratePatchCommand.cmake @@ -0,0 +1,14 @@ +## ############################################################################# +## Generate patch command for an external project. To use with ExternalProject_Add +## ############################################################################# +function(ep_GeneratePatchCommand ep OutVar patch) + set(PATCH_COMMAND ${CMAKE_COMMAND} + -Dep:STRING=${ep} + -DANIMA_SOURCE_DIR:STRING=${CMAKE_SOURCE_DIR} + -DEP_PATH_SOURCE:STRING=${EP_PATH_SOURCE} + -Dpatch:STRING=${patch} + -P + ${CMAKE_SOURCE_DIR}/superbuild/EP_PatcherScript.cmake) + + set(${OutVar} ${PATCH_COMMAND} PARENT_SCOPE) +endfunction() diff --git a/superbuild/EP_PatcherScript.cmake b/superbuild/EP_PatcherScript.cmake new file mode 100644 index 000000000..fc315370f --- /dev/null +++ b/superbuild/EP_PatcherScript.cmake @@ -0,0 +1,66 @@ +## ############################################################################# +## Check if a patch should be applied and apply it if needed +## ############################################################################# +macro(directory_count_elements ep_path elements_count_res) + file(GLOB RESULT ${ep_path}/*) + list(LENGTH RESULT ${elements_count_res}) +endmacro() + +function(ep_GenerateValidPatcherScript ep OutVar patch) + find_program(GIT_BIN NAMES git) + + set(PATCHES_TO_APPLY_CUR ${ANIMA_SOURCE_DIR}/superbuild/patches/${patch}) + directory_count_elements(${EP_PATH_SOURCE}/${ep} count) + + if(NOT ${count} EQUAL 0) #Check if external_project is already cloned + execute_process(COMMAND ${GIT_BIN} apply --reverse --ignore-whitespace --check ${ANIMA_SOURCE_DIR}/superbuild/patches/${patch} + WORKING_DIRECTORY ${EP_PATH_SOURCE}/${ep} + RESULT_VARIABLE PATCH_ALREADY_APPLIED + OUTPUT_QUIET + ERROR_QUIET) + + if (PATCH_ALREADY_APPLIED EQUAL 0) #As all OS system commands: 0 is Ok, not 0 is bad. + # The patch is already applied + message("The patch ${patch} for the external project ${ep} is already applied") + set (PATCHES_TO_APPLY_CUR "") #Then erase PATCHES_TO_APPLY_CUR + else() + execute_process(COMMAND ${GIT_BIN} apply --ignore-whitespace --check ${ANIMA_SOURCE_DIR}/superbuild/patches/${patch} + WORKING_DIRECTORY ${EP_PATH_SOURCE}/${ep} + RESULT_VARIABLE PATCH_APPLICABLE) + #OUTPUT_QUIET + #ERROR_QUIET) + if(PATCH_APPLICABLE EQUAL 0) #As all OS system commands: 0 is Ok, not 0 is bad. + #Do nothing, the value is already set + message("The patch ${patch} will be applied on pre-existing ${EP_PATH_SOURCE}/${ep}") + else() + message("*****************************************************************************************************\n") + message("***** The patch ${patch} for the external project ${ep} is NOT APPLICABLE") + message("***** EP PATH = ${EP_PATH_SOURCE}/${ep}") + message("***** PATCH file = ${ANIMA_SOURCE_DIR}/superbuild/patches/${patch}") + message("*****************************************************************************************************\n") + message(FATAL_ERROR "") + + endif() + endif() + else() + message("The patch ${patch} will be applied after ${ep} cloning into ${EP_PATH_SOURCE}/${ep}") + endif() + set(PATCHES_TO_APPLY ${PATCHES_TO_APPLY} ${PATCHES_TO_APPLY_CUR}) + + + set(PATCH_COMMAND "") + if (NOT "${PATCHES_TO_APPLY}" STREQUAL "") + set(PATCH_COMMAND ${GIT_BIN} apply --ignore-whitespace ${PATCHES_TO_APPLY}) + endif() + + set(${OutVar} ${PATCH_COMMAND} PARENT_SCOPE) +endfunction() + + +ep_GenerateValidPatcherScript(${ep} patchCommand ${patch}) + +if(patchCommand) + execute_process(COMMAND ${patchCommand} WORKING_DIRECTORY ${EP_PATH_SOURCE}/${ep}) +else() + message(DEBUG "No patch treatment for ${ep}") +endif() diff --git a/superbuild/ITK.cmake b/superbuild/ITK.cmake index 569f69c64..686f5ec87 100644 --- a/superbuild/ITK.cmake +++ b/superbuild/ITK.cmake @@ -32,6 +32,16 @@ if (NOT DEFINED ${proj}_SRC_DIR) set(location GIT_REPOSITORY ${GITHUB_PREFIX}InsightSoftwareConsortium/ITK.git GIT_TAG ${tag}) endif() +## ############################################################################# +## Check if patch has to be applied +## ############################################################################# + +ep_GeneratePatchCommand(ITK ITK_PATCH_COMMAND ITK.patch) + +## ############################################################################# +## Add external-project +## ############################################################################# + ExternalProject_Add(${proj} ${location} DEPENDS ${VTK_PROJ_DEP} @@ -40,6 +50,7 @@ ExternalProject_Add(${proj} CMAKE_GENERATOR ${cmake_gen} CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM} CMAKE_ARGS ${cmake_args} + PATCH_COMMAND ${ITK_PATCH_COMMAND} BUILD_ALWAYS 1 BINARY_DIR ${CMAKE_BINARY_DIR}/${proj} INSTALL_COMMAND "" diff --git a/superbuild/patches/ITK.patch b/superbuild/patches/ITK.patch new file mode 100644 index 000000000..63d3545e6 --- /dev/null +++ b/superbuild/patches/ITK.patch @@ -0,0 +1,12 @@ +unchanged: +--- a/Modules/Filtering/MathematicalMorphology/include/itkMathematicalMorphologyEnums.h ++++ b/Modules/Filtering/MathematicalMorphology/include/itkMathematicalMorphologyEnums.h +@@ -19,6 +19,7 @@ + #define itkMathematicalMorphologyEnums_h + + #include ++#include + #include "ITKMathematicalMorphologyExport.h" + + + \ No newline at end of file