2017-01-27 12:49:09 -08:00
|
|
|
#include "pch.h"
|
2016-09-22 23:28:50 -07:00
|
|
|
#include "vcpkg_Dependencies.h"
|
|
|
|
#include "vcpkg_Graphs.h"
|
2017-04-03 16:29:11 -07:00
|
|
|
#include "VcpkgPaths.h"
|
2017-04-03 14:45:00 -07:00
|
|
|
#include "PackageSpec.h"
|
2016-09-22 23:28:50 -07:00
|
|
|
#include "StatusParagraphs.h"
|
2016-11-07 16:38:49 -08:00
|
|
|
#include "vcpkg_Files.h"
|
2017-02-27 15:52:57 -08:00
|
|
|
#include "Paragraphs.h"
|
2016-09-22 23:28:50 -07:00
|
|
|
|
2017-01-05 14:14:11 -08:00
|
|
|
namespace vcpkg::Dependencies
|
2016-09-22 23:28:50 -07:00
|
|
|
{
|
2017-04-06 18:57:17 -07:00
|
|
|
std::string to_output_string(RequestType request_type, const CStringView s)
|
|
|
|
{
|
|
|
|
switch (request_type)
|
|
|
|
{
|
2017-04-07 12:50:46 -07:00
|
|
|
case RequestType::AUTO_SELECTED:
|
|
|
|
return Strings::format(" * %s", s);
|
|
|
|
case RequestType::USER_REQUESTED:
|
|
|
|
return Strings::format(" %s", s);
|
|
|
|
default:
|
|
|
|
Checks::unreachable(VCPKG_LINE_INFO);
|
2017-04-06 18:57:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:50:46 -07:00
|
|
|
InstallPlanAction::InstallPlanAction() : plan_type(InstallPlanType::UNKNOWN), binary_pgh(nullopt), source_pgh(nullopt) { }
|
2017-01-30 17:52:53 -08:00
|
|
|
|
2017-04-03 16:27:51 -07:00
|
|
|
InstallPlanAction::InstallPlanAction(const InstallPlanType& plan_type, Optional<BinaryParagraph> binary_pgh, Optional<SourceParagraph> source_pgh)
|
2017-04-07 12:50:46 -07:00
|
|
|
: plan_type(std::move(plan_type)), binary_pgh(std::move(binary_pgh)), source_pgh(std::move(source_pgh)) { }
|
2017-01-30 17:52:53 -08:00
|
|
|
|
2017-04-07 12:50:46 -07:00
|
|
|
PackageSpecWithInstallPlan::PackageSpecWithInstallPlan(const PackageSpec& spec, InstallPlanAction&& plan) : spec(spec), plan(std::move(plan)) { }
|
2017-01-30 17:52:53 -08:00
|
|
|
|
2017-04-07 12:50:46 -07:00
|
|
|
RemovePlanAction::RemovePlanAction() : plan_type(RemovePlanType::UNKNOWN), request_type(RequestType::UNKNOWN) { }
|
2017-01-30 17:52:53 -08:00
|
|
|
|
2017-04-07 12:50:46 -07:00
|
|
|
RemovePlanAction::RemovePlanAction(const RemovePlanType& plan_type, const Dependencies::RequestType& request_type) : plan_type(plan_type), request_type(request_type) { }
|
2017-01-30 17:52:53 -08:00
|
|
|
|
2017-04-06 17:37:19 -07:00
|
|
|
bool PackageSpecWithRemovePlan::compare_by_name(const PackageSpecWithRemovePlan* left, const PackageSpecWithRemovePlan* right)
|
|
|
|
{
|
|
|
|
return left->spec.name() < right->spec.name();
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:24:44 -07:00
|
|
|
PackageSpecWithRemovePlan::PackageSpecWithRemovePlan(const PackageSpec& spec, RemovePlanAction&& plan)
|
2017-04-07 12:50:46 -07:00
|
|
|
: spec(spec), plan(std::move(plan)) { }
|
2017-01-30 17:52:53 -08:00
|
|
|
|
2017-04-03 16:29:11 -07:00
|
|
|
std::vector<PackageSpecWithInstallPlan> create_install_plan(const VcpkgPaths& paths, const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db)
|
2016-11-07 17:05:32 -08:00
|
|
|
{
|
2017-04-03 16:22:51 -07:00
|
|
|
std::unordered_map<PackageSpec, InstallPlanAction> was_examined; // Examine = we have checked its immediate (non-recursive) dependencies
|
2017-04-03 14:45:00 -07:00
|
|
|
Graphs::Graph<PackageSpec> graph;
|
2016-11-15 11:56:46 -08:00
|
|
|
graph.add_vertices(specs);
|
2016-09-22 23:28:50 -07:00
|
|
|
|
2017-04-03 14:45:00 -07:00
|
|
|
std::vector<PackageSpec> examine_stack(specs);
|
2016-09-22 23:28:50 -07:00
|
|
|
while (!examine_stack.empty())
|
|
|
|
{
|
2017-04-03 14:45:00 -07:00
|
|
|
const PackageSpec spec = examine_stack.back();
|
2016-09-22 23:28:50 -07:00
|
|
|
examine_stack.pop_back();
|
|
|
|
|
|
|
|
if (was_examined.find(spec) != was_examined.end())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-15 11:56:46 -08:00
|
|
|
auto process_dependencies = [&](const std::vector<std::string>& dependencies_as_string)
|
|
|
|
{
|
|
|
|
for (const std::string& dep_as_string : dependencies_as_string)
|
|
|
|
{
|
2017-04-03 14:45:00 -07:00
|
|
|
const PackageSpec current_dep = PackageSpec::from_name_and_triplet(dep_as_string, spec.target_triplet()).value_or_exit(VCPKG_LINE_INFO);
|
2016-11-15 11:56:46 -08:00
|
|
|
graph.add_edge(spec, current_dep);
|
|
|
|
if (was_examined.find(current_dep) == was_examined.end())
|
|
|
|
{
|
|
|
|
examine_stack.push_back(std::move(current_dep));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-09-22 23:28:50 -07:00
|
|
|
|
2016-11-15 11:56:46 -08:00
|
|
|
auto it = status_db.find(spec);
|
2017-04-03 15:42:26 -07:00
|
|
|
if (it != status_db.end() && (*it)->want == Want::INSTALL)
|
2016-09-22 23:28:50 -07:00
|
|
|
{
|
2017-04-07 12:50:46 -07:00
|
|
|
was_examined.emplace(spec, InstallPlanAction{ InstallPlanType::ALREADY_INSTALLED, nullopt, nullopt });
|
2016-11-15 11:56:46 -08:00
|
|
|
continue;
|
|
|
|
}
|
2016-10-04 15:25:53 -07:00
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected<BinaryParagraph> maybe_bpgh = Paragraphs::try_load_cached_package(paths, spec);
|
2016-11-15 11:56:46 -08:00
|
|
|
if (BinaryParagraph* bpgh = maybe_bpgh.get())
|
|
|
|
{
|
|
|
|
process_dependencies(bpgh->depends);
|
2017-04-07 12:50:46 -07:00
|
|
|
was_examined.emplace(spec, InstallPlanAction{ InstallPlanType::INSTALL, std::move(*bpgh), nullopt });
|
2016-11-15 11:56:46 -08:00
|
|
|
continue;
|
2016-09-22 23:28:50 -07:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(paths.port_dir(spec));
|
2017-03-31 09:26:58 -07:00
|
|
|
if (auto spgh = maybe_spgh.get())
|
|
|
|
{
|
|
|
|
process_dependencies(filter_dependencies(spgh->depends, spec.target_triplet()));
|
2017-04-03 16:22:51 -07:00
|
|
|
was_examined.emplace(spec, InstallPlanAction{ InstallPlanType::BUILD_AND_INSTALL, nullopt, std::move(*spgh) });
|
2017-03-31 09:26:58 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Checks::exit_with_message(VCPKG_LINE_INFO, "Cannot find package %s", spec.name());
|
|
|
|
}
|
2016-09-22 23:28:50 -07:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:23:23 -07:00
|
|
|
std::vector<PackageSpecWithInstallPlan> ret;
|
2016-11-07 16:38:49 -08:00
|
|
|
|
2017-04-03 14:45:00 -07:00
|
|
|
const std::vector<PackageSpec> pkgs = graph.find_topological_sort();
|
|
|
|
for (const PackageSpec& pkg : pkgs)
|
2016-11-07 16:38:49 -08:00
|
|
|
{
|
2017-04-03 16:23:23 -07:00
|
|
|
ret.push_back(PackageSpecWithInstallPlan(pkg, std::move(was_examined[pkg])));
|
2016-11-07 16:38:49 -08:00
|
|
|
}
|
2016-11-15 11:56:46 -08:00
|
|
|
return ret;
|
2016-11-07 16:38:49 -08:00
|
|
|
}
|
2017-01-26 17:53:45 -08:00
|
|
|
|
2017-04-03 16:24:44 -07:00
|
|
|
std::vector<PackageSpecWithRemovePlan> create_remove_plan(const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db)
|
2017-01-26 17:53:45 -08:00
|
|
|
{
|
2017-04-03 14:45:00 -07:00
|
|
|
std::unordered_set<PackageSpec> specs_as_set(specs.cbegin(), specs.cend());
|
2017-01-26 17:53:45 -08:00
|
|
|
|
2017-04-03 16:24:18 -07:00
|
|
|
std::unordered_map<PackageSpec, RemovePlanAction> was_examined; // Examine = we have checked its immediate (non-recursive) dependencies
|
2017-04-03 14:45:00 -07:00
|
|
|
Graphs::Graph<PackageSpec> graph;
|
2017-01-26 17:53:45 -08:00
|
|
|
graph.add_vertices(specs);
|
|
|
|
|
2017-04-03 14:45:00 -07:00
|
|
|
std::vector<PackageSpec> examine_stack(specs);
|
2017-01-26 17:53:45 -08:00
|
|
|
while (!examine_stack.empty())
|
|
|
|
{
|
2017-04-03 14:45:00 -07:00
|
|
|
const PackageSpec spec = examine_stack.back();
|
2017-01-26 17:53:45 -08:00
|
|
|
examine_stack.pop_back();
|
|
|
|
|
|
|
|
if (was_examined.find(spec) != was_examined.end())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:29:02 -08:00
|
|
|
const StatusParagraphs::const_iterator it = status_db.find(spec);
|
2017-04-03 15:25:34 -07:00
|
|
|
if (it == status_db.end() || (*it)->state == InstallState::NOT_INSTALLED)
|
2017-01-26 17:53:45 -08:00
|
|
|
{
|
2017-04-03 16:24:18 -07:00
|
|
|
was_examined.emplace(spec, RemovePlanAction(RemovePlanType::NOT_INSTALLED, RequestType::USER_REQUESTED));
|
2017-01-26 17:53:45 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const std::unique_ptr<StatusParagraph>& an_installed_package : status_db)
|
|
|
|
{
|
2017-04-03 15:42:26 -07:00
|
|
|
if (an_installed_package->want != Want::INSTALL)
|
2017-01-26 17:53:45 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:21:46 -07:00
|
|
|
const RequestType request_type = specs_as_set.find(spec) != specs_as_set.end() ? RequestType::USER_REQUESTED : RequestType::AUTO_SELECTED;
|
2017-04-03 16:24:18 -07:00
|
|
|
was_examined.emplace(spec, RemovePlanAction(RemovePlanType::REMOVE, request_type));
|
2017-01-26 17:53:45 -08:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:24:44 -07:00
|
|
|
std::vector<PackageSpecWithRemovePlan> ret;
|
2017-01-26 17:53:45 -08:00
|
|
|
|
2017-04-03 14:45:00 -07:00
|
|
|
const std::vector<PackageSpec> pkgs = graph.find_topological_sort();
|
|
|
|
for (const PackageSpec& pkg : pkgs)
|
2017-01-26 17:53:45 -08:00
|
|
|
{
|
2017-04-03 16:24:44 -07:00
|
|
|
ret.push_back(PackageSpecWithRemovePlan(pkg, std::move(was_examined[pkg])));
|
2017-01-26 17:53:45 -08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-05 14:14:11 -08:00
|
|
|
}
|