CMake download and extract compressed files
Using CMake to download, verify the checksum of files and extract compressed files is easy and seamless. FetchContent downloads the file at configure time.
include(FetchContent)
option(CMAKE_TLS_VERIFY "Verify SSL certificates" ON)
function(download_file url hash)
FetchContent_Declare(download_${hash}
URL ${url}
URL_HASH SHA256=${hash}
DOWNLOAD_NO_EXTRACT true
)
if(NOT download_${hash}_POPULATED)
FetchContent_Populate(download_${hash})
endif()
endfunction(download_file)
# === example
download_file(
https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg
12794390cce7d0682ffc783c785e4282305684431b30b29ed75c224da24035b4
)
This downloads the file and:
- checks hash signature
- verifies the SSL certificate
CMake uses vendored cURL internally.
CMake also can extract compressed files like .zip, .tar.bz2, etc. This command can also specify output directory, extract a subset of files matching a pattern and more.
file(ARCHIVE_EXTRACT INPUT in_file.zip)