1
0

download.cmake.in 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(VERBOSE "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(VERBOSE "@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(VERBOSE "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(VERBOSE "Retry after ${sleep_seconds} seconds (attempt #${attempt}) ...")
  57. execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep "${sleep_seconds}")
  58. endfunction()
  59. if(EXISTS "@LOCAL@")
  60. check_file_hash(has_hash hash_is_good)
  61. if(has_hash)
  62. if(hash_is_good)
  63. message(VERBOSE "File already exists and hash match (skip download):
  64. file='@LOCAL@'
  65. @ALGO@='@EXPECT_VALUE@'"
  66. )
  67. return()
  68. else()
  69. message(VERBOSE "File already exists but hash mismatch. Removing...")
  70. file(REMOVE "@LOCAL@")
  71. endif()
  72. else()
  73. message(VERBOSE "File already exists but no hash specified (use URL_HASH):
  74. file='@LOCAL@'
  75. Old file will be removed and new file downloaded from URL."
  76. )
  77. file(REMOVE "@LOCAL@")
  78. endif()
  79. endif()
  80. set(retry_number 5)
  81. message(VERBOSE "Downloading...
  82. dst='@LOCAL@'
  83. timeout='@TIMEOUT_MSG@'
  84. inactivity timeout='@INACTIVITY_TIMEOUT_MSG@'"
  85. )
  86. set(download_retry_codes 7 6 8 15 28 35)
  87. set(skip_url_list)
  88. set(status_code)
  89. foreach(i RANGE ${retry_number})
  90. if(status_code IN_LIST download_retry_codes)
  91. sleep_before_download(${i})
  92. endif()
  93. foreach(url IN ITEMS @REMOTE@)
  94. if(NOT url IN_LIST skip_url_list)
  95. message(VERBOSE "Using src='${url}'")
  96. @TLS_VERSION_CODE@
  97. @TLS_VERIFY_CODE@
  98. @TLS_CAINFO_CODE@
  99. @NETRC_CODE@
  100. @NETRC_FILE_CODE@
  101. file(
  102. DOWNLOAD
  103. "${url}" "@LOCAL@"
  104. @SHOW_PROGRESS@
  105. @TIMEOUT_ARGS@
  106. @INACTIVITY_TIMEOUT_ARGS@
  107. STATUS status
  108. LOG log
  109. @USERPWD_ARGS@
  110. @HTTP_HEADERS_ARGS@
  111. )
  112. list(GET status 0 status_code)
  113. list(GET status 1 status_string)
  114. if(status_code EQUAL 0)
  115. check_file_hash(has_hash hash_is_good)
  116. if(has_hash AND NOT hash_is_good)
  117. message(VERBOSE "Hash mismatch, removing...")
  118. file(REMOVE "@LOCAL@")
  119. else()
  120. message(VERBOSE "Downloading... done")
  121. return()
  122. endif()
  123. else()
  124. string(APPEND logFailedURLs "error: downloading '${url}' failed
  125. status_code: ${status_code}
  126. status_string: ${status_string}
  127. log:
  128. --- LOG BEGIN ---
  129. ${log}
  130. --- LOG END ---
  131. "
  132. )
  133. if(NOT status_code IN_LIST download_retry_codes)
  134. list(APPEND skip_url_list "${url}")
  135. break()
  136. endif()
  137. endif()
  138. endif()
  139. endforeach()
  140. endforeach()
  141. message(FATAL_ERROR "Each download failed!
  142. ${logFailedURLs}
  143. "
  144. )