summaryrefslogtreecommitdiff
path: root/src/buildtool/build_engine/base_maps/directory_map.cpp
blob: 522b0f7dc5dc3d2a2a2bb5fabd6ac06f8867d925 (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
// 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.

#include "src/buildtool/build_engine/base_maps/directory_map.hpp"

#include <functional>
#include <string>

#include "fmt/core.h"
#include "src/buildtool/multithreading/async_map_consumer.hpp"

auto BuildMaps::Base::CreateDirectoryEntriesMap(
    gsl::not_null<const RepositoryConfig*> const& repo_config,
    std::size_t jobs) -> DirectoryEntriesMap {
    auto directory_reader = [repo_config](auto /* unused*/,
                                          auto setter,
                                          auto logger,
                                          auto /* unused */,
                                          auto const& key) {
        auto const* ws_root = repo_config->WorkspaceRoot(key.repository);
        if (ws_root == nullptr) {
            (*logger)(
                fmt::format("Cannot determine workspace root for repository {}",
                            key.repository),
                /*fatal=*/true);
            return;
        }
        if (ws_root->IsAbsent()) {
            std::string missing_root = "[unknown]";
            auto absent_tree = ws_root->GetAbsentTreeId();
            if (absent_tree) {
                missing_root = *absent_tree;
            }
            (*logger)(fmt::format("Would have to read directory entries of "
                                  "absent root {}",
                                  missing_root),
                      /*fatal=*/true);
            return;
        }
        auto dir_path = key.module.empty() ? "." : key.module;
        if (not ws_root->IsDirectory(dir_path)) {
            // Missing directory is fine (source tree might be incomplete),
            // contains no entries.
            (*setter)(FileRoot::DirectoryEntries{
                FileRoot::DirectoryEntries::pairs_t{}});
            return;
        }
        if (auto read_dir = ws_root->ReadDirectory(dir_path)) {
            (*setter)(*std::move(read_dir));
            return;
        }
        (*logger)(fmt::format("Failed to read directory {} from repository {}",
                              dir_path,
                              key.repository),
                  /*fatal=*/true);
    };
    return AsyncMapConsumer<BuildMaps::Base::ModuleName,
                            FileRoot::DirectoryEntries>{directory_reader, jobs};
}