2020-12-02 05:37:26 +08:00
#[===[.md:
# vcpkg_download_distfile
D o w n l o a d a n d c a c h e a f i l e n e e d e d f o r t h i s p o r t .
T h i s h e l p e r s h o u l d a l w a y s b e u s e d i n s t e a d o f C M a k e ' s b u i l t - i n ` file ( DOWNLOAD ) ` c o m m a n d .
## Usage
` ` ` c m a k e
vcpkg_download_distfile (
< O U T _ V A R I A B L E >
U R L S < h t t p : / / m a i n U r l > < h t t p : / / m i r r o r 1 > . . .
F I L E N A M E < o u t p u t . z i p >
S H A 5 1 2 < 5 9 8 1 d e . . . >
2021-07-15 03:45:18 +08:00
[ A L W A Y S _ R E D O W N L O A D ]
2020-12-02 05:37:26 +08:00
)
` ` `
## Parameters
### OUT_VARIABLE
T h i s v a r i a b l e w i l l b e s e t t o t h e f u l l p a t h t o t h e d o w n l o a d e d f i l e . T h i s c a n t h e n i m m e d i a t e l y b e p a s s e d i n t o [ ` v c p k g _ e x t r a c t _ s o u r c e _ a r c h i v e ` ] ( v c p k g _ e x t r a c t _ s o u r c e _ a r c h i v e . m d ) f o r s o u r c e s .
### URLS
A l i s t o f U R L s t o b e c o n s u l t e d . T h e y w i l l b e t r i e d i n o r d e r u n t i l o n e o f t h e d o w n l o a d e d f i l e s s u c c e s s f u l l y m a t c h e s t h e S H A 5 1 2 g i v e n .
### FILENAME
T h e l o c a l n a m e f o r t h e f i l e . F i l e s a r e s h a r e d b e t w e e n p o r t s , s o t h e f i l e m a y n e e d t o b e r e n a m e d t o m a k e i t c l e a r l y a t t r i b u t e d t o t h i s p o r t a n d a v o i d c o n f l i c t s .
### SHA512
T h e e x p e c t e d h a s h f o r t h e f i l e .
I f t h i s d o e s n ' t m a t c h t h e d o w n l o a d e d v e r s i o n , t h e b u i l d w i l l b e t e r m i n a t e d w i t h a m e s s a g e d e s c r i b i n g t h e m i s m a t c h .
### QUIET
S u p p r e s s o u t p u t o n c a c h e h i t
### SKIP_SHA512
S k i p S H A 5 1 2 h a s h c h e c k f o r f i l e .
T h i s s w i t c h i s o n l y v a l i d w h e n b u i l d i n g w i t h t h e ` - - h e a d ` c o m m a n d l i n e f l a g .
2021-07-15 03:45:18 +08:00
### ALWAYS_REDOWNLOAD
A v o i d c a c h i n g ; t h i s i s a R E S T c a l l o r o t h e r w i s e u n s t a b l e .
R e q u i r e s ` S K I P _ S H A 5 1 2 ` .
2020-12-02 05:37:26 +08:00
### HEADERS
A l i s t o f h e a d e r s t o a p p e n d t o t h e d o w n l o a d r e q u e s t . T h i s c a n b e u s e d f o r a u t h e n t i c a t i o n d u r i n g a d o w n l o a d .
H e a d e r s s h o u l d b e s p e c i f i e d a s " < h e a d e r - n a m e > : < h e a d e r - v a l u e > " .
## Notes
T h e h e l p e r [ ` v c p k g _ f r o m _ g i t h u b ` ] ( v c p k g _ f r o m _ g i t h u b . m d ) s h o u l d b e u s e d f o r d o w n l o a d i n g f r o m G i t H u b p r o j e c t s .
## Examples
* [ a p r ] ( h t t p s : / / g i t h u b . c o m / M i c r o s o f t / v c p k g / b l o b / m a s t e r / p o r t s / a p r / p o r t f i l e . c m a k e )
* [ f o n t c o n f i g ] ( h t t p s : / / g i t h u b . c o m / M i c r o s o f t / v c p k g / b l o b / m a s t e r / p o r t s / f o n t c o n f i g / p o r t f i l e . c m a k e )
* [ f r e e t y p e ] ( h t t p s : / / g i t h u b . c o m / M i c r o s o f t / v c p k g / b l o b / m a s t e r / p o r t s / f r e e t y p e / p o r t f i l e . c m a k e )
#]===]
2020-09-10 01:36:58 +08:00
include ( vcpkg_execute_in_download_mode )
2016-09-19 11:50:08 +08:00
function ( vcpkg_download_distfile VAR )
2021-07-15 03:45:18 +08:00
set ( options SKIP_SHA512 SILENT_EXIT QUIET ALWAYS_REDOWNLOAD )
2016-09-25 06:23:04 +08:00
set ( oneValueArgs FILENAME SHA512 )
2019-03-15 04:43:49 +08:00
set ( multipleValuesArgs URLS HEADERS )
2020-10-27 10:30:27 +08:00
# parse parameters such that semicolons in options arguments to COMMAND don't get erased
2020-09-26 20:10:38 +08:00
cmake_parse_arguments ( PARSE_ARGV 1 vcpkg_download_distfile "${options}" "${oneValueArgs}" "${multipleValuesArgs}" )
2017-11-03 09:54:10 +08:00
2017-12-21 19:47:02 +08:00
if ( NOT DEFINED vcpkg_download_distfile_URLS )
message ( FATAL_ERROR "vcpkg_download_distfile requires a URLS argument." )
endif ( )
if ( NOT DEFINED vcpkg_download_distfile_FILENAME )
message ( FATAL_ERROR "vcpkg_download_distfile requires a FILENAME argument." )
endif ( )
2021-06-05 07:48:26 +08:00
if ( vcpkg_download_distfile_SILENT_EXIT )
message ( WARNING "SILENT_EXIT has been deprecated as an argument to vcpkg_download_distfile -- remove the argument to resolve this warning" )
endif ( )
2021-07-15 03:45:18 +08:00
if ( vcpkg_download_distfile_ALWAYS_REDOWNLOAD AND NOT vcpkg_download_distfile_SKIP_SHA512 )
message ( FATAL_ERROR "ALWAYS_REDOWNLOAD option requires SKIP_SHA512 as well" )
endif ( )
2021-06-05 07:48:26 +08:00
if ( _VCPKG_INTERNAL_NO_HASH_CHECK )
set ( vcpkg_download_distfile_SKIP_SHA512 1 )
else ( )
2018-01-14 11:49:04 +08:00
if ( NOT vcpkg_download_distfile_SKIP_SHA512 AND NOT DEFINED vcpkg_download_distfile_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." )
endif ( )
if ( vcpkg_download_distfile_SKIP_SHA512 AND DEFINED vcpkg_download_distfile_SHA512 )
message ( FATAL_ERROR "vcpkg_download_distfile must not be passed both SHA512 and SKIP_SHA512." )
endif ( )
2017-12-21 19:47:02 +08:00
endif ( )
2021-06-16 01:14:40 +08:00
if ( NOT vcpkg_download_distfile_SKIP_SHA512 )
if ( vcpkg_download_distfile_SHA512 STREQUAL "0" )
string ( REPEAT "0" 128 vcpkg_download_distfile_SHA512 )
endif ( )
string ( LENGTH "${vcpkg_download_distfile_SHA512}" vcpkg_download_distfile_SHA512_length )
if ( NOT vcpkg_download_distfile_SHA512_length EQUAL "128" )
message ( FATAL_ERROR "Invalid SHA512: ${vcpkg_download_distfile_SHA512}. If you do not know the file's SHA512, set this to \" 0\ "." )
endif ( )
2021-06-12 00:58:25 +08:00
endif ( )
2017-12-21 19:47:02 +08:00
2016-09-24 21:23:18 +08:00
set ( downloaded_file_path ${ DOWNLOADS } / ${ vcpkg_download_distfile_FILENAME } )
2017-11-03 09:54:10 +08:00
set ( download_file_path_part "${DOWNLOADS}/temp/${vcpkg_download_distfile_FILENAME}" )
2018-08-31 06:57:37 +08:00
# Works around issue #3399
if ( IS_DIRECTORY "${DOWNLOADS}/temp" )
2019-09-24 05:03:59 +08:00
# Delete "temp0" directory created by the old version of vcpkg
2018-08-31 06:57:37 +08:00
file ( REMOVE_RECURSE "${DOWNLOADS}/temp0" )
2019-09-24 05:03:59 +08:00
file ( GLOB temp_files "${DOWNLOADS}/temp" )
file ( REMOVE_RECURSE ${ temp_files } )
else ( )
file ( MAKE_DIRECTORY "${DOWNLOADS}/temp" )
2018-08-31 06:57:37 +08:00
endif ( )
2016-09-25 06:36:50 +08:00
2018-03-02 03:10:19 +08:00
function ( test_hash FILE_PATH FILE_KIND CUSTOM_ERROR_ADVICE )
2018-01-06 08:16:08 +08:00
if ( _VCPKG_INTERNAL_NO_HASH_CHECK )
# When using the internal hash skip, do not output an explicit message.
return ( )
endif ( )
if ( vcpkg_download_distfile_SKIP_SHA512 )
2018-03-02 03:10:19 +08:00
message ( STATUS "Skipping hash check for ${FILE_PATH}." )
2017-11-04 05:34:08 +08:00
return ( )
endif ( )
2018-03-02 03:10:19 +08:00
file ( SHA512 ${ FILE_PATH } FILE_HASH )
2018-06-27 12:48:29 +08:00
if ( NOT FILE_HASH STREQUAL vcpkg_download_distfile_SHA512 )
2016-09-19 11:50:08 +08:00
message ( FATAL_ERROR
2016-09-24 21:23:18 +08:00
" \ n F i l e d o e s n o t h a v e e x p e c t e d h a s h : \ n "
2018-03-02 03:10:19 +08:00
" F i l e p a t h : [ $ { F I L E _ P A T H } ] \ n "
2016-09-25 06:24:38 +08:00
" E x p e c t e d h a s h : [ $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ S H A 5 1 2 } ] \ n "
" A c t u a l h a s h : [ $ { F I L E _ H A S H } ] \ n "
2016-09-25 06:36:50 +08:00
" $ { C U S T O M _ E R R O R _ A D V I C E } \ n " )
2016-09-19 11:50:08 +08:00
endif ( )
2016-09-25 06:36:50 +08:00
endfunction ( )
2021-07-15 03:45:18 +08:00
# 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 ( EXISTS "${downloaded_file_path}" AND NOT ( vcpkg_download_distfile_ALWAYS_REDOWNLOAD AND NOT _VCPKG_NO_DOWNLOADS ) )
2020-09-01 13:36:25 +08:00
if ( NOT vcpkg_download_distfile_QUIET )
2021-06-05 07:48:26 +08:00
message ( STATUS "Using ${downloaded_file_path}" )
2020-09-01 13:36:25 +08:00
endif ( )
2018-03-02 03:10:19 +08:00
test_hash ( "${downloaded_file_path}" "cached file" "Please delete the file and retry if this file should be downloaded again." )
2016-09-19 11:50:08 +08:00
else ( )
2017-05-03 11:34:11 +08:00
if ( _VCPKG_NO_DOWNLOADS )
message ( FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist." )
endif ( )
2016-09-24 21:23:18 +08:00
# Tries to download the file.
2018-03-07 17:57:16 +08:00
list ( GET vcpkg_download_distfile_URLS 0 SAMPLE_URL )
2018-06-27 12:48:29 +08:00
if ( _VCPKG_DOWNLOAD_TOOL STREQUAL "ARIA2" AND NOT SAMPLE_URL MATCHES "aria2" )
2018-03-07 17:57:16 +08:00
vcpkg_find_acquire_program ( "ARIA2" )
message ( STATUS "Downloading ${vcpkg_download_distfile_FILENAME}..." )
2019-03-15 04:43:49 +08:00
if ( vcpkg_download_distfile_HEADERS )
2021-06-05 07:48:26 +08:00
foreach ( header IN LISTS vcpkg_download_distfile_HEADERS )
2019-03-15 04:43:49 +08:00
list ( APPEND request_headers "--header=${header}" )
endforeach ( )
endif ( )
2020-09-10 01:36:58 +08:00
vcpkg_execute_in_download_mode (
2018-03-07 17:57:16 +08:00
C O M M A N D $ { A R I A 2 } $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ U R L S }
- o t e m p / $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E }
- l d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - d e t a i l e d . l o g
2019-03-15 04:43:49 +08:00
$ { r e q u e s t _ h e a d e r s }
2018-03-07 17:57:16 +08:00
O U T P U T _ F I L E d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - o u t . l o g
E R R O R _ F I L E d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - e r r . l o g
R E S U L T _ V A R I A B L E e r r o r _ c o d e
2021-06-12 00:57:58 +08:00
W O R K I N G _ D I R E C T O R Y " $ { D O W N L O A D S } "
2018-03-07 17:57:16 +08:00
)
if ( NOT "${error_code}" STREQUAL "0" )
message ( STATUS
" D o w n l o a d i n g $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } . . . F a i l e d . \ n "
" E x i t C o d e : $ { e r r o r _ c o d e } \ n "
" S e e l o g s f o r m o r e i n f o r m a t i o n : \ n "
" $ { D O W N L O A D S } / d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - o u t . l o g \ n "
" $ { D O W N L O A D S } / d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - e r r . l o g \ n "
" $ { D O W N L O A D S } / d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - d e t a i l e d . l o g \ n "
)
2016-09-24 21:23:18 +08:00
set ( download_success 0 )
else ( )
2021-06-12 00:57:58 +08:00
test_hash ( "${DOWNLOADS}/temp/${vcpkg_download_distfile_FILENAME}" "downloaded file" "The file may have been corrupted in transit." )
2018-03-15 13:17:54 +08:00
file ( REMOVE
$ { D O W N L O A D S } / d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - o u t . l o g
$ { D O W N L O A D S } / d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - e r r . l o g
$ { D O W N L O A D S } / d o w n l o a d - $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ F I L E N A M E } - d e t a i l e d . l o g
)
2021-06-12 00:57:58 +08:00
get_filename_component ( downloaded_file_dir "${downloaded_file_path}" DIRECTORY )
file ( MAKE_DIRECTORY "${downloaded_file_dir}" )
file ( RENAME "${DOWNLOADS}/temp/${vcpkg_download_distfile_FILENAME}" "${downloaded_file_path}" )
2016-09-24 21:23:18 +08:00
set ( download_success 1 )
endif ( )
2021-06-12 00:57:58 +08:00
elseif ( vcpkg_download_distfile_SKIP_SHA512 OR vcpkg_download_distfile_HEADERS )
# This is a workaround until the vcpkg tool supports downloading files without SHA512 and with headers
2021-06-05 07:48:26 +08:00
set ( download_success 0 )
set ( request_headers )
if ( vcpkg_download_distfile_HEADERS )
foreach ( header IN LISTS vcpkg_download_distfile_HEADERS )
list ( APPEND request_headers HTTPHEADER ${ header } )
endforeach ( )
endif ( )
2018-03-07 17:57:16 +08:00
foreach ( url IN LISTS vcpkg_download_distfile_URLS )
2021-01-12 01:16:32 +08:00
message ( STATUS "Downloading ${url} -> ${vcpkg_download_distfile_FILENAME}..." )
2020-09-26 20:10:38 +08:00
file ( DOWNLOAD "${url}" "${download_file_path_part}" STATUS download_status ${ request_headers } )
2018-03-07 17:57:16 +08:00
list ( GET download_status 0 status_code )
if ( NOT "${status_code}" STREQUAL "0" )
message ( STATUS "Downloading ${url}... Failed. Status: ${download_status}" )
else ( )
2021-06-05 07:48:26 +08:00
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" )
get_filename_component ( downloaded_file_dir "${downloaded_file_path}" DIRECTORY )
file ( MAKE_DIRECTORY "${downloaded_file_dir}" )
file ( RENAME ${ download_file_path_part } ${ downloaded_file_path } )
2018-03-07 17:57:16 +08:00
set ( download_success 1 )
break ( )
endif ( )
endforeach ( url )
2021-06-05 07:48:26 +08:00
else ( )
set ( urls )
foreach ( url IN LISTS vcpkg_download_distfile_URLS )
list ( APPEND urls "--url=${url}" )
endforeach ( )
if ( NOT vcpkg_download_distfile_QUIET )
message ( STATUS "Downloading ${vcpkg_download_distfile_URLS} -> ${vcpkg_download_distfile_FILENAME}..." )
endif ( )
set ( request_headers )
if ( vcpkg_download_distfile_HEADERS )
foreach ( header IN LISTS vcpkg_download_distfile_HEADERS )
list ( APPEND request_headers "--header=${header}" )
endforeach ( )
endif ( )
vcpkg_execute_in_download_mode (
2021-06-06 00:45:39 +08:00
C O M M A N D " $ E N V { V C P K G _ C O M M A N D } " x - d o w n l o a d
" $ { d o w n l o a d e d _ f i l e _ p a t h } "
" $ { v c p k g _ d o w n l o a d _ d i s t f i l e _ S H A 5 1 2 } "
$ { u r l s }
$ { r e q u e s t _ h e a d e r s }
- - d e b u g
- - f e a t u r e - f l a g s = - m a n i f e s t s # there's a bug in vcpkg x-download when it finds a manifest-root
2021-06-05 07:48:26 +08:00
O U T P U T _ V A R I A B L E o u t p u t
E R R O R _ V A R I A B L E o u t p u t
R E S U L T _ V A R I A B L E f a i l u r e
W O R K I N G _ D I R E C T O R Y " $ { D O W N L O A D S } "
)
if ( failure )
message ( "${output}" )
set ( download_success 0 )
else ( )
set ( download_success 1 )
endif ( )
2018-03-07 17:57:16 +08:00
endif ( )
2016-09-24 21:23:18 +08:00
2021-06-05 07:48:26 +08:00
if ( NOT download_success )
message ( FATAL_ERROR
2020-07-04 00:36:40 +08:00
" \ n "
2021-05-21 16:00:41 +08:00
" F a i l e d t o d o w n l o a d f i l e . \ n "
" I f y o u u s e a p r o x y , p l e a s e c h e c k y o u r p r o x y s e t t i n g . P o s s i b l e c a u s e s a r e : \ n "
2021-01-06 04:39:57 +08:00
" \ n "
2021-05-21 16:00:41 +08:00
" 1 . Y o u a r e a c t u a l l y u s i n g a n H T T P p r o x y , b u t s e t t i n g H T T P S _ P R O X Y v a r i a b l e \ n "
" t o ` h t t p s : / / a d d r e s s : p o r t ` . T h i s i s n o t c o r r e c t , b e c a u s e ` h t t p s : / / ` p r e f i x \ n "
" c l a i m s t h e p r o x y i s a n H T T P S p r o x y , w h i l e y o u r proxy ( v2ray, shadowsocksr\n "
" , e t c . . ) i s a n H T T P p r o x y . T r y s e t t i n g ` h t t p : / / a d d r e s s : p o r t ` t o b o t h \ n "
" H T T P _ P R O X Y a n d H T T P S _ P R O X Y i n s t e a d . \ n "
" \ n "
" 2 . Y o u a r e u s i n g F i d d l e r . C u r r e n t l y a bug ( https://github.com/microsoft/vcpkg/issues/17752 ) \ n "
" w i l l s e t H T T P S _ P R O X Y t o ` h t t p s : / / f i d d l e r _ a d d r e s s : p o r t ` w h i c h l e a d t o p r o b l e m 1 a b o v e . \ n "
" W o r k a r o u n d i s o p e n W i n d o w s 1 0 S e t t i n g s A p p , a n d s e a r c h f o r P r o x y C o n f i g u r a t i o n p a g e , \ n "
" C h a n g e ` h t t p = a d d r e s s : p o r t ; h t t p s = a d d r e s s : p o r t ` t o ` a d d r e s s ` , a n d f i l l t h e p o r t n u m b e r . \ n "
" \ n "
" 3 . Y o u p r o x y ' s r e m o t e s e r v e r i s o u t o f s e r v i c e . \ n "
" \ n "
" I n f u t u r e v c p k g r e l e a s e s , i f y o u a r e u s i n g W i n d o w s , y o u n o l o n g e r n e e d t o s e t \ n "
" HTTP ( S ) _ P R O X Y e n v i r o n m e n t v a r i a b l e s . V c p k g w i l l s i m p l y a p p l y W i n d o w s I E P r o x y \ n "
" S e t t i n g s s e t b y y o u r p r o x y s o f t w a r e . See ( https://github.com/microsoft/vcpkg-tool/pull/49 ) \ n "
" and ( https://github.com/microsoft/vcpkg-tool/pull/77 ) \ n "
2021-01-06 04:39:57 +08:00
" \ n "
2020-07-04 00:36:40 +08:00
" O t h e r w i s e , p l e a s e s u b m i t a n i s s u e a t h t t p s : / / g i t h u b . c o m / M i c r o s o f t / v c p k g / i s s u e s \ n " )
2016-09-24 21:23:18 +08:00
endif ( )
2016-09-19 11:50:08 +08:00
endif ( )
2016-09-24 21:23:18 +08:00
set ( ${ VAR } ${ downloaded_file_path } PARENT_SCOPE )
2016-09-19 11:50:08 +08:00
endfunction ( )