1
0

ExternalProject-download.cmake.in 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. inactivity timeout='@INACTIVITY_TIMEOUT_MSG@'"
  91. )
  92. set(download_retry_codes 7 6 8 15)
  93. set(skip_url_list)
  94. set(status_code)
  95. foreach(i RANGE ${retry_number})
  96. if(status_code IN_LIST download_retry_codes)
  97. sleep_before_download(${i})
  98. endif()
  99. foreach(url @REMOTE@)
  100. if(NOT url IN_LIST skip_url_list)
  101. message(STATUS "Using src='${url}'")
  102. @TLS_VERIFY_CODE@
  103. @TLS_CAINFO_CODE@
  104. @NETRC_CODE@
  105. @NETRC_FILE_CODE@
  106. file(
  107. DOWNLOAD
  108. "${url}" "@LOCAL@"
  109. @SHOW_PROGRESS@
  110. @TIMEOUT_ARGS@
  111. @INACTIVITY_TIMEOUT_ARGS@
  112. STATUS status
  113. LOG log
  114. @USERPWD_ARGS@
  115. @HTTP_HEADERS_ARGS@
  116. )
  117. list(GET status 0 status_code)
  118. list(GET status 1 status_string)
  119. if(status_code EQUAL 0)
  120. check_file_hash(has_hash hash_is_good)
  121. if(has_hash AND NOT hash_is_good)
  122. message(STATUS "Hash mismatch, removing...")
  123. file(REMOVE "@LOCAL@")
  124. else()
  125. message(STATUS "Downloading... done")
  126. return()
  127. endif()
  128. else()
  129. string(APPEND logFailedURLs "error: downloading '${url}' failed
  130. status_code: ${status_code}
  131. status_string: ${status_string}
  132. log:
  133. --- LOG BEGIN ---
  134. ${log}
  135. --- LOG END ---
  136. "
  137. )
  138. if(NOT status_code IN_LIST download_retry_codes)
  139. list(APPEND skip_url_list "${url}")
  140. break()
  141. endif()
  142. endif()
  143. endif()
  144. endforeach()
  145. endforeach()
  146. message(FATAL_ERROR "Each download failed!
  147. ${logFailedURLs}
  148. "
  149. )