summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/remote/bazel/bazel_action.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildtool/execution_api/remote/bazel/bazel_action.cpp')
-rw-r--r--src/buildtool/execution_api/remote/bazel/bazel_action.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/buildtool/execution_api/remote/bazel/bazel_action.cpp b/src/buildtool/execution_api/remote/bazel/bazel_action.cpp
new file mode 100644
index 00000000..34fc5380
--- /dev/null
+++ b/src/buildtool/execution_api/remote/bazel/bazel_action.cpp
@@ -0,0 +1,94 @@
+#include "src/buildtool/execution_api/remote/bazel/bazel_action.hpp"
+
+#include "src/buildtool/execution_api/bazel_msg/bazel_blob_container.hpp"
+#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp"
+#include "src/buildtool/execution_api/remote/bazel/bazel_response.hpp"
+
+BazelAction::BazelAction(
+ std::shared_ptr<BazelNetwork> network,
+ std::shared_ptr<LocalTreeMap> tree_map,
+ bazel_re::Digest root_digest,
+ std::vector<std::string> command,
+ std::vector<std::string> output_files,
+ std::vector<std::string> output_dirs,
+ std::map<std::string, std::string> const& env_vars,
+ std::map<std::string, std::string> const& properties) noexcept
+ : network_{std::move(network)},
+ tree_map_{std::move(tree_map)},
+ root_digest_{std::move(root_digest)},
+ cmdline_{std::move(command)},
+ output_files_{std::move(output_files)},
+ output_dirs_{std::move(output_dirs)},
+ env_vars_{BazelMsgFactory::CreateMessageVectorFromMap<
+ bazel_re::Command_EnvironmentVariable>(env_vars)},
+ properties_{BazelMsgFactory::CreateMessageVectorFromMap<
+ bazel_re::Platform_Property>(properties)} {
+ std::sort(output_files_.begin(), output_files_.end());
+ std::sort(output_dirs_.begin(), output_dirs_.end());
+}
+
+auto BazelAction::Execute(Logger const* logger) noexcept
+ -> IExecutionResponse::Ptr {
+ BlobContainer blobs{};
+ auto do_cache = CacheEnabled(cache_flag_);
+ auto action = CreateBundlesForAction(&blobs, root_digest_, not do_cache);
+
+ if (logger != nullptr) {
+ logger->Emit(LogLevel::Trace,
+ "start execution\n"
+ " - exec_dir digest: {}\n"
+ " - action digest: {}",
+ root_digest_.hash(),
+ action.hash());
+ }
+
+ if (do_cache) {
+ if (auto result =
+ network_->GetCachedActionResult(action, output_files_)) {
+ if (result->exit_code() == 0) {
+ return IExecutionResponse::Ptr{new BazelResponse{
+ action.hash(), network_, tree_map_, {*result, true}}};
+ }
+ }
+ }
+
+ if (ExecutionEnabled(cache_flag_) and network_->UploadBlobs(blobs)) {
+ if (auto output = network_->ExecuteBazelActionSync(action)) {
+ if (cache_flag_ == CacheFlag::PretendCached) {
+ // ensure the same id is created as if caching were enabled
+ auto action_id =
+ CreateBundlesForAction(nullptr, root_digest_, false).hash();
+ output->cached_result = true;
+ return IExecutionResponse::Ptr{
+ new BazelResponse{std::move(action_id),
+ network_,
+ tree_map_,
+ std::move(*output)}};
+ }
+ return IExecutionResponse::Ptr{new BazelResponse{
+ action.hash(), network_, tree_map_, std::move(*output)}};
+ }
+ }
+
+ return nullptr;
+}
+
+auto BazelAction::CreateBundlesForAction(BlobContainer* blobs,
+ bazel_re::Digest const& exec_dir,
+ bool do_not_cache) const noexcept
+ -> bazel_re::Digest {
+ return BazelMsgFactory::CreateActionDigestFromCommandLine(
+ cmdline_,
+ exec_dir,
+ output_files_,
+ output_dirs_,
+ {} /*FIXME output node properties*/,
+ env_vars_,
+ properties_,
+ do_not_cache,
+ timeout_,
+ blobs == nullptr ? std::nullopt
+ : std::make_optional([&blobs](BazelBlob&& blob) {
+ blobs->Emplace(std::move(blob));
+ }));
+}