From 33a2969b9c80c602a28e0d53560f39915de933fc Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 22 Sep 2016 23:28:50 -0700 Subject: Refactor dependency related code to vcpkg::Dependencies --- toolsrc/src/commands_installation.cpp | 14 ++++---- toolsrc/src/vcpkg_Dependencies.cpp | 68 +++++++++++++++++++++++++++++++++++ toolsrc/src/vcpkg_cmd_arguments.cpp | 41 --------------------- 3 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 toolsrc/src/vcpkg_Dependencies.cpp (limited to 'toolsrc/src') diff --git a/toolsrc/src/commands_installation.cpp b/toolsrc/src/commands_installation.cpp index 36f26fdab..7004307b3 100644 --- a/toolsrc/src/commands_installation.cpp +++ b/toolsrc/src/commands_installation.cpp @@ -6,6 +6,7 @@ #include "vcpkg_Files.h" #include "post_build_lint.h" #include "vcpkg_System.h" +#include "vcpkg_Dependencies.h" namespace vcpkg { @@ -59,18 +60,19 @@ namespace vcpkg { StatusParagraphs status_db = database_load_check(paths); - std::vector specs = args.extract_package_specs_with_unmet_dependencies(paths, default_target_triplet, status_db); - Checks::check_exit(!specs.empty(), "Specs cannot be empty"); - std::string specs_string = to_string(specs[0]); - for (size_t i = 1; i < specs.size(); ++i) + std::vector specs = args.parse_all_arguments_as_package_specs(default_target_triplet); + std::vector install_plan = Dependencies::create_dependency_ordered_install_plan(paths, specs, status_db); + Checks::check_exit(!install_plan.empty(), "Install plan cannot be empty"); + std::string specs_string = to_string(install_plan[0]); + for (size_t i = 1; i < install_plan.size(); ++i) { specs_string.push_back(','); - specs_string.append(to_string(specs[i])); + specs_string.append(to_string(install_plan[i])); } TrackProperty("installplan", specs_string); Environment::ensure_utilities_on_path(paths); - for (const package_spec& spec : specs) + for (const package_spec& spec : install_plan) { if (status_db.find_installed(spec.name, spec.target_triplet) != status_db.end()) { diff --git a/toolsrc/src/vcpkg_Dependencies.cpp b/toolsrc/src/vcpkg_Dependencies.cpp new file mode 100644 index 000000000..751b503c0 --- /dev/null +++ b/toolsrc/src/vcpkg_Dependencies.cpp @@ -0,0 +1,68 @@ +#include "vcpkg_Dependencies.h" +#include +#include "vcpkg_Graphs.h" +#include "vcpkg_paths.h" +#include "package_spec.h" +#include "StatusParagraphs.h" +#include +#include "vcpkg.h" +#include "vcpkg_Maps.h" +#include "vcpkg_Sets.h" + +namespace vcpkg { namespace Dependencies +{ + static Graphs::Graph build_dependency_graph(const vcpkg_paths& paths, const std::vector& specs, const StatusParagraphs& status_db) + { + std::vector examine_stack(specs); + std::unordered_set was_examined; // Examine = we have checked its immediate (non-recursive) dependencies + Graphs::Graph graph; + graph.add_vertices(examine_stack); + + while (!examine_stack.empty()) + { + package_spec spec = examine_stack.back(); + examine_stack.pop_back(); + + if (was_examined.find(spec) != was_examined.end()) + { + continue; + } + + std::vector dependencies_as_string = get_unmet_package_dependencies(paths, spec, status_db); + + for (const std::string& dep_as_string : dependencies_as_string) + { + package_spec current_dep = {dep_as_string, spec.target_triplet}; + auto it = status_db.find(current_dep.name, current_dep.target_triplet); + if (it != status_db.end() && (*it)->want == want_t::install) + { + continue; + } + + graph.add_edge(spec, current_dep); + if (was_examined.find(current_dep) == was_examined.end()) + { + examine_stack.push_back(std::move(current_dep)); + } + } + + was_examined.insert(spec); + } + + return graph; + } + + std::vector create_dependency_ordered_install_plan(const vcpkg_paths& paths, const std::vector& specs, const StatusParagraphs& status_db) + { + return build_dependency_graph(paths, specs, status_db).find_topological_sort(); + } + + std::unordered_set find_unmet_dependencies(const vcpkg_paths& paths, const std::vector& specs, const StatusParagraphs& status_db) + { + const Graphs::Graph dependency_graph = build_dependency_graph(paths, specs, status_db); + std::unordered_set key_set = Maps::extract_key_set(dependency_graph.adjacency_list()); + Sets::remove_all(&key_set, specs); + + return key_set; + } +}} diff --git a/toolsrc/src/vcpkg_cmd_arguments.cpp b/toolsrc/src/vcpkg_cmd_arguments.cpp index 4cfc12716..3605503b0 100644 --- a/toolsrc/src/vcpkg_cmd_arguments.cpp +++ b/toolsrc/src/vcpkg_cmd_arguments.cpp @@ -179,47 +179,6 @@ namespace vcpkg } } - std::vector vcpkg_cmd_arguments::extract_package_specs_with_unmet_dependencies(const vcpkg_paths& paths, const triplet& default_target_triplet, const StatusParagraphs& status_db) const - { - std::vector specs = parse_all_arguments_as_package_specs(default_target_triplet); - std::unordered_set had_its_immediate_dependencies_added; - Graphs::Graph graph; - graph.add_vertices(specs); - - while (!specs.empty()) - { - package_spec spec = specs.back(); - specs.pop_back(); - - if (had_its_immediate_dependencies_added.find(spec) != had_its_immediate_dependencies_added.end()) - { - continue; - } - - std::vector dependencies_as_string = get_unmet_package_dependencies(paths, spec, status_db); - - for (const std::string& dep_as_string : dependencies_as_string) - { - package_spec current_dep = {dep_as_string, spec.target_triplet}; - auto it = status_db.find(current_dep.name, current_dep.target_triplet); - if (it != status_db.end() && (*it)->want == want_t::install) - { - continue; - } - - graph.add_edge(spec, current_dep); - if (had_its_immediate_dependencies_added.find(current_dep) == had_its_immediate_dependencies_added.end()) - { - specs.push_back(std::move(current_dep)); - } - } - - had_its_immediate_dependencies_added.insert(spec); - } - - return graph.find_topological_sort(); - } - std::vector vcpkg_cmd_arguments::parse_all_arguments_as_package_specs(const triplet& default_target_triplet, const char* example_text) const { size_t arg_count = command_arguments.size(); -- cgit v1.2.3 From a26c88c754c14e2fd059f2ddbd69802b875b77ea Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 22 Sep 2016 23:53:13 -0700 Subject: `vcpkg build` will now show helpful error if dependencies are missing --- toolsrc/src/commands_installation.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'toolsrc/src') diff --git a/toolsrc/src/commands_installation.cpp b/toolsrc/src/commands_installation.cpp index 7004307b3..29233c5d9 100644 --- a/toolsrc/src/commands_installation.cpp +++ b/toolsrc/src/commands_installation.cpp @@ -113,7 +113,23 @@ namespace vcpkg void build_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet) { + StatusParagraphs status_db = database_load_check(paths); + std::vector specs = args.parse_all_arguments_as_package_specs(default_target_triplet); + std::unordered_set unmet_dependencies = Dependencies::find_unmet_dependencies(paths, specs, status_db); + if (!unmet_dependencies.empty()) + { + 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 package_spec& p : unmet_dependencies) + { + System::println(" %s", p.name); + } + System::println(""); + exit(EXIT_FAILURE); + } + Environment::ensure_utilities_on_path(paths); for (const package_spec& spec : specs) { -- cgit v1.2.3 From 5b89712df01c96242ced20c38f0fa27631c3f4e3 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Fri, 23 Sep 2016 00:02:51 -0700 Subject: Restruct `vcpkg build` to 1 package --- toolsrc/src/commands_installation.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'toolsrc/src') diff --git a/toolsrc/src/commands_installation.cpp b/toolsrc/src/commands_installation.cpp index 29233c5d9..35c78db8f 100644 --- a/toolsrc/src/commands_installation.cpp +++ b/toolsrc/src/commands_installation.cpp @@ -113,6 +113,10 @@ namespace vcpkg void build_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet) { + // Currently the code won't work for multiple packages if one of them depends on another. + // Allowing only 1 package for now. + args.check_max_args(1); + StatusParagraphs status_db = database_load_check(paths); std::vector specs = args.parse_all_arguments_as_package_specs(default_target_triplet); -- cgit v1.2.3