@ -1,11 +1,12 @@
#
# D o w n l o a d a n d o p t i o n a l l y u n p a c k a f i l e
#
# ocv_download ( FILENAME p HASH h URL u1 [u2 ...] DESTINATION_DIR d [STATUS s] [UNPACK] [RELATIVE] )
# ocv_download ( FILENAME p HASH h URL u1 [u2 ...] DESTINATION_DIR d [ID id] [ STATUS s] [UNPACK] [RELATIVE_URL ] )
# F I L E N A M E - f i l e n a m e
# H A S H - M D 5 h a s h
# U R L - f u l l d o w n l o a d url ( first nonempty value will be chosen )
# D E S T I N A T I O N _ D I R - f i l e w i l l b e c o p i e d t o t h i s d i r e c t o r y
# I D - i d e n t i f i e r f o r p r o j e c t / g r o u p o f d o w n l o a d e d f i l e s
# S T A T U S - p a s s e d v a r i a b l e w i l l b e u p d a t e d i n p a r e n t s c o p e ,
# f u n c t i o n w i l l n o t f a i l t h e b u i l d i n c a s e o f d o w n l o a d p r o b l e m i f t h i s o p t i o n i s p r o v i d e d ,
# b u t w i l l f a i l i n c a s e w h e n o t h e r operations ( copy, remove, etc. ) f a i l e d
@ -13,28 +14,76 @@
# R E L A T I V E _ U R L - i f s e t , t h e n U R L i s t r e a t e d a s a b a s e , a n d F I L E N A M E w i l l b e a p p e n d e d t o i t
# N o t e : u s e s O P E N C V _ D O W N L O A D _ P A T H f o l d e r a s c a c h e , d e f a u l t i s < o p e n c v > / . c a c h e
set ( OPENCV_DOWNLOAD_PATH "${OpenCV_SOURCE_DIR}/.cache" CACHE PATH "Cache directory for downloaded files" )
set ( HELP_OPENCV_DOWNLOAD_PATH "Cache directory for downloaded files" )
if ( DEFINED ENV{OPENCV_DOWNLOAD_PATH} )
set ( OPENCV_DOWNLOAD_PATH "$ENV{OPENCV_DOWNLOAD_PATH}" CACHE PATH "${HELP_OPENCV_DOWNLOAD_PATH}" )
endif ( )
set ( OPENCV_DOWNLOAD_PATH "${OpenCV_SOURCE_DIR}/.cache" CACHE PATH "${HELP_OPENCV_DOWNLOAD_PATH}" )
set ( OPENCV_DOWNLOAD_LOG "${OpenCV_BINARY_DIR}/CMakeDownloadLog.txt" )
# I n i t d o w n l o a d c a c h e d i r e c t o r y a n d l o g f i l e
if ( NOT EXISTS "${OPENCV_DOWNLOAD_PATH}" )
file ( MAKE_DIRECTORY ${ OPENCV_DOWNLOAD_PATH } )
endif ( )
if ( NOT EXISTS "${OPENCV_DOWNLOAD_PATH}/.gitignore" )
file ( WRITE "${OPENCV_DOWNLOAD_PATH}/.gitignore" "*\n" )
endif ( )
file ( WRITE "${OPENCV_DOWNLOAD_LOG}" "use_cache \" ${ OPENCV_DOWNLOAD_PATH } \"\n")
function ( ocv_download )
cmake_parse_arguments ( DL "UNPACK;RELATIVE_URL" "FILENAME;HASH;DESTINATION_DIR;STATUS" "URL" ${ ARGN } )
ocv_assert ( DEFINED DL_FILENAME )
ocv_assert ( DEFINED DL_HASH )
ocv_assert ( DEFINED DL_URL )
ocv_assert ( DEFINED DL_DESTINATION_DIR )
cmake_parse_arguments ( DL "UNPACK;RELATIVE_URL" "FILENAME;HASH;DESTINATION_DIR;ID;STATUS" "URL" ${ ARGN } )
macro ( ocv_download_log )
file ( APPEND "${OPENCV_DOWNLOAD_LOG}" "${ARGN}\n" )
endmacro ( )
ocv_assert ( DL_FILENAME )
ocv_assert ( DL_HASH )
ocv_assert ( DL_URL )
ocv_assert ( DL_DESTINATION_DIR )
if ( ( NOT " ${DL_UNPARSED_ARGUMENTS}" STREQUAL " " )
O R D L _ F I L E N A M E S T R E Q U A L " "
O R D L _ H A S H S T R E Q U A L " "
O R D L _ U R L S T R E Q U A L " "
O R D L _ D E S T I N A T I O N _ D I R S T R E Q U A L " "
)
set ( msg_level FATAL_ERROR )
if ( DEFINED DL_STATUS )
set ( ${ DL_STATUS } FALSE PARENT_SCOPE )
set ( msg_level WARNING )
endif ( )
message ( ${ msg_level } "ERROR: ocv_download() unsupported arguments: ${ARGV}" )
return ( )
endif ( )
if ( DEFINED DL_STATUS )
set ( ${ DL_STATUS } TRUE PARENT_SCOPE )
endif ( )
# C h e c k C M a k e c a c h e f o r a l r e a d y p r o c e s s e d t a s k s
string ( FIND "${DL_DESTINATION_DIR}" "${CMAKE_BINARY_DIR}" DL_BINARY_PATH_POS )
if ( DL_BINARY_PATH_POS EQUAL 0 )
set ( __file_id "${DL_DESTINATION_DIR}/${DL_FILENAME}" )
file ( RELATIVE_PATH __file_id "${CMAKE_BINARY_DIR}" "${__file_id}" )
string ( REGEX REPLACE "[^a-zA-Z0-9_]" "_" __file_id "${__file_id}" )
if ( DL_ID )
string ( TOUPPER ${ DL_ID } __id )
string ( REGEX REPLACE "[^a-zA-Z0-9_]" "_" __id "${__id}" )
set ( OCV_DOWNLOAD_HASH_NAME "OCV_DOWNLOAD_${__id}_HASH_${__file_id}" )
else ( )
set ( OCV_DOWNLOAD_HASH_NAME "OCV_DOWNLOAD_HASH_${__file_id}" )
endif ( )
if ( " ${${OCV_DOWNLOAD_HASH_NAME}}" STREQUAL " ${DL_HASH}" )
ocv_download_log ( "#match_hash_in_cmake_cache \" ${ OCV_DOWNLOAD_HASH_NAME } \"")
return ( )
endif ( )
unset ( "${OCV_DOWNLOAD_HASH_NAME}" CACHE )
else ( )
set ( OCV_DOWNLOAD_HASH_NAME "" )
# message ( WARNING "Download destination is not in CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}: ${DL_DESTINATION_DIR}" )
endif ( )
# S e l e c t f i r s t n o n - e m p t y u r l
foreach ( url ${ DL_URL } )
if ( url )
@ -54,58 +103,85 @@ function(ocv_download)
endif ( )
# L o g a l l c a l l s t o f i l e
file ( APPEND "${OPENCV_DOWNLOAD_LOG}" "do_${mode} \" ${ DL_FILENAME } \" \"${DL_HASH}\" \"${DL_URL}\" \"${DL_DESTINATION_DIR}\"\n ")
ocv_download_log ( "do_${mode} \" ${ DL_FILENAME } \" \"${DL_HASH}\" \"${DL_URL}\" \"${DL_DESTINATION_DIR}\"")
# . . . a n d t o c o n s o l e
message ( STATUS "Download: ${DL_FILENAME}" )
set ( __msg_prefix "" )
if ( DL_ID )
set ( __msg_prefix "${DL_ID}: " )
endif ( )
message ( STATUS "${__msg_prefix}Download: ${DL_FILENAME}" )
# C o p y m o d e : c h e c k i f c o p y d e s t i n a t i o n e x i s t s a n d i s c o r r e c t
if ( NOT DL_UNPACK )
set ( COPY_DESTINATION "${DL_DESTINATION_DIR}/${DL_FILENAME}" )
if ( EXISTS "${COPY_DESTINATION}" )
ocv_download_log ( "#check_md5 \" ${ COPY_DESTINATION } \"")
file ( MD5 "${COPY_DESTINATION}" target_md5 )
if ( target_md5 STREQUAL DL_HASH )
ocv_download_log ( "#match_md5 \" ${ COPY_DESTINATION } \" \"${target_md5}\"")
if ( OCV_DOWNLOAD_HASH_NAME )
set ( ${ OCV_DOWNLOAD_HASH_NAME } "${DL_HASH}" CACHE INTERNAL "" )
endif ( )
return ( )
endif ( )
ocv_download_log ( "#mismatch_md5 \" ${ COPY_DESTINATION } \" \"${target_md5}\"")
else ( )
ocv_download_log ( "#missing \" ${ COPY_DESTINATION } \"")
endif ( )
endif ( )
# C h e c k c a c h e f i r s t
if ( DL_ID )
string ( TOLOWER "${DL_ID}" __id )
string ( REGEX REPLACE "[^a-zA-Z0-9_/ ]" "_" __id "${__id}" )
set ( CACHE_CANDIDATE "${OPENCV_DOWNLOAD_PATH}/${__id}/${DL_HASH}-${DL_FILENAME}" )
else ( )
set ( CACHE_CANDIDATE "${OPENCV_DOWNLOAD_PATH}/${DL_HASH}-${DL_FILENAME}" )
endif ( )
if ( EXISTS "${CACHE_CANDIDATE}" )
ocv_download_log ( "#check_md5 \" ${ CACHE_CANDIDATE } \"")
file ( MD5 "${CACHE_CANDIDATE}" target_md5 )
if ( NOT target_md5 STREQUAL DL_HASH )
ocv_download_log ( "#mismatch_md5 \" ${ CACHE_CANDIDATE } \" \"${target_md5}\"")
ocv_download_log ( "#delete \" ${ CACHE_CANDIDATE } \"")
file ( REMOVE ${ CACHE_CANDIDATE } )
endif ( )
endif ( )
# D o w n l o a d
if ( NOT EXISTS "${CACHE_CANDIDATE}" )
ocv_download_log ( "#cmake_download \" ${ CACHE_CANDIDATE } \" \"${DL_URL}\"")
file ( DOWNLOAD "${DL_URL}" "${CACHE_CANDIDATE}"
I N A C T I V I T Y _ T I M E O U T 6 0
T I M E O U T 6 0 0
S T A T U S s t a t u s
L O G l o g )
string ( REPLACE "\n" "\n# " log "# ${log}" )
file ( APPEND "${OPENCV_DOWNLOAD_LOG}" "${log}\n\n" )
L O G _ _ l o g )
string ( LENGTH "${__log}" __log_length )
if ( __log_length LESS 65536 )
string ( REPLACE "\n" "\n# " __log "${__log}" )
ocv_download_log ( "# ${__log}\n" )
endif ( )
if ( NOT status EQUAL 0 )
set ( msg_level FATAL_ERROR )
if ( DEFINED DL_STATUS )
set ( ${ DL_STATUS } FALSE PARENT_SCOPE )
set ( msg_level WARNING )
endif ( )
message ( ${ msg_level } "Download failed: ${status}" )
message ( ${ msg_level } "${__msg_prefix} Download failed: ${status}" )
return ( )
endif ( )
# D o n ' t r e m o v e t h i s c o d e , b e c a u s e E X P E C T E D _ M D 5 p a r a m e t e r d o e s n ' t f a i l " file ( DOWNLOAD ) " s t e p o n w r o n g h a s h
ocv_download_log ( "#check_md5 \" ${ CACHE_CANDIDATE } \"")
file ( MD5 "${CACHE_CANDIDATE}" target_md5 )
if ( NOT target_md5 STREQUAL DL_HASH )
ocv_download_log ( "#mismatch_md5 \" ${ CACHE_CANDIDATE } \" \"${target_md5}\"")
set ( msg_level FATAL_ERROR )
if ( DEFINED DL_STATUS )
set ( ${ DL_STATUS } FALSE PARENT_SCOPE )
set ( msg_level WARNING )
endif ( )
message ( ${ msg_level } "Hash mismatch: ${target_md5}" )
message ( ${ msg_level } "${__msg_prefix} Hash mismatch: ${target_md5}" )
return ( )
endif ( )
endif ( )
@ -113,21 +189,28 @@ function(ocv_download)
# U n p a c k o r c o p y
if ( DL_UNPACK )
if ( EXISTS "${DL_DESTINATION_DIR}" )
ocv_download_log ( "#remove_unpack \" ${ DL_DESTINATION_DIR } \"")
file ( REMOVE_RECURSE "${DL_DESTINATION_DIR}" )
endif ( )
ocv_download_log ( "#mkdir \" ${ DL_DESTINATION_DIR } \"")
file ( MAKE_DIRECTORY "${DL_DESTINATION_DIR}" )
ocv_download_log ( "#unpack \" ${ DL_DESTINATION_DIR } \" \"${CACHE_CANDIDATE}\"")
execute_process ( COMMAND "${CMAKE_COMMAND}" -E tar xz "${CACHE_CANDIDATE}"
W O R K I N G _ D I R E C T O R Y " $ { D L _ D E S T I N A T I O N _ D I R } "
R E S U L T _ V A R I A B L E r e s )
if ( NOT res EQUAL 0 )
message ( FATAL_ERROR "Unpack failed: ${res}" )
message ( FATAL_ERROR "${__msg_prefix} Unpack failed: ${res}" )
endif ( )
else ( )
ocv_download_log ( "#copy \" ${ COPY_DESTINATION } \" \"${CACHE_CANDIDATE}\"")
execute_process ( COMMAND ${ CMAKE_COMMAND } -E copy_if_different "${CACHE_CANDIDATE}" "${COPY_DESTINATION}"
R E S U L T _ V A R I A B L E r e s )
if ( NOT res EQUAL 0 )
message ( FATAL_ERROR "Copy failed: ${res}" )
message ( FATAL_ERROR "${__msg_prefix} Copy failed: ${res}" )
endif ( )
endif ( )
if ( OCV_DOWNLOAD_HASH_NAME )
set ( ${ OCV_DOWNLOAD_HASH_NAME } "${DL_HASH}" CACHE INTERNAL "" )
endif ( )
endfunction ( )