1
0

ExternalProject-download.cmake.in 4.0 KB

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