Improve CMake file downloads

- Files are redownloaded if their expected MD5 changes in CMakeLists.txt
- Refactored some
develop
lethosor 2016-08-08 16:49:22 -04:00
parent e22b3b1de7
commit 9c28d1a085
2 changed files with 100 additions and 58 deletions

@ -0,0 +1,48 @@
# Helper to download files as needed
function(file_md5_if_exists FILE VAR)
if(EXISTS "${FILE}")
file(MD5 "${FILE}" "${VAR}")
set(${VAR} "${${VAR}}" PARENT_SCOPE)
else()
set(${VAR} "" PARENT_SCOPE)
endif()
endfunction()
function(download_file URL DEST EXPECTED_MD5)
get_filename_component(FILENAME "${URL}" NAME)
file_md5_if_exists("${DEST}" CUR_MD5)
if(NOT "${EXPECTED_MD5}" STREQUAL "${CUR_MD5}")
message("* Downloading ${FILENAME}")
file(DOWNLOAD "${URL}" "${DEST}" EXPECTED_MD5 "${EXPECTED_MD5}" SHOW_PROGRESS)
endif()
endfunction()
# Download a file and uncompress it
function(download_file_unzip URL ZIP_TYPE ZIP_DEST ZIP_MD5 UNZIP_DEST UNZIP_MD5)
get_filename_component(FILENAME "${URL}" NAME)
file_md5_if_exists("${UNZIP_DEST}" CUR_UNZIP_MD5)
# 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}")
if(EXISTS "${ZIP_DEST}")
message("* Decompressing ${FILENAME}")
if("${ZIP_TYPE}" STREQUAL "gz")
execute_process(COMMAND gunzip --force "${ZIP_DEST}")
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()
endif()
endif()
endfunction()

@ -203,22 +203,21 @@ endif()
#### download depends ####
include(CMake/DownloadFile.cmake)
if(WIN32)
# Download zlib on Windows
set(ZLIB_DOWNLOAD_DIR ${CMAKE_SOURCE_DIR}/depends/zlib/lib/win${DFHACK_BUILD_ARCH})
if(NOT EXISTS ${ZLIB_DOWNLOAD_DIR}/zlib.lib)
if(${DFHACK_BUILD_ARCH} STREQUAL "64")
message("Downloading win64-zlib.lib")
file(DOWNLOAD "https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win64-zlib.lib"
download_file("https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win64-zlib.lib"
${ZLIB_DOWNLOAD_DIR}/zlib.lib
EXPECTED_MD5 "a3b2fc6b68efafa89b0882e354fc8418")
"a3b2fc6b68efafa89b0882e354fc8418")
else()
message("Downloading win32-zlib.lib")
file(DOWNLOAD "https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win32-zlib.lib"
download_file("https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win32-zlib.lib"
${ZLIB_DOWNLOAD_DIR}/zlib.lib
EXPECTED_MD5 "f4ebaa21d9de28566e88b1edfcdff901")
endif()
"f4ebaa21d9de28566e88b1edfcdff901")
endif()
# Move zlib to the build folder so possible 32 and 64-bit builds
# in the same source tree don't conflict
file(COPY ${CMAKE_SOURCE_DIR}/depends/zlib
@ -230,18 +229,14 @@ if(WIN32)
# Do the same for SDLreal.dll
# (DFHack doesn't require this at build time, so no need to move it to the build folder)
set(SDLREAL_DOWNLOAD_DIR ${CMAKE_SOURCE_DIR}/package/windows/win${DFHACK_BUILD_ARCH})
if(NOT EXISTS ${SDLREAL_DOWNLOAD_DIR}/SDLreal.dll)
if(${DFHACK_BUILD_ARCH} STREQUAL "64")
message("Downloading win64-SDL.dll")
file(DOWNLOAD "https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win64-SDL.dll"
download_file("https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win64-SDL.dll"
${SDLREAL_DOWNLOAD_DIR}/SDLreal.dll
EXPECTED_MD5 "1ae242c4b94cb03756a1288122a66faf")
"1ae242c4b94cb03756a1288122a66faf")
else()
message("Downloading win32-SDL.dll")
file(DOWNLOAD "https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win32-SDL.dll"
download_file("https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/win32-SDL.dll"
${SDLREAL_DOWNLOAD_DIR}/SDLreal.dll
EXPECTED_MD5 "5a09604daca6b2b5ce049d79af935d6a")
endif()
"5a09604daca6b2b5ce049d79af935d6a")
endif()
endif()
@ -249,21 +244,20 @@ if(APPLE)
# libstdc++ (GCC 4.8.5 for OS X 10.6)
# fixes crash-on-unwind bug in DF's libstdc++
set(LIBSTDCXX_DOWNLOAD_DIR ${CMAKE_SOURCE_DIR}/package/darwin/osx${DFHACK_BUILD_ARCH})
# check for existence of uncompressed library
if(NOT EXISTS ${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib)
if(${DFHACK_BUILD_ARCH} STREQUAL "64")
message("Downloading osx64-libstdcxx.6.dylib.gz")
file(DOWNLOAD "https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/osx64-libstdcxx.6.dylib.gz"
download_file_unzip("https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/osx64-libstdcxx.6.dylib.gz"
"gz"
${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib.gz
EXPECTED_MD5 "cf26ed588be8e83c8e3a49919793b416")
execute_process(COMMAND gunzip ${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib.gz)
"cf26ed588be8e83c8e3a49919793b416"
${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib
"16dc6dbd4ecde7f9b95bb6dc91f07404")
else()
message("Downloading osx32-libstdcxx.6.dylib.gz")
file(DOWNLOAD "https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/osx32-libstdcxx.6.dylib.gz"
download_file_unzip("https://github.com/DFHack/dfhack-bin/releases/download/0.43.05/osx32-libstdcxx.6.dylib.gz"
"gz"
${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib.gz
EXPECTED_MD5 "40f3d83871b114f0279240626311621b")
execute_process(COMMAND gunzip ${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib.gz)
endif()
"40f3d83871b114f0279240626311621b"
${LIBSTDCXX_DOWNLOAD_DIR}/libstdc++.6.dylib
"c3f5678b8204917e03870834902c3e8b")
endif()
endif()