blob: 8aa405aa0582cfb368bdc398c199f9fddc6f38b5 [file]
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
cmake_minimum_required(VERSION ${CMAKE_VERSION}) # this file comes with cmake
if(EXISTS "@gitclone_stampfile@" AND EXISTS "@gitclone_infofile@" AND
"@gitclone_stampfile@" IS_NEWER_THAN "@gitclone_infofile@")
message(VERBOSE
"Avoiding repeated git clone, stamp file is up to date: "
"'@gitclone_stampfile@'"
)
return()
endif()
# Even at VERBOSE level, we don't want to see the commands executed, but
# enabling them to be shown for DEBUG may be useful to help diagnose problems.
cmake_language(GET_MESSAGE_LOG_LEVEL active_log_level)
if(active_log_level MATCHES "DEBUG|TRACE")
set(maybe_show_command COMMAND_ECHO STDOUT)
else()
set(maybe_show_command "")
endif()
execute_process(
COMMAND ${CMAKE_COMMAND} -E rm -rf "@source_dir@"
RESULT_VARIABLE error_code
${maybe_show_command}
)
if(error_code)
message(FATAL_ERROR "Failed to remove directory: '@source_dir@'")
endif()
# try the clone 1 + N times in case there is an odd git clone issue
set(error_code 1)
set(number_of_tries 0)
math(EXPR max_tries "1 + @git_clone_retries@")
# For a shallow clone of a commit hash, first try to fetch only that commit
# into a new repository configured as git clone would, which has no option
# for this before git 2.49. Some servers only send the commits of advertised
# refs, so after any failure make the ordinary clone below, which may have
# the commit, and reports its own errors.
if(@git_clone_commit_only@)
set(git_progress "@git_progress@")
set(fetch_progress "")
if(git_progress)
message(STATUS "Cloning commit @git_tag@")
set(fetch_progress --progress)
endif()
# Keep recoverable git errors out of the build tool's diagnostics.
set(capture_output
OUTPUT_VARIABLE clone_output
ERROR_VARIABLE clone_output
)
# git init doesn't know the object format of the remote repository.
execute_process(
COMMAND "${CMAKE_COMMAND}" -E env "GIT_DEFAULT_HASH=@git_hash_algorithm@"
"@git_EXECUTABLE@" init "@src_name@"
WORKING_DIRECTORY "@work_dir@"
RESULT_VARIABLE error_code
${capture_output}
${maybe_show_command}
)
set(git_commit_only_config "@git_commit_only_config@")
foreach(config IN LISTS git_commit_only_config)
if(NOT error_code AND config MATCHES "^([^=]+)=(.*)$")
# Name the file, as git config would otherwise write to $GIT_CONFIG.
execute_process(
COMMAND "@git_EXECUTABLE@" config --file .git/config
--add "${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}"
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
${capture_output}
${maybe_show_command}
)
else()
set(error_code 1)
endif()
endforeach()
if(NOT error_code)
# Any other fetch refspec of the remote, whether from GIT_CONFIG or the
# user's configuration, asks for more than the commit, which the
# ordinary clone fetches. git config would otherwise read $GIT_CONFIG.
execute_process(
COMMAND "${CMAKE_COMMAND}" -E env --unset=GIT_CONFIG
"@git_EXECUTABLE@" config --get-all "remote.@git_remote_name@.fetch"
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
OUTPUT_VARIABLE fetch_refspecs
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE clone_output
${maybe_show_command}
)
if(NOT fetch_refspecs STREQUAL "+refs/heads/*:refs/remotes/@git_remote_name@/*")
set(error_code 1)
endif()
endif()
if(NOT error_code)
# git fetches the tags that point at the commit too, as the ordinary
# clone has them, but only if the commit is fetched into a ref.
execute_process(
COMMAND "@git_EXECUTABLE@" fetch --depth 1 ${fetch_progress}
"@git_remote_name@" "+@git_tag@:refs/cmake/commit"
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
${capture_output}
${maybe_show_command}
)
endif()
if(NOT error_code)
execute_process(
COMMAND "@git_EXECUTABLE@" update-ref -d refs/cmake/commit
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
${maybe_show_command}
)
endif()
if(error_code)
message(DEBUG "${clone_output}")
if(git_progress)
message(STATUS "Cloning the branch tips and tags instead")
endif()
execute_process(
COMMAND ${CMAKE_COMMAND} -E rm -rf "@source_dir@"
COMMAND_ERROR_IS_FATAL ANY
${maybe_show_command}
)
elseif(git_progress AND NOT clone_output STREQUAL "")
message(NOTICE "${clone_output}")
endif()
endif()
while(error_code AND number_of_tries LESS ${max_tries})
if(number_of_tries GREATER 0 AND @git_clone_retry_delay@ GREATER 0)
message(STATUS "Retry #${number_of_tries}, waiting @git_clone_retry_delay@ seconds before next attempt...")
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep @git_clone_retry_delay@)
endif()
execute_process(
COMMAND "@git_EXECUTABLE@"
clone @git_clone_options@ "@git_repository@" "@src_name@"
WORKING_DIRECTORY "@work_dir@"
RESULT_VARIABLE error_code
${maybe_show_command}
)
math(EXPR number_of_tries "${number_of_tries} + 1")
endwhile()
if(number_of_tries GREATER 1)
message(NOTICE "Had to git clone more than once: ${number_of_tries} times.")
endif()
if(error_code)
message(FATAL_ERROR "Failed to clone repository:\n '@git_repository@'")
endif()
# A shallow clone has only the branch tips and tags, so it lacks a commit
# hash that is neither. Fetch that commit by itself. Servers that refuse to
# send an unadvertised commit make this fail, and the checkout below then
# reports the error.
set(git_shallow "@git_shallow@")
set(git_tag "@git_tag@")
set(git_hash_algorithm "@git_hash_algorithm@")
if(git_shallow AND git_hash_algorithm)
execute_process(
COMMAND "@git_EXECUTABLE@" rev-parse --verify --quiet "${git_tag}^0"
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
OUTPUT_QUIET
ERROR_QUIET
)
if(error_code)
execute_process(
COMMAND "@git_EXECUTABLE@" fetch --depth 1 "@git_remote_name@" "${git_tag}"
WORKING_DIRECTORY "@work_dir@/@src_name@"
${maybe_show_command}
)
endif()
endif()
execute_process(
COMMAND "@git_EXECUTABLE@"
checkout "@git_tag@" @git_checkout_explicit--@
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
${maybe_show_command}
)
if(error_code)
message(FATAL_ERROR "Failed to checkout tag: '@git_tag@'")
endif()
set(init_submodules @init_submodules@)
if(init_submodules)
execute_process(
COMMAND "@git_EXECUTABLE@" @git_submodules_config_options@
submodule update @git_submodules_recurse@ --init @git_submodules@
WORKING_DIRECTORY "@work_dir@/@src_name@"
RESULT_VARIABLE error_code
${maybe_show_command}
)
endif()
if(error_code)
message(FATAL_ERROR "Failed to update submodules in: '@work_dir@/@src_name@'")
endif()
# Complete success, update the script-last-run stamp file:
#
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy "@gitclone_infofile@" "@gitclone_stampfile@"
RESULT_VARIABLE error_code
${maybe_show_command}
)
if(error_code)
message(FATAL_ERROR "Failed to copy script-last-run stamp file: '@gitclone_stampfile@'")
endif()