[PATCH] ExternalProject: Do not delete automatically the source folder

Daniele E. Domenichelli daniele.domenichelli at iit.it
Tue Sep 10 11:23:31 EDT 2013


This will ensure that if the user keeps the sources outside the build
repository, and has some valuable work in it, it won't be lost if he
deletes the build directory.
Also it should avoid multiple useless clones  with different build
directories.
---
 Modules/ExternalProject.cmake | 70 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 64 insertions(+), 6 deletions(-)

diff --git a/Modules/ExternalProject.cmake b/Modules/ExternalProject.cmake
index 0781ea1..a08188f 100644
--- a/Modules/ExternalProject.cmake
+++ b/Modules/ExternalProject.cmake
@@ -158,6 +158,8 @@
 # any ExternalProject_Add calls in your CMakeLists file:
 #
 #   set_property(DIRECTORY PROPERTY EP_STEP_TARGETS configure build test)
+#
+# FIXME: Add documentation here
 
 #=============================================================================
 # Copyright 2008-2012 Kitware, Inc.
@@ -288,12 +290,68 @@ if(NOT run)
   return()
 endif()
 
-execute_process(
-  COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
-  RESULT_VARIABLE error_code
-  )
-if(error_code)
-  message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
+get_filename_component(abs_binary_dir \"${CMAKE_BINARY_DIR}\" ABSOLUTE)
+get_filename_component(abs_source_dir \"${source_dir}\" ABSOLUTE)
+string(REGEX MATCH \"^\${abs_binary_dir}\" source_in_binary_dir \"\${abs_source_dir}\")
+
+if(EXISTS \"${source_dir}\" AND NOT source_in_binary_dir)
+  if(NOT IS_DIRECTORY \"${source_dir}\")
+    message(FATAL_ERROR \"\\\"${source_dir}\\\" exists and is not a git repository. Remove it and try again\")
+  endif()
+
+  if(IS_DIRECTORY \"${source_dir}/.git\")
+    # Already initialized git repository: no need to clone again
+    execute_process(
+      COMMAND \"${git_EXECUTABLE}\" config --local --get remote.origin.url
+      WORKING_DIRECTORY \"${work_dir}/${src_name}\"
+      OUTPUT_VARIABLE origin_url
+      RESULT_VARIABLE error_code
+      OUTPUT_STRIP_TRAILING_WHITESPACE
+      )
+    if(error_code)
+      message(FATAL_ERROR \"Failed to get origin remote url in: '${work_dir}/${src_name}'\")
+    endif()
+    if(\"\${origin_url}\" STREQUAL \"${git_repository}\")
+      message(STATUS \"Avoiding repeated git clone, repository already exists\")
+      return()
+    else()
+      message(WARNING \"Repository URL is different. Renaming origin remote to origin.old\")
+      execute_process(
+        COMMAND \"${git_EXECUTABLE}\" remote rename origin origin.old
+        WORKING_DIRECTORY \"${work_dir}/${src_name}\"
+        RESULT_VARIABLE error_code
+        )
+      if(error_code)
+        message(FATAL_ERROR \"Failed to rename remote in: '${work_dir}/${src_name}'\")
+      endif()
+
+      execute_process(
+        COMMAND \"${git_EXECUTABLE}\" remote add origin \"${git_repository}\"
+        WORKING_DIRECTORY \"${work_dir}/${src_name}\"
+        RESULT_VARIABLE error_code
+        )
+      if(error_code)
+        message(FATAL_ERROR \"Failed to add origin remote in: '${work_dir}/${src_name}'\")
+      endif()
+
+      execute_process(
+        COMMAND \"${git_EXECUTABLE}\" fetch origin
+        WORKING_DIRECTORY \"${work_dir}/${src_name}\"
+        RESULT_VARIABLE error_code
+        )
+      if(error_code)
+        message(FATAL_ERROR \"Failed to fetch in: '${work_dir}/${src_name}'\")
+      endif()
+    endif()
+  endif()
+else()
+  execute_process(
+    COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
+    RESULT_VARIABLE error_code
+    )
+  if(error_code)
+    message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
+  endif()
 endif()
 
 # try the clone 3 times incase there is an odd git clone issue
-- 
1.8.4.rc3


--------------080001010705050003030302--


More information about the cmake-developers mailing list