254 lines
9.3 KiB
C++
254 lines
9.3 KiB
C++
#include <Void/Build/Package/Solver.hpp>
|
|
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
namespace Void::Build::Package {
|
|
namespace {
|
|
|
|
Void::Result::Error solve_err(std::string msg) {
|
|
return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
|
|
std::move(msg));
|
|
}
|
|
|
|
// Delegates directly to Void::Semver::parse_range (VL-013 shorthand comparators now upstream).
|
|
std::optional<Void::Semver::VersionRange> parse_semver_range_compat(std::string_view expr) {
|
|
return Void::Semver::parse_range(expr);
|
|
}
|
|
|
|
struct ActiveConstraint {
|
|
Void::Semver::VersionRange range;
|
|
std::string range_expr;
|
|
std::string requirer;
|
|
std::vector<std::string> features;
|
|
};
|
|
|
|
// PubGrub-style conflict-driven backtracking solver with incompatibility explanation trace.
|
|
class PubGrubSolver {
|
|
public:
|
|
PubGrubSolver(const PackageIndex &index, const SolveOptions &options)
|
|
: index_{index}, options_{options} {}
|
|
|
|
Void::Result::Result<SolveResult, Void::Result::Error>
|
|
solve(const std::vector<DependencyConstraint> &roots) {
|
|
std::map<PackageId, std::vector<ActiveConstraint>> constraints;
|
|
for (const auto &rc : roots) {
|
|
auto parsed = parse_semver_range_compat(rc.range_expr);
|
|
if (!parsed) {
|
|
return std::unexpected{
|
|
solve_err(std::format("invalid version range '{}' for '{}' (from {})",
|
|
rc.range_expr, rc.target.format(), rc.origin_label))};
|
|
}
|
|
constraints[rc.target].push_back(ActiveConstraint{
|
|
*parsed, rc.range_expr, rc.origin_label.empty() ? "root" : rc.origin_label,
|
|
rc.required_features});
|
|
}
|
|
|
|
std::map<PackageId, ReleaseRecord> chosen;
|
|
if (backtrack(constraints, chosen)) {
|
|
SolveResult out;
|
|
for (const auto &[id, rel] : chosen) {
|
|
SolvedPackage sp;
|
|
sp.id = id;
|
|
sp.version = rel.version;
|
|
sp.source_sha256 = rel.source_sha256;
|
|
sp.source_url = rel.source_url;
|
|
sp.signature = rel.signature;
|
|
sp.public_key = rel.public_key;
|
|
sp.binaries = rel.binaries;
|
|
if (const auto cit = constraints.find(id); cit != constraints.end()) {
|
|
for (const auto &ac : cit->second) {
|
|
for (const auto &f : ac.features) {
|
|
sp.enabled_features.insert(f);
|
|
}
|
|
}
|
|
}
|
|
for (const auto &dep : rel.dependencies) {
|
|
sp.dependencies.push_back(dep.target);
|
|
}
|
|
std::ranges::sort(sp.dependencies);
|
|
sp.dependencies.erase(std::unique(sp.dependencies.begin(), sp.dependencies.end()),
|
|
sp.dependencies.end());
|
|
out.packages.emplace(id, std::move(sp));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string report = "dependency version resolution failed:\n";
|
|
if (conflicts_.empty()) {
|
|
report += " no satisfiable version assignment found within solver step budget";
|
|
} else {
|
|
const std::size_t shown = std::min<std::size_t>(conflicts_.size(), 8);
|
|
for (std::size_t i = 0; i < shown; ++i) {
|
|
report += " - " + conflicts_[i] + "\n";
|
|
}
|
|
}
|
|
return std::unexpected{solve_err(std::move(report))};
|
|
}
|
|
|
|
private:
|
|
bool matches_all(const Void::Semver::Version &v,
|
|
const std::vector<ActiveConstraint> &list) const {
|
|
for (const auto &c : list) {
|
|
if (!c.range.contains(v)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::vector<ReleaseRecord> ordered_candidates(const PackageId &id,
|
|
const std::vector<ActiveConstraint> &list) const {
|
|
auto all = index_.releases_for(id);
|
|
std::vector<ReleaseRecord> matching;
|
|
const auto pin_it = options_.pinned_versions.find(id);
|
|
|
|
for (auto &rel : all) {
|
|
const bool is_pinned =
|
|
(pin_it != options_.pinned_versions.end() && rel.version == pin_it->second);
|
|
if (rel.yanked && !(is_pinned && options_.allow_yanked_if_pinned)) {
|
|
continue;
|
|
}
|
|
if (matches_all(rel.version, list)) {
|
|
matching.push_back(std::move(rel));
|
|
}
|
|
}
|
|
|
|
// Deterministic priority: pinned compatible version first, then highest SemVer descending.
|
|
std::ranges::sort(matching, [&](const ReleaseRecord &a, const ReleaseRecord &b) {
|
|
const bool a_pin =
|
|
(pin_it != options_.pinned_versions.end() && a.version == pin_it->second);
|
|
const bool b_pin =
|
|
(pin_it != options_.pinned_versions.end() && b.version == pin_it->second);
|
|
if (a_pin != b_pin) {
|
|
return a_pin;
|
|
}
|
|
return a.version > b.version;
|
|
});
|
|
return matching;
|
|
}
|
|
|
|
void record_incompatibility(const PackageId &id, const std::vector<ActiveConstraint> &list) {
|
|
std::string msg = "no version of " + id.format() + " satisfies: ";
|
|
for (std::size_t i = 0; i < list.size(); ++i) {
|
|
if (i > 0) {
|
|
msg += " AND ";
|
|
}
|
|
msg += list[i].range_expr + " (from " + list[i].requirer + ")";
|
|
}
|
|
if (conflicts_.size() < 32 && !std::ranges::contains(conflicts_, msg)) {
|
|
conflicts_.push_back(std::move(msg));
|
|
}
|
|
}
|
|
|
|
bool backtrack(std::map<PackageId, std::vector<ActiveConstraint>> &constraints,
|
|
std::map<PackageId, ReleaseRecord> &chosen) {
|
|
if (++steps_ > options_.max_steps) {
|
|
return false;
|
|
}
|
|
|
|
// Unit propagation / fail-fast check: choose undecided package with fewest candidates!
|
|
std::optional<PackageId> next_pkg;
|
|
std::vector<ReleaseRecord> best_candidates;
|
|
std::size_t min_count = std::numeric_limits<std::size_t>::max();
|
|
|
|
for (const auto &[id, clist] : constraints) {
|
|
if (const auto cit = chosen.find(id); cit != chosen.end()) {
|
|
if (!matches_all(cit->second.version, clist)) {
|
|
record_incompatibility(id, clist);
|
|
return false;
|
|
}
|
|
continue;
|
|
}
|
|
auto cands = ordered_candidates(id, clist);
|
|
if (cands.empty()) {
|
|
record_incompatibility(id, clist);
|
|
return false;
|
|
}
|
|
if (cands.size() < min_count) {
|
|
min_count = cands.size();
|
|
next_pkg = id;
|
|
best_candidates = std::move(cands);
|
|
if (min_count == 1) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!next_pkg) {
|
|
return true; // All constrained packages decided and mutually consistent!
|
|
}
|
|
|
|
const PackageId pkg = *next_pkg;
|
|
for (const auto &candidate : best_candidates) {
|
|
// Save constraints snapshot before descending.
|
|
auto next_constraints = constraints;
|
|
bool valid_ranges = true;
|
|
const std::string origin = pkg.format() + "@" + candidate.version.to_string();
|
|
|
|
for (const auto &dep : candidate.dependencies) {
|
|
auto parsed = parse_semver_range_compat(dep.range_expr);
|
|
if (!parsed) {
|
|
valid_ranges = false;
|
|
conflicts_.push_back(
|
|
std::format("package {} declares invalid range '{}' for {}", origin,
|
|
dep.range_expr, dep.target.format()));
|
|
break;
|
|
}
|
|
next_constraints[dep.target].push_back(
|
|
ActiveConstraint{*parsed, dep.range_expr, origin, dep.required_features});
|
|
}
|
|
if (!valid_ranges) {
|
|
continue;
|
|
}
|
|
|
|
chosen.insert_or_assign(pkg, candidate);
|
|
if (backtrack(next_constraints, chosen)) {
|
|
constraints = std::move(next_constraints);
|
|
return true;
|
|
}
|
|
chosen.erase(pkg);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const PackageIndex &index_;
|
|
const SolveOptions &options_;
|
|
std::size_t steps_{0};
|
|
std::vector<std::string> conflicts_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
std::string PackageId::format() const {
|
|
if (registry_authority.empty()) {
|
|
return name;
|
|
}
|
|
return registry_authority + "/" + name;
|
|
}
|
|
|
|
void PackageIndex::add_release(ReleaseRecord release) {
|
|
releases_[release.id].push_back(std::move(release));
|
|
}
|
|
|
|
std::vector<ReleaseRecord> PackageIndex::releases_for(const PackageId &id) const {
|
|
const auto it = releases_.find(id);
|
|
if (it == releases_.end()) {
|
|
return {};
|
|
}
|
|
return it->second;
|
|
}
|
|
|
|
Void::Result::Result<SolveResult, Void::Result::Error>
|
|
solve_dependencies(const std::vector<DependencyConstraint> &root_constraints,
|
|
const PackageIndex &index, const SolveOptions &options) {
|
|
PubGrubSolver solver{index, options};
|
|
return solver.solve(root_constraints);
|
|
}
|
|
|
|
} // namespace Void::Build::Package
|