Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active March 1, 2023 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scivision/8bce2a0e3e5fdc217473aaa53600eeb6 to your computer and use it in GitHub Desktop.
Save scivision/8bce2a0e3e5fdc217473aaa53600eeb6 to your computer and use it in GitHub Desktop.
CMake FetchContent with manual download and optional retry
# Created as a concept demo, hasn't been tried in real projects
cmake_minimum_required(VERSION 3.15...3.26)
project(EPretryDownload LANGUAGES C CXX)
include(FetchContent)
set(url https://github.com/scivision/fortran-filesystem/archive/refs/tags/v4.3.1.tar.gz)
set(hash c3d5b39edbd20cb0fa528af5282feb7c19bbe07ca1e72e7abce948b79c1a9e2a)
set(CMAKE_TLS_VERIFY ON)
set(ep_srcdir ${CMAKE_CURRENT_BINARY_DIR}/filesystem-src)
set(archive ${ep_srcdir}/filesystem.tar.gz)
macro(download url hash archive)
file(DOWNLOAD ${url} ${archive}
EXPECTED_HASH SHA256=${hash}
INACTIVITY_TIMEOUT 60
SHOW_PROGRESS
STATUS stat
LOG log
)
message(DEBUG "${log}")
endmacro()
if(EXISTS ${archive})
file(SHA256 ${archive} hash_file)
endif()
if(hash_file STREQUAL hash)
set(stat "0;file hash matches")
else()
message(STATUS "Downloading ${url}")
download(${url} ${hash} ${archive})
endif()
message(STATUS "${url}: ${stat}")
list(GET stat 0 code)
foreach(i RANGE 1 3)
if(code EQUAL 0)
break()
endif()
message(STATUS "${stat}")
message(STATUS "Retrying download, attempt ${i}")
download(${url} ${hash} ${archive})
endforeach()
# these variables are intended for the FetchContent project, which ignores
# CMAKE_ARGS and CMAKE_CACHE_ARGS
set(fortran off)
set(BUILD_SHARED_LIBS off)
FetchContent_Declare(fs
URL ${archive}
URL_HASH SHA256=${hash}
)
FetchContent_MakeAvailable(fs)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE ffilesystem::filesystem)
#include <iostream>
#include "ffilesystem.h"
int main(){
char p[MAXP];
fs_get_cwd(p, MAXP);
std::cout << "current working dir: " << p << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment