summaryrefslogtreecommitdiff
path: root/src/buildtool/file_system/jsonfs.hpp
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2022-02-22 17:03:21 +0100
committerKlaus Aehlig <klaus.aehlig@huawei.com>2022-02-22 17:03:21 +0100
commit619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 (patch)
tree01868de723cb82c86842f33743fa7b14e24c1fa3 /src/buildtool/file_system/jsonfs.hpp
downloadjustbuild-619def44c1cca9f3cdf63544d5f24f2c7a7d9b77.tar.gz
Initial self-hosting commit
This is the initial version of our tool that is able to build itself. In can be bootstrapped by ./bin/bootstrap.py Co-authored-by: Oliver Reiche <oliver.reiche@huawei.com> Co-authored-by: Victor Moreno <victor.moreno1@huawei.com>
Diffstat (limited to 'src/buildtool/file_system/jsonfs.hpp')
-rw-r--r--src/buildtool/file_system/jsonfs.hpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/buildtool/file_system/jsonfs.hpp b/src/buildtool/file_system/jsonfs.hpp
new file mode 100644
index 00000000..0ec381c7
--- /dev/null
+++ b/src/buildtool/file_system/jsonfs.hpp
@@ -0,0 +1,47 @@
+#ifndef INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_JSONFS_HPP
+#define INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_JSONFS_HPP
+
+#include <exception>
+#include <filesystem>
+#include <fstream>
+#include <optional>
+
+#include "src/buildtool/file_system/file_system_manager.hpp"
+#include "src/buildtool/file_system/object_type.hpp"
+#include "src/utils/cpp/json.hpp"
+
+class Json {
+ public:
+ // Note that we are not using std::pair<std::nlohmann, bool> and being
+ // coherent with FileSystemManager::ReadFile because there is a bug in llvm
+ // toolchain related to type_traits that does not allow us to use
+ // std::pair<T,U> where T or U are nlohmann::json.
+ // LLVM bug report: https://bugs.llvm.org/show_bug.cgi?id=48507
+ // Minimal example: https://godbolt.org/z/zacedsGzo
+ [[nodiscard]] static auto ReadFile(
+ std::filesystem::path const& file) noexcept
+ -> std::optional<nlohmann::json> {
+ auto const type = FileSystemManager::Type(file);
+ if (not type or not IsFileObject(*type)) {
+ Logger::Log(LogLevel::Debug,
+ "{} can not be read because it is not a file.",
+ file.string());
+ return std::nullopt;
+ }
+ try {
+ nlohmann::json content;
+ std::ifstream file_reader(file.string());
+ if (file_reader.is_open()) {
+ file_reader >> content;
+ return content;
+ }
+ return std::nullopt;
+ } catch (std::exception const& e) {
+ Logger::Log(LogLevel::Error, e.what());
+ return std::nullopt;
+ }
+ }
+
+}; // Class Json
+
+#endif // INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_JSONFS_HPP