@@ -448,16 +448,44 @@ endfunction()
448448function (_board_mgr_download url _file_path)
449449
450450 cmake_parse_arguments (parsed_args "QUIET;REQUIRED"
451- "RESULT_VARIABLE" "" ${ARGN} )
451+ "RESULT_VARIABLE;EXPECTED_HASH " "" ${ARGN} )
452452
453453 if (CMAKE_VERBOSE_MAKEFILE )
454454 message (STATUS "Downloading ${url} ..." )
455455 endif ()
456456
457- file (DOWNLOAD "${url} " "${_file_path} " STATUS _status
458- ${parsed_args_UNPARSED_ARGUMENTS} )
459- list (GET _status 0 _result)
460- list (GET _status 1 _result_str)
457+ string (REPLACE "=" ";" _hash_args "${parsed_args_EXPECTED_HASH} " )
458+ list (GET _hash_args 0 _hash_type)
459+ list (GET _hash_args 1 _hash_value)
460+ # Check hash before download
461+ set (_result 1)
462+ if (EXISTS "${_file_path} " )
463+ _board_mgr_check_file_hash("${_hash_type} " "${_hash_value} "
464+ "${_file_path} " _result QUIET )
465+ endif ()
466+
467+ if (NOT _result EQUAL 0)
468+ file (DOWNLOAD "${url} " "${_file_path} " STATUS _status INACTIVITY_TIMEOUT 5
469+ ${parsed_args_UNPARSED_ARGUMENTS} )
470+ if (CMAKE_VERBOSE_MAKEFILE )
471+ message ("${_file_path} download status:${_status} " )
472+ endif ()
473+ list (GET _status 0 _result)
474+ list (GET _status 1 _result_str)
475+ else ()
476+ if (CMAKE_VERBOSE_MAKEFILE )
477+ message (STATUS "${_file_path} with correct hash exists already!" )
478+ endif ()
479+ set (_result 0)
480+ endif ()
481+
482+ # Check hash after download
483+ if (_result EQUAL 0 AND parsed_args_EXPECTED_HASH)
484+ _board_mgr_check_file_hash("${_hash_type} " "${_hash_value} "
485+ "${_file_path} " _result)
486+ set (_result_str "Download HASH mismatch" )
487+ endif ()
488+
461489 if (DEFINED parsed_args_RESULT_VARIABLE)
462490 set (${parsed_args_RESULT_VARIABLE} ${_result} PARENT_SCOPE)
463491 endif ()
@@ -559,5 +587,34 @@ macro(_board_mgr_get_pl_tools_list)
559587
560588endmacro ()
561589
590+ # Check hash, returns 0
591+ function (_board_mgr_check_file_hash hash_type expected_hash file_path result_var)
592+
593+ cmake_parse_arguments (parsed_args "QUIET;REQUIRED"
594+ "" "" ${ARGN} )
595+
596+ string (TOLOWER "${expected_hash} " expected_hash )
597+ file ("${hash_type} " "${file_path} " _actual_hash)
598+ string (TOLOWER "${_actual_hash} " _actual_hash)
599+ if (NOT "${_actual_hash} " STREQUAL "${expected_hash} " )
600+ set ("${result_var} " 1 PARENT_SCOPE)
601+ if (NOT parsed_args_QUIET)
602+ execute_process (COMMAND ${CMAKE_COMMAND} -E echo
603+ "HASH mismatch for ${file_path} \n "
604+ " expected: [${expected_hash} ]\n "
605+ " actual: [${_actual_hash} ]" )
606+ endif ()
607+
608+ if (parsed_args_REQUIRED)
609+ message (SEND_ERROR "Hash match failed!!!" )
610+ elseif (NOT parsed_args_QUIET)
611+ message (WARNING "Hash match failed!!!" )
612+ endif ()
613+ return ()
614+ endif ()
615+
616+ set ("${result_var} " 0 PARENT_SCOPE)
617+ endfunction ()
618+
562619#libraries
563620
0 commit comments