From fbbd7697611a1cdb5a70a357e6054b5d8893cf59 Mon Sep 17 00:00:00 2001
From: Rashad Kanavath <rashad.kanavath@c-s.fr>
Date: Wed, 15 Jun 2016 16:29:54 +0200
Subject: [PATCH] SuperBuild: WIP: auto-generate uninstall target for
 dependencies

---
 SuperBuild/CMake/External_geotiff.cmake   |  3 +++
 SuperBuild/CMake/External_itk.cmake       |  3 +++
 SuperBuild/CMake/External_opencv.cmake    |  8 +++++---
 SuperBuild/CMake/External_openjpeg.cmake  |  2 ++
 SuperBuild/CMake/External_ossim.cmake     |  5 ++---
 SuperBuild/CMake/SuperBuild_Macro.cmake   | 17 +++++++++++++++++
 SuperBuild/CMake/cmake_uninstall.cmake.in | 22 ++++++++++++++++++++++
 7 files changed, 54 insertions(+), 6 deletions(-)
 create mode 100644 SuperBuild/CMake/cmake_uninstall.cmake.in

diff --git a/SuperBuild/CMake/External_geotiff.cmake b/SuperBuild/CMake/External_geotiff.cmake
index 2416c218b5..26a63aef50 100644
--- a/SuperBuild/CMake/External_geotiff.cmake
+++ b/SuperBuild/CMake/External_geotiff.cmake
@@ -62,6 +62,9 @@ else()
     ${GEOTIFF_SB_CONFIG}
     CMAKE_COMMAND ${SB_CMAKE_COMMAND}
     )
+
+  CREATE_UNINSTALL_TARGET_FOR(GEOTIFF)
+
 endif()
 
 set(_SB_GEOTIFF_INCLUDE_DIR ${SB_INSTALL_PREFIX}/include)
diff --git a/SuperBuild/CMake/External_itk.cmake b/SuperBuild/CMake/External_itk.cmake
index e27b7baaea..e9dae756fa 100644
--- a/SuperBuild/CMake/External_itk.cmake
+++ b/SuperBuild/CMake/External_itk.cmake
@@ -162,4 +162,7 @@ ExternalProject_Add(ITK
 
 SUPERBUILD_PATCH_SOURCE(ITK)
 
+CREATE_UNINSTALL_TARGET_FOR(ITK)
+
+
 set(_SB_ITK_DIR ${SB_INSTALL_PREFIX}/lib/cmake/ITK-${ITK_SB_VERSION})
diff --git a/SuperBuild/CMake/External_opencv.cmake b/SuperBuild/CMake/External_opencv.cmake
index 61b94ee153..5ef3ad0556 100644
--- a/SuperBuild/CMake/External_opencv.cmake
+++ b/SuperBuild/CMake/External_opencv.cmake
@@ -68,8 +68,10 @@ INCLUDE_ONCE_MACRO(OPENCV)
       CMAKE_COMMAND ${SB_CMAKE_COMMAND}
     )
 
-  if(APPLE)
-    SUPERBUILD_PATCH_SOURCE(OPENCV)
-  endif()
+
+  SUPERBUILD_PATCH_SOURCE(OPENCV)
 
   set(_SB_OpenCV_DIR ${SB_INSTALL_PREFIX}/share/OpenCV)
+
+
+  CREATE_UNINSTALL_TARGET_FOR(OPENCV)
\ No newline at end of file
diff --git a/SuperBuild/CMake/External_openjpeg.cmake b/SuperBuild/CMake/External_openjpeg.cmake
index 3980874525..fbcc36b7f4 100644
--- a/SuperBuild/CMake/External_openjpeg.cmake
+++ b/SuperBuild/CMake/External_openjpeg.cmake
@@ -38,3 +38,5 @@ ExternalProject_Add(OPENJPEG
   )
 
 SUPERBUILD_UPDATE_CMAKE_VARIABLES(OPENJPEG FALSE)
+
+CREATE_UNINSTALL_TARGET_FOR(OPENJPEG)
\ No newline at end of file
diff --git a/SuperBuild/CMake/External_ossim.cmake b/SuperBuild/CMake/External_ossim.cmake
index a3bfaa0918..77de21ce28 100644
--- a/SuperBuild/CMake/External_ossim.cmake
+++ b/SuperBuild/CMake/External_ossim.cmake
@@ -56,9 +56,8 @@ ExternalProject_Add_Step(OSSIM patch_no_cmakelists
   DEPENDEES patch
   DEPENDERS configure )
 
-
-
 SUPERBUILD_PATCH_SOURCE(OSSIM)
 
-
 SUPERBUILD_UPDATE_CMAKE_VARIABLES(OSSIM FALSE)
+
+CREATE_UNINSTALL_TARGET_FOR(OSSIM)
\ No newline at end of file
diff --git a/SuperBuild/CMake/SuperBuild_Macro.cmake b/SuperBuild/CMake/SuperBuild_Macro.cmake
index 9ba45b420d..6d67aa5988 100644
--- a/SuperBuild/CMake/SuperBuild_Macro.cmake
+++ b/SuperBuild/CMake/SuperBuild_Macro.cmake
@@ -150,3 +150,20 @@ macro(SUPERBUILD_UPDATE_CMAKE_VARIABLES PROJECT with_prefix)
   set(_SB_${PROJECT}_LIBRARY ${SB_INSTALL_PREFIX}/lib/${lib_file})
 
 endmacro()
+
+function(CREATE_UNINSTALL_TARGET_FOR prj)
+  set(SUPERBUILD_PROJECT_BINARY_DIR ${${prj}_SB_BUILD_DIR})
+  if(SUPERBUILD_PROJECT_BINARY_DIR)
+  configure_file(
+    "${CMAKE_SOURCE_DIR}/CMake/cmake_uninstall.cmake.in"
+    "${SUPERBUILD_PROJECT_BINARY_DIR}/cmake_uninstall.cmake"
+    IMMEDIATE @ONLY)
+
+  add_custom_target(${prj}-uninstall
+    COMMAND ${CMAKE_COMMAND} -P ${${prj}_SB_BUILD_DIR}/cmake_uninstall.cmake
+    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/${prj}/src/${prj}-stamp/${prj}-install
+    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/${prj}/src/${prj}-stamp/${prj}-done
+    )
+  unset(SUPERBUILD_PROJECT_BINARY_DIR)
+endif()
+endfunction()
diff --git a/SuperBuild/CMake/cmake_uninstall.cmake.in b/SuperBuild/CMake/cmake_uninstall.cmake.in
new file mode 100644
index 0000000000..e1396254fb
--- /dev/null
+++ b/SuperBuild/CMake/cmake_uninstall.cmake.in
@@ -0,0 +1,22 @@
+
+if(NOT EXISTS "@SUPERBUILD_PROJECT_BINARY_DIR@/install_manifest.txt")
+  message(FATAL_ERROR "Cannot find install manifest: @CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+endif()
+
+file(READ "@SUPERBUILD_PROJECT_BINARY_DIR@/install_manifest.txt" files)
+string(REGEX REPLACE "\n" ";" files "${files}")
+foreach(file ${files})
+  message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
+  if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
+    exec_program(
+      "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
+      OUTPUT_VARIABLE rm_out
+      RETURN_VALUE rm_retval
+      )
+    if(NOT "${rm_retval}" STREQUAL 0)
+      message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
+    endif(NOT "${rm_retval}" STREQUAL 0)
+  else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
+    message(STATUS "File $ENV{DESTDIR}${file} does not exist.")
+  endif(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
+endforeach(file)
\ No newline at end of file
-- 
GitLab