Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
This commit is contained in:
1 parent
3cac890ce9
commit
ab64147f9d
114 files changed
+23777
-1392
No files matched your search
@@ -0,0 +1,342 @@
|
||||
#include <Void/Build/Package/Solver.hpp>
|
||||
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Text/Str.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));
|
||||
}
|
||||
|
||||
bool all_digits(std::string_view s) { return Void::Str::is_digit_ascii(s); }
|
||||
|
||||
std::string normalize_semver_token(std::string_view tok) {
|
||||
if (tok.empty() || tok == "*") {
|
||||
return std::string{tok};
|
||||
}
|
||||
std::string_view op;
|
||||
if (tok.starts_with(">=") || tok.starts_with("<=")) {
|
||||
op = tok.substr(0, 2);
|
||||
tok.remove_prefix(2);
|
||||
} else if (tok.starts_with('^') || tok.starts_with('~') || tok.starts_with('>') ||
|
||||
tok.starts_with('<') || tok.starts_with('=')) {
|
||||
op = tok.substr(0, 1);
|
||||
tok.remove_prefix(1);
|
||||
}
|
||||
|
||||
const auto meta_pos = tok.find_first_of("-+");
|
||||
const auto core = meta_pos == std::string_view::npos ? tok : tok.substr(0, meta_pos);
|
||||
const auto meta =
|
||||
meta_pos == std::string_view::npos ? std::string_view{} : tok.substr(meta_pos);
|
||||
|
||||
if (core.find_first_of("xX*") != std::string_view::npos) {
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
|
||||
const auto dot1 = core.find('.');
|
||||
if (dot1 == std::string_view::npos) {
|
||||
if (!all_digits(core)) {
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
if ((op == "~" || op == "^") && meta.empty()) {
|
||||
const unsigned long maj = std::stoul(std::string{core});
|
||||
if (op == "~" || maj == 0) {
|
||||
return std::format(">={}.0.0 <{}.0.0", maj, maj + 1);
|
||||
}
|
||||
}
|
||||
return std::string{op} + std::string{core} + ".0.0" + std::string{meta};
|
||||
}
|
||||
const auto dot2 = core.find('.', dot1 + 1);
|
||||
if (dot2 == std::string_view::npos) {
|
||||
const auto p1 = core.substr(0, dot1);
|
||||
const auto p2 = core.substr(dot1 + 1);
|
||||
if (!all_digits(p1) || !all_digits(p2)) {
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
return std::string{op} + std::string{p1} + "." + std::string{p2} + ".0" + std::string{meta};
|
||||
}
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
|
||||
// Adapter for shorthand SemVer comparator tokens (^1, ^1.0, ~1.2, <2) since
|
||||
// pinned Void::Semver::parse_range requires full major.minor.patch triplets.
|
||||
std::optional<Void::Semver::VersionRange> parse_semver_range_compat(std::string_view expr) {
|
||||
if (auto direct = Void::Semver::parse_range(expr); direct) {
|
||||
return direct;
|
||||
}
|
||||
std::string normalized;
|
||||
std::size_t pos = 0;
|
||||
bool first_clause = true;
|
||||
while (pos <= expr.size()) {
|
||||
const auto sep = expr.find("||", pos);
|
||||
const auto clause = expr.substr(pos, sep == std::string_view::npos ? sep : sep - pos);
|
||||
if (!first_clause) {
|
||||
normalized += " || ";
|
||||
}
|
||||
first_clause = false;
|
||||
|
||||
bool first_tok = true;
|
||||
std::size_t i = 0;
|
||||
while (i < clause.size()) {
|
||||
while (i < clause.size() && (clause[i] == ' ' || clause[i] == '\t')) {
|
||||
++i;
|
||||
}
|
||||
if (i == clause.size()) {
|
||||
break;
|
||||
}
|
||||
auto end = clause.find_first_of(" \t", i);
|
||||
if (end == std::string_view::npos) {
|
||||
end = clause.size();
|
||||
}
|
||||
if (!first_tok) {
|
||||
normalized.push_back(' ');
|
||||
}
|
||||
first_tok = false;
|
||||
normalized += normalize_semver_token(clause.substr(i, end - i));
|
||||
i = end;
|
||||
}
|
||||
if (sep == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
pos = sep + 2;
|
||||
}
|
||||
return Void::Semver::parse_range(normalized);
|
||||
}
|
||||
|
||||
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.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
|
||||
Reference in new issue
Block a user