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 and extracts the file at configure time.

include(FetchContent)

function(download_file url sha256)

FetchContent_Declare(download_${sha256}
URL ${url}
URL_HASH SHA256=${sha256}
)

FetchContent_MakeAvailable(download_${sha256})

endfunction()

# === 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

Then extracts the file if it is compressed.

CMake uses vendored curl internally.