Skip to content

Commit 39f9f90

Browse files
committed
ExternalProject: Restore patch to avoid deleting the clones
1 parent 15c8e7e commit 39f9f90

File tree

1 file changed

+94
-24
lines changed

1 file changed

+94
-24
lines changed

cmake-next/proposed/ExternalProject.cmake

+94-24
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,13 @@ define_property(DIRECTORY PROPERTY "EP_UPDATE_DISCONNECTED" INHERITED
10411041
"ExternalProject module."
10421042
)
10431043

1044+
define_property(DIRECTORY PROPERTY "EP_SOURCE_DIR_PERSISTENT" INHERITED
1045+
BRIEF_DOCS "Whether source dir stored outside the build directory should be preserved."
1046+
FULL_DOCS
1047+
"See documentation of the ExternalProject_Add() function in the "
1048+
"ExternalProject module."
1049+
)
1050+
10441051
function(_ep_write_gitclone_script script_filename source_dir git_EXECUTABLE git_repository git_tag git_remote_name git_submodules git_shallow git_progress git_config src_name work_dir gitclone_infofile gitclone_stampfile tls_verify)
10451052
if(NOT GIT_VERSION_STRING VERSION_LESS 1.7.10)
10461053
set(git_clone_shallow_options "--depth 1 --no-single-branch")
@@ -1072,14 +1079,6 @@ if(NOT run)
10721079
return()
10731080
endif()
10741081
1075-
execute_process(
1076-
COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
1077-
RESULT_VARIABLE error_code
1078-
)
1079-
if(error_code)
1080-
message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
1081-
endif()
1082-
10831082
set(git_options)
10841083
10851084
# disable cert checking if explicitly told not to do it
@@ -1106,23 +1105,94 @@ foreach(config IN LISTS git_config)
11061105
list(APPEND git_clone_options --config \${config})
11071106
endforeach()
11081107
1109-
# try the clone 3 times in case there is an odd git clone issue
1110-
set(error_code 1)
1111-
set(number_of_tries 0)
1112-
while(error_code AND number_of_tries LESS 3)
1113-
execute_process(
1114-
COMMAND \"${git_EXECUTABLE}\" \${git_options} clone \${git_clone_options} --origin \"${git_remote_name}\" \"${git_repository}\" \"${src_name}\"
1115-
WORKING_DIRECTORY \"${work_dir}\"
1116-
RESULT_VARIABLE error_code
1117-
)
1118-
math(EXPR number_of_tries \"\${number_of_tries} + 1\")
1119-
endwhile()
1120-
if(number_of_tries GREATER 1)
1121-
message(STATUS \"Had to git clone more than once:
1122-
\${number_of_tries} times.\")
1108+
if(EXISTS \"${source_dir}\" AND ${source_dir_persistent})
1109+
if(NOT IS_DIRECTORY \"${source_dir}\")
1110+
# FIXME Perhaps support symbolic links?
1111+
message(FATAL_ERROR \"\\\"${source_dir}\\\" exists and is not a git repository. Remove it and try again\")
1112+
elseif(NOT IS_DIRECTORY \"${source_dir}/.git\")
1113+
file(GLOB files \"${source_dir}/*\")
1114+
list(LENGTH files nfiles)
1115+
if(nfiles)
1116+
message(FATAL_ERROR \"\\\"${source_dir}\\\" folder exists and is not a git repository. Remove it and try again\")
1117+
endif()
1118+
else()
1119+
# Already initialized git repository: no need to clone again
1120+
execute_process(
1121+
COMMAND \"${git_EXECUTABLE}\" config --local --get remote.origin.url
1122+
WORKING_DIRECTORY \"${work_dir}/${src_name}\"
1123+
OUTPUT_VARIABLE origin_url
1124+
RESULT_VARIABLE error_code
1125+
OUTPUT_STRIP_TRAILING_WHITESPACE
1126+
)
1127+
if(error_code)
1128+
message(FATAL_ERROR \"Failed to get origin remote url in: '${work_dir}/${src_name}'\")
1129+
endif()
1130+
if(\"\${origin_url}\" STREQUAL \"${git_repository}\")
1131+
message(STATUS \"Avoiding repeated git clone, repository already exists\")
1132+
return()
1133+
else()
1134+
string(TIMESTAMP now \"%Y%m%d%H%M%S\")
1135+
message(WARNING \"Repository URL is different. Renaming origin remote to origin.\${now}\")
1136+
execute_process(
1137+
COMMAND \"${git_EXECUTABLE}\" remote rename origin origin.\${now}
1138+
WORKING_DIRECTORY \"${work_dir}/${src_name}\"
1139+
RESULT_VARIABLE error_code
1140+
)
1141+
if(error_code)
1142+
message(FATAL_ERROR \"Failed to rename remote in: '${work_dir}/${src_name}'\")
1143+
endif()
1144+
1145+
execute_process(
1146+
COMMAND \"${git_EXECUTABLE}\" remote add origin \"${git_repository}\"
1147+
WORKING_DIRECTORY \"${work_dir}/${src_name}\"
1148+
RESULT_VARIABLE error_code
1149+
)
1150+
if(error_code)
1151+
message(FATAL_ERROR \"Failed to add origin remote in: '${work_dir}/${src_name}'\")
1152+
endif()
1153+
1154+
# try the fetch 3 times incase there is an odd git fetch issue
1155+
set(error_code 1)
1156+
set(number_of_tries 0)
1157+
while(error_code AND number_of_tries LESS 3)
1158+
execute_process(
1159+
COMMAND \"${git_EXECUTABLE}\" \${git_options} fetch \${git_clone_options} origin
1160+
WORKING_DIRECTORY \"${work_dir}/${src_name}\"
1161+
RESULT_VARIABLE error_code
1162+
)
1163+
math(EXPR number_of_tries \"\${number_of_tries} + 1\")
1164+
endwhile()
1165+
if(number_of_tries GREATER 1)
1166+
message(STATUS \"Had to git fetch more than once:
1167+
\${number_of_tries} times.\")
1168+
endif()
1169+
if(error_code)
1170+
message(FATAL_ERROR \"Failed to fetch in: '${work_dir}/${src_name}'\")
1171+
endif()
1172+
endif()
1173+
endif()
11231174
endif()
1124-
if(error_code)
1125-
message(FATAL_ERROR \"Failed to clone repository: '${git_repository}'\")
1175+
1176+
# Now perform the clone if still required
1177+
if(NOT IS_DIRECTORY \"${source_dir}/.git\")
1178+
# try the clone 3 times incase there is an odd git clone issue
1179+
set(error_code 1)
1180+
set(number_of_tries 0)
1181+
while(error_code AND number_of_tries LESS 3)
1182+
execute_process(
1183+
COMMAND \"${git_EXECUTABLE}\" \${git_options} clone \${git_clone_options} --origin \"${git_remote_name}\" \"${git_repository}\" \"${src_name}\"
1184+
WORKING_DIRECTORY \"${work_dir}\"
1185+
RESULT_VARIABLE error_code
1186+
)
1187+
math(EXPR number_of_tries \"\${number_of_tries} + 1\")
1188+
endwhile()
1189+
if(number_of_tries GREATER 1)
1190+
message(STATUS \"Had to git clone more than once:
1191+
\${number_of_tries} times.\")
1192+
endif()
1193+
if(error_code)
1194+
message(FATAL_ERROR \"Failed to clone repository: '${git_repository}'\")
1195+
endif()
11261196
endif()
11271197
11281198
execute_process(

0 commit comments

Comments
 (0)