Skip to content

Commit 233b630

Browse files
[CMake] Use find or fetch mechanism for nlohmann json (#5775)
use find or fetch mechanism for nlohmann json Co-authored-by: Paul Baksic <30337881+bakpaul@users.noreply.github.com>
1 parent 38b8425 commit 233b630

10 files changed

Lines changed: 38 additions & 25699 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ option(SOFA_BUILD_RELEASE_PACKAGE "Run package specific configure" OFF)
5353
# Option to allow some dependencies such as cxxopts to be fetched by cmake if
5454
# the package is not found
5555
option(SOFA_ALLOW_FETCH_DEPENDENCIES "Allow compatible dependencies to be fetched if the package is not found by cmake.
56-
List of dependencies that can be fetched: cxxopts, gtest, metis, CImg" ON)
56+
List of dependencies that can be fetched: cxxopts, gtest, metis, CImg, nlohmann json" ON)
5757

5858
# Option to accelerate the compilation
5959
# see https://cmake.org/cmake/help/v3.16/command/target_precompile_headers.html

Sofa/framework/Config/cmake/Modules/FindJson.cmake

Lines changed: 0 additions & 86 deletions
This file was deleted.

Sofa/framework/Core/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
cmake_minimum_required(VERSION 3.22)
22
project(Sofa.Core LANGUAGES CXX)
33

4+
find_package(nlohmann_json QUIET)
5+
6+
if(NOT TARGET nlohmann_json AND SOFA_ALLOW_FETCH_DEPENDENCIES)
7+
message("Sofa.Core: DEPENDENCY nlohmann_json NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is ON, fetching nlohmann_json...")
8+
9+
set(JSON_BuildTests OFF CACHE INTERNAL "")
10+
11+
sofa_fetch_dependency(nlohmann_json
12+
GIT_REPOSITORY https://github.com/nlohmann/json.git
13+
GIT_TAG v3.12.0
14+
)
15+
elseif(NOT TARGET nlohmann_json)
16+
message(FATAL_ERROR "Sofa.Core: DEPENDENCY nlohmann_json NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is OFF and thus cannot be fetched. Install nlohmann_json (version>=3.9.1), or enable SOFA_ALLOW_FETCH_DEPENDENCIES to fix this issue.")
17+
endif()
18+
419
set(SRC_ROOT "src/sofa/core")
520

621
set(HEADER_FILES
@@ -352,6 +367,8 @@ add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
352367

353368
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Helper Sofa.Topology Sofa.DefaultType)
354369

370+
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)
371+
355372
if(SOFA_BUILD_WITH_PCH_ENABLED)
356373
message("Adding precompiled header for Sofa.Core")
357374
target_precompile_headers(${PROJECT_NAME} PUBLIC ${SRC_ROOT}/objectmodel/BaseObject.h ${SRC_ROOT}/objectmodel/Data.h)

Sofa/framework/Core/src/sofa/core/ObjectFactoryJson.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
******************************************************************************/
2222
#include <sofa/core/ObjectFactoryJson.h>
2323
#include <sofa/core/ObjectFactory.h>
24-
#include <json.h>
24+
#include <nlohmann/json.hpp>
2525
#include <sofa/core/CategoryLibrary.h>
2626

2727

Sofa/framework/Helper/CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,23 @@ project(Sofa.Helper LANGUAGES CXX)
33

44
# Eigen (header only)
55
sofa_find_package(Eigen3 REQUIRED)
6+
67
# Json (header only) needed by AdvancedTimer
7-
sofa_find_package(Json 3.1.2 REQUIRED)
8+
find_package(nlohmann_json QUIET)
9+
10+
if(NOT TARGET nlohmann_json AND SOFA_ALLOW_FETCH_DEPENDENCIES)
11+
message("Sofa.GUI.Common: DEPENDENCY nlohmann_json NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is ON, fetching nlohmann_json...")
12+
13+
set(JSON_BuildTests OFF CACHE INTERNAL "")
14+
15+
sofa_fetch_dependency(nlohmann_json
16+
GIT_REPOSITORY https://github.com/nlohmann/json.git
17+
GIT_TAG v3.12.0
18+
)
19+
elseif(NOT TARGET nlohmann_json)
20+
message(FATAL_ERROR "Sofa.GUI.Common: DEPENDENCY nlohmann_json NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is OFF and thus cannot be fetched. Install nlohmann_json (version>=3.9.1), or enable SOFA_ALLOW_FETCH_DEPENDENCIES to fix this issue.")
21+
endif()
22+
823
# STB (header only)
924
sofa_find_package(STB REQUIRED)
1025
# DiffLib (header only)
@@ -249,7 +264,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
249264
endif()
250265

251266
# Json (header only) needed by AdvancedTimer
252-
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC "$<BUILD_INTERFACE:${JSON_INCLUDE_DIR}>")
267+
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)
253268

254269
# STB (header only) for Image
255270
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC "$<BUILD_INTERFACE:${STB_INCLUDE_DIR}>")

Sofa/framework/Helper/src/sofa/helper/AdvancedTimer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <sofa/helper/logging/Messaging.h>
2525
#include <sofa/helper/AdvancedTimer.h>
2626
#include <sofa/type/vector.h>
27-
#include <json.h>
27+
#include <nlohmann/json.hpp>
2828

2929
#include <iomanip>
3030
#include <cmath>
@@ -37,7 +37,7 @@
3737

3838
#define DEFAULT_INTERVAL 100
3939

40-
using json = sofa::helper::json;
40+
using json = nlohmann::json;
4141

4242

4343
namespace sofa::helper

extlibs/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.22)
33

44
############# extlibs ##############
55

6-
# Nlohmann JSON (header-only)
7-
set(json_ROOT "${CMAKE_CURRENT_LIST_DIR}/json" CACHE PATH "Nlohmann JSON directory")
86
# STB (header-only)
97
set(STB_ROOT "${CMAKE_CURRENT_LIST_DIR}/stb" CACHE PATH "STB directory")
108
# DiffLib

extlibs/json/LICENSE.MIT

Lines changed: 0 additions & 21 deletions
This file was deleted.

extlibs/json/json.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)