291 lines
13 KiB
C++
291 lines
13 KiB
C++
#include <Void/Build/Package/Resolver.hpp>
|
|
|
|
#include <Void/Build/Package/Archive.hpp>
|
|
#include <Void/Build/Provenance/Verification.hpp>
|
|
#include <Void/Build/Util/Sha256.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
#include <Void/Text/Semver.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <map>
|
|
#include <queue>
|
|
#include <set>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Build::Package {
|
|
|
|
Void::Result::Result<ResolvedDepMaterialization, Void::Result::Error>
|
|
resolve_and_materialize_dependencies(const ProfileContext &ctx,
|
|
const std::vector<NormalizedDependency> &merged_deps,
|
|
const ResolveOptions &options) {
|
|
const std::string ctx_key = make_lock_context_key(ctx, "target");
|
|
const std::string fp = compute_declaration_fingerprint(ctx, "target", merged_deps);
|
|
|
|
ResolvedDepMaterialization out;
|
|
out.lock_context.context_key = ctx_key;
|
|
out.lock_context.profile_name = ctx.name;
|
|
out.lock_context.compiler = std::string{compiler_name(ctx.compiler)};
|
|
out.lock_context.os = ctx.os;
|
|
out.lock_context.architecture = ctx.architecture;
|
|
out.lock_context.machine_role = "target";
|
|
out.lock_context.declaration_fingerprint = fp;
|
|
|
|
// Collect existing lock pins (used unless an upgrade was requested).
|
|
SolveOptions solve_opts;
|
|
std::map<PackageId, LockedSourceEntry> locked_pkg_entries;
|
|
if (!options.allow_upgrade && options.pinned_lock &&
|
|
options.pinned_lock->document.contexts.contains(ctx_key)) {
|
|
for (const auto &entry : options.pinned_lock->document.contexts.at(ctx_key).entries) {
|
|
if (entry.source_kind == "package" && !entry.package_name.empty()) {
|
|
const PackageId pid{entry.registry_authority, entry.package_name};
|
|
locked_pkg_entries[pid] = entry;
|
|
if (auto ver = Void::Semver::parse_strict(entry.version); ver) {
|
|
solve_opts.pinned_versions[pid] = *ver;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 1. Solve all registry package dependencies together via PubGrub.
|
|
std::vector<DependencyConstraint> root_constraints;
|
|
std::map<std::string, PackageId, std::less<>> alias_to_pkg_id;
|
|
std::vector<const NormalizedDependency *> package_deps;
|
|
|
|
for (const auto &nd : merged_deps) {
|
|
if (nd.record.source_kind != DependencySourceKind::Package) {
|
|
continue;
|
|
}
|
|
const std::string reg_url = nd.record.registry_authority.empty()
|
|
? options.default_registry_url
|
|
: nd.record.registry_authority;
|
|
if (reg_url.empty()) {
|
|
return std::unexpected{Void::System::make_system_error(
|
|
Void::System::SystemErrorCode::invalid_argument,
|
|
std::format("package dependency '{}' requires .registry(url) or "
|
|
"VBS_REGISTRY_URL",
|
|
nd.record.name))};
|
|
}
|
|
const std::string pkg_name = nd.record.package.empty() ? nd.record.name : nd.record.package;
|
|
const PackageId pid{reg_url, pkg_name};
|
|
alias_to_pkg_id[nd.record.name] = pid;
|
|
package_deps.push_back(&nd);
|
|
root_constraints.push_back(
|
|
DependencyConstraint{pid, nd.record.version.empty() ? ">=0.0.0" : nd.record.version,
|
|
nd.record.features, "root(" + nd.record.name + ")"});
|
|
}
|
|
|
|
std::map<PackageId, SolvedPackage> solved_packages;
|
|
if (!root_constraints.empty()) {
|
|
Void::Log::info("Deps", "Solving {} registry package constraint(s)...",
|
|
root_constraints.size());
|
|
bool all_locked_cached = options.offline;
|
|
const auto pkg_cache_root = options.build_root / ".vbs" / "cache" / "packages";
|
|
if (all_locked_cached) {
|
|
for (const auto *nd : package_deps) {
|
|
const auto &pid = alias_to_pkg_id.at(nd->record.name);
|
|
const auto it = locked_pkg_entries.find(pid);
|
|
if (it == locked_pkg_entries.end() || it->second.sha256.empty() ||
|
|
!std::filesystem::is_directory(pkg_cache_root / it->second.sha256 / "src")) {
|
|
all_locked_cached = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (all_locked_cached) {
|
|
Void::Log::info("Deps", "Using offline cached locked package graph");
|
|
for (const auto *nd : package_deps) {
|
|
const auto &pid = alias_to_pkg_id.at(nd->record.name);
|
|
const auto &le = locked_pkg_entries.at(pid);
|
|
auto parsed_ver = Void::Semver::parse_strict(le.version);
|
|
if (!parsed_ver) {
|
|
return std::unexpected{Void::System::make_system_error(
|
|
Void::System::SystemErrorCode::invalid_argument,
|
|
std::format("corrupt locked version '{}' for package '{}'", le.version,
|
|
pid.name))};
|
|
}
|
|
SolvedPackage sp;
|
|
sp.id = pid;
|
|
sp.version = *parsed_ver;
|
|
sp.source_sha256 = le.sha256;
|
|
solved_packages[pid] = std::move(sp);
|
|
}
|
|
} else {
|
|
if (options.offline) {
|
|
return std::unexpected{Void::System::make_system_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
"offline mode cannot query live package registry; populate cache first")};
|
|
}
|
|
PackageIndex index;
|
|
std::set<PackageId> fetched_ids;
|
|
std::queue<PackageId> pending;
|
|
for (const auto &c : root_constraints) {
|
|
if (fetched_ids.insert(c.target).second) {
|
|
pending.push(c.target);
|
|
}
|
|
}
|
|
while (!pending.empty()) {
|
|
const auto pid = pending.front();
|
|
pending.pop();
|
|
Void::Log::debug("Deps", "Fetching release index for '{}' from {}", pid.name,
|
|
pid.registry_authority);
|
|
auto releases =
|
|
fetch_package_releases(pid.registry_authority, pid.name, options.registry_auth);
|
|
if (!releases) {
|
|
return std::unexpected{releases.error()};
|
|
}
|
|
for (auto &rel : *releases) {
|
|
for (const auto &dep_c : rel.dependencies) {
|
|
if (fetched_ids.insert(dep_c.target).second) {
|
|
pending.push(dep_c.target);
|
|
}
|
|
}
|
|
index.add_release(std::move(rel));
|
|
}
|
|
}
|
|
|
|
auto solved = solve_dependencies(root_constraints, index, solve_opts);
|
|
if (!solved) {
|
|
return std::unexpected{solved.error()};
|
|
}
|
|
solved_packages = std::move(solved->packages);
|
|
}
|
|
}
|
|
|
|
// 2. Materialize every dependency (registry packages + git/path/archive/conan).
|
|
const auto pkg_cache_root = options.build_root / ".vbs" / "cache" / "packages";
|
|
std::error_code ec;
|
|
for (const auto &nd : merged_deps) {
|
|
if (nd.record.source_kind == DependencySourceKind::Package) {
|
|
const auto &pid = alias_to_pkg_id.at(nd.record.name);
|
|
const auto &sp = solved_packages.at(pid);
|
|
const auto tar_path = pkg_cache_root / (sp.source_sha256 + ".tar");
|
|
const auto extract_dir = pkg_cache_root / sp.source_sha256 / "src";
|
|
|
|
std::string package_signature;
|
|
std::string package_public_key;
|
|
if (const auto it = locked_pkg_entries.find(pid);
|
|
it != locked_pkg_entries.end() && it->second.sha256 == sp.source_sha256) {
|
|
package_signature = it->second.signature.value_or(std::string{});
|
|
package_public_key = it->second.public_key.value_or(std::string{});
|
|
} else {
|
|
package_signature = sp.signature.value_or(std::string{});
|
|
package_public_key = sp.public_key.value_or(std::string{});
|
|
}
|
|
|
|
if (!options.trusted_public_keys.empty()) {
|
|
auto decision = Provenance::verify_trusted_package_signature(
|
|
pid.name, sp.version.to_string(),
|
|
Provenance::PackageSignature{sp.source_sha256, package_signature,
|
|
package_public_key},
|
|
options.trusted_public_keys);
|
|
if (!decision) {
|
|
return std::unexpected{decision.error()};
|
|
}
|
|
if (*decision != Provenance::TrustDecision::Verified) {
|
|
return std::unexpected{Void::System::make_system_error(
|
|
Void::System::SystemErrorCode::permission_denied,
|
|
std::format("package {}@{} is not signed by a trusted key ({})", pid.name,
|
|
sp.version.to_string(),
|
|
Provenance::trust_decision_name(*decision)))};
|
|
}
|
|
}
|
|
|
|
if (!std::filesystem::is_directory(extract_dir, ec)) {
|
|
bool need_download = true;
|
|
if (std::filesystem::is_regular_file(tar_path, ec)) {
|
|
auto have_sha = Util::sha256_file_hex(tar_path);
|
|
if (have_sha && *have_sha == sp.source_sha256) {
|
|
need_download = false;
|
|
}
|
|
}
|
|
if (need_download) {
|
|
Void::Log::info("Fetch", "Downloading {}@{} from {}", pid.name,
|
|
sp.version.to_string(), pid.registry_authority);
|
|
auto dl = download_release_archive(pid.registry_authority, pid.name,
|
|
sp.version.to_string(), sp.source_sha256,
|
|
tar_path, options.registry_auth);
|
|
if (!dl) {
|
|
return std::unexpected{dl.error()};
|
|
}
|
|
}
|
|
Void::Log::info("Fetch", "Extracting {}@{} ({})", pid.name, sp.version.to_string(),
|
|
sp.source_sha256.substr(0, 12));
|
|
auto ex = extract_safe_tar(tar_path, extract_dir);
|
|
if (!ex) {
|
|
return std::unexpected{ex.error()};
|
|
}
|
|
} else {
|
|
Void::Log::debug("Fetch", "Using cached package {}@{} ({})", pid.name,
|
|
sp.version.to_string(), sp.source_sha256.substr(0, 12));
|
|
}
|
|
|
|
MaterializedSource mat{nd.record.name, extract_dir, sp.source_sha256, false};
|
|
out.materialized.emplace_back(nd.record, std::move(mat));
|
|
|
|
LockedSourceEntry le;
|
|
le.alias = nd.record.name;
|
|
le.source_kind = "package";
|
|
le.registry_authority = pid.registry_authority;
|
|
le.package_name = pid.name;
|
|
le.version = sp.version.to_string();
|
|
le.sha256 = sp.source_sha256;
|
|
le.builder = nd.record.builder;
|
|
le.features = nd.record.features;
|
|
le.build_dependency = nd.record.build_dependency;
|
|
if (!package_signature.empty()) {
|
|
le.signature = package_signature;
|
|
}
|
|
if (!package_public_key.empty()) {
|
|
le.public_key = package_public_key;
|
|
}
|
|
out.lock_context.entries.push_back(std::move(le));
|
|
} else {
|
|
FetchOptions fopt{
|
|
options.build_root / ".vbs" / "cache", options.project_root, options.offline, {}};
|
|
auto mat = materialize_dependency_source(nd.record, fopt);
|
|
if (!mat) {
|
|
return std::unexpected{mat.error()};
|
|
}
|
|
LockedSourceEntry le;
|
|
le.alias = nd.record.name;
|
|
switch (nd.record.source_kind) {
|
|
case DependencySourceKind::Git:
|
|
le.source_kind = "git";
|
|
break;
|
|
case DependencySourceKind::Path:
|
|
le.source_kind = "path";
|
|
break;
|
|
case DependencySourceKind::Archive:
|
|
le.source_kind = "archive";
|
|
break;
|
|
case DependencySourceKind::Conan:
|
|
le.source_kind = "conan";
|
|
break;
|
|
case DependencySourceKind::Package:
|
|
le.source_kind = "package";
|
|
break;
|
|
case DependencySourceKind::Unspecified:
|
|
le.source_kind = "unspecified";
|
|
break;
|
|
}
|
|
le.location = nd.record.location;
|
|
le.revision = nd.record.revision;
|
|
le.sha256 = mat->content_identity_hash;
|
|
le.builder = nd.record.builder;
|
|
le.features = nd.record.features;
|
|
le.build_dependency = nd.record.build_dependency;
|
|
out.lock_context.entries.push_back(std::move(le));
|
|
out.materialized.emplace_back(nd.record, std::move(*mat));
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
} // namespace Void::Build::Package
|