Skip to content

Commit

Permalink
Compatibility with toml11 4.0 (#1645)
Browse files Browse the repository at this point in the history
* Support toml11 4.0 in CMake

* Fixes for toml11 4.Fixes for toml11 4.00

Still missing: proper setting for precision, backwards compatibility
with toml11 3.0.
Ideally, move toml::format to an internal helper that takes care of all
of this.

* Manually set precisions...

* Compatibility with toml11 3.*

* Add a little comment in CMake

* Add a toml11 4.0.3 CI run

* tmp

* Use two different find_package() calls

* More concise build script for toml11

Co-authored-by: Axel Huebl <[email protected]>

* Some cleanup, fixes and documentation in the find_package() logic

---------

Co-authored-by: Axel Huebl <[email protected]>
  • Loading branch information
franzpoeschel and ax3l authored Jul 19, 2024
1 parent 0fa1803 commit 0358d09
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 17 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ jobs:
python3.11 -m venv /opt/python_venv
. /opt/python_venv/bin/activate
python -m pip install numpy pandas
git clone -b v4.0.3 https://github.com/ToruNiina/toml11
cmake -S toml11 -B build_toml11 \
-DCMAKE_INSTALL_PREFIX=toml11_install \
-DCMAKE_CXX_STANDARD_REQUIRED=OFF \
-DCMAKE_CXX_STANDARD=11
cmake --build build_toml11 -j 2 --target install
- name: Build
env: {CC: clang-14, CXX: clang++-14, CXXFLAGS: -Werror}
run: |
Expand All @@ -144,13 +150,15 @@ jobs:
. /opt/python_venv/bin/activate
share/openPMD/download_samples.sh build
export CMAKE_PREFIX_PATH="$(realpath toml11_install):$CMAKE_PREFIX_PATH"
cmake -S . -B build \
-DopenPMD_USE_PYTHON=ON \
-DopenPMD_USE_MPI=OFF \
-DopenPMD_USE_HDF5=ON \
-DopenPMD_USE_ADIOS2=ON \
-DopenPMD_USE_INVASIVE_TESTS=ON \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DopenPMD_USE_INVASIVE_TESTS=ON \
-DopenPMD_USE_INTERNAL_TOML11=OFF \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DPython_EXECUTABLE="$(which python)"
cmake --build build --parallel 2
ctest --test-dir build --output-on-failure
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,15 @@ if(openPMD_USE_INTERNAL_TOML11)
add_subdirectory("${openPMD_SOURCE_DIR}/share/openPMD/thirdParty/toml11")
message(STATUS "toml11: Using INTERNAL version '3.7.1'")
else()
find_package(toml11 3.7.1 CONFIG REQUIRED)
# toml11 4.0 was a breaking change. This is reflected in the library's CMake
# logic: version 4.0 is not accepted by a call to find_package(toml11 3.7).
# Since we support both incompatible versions, we use two find_package()
# calls. Search for version 4 first in order to prefer that
# in (the unlikely) case that both versions are installed.
find_package(toml11 4.0 CONFIG QUIET)
if(NOT toml11_FOUND)
find_package(toml11 3.7.1 CONFIG REQUIRED)
endif()
message(STATUS "toml11: Found version '${toml11_VERSION}'")
endif()
add_library(openPMD::thirdparty::toml11 INTERFACE IMPORTED)
Expand Down
3 changes: 3 additions & 0 deletions include/openPMD/auxiliary/JSON_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,8 @@ namespace json

nlohmann::json &filterByTemplate(
nlohmann::json &defaultVal, nlohmann::json const &positiveMask);

template <typename toml_t>
std::string format_toml(toml_t &&);
} // namespace json
} // namespace openPMD
2 changes: 1 addition & 1 deletion src/IO/ADIOS/ADIOS2File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ void ADIOS2File::configure_IO()
auto asToml = json::jsonToToml(shadow);
std::cerr << "Warning: parts of the backend configuration for "
"ADIOS2 remain unused:\n"
<< asToml << std::endl;
<< json::format_toml(asToml) << std::endl;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/IO/ADIOS/ADIOS2IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ ADIOS2IOHandlerImpl::flush(internal::ParsedFlushParams &flushParams)
auto asToml = json::jsonToToml(shadow);
std::cerr << "Warning: parts of the backend configuration for "
"ADIOS2 remain unused:\n"
<< asToml << std::endl;
<< json::format_toml(asToml) << std::endl;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/IO/HDF5/HDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ HDF5IOHandlerImpl::HDF5IOHandlerImpl(
std::cerr
<< "Warning: parts of the backend configuration for "
"HDF5 remain unused:\n"
<< asToml << std::endl;
<< json::format_toml(asToml) << std::endl;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/IO/HDF5/ParallelHDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ ParallelHDF5IOHandlerImpl::ParallelHDF5IOHandlerImpl(
auto asToml = json::jsonToToml(shadow);
std::cerr << "Warning: parts of the backend configuration for "
"HDF5 remain unused:\n"
<< asToml << std::endl;
<< json::format_toml(asToml) << std::endl;
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/IO/IOTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void Parameter<Operation::CREATE_DATASET>::warnUnusedParameters<
break;
case json::SupportedLanguages::TOML: {
auto asToml = json::jsonToToml(shadow);
std::cerr << warningMessage << asToml << std::endl;
std::cerr << warningMessage << json::format_toml(asToml)
<< std::endl;
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,8 @@ auto JSONIOHandlerImpl::putJsonContents(
*fh_with_precision << *it->second << std::endl;
break;
case FileFormat::Toml:
*fh_with_precision << openPMD::json::jsonToToml(*it->second)
*fh_with_precision << openPMD::json::format_toml(
openPMD::json::jsonToToml(*it->second))
<< std::endl;
break;
}
Expand Down
62 changes: 55 additions & 7 deletions src/auxiliary/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
#include "openPMD/Error.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
#include "openPMD/auxiliary/Variant.hpp"

#include <nlohmann/json.hpp>
#include <limits>
#include <queue>
#include <toml.hpp>

Expand Down Expand Up @@ -208,10 +209,6 @@ namespace
}
return result;
}
else if (val.is_uninitialized())
{
return nlohmann::json(); // null
}

// @todo maybe generalize error type
throw error::BackendConfigSchema(
Expand Down Expand Up @@ -556,7 +553,7 @@ void warnGlobalUnusedOptions(TracingJSON const &config)
std::cerr
<< "[Series] The following parts of the global TOML config "
"remains unused:\n"
<< asToml << std::endl;
<< json::format_toml(asToml) << std::endl;
}
}
}
Expand Down Expand Up @@ -612,7 +609,7 @@ std::string merge(std::string const &defaultValue, std::string const &overwrite)
case SupportedLanguages::TOML: {
auto asToml = json::jsonToToml(res);
std::stringstream sstream;
sstream << asToml;
sstream << json::format_toml(asToml);
return sstream.str();
}
}
Expand Down Expand Up @@ -646,4 +643,55 @@ filterByTemplate(nlohmann::json &defaultVal, nlohmann::json const &positiveMask)
} // else noop
return defaultVal;
}

constexpr int toml_precision = std::numeric_limits<double>::digits10 + 1;

#if TOML11_VERSION_MAJOR < 4
template <typename toml_t>
std ::string format_toml(toml_t &&val)
{
std::stringstream res;
res << std::setprecision(toml_precision) << std::forward<toml_t>(val);
return res.str();
}

#else

namespace
{
auto set_precision(toml::value &) -> void;
auto set_precision(toml::value &val) -> void
{
if (val.is_table())
{
for (auto &pair : val.as_table())
{
set_precision(pair.second);
}
}
else if (val.is_array())
{
for (auto &entry : val.as_array())
{
set_precision(entry);
}
}
else if (val.is_floating())
{
val.as_floating_fmt().prec = toml_precision;
}
}
} // namespace

template <typename toml_t>
std::string format_toml(toml_t &&val)
{
set_precision(val);
return toml::format(std::forward<toml_t>(val));
}

#endif

template std::string format_toml(toml::value &&);
template std::string format_toml(toml::value &);
} // namespace openPMD::json
2 changes: 1 addition & 1 deletion test/JSONTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ right = "val"
raw, std::ios_base::binary | std::ios_base::in);
toml::value tomlVal = toml::parse(istream);
std::stringstream sstream;
sstream << tomlVal;
sstream << toml::format(tomlVal);
return sort_lines(sstream.str());
}();

Expand Down

0 comments on commit 0358d09

Please sign in to comment.