gitclone.cmake.in 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(VERBOSE
  7. "Avoiding repeated git clone, stamp file is up to date: "
  8. "'@gitclone_stampfile@'"
  9. )
  10. return()
  11. endif()
  12. # Even at VERBOSE level, we don't want to see the commands executed, but
  13. # enabling them to be shown for DEBUG may be useful to help diagnose problems.
  14. cmake_language(GET_MESSAGE_LOG_LEVEL active_log_level)
  15. if(active_log_level MATCHES "DEBUG|TRACE")
  16. set(maybe_show_command COMMAND_ECHO STDOUT)
  17. else()
  18. set(maybe_show_command "")
  19. endif()
  20. execute_process(
  21. COMMAND ${CMAKE_COMMAND} -E rm -rf "@source_dir@"
  22. RESULT_VARIABLE error_code
  23. ${maybe_show_command}
  24. )
  25. if(error_code)
  26. message(FATAL_ERROR "Failed to remove directory: '@source_dir@'")
  27. endif()
  28. # try the clone 3 times in case there is an odd git clone issue
  29. set(error_code 1)
  30. set(number_of_tries 0)
  31. while(error_code AND number_of_tries LESS 3)
  32. execute_process(
  33. COMMAND "@git_EXECUTABLE@"
  34. clone @git_clone_options@ "@git_repository@" "@src_name@"
  35. WORKING_DIRECTORY "@work_dir@"
  36. RESULT_VARIABLE error_code
  37. ${maybe_show_command}
  38. )
  39. math(EXPR number_of_tries "${number_of_tries} + 1")
  40. endwhile()
  41. if(number_of_tries GREATER 1)
  42. message(NOTICE "Had to git clone more than once: ${number_of_tries} times.")
  43. endif()
  44. if(error_code)
  45. message(FATAL_ERROR "Failed to clone repository: '@git_repository@'")
  46. endif()
  47. execute_process(
  48. COMMAND "@git_EXECUTABLE@"
  49. checkout "@git_tag@" @git_checkout_explicit--@
  50. WORKING_DIRECTORY "@work_dir@/@src_name@"
  51. RESULT_VARIABLE error_code
  52. ${maybe_show_command}
  53. )
  54. if(error_code)
  55. message(FATAL_ERROR "Failed to checkout tag: '@git_tag@'")
  56. endif()
  57. set(init_submodules @init_submodules@)
  58. if(init_submodules)
  59. execute_process(
  60. COMMAND "@git_EXECUTABLE@" @git_submodules_config_options@
  61. submodule update @git_submodules_recurse@ --init @git_submodules@
  62. WORKING_DIRECTORY "@work_dir@/@src_name@"
  63. RESULT_VARIABLE error_code
  64. ${maybe_show_command}
  65. )
  66. endif()
  67. if(error_code)
  68. message(FATAL_ERROR "Failed to update submodules in: '@work_dir@/@src_name@'")
  69. endif()
  70. # Complete success, update the script-last-run stamp file:
  71. #
  72. execute_process(
  73. COMMAND ${CMAKE_COMMAND} -E copy "@gitclone_infofile@" "@gitclone_stampfile@"
  74. RESULT_VARIABLE error_code
  75. ${maybe_show_command}
  76. )
  77. if(error_code)
  78. message(FATAL_ERROR "Failed to copy script-last-run stamp file: '@gitclone_stampfile@'")
  79. endif()