Last active
March 16, 2019 17:57
-
-
Save ggerganov/5692f5d4c2fafbe497d89e503b5aac6d to your computer and use it in GitHub Desktop.
CMake macro for downloading from a list of possible remote locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MIT License | |
# http://opensource.org/licenses/MIT | |
# | |
# Copyright (c) 2019 Georgi Gerganov | |
# | |
# file_download_multi | |
# | |
# Try to download a file from a list of possible remote location (URLs) | |
# The macro loops through the list and attempts to download using | |
# the standard file(DOWNLOAD ...) function. | |
# | |
# Arguments: | |
# FILE - the output path for the downloaded file | |
# URLS - list of URLs to try to download the file from | |
# PARAMS_PER_URL - list all parameters that you would use with | |
# the original file(DOWNLOAD ...) command | |
# | |
# Example: | |
# | |
# file_download_multi( | |
# FILE | |
# ${CMAKE_CURRENT_SOURCE_DIR}/file.txt | |
# URLS | |
# http://www.some-url.com/file.txt | |
# http://www.some-other-url.org/file.txt | |
# http://127.0.0.1/file.txt | |
# https://ggerganov.github.io/index.html | |
# PARAMS_PER_URL # list standard file() parameters after PARAMS_PER_URL | |
# SHOW_PROGRESS | |
# STATUS status | |
# INACTIVITY_TIMEOUT 1 | |
# ) | |
# | |
macro(file_download_multi) | |
cmake_parse_arguments(ARG "" "FILE" "URLS;PARAMS_PER_URL" ${ARGN}) | |
cmake_parse_arguments(ARG2 "" "STATUS" "" ${ARGN}) | |
unset(download_success) | |
foreach(url IN LISTS ARG_URLS) | |
message(STATUS "Trying to download '${ARG_FILE}' from '${url}' ...") | |
file(DOWNLOAD ${url} ${ARG_FILE} ${ARG_PARAMS_PER_URL} STATUS status_internal) | |
list( GET status_internal 0 error_code ) | |
list( GET status_internal 1 error_message ) | |
if (error_code) | |
message(STATUS "Failed to download. Reason (${error_code}): ${error_message}") | |
else() | |
set(download_success 1) | |
break() | |
endif() | |
endforeach() | |
if (DEFINED download_success) | |
message(STATUS "File '${ARG_FILE}' downloaded successfully") | |
else() | |
message(STATUS "Failed to download '${ARG_FILE}' from all secified URLs") | |
file(SIZE ${ARG_FILE} fsize) | |
if (NOT fsize) | |
file(REMOVE ${ARG_FILE}) | |
endif() | |
endif() | |
if (DEFINED ARG2_STATUS) | |
set(${ARG2_STATUS} ${status_internal}) | |
endif() | |
endmacro(file_download_multi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment