dockercmakefetchcontent

cmake FetchContent not downloading with DOWNLOAD_NO_EXTRACT in docker


My CMakeLists.txt downloads a JAR file with FetchContent, which works as expected in Ubuntu 18.04 WSL. Running cmake in docker, does however not download the file.

I specify DOWNLOAD_NO_EXTRACT TRUE, since it otherwise extracts the JAR file. Without it, it does extract the JAR contents in docker.

Here's a minimal reproduce.

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 3.7 FATAL_ERROR)

include (FetchContent)
FetchContent_Declare(
  AntlrJar
  URL      https://www.antlr.org/download/antlr-4.10.1-complete.jar
  SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/dependencies
  DOWNLOAD_NO_EXTRACT TRUE
)

FetchContent_GetProperties(AntlrJar)
FetchContent_Populate(AntlrJar)

Dockerfile:

FROM ubuntu:20.04

WORKDIR /src

RUN apt update
RUN apt install -y cmake g++

COPY CMakeLists.txt /src

And then I just execute cmake . in /src and notice "dependencies" being empty.


Solution

  • Parameter SOURCE_DIR of FetchContent_Declare (and of ExternalProject_Add) affects where the downloaded file will be unpacked:

    SOURCE_DIR <dir>

    Source directory into which downloaded contents will be unpacked, or for non-URL download methods, the directory in which the repository should be checked out, cloned, etc.

    (cited from documentation).

    For specify directory where downloaded file will be stored, use DOWNLOAD_DIR parameter:

    DOWNLOAD_DIR <dir>

    Directory in which to store downloaded files before unpacking them. This directory is only used by the URL download method, all other download methods use SOURCE_DIR directly instead.

    FetchContent_Declare(
      AntlrJar
      URL      https://www.antlr.org/download/antlr-4.10.1-complete.jar
      DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/dependencies
      DOWNLOAD_NO_EXTRACT TRUE
    )