summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2022-08-30 11:48:02 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2022-12-21 14:44:08 +0100
commit8d7bd8a41a1881ae244c9709dc45cf79f0a6ecea (patch)
treeee7aede428244d3fa58509398b1bceacd1543569
parent0737abb8b7e6180a6b38f04d0e5d072c5d60f4ab (diff)
downloadjustbuild-8d7bd8a41a1881ae244c9709dc45cf79f0a6ecea.tar.gz
Just-MR: Add utilities library for just-mr
-rw-r--r--src/buildtool/execution_api/local/config.hpp12
-rw-r--r--src/other_tools/just_mr/TARGETS14
-rw-r--r--src/other_tools/just_mr/utils.cpp77
-rw-r--r--src/other_tools/just_mr/utils.hpp150
-rw-r--r--src/utils/cpp/TARGETS6
5 files changed, 249 insertions, 10 deletions
diff --git a/src/buildtool/execution_api/local/config.hpp b/src/buildtool/execution_api/local/config.hpp
index c3b526bd..1b129053 100644
--- a/src/buildtool/execution_api/local/config.hpp
+++ b/src/buildtool/execution_api/local/config.hpp
@@ -142,12 +142,6 @@ class LocalExecutionConfig {
return Data().launcher;
}
- private:
- [[nodiscard]] static auto Data() noexcept -> ConfigData& {
- static ConfigData instance{};
- return instance;
- }
-
/// \brief Determine user root directory
[[nodiscard]] static auto GetUserHome() noexcept -> std::filesystem::path {
char const* root{nullptr};
@@ -167,6 +161,12 @@ class LocalExecutionConfig {
return root;
}
+
+ private:
+ [[nodiscard]] static auto Data() noexcept -> ConfigData& {
+ static ConfigData instance{};
+ return instance;
+ }
};
#endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_CONFIG_HPP
diff --git a/src/other_tools/just_mr/TARGETS b/src/other_tools/just_mr/TARGETS
new file mode 100644
index 00000000..181e563c
--- /dev/null
+++ b/src/other_tools/just_mr/TARGETS
@@ -0,0 +1,14 @@
+{ "utils":
+ { "type": ["@", "rules", "CC", "library"]
+ , "name": ["utils"]
+ , "hdrs": ["utils.hpp"]
+ , "srcs": ["utils.cpp"]
+ , "deps":
+ [ ["src/utils/cpp", "tmp_dir"]
+ , ["src/buildtool/execution_api/local", "config"]
+ , ["src/buildtool/main", "constants"]
+ ]
+ , "stage": ["src", "other_tools", "just_mr"]
+ , "private-deps": [["src/utils/cpp", "path"]]
+ }
+}
diff --git a/src/other_tools/just_mr/utils.cpp b/src/other_tools/just_mr/utils.cpp
new file mode 100644
index 00000000..3470b7db
--- /dev/null
+++ b/src/other_tools/just_mr/utils.cpp
@@ -0,0 +1,77 @@
+// Copyright 2022 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/other_tools/just_mr/utils.hpp"
+
+#include "src/utils/cpp/path.hpp"
+
+namespace JustMR::Utils {
+
+auto GetGitCacheRoot() noexcept -> std::filesystem::path {
+ return LocalExecutionConfig::BuildRoot() / "git";
+}
+
+auto GetGitRoot(JustMR::PathsPtr const& just_mr_paths,
+ std::string const& repo_url) noexcept -> std::filesystem::path {
+ if (just_mr_paths->git_checkout_locations.contains(repo_url)) {
+ return std::filesystem::absolute(ToNormalPath(std::filesystem::path(
+ just_mr_paths->git_checkout_locations[repo_url])));
+ }
+ auto repo_url_as_path = std::filesystem::absolute(
+ ToNormalPath(std::filesystem::path(repo_url)));
+ if (not repo_url_as_path.empty() and
+ FileSystemManager::IsAbsolutePath(repo_url_as_path) and
+ FileSystemManager::IsDirectory(repo_url_as_path)) {
+ return repo_url_as_path;
+ }
+ return GetGitCacheRoot();
+}
+
+auto CreateTypedTmpDir(std::string const& type) noexcept -> TmpDirPtr {
+ // try to create parent dir
+ auto parent_path =
+ LocalExecutionConfig::BuildRoot() / "tmp-workspaces" / type;
+ return TmpDir::Create(parent_path);
+}
+
+auto GetArchiveTreeIDFile(std::string const& repo_type,
+ std::string const& content) noexcept
+ -> std::filesystem::path {
+ return LocalExecutionConfig::BuildRoot() / "tree-map" / repo_type / content;
+}
+
+auto GetDistdirTreeIDFile(std::string const& content) noexcept
+ -> std::filesystem::path {
+ return LocalExecutionConfig::BuildRoot() / "distfiles-tree-map" / content;
+}
+
+auto WriteTreeIDFile(std::filesystem::path const& tree_id_file,
+ std::string const& tree_id) noexcept -> bool {
+ // needs to be done safely, so use the rename trick
+ auto tmp_dir = TmpDir::Create(tree_id_file.parent_path());
+ if (not tmp_dir) {
+ Logger::Log(LogLevel::Error,
+ "could not create tmp dir for writing tree id file {}",
+ tree_id_file.string());
+ return false;
+ }
+ std::filesystem::path tmp_file{tmp_dir->GetPath() / "tmp_file"};
+ if (not FileSystemManager::WriteFile(tree_id, tmp_file)) {
+ Logger::Log(LogLevel::Error, "could not create tmp tree id file");
+ return false;
+ }
+ return FileSystemManager::Rename(tmp_file.string(), tree_id_file);
+}
+
+} // namespace JustMR::Utils
diff --git a/src/other_tools/just_mr/utils.hpp b/src/other_tools/just_mr/utils.hpp
new file mode 100644
index 00000000..4e5e69c8
--- /dev/null
+++ b/src/other_tools/just_mr/utils.hpp
@@ -0,0 +1,150 @@
+// Copyright 2022 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_OTHER_TOOLS_JUST_MR_UTILS_HPP
+#define INCLUDED_SRC_OTHER_TOOLS_JUST_MR_UTILS_HPP
+
+#include <unordered_set>
+
+#include "nlohmann/json.hpp"
+#include "src/buildtool/execution_api/local/config.hpp"
+#include "src/buildtool/main/constants.hpp"
+#include "src/utils/cpp/tmp_dir.hpp"
+
+/* Paths and constants required by just-mr */
+
+std::unordered_set<std::string> const kLocationTypes{"workspace",
+ "home",
+ "system"};
+auto const kDefaultJustPath = "just";
+auto const kDefaultRCPath = LocalExecutionConfig::GetUserHome() / ".just-mrrc";
+auto const kDefaultBuildRoot = LocalExecutionConfig::GetUserDir();
+auto const kDefaultCheckoutLocationsFile =
+ LocalExecutionConfig::GetUserHome() / ".just-local.json";
+auto const kDefaultDistdirs =
+ LocalExecutionConfig::GetUserHome() / ".distfiles";
+
+std::vector<std::string> const kAltDirs = {"target_root",
+ "rule_root",
+ "expression_root"};
+
+std::vector<std::string> const kTakeOver = {"bindings",
+ "target_file_name",
+ "rule_file_name",
+ "expression_file_name"};
+
+struct JustSubCmdFlags {
+ bool config;
+ bool build_root;
+};
+// ordered, so that we have replicability
+std::map<std::string, JustSubCmdFlags> const kKnownJustSubcommands{
+ {"version", {false, false}},
+ {"describe", {true, false}},
+ {"analyse", {true, true}},
+ {"build", {true, true}},
+ {"install", {true, true}},
+ {"rebuild", {true, true}},
+ {"install-cas", {false, true}}};
+
+nlohmann::json const kDefaultConfigLocations = nlohmann::json::array(
+ {{{"root", "workspace"}, {"path", "repos.json"}},
+ {{"root", "workspace"}, {"path", "etc/repos.json"}},
+ {{"root", "home"}, {"path", ".just-repos.json"}},
+ {{"root", "system"}, {"path", "etc/just-repos.json"}}});
+
+/// \brief Checkout type enum
+enum class CheckoutType : std::uint8_t { Git, Archive, File, Distdir };
+
+/// \brief Checkout type map
+std::unordered_map<std::string, CheckoutType> const kCheckoutTypeMap = {
+ {"git", CheckoutType::Git},
+ {"archive", CheckoutType::Archive},
+ {"zip", CheckoutType::Archive}, // treated the same as "archive"
+ {"file", CheckoutType::File},
+ {"distdir", CheckoutType::Distdir}};
+
+namespace JustMR {
+
+struct Paths {
+ // user-defined locations
+ std::optional<std::filesystem::path> root{std::nullopt};
+ std::filesystem::path setup_root{FileSystemManager::GetCurrentDirectory()};
+ std::optional<std::filesystem::path> workspace_root{
+ // find workspace root
+ []() -> std::optional<std::filesystem::path> {
+ std::function<bool(std::filesystem::path const&)>
+ is_workspace_root = [&](std::filesystem::path const& path) {
+ return std::any_of(
+ kRootMarkers.begin(),
+ kRootMarkers.end(),
+ [&path](auto const& marker) {
+ return FileSystemManager::Exists(path / marker);
+ });
+ };
+ // default path to current dir
+ auto path = FileSystemManager::GetCurrentDirectory();
+ auto root_path = path.root_path();
+ while (true) {
+ if (is_workspace_root(path)) {
+ return path;
+ }
+ if (path == root_path) {
+ return std::nullopt;
+ }
+ path = path.parent_path();
+ }
+ }()};
+ nlohmann::json git_checkout_locations{};
+ std::vector<std::filesystem::path> distdirs{};
+};
+
+using PathsPtr = std::shared_ptr<JustMR::Paths>;
+
+namespace Utils {
+
+/// \brief Get the Git cache root path.
+[[nodiscard]] auto GetGitCacheRoot() noexcept -> std::filesystem::path;
+
+/// \brief Get location of Git repository. Defaults to the Git cache root when
+/// no better location is found.
+[[nodiscard]] auto GetGitRoot(JustMR::PathsPtr const& just_mr_paths,
+ std::string const& repo_url) noexcept
+ -> std::filesystem::path;
+
+/// \brief Create a tmp directory with controlled lifetime for specific
+/// operations (archive, zip, file, distdir checkouts; fetch; update).
+[[nodiscard]] auto CreateTypedTmpDir(std::string const& type) noexcept
+ -> TmpDirPtr;
+
+/// \brief Get the path to the file storing the tree id of an archive
+/// content.
+[[nodiscard]] auto GetArchiveTreeIDFile(std::string const& repo_type,
+ std::string const& content) noexcept
+ -> std::filesystem::path;
+
+/// \brief Get the path to the file storing the tree id of a distdir list
+/// content.
+[[nodiscard]] auto GetDistdirTreeIDFile(std::string const& content) noexcept
+ -> std::filesystem::path;
+
+/// \brief Write a tree id to file. The parent folder of the file must exist!
+[[nodiscard]] auto WriteTreeIDFile(std::filesystem::path const& tree_id_file,
+ std::string const& tree_id) noexcept -> bool;
+
+} // namespace Utils
+
+} // namespace JustMR
+
+#endif // INCLUDED_SRC_OTHER_TOOLS_JUST_MR_UTILS_HPP \ No newline at end of file
diff --git a/src/utils/cpp/TARGETS b/src/utils/cpp/TARGETS
index 7c190a06..43ff504a 100644
--- a/src/utils/cpp/TARGETS
+++ b/src/utils/cpp/TARGETS
@@ -54,10 +54,8 @@
, "name": ["tmp_dir"]
, "hdrs": ["tmp_dir.hpp"]
, "srcs": ["tmp_dir.cpp"]
- , "deps":
- [ ["src/buildtool/file_system", "file_system_manager"]
- , ["@", "gsl-lite", "", "gsl-lite"]
- ]
+ , "deps": [["@", "gsl-lite", "", "gsl-lite"]]
, "stage": ["src", "utils", "cpp"]
+ , "private-deps": [["src/buildtool/file_system", "file_system_manager"]]
}
}