Skip to content

Commit

Permalink
Delete handling for --x-use-aria2, avoid duplicate file hashing, and …
Browse files Browse the repository at this point in the history
…fix hash mismatch message. (#43418)
  • Loading branch information
BillyONeal authored Jan 31, 2025
1 parent e9c53cd commit d504de0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 195 deletions.
226 changes: 59 additions & 167 deletions scripts/cmake/vcpkg_download_distfile.cmake
Original file line number Diff line number Diff line change
@@ -1,93 +1,3 @@
function(z_vcpkg_check_hash result file_path sha512)
file(SHA512 "${file_path}" file_hash)
string(TOLOWER "${sha512}" sha512_lower)
string(COMPARE EQUAL "${file_hash}" "${sha512_lower}" hash_match)
set("${result}" "${hash_match}" PARENT_SCOPE)
endfunction()

function(z_vcpkg_download_distfile_test_hash file_path kind error_advice sha512 skip_sha512)
if(_VCPKG_INTERNAL_NO_HASH_CHECK)
# When using the internal hash skip, do not output an explicit message.
return()
endif()
if(skip_sha512)
message(STATUS "Skipping hash check for ${file_path}.")
return()
endif()

set(hash_match OFF)
z_vcpkg_check_hash(hash_match "${file_path}" "${sha512}")

if(NOT hash_match)
message(FATAL_ERROR
"\nFile does not have expected hash:\n"
" File path: [ ${file_path} ]\n"
" Expected hash: [ ${sha512} ]\n"
" Actual hash: [ ${file_hash} ]\n"
"${error_advice}\n")
endif()
endfunction()

function(z_vcpkg_download_distfile_via_aria)
cmake_parse_arguments(PARSE_ARGV 1 arg
"SKIP_SHA512"
"FILENAME;SHA512"
"URLS;HEADERS"
)

message(STATUS "Downloading ${arg_FILENAME}...")

vcpkg_list(SET headers_param)
foreach(header IN LISTS arg_HEADERS)
vcpkg_list(APPEND headers_param "--header=${header}")
endforeach()

foreach(URL IN LISTS arg_URLS)
debug_message("Download Command: ${ARIA2} ${URL} -o temp/${filename} -l download-${filename}-detailed.log ${headers_param}")
vcpkg_execute_in_download_mode(
COMMAND ${ARIA2} ${URL}
-o temp/${arg_FILENAME}
-l download-${arg_FILENAME}-detailed.log
${headers_param}
OUTPUT_FILE download-${arg_FILENAME}-out.log
ERROR_FILE download-${arg_FILENAME}-err.log
RESULT_VARIABLE error_code
WORKING_DIRECTORY "${DOWNLOADS}"
)

if ("${error_code}" STREQUAL "0")
break()
endif()
endforeach()
if (NOT "${error_code}" STREQUAL "0")
message(STATUS
"Downloading ${arg_FILENAME}... Failed.\n"
" Exit Code: ${error_code}\n"
" See logs for more information:\n"
" ${DOWNLOADS}/download-${arg_FILENAME}-out.log\n"
" ${DOWNLOADS}/download-${arg_FILENAME}-err.log\n"
" ${DOWNLOADS}/download-${arg_FILENAME}-detailed.log\n"
)
z_vcpkg_download_distfile_show_proxy_and_fail("${error_code}")
else()
z_vcpkg_download_distfile_test_hash(
"${DOWNLOADS}/temp/${arg_FILENAME}"
"downloaded file"
"The file may have been corrupted in transit."
"${arg_SHA512}"
${arg_SKIP_SHA512}
)
file(REMOVE
${DOWNLOADS}/download-${arg_FILENAME}-out.log
${DOWNLOADS}/download-${arg_FILENAME}-err.log
${DOWNLOADS}/download-${arg_FILENAME}-detailed.log
)
get_filename_component(downloaded_file_dir "${downloaded_file_path}" DIRECTORY)
file(MAKE_DIRECTORY "${downloaded_file_dir}")
file(RENAME "${DOWNLOADS}/temp/${arg_FILENAME}" "${downloaded_file_path}")
endif()
endfunction()

function(vcpkg_download_distfile out_var)
cmake_parse_arguments(PARSE_ARGV 1 arg
"SKIP_SHA512;SILENT_EXIT;QUIET;ALWAYS_REDOWNLOAD"
Expand All @@ -102,17 +12,19 @@ function(vcpkg_download_distfile out_var)
message(FATAL_ERROR "vcpkg_download_distfile requires a FILENAME argument.")
endif()
if(arg_SILENT_EXIT)
message(WARNING "SILENT_EXIT has been deprecated as an argument to vcpkg_download_distfile -- remove the argument to resolve this warning")
message(WARNING "SILENT_EXIT no longer has any effect. To resolve this warning, remove SILENT_EXIT.")
endif()

# Note that arg_ALWAYS_REDOWNLOAD implies arg_SKIP_SHA512, and NOT arg_SKIP_SHA512 implies NOT arg_ALWAYS_REDOWNLOAD
if(arg_ALWAYS_REDOWNLOAD AND NOT arg_SKIP_SHA512)
message(FATAL_ERROR "ALWAYS_REDOWNLOAD option requires SKIP_SHA512 as well")
message(FATAL_ERROR "ALWAYS_REDOWNLOAD requires SKIP_SHA512")
endif()

if(NOT arg_SKIP_SHA512 AND NOT DEFINED arg_SHA512)
message(FATAL_ERROR "vcpkg_download_distfile requires a SHA512 argument.
If you do not know the SHA512, add it as 'SHA512 0' and re-run this command.")
If you do not know the SHA512, add it as 'SHA512 0' and retry.")
elseif(arg_SKIP_SHA512 AND DEFINED arg_SHA512)
message(FATAL_ERROR "vcpkg_download_distfile must not be passed both SHA512 and SKIP_SHA512.")
message(FATAL_ERROR "SHA512 may not be used with SKIP_SHA512.")
endif()

if(_VCPKG_INTERNAL_NO_HASH_CHECK)
Expand All @@ -128,108 +40,88 @@ If you do not know the SHA512, add it as 'SHA512 0' and re-run this command.")
message(FATAL_ERROR "Invalid SHA512: ${arg_SHA512}.
If you do not know the file's SHA512, set this to \"0\".")
endif()

string(TOLOWER "${arg_SHA512}" arg_SHA512)
endif()
endif()

set(downloaded_file_path "${DOWNLOADS}/${arg_FILENAME}")

if(EXISTS "${downloaded_file_path}" AND NOT arg_SKIP_SHA512)
set(hash_match OFF)
z_vcpkg_check_hash(hash_match "${downloaded_file_path}" "${arg_SHA512}")

if(NOT hash_match)
if(EXISTS "${downloaded_file_path}")
if(arg_SKIP_SHA512)
if(NOT arg_ALWAYS_REDOWNLOAD)
if(NOT _VCPKG_INTERNAL_NO_HASH_CHECK)
message(STATUS "Skipping hash check and using cached ${arg_FILENAME}")
endif()

set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE)
return()
endif()
else()
# Note that NOT arg_SKIP_SHA512 implies NOT arg_ALWAYS_REDOWNLOAD
file(SHA512 "${downloaded_file_path}" file_hash)
if("${file_hash}" STREQUAL "${arg_SHA512}")
message(STATUS "Using cached ${arg_FILENAME}")
set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE)
return()
endif()

# The existing file hash mismatches. Perhaps the expected SHA512 changed. Try adding the expected SHA512
# into the file name and try again to hopefully not conflict.
get_filename_component(filename_component "${arg_FILENAME}" NAME_WE)
get_filename_component(extension_component "${arg_FILENAME}" EXT)
get_filename_component(directory_component "${arg_FILENAME}" DIRECTORY)

string(SUBSTRING "${arg_SHA512}" 0 8 hash)
set(arg_FILENAME "${directory_component}${filename_component}-${hash}${extension_component}")
set(downloaded_file_path "${DOWNLOADS}/${arg_FILENAME}")
if(EXISTS "${downloaded_file_path}")
if(_VCPKG_NO_DOWNLOADS)
set(advice_message "note: Downloads are disabled. Please ensure that the expected file is placed at ${downloaded_file_path} and retry.")
else()
set(advice_message "note: You may be able to resolve this failure by redownloading the file. To do so, delete ${downloaded_file_path} and retry.")
endif()

file(SHA512 "${downloaded_file_path}" file_hash)
if("${file_hash}" STREQUAL "${arg_SHA512}")
message(STATUS "Using cached ${arg_FILENAME}")
set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE)
return()
endif()

# Note that the extra leading spaces are here to prevent CMake from badly attempting to wrap this
message(FATAL_ERROR
" ${downloaded_file_path}: error: existing downloaded file had an unexpected hash\n"
" Expected: ${arg_SHA512}\n"
" Actual : ${file_hash}\n"
" ${advice_message}")
endif()
endif()
endif()

set(download_file_path_part "${DOWNLOADS}/temp/${arg_FILENAME}")

# Works around issue #3399
# Delete "temp0" directory created by the old version of vcpkg
file(REMOVE_RECURSE "${DOWNLOADS}/temp0")
file(REMOVE_RECURSE "${DOWNLOADS}/temp")
file(MAKE_DIRECTORY "${DOWNLOADS}/temp")

# check if file with same name already exists in downloads
if(EXISTS "${downloaded_file_path}" AND NOT arg_ALWAYS_REDOWNLOAD)
set(advice_message "The cached file SHA512 doesn't match. The file may have been corrupted.")
if(_VCPKG_NO_DOWNLOADS)
string(APPEND advice_message " Downloads are disabled please provide a valid file at path ${downloaded_file_path} and retry.")
else()
string(APPEND advice_message " To re-download this file please delete cached file at path ${downloaded_file_path} and retry.")
endif()

z_vcpkg_download_distfile_test_hash(
"${downloaded_file_path}"
"cached file"
"${advice_message}"
"${arg_SHA512}"
"${arg_SKIP_SHA512}"
)
message(STATUS "Using cached ${arg_FILENAME}.")
endif()

# vcpkg_download_distfile_ALWAYS_REDOWNLOAD only triggers when NOT _VCPKG_NO_DOWNLOADS
# this could be de-morgan'd out but it's more clear this way
if(_VCPKG_NO_DOWNLOADS)
if(NOT EXISTS "${downloaded_file_path}")
message(FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist.")
endif()

set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE)
return()
message(FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist.")
endif()

if(NOT arg_DISABLE_ARIA2 AND _VCPKG_DOWNLOAD_TOOL STREQUAL "ARIA2" AND NOT EXISTS "${downloaded_file_path}")
if (arg_SKIP_SHA512)
set(OPTION_SKIP_SHA512 "SKIP_SHA512")
endif()
z_vcpkg_download_distfile_via_aria(
"${OPTION_SKIP_SHA512}"
FILENAME "${arg_FILENAME}"
SHA512 "${arg_SHA512}"
URLS "${arg_URLS}"
HEADERS "${arg_HEADERS}"
)
set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE)
return()
endif()

vcpkg_list(SET urls_param)
vcpkg_list(SET params "x-download" "${downloaded_file_path}")
foreach(url IN LISTS arg_URLS)
vcpkg_list(APPEND urls_param "--url=${url}")
vcpkg_list(APPEND params "--url=${url}")
endforeach()

vcpkg_list(SET headers_param)
foreach(header IN LISTS arg_HEADERS)
list(APPEND headers_param "--header=${header}")
list(APPEND params "--header=${header}")
endforeach()

if(arg_SKIP_SHA512)
vcpkg_list(SET sha512_param "--skip-sha512")
vcpkg_list(APPEND params "--skip-sha512")
else()
vcpkg_list(SET sha512_param "--sha512=${arg_SHA512}")
vcpkg_list(APPEND params "--sha512=${arg_SHA512}")
endif()

if(NOT EXISTS "${downloaded_file_path}" OR arg_ALWAYS_REDOWNLOAD)
vcpkg_execute_in_download_mode(
COMMAND "$ENV{VCPKG_COMMAND}" x-download
"${downloaded_file_path}"
${sha512_param}
${urls_param}
${headers_param}
RESULT_VARIABLE error_code
WORKING_DIRECTORY "${DOWNLOADS}"
)
if(NOT "${error_code}" EQUAL "0")
message(FATAL_ERROR "Download failed, halting portfile.")
endif()
vcpkg_execute_in_download_mode(COMMAND "$ENV{VCPKG_COMMAND}" ${params} RESULT_VARIABLE error_code)
if(NOT "${error_code}" EQUAL "0")
message(FATAL_ERROR "Download failed, halting portfile.")
endif()

set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE)
Expand Down
8 changes: 0 additions & 8 deletions scripts/cmake/vcpkg_find_acquire_program(ARIA2).cmake

This file was deleted.

3 changes: 1 addition & 2 deletions scripts/test_ports/vcpkg-find-acquire-program/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ if(VCPKG_HOST_IS_WINDOWS)
file(REMOVE_RECURSE "${DOWNLOADS}/tools/nasm")
file(MAKE_DIRECTORY "${DOWNLOADS}/tools/nasm")

list(APPEND variables 7Z ARIA2 CLANG DARK DOXYGEN GASPREPROCESSOR GO GPERF JOM NASM NUGET PYTHON2 RUBY SWIG)
list(APPEND variables 7Z CLANG DARK DOXYGEN GASPREPROCESSOR GO GPERF JOM NASM NUGET PYTHON2 RUBY SWIG)
vcpkg_find_acquire_program(7Z)
vcpkg_find_acquire_program(ARIA2)
vcpkg_find_acquire_program(CLANG)
vcpkg_find_acquire_program(DARK)
vcpkg_find_acquire_program(DOXYGEN)
Expand Down
18 changes: 0 additions & 18 deletions scripts/vcpkg-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,6 @@
"url": "https://github.com/ip7z/7zip/releases/download/24.09/7zr.exe",
"sha512": "44d8504a693ad4d6b79631b653fc19b572de6bbe38713b53c45d9c9d5d3710aa8df93ee867a2a24419ebe883b8255fd18f30f8cf374b2242145fd6acb2189659"
},
{
"name": "aria2",
"os": "windows",
"version": "1.37.0",
"executable": "aria2-1.37.0-win-64bit-build1/aria2c.exe",
"url": "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-64bit-build1.zip",
"sha512": "6d78405da9cf5639dbe8174787002161b8124d73880fb57cc8c0a3a63982f84e46df4e626990c58f23452965ad925f0d37cb9147e99b25c3d7ca0ea49602f34d",
"archive": "aria2-1.37.0-win-64bit-build1.zip"
},
{
"name": "aria2",
"os": "osx",
"version": "1.35.0",
"executable": "aria2-1.35.0/bin/aria2c",
"url": "https://github.com/aria2/aria2/releases/download/release-1.35.0/aria2-1.35.0-osx-darwin.tar.bz2",
"sha512": "3bb32b7d55347d1af37c6f4ebf0e20b38ce51c37a1baf92f7ad1762000539a03413dd679a6d902fdb1805fa71917300c9692aceee012eb06ecdff10491137aec",
"archive": "aria2-1.35.0-osx-darwin.tar.bz2"
},
{
"name": "ninja",
"os": "windows",
Expand Down

0 comments on commit d504de0

Please sign in to comment.