summaryrefslogtreecommitdiff
path: root/src/buildtool/computed_roots/lookup_cache.cpp
blob: ac3eeaefcf228d803e5e33f0d3774cf95e54fe95 (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
// 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/lookup_cache.hpp"

#include <functional>
#include <memory>
#include <vector>

#include "fmt/core.h"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/base_maps/entity_name_data.hpp"
#include "src/buildtool/build_engine/base_maps/field_reader.hpp"
#include "src/buildtool/build_engine/base_maps/module_name.hpp"
#include "src/buildtool/build_engine/base_maps/targets_file_map.hpp"
#include "src/buildtool/build_engine/expression/configuration.hpp"
#include "src/buildtool/build_engine/expression/target_result.hpp"
#include "src/buildtool/computed_roots/artifacts_root.hpp"
#include "src/buildtool/file_system/file_root.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/buildtool/storage/target_cache_entry.hpp"
#include "src/buildtool/storage/target_cache_key.hpp"

auto LookupCache(BuildMaps::Target::ConfiguredTarget const& ctarget,
                 gsl::not_null<RepositoryConfig*> const& repository_config,
                 Storage const& storage,
                 AsyncMapConsumerLoggerPtr const& logger,
                 std::optional<RehashUtils::Rehasher> const& rehash)
    -> expected<std::optional<std::string>, std::monostate> {
    auto const* target_root =
        repository_config->TargetRoot(ctarget.target.ToModule().repository);
    if ((target_root == nullptr) or target_root->IsAbsent()) {
        return expected<std::optional<std::string>, std::monostate>(
            std::nullopt);
    }
    auto repo_key = repository_config->RepositoryKey(
        storage, ctarget.target.GetNamedTarget().repository);
    if (not repo_key) {
        (*logger)(fmt::format("Repository {} is not content-fixed",
                              nlohmann::json(
                                  ctarget.target.GetNamedTarget().repository)),
                  /*fatal=*/true);
        return unexpected(std::monostate{});
    }
    auto targets_file_map =
        BuildMaps::Base::CreateTargetsFileMap(repository_config, 1);
    nlohmann::json targets_file{};
    bool failed{false};
    {
        TaskSystem ts{1};
        targets_file_map.ConsumeAfterKeysReady(
            &ts,
            {ctarget.target.ToModule()},
            [&targets_file](auto values) { targets_file = *values[0]; },
            [&logger, &failed](auto const& msg, bool fatal) {
                (*logger)(
                    fmt::format("While searching for target description:\n{}",
                                msg),
                    fatal);
                failed = failed or fatal;
            });
    }
    if (failed) {
        return unexpected(std::monostate{});
    }
    auto desc_it = targets_file.find(ctarget.target.GetNamedTarget().name);
    if (desc_it == targets_file.end()) {
        (*logger)("Not referring to a defined target", /*fatal=*/true);
        return unexpected(std::monostate{});
    }
    nlohmann::json const& desc = *desc_it;
    auto rule_it = desc.find("type");
    if (rule_it == desc.end()) {
        (*logger)(fmt::format("No type specified in target-description {}",
                              desc.dump()),
                  true);
        return unexpected(std::monostate{});
    }
    auto const& rule = *rule_it;
    if (not(rule.is_string() and rule.get<std::string>() == "export")) {
        (*logger)(fmt::format("Target not an export target, but of type {}",
                              rule.dump()),
                  true);
        return unexpected(std::monostate{});
    }
    auto reader = BuildMaps::Base::FieldReader::CreatePtr(
        desc, ctarget.target, "export target", logger);
    auto flexible_vars = reader->ReadStringList("flexible_config");
    if (not flexible_vars) {
        return unexpected(std::monostate{});
    }
    auto effective_config = ctarget.config.Prune(*flexible_vars);
    auto cache_key = storage.TargetCache().ComputeKey(
        *repo_key, ctarget.target.GetNamedTarget(), effective_config);
    if (not cache_key) {
        (*logger)("Target-cache key generation failed", true);
        return unexpected(std::monostate{});
    }
    auto target_cache_value = storage.TargetCache().Read(*cache_key);
    if (not target_cache_value) {
        return expected<std::optional<std::string>, std::monostate>(
            std::nullopt);
    }
    auto const& [entry, info] = *target_cache_value;
    auto result = entry.ToResult();
    if (not result) {
        (*logger)(fmt::format("Failed to deserialize cache entry {} for key {}",
                              info.ToString(),
                              cache_key->Id().ToString()),
                  true);
        return unexpected(std::monostate{});
    }

    auto wrapped_logger = std::make_shared<AsyncMapConsumerLogger>(
        [&logger](auto const& msg, bool fatal) {
            (*logger)(
                fmt::format("While computing git tree for artifacts stage:\n{}",
                            msg),
                fatal);
        });
    auto root = ArtifactsRoot(result->artifact_stage, wrapped_logger, rehash);
    if (not root) {
        return unexpected(std::monostate{});
    }
    return root;
}