From 619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 22 Feb 2022 17:03:21 +0100 Subject: 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 Co-authored-by: Victor Moreno --- src/buildtool/common/repository_config.hpp | 133 +++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/buildtool/common/repository_config.hpp (limited to 'src/buildtool/common/repository_config.hpp') diff --git a/src/buildtool/common/repository_config.hpp b/src/buildtool/common/repository_config.hpp new file mode 100644 index 00000000..62c2c4ff --- /dev/null +++ b/src/buildtool/common/repository_config.hpp @@ -0,0 +1,133 @@ +#ifndef INCLUDED_SRC_BUILDTOOL_COMMON_REPOSITORY_CONFIG_HPP +#define INCLUDED_SRC_BUILDTOOL_COMMON_REPOSITORY_CONFIG_HPP + +#include +#include +#include + +#include "src/buildtool/file_system/file_root.hpp" +#include "src/buildtool/file_system/git_cas.hpp" + +class RepositoryConfig { + public: + struct RepositoryInfo { + FileRoot workspace_root; + FileRoot target_root{workspace_root}; + FileRoot rule_root{target_root}; + FileRoot expression_root{rule_root}; + std::unordered_map name_mapping{}; + std::string target_file_name{"TARGETS"}; + std::string rule_file_name{"RULES"}; + std::string expression_file_name{"EXPRESSIONS"}; + }; + + [[nodiscard]] static auto Instance() noexcept -> RepositoryConfig& { + static RepositoryConfig instance{}; + return instance; + } + + void SetInfo(std::string const& repo, RepositoryInfo&& info) { + infos_.emplace(repo, std::move(info)); + } + + [[nodiscard]] auto SetGitCAS( + std::filesystem::path const& repo_path) noexcept { + git_cas_ = GitCAS::Open(repo_path); + return static_cast(git_cas_); + } + + [[nodiscard]] auto Info(std::string const& repo) const noexcept + -> RepositoryInfo const* { + auto it = infos_.find(repo); + if (it != infos_.end()) { + return &it->second; + } + return nullptr; + } + + [[nodiscard]] auto ReadBlobFromGitCAS(std::string const& hex_id) + const noexcept -> std::optional { + return git_cas_ ? git_cas_->ReadObject(hex_id, /*is_hex_id=*/true) + : std::nullopt; + } + + [[nodiscard]] auto WorkspaceRoot(std::string const& repo) const noexcept + -> FileRoot const* { + return Get( + repo, [](auto const& info) { return &info.workspace_root; }); + } + + [[nodiscard]] auto TargetRoot(std::string const& repo) const noexcept + -> FileRoot const* { + return Get( + repo, [](auto const& info) { return &info.target_root; }); + } + + [[nodiscard]] auto RuleRoot(std::string const& repo) const + -> FileRoot const* { + return Get(repo, + [](auto const& info) { return &info.rule_root; }); + } + + [[nodiscard]] auto ExpressionRoot(std::string const& repo) const noexcept + -> FileRoot const* { + return Get( + repo, [](auto const& info) { return &info.expression_root; }); + } + + [[nodiscard]] auto GlobalName(std::string const& repo, + std::string const& local_name) const noexcept + -> std::string const* { + return Get( + repo, [&local_name](auto const& info) -> std::string const* { + auto it = info.name_mapping.find(local_name); + if (it != info.name_mapping.end()) { + return &it->second; + } + return nullptr; + }); + } + + [[nodiscard]] auto TargetFileName(std::string const& repo) const noexcept + -> std::string const* { + return Get( + repo, [](auto const& info) { return &info.target_file_name; }); + } + + [[nodiscard]] auto RuleFileName(std::string const& repo) const noexcept + -> std::string const* { + return Get( + repo, [](auto const& info) { return &info.rule_file_name; }); + } + + [[nodiscard]] auto ExpressionFileName( + std::string const& repo) const noexcept -> std::string const* { + return Get( + repo, [](auto const& info) { return &info.expression_file_name; }); + } + + // used for testing + void Reset() { + infos_.clear(); + git_cas_.reset(); + } + + private: + std::unordered_map infos_; + GitCASPtr git_cas_; + + template + [[nodiscard]] auto Get(std::string const& repo, + std::function const& + getter) const noexcept -> T const* { + if (auto const* info = Info(repo)) { + try { // satisfy clang-tidy's bugprone-exception-escape + return getter(*info); + } catch (...) { + } + } + return nullptr; + } +}; + +#endif // INCLUDED_SRC_BUILDTOOL_COMMON_REPOSITORY_CONFIG_HPP -- cgit v1.2.3