Download, verify and extract compressed files with CMake

CMake ≥ 3.0 makes downloading and optionally verifying the checksum of files quite easy and seamless. While you will find CMake purists who would do this via add_custom_command(), the net effect is the same, and I find this method less confusing and more flexible. If you don’t use the downloaded file until runtime (e.g. data files used for CI/self-test) then you can put this file download snippet anywhere in CMakeLists.txt.

cmake_minimum_required(VERSION 3.0)
project(SSLdemo
        LANGUAGES NONE)
        #DESCRIPTION "Demonstrate that CMake SSL is installed and working"
        #HOMEPAGE_URL https://github.com/scivision/cmake-utils)

set(OUTFN ${CMAKE_CURRENT_BINARY_DIR}/github-mark.svg)

if(NOT EXISTS ${OUTFN})
  file(DOWNLOAD https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg ${OUTFN}
     TIMEOUT 15
     EXPECTED_HASH MD5=07883e93734b98cae0f7b9c55d287250
     TLS_VERIFY ON
     )
endif()

This downloads the file and optionally:

  • checks MD5sum
  • verifies the SSL certificate

Extract compressed file

CMake also can extract a ZIP file –disregard the “tar” command name, it is universal and platform agnostic

Notes

  • Install latest CMake without sudo using cmake_setup.sh.

Tags:

Categories:

Comments

Written by Michael Hirsch, Ph.D. //