Skip to content

Commit c6b8e71

Browse files
Add CMake fetchcontent documentation and tests
Github issue: #2073 nlohmann::json documents 2 way of depending on it using CMake 1) Copy-paste the project/source into your own project. 2) Install nlohman::json and then use find_package. (1) pollutes your git repository, (2) requires everyone to install the dependencies themselves. Since 2018, CMake provide some kind of 'package manager' features using [FetchContent](https://cmake.org/cmake/help/v3.17/module/FetchContent.html) It gives the following: ~~~cmake include(FetchContent) FetchContent_Declare(json GIT_REPOSITORY https://github.com/nlohmann/json GIT_TAG v3.7.3) FetchContent_GetProperties(json) if(NOT json_POPULATED) FetchContent_Populate(json) add_subdirectory( ${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL) endif() ~~~ Then declares the dependency in the target using it: ~~~cmake target_link_library(my_project PRIVATE nlohmann_json::nlohmann_json ~~~ This patch updates the documentation and provides tests.
1 parent b1fe6ee commit c6b8e71

5 files changed

Lines changed: 76 additions & 0 deletions

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ add_library(foo ...)
133133
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)
134134
```
135135

136+
##### Embedded (FetchContent)
137+
Since CMake v3.11,
138+
[FetchContent](https://cmake.org/cmake/help/v3.11/module/FetchContent.html) can
139+
be used to automatically download the repository as a dependency.
140+
141+
Example:
142+
~~~cmake
143+
include(FetchContent)
144+
145+
FetchContent_Declare(json
146+
GIT_REPOSITORY https://github.com/nlohmann/json
147+
GIT_TAG v3.7.3)
148+
149+
FetchContent_GetProperties(json)
150+
if(NOT json_POPULATED)
151+
FetchContent_Populate(json)
152+
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
153+
endif()
154+
155+
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)
156+
~~~
157+
158+
**Note**: The repository https://github.com/nlohmann/json download size is huge.
159+
It contains all the dataset used for benchmarking. You might want to depend on
160+
repository tracking only the release files. For instance, you might want to
161+
replace the URL above by
162+
https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
163+
164+
/!\ DO_NOT_COMMIT: Request adding CMakeLists.txt to astoeckel/json before landing this.
165+
136166
#### Supporting Both
137167

138168
To allow your project to support either an externally supplied or an embedded JSON library, you can use a pattern akin to the following:

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,4 @@ endforeach()
189189
add_subdirectory(cmake_import)
190190
add_subdirectory(cmake_import_minver)
191191
add_subdirectory(cmake_add_subdirectory)
192+
add_subdirectory(cmake_fetch_content)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
add_test(NAME cmake_fetch_content_configure
2+
COMMAND ${CMAKE_COMMAND}
3+
-G "${CMAKE_GENERATOR}"
4+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
5+
-Dnlohmann_json_source=${PROJECT_SOURCE_DIR}
6+
${CMAKE_CURRENT_SOURCE_DIR}/project
7+
)
8+
add_test(NAME cmake_fetch_content_build
9+
COMMAND ${CMAKE_COMMAND} --build .
10+
)
11+
set_tests_properties(cmake_fetch_content_configure PROPERTIES
12+
FIXTURES_SETUP cmake_fetch_content
13+
)
14+
set_tests_properties(cmake_fetch_content_build PROPERTIES
15+
FIXTURES_REQUIRED cmake_fetch_content
16+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
3+
project(DummyImport CXX)
4+
5+
include(FetchContent)
6+
7+
FetchContent_Declare(json
8+
GIT_REPOSITORY ${CMAKE_CURRENT_SOURCE_DIR}/../../..
9+
GIT_TAG HEAD)
10+
11+
FetchContent_GetProperties(json)
12+
if(NOT json_POPULATED)
13+
FetchContent_Populate(json)
14+
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
15+
endif()
16+
17+
add_executable(with_namespace_target main.cpp)
18+
target_link_libraries(with_namespace_target nlohmann_json::nlohmann_json)
19+
20+
add_executable(without_namespace_target main.cpp)
21+
target_link_libraries(without_namespace_target nlohmann_json)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <nlohmann/json.hpp>
2+
3+
int main(int argc, char **argv)
4+
{
5+
nlohmann::json j;
6+
7+
return 0;
8+
}

0 commit comments

Comments
 (0)