summaryrefslogtreecommitdiff
path: root/src/buildtool/graph_traverser/graph_traverser.hpp
blob: e2587701fdf7d0421f86a6d86e53e31e5ac8d3cb (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef INCLUDED_SRC_BUILDTOOL_GRAPH_TRAVERSER_GRAPH_TRAVERSER_HPP
#define INCLUDED_SRC_BUILDTOOL_GRAPH_TRAVERSER_GRAPH_TRAVERSER_HPP

#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <functional>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>

#include "fmt/core.h"
#include "gsl/gsl"
#include "src/buildtool/common/cli.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/common/tree.hpp"
#include "src/buildtool/execution_api/bazel_msg/bazel_blob_container.hpp"
#include "src/buildtool/execution_api/local/local_api.hpp"
#include "src/buildtool/execution_api/remote/bazel/bazel_api.hpp"
#include "src/buildtool/execution_api/remote/config.hpp"
#include "src/buildtool/execution_api/utils/subobject.hpp"
#include "src/buildtool/execution_engine/dag/dag.hpp"
#include "src/buildtool/execution_engine/executor/executor.hpp"
#include "src/buildtool/execution_engine/traverser/traverser.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/file_system/jsonfs.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/logging/log_sink_cmdline.hpp"
#include "src/buildtool/logging/log_sink_file.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/progress_reporting/base_progress_reporter.hpp"
#include "src/utils/cpp/json.hpp"

class GraphTraverser {
  public:
    struct CommandLineArguments {
        std::size_t jobs;
        BuildArguments build;
        std::optional<StageArguments> stage;
        std::optional<RebuildArguments> rebuild;
    };

    struct BuildResult {
        std::vector<std::filesystem::path> output_paths;
        // Object infos of extra artifacts requested to build.
        std::unordered_map<ArtifactDescription, Artifact::ObjectInfo>
            extra_infos;
        bool failed_artifacts;
    };

    explicit GraphTraverser(CommandLineArguments clargs)
        : clargs_{std::move(clargs)},
          local_api_{CreateExecutionApi(std::nullopt)},
          remote_api_{
              CreateExecutionApi(RemoteExecutionConfig::RemoteAddress())},
          reporter_{[](auto done, auto cv) {}} {}

    explicit GraphTraverser(CommandLineArguments clargs,
                            progress_reporter_t reporter)
        : clargs_{std::move(clargs)},
          local_api_{CreateExecutionApi(std::nullopt)},
          remote_api_{
              CreateExecutionApi(RemoteExecutionConfig::RemoteAddress())},
          reporter_{std::move(reporter)} {}

    /// \brief Parses actions and blobs into graph, traverses it and retrieves
    /// outputs specified by command line arguments.
    /// \param artifact_descriptions Artifacts to build (and stage).
    /// \param runfile_descriptions  Runfiles to build (and stage).
    /// \param action_descriptions   All required actions for building.
    /// \param blobs                 Blob artifacts to upload before the build.
    /// \param trees                 Tree artifacts to compute graph nodes from.
    /// \param extra_artifacts       Extra artifacts to obtain object infos for.
    [[nodiscard]] auto BuildAndStage(
        std::map<std::string, ArtifactDescription> const& artifact_descriptions,
        std::map<std::string, ArtifactDescription> const& runfile_descriptions,
        std::vector<ActionDescription::Ptr> const& action_descriptions,
        std::vector<std::string> const& blobs,
        std::vector<Tree::Ptr> const& trees,
        std::vector<ArtifactDescription>&& extra_artifacts = {}) const
        -> std::optional<BuildResult> {
        DependencyGraph graph;  // must outlive artifact_nodes
        auto artifacts = BuildArtifacts(&graph,
                                        artifact_descriptions,
                                        runfile_descriptions,
                                        action_descriptions,
                                        trees,
                                        blobs,
                                        extra_artifacts);
        if (not artifacts) {
            return std::nullopt;
        }
        auto const [rel_paths, artifact_nodes, extra_nodes] = *artifacts;

        auto const object_infos = CollectObjectInfos(artifact_nodes);
        auto extra_infos = CollectObjectInfos(extra_nodes);
        if (not object_infos or not extra_infos) {
            return std::nullopt;
        }

        Expects(extra_artifacts.size() == extra_infos->size());
        std::unordered_map<ArtifactDescription, Artifact::ObjectInfo> infos;
        infos.reserve(extra_infos->size());
        std::transform(
            std::make_move_iterator(extra_artifacts.begin()),
            std::make_move_iterator(extra_artifacts.end()),
            std::make_move_iterator(extra_infos->begin()),
            std::inserter(infos, infos.end()),
            std::make_pair<ArtifactDescription&&, Artifact::ObjectInfo&&>);

        bool failed_artifacts = std::any_of(
            object_infos->begin(), object_infos->end(), [](auto const& info) {
                return info.failed;
            });

        if (not clargs_.stage) {
            PrintOutputs("Artifacts built, logical paths are:",
                         rel_paths,
                         artifact_nodes,
                         runfile_descriptions);
            MaybePrintToStdout(rel_paths, artifact_nodes);
            return BuildResult{
                .output_paths = std::move(std::get<0>(*artifacts)),
                .extra_infos = std::move(infos),
                .failed_artifacts = failed_artifacts};
        }

        if (clargs_.stage->remember) {
            if (not remote_api_->RetrieveToCas(*object_infos, GetLocalApi())) {
                Logger::Log(LogLevel::Warning, "Failed to copy objects to CAS");
            }
        }

        auto output_paths = RetrieveOutputs(rel_paths, *object_infos);
        if (not output_paths) {
            return std::nullopt;
        }
        PrintOutputs("Artifacts can be found in:",
                     *output_paths,
                     artifact_nodes,
                     runfile_descriptions);

        MaybePrintToStdout(rel_paths, artifact_nodes);

        return BuildResult{.output_paths = *output_paths,
                           .extra_infos = std::move(infos),
                           .failed_artifacts = failed_artifacts};
    }

    /// \brief Parses graph description into graph, traverses it and retrieves
    /// outputs specified by command line arguments
    [[nodiscard]] auto BuildAndStage(
        std::filesystem::path const& graph_description,
        nlohmann::json const& artifacts) const -> std::optional<BuildResult> {
        // Read blobs to upload and actions from graph description file
        auto desc = ReadGraphDescription(graph_description);
        if (not desc) {
            return std::nullopt;
        }
        auto const [blobs, tree_descs, actions] = *desc;

        std::vector<ActionDescription::Ptr> action_descriptions{};
        action_descriptions.reserve(actions.size());
        for (auto const& [id, description] : actions.items()) {
            auto action = ActionDescription::FromJson(id, description);
            if (not action) {
                return std::nullopt;  // Error already logged
            }
            action_descriptions.emplace_back(std::move(*action));
        }

        std::vector<Tree::Ptr> trees{};
        for (auto const& [id, description] : tree_descs.items()) {
            auto tree = Tree::FromJson(id, description);
            if (not tree) {
                return std::nullopt;
            }
            trees.emplace_back(std::move(*tree));
        }

        std::map<std::string, ArtifactDescription> artifact_descriptions{};
        for (auto const& [rel_path, description] : artifacts.items()) {
            auto artifact = ArtifactDescription::FromJson(description);
            if (not artifact) {
                return std::nullopt;  // Error already logged
            }
            artifact_descriptions.emplace(rel_path, std::move(*artifact));
        }

        return BuildAndStage(
            artifact_descriptions, {}, action_descriptions, blobs, trees);
    }

    [[nodiscard]] auto GetLocalApi() const -> gsl::not_null<IExecutionApi*> {
        return &(*local_api_);
    }

    [[nodiscard]] auto GetRemoteApi() const -> gsl::not_null<IExecutionApi*> {
        return &(*remote_api_);
    }

  private:
    CommandLineArguments const clargs_;
    gsl::not_null<IExecutionApi::Ptr> const local_api_;
    gsl::not_null<IExecutionApi::Ptr> const remote_api_;
    progress_reporter_t reporter_;

    /// \brief Reads contents of graph description file as json object. In case
    /// the description is missing "blobs" or "actions" key/value pairs or they
    /// can't be retrieved with the appropriate types, execution is terminated
    /// after logging error
    /// \returns A pair containing the blobs to upload (as a vector of strings)
    /// and the actions as a json object.
    [[nodiscard]] static auto ReadGraphDescription(
        std::filesystem::path const& graph_description)
        -> std::optional<
            std::tuple<nlohmann::json, nlohmann::json, nlohmann::json>> {
        auto const graph_description_opt = Json::ReadFile(graph_description);
        if (not graph_description_opt.has_value()) {
            Logger::Log(LogLevel::Error,
                        "parsing graph from {}",
                        graph_description.string());
            return std::nullopt;
        }
        auto blobs_opt = ExtractValueAs<std::vector<std::string>>(
            *graph_description_opt, "blobs", [](std::string const& s) {
                Logger::Log(LogLevel::Error,
                            "{}\ncan not retrieve value for \"blobs\" from "
                            "graph description.",
                            s);
            });
        auto trees_opt = ExtractValueAs<nlohmann::json>(
            *graph_description_opt, "trees", [](std::string const& s) {
                Logger::Log(LogLevel::Error,
                            "{}\ncan not retrieve value for \"trees\" from "
                            "graph description.",
                            s);
            });
        auto actions_opt = ExtractValueAs<nlohmann::json>(
            *graph_description_opt, "actions", [](std::string const& s) {
                Logger::Log(LogLevel::Error,
                            "{}\ncan not retrieve value for \"actions\" from "
                            "graph description.",
                            s);
            });
        if (not blobs_opt or not trees_opt or not actions_opt) {
            return std::nullopt;
        }
        return std::make_tuple(std::move(*blobs_opt),
                               std::move(*trees_opt),
                               std::move(*actions_opt));
    }

    [[nodiscard]] static auto CreateExecutionApi(
        std::optional<RemoteExecutionConfig::ServerAddress> const& address)
        -> gsl::not_null<IExecutionApi::Ptr> {
        if (address) {
            ExecutionConfiguration config;
            config.skip_cache_lookup = false;

            return std::make_unique<BazelApi>(
                "remote-execution", address->host, address->port, config);
        }
        return std::make_unique<LocalApi>();
    }

    /// \brief Requires for the executor to upload blobs to CAS. In the case any
    /// of the uploads fails, execution is terminated
    /// \param[in]  blobs   blobs to be uploaded
    [[nodiscard]] auto UploadBlobs(
        std::vector<std::string> const& blobs) const noexcept -> bool {
        BlobContainer container;
        for (auto const& blob : blobs) {
            auto digest = ArtifactDigest::Create<ObjectType::File>(blob);
            Logger::Log(LogLevel::Trace, [&]() {
                return fmt::format(
                    "Uploaded blob {}, its digest has id {} and size {}.",
                    nlohmann::json(blob).dump(),
                    digest.hash(),
                    digest.size());
            });
            try {
                container.Emplace(
                    BazelBlob{std::move(digest), blob, /*is_exec=*/false});
            } catch (std::exception const& ex) {
                Logger::Log(
                    LogLevel::Error, "failed to create blob with: ", ex.what());
                return false;
            }
        }
        return remote_api_->Upload(container);
    }

    /// \brief Adds the artifacts to be retrieved to the graph
    /// \param[in]  g   dependency graph
    /// \param[in]  artifacts   output artifact map
    /// \param[in]  runfiles    output runfile map
    /// \returns    pair of vectors where the first vector contains the absolute
    /// paths to which the artifacts will be retrieved and the second one
    /// contains the ids of the artifacts to be retrieved
    [[nodiscard]] static auto AddArtifactsToRetrieve(
        gsl::not_null<DependencyGraph*> const& g,
        std::map<std::string, ArtifactDescription> const& artifacts,
        std::map<std::string, ArtifactDescription> const& runfiles)
        -> std::optional<std::pair<std::vector<std::filesystem::path>,
                                   std::vector<ArtifactIdentifier>>> {
        std::vector<std::filesystem::path> rel_paths;
        std::vector<ArtifactIdentifier> ids;
        auto total_size = artifacts.size() + runfiles.size();
        rel_paths.reserve(total_size);
        ids.reserve(total_size);
        auto add_and_get_info =
            [&g, &rel_paths, &ids](
                std::map<std::string, ArtifactDescription> const& descriptions)
            -> bool {
            for (auto const& [rel_path, artifact] : descriptions) {
                rel_paths.emplace_back(rel_path);
                ids.emplace_back(g->AddArtifact(artifact));
            }
            return true;
        };
        if (add_and_get_info(artifacts) and add_and_get_info(runfiles)) {
            return std::make_pair(std::move(rel_paths), std::move(ids));
        }
        return std::nullopt;
    }

    /// \brief Traverses the graph. In case any of the artifact ids
    /// specified by the command line arguments is duplicated, execution is
    /// terminated.
    [[nodiscard]] auto Traverse(
        DependencyGraph const& g,
        std::vector<ArtifactIdentifier> const& artifact_ids) const -> bool {
        Executor executor{&(*local_api_),
                          &(*remote_api_),
                          RemoteExecutionConfig::PlatformProperties(),
                          clargs_.build.timeout};
        bool traversing{};
        std::atomic<bool> done = false;
        std::atomic<bool> failed = false;
        std::condition_variable cv{};
        auto observer =
            std::thread([this, &done, &cv]() { reporter_(&done, &cv); });
        {
            Traverser t{executor, g, clargs_.jobs, &failed};
            traversing =
                t.Traverse({std::begin(artifact_ids), std::end(artifact_ids)});
        }
        done = true;
        cv.notify_all();
        observer.join();
        return traversing and not failed;
    }

    [[nodiscard]] auto TraverseRebuild(
        DependencyGraph const& g,
        std::vector<ArtifactIdentifier> const& artifact_ids) const -> bool {
        // setup rebuilder with api for cache endpoint
        auto api_cached =
            CreateExecutionApi(RemoteExecutionConfig::CacheAddress());
        Rebuilder executor{&(*local_api_),
                           &(*remote_api_),
                           &(*api_cached),
                           RemoteExecutionConfig::PlatformProperties(),
                           clargs_.build.timeout};
        bool traversing{false};
        std::atomic<bool> done = false;
        std::atomic<bool> failed = false;
        std::condition_variable cv{};
        auto observer =
            std::thread([this, &done, &cv]() { reporter_(&done, &cv); });
        {
            Traverser t{executor, g, clargs_.jobs, &failed};
            traversing =
                t.Traverse({std::begin(artifact_ids), std::end(artifact_ids)});
        }
        done = true;
        cv.notify_all();
        observer.join();

        if (traversing and not failed and clargs_.rebuild->dump_flaky) {
            std::ofstream file{*clargs_.rebuild->dump_flaky};
            file << executor.DumpFlakyActions().dump(2);
        }
        return traversing and not failed;
    }

    /// \brief Retrieves nodes corresponding to artifacts with ids in artifacts.
    /// In case any of the identifiers doesn't correspond to a node inside the
    /// graph, we write out error message and stop execution with failure code
    [[nodiscard]] static auto GetArtifactNodes(
        DependencyGraph const& g,
        std::vector<ArtifactIdentifier> const& artifact_ids) noexcept
        -> std::optional<std::vector<DependencyGraph::ArtifactNode const*>> {
        std::vector<DependencyGraph::ArtifactNode const*> nodes{};

        for (auto const& art_id : artifact_ids) {
            auto const* node = g.ArtifactNodeWithId(art_id);
            if (node == nullptr) {
                Logger::Log(
                    LogLevel::Error, "Artifact {} not found in graph.", art_id);
                return std::nullopt;
            }
            nodes.push_back(node);
        }
        return nodes;
    }

    void LogStatistics() const noexcept {
        auto const& stats = Statistics::Instance();
        if (clargs_.rebuild) {
            std::stringstream ss{};
            ss << stats.RebuiltActionComparedCounter()
               << " actions compared with cache";
            if (stats.ActionsFlakyCounter() > 0) {
                ss << ", " << stats.ActionsFlakyCounter()
                   << " flaky actions found";
                ss << " (" << stats.ActionsFlakyTaintedCounter()
                   << " of which tainted)";
            }
            if (stats.RebuiltActionMissingCounter() > 0) {
                ss << ", no cache entry found for "
                   << stats.RebuiltActionMissingCounter() << " actions";
            }
            ss << ".";
            Logger::Log(LogLevel::Info, ss.str());
        }
        else {
            Logger::Log(LogLevel::Info,
                        "Processed {} actions, {} cache hits.",
                        stats.ActionsQueuedCounter(),
                        stats.ActionsCachedCounter());
        }
    }

    [[nodiscard]] auto BuildArtifacts(
        gsl::not_null<DependencyGraph*> const& graph,
        std::map<std::string, ArtifactDescription> const& artifacts,
        std::map<std::string, ArtifactDescription> const& runfiles,
        std::vector<ActionDescription::Ptr> const& actions,
        std::vector<Tree::Ptr> const& trees,
        std::vector<std::string> const& blobs,
        std::vector<ArtifactDescription> const& extra_artifacts = {}) const
        -> std::optional<
            std::tuple<std::vector<std::filesystem::path>,
                       std::vector<DependencyGraph::ArtifactNode const*>,
                       std::vector<DependencyGraph::ArtifactNode const*>>> {
        if (not UploadBlobs(blobs)) {
            return std::nullopt;
        }

        auto artifact_infos =
            AddArtifactsToRetrieve(graph, artifacts, runfiles);
        if (not artifact_infos) {
            return std::nullopt;
        }
        auto& [output_paths, artifact_ids] = *artifact_infos;

        // Add extra artifacts to ids to build
        artifact_ids.reserve(artifact_ids.size() + extra_artifacts.size());
        for (auto const& artifact : extra_artifacts) {
            artifact_ids.emplace_back(graph->AddArtifact(artifact));
        }

        std::vector<ActionDescription> tree_actions{};
        tree_actions.reserve(trees.size());
        for (auto const& tree : trees) {
            tree_actions.emplace_back(tree->Action());
        }

        if (not graph->Add(actions) or not graph->Add(tree_actions)) {
            Logger::Log(LogLevel::Error, [&actions]() {
                auto json = nlohmann::json::array();
                for (auto const& desc : actions) {
                    json.push_back(desc->ToJson());
                }
                return fmt::format(
                    "could not build the dependency graph from the actions "
                    "described in {}.",
                    json.dump());
            });
            return std::nullopt;
        }

        if (clargs_.rebuild ? not TraverseRebuild(*graph, artifact_ids)
                            : not Traverse(*graph, artifact_ids)) {
            Logger::Log(LogLevel::Error, "Build failed.");
            return std::nullopt;
        }

        LogStatistics();

        auto artifact_nodes = GetArtifactNodes(*graph, artifact_ids);
        if (not artifact_nodes) {
            return std::nullopt;
        }

        // split extra artifacts' nodes from artifact nodes
        auto extra_nodes = std::vector<DependencyGraph::ArtifactNode const*>{
            std::make_move_iterator(artifact_nodes->begin() +
                                    output_paths.size()),
            std::make_move_iterator(artifact_nodes->end())};
        artifact_nodes->erase(artifact_nodes->begin() + output_paths.size(),
                              artifact_nodes->end());

        return std::make_tuple(std::move(output_paths),
                               std::move(*artifact_nodes),
                               std::move(extra_nodes));
    }

    [[nodiscard]] auto PrepareOutputPaths(
        std::vector<std::filesystem::path> const& rel_paths) const
        -> std::optional<std::vector<std::filesystem::path>> {
        std::vector<std::filesystem::path> output_paths{};
        output_paths.reserve(rel_paths.size());
        for (auto const& rel_path : rel_paths) {
            output_paths.emplace_back(clargs_.stage->output_dir / rel_path);
        }
        return output_paths;
    }

    [[nodiscard]] static auto CollectObjectInfos(
        std::vector<DependencyGraph::ArtifactNode const*> const& artifact_nodes)
        -> std::optional<std::vector<Artifact::ObjectInfo>> {
        std::vector<Artifact::ObjectInfo> object_infos;
        object_infos.reserve(artifact_nodes.size());
        for (auto const* art_ptr : artifact_nodes) {
            auto const& info = art_ptr->Content().Info();
            if (info) {
                object_infos.push_back(*info);
            }
            else {
                Logger::Log(LogLevel::Error,
                            "artifact {} could not be retrieved, it can not be "
                            "found in CAS.",
                            art_ptr->Content().Id());
                return std::nullopt;
            }
        }
        return object_infos;
    }

    /// \brief Asks execution API to copy output artifacts to paths specified by
    /// command line arguments and writes location info. In case the executor
    /// couldn't retrieve any of the outputs, execution is terminated.
    [[nodiscard]] auto RetrieveOutputs(
        std::vector<std::filesystem::path> const& rel_paths,
        std::vector<Artifact::ObjectInfo> const& object_infos) const
        -> std::optional<std::vector<std::filesystem::path>> {
        // Create output directory
        if (not FileSystemManager::CreateDirectory(clargs_.stage->output_dir)) {
            return std::nullopt;  // Message logged in the file system manager
        }

        auto output_paths = PrepareOutputPaths(rel_paths);

        if (not output_paths or
            not remote_api_->RetrieveToPaths(
                object_infos, *output_paths, GetLocalApi())) {
            Logger::Log(LogLevel::Error, "Could not retrieve outputs.");
            return std::nullopt;
        }

        return std::move(*output_paths);
    }

    void PrintOutputs(
        std::string message,
        std::vector<std::filesystem::path> const& paths,
        std::vector<DependencyGraph::ArtifactNode const*> const& artifact_nodes,
        std::map<std::string, ArtifactDescription> const& runfiles) const {
        std::string msg_dbg{"Artifact ids:"};
        std::string msg_failed{"Failed artifacts:"};
        bool failed{false};
        nlohmann::json json{};
        for (std::size_t pos = 0; pos < paths.size(); ++pos) {
            auto path = paths[pos].string();
            auto id = IdentifierToString(artifact_nodes[pos]->Content().Id());
            if (clargs_.build.show_runfiles or
                not runfiles.contains(clargs_.stage
                                          ? std::filesystem::proximate(
                                                path, clargs_.stage->output_dir)
                                                .string()
                                          : path)) {
                auto info = artifact_nodes[pos]->Content().Info();
                if (info) {
                    message += fmt::format("\n  {} {}", path, info->ToString());
                    if (info->failed) {
                        msg_failed +=
                            fmt::format("\n  {} {}", path, info->ToString());
                        failed = true;
                    }
                    if (clargs_.build.dump_artifacts) {
                        json[path] = info->ToJson();
                    }
                }
                else {
                    Logger::Log(
                        LogLevel::Error, "Missing info for artifact {}.", id);
                }
            }
            msg_dbg += fmt::format("\n  {}: {}", path, id);
        }

        if (not clargs_.build.show_runfiles and !runfiles.empty()) {
            message += fmt::format("\n({} runfiles omitted.)", runfiles.size());
        }

        Logger::Log(LogLevel::Info, "{}", message);
        Logger::Log(LogLevel::Debug, "{}", msg_dbg);
        if (failed) {
            Logger::Log(LogLevel::Info, "{}", msg_failed);
        }

        if (clargs_.build.dump_artifacts) {
            if (*clargs_.build.dump_artifacts == "-") {
                std::cout << std::setw(2) << json << std::endl;
            }
            else {
                std::ofstream os(*clargs_.build.dump_artifacts);
                os << std::setw(2) << json << std::endl;
            }
        }
    }

    void MaybePrintToStdout(
        std::vector<std::filesystem::path> const& paths,
        std::vector<DependencyGraph::ArtifactNode const*> const& artifacts)
        const {
        if (clargs_.build.print_to_stdout) {
            for (size_t i = 0; i < paths.size(); i++) {
                if (paths[i] == *(clargs_.build.print_to_stdout)) {
                    auto info = artifacts[i]->Content().Info();
                    if (info) {
                        if (not remote_api_->RetrieveToFds(
                                {*info},
                                {dup(fileno(stdout))},
                                /*raw_tree=*/false)) {
                            Logger::Log(LogLevel::Error,
                                        "Failed to retrieve {}",
                                        *(clargs_.build.print_to_stdout));
                        }
                    }
                    else {
                        Logger::Log(
                            LogLevel::Error,
                            "Failed to obtain object information for {}",
                            *(clargs_.build.print_to_stdout));
                    }
                    return;
                }
            }
            // Not directly an artifact, hence check if the path is contained in
            // some artifact
            auto target_path = ToNormalPath(std::filesystem::path{
                                                *clargs_.build.print_to_stdout})
                                   .relative_path();
            auto remote = GetRemoteApi();
            for (size_t i = 0; i < paths.size(); i++) {
                auto const& path = paths[i];
                auto relpath = target_path.lexically_relative(path);
                if ((not relpath.empty()) and *relpath.begin() != "..") {
                    Logger::Log(
                        LogLevel::Info,
                        "'{}' not a direct logical path of the specified "
                        "target; will take subobject '{}' of '{}'",
                        *(clargs_.build.print_to_stdout),
                        relpath.string(),
                        path.string());
                    auto info = artifacts[i]->Content().Info();
                    if (info) {
                        auto new_info =
                            RetrieveSubPathId(*info, remote, relpath);
                        if (new_info) {
                            if (not remote_api_->RetrieveToFds(
                                    {*new_info},
                                    {dup(fileno(stdout))},
                                    /*raw_tree=*/false)) {
                                Logger::Log(LogLevel::Error,
                                            "Failed to retrieve artifact {} at "
                                            "path '{}' of '{}'",
                                            new_info->ToString(),
                                            relpath.string(),
                                            path.string());
                            }
                        }
                    }
                    else {
                        Logger::Log(
                            LogLevel::Error,
                            "Failed to obtain object information for {}",
                            *(clargs_.build.print_to_stdout));
                    }
                    return;
                }
            }
            Logger::Log(LogLevel::Warning,
                        "{} not a logical path of the specified target",
                        *(clargs_.build.print_to_stdout));
        }
    }
};

#endif  // INCLUDED_SRC_BUILDTOOL_GRAPH_TRAVERSER_GRAPH_TRAVERSER_HPP