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