diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2022-07-29 14:47:50 +0200 |
---|---|---|
committer | Sascha Roloff <sascha.roloff@huawei.com> | 2022-08-05 14:41:31 +0200 |
commit | baa073d46cae264183f576c0acae8df57f42e759 (patch) | |
tree | a6cc74f0ddf0dbe319b13cfb0b0edc400534a2f2 /src | |
parent | 406ee1b8c0f6c2ca3e3cd1281eee6dd59e473a68 (diff) | |
download | justbuild-baa073d46cae264183f576c0acae8df57f42e759.tar.gz |
InstallCas: Moved install-cas code to separate library
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/common/TARGETS | 3 | ||||
-rw-r--r-- | src/buildtool/common/artifact.cpp | 25 | ||||
-rw-r--r-- | src/buildtool/common/artifact.hpp | 3 | ||||
-rw-r--r-- | src/buildtool/main/TARGETS | 18 | ||||
-rw-r--r-- | src/buildtool/main/install_cas.cpp | 62 | ||||
-rw-r--r-- | src/buildtool/main/install_cas.hpp | 24 | ||||
-rw-r--r-- | src/buildtool/main/main.cpp | 37 |
7 files changed, 107 insertions, 65 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS index 950f6347..22a8ffd1 100644 --- a/src/buildtool/common/TARGETS +++ b/src/buildtool/common/TARGETS @@ -30,12 +30,13 @@ , "identifier.hpp" , "statistics.hpp" ] - , "srcs": ["artifact.cpp"] , "deps": [ "bazel_types" , ["src/buildtool/crypto", "hash_function"] , ["src/buildtool/file_system", "object_type"] , ["src/buildtool/compatibility", "compatibility"] + , ["src/buildtool/logging", "logging"] + , ["src/buildtool/execution_api/remote", "config"] , ["src/utils/cpp", "type_safe_arithmetic"] , ["src/utils/cpp", "hash_combine"] , ["@", "json", "", "json"] diff --git a/src/buildtool/common/artifact.cpp b/src/buildtool/common/artifact.cpp deleted file mode 100644 index 0ff89c89..00000000 --- a/src/buildtool/common/artifact.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "src/buildtool/common/artifact.hpp" - -#include <string> - -auto Artifact::ObjectInfo::LiberalFromString(std::string const& s) noexcept - -> Artifact::ObjectInfo { - std::istringstream iss(s); - std::string id{}; - std::string size_str{"0"}; - std::string type{"f"}; - if (iss.peek() == '[') { - (void)iss.get(); - } - std::getline(iss, id, ':'); - if (not iss.eof()) { - std::getline(iss, size_str, ':'); - } - if (not iss.eof()) { - std::getline(iss, type, ']'); - } - auto size = static_cast<std::size_t>(std::atol(size_str.c_str())); - auto const& object_type = FromChar(*type.c_str()); - return Artifact::ObjectInfo{ - ArtifactDigest{id, size, IsTreeObject(object_type)}, object_type}; -} diff --git a/src/buildtool/common/artifact.hpp b/src/buildtool/common/artifact.hpp index 3a713659..2a8c0ebb 100644 --- a/src/buildtool/common/artifact.hpp +++ b/src/buildtool/common/artifact.hpp @@ -93,9 +93,6 @@ class Artifact { } return std::nullopt; } - - [[nodiscard]] static auto LiberalFromString( - std::string const& s) noexcept -> ObjectInfo; }; explicit Artifact(ArtifactIdentifier id) noexcept : id_{std::move(id)} {} diff --git a/src/buildtool/main/TARGETS b/src/buildtool/main/TARGETS index 5b7ff0e5..d09975ae 100644 --- a/src/buildtool/main/TARGETS +++ b/src/buildtool/main/TARGETS @@ -20,6 +20,7 @@ , ["src/utils/cpp", "concepts"] , ["src/utils/cpp", "json"] , "version" + , "install_cas" ] , "stage": ["src", "buildtool", "main"] , "link external": @@ -28,6 +29,23 @@ , "then": ["-static"] } } +, "install_cas": + { "type": ["@", "rules", "CC", "library"] + , "name": ["install_cas"] + , "hdrs": ["install_cas.hpp"] + , "srcs": ["install_cas.cpp"] + , "deps": + [ ["src/buildtool/common", "cli"] + , ["src/buildtool/common", "common"] + , ["src/buildtool/compatibility", "compatibility"] + , ["src/buildtool/crypto", "hash_function"] + , ["src/buildtool/execution_api/common", "common"] + , ["src/buildtool/execution_api/remote", "config"] + , ["src/buildtool/logging", "logging"] + , ["@", "gsl-lite", "", "gsl-lite"] + ] + , "stage": ["src", "buildtool", "main"] + } , "version": { "type": ["@", "rules", "CC", "library"] , "arguments_config": ["SOURCE_DATE_EPOCH", "VERSION_EXTRA_SUFFIX"] diff --git a/src/buildtool/main/install_cas.cpp b/src/buildtool/main/install_cas.cpp new file mode 100644 index 00000000..dbe3b5ec --- /dev/null +++ b/src/buildtool/main/install_cas.cpp @@ -0,0 +1,62 @@ +#include "src/buildtool/main/install_cas.hpp" + +#include "src/buildtool/compatibility/compatibility.hpp" +#include "src/buildtool/crypto/hash_function.hpp" +#include "src/buildtool/execution_api/remote/config.hpp" + +auto ObjectInfoFromLiberalString(std::string const& s) noexcept + -> Artifact::ObjectInfo { + std::istringstream iss(s); + std::string id{}; + std::string size_str{"0"}; + std::string type{"f"}; + if (iss.peek() == '[') { + (void)iss.get(); + } + std::getline(iss, id, ':'); + if (not iss.eof()) { + std::getline(iss, size_str, ':'); + } + if (not iss.eof()) { + std::getline(iss, type, ']'); + } + auto size = static_cast<std::size_t>( + size_str.empty() ? 0 : std::atol(size_str.c_str())); + auto const& object_type = FromChar(*type.c_str()); + return Artifact::ObjectInfo{ + ArtifactDigest{id, size, IsTreeObject(object_type)}, object_type}; +} + +#ifndef BOOTSTRAP_BUILD_TOOL +auto FetchAndInstallArtifacts(gsl::not_null<IExecutionApi*> const& api, + FetchArguments const& clargs) -> bool { + auto object_info = ObjectInfoFromLiberalString(clargs.object_id); + + if (clargs.output_path) { + auto output_path = (*clargs.output_path / "").parent_path(); + if (FileSystemManager::IsDirectory(output_path)) { + output_path /= object_info.digest.hash(); + } + + if (not FileSystemManager::CreateDirectory(output_path.parent_path()) or + not api->RetrieveToPaths({object_info}, {output_path})) { + Logger::Log(LogLevel::Error, "failed to retrieve artifact."); + return false; + } + + Logger::Log(LogLevel::Info, + "artifact {} was installed to {}", + object_info.ToString(), + output_path.string()); + } + else { // dump to stdout + if (not api->RetrieveToFds( + {object_info}, {dup(fileno(stdout))}, clargs.raw_tree)) { + Logger::Log(LogLevel::Error, "failed to dump artifact."); + return false; + } + } + + return true; +} +#endif diff --git a/src/buildtool/main/install_cas.hpp b/src/buildtool/main/install_cas.hpp new file mode 100644 index 00000000..1aab854a --- /dev/null +++ b/src/buildtool/main/install_cas.hpp @@ -0,0 +1,24 @@ + +#ifndef INCLUDED_SRC_BUILDTOOL_MAIN_INSTALL_CAS_HPP +#define INCLUDED_SRC_BUILDTOOL_MAIN_INSTALL_CAS_HPP + +#include <string> + +#include <gsl-lite/gsl-lite.h> + +#include "src/buildtool/common/artifact.hpp" +#include "src/buildtool/common/cli.hpp" +#ifndef BOOTSTRAP_BUILD_TOOL +#include "src/buildtool/execution_api/common/execution_api.hpp" +#endif + +[[nodiscard]] auto ObjectInfoFromLiberalString(std::string const& s) noexcept + -> Artifact::ObjectInfo; + +#ifndef BOOTSTRAP_BUILD_TOOL +[[nodiscard]] auto FetchAndInstallArtifacts( + gsl::not_null<IExecutionApi*> const& api, + FetchArguments const& clargs) -> bool; +#endif + +#endif // INCLUDED_SRC_BUILDTOOL_MAIN_INSTALL_CAS_HPP diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp index 953504ca..af4d044f 100644 --- a/src/buildtool/main/main.cpp +++ b/src/buildtool/main/main.cpp @@ -23,6 +23,7 @@ #include "src/buildtool/common/cli.hpp" #include "src/buildtool/common/repository_config.hpp" #include "src/buildtool/compatibility/compatibility.hpp" +#include "src/buildtool/main/install_cas.hpp" #ifndef BOOTSTRAP_BUILD_TOOL #include "src/buildtool/graph_traverser/graph_traverser.hpp" #include "src/buildtool/progress_reporting/base_progress_reporter.hpp" @@ -1164,42 +1165,6 @@ void ReportTaintedness(const AnalysisResult& result) { Logger::Log(LogLevel::Info, "Target tainted {}.", tainted.dump()); } -#ifndef BOOTSTRAP_BUILD_TOOL -[[nodiscard]] auto FetchAndInstallArtifacts( - gsl::not_null<IExecutionApi*> const& api, - FetchArguments const& clargs) -> bool { - auto object_info = - Artifact::ObjectInfo::LiberalFromString(clargs.object_id); - - if (clargs.output_path) { - auto output_path = (*clargs.output_path / "").parent_path(); - if (FileSystemManager::IsDirectory(output_path)) { - output_path /= object_info.digest.hash(); - } - - if (not FileSystemManager::CreateDirectory(output_path.parent_path()) or - not api->RetrieveToPaths({object_info}, {output_path})) { - Logger::Log(LogLevel::Error, "failed to retrieve artifact."); - return false; - } - - Logger::Log(LogLevel::Info, - "artifact {} was installed to {}", - object_info.ToString(), - output_path.string()); - } - else { // dump to stdout - if (not api->RetrieveToFds( - {object_info}, {dup(fileno(stdout))}, clargs.raw_tree)) { - Logger::Log(LogLevel::Error, "failed to dump artifact."); - return false; - } - } - - return true; -} -#endif - void PrintDoc(const nlohmann::json& doc, const std::string& indent) { if (not doc.is_array()) { return; |