Created
June 10, 2017 12:21
-
-
Save bc-lee/c08fbd8a2d705abaebae0c5af44688c6 to your computer and use it in GitHub Desktop.
download.cmake
This file contains hidden or 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
if(NOT(DEST)) | |
message(FATAL_ERROR "DEST NOT DEFINED") | |
endif() | |
if(SHA1) | |
string(TOLOWER ${SHA1} SHA1) | |
endif() | |
if(EXISTS "${DEST}") | |
if(NOT(SHA1)) | |
message(STATUS "File ${DEST} Exist. SHA1 Hash not checked.") | |
return() | |
else() | |
file(SHA1 "${DEST}" SHA1_RESULT) | |
string(TOLOWER ${SHA1_RESULT} SHA1_RESULT) | |
if("${SHA1}" STREQUAL "${SHA1_RESULT}") | |
message(STATUS "File ${DEST} Exist. SHA1 Hash match.") | |
return() | |
else() | |
message(FATAL_ERROR "File ${DEST} Exist but SHA1 Hash mismatch(${SHA1} != ${SHA1_RESULT})") | |
endif() | |
endif() | |
message(FATAL_ERROR "UNREACHABLE") | |
endif() | |
if(NOT(URL)) | |
message(FATAL_ERROR "URL NOT DEFINED") | |
endif() | |
function(mktemp) | |
set(TEMP ENV{shell} PARENT_SCOPE) | |
if(NOT(TMPDIR)) | |
if(CMAKE_HOST_UNIX) | |
execute_process(COMMAND mktemp -d OUTPUT_VARIABLE TEMP) | |
string(STRIP "${TEMP}" TEMP) | |
set(TEMP "${TEMP}" PARENT_SCOPE) | |
else() | |
message(FATAL_ERROR "Temp Dir not found") | |
endif() | |
endif() | |
endfunction() | |
if(NOT(TEMP)) | |
mktemp(TEMP) | |
else() | |
file(MAKE_DIRECTORY "${TEMP}") | |
endif() | |
get_filename_component(DEST_FILENAME "${DEST}" NAME) | |
get_filename_component(DEST_DIRECTORY "${DEST}" DIRECTORY) | |
message(STATUS "URL: ${URL}") | |
message(STATUS "DEST_DIRECTORY: ${DEST_DIRECTORY}") | |
message(STATUS "TEMP DEST: ${TEMP}/${DEST_FILENAME}") | |
if(SHA1) | |
file(DOWNLOAD "${URL}" "${TEMP}/${DEST_FILENAME}" EXPECTED_HASH ALGO=${SHA1}) | |
else() | |
file(DOWNLOAD "${URL}" "${TEMP}/${DEST_FILENAME}") | |
endif() | |
file(MAKE_DIRECTORY "${DEST_DIRECTORY}") | |
file(COPY "${TEMP}/${DEST_FILENAME}" DESTINATION "${DEST_DIRECTORY}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment