summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-07-09 12:26:58 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-07-16 17:51:12 +0200
commitb095fb67afbd8fe72122cad22d3cbde8b2bf897c (patch)
tree4c217ac02b85dd805c4a5543e88c419632eac511
parent6c95b7c6e20467270da26cd5d10111bb9307c065 (diff)
downloadjustbuild-b095fb67afbd8fe72122cad22d3cbde8b2bf897c.tar.gz
Create separate library for DescribeBackend
-rw-r--r--src/buildtool/storage/TARGETS12
-rw-r--r--src/buildtool/storage/backend_description.cpp59
-rw-r--r--src/buildtool/storage/backend_description.hpp34
3 files changed, 105 insertions, 0 deletions
diff --git a/src/buildtool/storage/TARGETS b/src/buildtool/storage/TARGETS
index 2d624afb..705d86c6 100644
--- a/src/buildtool/storage/TARGETS
+++ b/src/buildtool/storage/TARGETS
@@ -103,4 +103,16 @@
, "stage": ["src", "buildtool", "storage"]
, "private-deps": [["@", "gsl", "", "gsl"]]
}
+, "backend_description":
+ { "type": ["@", "rules", "CC", "library"]
+ , "name": ["backend_description"]
+ , "hdrs": ["backend_description.hpp"]
+ , "srcs": ["backend_description.cpp"]
+ , "deps":
+ [ ["src/buildtool/common/remote", "remote_common"]
+ , ["src/utils/cpp", "expected"]
+ ]
+ , "stage": ["src", "buildtool", "storage"]
+ , "private-deps": [["@", "fmt", "", "fmt"], ["@", "json", "", "json"]]
+ }
}
diff --git a/src/buildtool/storage/backend_description.cpp b/src/buildtool/storage/backend_description.cpp
new file mode 100644
index 00000000..456f79c6
--- /dev/null
+++ b/src/buildtool/storage/backend_description.cpp
@@ -0,0 +1,59 @@
+// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/buildtool/storage/backend_description.hpp"
+
+#include <exception>
+
+#include "fmt/core.h"
+#include "nlohmann/json.hpp"
+
+auto DescribeBackend(std::optional<ServerAddress> const& address,
+ ExecutionProperties const& properties,
+ std::vector<DispatchEndpoint> const& dispatch) noexcept
+ -> expected<std::string, std::string> {
+ auto description = nlohmann::json{
+ {"remote_address", address ? address->ToJson() : nlohmann::json{}},
+ {"platform_properties", properties}};
+ if (not dispatch.empty()) {
+ try {
+ // only add the dispatch list, if not empty, so that keys remain
+ // not only more readable, but also backwards compatible with
+ // earlier versions.
+ auto dispatch_list = nlohmann::json::array();
+ for (auto const& [props, endpoint] : dispatch) {
+ auto entry = nlohmann::json::array();
+ entry.push_back(nlohmann::json(props));
+ entry.push_back(endpoint.ToJson());
+ dispatch_list.push_back(entry);
+ }
+ description["endpoint dispatch list"] = dispatch_list;
+ } catch (std::exception const& e) {
+ return unexpected{fmt::format(
+ "Failed to serialize endpoint dispatch list:\n{}", e.what())};
+ }
+ }
+ try {
+ // json::dump with json::error_handler_t::replace will not throw an
+ // exception if invalid UTF-8 sequences are detected in the input.
+ // Instead, it will replace them with the UTF-8 replacement
+ // character, but still it needs to be inside a try-catch clause to
+ // ensure the noexcept modifier of the enclosing function.
+ return description.dump(
+ 2, ' ', false, nlohmann::json::error_handler_t::replace);
+ } catch (std::exception const& e) {
+ return unexpected{fmt::format(
+ "Failed to dump backend description to JSON:\n{}", e.what())};
+ }
+}
diff --git a/src/buildtool/storage/backend_description.hpp b/src/buildtool/storage/backend_description.hpp
new file mode 100644
index 00000000..544ee1b9
--- /dev/null
+++ b/src/buildtool/storage/backend_description.hpp
@@ -0,0 +1,34 @@
+// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef INCLUDED_SRC_BUILDTOOL_STORAGE_BACKEND_DESCRIPTION_HPP
+#define INCLUDED_SRC_BUILDTOOL_STORAGE_BACKEND_DESCRIPTION_HPP
+
+#include <map>
+#include <optional>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "src/buildtool/common/remote/remote_common.hpp"
+#include "src/utils/cpp/expected.hpp"
+
+/// \brief String representation of the used execution backend.
+[[nodiscard]] auto DescribeBackend(
+ std::optional<ServerAddress> const& address,
+ ExecutionProperties const& properties,
+ std::vector<DispatchEndpoint> const& dispatch) noexcept
+ -> expected<std::string, std::string>;
+
+#endif // INCLUDED_SRC_BUILDTOOL_STORAGE_BACKEND_DESCRIPTION_HPP