On 10/09/13 11:49, Daniele E. Domenichelli wrote:
> Then I think I'll go for the first option ("Skip the clone phase if the
> source_dir exists and is not in the build directory")


This is a preliminary patch, that partially fixes the issue for git only
(I still want to ensure that the folder is empty before removing it, but
I don't know how to check if a directory is empty, suggestions?)

Does it look acceptable such a behaviour, or do I have to add a
variable? Comments?

Thanks.


Regards,
 Daniele
>From b9e228e7b5709cb2843b46168a9cbc42d1dac74d Mon Sep 17 00:00:00 2001
From: "Daniele E. Domenichelli" <[email protected]>
Date: Tue, 10 Sep 2013 17:23:31 +0200
Subject: [PATCH] ExternalProject: Do not delete automatically the source
 folder

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

--

Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers

Reply via email to