gitclone.cmake.in 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. cmake_minimum_required(VERSION 3.5)
  4. set(quiet "@quiet@")
  5. set(script_dir "@CMAKE_CURRENT_FUNCTION_LIST_DIR@/ExternalProject")
  6. include(${script_dir}/captured_process_setup.cmake)
  7. if(EXISTS "@gitclone_stampfile@" AND EXISTS "@gitclone_infofile@" AND
  8. "@gitclone_stampfile@" IS_NEWER_THAN "@gitclone_infofile@")
  9. if(NOT quiet)
  10. message(STATUS
  11. "Avoiding repeated git clone, stamp file is up to date: "
  12. "'@gitclone_stampfile@'"
  13. )
  14. endif()
  15. return()
  16. endif()
  17. execute_process(
  18. COMMAND ${CMAKE_COMMAND} -E rm -rf "@source_dir@"
  19. RESULT_VARIABLE error_code
  20. ${capture_output}
  21. )
  22. _ep_command_check_result(
  23. error_code "Failed to remove directory: '@source_dir@'"
  24. )
  25. # try the clone 3 times in case there is an odd git clone issue
  26. set(error_code 1)
  27. set(number_of_tries 0)
  28. while(error_code AND number_of_tries LESS 3)
  29. # If you are seeing the following call hang and you have QUIET enabled, try
  30. # turning QUIET off to show any output immediately. The command may be
  31. # blocking while waiting for user input (e.g. a password to a SSH key).
  32. execute_process(
  33. COMMAND "@git_EXECUTABLE@" @git_options@
  34. clone @git_clone_options@ "@git_repository@" "@src_name@"
  35. WORKING_DIRECTORY "@work_dir@"
  36. RESULT_VARIABLE error_code
  37. ${capture_output}
  38. )
  39. if(NOT "${out_var}" STREQUAL "")
  40. string(APPEND accumulated_output "${out_var}\n")
  41. endif()
  42. math(EXPR number_of_tries "${number_of_tries} + 1")
  43. endwhile()
  44. if(number_of_tries GREATER 1)
  45. set(msg "Had to git clone more than once: ${number_of_tries} times.")
  46. if(quiet)
  47. string(APPEND accumulated_output "${msg}\n")
  48. else()
  49. message(STATUS "${msg}")
  50. endif()
  51. endif()
  52. _ep_command_check_result(
  53. error_code "Failed to clone repository: '@git_repository@'"
  54. )
  55. execute_process(
  56. COMMAND "@git_EXECUTABLE@" @git_options@
  57. checkout "@git_tag@" @git_checkout_explicit--@
  58. WORKING_DIRECTORY "@work_dir@/@src_name@"
  59. RESULT_VARIABLE error_code
  60. ${capture_output}
  61. )
  62. _ep_command_check_result(error_code "Failed to checkout tag: '@git_tag@'")
  63. set(init_submodules @init_submodules@)
  64. if(init_submodules)
  65. execute_process(
  66. COMMAND "@git_EXECUTABLE@" @git_options@
  67. submodule update @git_submodules_recurse@ --init @git_submodules@
  68. WORKING_DIRECTORY "@work_dir@/@src_name@"
  69. RESULT_VARIABLE error_code
  70. ${capture_output}
  71. )
  72. _ep_command_check_result(
  73. error_code "Failed to update submodules in: '@work_dir@/@src_name@'"
  74. )
  75. endif()
  76. # Complete success, update the script-last-run stamp file:
  77. #
  78. execute_process(
  79. COMMAND ${CMAKE_COMMAND} -E copy "@gitclone_infofile@" "@gitclone_stampfile@"
  80. RESULT_VARIABLE error_code
  81. ${capture_output}
  82. )
  83. _ep_command_check_result(
  84. error_code "Failed to copy script-last-run stamp file: '@gitclone_stampfile@'"
  85. )