gitclone.cmake.in 2.1 KB

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