summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/local/local_action.hpp
blob: 43569ea3c057e3fe6cdcb2400f7d3016b892c05b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_ACTION_HPP
#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_ACTION_HPP

#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp"
#include "src/buildtool/execution_api/common/execution_action.hpp"
#include "src/buildtool/execution_api/common/execution_response.hpp"
#include "src/buildtool/execution_api/local/config.hpp"
#include "src/buildtool/execution_api/local/local_storage.hpp"

class LocalApi;

/// \brief Action for local execution.
class LocalAction final : public IExecutionAction {
    friend class LocalApi;

  public:
    struct Output {
        bazel_re::ActionResult action{};
        bool is_cached{};
    };

    auto Execute(Logger const* logger) noexcept
        -> IExecutionResponse::Ptr final;

    void SetCacheFlag(CacheFlag flag) noexcept final { cache_flag_ = flag; }

    void SetTimeout(std::chrono::milliseconds timeout) noexcept final {
        timeout_ = timeout;
    }

  private:
    Logger logger_{"LocalExecution"};
    std::shared_ptr<LocalStorage> storage_;
    std::shared_ptr<LocalTreeMap> tree_map_;
    ArtifactDigest root_digest_{};
    std::vector<std::string> cmdline_{};
    std::vector<std::string> output_files_{};
    std::vector<std::string> output_dirs_{};
    std::map<std::string, std::string> env_vars_{};
    std::vector<bazel_re::Platform_Property> properties_;
    std::chrono::milliseconds timeout_{kDefaultTimeout};
    CacheFlag cache_flag_{CacheFlag::CacheOutput};

    LocalAction(std::shared_ptr<LocalStorage> storage,
                std::shared_ptr<LocalTreeMap> tree_map,
                ArtifactDigest 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> env_vars,
                std::map<std::string, std::string> const& properties) noexcept
        : storage_{std::move(storage)},
          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_{std::move(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());
    }

    [[nodiscard]] auto CreateActionDigest(bazel_re::Digest const& exec_dir,
                                          bool do_not_cache)
        -> bazel_re::Digest {
        return BazelMsgFactory::CreateActionDigestFromCommandLine(
            cmdline_,
            exec_dir,
            output_files_,
            output_dirs_,
            {} /*FIXME output node properties*/,
            BazelMsgFactory::CreateMessageVectorFromMap<
                bazel_re::Command_EnvironmentVariable>(env_vars_),
            properties_,
            do_not_cache,
            timeout_);
    }

    [[nodiscard]] auto Run(bazel_re::Digest const& action_id) const noexcept
        -> std::optional<Output>;

    [[nodiscard]] auto StageFile(std::filesystem::path const& target_path,
                                 Artifact::ObjectInfo const& info) const
        -> bool;

    /// \brief Stage input artifacts to the execution directory.
    /// Stage artifacts and their parent directory structure from CAS to the
    /// specified execution directory. The execution directory may no exist.
    /// \param[in] exec_path Absolute path to the execution directory.
    /// \returns Success indicator.
    [[nodiscard]] auto StageInputFiles(
        std::filesystem::path const& exec_path) const noexcept -> bool;

    [[nodiscard]] auto CreateDirectoryStructure(
        std::filesystem::path const& exec_path) const noexcept -> bool;

    [[nodiscard]] auto CollectOutputFile(std::filesystem::path const& exec_path,
                                         std::string const& local_path)
        const noexcept -> std::optional<bazel_re::OutputFile>;

    [[nodiscard]] auto CollectOutputDir(std::filesystem::path const& exec_path,
                                        std::string const& local_path)
        const noexcept -> std::optional<bazel_re::OutputDirectory>;

    [[nodiscard]] auto CollectAndStoreOutputs(
        bazel_re::ActionResult* result,
        std::filesystem::path const& exec_path) const noexcept -> bool;

    /// \brief Store file from path in file CAS and return pointer to digest.
    [[nodiscard]] auto DigestFromOwnedFile(
        std::filesystem::path const& file_path) const noexcept
        -> gsl::owner<bazel_re::Digest*>;
};

#endif  // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_ACTION_HPP