gitclone.cmake.in 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if(EXISTS "@gitclone_stampfile@" AND EXISTS "@gitclone_infofile@" AND
  5. "@gitclone_stampfile@" IS_NEWER_THAN "@gitclone_infofile@")
  6. message(STATUS
  7. "Avoiding repeated git clone, stamp file is up to date: "
  8. "'@gitclone_stampfile@'"
  9. )
  10. return()
  11. endif()
  12. execute_process(
  13. COMMAND ${CMAKE_COMMAND} -E rm -rf "@source_dir@"
  14. RESULT_VARIABLE error_code
  15. )
  16. if(error_code)
  17. message(FATAL_ERROR "Failed to remove directory: '@source_dir@'")
  18. endif()
  19. # try the clone 3 times in case there is an odd git clone issue
  20. set(error_code 1)
  21. set(number_of_tries 0)
  22. while(error_code AND number_of_tries LESS 3)
  23. execute_process(
  24. COMMAND "@git_EXECUTABLE@" @git_options@
  25. clone @git_clone_options@ "@git_repository@" "@src_name@"
  26. WORKING_DIRECTORY "@work_dir@"
  27. RESULT_VARIABLE error_code
  28. )
  29. math(EXPR number_of_tries "${number_of_tries} + 1")
  30. endwhile()
  31. if(number_of_tries GREATER 1)
  32. message(STATUS "Had to git clone more than once: ${number_of_tries} times.")
  33. endif()
  34. if(error_code)
  35. message(FATAL_ERROR "Failed to clone repository: '@git_repository@'")
  36. endif()
  37. execute_process(
  38. COMMAND "@git_EXECUTABLE@" @git_options@
  39. checkout "@git_tag@" @git_checkout_explicit--@
  40. WORKING_DIRECTORY "@work_dir@/@src_name@"
  41. RESULT_VARIABLE error_code
  42. )
  43. if(error_code)
  44. message(FATAL_ERROR "Failed to checkout tag: '@git_tag@'")
  45. endif()
  46. set(init_submodules @init_submodules@)
  47. if(init_submodules)
  48. execute_process(
  49. COMMAND "@git_EXECUTABLE@" @git_options@
  50. submodule update @git_submodules_recurse@ --init @git_submodules@
  51. WORKING_DIRECTORY "@work_dir@/@src_name@"
  52. RESULT_VARIABLE error_code
  53. )
  54. endif()
  55. if(error_code)
  56. message(FATAL_ERROR "Failed to update submodules in: '@work_dir@/@src_name@'")
  57. endif()
  58. # Complete success, update the script-last-run stamp file:
  59. #
  60. execute_process(
  61. COMMAND ${CMAKE_COMMAND} -E copy "@gitclone_infofile@" "@gitclone_stampfile@"
  62. RESULT_VARIABLE error_code
  63. )
  64. if(error_code)
  65. message(FATAL_ERROR "Failed to copy script-last-run stamp file: '@gitclone_stampfile@'")
  66. endif()