FetchContent: SOURCE_DIR is ignored with FULLY_DISCONNECTED defined
After initial CMake call which downloads all necessary dependencies via FetchContent I turn it off by defining FETCHCONTENT_FULLY_DISCONNECTED=ON
to shorten subsequent CMake calls - on Windows where process creation is slow and dozen of contents it's tiresome to wait tens of seconds even after a trivial change in one of the CMake files.
With some dependencies with flat directory structure I override their default SOURCE_DIR
in order to locate them in an additional subdirectory (for #include
purpose). However, SOURCE_DIR
provided in FetchContent_Declare
is ignored with FETCHCONTENT_FULLY_DISCONNECTED=ON
. MWE:
cmake_minimum_required(VERSION 3.11)
project(fetchcontenttest LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(json11
URL https://github.com/dropbox/json11/archive/2df9473fb3605980db55ecddf34392a2e832ad35.zip
URL_HASH SHA256=1616ef63fde58df57a403296c7811adf458d4c0e15388f37ca7e8554d7ff6f2c
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/json11-src/json11
)
FetchContent_GetProperties(json11)
if(NOT json11_POPULATED)
FetchContent_Populate(json11)
message(STATUS "json11_SOURCE_DIR: ${json11_SOURCE_DIR}")
endif()
To this day, I've been using a patched FetchContent.cmake which seems to work fine:
elseif(FETCHCONTENT_FULLY_DISCONNECTED)
# Bypass population and assume source is already there from a previous run
- set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
- set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
+ __FetchContent_getSavedDetails(${contentName} contentDetails)
+ if("${contentDetails}" STREQUAL "")
+ message(FATAL_ERROR "No details have been set for content: ${contentName}")
+ endif()
+ cmake_parse_arguments(savedDetails "" "SOURCE_DIR;BINARY_DIR" "" ${contentDetails})
+
+ set(${contentNameLower}_SOURCE_DIR ${savedDetails_SOURCE_DIR})
+ set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
else()
Edited by Craig Scott