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
|
#include "pch.h"
#include "PackageSpec.h"
#include "Paragraphs.h"
#include "StatusParagraphs.h"
#include "VcpkgPaths.h"
#include "vcpkg_Dependencies.h"
#include "vcpkg_Files.h"
#include "vcpkg_Graphs.h"
#include "vcpkg_Util.h"
#include "vcpkglib.h"
namespace vcpkg::Dependencies
{
bool operator==(const ClusterPtr& l, const ClusterPtr& r) { return l.ptr == r.ptr; }
std::vector<PackageSpec> AnyParagraph::dependencies(const Triplet& triplet) const
{
auto to_package_specs = [&](const std::vector<std::string>& dependencies_as_string) {
return Util::fmap(dependencies_as_string, [&](const std::string s) {
return PackageSpec::from_name_and_triplet(s, triplet).value_or_exit(VCPKG_LINE_INFO);
});
};
if (auto p = this->status_paragraph.get())
{
return to_package_specs(p->package.depends);
}
if (auto p = this->binary_paragraph.get())
{
return to_package_specs(p->depends);
}
if (auto p = this->source_paragraph.get())
{
return to_package_specs(filter_dependencies(p->depends, triplet));
}
Checks::exit_with_message(VCPKG_LINE_INFO,
"Cannot get dependencies because there was none of: source/binary/status paragraphs");
}
std::string to_output_string(RequestType request_type, const CStringView s)
{
switch (request_type)
{
case RequestType::AUTO_SELECTED: return Strings::format(" * %s", s);
case RequestType::USER_REQUESTED: return Strings::format(" %s", s);
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
InstallPlanAction::InstallPlanAction()
: spec(), any_paragraph(), plan_type(InstallPlanType::UNKNOWN), request_type(RequestType::UNKNOWN)
{
}
InstallPlanAction::InstallPlanAction(const PackageSpec& spec,
const SourceControlFile& any_paragraph,
const std::unordered_set<std::string>& features,
const RequestType& request_type)
: InstallPlanAction()
{
this->spec = spec;
this->request_type = request_type;
this->plan_type = InstallPlanType::BUILD_AND_INSTALL;
this->any_paragraph.source_control_file = &any_paragraph;
this->feature_list = features;
}
InstallPlanAction::InstallPlanAction(const PackageSpec& spec,
const AnyParagraph& any_paragraph,
const RequestType& request_type)
: InstallPlanAction()
{
this->spec = spec;
this->request_type = request_type;
if (auto p = any_paragraph.status_paragraph.get())
{
this->plan_type = InstallPlanType::ALREADY_INSTALLED;
this->any_paragraph.status_paragraph = *p;
return;
}
if (auto p = any_paragraph.binary_paragraph.get())
{
this->plan_type = InstallPlanType::INSTALL;
this->any_paragraph.binary_paragraph = *p;
return;
}
if (auto p = any_paragraph.source_paragraph.get())
{
this->plan_type = InstallPlanType::BUILD_AND_INSTALL;
this->any_paragraph.source_paragraph = *p;
return;
}
this->plan_type = InstallPlanType::UNKNOWN;
}
bool InstallPlanAction::compare_by_name(const InstallPlanAction* left, const InstallPlanAction* right)
{
return left->spec.name() < right->spec.name();
}
RemovePlanAction::RemovePlanAction() : plan_type(RemovePlanType::UNKNOWN), request_type(RequestType::UNKNOWN) {}
RemovePlanAction::RemovePlanAction(const PackageSpec& spec,
const RemovePlanType& plan_type,
const RequestType& request_type)
: spec(spec), plan_type(plan_type), request_type(request_type)
{
}
bool ExportPlanAction::compare_by_name(const ExportPlanAction* left, const ExportPlanAction* right)
{
return left->spec.name() < right->spec.name();
}
ExportPlanAction::ExportPlanAction()
: spec(), any_paragraph(), plan_type(ExportPlanType::UNKNOWN), request_type(RequestType::UNKNOWN)
{
}
ExportPlanAction::ExportPlanAction(const PackageSpec& spec,
const AnyParagraph& any_paragraph,
const RequestType& request_type)
: ExportPlanAction()
{
this->spec = spec;
this->request_type = request_type;
if (auto p = any_paragraph.binary_paragraph.get())
{
this->plan_type = ExportPlanType::ALREADY_BUILT;
this->any_paragraph.binary_paragraph = *p;
return;
}
if (auto p = any_paragraph.source_paragraph.get())
{
this->plan_type = ExportPlanType::PORT_AVAILABLE_BUT_NOT_BUILT;
this->any_paragraph.source_paragraph = *p;
return;
}
this->plan_type = ExportPlanType::UNKNOWN;
}
bool RemovePlanAction::compare_by_name(const RemovePlanAction* left, const RemovePlanAction* right)
{
return left->spec.name() < right->spec.name();
}
MapPortFile::MapPortFile(const std::unordered_map<PackageSpec, SourceControlFile>& map) : ports(map) {}
const SourceControlFile& MapPortFile::get_control_file(const PackageSpec& spec) const
{
auto scf = ports.find(spec);
if (scf == ports.end())
{
Checks::exit_fail(VCPKG_LINE_INFO);
}
return scf->second;
}
PathsPortFile::PathsPortFile(const VcpkgPaths& paths) : ports(paths) {}
const SourceControlFile& PathsPortFile::get_control_file(const PackageSpec& spec) const
{
std::unordered_map<PackageSpec, SourceControlFile>::iterator cache_it = cache.find(spec);
if (cache_it != cache.end())
{
return cache_it->second;
}
Parse::ParseExpected<SourceControlFile> source_control_file =
Paragraphs::try_load_port(ports.get_filesystem(), ports.port_dir(spec));
if (auto scf = source_control_file.get())
{
auto it = cache.emplace(spec, std::move(*scf->get()));
return it.first->second;
}
print_error_message(source_control_file.error());
Checks::exit_fail(VCPKG_LINE_INFO);
}
std::vector<InstallPlanAction> create_install_plan(const PortFileProvider& port_file_provider,
const std::vector<PackageSpec>& specs,
const StatusParagraphs& status_db)
{
struct InstallAdjacencyProvider final : Graphs::AdjacencyProvider<PackageSpec, InstallPlanAction>
{
const PortFileProvider& port_file_provider;
const StatusParagraphs& status_db;
const std::unordered_set<PackageSpec>& specs_as_set;
InstallAdjacencyProvider(const PortFileProvider& port_file_provider,
const StatusParagraphs& s,
const std::unordered_set<PackageSpec>& specs_as_set)
: port_file_provider(port_file_provider), status_db(s), specs_as_set(specs_as_set)
{
}
std::vector<PackageSpec> adjacency_list(const InstallPlanAction& plan) const override
{
if (plan.any_paragraph.status_paragraph.get()) return std::vector<PackageSpec>{};
return plan.any_paragraph.dependencies(plan.spec.triplet());
}
InstallPlanAction load_vertex_data(const PackageSpec& spec) const override
{
const RequestType request_type = specs_as_set.find(spec) != specs_as_set.end()
? RequestType::USER_REQUESTED
: RequestType::AUTO_SELECTED;
auto it = status_db.find_installed(spec);
if (it != status_db.end()) return InstallPlanAction{spec, {*it->get(), nullopt, nullopt}, request_type};
return InstallPlanAction{
spec, {nullopt, nullopt, *port_file_provider.get_control_file(spec).core_paragraph}, request_type};
}
};
const std::unordered_set<PackageSpec> specs_as_set(specs.cbegin(), specs.cend());
std::vector<InstallPlanAction> toposort =
Graphs::topological_sort(specs, InstallAdjacencyProvider{port_file_provider, status_db, specs_as_set});
Util::erase_remove_if(toposort, [](const InstallPlanAction& plan) {
return plan.request_type == RequestType::AUTO_SELECTED &&
plan.plan_type == InstallPlanType::ALREADY_INSTALLED;
});
return toposort;
}
std::vector<RemovePlanAction> create_remove_plan(const std::vector<PackageSpec>& specs,
const StatusParagraphs& status_db)
{
struct RemoveAdjacencyProvider final : Graphs::AdjacencyProvider<PackageSpec, RemovePlanAction>
{
const StatusParagraphs& status_db;
const std::vector<StatusParagraph*>& installed_ports;
const std::unordered_set<PackageSpec>& specs_as_set;
RemoveAdjacencyProvider(const StatusParagraphs& status_db,
const std::vector<StatusParagraph*>& installed_ports,
const std::unordered_set<PackageSpec>& specs_as_set)
: status_db(status_db), installed_ports(installed_ports), specs_as_set(specs_as_set)
{
}
std::vector<PackageSpec> adjacency_list(const RemovePlanAction& plan) const override
{
if (plan.plan_type == RemovePlanType::NOT_INSTALLED)
{
return {};
}
const PackageSpec& spec = plan.spec;
std::vector<PackageSpec> dependents;
for (const StatusParagraph* an_installed_package : installed_ports)
{
if (an_installed_package->package.spec.triplet() != spec.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;
dependents.push_back(an_installed_package->package.spec);
}
return dependents;
}
RemovePlanAction load_vertex_data(const PackageSpec& spec) const override
{
const RequestType request_type = specs_as_set.find(spec) != specs_as_set.end()
? RequestType::USER_REQUESTED
: RequestType::AUTO_SELECTED;
const StatusParagraphs::const_iterator it = status_db.find_installed(spec);
if (it == status_db.end())
{
return RemovePlanAction{spec, RemovePlanType::NOT_INSTALLED, request_type};
}
return RemovePlanAction{spec, RemovePlanType::REMOVE, request_type};
}
};
const std::vector<StatusParagraph*>& installed_ports = get_installed_ports(status_db);
const std::unordered_set<PackageSpec> specs_as_set(specs.cbegin(), specs.cend());
return Graphs::topological_sort(specs, RemoveAdjacencyProvider{status_db, installed_ports, specs_as_set});
}
std::vector<ExportPlanAction> create_export_plan(const VcpkgPaths& paths,
const std::vector<PackageSpec>& specs,
const StatusParagraphs& status_db)
{
struct ExportAdjacencyProvider final : Graphs::AdjacencyProvider<PackageSpec, ExportPlanAction>
{
const VcpkgPaths& paths;
const StatusParagraphs& status_db;
const std::unordered_set<PackageSpec>& specs_as_set;
ExportAdjacencyProvider(const VcpkgPaths& p,
const StatusParagraphs& s,
const std::unordered_set<PackageSpec>& specs_as_set)
: paths(p), status_db(s), specs_as_set(specs_as_set)
{
}
std::vector<PackageSpec> adjacency_list(const ExportPlanAction& plan) const override
{
return plan.any_paragraph.dependencies(plan.spec.triplet());
}
ExportPlanAction load_vertex_data(const PackageSpec& spec) const override
{
const RequestType request_type = specs_as_set.find(spec) != specs_as_set.end()
? RequestType::USER_REQUESTED
: RequestType::AUTO_SELECTED;
Expected<BinaryParagraph> maybe_bpgh = Paragraphs::try_load_cached_package(paths, spec);
if (auto bpgh = maybe_bpgh.get())
return ExportPlanAction{spec, {nullopt, *bpgh, nullopt}, request_type};
auto maybe_scf = Paragraphs::try_load_port(paths.get_filesystem(), paths.port_dir(spec));
if (auto scf = maybe_scf.get())
return ExportPlanAction{spec, {nullopt, nullopt, *scf->get()->core_paragraph}, request_type};
else
print_error_message(maybe_scf.error());
Checks::exit_with_message(VCPKG_LINE_INFO, "Could not find package %s", spec);
}
};
const std::unordered_set<PackageSpec> specs_as_set(specs.cbegin(), specs.cend());
std::vector<ExportPlanAction> toposort =
Graphs::topological_sort(specs, ExportAdjacencyProvider{paths, status_db, specs_as_set});
return toposort;
}
std::vector<FeatureSpec> to_feature_specs(const std::vector<std::string>& depends, const Triplet& triplet)
{
std::vector<FeatureSpec> f_specs;
for (auto&& depend : depends)
{
int end = (int)depend.find(']');
if (end != std::string::npos)
{
int start = (int)depend.find('[');
auto feature_name = depend.substr(start + 1, end - start - 1);
auto package_name = depend.substr(0, start);
auto p_spec = PackageSpec::from_name_and_triplet(package_name, triplet).value_or_exit(VCPKG_LINE_INFO);
auto feature_spec = FeatureSpec{p_spec, feature_name};
f_specs.emplace_back(std::move(feature_spec));
}
else
{
auto p_spec = PackageSpec::from_name_and_triplet(depend, triplet).value_or_exit(VCPKG_LINE_INFO);
auto feature_spec = FeatureSpec{p_spec, ""};
f_specs.emplace_back(std::move(feature_spec));
}
}
return f_specs;
}
bool mark_plus(const std::string& feature,
Cluster& cluster,
std::unordered_map<PackageSpec, Cluster>& pkg_to_cluster,
GraphPlan& graph_plan)
{
auto it = cluster.edges.find(feature);
std::string updated_feature = feature;
if (updated_feature == "")
{
updated_feature = "core";
it = cluster.edges.find("core");
}
if (it == cluster.edges.end())
{
Checks::unreachable(VCPKG_LINE_INFO);
}
if (cluster.edges[updated_feature].plus) return true;
if (cluster.original_features.find(updated_feature) == cluster.original_features.end())
{
cluster.transient_uninstalled = true;
}
if (!cluster.transient_uninstalled)
{
return false;
}
cluster.edges[updated_feature].plus = true;
if (!cluster.original_features.empty())
{
mark_minus(cluster, pkg_to_cluster, graph_plan);
}
graph_plan.install_graph.add_vertex({&cluster});
auto& tracked = cluster.to_install_features;
tracked.insert(updated_feature);
if (tracked.find("core") == tracked.end() && tracked.find("") == tracked.end())
{
cluster.to_install_features.insert("core");
for (auto&& depend : cluster.edges["core"].build_edges)
{
auto& depend_cluster = pkg_to_cluster[depend.spec];
mark_plus(depend.feature_name, depend_cluster, pkg_to_cluster, graph_plan);
graph_plan.install_graph.add_edge({&cluster}, {&depend_cluster});
}
}
for (auto&& depend : cluster.edges[updated_feature].build_edges)
{
auto& depend_cluster = pkg_to_cluster[depend.spec];
mark_plus(depend.feature_name, depend_cluster, pkg_to_cluster, graph_plan);
if (&depend_cluster == &cluster) continue;
graph_plan.install_graph.add_edge({&cluster}, {&depend_cluster});
}
return true;
}
void mark_minus(Cluster& cluster, std::unordered_map<PackageSpec, Cluster>& pkg_to_cluster, GraphPlan& graph_plan)
{
if (cluster.will_remove) return;
cluster.will_remove = true;
graph_plan.remove_graph.add_vertex({&cluster});
for (auto&& pair : cluster.edges)
{
auto& remove_edges_edges = pair.second.remove_edges;
for (auto&& depend : remove_edges_edges)
{
auto& depend_cluster = pkg_to_cluster[depend.spec];
graph_plan.remove_graph.add_edge({&cluster}, {&depend_cluster});
depend_cluster.transient_uninstalled = true;
mark_minus(depend_cluster, pkg_to_cluster, graph_plan);
}
}
for (auto&& original_feature : cluster.original_features)
{
cluster.transient_uninstalled = true;
mark_plus(original_feature, cluster, pkg_to_cluster, graph_plan);
}
}
std::vector<AnyAction> create_feature_install_plan(const std::unordered_map<PackageSpec, SourceControlFile>& map,
const std::vector<FullPackageSpec>& specs,
const StatusParagraphs& status_db)
{
std::unordered_map<PackageSpec, Cluster> pkg_spec_to_package_node;
for (const auto& it : map)
{
Cluster& node = pkg_spec_to_package_node[it.first];
node.spec = it.first;
FeatureNodeEdges core_dependencies;
auto core_depends = filter_dependencies(it.second.core_paragraph->depends, node.spec.triplet());
core_dependencies.build_edges = to_feature_specs(core_depends, node.spec.triplet());
node.edges["core"] = std::move(core_dependencies);
for (const auto& feature : it.second.feature_paragraphs)
{
FeatureNodeEdges added_edges;
auto depends = filter_dependencies(feature->depends, node.spec.triplet());
added_edges.build_edges = to_feature_specs(depends, node.spec.triplet());
node.edges.emplace(feature->name, std::move(added_edges));
}
node.source_control_file = &it.second;
}
for (auto&& status_paragraph : get_installed_ports(status_db))
{
auto& spec = status_paragraph->package.spec;
auto& status_paragraph_feature = status_paragraph->package.feature;
Cluster& cluster = pkg_spec_to_package_node[spec];
cluster.transient_uninstalled = false;
auto reverse_edges =
to_feature_specs(status_paragraph->package.depends, status_paragraph->package.spec.triplet());
for (auto&& dependency : reverse_edges)
{
auto pkg_node = pkg_spec_to_package_node.find(dependency.spec);
auto depends_name = dependency.feature_name;
if (depends_name == "")
{
for (auto&& default_feature : status_paragraph->package.default_features)
{
auto& target_node = pkg_node->second.edges[default_feature];
target_node.remove_edges.emplace_back(FeatureSpec{spec, status_paragraph_feature});
}
depends_name = "core";
}
auto& target_node = pkg_node->second.edges[depends_name];
target_node.remove_edges.emplace_back(FeatureSpec{spec, status_paragraph_feature});
}
cluster.status_paragraphs.emplace_back(*status_paragraph);
if (status_paragraph_feature == "")
{
cluster.original_features.insert("core");
}
else
{
cluster.original_features.insert(status_paragraph_feature);
}
}
GraphPlan graph_plan;
for (auto&& spec : specs)
{
Cluster& spec_cluster = pkg_spec_to_package_node[spec.package_spec];
for (auto&& feature : spec.features)
{
mark_plus(feature, spec_cluster, pkg_spec_to_package_node, graph_plan);
}
}
Graphs::GraphAdjacencyProvider<ClusterPtr> adjacency_remove_graph(graph_plan.remove_graph.adjacency_list());
auto remove_vertex_list = graph_plan.remove_graph.vertex_list();
auto remove_toposort = Graphs::topological_sort(remove_vertex_list, adjacency_remove_graph);
Graphs::GraphAdjacencyProvider<ClusterPtr> adjacency_install_graph(graph_plan.install_graph.adjacency_list());
auto insert_vertex_list = graph_plan.install_graph.vertex_list();
auto insert_toposort = Graphs::topological_sort(insert_vertex_list, adjacency_install_graph);
std::vector<AnyAction> install_plan;
for (auto&& like_cluster : remove_toposort)
{
auto scf = *like_cluster.ptr->source_control_file.get();
AnyAction any_plan;
any_plan.remove_plan = RemovePlanAction{
PackageSpec::from_name_and_triplet(scf->core_paragraph->name, like_cluster.ptr->spec.triplet())
.value_or_exit(VCPKG_LINE_INFO),
RemovePlanType::REMOVE,
RequestType::AUTO_SELECTED};
install_plan.emplace_back(std::move(any_plan));
}
for (auto&& like_cluster : insert_toposort)
{
if (!like_cluster.ptr->transient_uninstalled) continue;
auto scf = *like_cluster.ptr->source_control_file.get();
auto pkg_spec =
PackageSpec::from_name_and_triplet(scf->core_paragraph->name, like_cluster.ptr->spec.triplet())
.value_or_exit(VCPKG_LINE_INFO);
auto action =
InstallPlanAction{pkg_spec, *scf, like_cluster.ptr->to_install_features, RequestType::AUTO_SELECTED};
AnyAction any_plan;
any_plan.install_plan = std::move(action);
install_plan.emplace_back(std::move(any_plan));
}
return install_plan;
}
}
|