summaryrefslogtreecommitdiff
path: root/src/buildtool/computed_roots/analyse_and_build.cpp
blob: 4f9dd1bfde6d85e625646c5bf8d566904628744b (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
// Copyright 2024 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.

#include "src/buildtool/computed_roots/analyse_and_build.hpp"

#include <functional>
#include <map>
#include <utility>
#include <vector>

#include "src/buildtool/build_engine/target_map/result_map.hpp"
#include "src/buildtool/common/artifact_description.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/main/build_utils.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/buildtool/storage/storage.hpp"

[[nodiscard]] auto AnalyseAndBuild(
    gsl::not_null<AnalyseContext*> const& analyse_context,
    GraphTraverser const& traverser,
    BuildMaps::Target::ConfiguredTarget const& id,
    std::size_t jobs,
    gsl::not_null<ApiBundle const*> const& apis,
    Logger const* logger) -> std::optional<AnalyseAndBuildResult> {
    auto analysis_result = AnalyseTarget(analyse_context,
                                         id,
                                         jobs,
                                         /*request_action_input*/ std::nullopt,
                                         logger);

    if (not analysis_result) {
        if (logger != nullptr) {
            logger->Emit(LogLevel::Warning,
                         "Failed to analyse target {}",
                         id.ToString());
        }
        return std::nullopt;
    }
    if (logger != nullptr) {
        logger->Emit(LogLevel::Info, "Analysed target {}", id.ToString());
    }

    auto const [artifacts, runfiles] =
        ReadOutputArtifacts(analysis_result->target);

    auto [actions, blobs, trees, tree_overlays] =
        analysis_result->result_map.ToResult(
            analyse_context->statistics, analyse_context->progress, logger);

    auto const cache_targets = analysis_result->result_map.CacheTargets();

    // Clean up analyse_result map, now that it is no longer needed
    {
        TaskSystem ts{jobs};
        analysis_result->result_map.Clear(&ts);
    }
    auto extra_artifacts = CollectNonKnownArtifacts(cache_targets);
    for (auto const& [name, desc] : runfiles) {
        extra_artifacts.emplace_back(desc);
    }

    auto build_result = traverser.BuildAndStage(artifacts,
                                                {},
                                                std::move(actions),
                                                std::move(blobs),
                                                std::move(trees),
                                                std::move(tree_overlays),
                                                std::move(extra_artifacts));

    if (not build_result) {
        if (logger != nullptr) {
            logger->Emit(
                LogLevel::Warning, "Build for target {} failed", id.ToString());
        }
        return std::nullopt;
    }
    WriteTargetCacheEntries(cache_targets,
                            build_result->extra_infos,
                            jobs,
                            *apis,
                            TargetCacheWriteStrategy::Sync,
                            analyse_context->storage->TargetCache(),
                            logger);

    return AnalyseAndBuildResult{.analysis_result = *std::move(analysis_result),
                                 .build_result = *std::move(build_result)};
}