diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-02-22 17:03:21 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-02-22 17:03:21 +0100 |
commit | 619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 (patch) | |
tree | 01868de723cb82c86842f33743fa7b14e24c1fa3 /src/buildtool/common/repository_config.hpp | |
download | justbuild-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/common/repository_config.hpp')
-rw-r--r-- | src/buildtool/common/repository_config.hpp | 133 |
1 files changed, 133 insertions, 0 deletions
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 <filesystem> +#include <string> +#include <unordered_map> + +#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<std::string, std::string> 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<bool>(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<std::string> { + 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<FileRoot>( + repo, [](auto const& info) { return &info.workspace_root; }); + } + + [[nodiscard]] auto TargetRoot(std::string const& repo) const noexcept + -> FileRoot const* { + return Get<FileRoot>( + repo, [](auto const& info) { return &info.target_root; }); + } + + [[nodiscard]] auto RuleRoot(std::string const& repo) const + -> FileRoot const* { + return Get<FileRoot>(repo, + [](auto const& info) { return &info.rule_root; }); + } + + [[nodiscard]] auto ExpressionRoot(std::string const& repo) const noexcept + -> FileRoot const* { + return Get<FileRoot>( + 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<std::string>( + 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<std::string>( + repo, [](auto const& info) { return &info.target_file_name; }); + } + + [[nodiscard]] auto RuleFileName(std::string const& repo) const noexcept + -> std::string const* { + return Get<std::string>( + repo, [](auto const& info) { return &info.rule_file_name; }); + } + + [[nodiscard]] auto ExpressionFileName( + std::string const& repo) const noexcept -> std::string const* { + return Get<std::string>( + repo, [](auto const& info) { return &info.expression_file_name; }); + } + + // used for testing + void Reset() { + infos_.clear(); + git_cas_.reset(); + } + + private: + std::unordered_map<std::string, RepositoryInfo> infos_; + GitCASPtr git_cas_; + + template <class T> + [[nodiscard]] auto Get(std::string const& repo, + std::function<T const*(RepositoryInfo const&)> 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 |