From fb5f062589c377c00ba0de7691da6425ad7ada1c Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Tue, 14 Mar 2023 00:43:27 +0100 Subject: [PATCH] Test for JSON before using it --- CMakeLists.txt | 10 ++++++++++ docs/source/Installing_SimGrid.rst | 3 +++ include/simgrid/config.h.in | 2 ++ src/dag/loaders.cpp | 11 ++++++++--- tools/cmake/MakeLib.cmake | 4 ++++ tools/cmake/Modules/nlohmann_jsonConfig.cmake | 15 +++++++++++++++ 6 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tools/cmake/Modules/nlohmann_jsonConfig.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f702831160..ac8fa34e57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -231,6 +231,16 @@ else() message(STATUS "Disabling model BMF because Eigen3 was not found. If it's installed, use EIGEN3_HINT to hint cmake about the location of Eigen3Config.cmake") endif() +# Check for our JSON dependency +set(SIMGRID_HAVE_JSON 0) +find_package(nlohmann_json 3.11.2 + HINTS ${nlohmann_json_HINT}) +if (nlohmann_json_FOUND) + set(SIMGRID_HAVE_JSON 1) + message(STATUS "Found nlohmann_json: ${NLOHMANN_JSON_INCLUDE_DIR}") + include_directories(${NLOHMANN_JSON_INCLUDE_DIR}) +endif() + set(HAVE_PAPI 0) if(enable_smpi_papi) include(FindPAPI) diff --git a/docs/source/Installing_SimGrid.rst b/docs/source/Installing_SimGrid.rst index adfb90ee6e..5cb50fbe0d 100644 --- a/docs/source/Installing_SimGrid.rst +++ b/docs/source/Installing_SimGrid.rst @@ -107,6 +107,9 @@ Eigen3 (optional) - On CentOS / Fedora: ``dnf install eigen3-devel`` - On macOS with homebrew: ``brew install eigen`` - Use EIGEN3_HINT to specify where it's installed if cmake doesn't find it automatically. +JSON (optional, for the DAG wfcommons loader) + - On Debian / Ubuntu: ``apt install nlohmann-json3-dev`` + - Use nlohmann_json_HINT to specify where it's installed if cmake doesn't find it automatically. For platform-specific details, please see below. diff --git a/include/simgrid/config.h.in b/include/simgrid/config.h.in index 2995c8d460..6ef3fd76b1 100644 --- a/include/simgrid/config.h.in +++ b/include/simgrid/config.h.in @@ -18,4 +18,6 @@ #cmakedefine NS3_MINOR_VERSION @NS3_MINOR_VERSION@ /* Was the Eigen3 support compiled in? */ #cmakedefine01 SIMGRID_HAVE_EIGEN3 +/* Was the JSON support compiled in? */ +#cmakedefine01 SIMGRID_HAVE_JSON #endif /* SIMGRID_PUBLIC_CONFIG_H */ diff --git a/src/dag/loaders.cpp b/src/dag/loaders.cpp index e78e7169a0..676b98b07f 100644 --- a/src/dag/loaders.cpp +++ b/src/dag/loaders.cpp @@ -1,5 +1,4 @@ -/* Copyright (c) 2009-2023. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2009-2023. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -21,7 +20,9 @@ #include "dax_dtd.h" #include "dax_dtd.c" +#if SIMGRID_HAVE_JSON #include +#endif #if HAVE_GRAPHVIZ #include @@ -89,6 +90,7 @@ static ExecPtr current_job; */ std::vector create_DAG_from_json(const std::string& filename) { +#if SIMGRID_HAVE_JSON std::ifstream f(filename); auto data = nlohmann::json::parse(f); std::vector dag = {}; @@ -149,8 +151,11 @@ std::vector create_DAG_from_json(const std::string& filename) activity->start(); } return dag; +#else + xbt_die("JSON support was not compiled in, probably because nlohmann/json was not found. Please install " + "nlohmann-json3-dev and recompile SimGrid to use this feature."); +#endif } - /** @brief loads a DAX file describing a DAG * * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details. diff --git a/tools/cmake/MakeLib.cmake b/tools/cmake/MakeLib.cmake index 4afca47839..11cf6b9c28 100644 --- a/tools/cmake/MakeLib.cmake +++ b/tools/cmake/MakeLib.cmake @@ -95,6 +95,10 @@ if(HAVE_GRAPHVIZ) endif() endif() +if(SIMGRID_HAVE_JSON) + target_link_libraries(simgrid nlohmann_json::nlohmann_json) +endif() + if(NOT ${DL_LIBRARY} STREQUAL "") SET(SIMGRID_DEP "${SIMGRID_DEP} ${DL_LIBRARY}") endif() diff --git a/tools/cmake/Modules/nlohmann_jsonConfig.cmake b/tools/cmake/Modules/nlohmann_jsonConfig.cmake new file mode 100644 index 0000000000..27b0a864a8 --- /dev/null +++ b/tools/cmake/Modules/nlohmann_jsonConfig.cmake @@ -0,0 +1,15 @@ +include(FindPackageHandleStandardArgs) +set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG ${CMAKE_CURRENT_LIST_FILE}) +find_package_handle_standard_args(nlohmann_json CONFIG_MODE) + +if(NOT TARGET nlohmann_json::nlohmann_json) + include("${CMAKE_CURRENT_LIST_DIR}/nlohmann_jsonTargets.cmake") + if((NOT TARGET nlohmann_json) AND + (NOT nlohmann_json_FIND_VERSION OR + nlohmann_json_FIND_VERSION VERSION_LESS 3.2.0)) + add_library(nlohmann_json INTERFACE IMPORTED) + set_target_properties(nlohmann_json PROPERTIES + INTERFACE_LINK_LIBRARIES nlohmann_json::nlohmann_json + ) + endif() +endif() -- 2.20.1