summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/remote/bazel/bazel_action.cpp
blob: 34fc538099b1346bf90475c98a5036231f438b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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));
                           }));
}