[CMake] Passing CMake Arguments to FetchContent

Jason Beach jason.m.beach at gmail.com
Fri Mar 22 22:58:08 EDT 2019


I'm upgrading from cmake 3.5.1 and am trying to understand the new
FetchContent command. So far I have:

cmake_minimum_required(VERSION 3.14)
project (json_test)

include(FetchContent)
set(JSON_BuildTests OFF) #if I try this I get a warning as it appears to be
deprecated

FetchContent_Declare(
    nlohmann_json_fc
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.6.1
    GIT_SHALLOW TRUE
#    CMAKE_ARGS -DJSON_BuildTests=OFF #doesn't work
)
FetchContent_MakeAvailable(nlohmann_json_fc)
add_executable(main src/main.cpp)
target_link_libraries(main nlohmann_json::nlohmann_json)


I first tried sending in the JSON_BuildTests=OFF with CMAKE_ARGS in the
FetchContent_Declare command but that didn't work.  On this post
https://cmake.org/pipermail/cmake/2018-July/067804.html it appears this is
because only the download portion of FetchContent_Declare is enabled with
the intent to not do too much during configure time. How does specifying
how the external project is configured fit that rationale?  If I execute

cmake -DJSON_BuildTests=OFF ..

the argument successfully makes it through to the external project. I guess
I'm trying to understand why it's ok to specify it on the command line but
not permanently in the CMakeLists.txt, particularly if you have many
libraries each potentially with their options that need to be configured.

Thanks,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190322/d6889321/attachment.html>


More information about the CMake mailing list