This commit is contained in:
hardliner66 2024-10-14 23:36:54 +02:00
parent c7fdb6445d
commit aeb1a72871
2 changed files with 124 additions and 114 deletions

View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.11)
project(DownloadTest)
cmake_minimum_required(VERSION 3.11)
# add to module path
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
@ -11,18 +12,21 @@ include(Download)
## include("${PROJECT_SOURCE_DIR}/cmake/Download.cmake")
# use like this
download_file_with_hash_check(
"http://127.0.0.1:8080/random_file.bin"
"${CMAKE_BINARY_DIR}/random_file.bin")
# or this
download_file_with_hash_check(
"http://127.0.0.1:8080/random_file.bin"
"${CMAKE_BINARY_DIR}/random_file.bin"
REQUIRED)
# or this
# download_file_with_hash_check(URL "http://127.0.0.1:8080/random_file.bin" DEST "${CMAKE_BINARY_DIR}/random_file.bin" REQUIRED)
# # or this
# download_file_with_hash_check(
# "http://127.0.0.1:8080/random_file.bin"
# "${CMAKE_BINARY_DIR}/random_file.bin")
# # or this
# download_file_with_hash_check(
# URL "http://127.0.0.1:8080/random_file.bin"
# DEST "${CMAKE_BINARY_DIR}/random_file.bin"
# REQUIRED)
# Or include directly

View File

@ -44,7 +44,6 @@ function(download_file_with_hash_check url dest)
if(ARG_HASH_URL)
set(hash_url "${ARG_HASH_URL}")
endif()
message(STATUS "${hash_url}")
check_file_reachable("${url}" RESULT_VAR file_reachable)
@ -116,6 +115,13 @@ function(download_file_with_hash_check url dest)
SHOW_PROGRESS
TIMEOUT 60)
list(GET download_status 0 download_status_code)
list(GET download_status 1 download_error_message)
if(NOT "${download_status_code}" STREQUAL "0")
file(REMOVE "${dest}")
message(FATAL "Failed to download file ${dest} from ${url}. Error: ${download_error_message}")
endif()
# Recheck the hash after downloading
file(SHA256 "${dest}" actual_hash)
if(NOT "${expected_hash}" STREQUAL "${actual_hash}")