]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/tools/vcpkg/scripts/cmake/vcpkg_download_distfile.cmake
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / tools / vcpkg / scripts / cmake / vcpkg_download_distfile.cmake
1 #[===[.md:
2 # vcpkg_download_distfile
3
4 Download and cache a file needed for this port.
5
6 This helper should always be used instead of CMake's built-in `file(DOWNLOAD)` command.
7
8 ## Usage
9 ```cmake
10 vcpkg_download_distfile(
11 <OUT_VARIABLE>
12 URLS <http://mainUrl> <http://mirror1>...
13 FILENAME <output.zip>
14 SHA512 <5981de...>
15 )
16 ```
17 ## Parameters
18 ### OUT_VARIABLE
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.
20
21 ### URLS
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.
23
24 ### FILENAME
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.
26
27 ### SHA512
28 The expected hash for the file.
29
30 If this doesn't match the downloaded version, the build will be terminated with a message describing the mismatch.
31
32 ### QUIET
33 Suppress output on cache hit
34
35 ### SKIP_SHA512
36 Skip SHA512 hash check for file.
37
38 This switch is only valid when building with the `--head` command line flag.
39
40 ### HEADERS
41 A list of headers to append to the download request. This can be used for authentication during a download.
42
43 Headers should be specified as "<header-name>: <header-value>".
44
45 ## Notes
46 The helper [`vcpkg_from_github`](vcpkg_from_github.md) should be used for downloading from GitHub projects.
47
48 ## Examples
49
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)
53 #]===]
54
55 include(vcpkg_execute_in_download_mode)
56
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}")
63
64 if(NOT DEFINED vcpkg_download_distfile_URLS)
65 message(FATAL_ERROR "vcpkg_download_distfile requires a URLS argument.")
66 endif()
67 if(NOT DEFINED vcpkg_download_distfile_FILENAME)
68 message(FATAL_ERROR "vcpkg_download_distfile requires a FILENAME argument.")
69 endif()
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.")
73 endif()
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.")
76 endif()
77 endif()
78
79 set(downloaded_file_path ${DOWNLOADS}/${vcpkg_download_distfile_FILENAME})
80 set(download_file_path_part "${DOWNLOADS}/temp/${vcpkg_download_distfile_FILENAME}")
81
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")
86
87 file(GLOB temp_files "${DOWNLOADS}/temp")
88 file(REMOVE_RECURSE ${temp_files})
89 else()
90 file(MAKE_DIRECTORY "${DOWNLOADS}/temp")
91 endif()
92
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.
96 return()
97 endif()
98 if(vcpkg_download_distfile_SKIP_SHA512)
99 message(STATUS "Skipping hash check for ${FILE_PATH}.")
100 return()
101 endif()
102
103 file(SHA512 ${FILE_PATH} FILE_HASH)
104 if(NOT FILE_HASH STREQUAL vcpkg_download_distfile_SHA512)
105 message(FATAL_ERROR
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")
111 endif()
112 endfunction()
113
114 if(EXISTS "${downloaded_file_path}")
115 if(NOT vcpkg_download_distfile_QUIET)
116 message(STATUS "Using cached ${downloaded_file_path}")
117 endif()
118 test_hash("${downloaded_file_path}" "cached file" "Please delete the file and retry if this file should be downloaded again.")
119 else()
120 if(_VCPKG_NO_DOWNLOADS)
121 message(FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist.")
122 endif()
123
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}")
132 endforeach()
133 endif()
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
138 ${request_headers}
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}
143 )
144 if (NOT "${error_code}" STREQUAL "0")
145 message(STATUS
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"
152 )
153 set(download_success 0)
154 else()
155 file(REMOVE
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
159 )
160 set(download_success 1)
161 endif()
162 else()
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})
168 endforeach()
169 endif()
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)
175 else()
176 set(download_success 1)
177 break()
178 endif()
179 endforeach(url)
180 endif()
181
182 if (NOT vcpkg_download_distfile_SILENT_EXIT)
183 if (NOT download_success)
184 message(FATAL_ERROR
185 " \n"
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"
189 " \n"
190 " If error with status 4 (Issue #15434),\n"
191 " try setting \"http://user:password@your-proxy-ip-address:port/\".\n"
192 " \n"
193 " Otherwise, please submit an issue at https://github.com/Microsoft/vcpkg/issues\n")
194 else()
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})
199 endif()
200 else()
201 if (NOT download_success)
202 message(WARNING
203 " \n"
204 " Failed to download file.\n")
205 else()
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})
210 endif()
211 endif()
212 endif()
213 set(${VAR} ${downloaded_file_path} PARENT_SCOPE)
214 endfunction()