2 # vcpkg_download_distfile
4 Download and cache a file needed for this port.
6 This helper should always be used instead of CMake's built-in `file(DOWNLOAD)` command.
10 vcpkg_download_distfile(
12 URLS <http://mainUrl> <http://mirror1>...
19 This variable will be set to the full path to the downloaded file. This can then immediately be passed in to [`vcpkg_extract_source_archive`](vcpkg_extract_source_archive.md) for sources.
22 A list of URLs to be consulted. They will be tried in order until one of the downloaded files successfully matches the SHA512 given.
25 The local name for the file. Files are shared between ports, so the file may need to be renamed to make it clearly attributed to this port and avoid conflicts.
28 The expected hash for the file.
30 If this doesn't match the downloaded version, the build will be terminated with a message describing the mismatch.
33 Suppress output on cache hit
36 Skip SHA512 hash check for file.
38 This switch is only valid when building with the `--head` command line flag.
41 A list of headers to append to the download request. This can be used for authentication during a download.
43 Headers should be specified as "<header-name>: <header-value>".
46 The helper [`vcpkg_from_github`](vcpkg_from_github.md) should be used for downloading from GitHub projects.
50 * [apr](https://github.com/Microsoft/vcpkg/blob/master/ports/apr/portfile.cmake)
51 * [fontconfig](https://github.com/Microsoft/vcpkg/blob/master/ports/fontconfig/portfile.cmake)
52 * [freetype](https://github.com/Microsoft/vcpkg/blob/master/ports/freetype/portfile.cmake)
55 include(vcpkg_execute_in_download_mode)
57 function(vcpkg_download_distfile VAR)
58 set(options SKIP_SHA512 SILENT_EXIT QUIET)
59 set(oneValueArgs FILENAME SHA512)
60 set(multipleValuesArgs URLS HEADERS)
61 # parse parameters such that semicolons in options arguments to COMMAND don't get erased
62 cmake_parse_arguments(PARSE_ARGV 1 vcpkg_download_distfile "${options}" "${oneValueArgs}" "${multipleValuesArgs}")
64 if(NOT DEFINED vcpkg_download_distfile_URLS)
65 message(FATAL_ERROR "vcpkg_download_distfile requires a URLS argument.")
67 if(NOT DEFINED vcpkg_download_distfile_FILENAME)
68 message(FATAL_ERROR "vcpkg_download_distfile requires a FILENAME argument.")
70 if(NOT _VCPKG_INTERNAL_NO_HASH_CHECK)
71 if(NOT vcpkg_download_distfile_SKIP_SHA512 AND NOT DEFINED vcpkg_download_distfile_SHA512)
72 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.")
74 if(vcpkg_download_distfile_SKIP_SHA512 AND DEFINED vcpkg_download_distfile_SHA512)
75 message(FATAL_ERROR "vcpkg_download_distfile must not be passed both SHA512 and SKIP_SHA512.")
79 set(downloaded_file_path ${DOWNLOADS}/${vcpkg_download_distfile_FILENAME})
80 set(download_file_path_part "${DOWNLOADS}/temp/${vcpkg_download_distfile_FILENAME}")
82 # Works around issue #3399
83 if(IS_DIRECTORY "${DOWNLOADS}/temp")
84 # Delete "temp0" directory created by the old version of vcpkg
85 file(REMOVE_RECURSE "${DOWNLOADS}/temp0")
87 file(GLOB temp_files "${DOWNLOADS}/temp")
88 file(REMOVE_RECURSE ${temp_files})
90 file(MAKE_DIRECTORY "${DOWNLOADS}/temp")
93 function(test_hash FILE_PATH FILE_KIND CUSTOM_ERROR_ADVICE)
94 if(_VCPKG_INTERNAL_NO_HASH_CHECK)
95 # When using the internal hash skip, do not output an explicit message.
98 if(vcpkg_download_distfile_SKIP_SHA512)
99 message(STATUS "Skipping hash check for ${FILE_PATH}.")
103 file(SHA512 ${FILE_PATH} FILE_HASH)
104 if(NOT FILE_HASH STREQUAL vcpkg_download_distfile_SHA512)
106 "\nFile does not have expected hash:\n"
107 " File path: [ ${FILE_PATH} ]\n"
108 " Expected hash: [ ${vcpkg_download_distfile_SHA512} ]\n"
109 " Actual hash: [ ${FILE_HASH} ]\n"
110 "${CUSTOM_ERROR_ADVICE}\n")
114 if(EXISTS "${downloaded_file_path}")
115 if(NOT vcpkg_download_distfile_QUIET)
116 message(STATUS "Using cached ${downloaded_file_path}")
118 test_hash("${downloaded_file_path}" "cached file" "Please delete the file and retry if this file should be downloaded again.")
120 if(_VCPKG_NO_DOWNLOADS)
121 message(FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist.")
124 # Tries to download the file.
125 list(GET vcpkg_download_distfile_URLS 0 SAMPLE_URL)
126 if(_VCPKG_DOWNLOAD_TOOL STREQUAL "ARIA2" AND NOT SAMPLE_URL MATCHES "aria2")
127 vcpkg_find_acquire_program("ARIA2")
128 message(STATUS "Downloading ${vcpkg_download_distfile_FILENAME}...")
129 if(vcpkg_download_distfile_HEADERS)
130 foreach(header ${vcpkg_download_distfile_HEADERS})
131 list(APPEND request_headers "--header=${header}")
134 vcpkg_execute_in_download_mode(
135 COMMAND ${ARIA2} ${vcpkg_download_distfile_URLS}
136 -o temp/${vcpkg_download_distfile_FILENAME}
137 -l download-${vcpkg_download_distfile_FILENAME}-detailed.log
139 OUTPUT_FILE download-${vcpkg_download_distfile_FILENAME}-out.log
140 ERROR_FILE download-${vcpkg_download_distfile_FILENAME}-err.log
141 RESULT_VARIABLE error_code
142 WORKING_DIRECTORY ${DOWNLOADS}
144 if (NOT "${error_code}" STREQUAL "0")
146 "Downloading ${vcpkg_download_distfile_FILENAME}... Failed.\n"
147 " Exit Code: ${error_code}\n"
148 " See logs for more information:\n"
149 " ${DOWNLOADS}/download-${vcpkg_download_distfile_FILENAME}-out.log\n"
150 " ${DOWNLOADS}/download-${vcpkg_download_distfile_FILENAME}-err.log\n"
151 " ${DOWNLOADS}/download-${vcpkg_download_distfile_FILENAME}-detailed.log\n"
153 set(download_success 0)
156 ${DOWNLOADS}/download-${vcpkg_download_distfile_FILENAME}-out.log
157 ${DOWNLOADS}/download-${vcpkg_download_distfile_FILENAME}-err.log
158 ${DOWNLOADS}/download-${vcpkg_download_distfile_FILENAME}-detailed.log
160 set(download_success 1)
163 foreach(url IN LISTS vcpkg_download_distfile_URLS)
164 message(STATUS "Downloading ${url} -> ${vcpkg_download_distfile_FILENAME}...")
165 if(vcpkg_download_distfile_HEADERS)
166 foreach(header ${vcpkg_download_distfile_HEADERS})
167 list(APPEND request_headers HTTPHEADER ${header})
170 file(DOWNLOAD "${url}" "${download_file_path_part}" STATUS download_status ${request_headers})
171 list(GET download_status 0 status_code)
172 if (NOT "${status_code}" STREQUAL "0")
173 message(STATUS "Downloading ${url}... Failed. Status: ${download_status}")
174 set(download_success 0)
176 set(download_success 1)
182 if (NOT vcpkg_download_distfile_SILENT_EXIT)
183 if (NOT download_success)
186 " Failed to download file.\n"
187 " If you use a proxy, please set the HTTPS_PROXY and HTTP_PROXY environment\n"
188 " variables to \"https://user:password@your-proxy-ip-address:port/\".\n"
190 " If error with status 4 (Issue #15434),\n"
191 " try setting \"http://user:password@your-proxy-ip-address:port/\".\n"
193 " Otherwise, please submit an issue at https://github.com/Microsoft/vcpkg/issues\n")
195 test_hash("${download_file_path_part}" "downloaded file" "The file may have been corrupted in transit. This can be caused by proxies. If you use a proxy, please set the HTTPS_PROXY and HTTP_PROXY environment variables to \"https://user:password@your-proxy-ip-address:port/\".\n")
196 get_filename_component(downloaded_file_dir "${downloaded_file_path}" DIRECTORY)
197 file(MAKE_DIRECTORY "${downloaded_file_dir}")
198 file(RENAME ${download_file_path_part} ${downloaded_file_path})
201 if (NOT download_success)
204 " Failed to download file.\n")
206 test_hash("${download_file_path_part}" "downloaded file" "The file may have been corrupted in transit. This can be caused by proxies. If you use a proxy, please set the HTTPS_PROXY and HTTP_PROXY environment variables to \"https://user:password@your-proxy-ip-address:port/\".\n")
207 get_filename_component(downloaded_file_dir "${downloaded_file_path}" DIRECTORY)
208 file(MAKE_DIRECTORY "${downloaded_file_dir}")
209 file(RENAME ${download_file_path_part} ${downloaded_file_path})
213 set(${VAR} ${downloaded_file_path} PARENT_SCOPE)