2016-08-08 14:49:22 -06:00
|
|
|
# Helper to download files as needed
|
|
|
|
|
|
|
|
function(file_md5_if_exists FILE VAR)
|
2019-07-20 14:16:25 -06:00
|
|
|
if(EXISTS "${FILE}")
|
|
|
|
file(MD5 "${FILE}" "${VAR}")
|
|
|
|
set(${VAR} "${${VAR}}" PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
set(${VAR} "" PARENT_SCOPE)
|
|
|
|
endif()
|
2016-08-08 14:49:22 -06:00
|
|
|
endfunction()
|
|
|
|
|
2017-03-01 19:29:17 -07:00
|
|
|
function(search_downloads FILE_MD5 VAR)
|
2019-07-20 14:16:25 -06:00
|
|
|
set(${VAR} "" PARENT_SCOPE)
|
2022-03-29 11:28:52 -06:00
|
|
|
file(GLOB FILES ${dfhack_SOURCE_DIR}/CMake/downloads/*)
|
2019-07-20 14:16:25 -06:00
|
|
|
foreach(FILE ${FILES})
|
|
|
|
file(MD5 "${FILE}" CUR_MD5)
|
|
|
|
if("${CUR_MD5}" STREQUAL "${FILE_MD5}")
|
|
|
|
set(${VAR} ${FILE} PARENT_SCOPE)
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
endforeach()
|
2017-03-01 19:29:17 -07:00
|
|
|
endfunction()
|
|
|
|
|
2016-08-08 14:49:22 -06:00
|
|
|
function(download_file URL DEST EXPECTED_MD5)
|
2019-07-20 14:16:25 -06:00
|
|
|
get_filename_component(FILENAME "${URL}" NAME)
|
|
|
|
file_md5_if_exists("${DEST}" CUR_MD5)
|
2016-08-08 14:49:22 -06:00
|
|
|
|
2019-07-20 14:16:25 -06:00
|
|
|
if(NOT "${EXPECTED_MD5}" STREQUAL "${CUR_MD5}")
|
|
|
|
search_downloads(${EXPECTED_MD5} DLPATH)
|
|
|
|
if(NOT("${DLPATH}" STREQUAL ""))
|
|
|
|
message("* Copying ${FILENAME} from ${DLPATH}")
|
|
|
|
execute_process(COMMAND "${CMAKE_COMMAND}" -E copy
|
|
|
|
"${DLPATH}"
|
|
|
|
"${DEST}")
|
|
|
|
return()
|
|
|
|
endif()
|
2017-03-17 22:31:15 -06:00
|
|
|
|
2019-07-20 14:16:25 -06:00
|
|
|
message("* Downloading ${FILENAME}")
|
|
|
|
file(DOWNLOAD "${URL}" "${DEST}" EXPECTED_MD5 "${EXPECTED_MD5}" SHOW_PROGRESS)
|
|
|
|
endif()
|
2016-08-08 14:49:22 -06:00
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# Download a file and uncompress it
|
|
|
|
function(download_file_unzip URL ZIP_TYPE ZIP_DEST ZIP_MD5 UNZIP_DEST UNZIP_MD5)
|
2019-07-20 14:16:25 -06:00
|
|
|
get_filename_component(FILENAME "${URL}" NAME)
|
|
|
|
file_md5_if_exists("${UNZIP_DEST}" CUR_UNZIP_MD5)
|
2016-08-08 14:49:22 -06:00
|
|
|
|
2019-07-20 14:16:25 -06:00
|
|
|
# Redownload if the MD5 of the uncompressed file doesn't match
|
|
|
|
if(NOT "${UNZIP_MD5}" STREQUAL "${CUR_UNZIP_MD5}")
|
|
|
|
download_file("${URL}" "${ZIP_DEST}" "${ZIP_MD5}")
|
2016-08-08 14:49:22 -06:00
|
|
|
|
2019-07-20 14:16:25 -06:00
|
|
|
if(EXISTS "${ZIP_DEST}")
|
|
|
|
message("* Decompressing ${FILENAME}")
|
|
|
|
if("${ZIP_TYPE}" STREQUAL "gz")
|
|
|
|
execute_process(COMMAND
|
2022-03-29 11:28:52 -06:00
|
|
|
"${PERL_EXECUTABLE}" "${dfhack_SOURCE_DIR}/depends/gunzip.pl"
|
2019-07-20 14:16:25 -06:00
|
|
|
"${ZIP_DEST}" --force)
|
|
|
|
else()
|
|
|
|
message(SEND_ERROR "Unknown ZIP_TYPE: ${ZIP_TYPE}")
|
|
|
|
endif()
|
|
|
|
if(NOT EXISTS "${UNZIP_DEST}")
|
|
|
|
message(SEND_ERROR "File failed to unzip to ${UNZIP_DEST}")
|
|
|
|
else()
|
|
|
|
file(MD5 "${UNZIP_DEST}" CUR_UNZIP_MD5)
|
|
|
|
if(NOT "${UNZIP_MD5}" STREQUAL "${CUR_UNZIP_MD5}")
|
|
|
|
message(SEND_ERROR "MD5 mismatch: ${UNZIP_DEST}: expected ${UNZIP_MD5}, got ${CUR_UNZIP_MD5}")
|
|
|
|
endif()
|
|
|
|
endif()
|
2016-08-08 14:49:22 -06:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endfunction()
|