ExternalProject-download.cmake.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. function(check_file_hash has_hash hash_is_good)
  5. if("${has_hash}" STREQUAL "")
  6. message(FATAL_ERROR "has_hash Can't be empty")
  7. endif()
  8. if("${hash_is_good}" STREQUAL "")
  9. message(FATAL_ERROR "hash_is_good Can't be empty")
  10. endif()
  11. if("@ALGO@" STREQUAL "")
  12. # No check
  13. set("${has_hash}" FALSE PARENT_SCOPE)
  14. set("${hash_is_good}" FALSE PARENT_SCOPE)
  15. return()
  16. endif()
  17. set("${has_hash}" TRUE PARENT_SCOPE)
  18. message(STATUS "verifying file...
  19. file='@LOCAL@'")
  20. file("@ALGO@" "@LOCAL@" actual_value)
  21. if(NOT "${actual_value}" STREQUAL "@EXPECT_VALUE@")
  22. set("${hash_is_good}" FALSE PARENT_SCOPE)
  23. message(STATUS "@ALGO@ hash of
  24. @LOCAL@
  25. does not match expected value
  26. expected: '@EXPECT_VALUE@'
  27. actual: '${actual_value}'")
  28. else()
  29. set("${hash_is_good}" TRUE PARENT_SCOPE)
  30. endif()
  31. endfunction()
  32. function(sleep_before_download attempt)
  33. if(attempt EQUAL 0)
  34. return()
  35. endif()
  36. if(attempt EQUAL 1)
  37. message(STATUS "Retrying...")
  38. return()
  39. endif()
  40. set(sleep_seconds 0)
  41. if(attempt EQUAL 2)
  42. set(sleep_seconds 5)
  43. elseif(attempt EQUAL 3)
  44. set(sleep_seconds 5)
  45. elseif(attempt EQUAL 4)
  46. set(sleep_seconds 15)
  47. elseif(attempt EQUAL 5)
  48. set(sleep_seconds 60)
  49. elseif(attempt EQUAL 6)
  50. set(sleep_seconds 90)
  51. elseif(attempt EQUAL 7)
  52. set(sleep_seconds 300)
  53. else()
  54. set(sleep_seconds 1200)
  55. endif()
  56. message(STATUS "Retry after ${sleep_seconds} seconds (attempt #${attempt}) ...")
  57. execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep "${sleep_seconds}")
  58. endfunction()
  59. if("@LOCAL@" STREQUAL "")
  60. message(FATAL_ERROR "LOCAL can't be empty")
  61. endif()
  62. if("@REMOTE@" STREQUAL "")
  63. message(FATAL_ERROR "REMOTE can't be empty")
  64. endif()
  65. if(EXISTS "@LOCAL@")
  66. check_file_hash(has_hash hash_is_good)
  67. if(has_hash)
  68. if(hash_is_good)
  69. message(STATUS "File already exists and hash match (skip download):
  70. file='@LOCAL@'
  71. @ALGO@='@EXPECT_VALUE@'"
  72. )
  73. return()
  74. else()
  75. message(STATUS "File already exists but hash mismatch. Removing...")
  76. file(REMOVE "@LOCAL@")
  77. endif()
  78. else()
  79. message(STATUS "File already exists but no hash specified (use URL_HASH):
  80. file='@LOCAL@'
  81. Old file will be removed and new file downloaded from URL."
  82. )
  83. file(REMOVE "@LOCAL@")
  84. endif()
  85. endif()
  86. set(retry_number 5)
  87. message(STATUS "Downloading...
  88. dst='@LOCAL@'
  89. timeout='@TIMEOUT_MSG@'"
  90. )
  91. foreach(i RANGE ${retry_number})
  92. sleep_before_download(${i})
  93. foreach(url @REMOTE@)
  94. message(STATUS "Using src='${url}'")
  95. @TLS_VERIFY_CODE@
  96. @TLS_CAINFO_CODE@
  97. @NETRC_CODE@
  98. @NETRC_FILE_CODE@
  99. file(
  100. DOWNLOAD
  101. "${url}" "@LOCAL@"
  102. @SHOW_PROGRESS@
  103. @TIMEOUT_ARGS@
  104. STATUS status
  105. LOG log
  106. @USERPWD_ARGS@
  107. @HTTP_HEADERS_ARGS@
  108. )
  109. list(GET status 0 status_code)
  110. list(GET status 1 status_string)
  111. if(status_code EQUAL 0)
  112. check_file_hash(has_hash hash_is_good)
  113. if(has_hash AND NOT hash_is_good)
  114. message(STATUS "Hash mismatch, removing...")
  115. file(REMOVE "@LOCAL@")
  116. else()
  117. message(STATUS "Downloading... done")
  118. return()
  119. endif()
  120. else()
  121. string(APPEND logFailedURLs "error: downloading '${url}' failed
  122. status_code: ${status_code}
  123. status_string: ${status_string}
  124. log:
  125. --- LOG BEGIN ---
  126. ${log}
  127. --- LOG END ---
  128. "
  129. )
  130. endif()
  131. endforeach()
  132. endforeach()
  133. message(FATAL_ERROR "Each download failed!
  134. ${logFailedURLs}
  135. "
  136. )