aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg_Dependencies.cpp
blob: 3b6db5bbdc35fb378c256352490abf604bac993f (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
#include "pch.h"
#include "vcpkg_Dependencies.h"
#include "vcpkg_Graphs.h"
#include "vcpkg_paths.h"
#include "package_spec.h"
#include "StatusParagraphs.h"
#include "vcpkg_Files.h"
#include "vcpkglib.h"

namespace vcpkg::Dependencies
{
    std::vector<package_spec_with_install_plan> create_install_plan(const vcpkg_paths& paths, const std::vector<package_spec>& specs, const StatusParagraphs& status_db)
    {
        std::unordered_map<package_spec, install_plan_action> was_examined; // Examine = we have checked its immediate (non-recursive) dependencies
        Graphs::Graph<package_spec> graph;
        graph.add_vertices(specs);

        std::vector<package_spec> examine_stack(specs);
        while (!examine_stack.empty())
        {
            const package_spec spec = examine_stack.back();
            examine_stack.pop_back();

            if (was_examined.find(spec) != was_examined.end())
            {
                continue;
            }

            auto process_dependencies = [&](const std::vector<std::string>& dependencies_as_string)
                {
                    for (const std::string& dep_as_string : dependencies_as_string)
                    {
                        const package_spec current_dep = package_spec::from_name_and_triplet(dep_as_string, spec.target_triplet()).get_or_throw();
                        graph.add_edge(spec, current_dep);
                        if (was_examined.find(current_dep) == was_examined.end())
                        {
                            examine_stack.push_back(std::move(current_dep));
                        }
                    }
                };

            auto it = status_db.find(spec);
            if (it != status_db.end() && (*it)->want == want_t::install)
            {
                was_examined.emplace(spec, install_plan_action{install_plan_type::ALREADY_INSTALLED, nullptr, nullptr});
                continue;
            }

            expected<BinaryParagraph> maybe_bpgh = try_load_cached_package(paths, spec);
            if (BinaryParagraph* bpgh = maybe_bpgh.get())
            {
                process_dependencies(bpgh->depends);
                was_examined.emplace(spec, install_plan_action{install_plan_type::INSTALL, std::make_unique<BinaryParagraph>(std::move(*bpgh)), nullptr});
                continue;
            }

            expected<SourceParagraph> maybe_spgh = try_load_port(paths, spec.name());
            SourceParagraph* spgh = maybe_spgh.get();
            Checks::check_exit(spgh != nullptr, "Cannot find package %s", spec.name());
            process_dependencies(filter_dependencies(spgh->depends, spec.target_triplet()));
            was_examined.emplace(spec, install_plan_action{install_plan_type::BUILD_AND_INSTALL, nullptr, std::make_unique<SourceParagraph>(std::move(*spgh))});
        }

        std::vector<package_spec_with_install_plan> ret;

        const std::vector<package_spec> pkgs = graph.find_topological_sort();
        for (const package_spec& pkg : pkgs)
        {
            ret.push_back({ pkg, std::move(was_examined[pkg]) });
        }
        return ret;
    }

    std::vector<package_spec_with_remove_plan> create_remove_plan(const vcpkg_paths& paths, const std::vector<package_spec>& specs, const StatusParagraphs& status_db)
    {
        std::unordered_set<package_spec> specs_as_set(specs.cbegin(), specs.cend());

        std::unordered_map<package_spec, remove_plan_action> was_examined; // Examine = we have checked its immediate (non-recursive) dependencies
        Graphs::Graph<package_spec> graph;
        graph.add_vertices(specs);

        std::vector<package_spec> examine_stack(specs);
        while (!examine_stack.empty())
        {
            const package_spec spec = examine_stack.back();
            examine_stack.pop_back();

            if (was_examined.find(spec) != was_examined.end())
            {
                continue;
            }

            auto it = status_db.find(spec);
            if (it == status_db.end() || (*it)->state == install_state_t::not_installed)
            {
                was_examined.emplace(spec, remove_plan_action{ remove_plan_type::NOT_INSTALLED, nullptr});
                continue;
            }

            for (const std::unique_ptr<StatusParagraph>& an_installed_package : status_db)
            {
                if (an_installed_package->want != want_t::install)
                    continue;
                if (an_installed_package->package.spec.target_triplet() != spec.target_triplet())
                    continue;

                const std::vector<std::string>& deps = an_installed_package->package.depends;
                if (std::find(deps.begin(), deps.end(), spec.name()) == deps.end())
                {
                    continue;
                }

                graph.add_edge(spec, an_installed_package.get()->package.spec);
                examine_stack.push_back(an_installed_package.get()->package.spec);
            }

            const remove_plan_type type = specs_as_set.find(spec) != specs_as_set.end() ? remove_plan_type::REMOVE_USER_REQUESTED: remove_plan_type::REMOVE;
            was_examined.emplace(spec, remove_plan_action{ type, std::make_unique<BinaryParagraph>(std::move((*it)->package))});
        }

        std::vector<package_spec_with_remove_plan> ret;

        const std::vector<package_spec> pkgs = graph.find_topological_sort();
        for (const package_spec& pkg : pkgs)
        {
            ret.push_back({ pkg, std::move(was_examined[pkg]) });
        }
        return ret;
    }
}