Simplify Install plan generation
MODIFY
include/vcpkg_Dependencies.h
include/vcpkg_Dependencies.h
@@ -9,9 +9,8 @@ namespace vcpkg::Dependencies
{
struct AnyParagraph
{
- std::vector<PackageSpec> dependencies() const;
+ std::vector<PackageSpec> dependencies(const Triplet& triplet) const;
- PackageSpec spec;
Optional<StatusParagraph> status_paragraph;
Optional<BinaryParagraph> binary_paragraph;
Optional<SourceParagraph> source_paragraph;
@@ -36,28 +35,19 @@ namespace vcpkg::Dependencies
struct InstallPlanAction
{
+ static bool compare_by_name(const InstallPlanAction* left, const InstallPlanAction* right);
+
InstallPlanAction();
- explicit InstallPlanAction(const AnyParagraph& any_paragraph, const RequestType& request_type);
- InstallPlanAction(const InstallPlanType& plan_type, const RequestType& request_type, Optional<BinaryParagraph> binary_pgh, Optional<SourceParagraph> source_pgh);
+ explicit InstallPlanAction(const PackageSpec& spec, const AnyParagraph& any_paragraph, const RequestType& request_type);
InstallPlanAction(const InstallPlanAction&) = delete;
InstallPlanAction(InstallPlanAction&&) = default;
InstallPlanAction& operator=(const InstallPlanAction&) = delete;
InstallPlanAction& operator=(InstallPlanAction&&) = default;
+ PackageSpec spec;
+ AnyParagraph any_paragraph;
InstallPlanType plan_type;
RequestType request_type;
- Optional<BinaryParagraph> binary_pgh;
- Optional<SourceParagraph> source_pgh;
- };
-
- struct PackageSpecWithInstallPlan
- {
- static bool compare_by_name(const PackageSpecWithInstallPlan* left, const PackageSpecWithInstallPlan* right);
-
- PackageSpecWithInstallPlan(const PackageSpec& spec, InstallPlanAction&& plan);
-
- PackageSpec spec;
- InstallPlanAction plan;
};
enum class RemovePlanType
@@ -67,6 +57,12 @@ namespace vcpkg::Dependencies
REMOVE
};
+ struct SpecAndRemovePlanType
+ {
+ PackageSpec spec;
+ RemovePlanType plan_type;
+ };
+
struct RemovePlanAction
{
RemovePlanAction();
@@ -90,7 +86,7 @@ namespace vcpkg::Dependencies
RemovePlanAction plan;
};
- std::vector<PackageSpecWithInstallPlan> create_install_plan(const VcpkgPaths& paths, const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db);
+ std::vector<InstallPlanAction> create_install_plan(const VcpkgPaths& paths, const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db);
std::vector<PackageSpecWithRemovePlan> create_remove_plan(const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db);
}

MODIFY
src/commands_build.cpp
src/commands_build.cpp
namespace vcpkg::Commands::Build
{
- using Dependencies::PackageSpecWithInstallPlan;
+ using Dependencies::InstallPlanAction;
using Dependencies::InstallPlanType;
static const std::string OPTION_CHECKS_ONLY = "--checks-only";
@@ -148,11 +148,11 @@ namespace vcpkg::Commands::Build
const BuildResult result = build_package(spgh, spec, paths, paths.port_dir(spec), status_db);
if (result == BuildResult::CASCADED_DUE_TO_MISSING_DEPENDENCIES)
{
- std::vector<PackageSpecWithInstallPlan> unmet_dependencies = Dependencies::create_install_plan(paths, { spec }, status_db);
+ std::vector<InstallPlanAction> unmet_dependencies = Dependencies::create_install_plan(paths, { spec }, status_db);
unmet_dependencies.erase(
- std::remove_if(unmet_dependencies.begin(), unmet_dependencies.end(), [&spec](const PackageSpecWithInstallPlan& p)
+ std::remove_if(unmet_dependencies.begin(), unmet_dependencies.end(), [&spec](const InstallPlanAction& p)
{
- return (p.spec == spec) || (p.plan.plan_type == InstallPlanType::ALREADY_INSTALLED);
+ return (p.spec == spec) || (p.plan_type == InstallPlanType::ALREADY_INSTALLED);
}),
unmet_dependencies.end());
@@ -160,7 +160,7 @@ namespace vcpkg::Commands::Build
System::println(System::Color::error, "The build command requires all dependencies to be already installed.");
System::println("The following dependencies are missing:");
System::println("");
- for (const PackageSpecWithInstallPlan& p : unmet_dependencies)
+ for (const InstallPlanAction& p : unmet_dependencies)
{
System::println(" %s", p.spec);
}

MODIFY
src/commands_ci.cpp
src/commands_ci.cpp
namespace vcpkg::Commands::CI
{
- using Dependencies::PackageSpecWithInstallPlan;
+ using Dependencies::InstallPlanAction;
using Dependencies::InstallPlanType;
using Build::BuildResult;
@@ -36,7 +36,7 @@ namespace vcpkg::Commands::CI
const std::vector<PackageSpec> specs = load_all_package_specs(paths.ports, triplet);
StatusParagraphs status_db = database_load_check(paths);
- const std::vector<PackageSpecWithInstallPlan> install_plan = Dependencies::create_install_plan(paths, specs, status_db);
+ const std::vector<InstallPlanAction> install_plan = Dependencies::create_install_plan(paths, specs, status_db);
Checks::check_exit(VCPKG_LINE_INFO, !install_plan.empty(), "Install plan cannot be empty");
std::vector<BuildResult> results;
@@ -44,7 +44,7 @@ namespace vcpkg::Commands::CI
const ElapsedTime timer = ElapsedTime::create_started();
size_t counter = 0;
const size_t package_count = install_plan.size();
- for (const PackageSpecWithInstallPlan& action : install_plan)
+ for (const InstallPlanAction& action : install_plan)
{
const ElapsedTime build_timer = ElapsedTime::create_started();
counter++;
@@ -56,7 +56,7 @@ namespace vcpkg::Commands::CI
try
{
- switch (action.plan.plan_type)
+ switch (action.plan_type)
{
case InstallPlanType::ALREADY_INSTALLED:
results.back() = BuildResult::SUCCEEDED;
@@ -65,7 +65,7 @@ namespace vcpkg::Commands::CI
case InstallPlanType::BUILD_AND_INSTALL:
{
System::println("Building package %s... ", display_name);
- const BuildResult result = Commands::Build::build_package(action.plan.source_pgh.value_or_exit(VCPKG_LINE_INFO),
+ const BuildResult result = Commands::Build::build_package(action.any_paragraph.source_paragraph.value_or_exit(VCPKG_LINE_INFO),
action.spec,
paths,
paths.port_dir(action.spec),
@@ -88,7 +88,7 @@ namespace vcpkg::Commands::CI
case InstallPlanType::INSTALL:
results.back() = BuildResult::SUCCEEDED;
System::println("Installing package %s... ", display_name);
- Install::install_package(paths, action.plan.binary_pgh.value_or_exit(VCPKG_LINE_INFO), &status_db);
+ Install::install_package(paths, action.any_paragraph.binary_paragraph.value_or_exit(VCPKG_LINE_INFO), &status_db);
System::println(System::Color::success, "Installing package %s... done", display_name);
break;
default:

MODIFY
src/commands_install.cpp
src/commands_install.cpp
namespace vcpkg::Commands::Install
{
- using Dependencies::PackageSpecWithInstallPlan;
+ using Dependencies::InstallPlanAction;
using Dependencies::RequestType;
using Dependencies::InstallPlanType;
@@ -138,15 +138,15 @@ namespace vcpkg::Commands::Install
return SortedVector<std::string>(std::move(installed_files));
}
- static void print_plan(const std::vector<PackageSpecWithInstallPlan>& plan)
+ static void print_plan(const std::vector<InstallPlanAction>& plan)
{
- std::vector<const PackageSpecWithInstallPlan*> already_installed;
- std::vector<const PackageSpecWithInstallPlan*> build_and_install;
- std::vector<const PackageSpecWithInstallPlan*> install;
+ std::vector<const InstallPlanAction*> already_installed;
+ std::vector<const InstallPlanAction*> build_and_install;
+ std::vector<const InstallPlanAction*> install;
- for (const PackageSpecWithInstallPlan& i : plan)
+ for (const InstallPlanAction& i : plan)
{
- switch (i.plan.plan_type)
+ switch (i.plan_type)
{
case InstallPlanType::ALREADY_INSTALLED:
already_installed.push_back(&i);
@@ -162,23 +162,23 @@ namespace vcpkg::Commands::Install
}
}
- auto print_lambda = [](const PackageSpecWithInstallPlan* p) { return to_output_string(p->plan.request_type, p->spec.to_string()); };
+ auto print_lambda = [](const InstallPlanAction* p) { return Dependencies::to_output_string(p->request_type, p->spec.to_string()); };
if (!already_installed.empty())
{
- std::sort(already_installed.begin(), already_installed.end(), &PackageSpecWithInstallPlan::compare_by_name);
+ std::sort(already_installed.begin(), already_installed.end(), &InstallPlanAction::compare_by_name);
System::println("The following packages are already installed:\n%s", Strings::join("\n", already_installed, print_lambda));
}
if (!build_and_install.empty())
{
- std::sort(build_and_install.begin(), build_and_install.end(), &PackageSpecWithInstallPlan::compare_by_name);
+ std::sort(build_and_install.begin(), build_and_install.end(), &InstallPlanAction::compare_by_name);
System::println("The following packages will be built and installed:\n%s", Strings::join("\n", build_and_install, print_lambda));
}
if (!install.empty())
{
- std::sort(install.begin(), install.end(), &PackageSpecWithInstallPlan::compare_by_name);
+ std::sort(install.begin(), install.end(), &InstallPlanAction::compare_by_name);
System::println("The following packages will be installed:\n%s", Strings::join("\n", install, print_lambda));
}
}
@@ -250,7 +250,7 @@ namespace vcpkg::Commands::Install
// create the plan
StatusParagraphs status_db = database_load_check(paths);
- std::vector<PackageSpecWithInstallPlan> install_plan = Dependencies::create_install_plan(paths, specs, status_db);
+ std::vector<InstallPlanAction> install_plan = Dependencies::create_install_plan(paths, specs, status_db);
Checks::check_exit(VCPKG_LINE_INFO, !install_plan.empty(), "Install plan cannot be empty");
// log the plan
@@ -264,9 +264,9 @@ namespace vcpkg::Commands::Install
print_plan(install_plan);
- const bool has_non_user_requested_packages = std::find_if(install_plan.cbegin(), install_plan.cend(), [](const PackageSpecWithInstallPlan& package)-> bool
+ const bool has_non_user_requested_packages = std::find_if(install_plan.cbegin(), install_plan.cend(), [](const InstallPlanAction& package)-> bool
{
- return package.plan.request_type != RequestType::USER_REQUESTED;
+ return package.request_type != RequestType::USER_REQUESTED;
}) != install_plan.cend();
if (has_non_user_requested_packages)
@@ -280,13 +280,13 @@ namespace vcpkg::Commands::Install
}
// execute the plan
- for (const PackageSpecWithInstallPlan& action : install_plan)
+ for (const InstallPlanAction& action : install_plan)
{
const std::string display_name = action.spec.to_string();
try
{
- switch (action.plan.plan_type)
+ switch (action.plan_type)
{
case InstallPlanType::ALREADY_INSTALLED:
System::println(System::Color::success, "Package %s is already installed", display_name);
@@ -294,7 +294,7 @@ namespace vcpkg::Commands::Install
case InstallPlanType::BUILD_AND_INSTALL:
{
System::println("Building package %s... ", display_name);
- const Build::BuildResult result = Commands::Build::build_package(action.plan.source_pgh.value_or_exit(VCPKG_LINE_INFO),
+ const Build::BuildResult result = Commands::Build::build_package(action.any_paragraph.source_paragraph.value_or_exit(VCPKG_LINE_INFO),
action.spec,
paths,
paths.port_dir(action.spec),
@@ -315,7 +315,7 @@ namespace vcpkg::Commands::Install
}
case InstallPlanType::INSTALL:
System::println("Installing package %s... ", display_name);
- install_package(paths, action.plan.binary_pgh.value_or_exit(VCPKG_LINE_INFO), &status_db);
+ install_package(paths, action.any_paragraph.binary_paragraph.value_or_exit(VCPKG_LINE_INFO), &status_db);
System::println(System::Color::success, "Installing package %s... done", display_name);
break;
case InstallPlanType::UNKNOWN:

MODIFY
src/vcpkg_Dependencies.cpp
src/vcpkg_Dependencies.cpp
namespace vcpkg::Dependencies
{
- std::vector<PackageSpec> AnyParagraph::dependencies() const
+ 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, this->spec.triplet()).value_or_exit(VCPKG_LINE_INFO);
+ return PackageSpec::from_name_and_triplet(s, triplet).value_or_exit(VCPKG_LINE_INFO);
});
};
@@ -33,10 +33,10 @@ namespace vcpkg::Dependencies
if (auto p = this->source_paragraph.get())
{
- return to_package_specs(filter_dependencies(p->depends, this->spec.triplet()));
+ return to_package_specs(filter_dependencies(p->depends, triplet));
}
- Checks::exit_with_message(VCPKG_LINE_INFO, "Cannot get dependencies for package %s because there was none of: source/binary/status paragraphs", spec.to_string());
+ 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)
@@ -52,52 +52,44 @@ namespace vcpkg::Dependencies
}
}
- InstallPlanAction::InstallPlanAction() : plan_type(InstallPlanType::UNKNOWN)
- , request_type(RequestType::UNKNOWN)
- , binary_pgh(nullopt)
- , source_pgh(nullopt) { }
+ InstallPlanAction::InstallPlanAction() : spec()
+ , any_paragraph()
+ , plan_type(InstallPlanType::UNKNOWN)
+ , request_type(RequestType::UNKNOWN) { }
- InstallPlanAction::InstallPlanAction(const AnyParagraph& any_paragraph, const RequestType& request_type) : InstallPlanAction()
+ InstallPlanAction::InstallPlanAction(const PackageSpec& spec, const AnyParagraph& any_paragraph, const RequestType& request_type) : InstallPlanAction()
{
+ this->spec = spec;
this->request_type = request_type;
- if (any_paragraph.status_paragraph.get())
+ 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->binary_pgh = *p;
+ this->any_paragraph.binary_paragraph = *p;
return;
}
if (auto p = any_paragraph.source_paragraph.get())
{
this->plan_type = InstallPlanType::BUILD_AND_INSTALL;
- this->source_pgh = *p;
+ this->any_paragraph.source_paragraph = *p;
return;
}
this->plan_type = InstallPlanType::UNKNOWN;
}
- InstallPlanAction::InstallPlanAction(const InstallPlanType& plan_type, const RequestType& request_type, Optional<BinaryParagraph> binary_pgh, Optional<SourceParagraph> source_pgh)
- : plan_type(std::move(plan_type))
- , request_type(request_type)
- , binary_pgh(std::move(binary_pgh))
- , source_pgh(std::move(source_pgh)) { }
-
- bool PackageSpecWithInstallPlan::compare_by_name(const PackageSpecWithInstallPlan* left, const PackageSpecWithInstallPlan* right)
+ bool InstallPlanAction::compare_by_name(const InstallPlanAction* left, const InstallPlanAction* right)
{
return left->spec.name() < right->spec.name();
}
- PackageSpecWithInstallPlan::PackageSpecWithInstallPlan(const PackageSpec& spec, InstallPlanAction&& plan)
- : spec(spec)
- , plan(std::move(plan)) { }
-
RemovePlanAction::RemovePlanAction() : plan_type(RemovePlanType::UNKNOWN)
, request_type(RequestType::UNKNOWN) { }
@@ -114,63 +106,48 @@ namespace vcpkg::Dependencies
: spec(spec)
, plan(std::move(plan)) { }
- std::vector<PackageSpecWithInstallPlan> create_install_plan(const VcpkgPaths& paths, const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db)
+ std::vector<InstallPlanAction> create_install_plan(const VcpkgPaths& paths, const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db)
{
- struct InstallAdjacencyProvider final : Graphs::AdjacencyProvider<PackageSpec, AnyParagraph>
+ struct InstallAdjacencyProvider final : Graphs::AdjacencyProvider<PackageSpec, InstallPlanAction>
{
const VcpkgPaths& paths;
const StatusParagraphs& status_db;
+ const std::unordered_set<PackageSpec>& specs_as_set;
- InstallAdjacencyProvider(const VcpkgPaths& p, const StatusParagraphs& s) : paths(p)
- , status_db(s) {}
+ InstallAdjacencyProvider(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 AnyParagraph& p) const override
+ std::vector<PackageSpec> adjacency_list(const InstallPlanAction& p) const override
{
- if (p.status_paragraph.get())
+ if (p.any_paragraph.status_paragraph.get())
return std::vector<PackageSpec>{};
- return p.dependencies();
+ return p.any_paragraph.dependencies(p.spec.triplet());
}
- AnyParagraph load_vertex_data(const PackageSpec& spec) const override
+ 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 { spec, *it->get(), nullopt, nullopt };
+ return InstallPlanAction{ spec, { *it->get(), nullopt, nullopt }, request_type };
Expected<BinaryParagraph> maybe_bpgh = Paragraphs::try_load_cached_package(paths, spec);
if (auto bpgh = maybe_bpgh.get())
- return { spec, nullopt, *bpgh, nullopt };
+ return InstallPlanAction{ spec, {nullopt, *bpgh, nullopt}, request_type };
Expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(paths.port_dir(spec));
if (auto spgh = maybe_spgh.get())
- return { spec, nullopt, nullopt, *spgh };
+ return InstallPlanAction{ spec, {nullopt, nullopt, *spgh}, request_type };
- return { spec , nullopt, nullopt, nullopt };
+ return InstallPlanAction{ spec , {nullopt, nullopt, nullopt}, request_type };
}
};
- const std::vector<AnyParagraph> toposort = Graphs::topological_sort(specs, InstallAdjacencyProvider{ paths, status_db });
-
const std::unordered_set<PackageSpec> specs_as_set(specs.cbegin(), specs.cend());
- std::vector<PackageSpecWithInstallPlan> ret;
- for (const AnyParagraph& pkg : toposort)
- {
- auto spec = pkg.spec;
- const RequestType request_type = specs_as_set.find(spec) != specs_as_set.end() ? RequestType::USER_REQUESTED : RequestType::AUTO_SELECTED;
- if (pkg.status_paragraph && request_type != RequestType::USER_REQUESTED)
- continue;
- InstallPlanAction a(pkg, request_type);
- ret.push_back(PackageSpecWithInstallPlan(spec, std::move(a)));
- }
- return ret;
+ return Graphs::topological_sort(specs, InstallAdjacencyProvider{ paths, status_db, specs_as_set });
}
- struct SpecAndRemovePlanType
- {
- PackageSpec spec;
- RemovePlanType plan_type;
- };
-
std::vector<PackageSpecWithRemovePlan> create_remove_plan(const std::vector<PackageSpec>& specs, const StatusParagraphs& status_db)
{
struct RemoveAdjacencyProvider final : Graphs::AdjacencyProvider<PackageSpec, SpecAndRemovePlanType>

Simplify Install plan generation