download.cmake.in 5.5 KB

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