feat(daemon): materialize project dependencies for daemon build requests
This commit is contained in:
1 parent
42172a24f8
commit
f5c0c9f09d
12 files changed
+580
-287
No files matched your search
+136
-17
@@ -5,9 +5,14 @@
|
||||
#include <Void/Build/Config/Frontend.hpp>
|
||||
#include <Void/Build/Execution/Scheduler.hpp>
|
||||
#include <Void/Build/Graph/Graph.hpp>
|
||||
#include <Void/Build/Package/Lockfile.hpp>
|
||||
#include <Void/Build/Package/Resolver.hpp>
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
#include <Void/Build/Provider/Provider.hpp>
|
||||
#include <Void/Build/State/StateStore.hpp>
|
||||
#include <Void/Build/Toolchain/Driver.hpp>
|
||||
#include <Void/Build/Watch/Watcher.hpp>
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
|
||||
@@ -330,7 +335,8 @@ Config::FrontendOptions make_frontend_options(const DaemonOptions &options) {
|
||||
|
||||
struct EvaluatedProject {
|
||||
ProfileContext context;
|
||||
Graph::BuildGraph graph;
|
||||
Graph::ProjectDescription description;
|
||||
Toolchain::ToolchainCapabilities toolchain;
|
||||
};
|
||||
|
||||
// Evaluates the project description in-process and lowers its graph. Workspace roots are
|
||||
@@ -372,19 +378,118 @@ evaluate_project_graph(const DaemonOptions &options, std::string_view profile_na
|
||||
return std::unexpected{std::format("{}: {}", op, toolchain.error().format())};
|
||||
}
|
||||
|
||||
Graph::GraphLowerOptions lower_options;
|
||||
lower_options.project_root = options.project_root;
|
||||
lower_options.build_root = options.build_root;
|
||||
auto graph = Graph::lower_project_graph(*description, *toolchain, lower_options);
|
||||
if (!graph) {
|
||||
return std::unexpected{std::format("{}: {}", op, graph.error().format())};
|
||||
}
|
||||
evaluated.graph = std::move(*graph);
|
||||
evaluated.description = std::move(*description);
|
||||
evaluated.toolchain = std::move(*toolchain);
|
||||
return evaluated;
|
||||
}
|
||||
|
||||
// Bounded build-serving request: lowers the project graph and executes it in-process against the
|
||||
// daemon's warm StateStore. Dependency materialization belongs to `vbs build`/`vbs deps`.
|
||||
Void::Result::Result<Graph::BuildGraph, std::string>
|
||||
lower_graph(const DaemonOptions &options, const EvaluatedProject &evaluated,
|
||||
std::map<std::string, Graph::ImportedTargetInterface, std::less<>> imported_targets,
|
||||
std::string_view op) {
|
||||
Graph::GraphLowerOptions lower_options;
|
||||
lower_options.project_root = options.project_root;
|
||||
lower_options.build_root = options.build_root;
|
||||
lower_options.imported_targets = std::move(imported_targets);
|
||||
auto graph =
|
||||
Graph::lower_project_graph(evaluated.description, evaluated.toolchain, lower_options);
|
||||
if (!graph) {
|
||||
return std::unexpected{std::format("{}: {}", op, graph.error().format())};
|
||||
}
|
||||
return std::move(*graph);
|
||||
}
|
||||
|
||||
// Resolves, materializes, and imports the project's declared dependencies into typed target
|
||||
// interfaces. Returns the number of materialized sources.
|
||||
Void::Result::Result<std::size_t, std::string> materialize_project_dependencies(
|
||||
const DaemonOptions &options, State::StateStore &state, const EvaluatedProject &evaluated,
|
||||
std::size_t jobs,
|
||||
std::map<std::string, Graph::ImportedTargetInterface, std::less<>> &imported_targets) {
|
||||
const auto &description = evaluated.description;
|
||||
if (description.dependencies.empty() && description.dependency_files.empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<Package::NormalizedDependency> toml_deps;
|
||||
for (const auto &dep_file : description.dependency_files) {
|
||||
const auto path = std::filesystem::path{dep_file}.is_absolute()
|
||||
? std::filesystem::path{dep_file}
|
||||
: (options.project_root / dep_file);
|
||||
auto loaded = Package::load_dependencies_toml(path);
|
||||
if (!loaded) {
|
||||
return std::unexpected{std::format("build: {}", loaded.error().format())};
|
||||
}
|
||||
toml_deps.insert(toml_deps.end(), loaded->begin(), loaded->end());
|
||||
}
|
||||
auto merged = Package::merge_dependencies(description.dependencies, toml_deps);
|
||||
if (!merged) {
|
||||
return std::unexpected{std::format("build: {}", merged.error().format())};
|
||||
}
|
||||
if (merged->empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto pinned_lock = Package::load_pinned_lock(options.project_root / "vbs.lock");
|
||||
if (!pinned_lock) {
|
||||
return std::unexpected{std::format("build: {}", pinned_lock.error().format())};
|
||||
}
|
||||
Package::ResolveOptions resolve_options;
|
||||
resolve_options.project_root = options.project_root;
|
||||
resolve_options.build_root = options.build_root;
|
||||
resolve_options.pinned_lock = *pinned_lock;
|
||||
resolve_options.default_registry_url = Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
resolve_options.registry_auth =
|
||||
Package::RegistryAuth{Void::Configuration::get_env_or("VBS_REGISTRY_TOKEN", "")};
|
||||
auto resolved =
|
||||
Package::resolve_and_materialize_dependencies(evaluated.context, *merged, resolve_options);
|
||||
if (!resolved) {
|
||||
return std::unexpected{std::format("build: {}", resolved.error().format())};
|
||||
}
|
||||
|
||||
const auto frontend = make_frontend_options(options);
|
||||
for (const auto &[dependency, source] : resolved->materialized) {
|
||||
DependencyRecord effective = dependency;
|
||||
std::error_code ec;
|
||||
if (effective.builder.empty()) {
|
||||
if (std::filesystem::is_regular_file(source.source_dir / "build.cpp", ec)) {
|
||||
effective.builder = "native";
|
||||
} else if (std::filesystem::is_regular_file(source.source_dir / "CMakeLists.txt", ec)) {
|
||||
effective.builder = "cmake";
|
||||
} else if (std::filesystem::is_regular_file(source.source_dir / "meson.build", ec)) {
|
||||
effective.builder = "meson";
|
||||
}
|
||||
}
|
||||
if (effective.builder != "cmake" && effective.builder != "meson" &&
|
||||
effective.builder != "conan" && effective.builder != "native") {
|
||||
continue;
|
||||
}
|
||||
Provider::ProviderRequest request{effective,
|
||||
source,
|
||||
evaluated.context,
|
||||
evaluated.toolchain,
|
||||
options.build_root / ".vbs" / "providers",
|
||||
Provider::ProviderMode::FullBuild,
|
||||
{},
|
||||
frontend.sdk_include_dir,
|
||||
frontend.sdk_source_dir,
|
||||
frontend.sdk_library_path.empty()
|
||||
? std::nullopt
|
||||
: std::optional{frontend.sdk_library_path},
|
||||
&state,
|
||||
jobs};
|
||||
auto exported = Provider::resolve_foreign_provider(request);
|
||||
if (!exported) {
|
||||
return std::unexpected{std::format("build: {}", exported.error().format())};
|
||||
}
|
||||
for (const auto &[name, interface] : exported->targets) {
|
||||
imported_targets[name] = interface;
|
||||
}
|
||||
}
|
||||
return resolved->materialized.size();
|
||||
}
|
||||
|
||||
// Bounded build-serving request: resolves dependencies, lowers the project graph, and executes it
|
||||
// in-process against the daemon's warm StateStore.
|
||||
std::string build_project(const DaemonOptions &options, State::StateStore &state,
|
||||
std::string_view profile_name,
|
||||
std::optional<CompilerFamily> compiler_override,
|
||||
@@ -394,10 +499,21 @@ std::string build_project(const DaemonOptions &options, State::StateStore &state
|
||||
return error_response(evaluated.error());
|
||||
}
|
||||
|
||||
std::map<std::string, Graph::ImportedTargetInterface, std::less<>> imported_targets;
|
||||
auto materialized =
|
||||
materialize_project_dependencies(options, state, *evaluated, jobs, imported_targets);
|
||||
if (!materialized) {
|
||||
return error_response(materialized.error());
|
||||
}
|
||||
auto graph = lower_graph(options, *evaluated, std::move(imported_targets), "build");
|
||||
if (!graph) {
|
||||
return error_response(graph.error());
|
||||
}
|
||||
|
||||
Execution::ExecutionOptions exec_options;
|
||||
exec_options.jobs = jobs;
|
||||
exec_options.selected_targets = selected_targets;
|
||||
auto report = Execution::execute_graph(evaluated->graph, state, exec_options);
|
||||
auto report = Execution::execute_graph(*graph, state, exec_options);
|
||||
if (!report) {
|
||||
return error_response(std::format("build: {}", report.error().format()));
|
||||
}
|
||||
@@ -417,10 +533,9 @@ std::string build_project(const DaemonOptions &options, State::StateStore &state
|
||||
object.emplace("ok", JsonValue{report->succeeded});
|
||||
object.emplace("op", JsonValue{std::string{"build"}});
|
||||
object.emplace("profile", JsonValue{evaluated->context.name});
|
||||
object.emplace("targets",
|
||||
JsonValue{static_cast<std::int64_t>(evaluated->graph.targets.size())});
|
||||
object.emplace("actions",
|
||||
JsonValue{static_cast<std::int64_t>(evaluated->graph.actions.size())});
|
||||
object.emplace("targets", JsonValue{static_cast<std::int64_t>(graph->targets.size())});
|
||||
object.emplace("actions", JsonValue{static_cast<std::int64_t>(graph->actions.size())});
|
||||
object.emplace("dependencies", JsonValue{static_cast<std::int64_t>(*materialized)});
|
||||
object.emplace("executed", JsonValue{static_cast<std::int64_t>(report->executed_count)});
|
||||
object.emplace("cached", JsonValue{static_cast<std::int64_t>(report->cached_count)});
|
||||
object.emplace("failed", JsonValue{static_cast<std::int64_t>(report->failed_count)});
|
||||
@@ -436,7 +551,11 @@ std::string describe_project(const DaemonOptions &options, std::string_view prof
|
||||
if (!evaluated) {
|
||||
return error_response(evaluated.error());
|
||||
}
|
||||
const auto &graph = evaluated->graph;
|
||||
auto lowered = lower_graph(options, *evaluated, {}, "describe");
|
||||
if (!lowered) {
|
||||
return error_response(lowered.error());
|
||||
}
|
||||
const auto &graph = *lowered;
|
||||
const auto &context = evaluated->context;
|
||||
|
||||
std::vector<std::string> target_names;
|
||||
|
||||
+14
-267
@@ -11,6 +11,7 @@
|
||||
#include <Void/Build/Package/Fetch.hpp>
|
||||
#include <Void/Build/Package/Lockfile.hpp>
|
||||
#include <Void/Build/Package/RegistryClient.hpp>
|
||||
#include <Void/Build/Package/Resolver.hpp>
|
||||
#include <Void/Build/Package/Solver.hpp>
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
@@ -260,279 +261,25 @@ SnapshotInputs collect_snapshot_inputs(const Graph::ProjectDescription &desc,
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ResolvedDepMaterialization {
|
||||
Package::LockContextRecord lock_context;
|
||||
std::vector<std::pair<DependencyRecord, Package::MaterializedSource>> materialized;
|
||||
};
|
||||
using ResolvedDepMaterialization = Package::ResolvedDepMaterialization;
|
||||
|
||||
// Shared dependency resolution + live registry fetching + safe archive extraction + lock context
|
||||
// builder for `vbs deps resolve|update`, `vbs build`, and `vbs test`.
|
||||
// Thin CLI adapter over the shared Package resolver (environment-provided registry defaults).
|
||||
Void::Result::Result<ResolvedDepMaterialization, Void::Result::Error> resolve_and_materialize_deps(
|
||||
const ProfileContext &ctx, const std::vector<Package::NormalizedDependency> &merged_deps,
|
||||
const std::filesystem::path &project_root, const std::filesystem::path &build_root,
|
||||
const std::optional<Package::PinnedLock> &pinned_lock, bool offline, bool allow_upgrade,
|
||||
const std::vector<std::string> &trusted_public_keys) {
|
||||
const std::string ctx_key = Package::make_lock_context_key(ctx, "target");
|
||||
const std::string fp = Package::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 `deps update` requested an upgrade).
|
||||
Package::SolveOptions solve_opts;
|
||||
std::map<Package::PackageId, Package::LockedSourceEntry> locked_pkg_entries;
|
||||
if (!allow_upgrade && pinned_lock && pinned_lock->document.contexts.contains(ctx_key)) {
|
||||
for (const auto &entry : pinned_lock->document.contexts.at(ctx_key).entries) {
|
||||
if (entry.source_kind == "package" && !entry.package_name.empty()) {
|
||||
const Package::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.
|
||||
const std::string default_registry = Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
Package::RegistryAuth reg_auth{Void::Configuration::get_env_or("VBS_REGISTRY_TOKEN", "")};
|
||||
std::vector<Package::DependencyConstraint> root_constraints;
|
||||
std::map<std::string, Package::PackageId, std::less<>> alias_to_pkg_id;
|
||||
std::vector<const Package::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() ? default_registry : 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 Package::PackageId pid{reg_url, pkg_name};
|
||||
alias_to_pkg_id[nd.record.name] = pid;
|
||||
package_deps.push_back(&nd);
|
||||
root_constraints.push_back(Package::DependencyConstraint{
|
||||
pid, nd.record.version.empty() ? ">=0.0.0" : nd.record.version, nd.record.features,
|
||||
"root(" + nd.record.name + ")"});
|
||||
}
|
||||
|
||||
std::map<Package::PackageId, Package::SolvedPackage> solved_packages;
|
||||
if (!root_constraints.empty()) {
|
||||
Void::Log::info("Deps", "Solving {} registry package constraint(s)...",
|
||||
root_constraints.size());
|
||||
bool all_locked_cached = offline;
|
||||
const auto pkg_cache_root = 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))};
|
||||
}
|
||||
Package::SolvedPackage sp;
|
||||
sp.id = pid;
|
||||
sp.version = *parsed_ver;
|
||||
sp.source_sha256 = le.sha256;
|
||||
solved_packages[pid] = std::move(sp);
|
||||
}
|
||||
} else {
|
||||
if (offline) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::not_found,
|
||||
"offline mode cannot query live package registry; populate cache first")};
|
||||
}
|
||||
Package::PackageIndex index;
|
||||
std::set<Package::PackageId> fetched_ids;
|
||||
std::queue<Package::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 =
|
||||
Package::fetch_package_releases(pid.registry_authority, pid.name, reg_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 = Package::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 = 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 (!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},
|
||||
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 = Package::download_release_archive(
|
||||
pid.registry_authority, pid.name, sp.version.to_string(), sp.source_sha256,
|
||||
tar_path, reg_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 = Package::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));
|
||||
}
|
||||
|
||||
Package::MaterializedSource mat{nd.record.name, extract_dir, sp.source_sha256, false};
|
||||
out.materialized.emplace_back(nd.record, std::move(mat));
|
||||
|
||||
Package::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 {
|
||||
Package::FetchOptions fopt{build_root / ".vbs" / "cache", project_root, offline, {}};
|
||||
auto mat = Package::materialize_dependency_source(nd.record, fopt);
|
||||
if (!mat) {
|
||||
return std::unexpected{mat.error()};
|
||||
}
|
||||
Package::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;
|
||||
Package::ResolveOptions options;
|
||||
options.project_root = project_root;
|
||||
options.build_root = build_root;
|
||||
options.pinned_lock = pinned_lock;
|
||||
options.offline = offline;
|
||||
options.allow_upgrade = allow_upgrade;
|
||||
options.trusted_public_keys = trusted_public_keys;
|
||||
options.default_registry_url = Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
options.registry_auth =
|
||||
Package::RegistryAuth{Void::Configuration::get_env_or("VBS_REGISTRY_TOKEN", "")};
|
||||
return Package::resolve_and_materialize_dependencies(ctx, merged_deps, options);
|
||||
}
|
||||
|
||||
struct BuildCmdOptions {
|
||||
|
||||
@@ -0,0 +1,290 @@
|
||||
#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
|
||||
Reference in new issue
Block a user