Spidey
Spidey

Reputation: 2589

How to show file download progress with CMake?

I've tried using the FetchContent module and file(DOWNLOAD ...) to download some dependencies for my project at build time. I'd rather use FetchContent only, but it creates so many files polluting the build dir that I'm willing to implement every step using the file command.

I managed to correctly download, check the file hash and extract it. But the download is blocking (as expected) and I get no output from it, so it's hard to check if anything went wrong or if Cmake is just downloading a big file. I'm using an HTTP URL. This is the relevant code snippet:

# Fetch file
if(NOT EXISTS "${EXTRACTED_DIR}")
    message("Downloading file...")
    file(DOWNLOAD "${FILE_URL}" "${DOWNLOADED_FILE}"
        SHOW_PROGRESS
        HTTPHEADER ${AUTH_HEADER}
        EXPECTED_HASH SHA256=${ARTIFACT_HASH}
        STATUS DOWNLOAD_RESULT)
    list(GET DOWNLOAD_RESULT 0 DOWNLOAD_RESULT_CODE)
    if(NOT DOWNLOAD_RESULT_CODE EQUAL 0)
        message(FATAL_ERROR "Failed downloading! Error: ${DOWNLOAD_RESULT}.")
    endif()
    message("Extracting file ${DOWNLOADED_FILE}...")
    execute_process(COMMAND "${CMAKE_COMMAND}" -E tar xv "${DOWNLOADED_FILE}"
        WORKING_DIRECTORY "${EXTRACTED_DIR}/"
        OUTPUT_QUIET)
endif()
include_directories("${EXTRACTED_DIR}/include")

Even using SHOW_PROGRESS on the file(DOWNLOAD ...), I see no output. I was expecting something like what wget provides. Is that normal behavior? What can I do to present download rate, ETA, etc, some feedback to the user?

Upvotes: 2

Views: 1934

Answers (1)

bw0248
bw0248

Reputation: 711

I had the same problem and could resolve it with simply setting Set(FETCHCONTENT_QUIET FALSE). I did not have to set any other properties to have a download indicator showing.

Upvotes: 2

Related Questions