diff options
-rw-r--r-- | src/buildtool/common/TARGETS | 15 | ||||
-rw-r--r-- | src/buildtool/common/location.cpp | 54 | ||||
-rw-r--r-- | src/buildtool/common/location.hpp | 33 | ||||
-rw-r--r-- | src/other_tools/just_mr/TARGETS | 1 | ||||
-rw-r--r-- | src/other_tools/just_mr/rc.cpp | 82 | ||||
-rw-r--r-- | src/other_tools/just_mr/utils.hpp | 3 |
6 files changed, 121 insertions, 67 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS index 5e882fce..c20ea4e5 100644 --- a/src/buildtool/common/TARGETS +++ b/src/buildtool/common/TARGETS @@ -145,4 +145,19 @@ , ["src/buildtool/main", "constants"] ] } +, "location": + { "type": ["@", "rules", "CC", "library"] + , "name": ["location"] + , "hdrs": ["location.hpp"] + , "srcs": ["location.cpp"] + , "deps": [["@", "json", "", "json"]] + , "stage": ["src", "buildtool", "common"] + , "private-deps": + [ ["@", "fmt", "", "fmt"] + , ["src/buildtool/file_system", "file_system_manager"] + , ["src/buildtool/logging", "log_level"] + , ["src/buildtool/logging", "logging"] + , ["src/buildtool/storage", "config"] + ] + } } diff --git a/src/buildtool/common/location.cpp b/src/buildtool/common/location.cpp new file mode 100644 index 00000000..bc3a57dc --- /dev/null +++ b/src/buildtool/common/location.cpp @@ -0,0 +1,54 @@ +// 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/common/location.hpp" + +#include "fmt/core.h" +#include "src/buildtool/file_system/file_system_manager.hpp" +#include "src/buildtool/logging/log_level.hpp" +#include "src/buildtool/logging/logger.hpp" +#include "src/buildtool/storage/config.hpp" + +auto ReadLocationObject(nlohmann::json const& location, + std::optional<std::filesystem::path> const& ws_root) + -> std::variant<std::string, std::optional<location_res_t>> { + if (not location.contains("path") or not location.contains("root")) { + return fmt::format("Malformed location object: {}", location.dump(-1)); + } + auto root = location["root"].get<std::string>(); + auto path = location["path"].get<std::string>(); + auto base = location.contains("base") ? location["base"].get<std::string>() + : std::string("."); + + std::filesystem::path root_path{}; + if (root == "workspace") { + if (not ws_root) { + Logger::Log(LogLevel::Warning, + "Not in workspace root, ignoring location {}.", + location.dump(-1)); + return std::nullopt; + } + root_path = *ws_root; + } + if (root == "home") { + root_path = StorageConfig::GetUserHome(); + } + if (root == "system") { + root_path = FileSystemManager::GetCurrentDirectory().root_path(); + } + return std::make_pair(std::filesystem::weakly_canonical( + std::filesystem::absolute(root_path / path)), + std::filesystem::weakly_canonical( + std::filesystem::absolute(root_path / base))); +} diff --git a/src/buildtool/common/location.hpp b/src/buildtool/common/location.hpp new file mode 100644 index 00000000..2e9d3841 --- /dev/null +++ b/src/buildtool/common/location.hpp @@ -0,0 +1,33 @@ +// 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 <filesystem> +#include <optional> +#include <string> +#include <unordered_set> +#include <utility> +#include <variant> + +#include "nlohmann/json.hpp" + +using location_res_t = std::pair<std::filesystem::path, std::filesystem::path>; + +/// \brief Parse a location object stored in a JSON object. +/// \returns An error + data union, where at index 0 is the string message on +/// fatal failure, and at index 1 is an optional parsed location (nullopt if +/// location should be ignored) on success. +[[nodiscard]] auto ReadLocationObject( + nlohmann::json const& location, + std::optional<std::filesystem::path> const& ws_root) + -> std::variant<std::string, std::optional<location_res_t>>; diff --git a/src/other_tools/just_mr/TARGETS b/src/other_tools/just_mr/TARGETS index 7633eaea..be03021d 100644 --- a/src/other_tools/just_mr/TARGETS +++ b/src/other_tools/just_mr/TARGETS @@ -220,6 +220,7 @@ , "private-deps": [ ["@", "json", "", "json"] , ["src/buildtool/build_engine/expression", "expression"] + , ["src/buildtool/common", "location"] , ["src/buildtool/file_system", "file_system_manager"] , ["src/buildtool/logging", "logging"] , ["src/buildtool/logging", "log_level"] diff --git a/src/other_tools/just_mr/rc.cpp b/src/other_tools/just_mr/rc.cpp index 8a81a686..89fb8ef2 100644 --- a/src/other_tools/just_mr/rc.cpp +++ b/src/other_tools/just_mr/rc.cpp @@ -19,6 +19,7 @@ #include "nlohmann/json.hpp" #include "src/buildtool/build_engine/expression/configuration.hpp" #include "src/buildtool/build_engine/expression/expression_ptr.hpp" +#include "src/buildtool/common/location.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/log_sink_file.hpp" @@ -28,83 +29,34 @@ namespace { +/// \brief Overlay for ReadLocationObject accepting an ExpressionPtr and that +/// can std::exit [[nodiscard]] auto ReadLocation( ExpressionPtr const& location, std::optional<std::filesystem::path> const& ws_root) -> std::optional<std::pair<std::filesystem::path, std::filesystem::path>> { if (location.IsNotNull()) { - auto root = location->Get("root", Expression::none_t{}); - auto path = location->Get("path", Expression::none_t{}); - auto base = location->Get("base", std::string(".")); - - if (not path->IsString() or not root->IsString() or - not kLocationTypes.contains(root->String())) { - Logger::Log(LogLevel::Error, - "Malformed location object: {}", - location.ToJson().dump(-1)); + auto res = ReadLocationObject(location->ToJson(), ws_root); + if (res.index() == 0) { + Logger::Log(LogLevel::Error, std::get<0>(res)); std::exit(kExitConfigError); } - auto root_str = root->String(); - std::filesystem::path root_path{}; - if (root_str == "workspace") { - if (not ws_root) { - Logger::Log(LogLevel::Warning, - "Not in workspace root, ignoring location {}.", - location.ToJson().dump(-1)); - return std::nullopt; - } - root_path = *ws_root; - } - if (root_str == "home") { - root_path = StorageConfig::GetUserHome(); - } - if (root_str == "system") { - root_path = FileSystemManager::GetCurrentDirectory().root_path(); - } - return std::make_pair( - std::filesystem::weakly_canonical( - std::filesystem::absolute(root_path / path->String())), - std::filesystem::weakly_canonical( - std::filesystem::absolute(root_path / base->String()))); + return std::get<1>(res); } return std::nullopt; } +/// \brief Overlay of ReadLocationObject that can std::exit [[nodiscard]] auto ReadLocation( nlohmann::json const& location, std::optional<std::filesystem::path> const& ws_root) -> std::optional<std::pair<std::filesystem::path, std::filesystem::path>> { - if (not location.contains("path") or not location.contains("root")) { - Logger::Log(LogLevel::Error, - "Malformed location object: {}", - location.dump(-1)); + auto res = ReadLocationObject(location, ws_root); + if (res.index() == 0) { + Logger::Log(LogLevel::Error, std::get<0>(res)); std::exit(kExitConfigError); } - auto root = location["root"].get<std::string>(); - auto path = location["path"].get<std::string>(); - auto base = location.contains("base") ? location["base"].get<std::string>() - : std::string("."); - - std::filesystem::path root_path{}; - if (root == "workspace") { - if (not ws_root) { - Logger::Log(LogLevel::Warning, - "Not in workspace root, ignoring location {}.", - location.dump(-1)); - return std::nullopt; - } - root_path = *ws_root; - } - if (root == "home") { - root_path = StorageConfig::GetUserHome(); - } - if (root == "system") { - root_path = FileSystemManager::GetCurrentDirectory().root_path(); - } - return std::make_pair(std::filesystem::weakly_canonical( - std::filesystem::absolute(root_path / path)), - std::filesystem::weakly_canonical( - std::filesystem::absolute(root_path / base))); + return std::get<1>(res); } [[nodiscard]] auto ReadOptionalLocationList( @@ -496,10 +448,12 @@ namespace { log_files->ToString()); std::exit(kExitConfigError); } - for (auto const& log_file : log_files->List()) { - auto path = - ReadLocation(log_file->ToJson(), - clargs->common.just_mr_paths->workspace_root); + auto const& files_list = log_files->List(); + clargs->log.log_files.reserve(clargs->log.log_files.size() + + files_list.size()); + for (auto const& log_file : files_list) { + auto path = ReadLocation( + log_file, clargs->common.just_mr_paths->workspace_root); if (path) { clargs->log.log_files.emplace_back(path->first); } diff --git a/src/other_tools/just_mr/utils.hpp b/src/other_tools/just_mr/utils.hpp index 4d397902..f470ee61 100644 --- a/src/other_tools/just_mr/utils.hpp +++ b/src/other_tools/just_mr/utils.hpp @@ -29,9 +29,6 @@ /* Paths and constants required by just-mr */ -std::unordered_set<std::string> const kLocationTypes{"workspace", - "home", - "system"}; auto const kDefaultJustPath = "just"; auto const kDefaultGitPath = "git"; auto const kDefaultRCPath = StorageConfig::GetUserHome() / ".just-mrrc"; |