2864 lines
127 KiB
C++
2864 lines
127 KiB
C++
#include <Void/Build.hpp>
|
|
#include <Void/Build/BootstrapConfig.hpp>
|
|
#include <Void/Build/Bridge/CMakeBridge.hpp>
|
|
#include <Void/Build/Config/Frontend.hpp>
|
|
#include <Void/Build/Daemon/Daemon.hpp>
|
|
#include <Void/Build/Editor/CompDb.hpp>
|
|
#include <Void/Build/Execution/Scheduler.hpp>
|
|
#include <Void/Build/Graph/Graph.hpp>
|
|
#include <Void/Build/Install/Installer.hpp>
|
|
#include <Void/Build/Package/Archive.hpp>
|
|
#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>
|
|
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
|
#include <Void/Build/Provenance/Sbom.hpp>
|
|
#include <Void/Build/Provenance/TrustedKeys.hpp>
|
|
#include <Void/Build/Provenance/Verification.hpp>
|
|
#include <Void/Build/Provider/Provider.hpp>
|
|
#include <Void/Build/Provider/Translator.hpp>
|
|
#include <Void/Build/SelfHost/TestRegistryGen.hpp>
|
|
#include <Void/Build/Snapshot/Snapshot.hpp>
|
|
#include <Void/Build/State/StateStore.hpp>
|
|
#include <Void/Build/Toolchain/Driver.hpp>
|
|
#include <Void/Build/Util/Json.hpp>
|
|
#include <Void/Build/Util/Sha256.hpp>
|
|
#include <Void/Build/Watch/Watcher.hpp>
|
|
#include <Void/Configuration/Argparse.hpp>
|
|
#include <Void/Configuration/Env.hpp>
|
|
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/System/SignalWatcher.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Text/Semver.hpp>
|
|
#include <Void/Text/Str.hpp>
|
|
#include <Void/Text/Url.hpp>
|
|
#include <Void/Time/Stopwatch.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <charconv>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <queue>
|
|
#include <set>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
using namespace Void::Build;
|
|
|
|
class ConsoleLogGuard {
|
|
public:
|
|
explicit ConsoleLogGuard(Void::Log::Level level) {
|
|
Void::Log::set_min_level(level);
|
|
if (level > Void::Log::Level::Debug) {
|
|
// Suppress internal VoidLibrary FileSystem open-miss warnings on cold cache probes.
|
|
Void::Log::set_module_level("FileSystem", Void::Log::Level::Error);
|
|
}
|
|
reg_id_ =
|
|
Void::Log::register_sink([mutex = std::make_shared<std::mutex>()](
|
|
Void::Log::Level lvl, std::string_view module,
|
|
std::string_view message, const std::source_location &) {
|
|
std::lock_guard lock{*mutex};
|
|
switch (lvl) {
|
|
case Void::Log::Level::Trace:
|
|
std::cerr << "[vbs][TRACE][" << module << "] " << message << '\n' << std::flush;
|
|
break;
|
|
case Void::Log::Level::Debug:
|
|
std::cerr << "[vbs][DEBUG][" << module << "] " << message << '\n' << std::flush;
|
|
break;
|
|
case Void::Log::Level::Info:
|
|
std::cerr << "[vbs][" << module << "] " << message << '\n' << std::flush;
|
|
break;
|
|
case Void::Log::Level::Warning:
|
|
std::cerr << "[vbs][WARN][" << module << "] " << message << '\n' << std::flush;
|
|
break;
|
|
case Void::Log::Level::Error:
|
|
std::cerr << "[vbs][ERROR][" << module << "] " << message << '\n' << std::flush;
|
|
break;
|
|
case Void::Log::Level::Fatal:
|
|
std::cerr << "[vbs][FATAL][" << module << "] " << message << '\n' << std::flush;
|
|
break;
|
|
case Void::Log::Level::Disabled:
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
|
|
~ConsoleLogGuard() {
|
|
if (reg_id_ != 0) {
|
|
(void)Void::Log::detach_and_drain(reg_id_);
|
|
}
|
|
}
|
|
|
|
ConsoleLogGuard(const ConsoleLogGuard &) = delete;
|
|
ConsoleLogGuard &operator=(const ConsoleLogGuard &) = delete;
|
|
|
|
private:
|
|
Void::Log::RegistrationId reg_id_{0};
|
|
};
|
|
|
|
Config::FrontendOptions
|
|
make_frontend_opts(const std::filesystem::path &project_root,
|
|
const std::filesystem::path &build_root,
|
|
const std::vector<std::filesystem::path> &extra_sources) {
|
|
Config::FrontendOptions opts;
|
|
opts.project_root = project_root;
|
|
opts.build_root = build_root;
|
|
opts.sdk_include_dir = std::filesystem::path{Bootstrap::sdk_include_dir};
|
|
opts.sdk_source_dir = std::filesystem::path{Bootstrap::sdk_source_dir};
|
|
std::error_code ec;
|
|
std::vector<std::filesystem::path> sdk_candidates;
|
|
if (const auto self_exe = std::filesystem::read_symlink("/proc/self/exe", ec);
|
|
!ec && !self_exe.empty()) {
|
|
const auto self_root = self_exe.parent_path().parent_path();
|
|
sdk_candidates.push_back(self_root / "libVoidBuildSdk.a");
|
|
sdk_candidates.push_back(self_root / "libVoidBuild_Sdk.a");
|
|
sdk_candidates.push_back(self_root / "lib" / "libVoidBuildSdk.a");
|
|
}
|
|
if (!Bootstrap::binary_dir.empty()) {
|
|
const auto bin_dir = std::filesystem::path{Bootstrap::binary_dir};
|
|
sdk_candidates.push_back(bin_dir / "libVoidBuild_Sdk.a");
|
|
sdk_candidates.push_back(bin_dir / "out" / "libVoidBuildSdk.a");
|
|
sdk_candidates.push_back(bin_dir / "profiles" / "debug" / "out" / "libVoidBuildSdk.a");
|
|
}
|
|
for (const auto &candidate : sdk_candidates) {
|
|
if (std::filesystem::is_regular_file(candidate, ec)) {
|
|
opts.sdk_library_path = candidate;
|
|
break;
|
|
}
|
|
}
|
|
opts.extra_build_sources = extra_sources;
|
|
return opts;
|
|
}
|
|
|
|
struct SnapshotInputs {
|
|
std::vector<std::filesystem::path> explicit_files;
|
|
std::vector<std::filesystem::path> scan_roots;
|
|
};
|
|
|
|
// Derives snapshot scan roots and explicit files from conventional directories plus every
|
|
// target's declared include directories, sources, headers, and custom action inputs so
|
|
// projects with code outside `src/` (e.g. `recipes/`, `registry/`) snapshot completely.
|
|
SnapshotInputs collect_snapshot_inputs(const Graph::ProjectDescription &desc,
|
|
const std::filesystem::path &project_root) {
|
|
SnapshotInputs out;
|
|
std::error_code ec;
|
|
const auto canon_root = std::filesystem::weakly_canonical(project_root, ec);
|
|
const auto &root = ec ? project_root : canon_root;
|
|
|
|
std::set<std::filesystem::path> root_dirs;
|
|
std::set<std::filesystem::path> files;
|
|
|
|
auto add_dir_if_in_project = [&](const std::filesystem::path &dir) {
|
|
std::error_code dec;
|
|
const auto norm = dir.lexically_normal();
|
|
if (std::filesystem::is_directory(norm, dec) && Platform::path_is_within(root, norm) &&
|
|
norm != root) {
|
|
root_dirs.insert(norm);
|
|
}
|
|
};
|
|
|
|
for (const char *d : {"src", "source", "include", "tests"}) {
|
|
add_dir_if_in_project(root / d);
|
|
}
|
|
|
|
auto is_generated_ref = [](std::string_view p) {
|
|
return p.starts_with("$GEN/") || p.starts_with("gen/");
|
|
};
|
|
|
|
for (const auto &t : desc.targets) {
|
|
for (const auto &inc : t.include_directories) {
|
|
if (is_generated_ref(inc.path)) {
|
|
continue;
|
|
}
|
|
const std::filesystem::path p{inc.path};
|
|
add_dir_if_in_project(p.is_absolute() ? p : (root / p));
|
|
}
|
|
for (const auto &src : t.sources) {
|
|
if (is_generated_ref(src.path)) {
|
|
continue;
|
|
}
|
|
if (src.kind == SourceKind::File) {
|
|
const std::filesystem::path p{src.path};
|
|
const auto abs = (p.is_absolute() ? p : (root / p)).lexically_normal();
|
|
if (std::filesystem::is_regular_file(abs, ec)) {
|
|
files.insert(abs);
|
|
if (abs.has_parent_path()) {
|
|
add_dir_if_in_project(abs.parent_path());
|
|
}
|
|
}
|
|
} else if (src.kind == SourceKind::Glob) {
|
|
const auto star = src.path.find_first_of("*?[");
|
|
const std::string_view prefix = star == std::string::npos
|
|
? std::string_view{src.path}
|
|
: std::string_view{src.path}.substr(0, star);
|
|
const auto slash = prefix.rfind('/');
|
|
if (slash != std::string_view::npos && slash > 0) {
|
|
add_dir_if_in_project(root / std::filesystem::path{prefix.substr(0, slash)});
|
|
}
|
|
}
|
|
}
|
|
for (const auto &in : t.action_inputs) {
|
|
if (is_generated_ref(in)) {
|
|
continue;
|
|
}
|
|
const std::filesystem::path p{in};
|
|
const auto abs = (p.is_absolute() ? p : (root / p)).lexically_normal();
|
|
if (std::filesystem::is_regular_file(abs, ec)) {
|
|
files.insert(abs);
|
|
}
|
|
}
|
|
if (!t.precompiled_header.empty() && !is_generated_ref(t.precompiled_header)) {
|
|
const std::filesystem::path p{t.precompiled_header};
|
|
const auto abs = (p.is_absolute() ? p : (root / p)).lexically_normal();
|
|
if (std::filesystem::is_regular_file(abs, ec)) {
|
|
files.insert(abs);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove subdirectories already covered by an ancestor scan root.
|
|
for (const auto &d : root_dirs) {
|
|
bool covered = false;
|
|
for (const auto &other : root_dirs) {
|
|
if (other != d && Platform::path_is_within(other, d)) {
|
|
covered = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!covered) {
|
|
out.scan_roots.push_back(d);
|
|
}
|
|
}
|
|
// Only keep explicit files not already covered by a scan root.
|
|
for (const auto &f : files) {
|
|
bool covered = false;
|
|
for (const auto &d : out.scan_roots) {
|
|
if (Platform::path_is_within(d, f)) {
|
|
covered = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!covered) {
|
|
out.explicit_files.push_back(f);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
using ResolvedDepMaterialization = Package::ResolvedDepMaterialization;
|
|
|
|
// 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) {
|
|
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);
|
|
}
|
|
|
|
Void::Result::Result<std::vector<std::string>, Void::Result::Error>
|
|
combine_trusted_keys(const std::vector<std::string> &inline_keys,
|
|
const std::vector<std::string> &key_files,
|
|
const std::filesystem::path &project_root) {
|
|
std::vector<std::filesystem::path> files;
|
|
files.reserve(key_files.size());
|
|
for (const auto &file : key_files) {
|
|
files.emplace_back(file);
|
|
}
|
|
const std::string environment = Void::Configuration::get_env_or("VBS_TRUSTED_PUBLIC_KEYS", "");
|
|
std::optional<std::string_view> environment_value;
|
|
if (!environment.empty()) {
|
|
environment_value = environment;
|
|
}
|
|
auto loaded = Provenance::load_trusted_keys(files, project_root, environment_value);
|
|
if (!loaded) {
|
|
return std::unexpected{loaded.error()};
|
|
}
|
|
std::vector<std::string> combined = inline_keys;
|
|
for (auto &key : *loaded) {
|
|
if (std::ranges::find(combined, key) == combined.end()) {
|
|
combined.push_back(std::move(key));
|
|
}
|
|
}
|
|
return combined;
|
|
}
|
|
|
|
struct BuildCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::vector<std::string> profiles;
|
|
std::vector<std::string> targets;
|
|
std::vector<std::string> extra_build_sources;
|
|
std::optional<std::string> editor_profile;
|
|
std::optional<std::string> compiler_arg;
|
|
std::optional<std::string> jobs_arg;
|
|
std::optional<std::string> remote_cache_arg;
|
|
std::optional<std::string> remote_cache_token_env;
|
|
std::vector<std::string> members;
|
|
std::optional<std::string> changed_since;
|
|
std::optional<std::string> sbom_arg;
|
|
std::optional<std::string> sbom_format_arg;
|
|
std::optional<std::filesystem::path> lock_root;
|
|
std::optional<std::filesystem::path> state_dir;
|
|
std::vector<std::string> trusted_public_keys;
|
|
std::vector<std::string> trusted_key_files;
|
|
bool publish_editor_selection{true};
|
|
bool no_cache{false};
|
|
bool locked{false};
|
|
bool offline{false};
|
|
bool native_foreign{false};
|
|
};
|
|
|
|
int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
|
const std::string cmd_name = run_tests ? "vbs test" : "vbs build";
|
|
const auto total_sw = Void::Time::Stopwatch::started();
|
|
|
|
std::optional<CompilerFamily> compiler_override;
|
|
if (opts.compiler_arg) {
|
|
try {
|
|
compiler_override = parse_compiler(*opts.compiler_arg);
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << cmd_name << ": " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
}
|
|
std::size_t jobs = 0;
|
|
if (opts.jobs_arg) {
|
|
int parsed_jobs = 0;
|
|
if (!Void::Str::to_int(*opts.jobs_arg, parsed_jobs) || parsed_jobs < 0) {
|
|
std::cerr << cmd_name << ": invalid --jobs value '" << *opts.jobs_arg << "'\n";
|
|
return 2;
|
|
}
|
|
jobs = static_cast<std::size_t>(parsed_jobs);
|
|
}
|
|
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
auto trusted_keys =
|
|
combine_trusted_keys(opts.trusted_public_keys, opts.trusted_key_files, project_root);
|
|
if (!trusted_keys) {
|
|
std::cerr << cmd_name << ": " << trusted_keys.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::vector<std::filesystem::path> extra_build_sources;
|
|
for (const auto &s : opts.extra_build_sources) {
|
|
extra_build_sources.emplace_back(s);
|
|
}
|
|
if (opts.profiles.empty()) {
|
|
opts.profiles.push_back("debug");
|
|
}
|
|
|
|
// D-010 / Plan 09: multi-profile builds MUST specify --editor-profile from selected profiles
|
|
// BEFORE doing any build work!
|
|
if (opts.profiles.size() > 1) {
|
|
if (!opts.editor_profile) {
|
|
std::cerr << "vbs: multi-profile build requires --editor-profile selecting one of "
|
|
"the requested profiles\n";
|
|
return 2;
|
|
}
|
|
if (!std::ranges::contains(opts.profiles, *opts.editor_profile)) {
|
|
std::cerr << "vbs: --editor-profile '" << *opts.editor_profile
|
|
<< "' must be one of the requested profiles\n";
|
|
return 2;
|
|
}
|
|
} else if (!opts.editor_profile) {
|
|
opts.editor_profile = opts.profiles.front();
|
|
}
|
|
|
|
// Reserve monotonic selection ticket on acceptance BEFORE expensive work!
|
|
auto reserved_ticket =
|
|
Editor::reserve_selection_ticket(project_root, build_root, *opts.editor_profile);
|
|
if (!reserved_ticket) {
|
|
std::cerr << "vbs: failed reserving editor ticket: " << reserved_ticket.error().message()
|
|
<< '\n';
|
|
return 1;
|
|
}
|
|
|
|
const auto fopts = make_frontend_opts(project_root, build_root, extra_build_sources);
|
|
Void::Log::info("Config", "Preparing build description in '{}'...", project_root.string());
|
|
const auto cfg_sw = Void::Time::Stopwatch::started();
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Config", "Build description {} ({:.0f} ms)",
|
|
compiled->recompiled ? "compiled" : "up to date (cached)", cfg_sw.elapsed_ms());
|
|
|
|
auto profs = Config::discover_profiles(*compiled, fopts);
|
|
if (!profs) {
|
|
std::cerr << "vbs: " << profs.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
auto state = State::StateStore::open(opts.state_dir ? *opts.state_dir : build_root / ".vbs");
|
|
if (!state) {
|
|
std::cerr << "vbs: " << state.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
const std::string remote_cache_url =
|
|
opts.remote_cache_arg
|
|
? *opts.remote_cache_arg
|
|
: std::string{Void::Configuration::get_env_or("VBS_REMOTE_CACHE", "")};
|
|
if (!remote_cache_url.empty()) {
|
|
State::RemoteCacheConfig remote_cfg;
|
|
remote_cfg.url = remote_cache_url;
|
|
const std::string token_env = opts.remote_cache_token_env
|
|
? *opts.remote_cache_token_env
|
|
: std::string{"VBS_REMOTE_CACHE_TOKEN"};
|
|
remote_cfg.bearer_token = Void::Configuration::get_env_or(token_env, "");
|
|
state->configure_remote_cache(std::move(remote_cfg));
|
|
Void::Log::info("Cache", "Using shared remote action cache at {}", remote_cache_url);
|
|
}
|
|
|
|
const auto lock_path = (opts.lock_root ? *opts.lock_root : project_root) / "vbs.lock";
|
|
auto pinned_lock = Package::load_pinned_lock(lock_path);
|
|
if (!pinned_lock) {
|
|
std::cerr << "vbs: " << pinned_lock.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
bool all_ok = true;
|
|
for (const auto &prof_name : opts.profiles) {
|
|
const auto prof_sw = Void::Time::Stopwatch::started();
|
|
ProfileContext ctx;
|
|
try {
|
|
ctx = profs->resolve(prof_name, compiler_override, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
|
|
auto tc = Toolchain::discover_and_probe(ctx, build_root / ".vbs" / "probes");
|
|
if (!tc) {
|
|
std::cerr << "vbs: " << tc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Toolchain", "[{}] Using {} ({}) [{}]", prof_name,
|
|
compiler_name(tc->family), tc->cxx_compiler.string(), tc->version_banner);
|
|
|
|
const auto eval_sw = Void::Time::Stopwatch::started();
|
|
auto desc = Config::evaluate_project(*compiled, ctx, fopts);
|
|
if (!desc) {
|
|
std::cerr << "vbs: " << desc.error().message() << '\n';
|
|
(void)Editor::mark_profile_stale(
|
|
build_root, prof_name, Editor::GenerationStatus::Invalid, desc.error().message());
|
|
return 1;
|
|
}
|
|
Void::Log::info("Eval", "[{}] Evaluated {} target(s), {} dependenc(ies) ({:.0f} ms)",
|
|
prof_name, desc->targets.size(), desc->dependencies.size(),
|
|
eval_sw.elapsed_ms());
|
|
|
|
// Load & merge any TOML dependency manifests declared by build.cpp.
|
|
std::vector<Package::NormalizedDependency> toml_deps;
|
|
for (const auto &dep_file : desc->dependency_files) {
|
|
const auto p = std::filesystem::path{dep_file}.is_absolute()
|
|
? std::filesystem::path{dep_file}
|
|
: (project_root / dep_file);
|
|
Void::Log::debug("Deps", "[{}] Loading dependency manifest '{}'", prof_name,
|
|
p.string());
|
|
auto loaded = Package::load_dependencies_toml(p);
|
|
if (!loaded) {
|
|
std::cerr << "vbs: " << loaded.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
toml_deps.insert(toml_deps.end(), loaded->begin(), loaded->end());
|
|
}
|
|
auto merged_deps = Package::merge_dependencies(desc->dependencies, toml_deps);
|
|
if (!merged_deps) {
|
|
std::cerr << "vbs: " << merged_deps.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
// Validate/update lockfile context and materialize dependencies (including live registry).
|
|
std::map<std::string, Graph::ImportedTargetInterface, std::less<>> imported_targets;
|
|
if (!merged_deps->empty()) {
|
|
Void::Log::info("Deps", "[{}] Resolving {} dependenc(ies)...", prof_name,
|
|
merged_deps->size());
|
|
const std::string ctx_key = Package::make_lock_context_key(ctx, "target");
|
|
const std::string fp =
|
|
Package::compute_declaration_fingerprint(ctx, "target", *merged_deps);
|
|
bool need_lock_publish = !pinned_lock->has_value();
|
|
if (pinned_lock->has_value()) {
|
|
auto valid =
|
|
Package::validate_locked_context((*pinned_lock)->document, ctx_key, fp);
|
|
if (!valid) {
|
|
if (opts.locked) {
|
|
std::cerr << "vbs: " << valid.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Deps",
|
|
"[{}] Updating vbs.lock for changed dependency declarations...",
|
|
prof_name);
|
|
need_lock_publish = true;
|
|
}
|
|
} else if (opts.locked) {
|
|
std::cerr << "vbs: --locked requires an existing vbs.lock\n";
|
|
return 1;
|
|
}
|
|
|
|
auto resolved =
|
|
resolve_and_materialize_deps(ctx, *merged_deps, project_root, build_root,
|
|
*pinned_lock, opts.offline, false, *trusted_keys);
|
|
if (!resolved) {
|
|
std::cerr << "vbs: " << resolved.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
for (const auto &[dep_rec, mat] : resolved->materialized) {
|
|
DependencyRecord effective_dep = dep_rec;
|
|
if (effective_dep.builder.empty() &&
|
|
effective_dep.source_kind == DependencySourceKind::Package) {
|
|
std::error_code bec;
|
|
if (std::filesystem::is_regular_file(mat.source_dir / "build.cpp", bec)) {
|
|
effective_dep.builder = "native";
|
|
} else if (std::filesystem::is_regular_file(mat.source_dir / "CMakeLists.txt",
|
|
bec)) {
|
|
effective_dep.builder = "cmake";
|
|
} else if (std::filesystem::is_regular_file(mat.source_dir / "meson.build",
|
|
bec)) {
|
|
effective_dep.builder = "meson";
|
|
}
|
|
}
|
|
if (effective_dep.builder == "cmake" || effective_dep.builder == "meson" ||
|
|
effective_dep.builder == "conan" || effective_dep.builder == "native") {
|
|
Provider::ProviderRequest preq{effective_dep,
|
|
mat,
|
|
ctx,
|
|
*tc,
|
|
build_root / ".vbs" / "providers",
|
|
Provider::ProviderMode::FullBuild,
|
|
{},
|
|
fopts.sdk_include_dir,
|
|
fopts.sdk_source_dir,
|
|
fopts.sdk_library_path.empty()
|
|
? std::nullopt
|
|
: std::optional{fopts.sdk_library_path},
|
|
&(*state),
|
|
jobs};
|
|
if (opts.native_foreign &&
|
|
(effective_dep.builder == "cmake" || effective_dep.builder == "meson")) {
|
|
auto tr = Provider::resolve_foreign_with_translation(
|
|
preq, *state, Provider::TranslationPolicy::FallbackToForeignBuild);
|
|
if (!tr) {
|
|
std::cerr << "vbs: " << tr.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Provider", "[{}] Imported {} target(s) from '{}' ({})",
|
|
prof_name, tr->exported.targets.size(), effective_dep.name,
|
|
tr->executed_natively
|
|
? "native-translated"
|
|
: (tr->exported.from_cache ? "cached" : "built"));
|
|
for (const auto &[k, v] : tr->exported.targets) {
|
|
imported_targets[k] = v;
|
|
}
|
|
} else {
|
|
auto exp = Provider::resolve_foreign_provider(preq);
|
|
if (!exp) {
|
|
std::cerr << "vbs: " << exp.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Provider", "[{}] Imported {} target(s) from '{}' ({})",
|
|
prof_name, exp->targets.size(), effective_dep.name,
|
|
exp->from_cache ? "cached" : "built");
|
|
for (const auto &[k, v] : exp->targets) {
|
|
imported_targets[k] = v;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (need_lock_publish) {
|
|
const std::string base_rev =
|
|
pinned_lock->has_value() ? (*pinned_lock)->revision_hash : std::string{};
|
|
auto pub_lock = Package::publish_lock_context_cas(
|
|
lock_path, base_rev, resolved->lock_context, opts.locked);
|
|
if (!pub_lock) {
|
|
std::cerr << "vbs: " << pub_lock.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
pinned_lock = *pub_lock;
|
|
Void::Log::info("Deps", "[{}] Updated vbs.lock (revision {})", prof_name,
|
|
(*pinned_lock)->revision_hash.substr(0, 12));
|
|
}
|
|
}
|
|
|
|
// Capture immutable snapshot over all declared & conventional project source trees.
|
|
const auto snap_sw = Void::Time::Stopwatch::started();
|
|
const auto snap_inputs = collect_snapshot_inputs(*desc, project_root);
|
|
Snapshot::SnapshotOptions sopts;
|
|
sopts.project_root = project_root;
|
|
sopts.snapshot_store_dir = build_root / ".vbs" / "snapshots";
|
|
sopts.reproducibility_epoch = ctx.reproducibility_epoch;
|
|
auto snap =
|
|
Snapshot::capture_snapshot(sopts, snap_inputs.explicit_files, snap_inputs.scan_roots);
|
|
if (!snap) {
|
|
std::cerr << "vbs: " << snap.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Snapshot", "[{}] Captured snapshot {} ({} file(s), {:.0f} ms)", prof_name,
|
|
snap->snapshot_id.substr(0, 12), snap->entries.size(),
|
|
snap_sw.elapsed_ms());
|
|
|
|
const auto graph_sw = Void::Time::Stopwatch::started();
|
|
Graph::GraphLowerOptions gopts{project_root, build_root, &(*snap),
|
|
std::move(imported_targets), 32768};
|
|
auto graph = Graph::lower_project_graph(*desc, *tc, gopts);
|
|
if (!graph) {
|
|
std::cerr << "vbs: " << graph.error().message() << '\n';
|
|
(void)Editor::mark_profile_stale(
|
|
build_root, prof_name, Editor::GenerationStatus::Invalid, graph.error().message());
|
|
return 1;
|
|
}
|
|
Void::Log::info("Graph", "[{}] Lowered {} action(s) across {} target(s) ({:.0f} ms)",
|
|
prof_name, graph->actions.size(), graph->targets.size(),
|
|
graph_sw.elapsed_ms());
|
|
|
|
// Publish compdb BEFORE compilation (Plan 09: compile failure does NOT undo selection!).
|
|
Editor::PublishOptions popts;
|
|
popts.project_root = project_root;
|
|
popts.build_root = build_root;
|
|
popts.update_selection =
|
|
opts.publish_editor_selection && (*opts.editor_profile == prof_name);
|
|
popts.reserved_ticket = *reserved_ticket;
|
|
popts.sdk_include_dir = fopts.sdk_include_dir;
|
|
popts.extra_build_sources = extra_build_sources;
|
|
if (auto pub_db = Editor::publish_profile_compdb(*graph, popts); pub_db) {
|
|
Void::Log::debug("CompDb", "[{}] Published compile_commands.json ({})", prof_name,
|
|
pub_db->compdb_path.string());
|
|
}
|
|
|
|
Execution::ExecutionOptions eopts;
|
|
eopts.jobs = jobs;
|
|
eopts.use_cache = !opts.no_cache;
|
|
eopts.selected_targets = opts.targets;
|
|
auto rep = Execution::execute_graph(*graph, *state, eopts);
|
|
if (!rep) {
|
|
std::cerr << "vbs: " << rep.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
if (!rep->succeeded) {
|
|
for (const auto &ar : rep->results) {
|
|
if (!ar.succeeded) {
|
|
std::cerr << "vbs: action '" << ar.action_id << "' failed: " << ar.error_message
|
|
<< '\n';
|
|
if (!ar.stderr_data.empty()) {
|
|
std::cerr << ar.stderr_data << '\n';
|
|
}
|
|
}
|
|
}
|
|
all_ok = false;
|
|
continue;
|
|
}
|
|
Void::Log::info("Build", "[{}] Finished in {:.2f}s ({} executed, {} cached)", prof_name,
|
|
prof_sw.elapsed_ms() / 1000.0, rep->executed_count, rep->cached_count);
|
|
std::cout << "vbs: [" << prof_name << "] built (" << rep->executed_count << " executed, "
|
|
<< rep->cached_count << " cached)\n";
|
|
|
|
if (opts.sbom_arg) {
|
|
Provenance::SbomInput sbom;
|
|
sbom.project_name = project_root.filename().string();
|
|
sbom.tool_version = std::string{Bootstrap::version};
|
|
sbom.created_epoch = ctx.reproducibility_epoch;
|
|
const auto sbom_ctx_key = Package::make_lock_context_key(ctx, "target");
|
|
if (pinned_lock && pinned_lock->has_value()) {
|
|
const auto &document = (*pinned_lock)->document;
|
|
if (const auto it = document.contexts.find(sbom_ctx_key);
|
|
it != document.contexts.end()) {
|
|
for (const auto &entry : it->second.entries) {
|
|
if (entry.version.empty()) {
|
|
continue;
|
|
}
|
|
Provenance::SbomPackage package;
|
|
package.name =
|
|
entry.package_name.empty() ? entry.alias : entry.package_name;
|
|
package.version = entry.version;
|
|
package.download_location = entry.registry_authority.empty()
|
|
? entry.location
|
|
: entry.registry_authority;
|
|
package.sha256 = entry.sha256;
|
|
package.purl = "pkg:generic/" + package.name + "@" + package.version;
|
|
sbom.packages.push_back(std::move(package));
|
|
}
|
|
}
|
|
}
|
|
for (const auto &target : graph->targets) {
|
|
for (const auto &output : target.primary_outputs) {
|
|
auto digest = Util::sha256_file_hex(output);
|
|
if (!digest) {
|
|
continue;
|
|
}
|
|
Provenance::SbomFile file;
|
|
file.path = Platform::path_is_within(project_root, output)
|
|
? output.lexically_relative(project_root).generic_string()
|
|
: output.filename().string();
|
|
file.sha256 = std::move(*digest);
|
|
sbom.files.push_back(std::move(file));
|
|
}
|
|
}
|
|
try {
|
|
const auto format = opts.sbom_format_arg
|
|
? Provenance::parse_sbom_format(*opts.sbom_format_arg)
|
|
: Provenance::SbomFormat::Spdx;
|
|
auto generated = Provenance::generate_sbom(sbom, format);
|
|
if (!generated) {
|
|
std::cerr << "vbs: SBOM generation failed: " << generated.error().message()
|
|
<< '\n';
|
|
all_ok = false;
|
|
continue;
|
|
}
|
|
std::filesystem::path sbom_path{*opts.sbom_arg};
|
|
if (opts.profiles.size() > 1) {
|
|
sbom_path =
|
|
sbom_path.parent_path() / (sbom_path.stem().string() + "." + prof_name +
|
|
sbom_path.extension().string());
|
|
}
|
|
if (auto wrote = Platform::atomic_write_file(sbom_path, *generated, true); !wrote) {
|
|
std::cerr << "vbs: failed writing SBOM: " << wrote.error().message() << '\n';
|
|
all_ok = false;
|
|
continue;
|
|
}
|
|
Void::Log::info("Sbom", "[{}] Wrote {} SBOM to {}", prof_name,
|
|
Provenance::sbom_format_name(format), sbom_path.string());
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs: " << ex.what() << '\n';
|
|
all_ok = false;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (run_tests) {
|
|
for (const auto &t : graph->targets) {
|
|
if (t.kind != TargetKind::Test || t.primary_outputs.empty()) {
|
|
continue;
|
|
}
|
|
if (!opts.targets.empty() && !std::ranges::contains(opts.targets, t.name)) {
|
|
continue;
|
|
}
|
|
Void::Log::info("Test", "[{}] Running test target '{}'...", prof_name, t.name);
|
|
const auto tsw = Void::Time::Stopwatch::started();
|
|
Platform::ProcessRequest treq;
|
|
treq.executable = t.primary_outputs.front();
|
|
treq.working_directory = project_root;
|
|
treq.timeout = std::chrono::seconds{300};
|
|
auto tres = Platform::run_supervised(treq);
|
|
if (!tres || !tres->succeeded()) {
|
|
Void::Log::error("Test", "[{}] FAILED '{}' ({:.0f} ms)", prof_name, t.name,
|
|
tsw.elapsed_ms());
|
|
std::cerr << "vbs test: FAILED '" << t.name << "'\n";
|
|
if (tres && !tres->stdout_data.empty()) {
|
|
std::cerr << tres->stdout_data << '\n';
|
|
}
|
|
if (tres && !tres->stderr_data.empty()) {
|
|
std::cerr << tres->stderr_data << '\n';
|
|
}
|
|
all_ok = false;
|
|
} else {
|
|
Void::Log::info("Test", "[{}] PASSED '{}' ({:.0f} ms)", prof_name, t.name,
|
|
tsw.elapsed_ms());
|
|
std::cout << "vbs test: PASSED '" << t.name << "'\n";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Void::Log::debug("Build", "Total wall time: {:.2f}s", total_sw.elapsed_ms() / 1000.0);
|
|
return all_ok ? 0 : 1;
|
|
}
|
|
|
|
int execute_workspace(BuildCmdOptions opts, bool run_tests) {
|
|
const std::filesystem::path project_root = std::filesystem::absolute(opts.project_arg);
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
|
|
std::vector<std::filesystem::path> extra_sources;
|
|
for (const auto &source : opts.extra_build_sources) {
|
|
extra_sources.emplace_back(source);
|
|
}
|
|
const auto fopts = make_frontend_opts(project_root, build_root, extra_sources);
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto workspace = Config::evaluate_workspace(*compiled, fopts);
|
|
if (!workspace) {
|
|
std::cerr << "vbs: " << workspace.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
if (workspace->members.empty()) {
|
|
std::cerr << "vbs: workspace declares no members\n";
|
|
return 1;
|
|
}
|
|
|
|
std::map<std::string, std::filesystem::path, std::less<>> member_dirs;
|
|
for (const auto &member : workspace->members) {
|
|
std::filesystem::path dir = member.path;
|
|
if (!dir.is_absolute()) {
|
|
dir = project_root / dir;
|
|
}
|
|
std::error_code ec;
|
|
const auto canonical = std::filesystem::weakly_canonical(dir, ec);
|
|
if (ec || !std::filesystem::is_directory(canonical)) {
|
|
std::cerr << "vbs: workspace member '" << member.name << "' path '" << dir.string()
|
|
<< "' is not a directory\n";
|
|
return 1;
|
|
}
|
|
member_dirs[member.name] = canonical;
|
|
const std::string env_name = "VBS_WORKSPACE_MEMBER_" + member.name;
|
|
(void)::setenv(env_name.c_str(), canonical.c_str(), 1);
|
|
}
|
|
|
|
std::set<std::string> selected;
|
|
if (!opts.members.empty()) {
|
|
for (const auto &name : opts.members) {
|
|
if (!member_dirs.contains(name)) {
|
|
std::cerr << "vbs: unknown workspace member '" << name << "'\n";
|
|
return 2;
|
|
}
|
|
selected.insert(name);
|
|
}
|
|
} else if (opts.changed_since) {
|
|
auto git = Platform::resolve_executable("git");
|
|
if (!git) {
|
|
std::cerr << "vbs: --changed-since requires git in PATH\n";
|
|
return 1;
|
|
}
|
|
Platform::ProcessRequest req;
|
|
req.executable = *git;
|
|
req.arguments = {"-C", project_root.string(), "diff", "--name-only", *opts.changed_since};
|
|
auto proc = Platform::run_supervised(req);
|
|
if (!proc || !proc->succeeded()) {
|
|
std::cerr << "vbs: git diff --name-only " << *opts.changed_since
|
|
<< " failed: " << (proc ? proc->stderr_data : proc.error().message()) << '\n';
|
|
return 1;
|
|
}
|
|
std::size_t pos = 0;
|
|
while (pos < proc->stdout_data.size()) {
|
|
const auto nl = proc->stdout_data.find('\n', pos);
|
|
const auto line = proc->stdout_data.substr(
|
|
pos, nl == std::string::npos ? std::string::npos : nl - pos);
|
|
if (!line.empty()) {
|
|
const auto changed = std::filesystem::weakly_canonical(project_root / line);
|
|
std::string best;
|
|
std::size_t best_len = 0;
|
|
for (const auto &[name, dir] : member_dirs) {
|
|
if (Platform::path_is_within(dir, changed) && dir.string().size() > best_len) {
|
|
best = name;
|
|
best_len = dir.string().size();
|
|
}
|
|
}
|
|
if (!best.empty()) {
|
|
selected.insert(best);
|
|
}
|
|
}
|
|
if (nl == std::string::npos) {
|
|
break;
|
|
}
|
|
pos = nl + 1;
|
|
}
|
|
if (selected.empty()) {
|
|
std::cout << "vbs: no workspace members changed since " << *opts.changed_since << '\n';
|
|
return 0;
|
|
}
|
|
} else {
|
|
for (const auto &[name, dir] : member_dirs) {
|
|
selected.insert(name);
|
|
}
|
|
}
|
|
|
|
bool all_ok = true;
|
|
for (const auto &member : workspace->members) {
|
|
if (!selected.contains(member.name)) {
|
|
continue;
|
|
}
|
|
BuildCmdOptions member_opts = opts;
|
|
member_opts.project_arg = member_dirs.at(member.name).string();
|
|
member_opts.build_arg = (build_root / "members" / member.name).string();
|
|
member_opts.lock_root = project_root;
|
|
member_opts.state_dir = build_root / ".vbs";
|
|
member_opts.members.clear();
|
|
member_opts.changed_since.reset();
|
|
// Members must not overwrite each other's project-root compile_commands.json alias.
|
|
member_opts.publish_editor_selection = false;
|
|
Void::Log::info("Workspace", "[{}] {}", member.name,
|
|
run_tests ? "building and testing member" : "building member");
|
|
if (execute_build_or_test(std::move(member_opts), run_tests) != 0) {
|
|
all_ok = false;
|
|
}
|
|
}
|
|
return all_ok ? 0 : 1;
|
|
}
|
|
|
|
struct CompDbCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::optional<std::string> profile_name;
|
|
std::vector<std::string> positional_profiles;
|
|
};
|
|
|
|
int execute_compdb_command(std::string_view sub, CompDbCmdOptions opts) {
|
|
if (sub == "use") {
|
|
if (opts.positional_profiles.size() > 1) {
|
|
std::cerr << "vbs compdb use: expected at most one positional profile argument\n";
|
|
return 2;
|
|
}
|
|
if (opts.positional_profiles.size() == 1) {
|
|
if (opts.profile_name && *opts.profile_name != opts.positional_profiles.front()) {
|
|
std::cerr << "vbs compdb use: conflicting profile specified via both --profile and "
|
|
"positional argument\n";
|
|
return 2;
|
|
}
|
|
opts.profile_name = opts.positional_profiles.front();
|
|
}
|
|
}
|
|
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
|
|
if (sub == "show") {
|
|
auto st = Editor::read_selection_state(project_root);
|
|
if (!st || !st->has_value()) {
|
|
std::cout << "no editor compilation database selected\n";
|
|
return 0;
|
|
}
|
|
std::cout << "profile=" << (*st)->selected_profile
|
|
<< " generation=" << (*st)->selected_generation_id << " ticket=" << (*st)->ticket
|
|
<< " path=" << (*st)->selected_compdb_path.string() << '\n';
|
|
return 0;
|
|
}
|
|
if (sub == "use") {
|
|
if (!opts.profile_name) {
|
|
std::cerr << "vbs compdb use: profile name required\n";
|
|
return 2;
|
|
}
|
|
auto st = Editor::select_editor_profile(project_root, build_root, *opts.profile_name);
|
|
if (!st) {
|
|
std::cerr << "vbs compdb use: " << st.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "selected editor profile '" << st->selected_profile << "' ("
|
|
<< st->selected_compdb_path.string() << ")\n";
|
|
return 0;
|
|
}
|
|
|
|
Watch::WatchLoopOptions wopts;
|
|
wopts.mode = Watch::WatchMode::MetadataOnly;
|
|
wopts.frontend = make_frontend_opts(project_root, build_root, {});
|
|
wopts.explicit_profile = opts.profile_name;
|
|
if (sub == "export") {
|
|
Void::Log::info("CompDb", "Exporting compile_commands.json in '{}'...",
|
|
project_root.string());
|
|
auto rep = Watch::run_reconcile_cycle(wopts, 0, false, std::nullopt);
|
|
if (!rep.compdb_published) {
|
|
std::cerr << "vbs compdb export: " << rep.diagnostic << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "exported compile_commands.json for profile '" << rep.profile_name << "'\n";
|
|
return 0;
|
|
}
|
|
|
|
Void::Log::info("CompDb", "Watching metadata in '{}' (Ctrl+C to stop)...",
|
|
project_root.string());
|
|
std::stop_source stop_src;
|
|
auto sig = Void::Process::SignalWatcher::create();
|
|
std::jthread sig_thread([&] {
|
|
if (sig) {
|
|
(void)sig->wait();
|
|
stop_src.request_stop();
|
|
}
|
|
});
|
|
auto res = Watch::run_watch_loop(wopts, stop_src.get_token());
|
|
return res ? 0 : 1;
|
|
}
|
|
|
|
struct DescribeCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::string profile_name{"debug"};
|
|
std::optional<std::string> compiler_arg;
|
|
};
|
|
|
|
int execute_describe_command(const DescribeCmdOptions &opts) {
|
|
std::optional<CompilerFamily> compiler_override;
|
|
if (opts.compiler_arg) {
|
|
try {
|
|
compiler_override = parse_compiler(*opts.compiler_arg);
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs describe: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
const auto fopts = make_frontend_opts(project_root, build_root, {});
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs describe: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto profs = Config::discover_profiles(*compiled, fopts);
|
|
if (!profs) {
|
|
std::cerr << "vbs describe: " << profs.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
try {
|
|
auto ctx = profs->resolve(opts.profile_name, compiler_override, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
auto desc = Config::evaluate_project(*compiled, ctx, fopts);
|
|
if (!desc) {
|
|
std::cerr << "vbs describe: " << desc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "{\"profile\":" << Util::json_quote(desc->profile.name)
|
|
<< ",\"targets\":" << desc->targets.size()
|
|
<< ",\"dependencies\":" << desc->dependencies.size() << "}\n";
|
|
return 0;
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs describe: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
struct WatchCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::optional<std::string> profile_arg;
|
|
std::vector<std::string> targets;
|
|
};
|
|
|
|
int execute_watch_command(WatchCmdOptions opts) {
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
Watch::WatchLoopOptions wopts;
|
|
wopts.mode = Watch::WatchMode::FullBuild;
|
|
wopts.frontend = make_frontend_opts(project_root, build_root, {});
|
|
wopts.explicit_profile = opts.profile_arg;
|
|
wopts.selected_targets = std::move(opts.targets);
|
|
wopts.on_cycle = [](const Watch::WatchCycleReport &rep) {
|
|
if (rep.build_succeeded) {
|
|
Void::Log::info("Watch", "Cycle #{} [{}] OK ({} executed, {} cached)", rep.cycle_index,
|
|
rep.profile_name, rep.executed_actions, rep.cached_actions);
|
|
} else {
|
|
Void::Log::error("Watch", "Cycle #{} [{}] FAILED: {}", rep.cycle_index,
|
|
rep.profile_name, rep.diagnostic);
|
|
}
|
|
};
|
|
|
|
Void::Log::info("Watch", "Watching '{}' (Ctrl+C to stop)...", project_root.string());
|
|
std::stop_source stop_src;
|
|
auto sig = Void::Process::SignalWatcher::create();
|
|
std::jthread sig_thread([&] {
|
|
if (sig) {
|
|
(void)sig->wait();
|
|
stop_src.request_stop();
|
|
}
|
|
});
|
|
auto res = Watch::run_watch_loop(wopts, stop_src.get_token());
|
|
return res ? 0 : 1;
|
|
}
|
|
|
|
struct BridgeCmdOptions {
|
|
std::optional<std::string> req_arg;
|
|
std::optional<std::string> out_arg;
|
|
};
|
|
|
|
int execute_bridge_command(const BridgeCmdOptions &opts) {
|
|
if (!opts.req_arg || !opts.out_arg) {
|
|
std::cerr << "vbs bridge: --request and --output are required\n";
|
|
return 2;
|
|
}
|
|
Void::Log::info("Bridge", "Processing CMake bridge request '{}'", *opts.req_arg);
|
|
auto r = Bridge::execute_cmake_bridge(*opts.req_arg, *opts.out_arg);
|
|
if (!r) {
|
|
std::cerr << "vbs bridge: " << r.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct PublishBinaryCmdOptions {
|
|
std::optional<std::string> registry_arg;
|
|
std::optional<std::string> name_arg;
|
|
std::optional<std::string> version_arg;
|
|
std::optional<std::string> variant_arg;
|
|
std::optional<std::string> file_arg;
|
|
std::optional<std::string> signing_key_arg;
|
|
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
|
};
|
|
|
|
int execute_publish_binary_command(const PublishBinaryCmdOptions &opts) {
|
|
const std::string registry_url = opts.registry_arg
|
|
? *opts.registry_arg
|
|
: Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
|
if (registry_url.empty() || !opts.name_arg || !opts.version_arg || !opts.variant_arg ||
|
|
!opts.file_arg) {
|
|
std::cerr << "vbs publish-binary: --registry, --name, --version, --variant, and --file "
|
|
"are required\n";
|
|
return 2;
|
|
}
|
|
Package::BinaryPublishOptions popts;
|
|
popts.registry_url = registry_url;
|
|
popts.package_name = *opts.name_arg;
|
|
popts.version = *opts.version_arg;
|
|
popts.variant_id = *opts.variant_arg;
|
|
popts.artifact_path = std::filesystem::path{*opts.file_arg};
|
|
popts.auth.bearer_token = Void::Configuration::get_env_or(opts.token_env, "");
|
|
if (opts.signing_key_arg) {
|
|
popts.signing_seed_path = std::filesystem::path{*opts.signing_key_arg};
|
|
}
|
|
Void::Log::info("Publish", "Uploading binary variant {}@{}/{} to {}...", popts.package_name,
|
|
popts.version, popts.variant_id, registry_url);
|
|
auto sha = Package::publish_binary_variant(popts);
|
|
if (!sha) {
|
|
std::cerr << "vbs publish-binary: " << sha.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "published binary " << popts.package_name << '@' << popts.version << '/'
|
|
<< popts.variant_id << " sha256=" << *sha << '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct BinariesCmdOptions {
|
|
std::optional<std::string> registry_arg;
|
|
std::optional<std::string> name_arg;
|
|
std::optional<std::string> version_arg;
|
|
std::optional<std::string> variant_arg;
|
|
std::optional<std::string> output_arg;
|
|
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
|
};
|
|
|
|
int execute_binaries_command(std::string_view sub, const BinariesCmdOptions &opts) {
|
|
const std::string registry_url = opts.registry_arg
|
|
? *opts.registry_arg
|
|
: Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
|
if (registry_url.empty() || !opts.name_arg || !opts.version_arg) {
|
|
std::cerr << "vbs binaries: --registry, --name, and --version are required\n";
|
|
return 2;
|
|
}
|
|
if (sub == "fetch" && (!opts.variant_arg || !opts.output_arg)) {
|
|
std::cerr << "vbs binaries fetch: --variant and --output are required\n";
|
|
return 2;
|
|
}
|
|
|
|
Package::RegistryAuth auth{Void::Configuration::get_env_or(opts.token_env, "")};
|
|
// Listing shows yanked variants (marked); fetch below only resolves non-yanked ones.
|
|
auto variants = Package::fetch_binary_variants(registry_url, *opts.name_arg, *opts.version_arg,
|
|
auth, sub == "list");
|
|
if (!variants) {
|
|
std::cerr << "vbs binaries: " << variants.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
if (sub == "list") {
|
|
for (const auto &variant : *variants) {
|
|
std::cout << variant.variant_id << ' ' << variant.sha256 << ' ' << variant.size_bytes
|
|
<< ' ' << ((variant.signature && variant.public_key) ? "signed" : "unsigned");
|
|
if (variant.yanked) {
|
|
std::cout << " yanked";
|
|
}
|
|
std::cout << '\n';
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
const auto match = std::ranges::find_if(*variants, [&](const Package::BinaryReleaseInfo &info) {
|
|
return info.variant_id == *opts.variant_arg;
|
|
});
|
|
if (match == variants->end()) {
|
|
std::cerr << "vbs binaries fetch: variant '" << *opts.variant_arg << "' not found\n";
|
|
return 1;
|
|
}
|
|
const std::string url = registry_url + "/api/v1/packages/" +
|
|
Void::Encoding::Url::encode(*opts.name_arg) + "/" +
|
|
Void::Encoding::Url::encode(*opts.version_arg) + "/binaries/" +
|
|
Void::Encoding::Url::encode(match->variant_id) + "/download";
|
|
auto downloaded = Package::download_binary_artifact(url, match->sha256,
|
|
std::filesystem::path{*opts.output_arg},
|
|
Package::RegistryAuth{auth.bearer_token});
|
|
if (!downloaded) {
|
|
std::cerr << "vbs binaries fetch: " << downloaded.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "fetched " << match->variant_id << " sha256=" << match->sha256 << " -> "
|
|
<< downloaded->string() << '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct PackCmdOptions {
|
|
std::string project_arg;
|
|
std::string out_arg{"package.tar"};
|
|
};
|
|
|
|
int execute_pack_command(const PackCmdOptions &opts) {
|
|
Void::Log::info("Pack", "Creating deterministic source archive '{}' from '{}'...", opts.out_arg,
|
|
opts.project_arg);
|
|
auto sha = Package::create_source_package(opts.project_arg, opts.out_arg, 0);
|
|
if (!sha) {
|
|
std::cerr << "vbs pack: " << sha.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "packed " << opts.out_arg << " sha256=" << *sha << '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct PublishCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> registry_arg;
|
|
std::optional<std::string> name_arg;
|
|
std::optional<std::string> version_arg;
|
|
std::string description;
|
|
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
|
std::optional<std::string> signing_key_arg;
|
|
bool is_private{false};
|
|
};
|
|
|
|
int execute_publish_command(const PublishCmdOptions &opts) {
|
|
const std::string registry_url = opts.registry_arg
|
|
? *opts.registry_arg
|
|
: Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
|
if (registry_url.empty() || !opts.name_arg || !opts.version_arg) {
|
|
std::cerr << "vbs publish: --registry, --name, and --version are required\n";
|
|
return 2;
|
|
}
|
|
|
|
Void::FileSystem::TempDir tmp{"vbs_publish_"};
|
|
const auto tar_path = tmp / (*opts.name_arg + "-" + *opts.version_arg + ".tar");
|
|
Void::Log::info("Publish", "Packing {}@{} from '{}'...", *opts.name_arg, *opts.version_arg,
|
|
opts.project_arg);
|
|
auto sha = Package::create_source_package(opts.project_arg, tar_path, 0);
|
|
if (!sha) {
|
|
std::cerr << "vbs publish: " << sha.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
Package::PublishOptions popts;
|
|
popts.registry_url = registry_url;
|
|
popts.package_name = *opts.name_arg;
|
|
popts.version = *opts.version_arg;
|
|
popts.archive_path = tar_path;
|
|
popts.description = opts.description;
|
|
popts.is_private = opts.is_private;
|
|
popts.auth.bearer_token = Void::Configuration::get_env_or(opts.token_env, "");
|
|
if (opts.signing_key_arg) {
|
|
popts.signing_seed_path = std::filesystem::path{*opts.signing_key_arg};
|
|
}
|
|
|
|
Void::Log::info("Publish", "Uploading {}@{} ({}) to {}...", *opts.name_arg, *opts.version_arg,
|
|
sha->substr(0, 12), registry_url);
|
|
auto receipt = Package::publish_package_archive(popts);
|
|
if (!receipt) {
|
|
std::cerr << "vbs publish: " << receipt.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << (receipt->already_published ? "already published " : "published ")
|
|
<< *opts.name_arg << "@" << *opts.version_arg << " sha256=" << receipt->sha256
|
|
<< '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct InstallCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::string profile_name{"debug"};
|
|
std::optional<std::string> compiler_arg;
|
|
std::optional<std::string> prefix_arg;
|
|
std::optional<std::string> pkg_name_arg;
|
|
std::string pkg_ver{"0.1.0"};
|
|
std::optional<std::string> component_arg;
|
|
bool no_cmake_export{false};
|
|
bool no_native_export{false};
|
|
};
|
|
|
|
int execute_install_command(const InstallCmdOptions &opts) {
|
|
if (!opts.prefix_arg || opts.prefix_arg->empty()) {
|
|
std::cerr << "vbs install: --prefix is required\n";
|
|
return 2;
|
|
}
|
|
|
|
std::optional<CompilerFamily> compiler_override;
|
|
if (opts.compiler_arg) {
|
|
try {
|
|
compiler_override = parse_compiler(*opts.compiler_arg);
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs install: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
|
|
Install::InstallOptions iopts;
|
|
iopts.prefix = *opts.prefix_arg;
|
|
iopts.package_version = opts.pkg_ver;
|
|
iopts.component = opts.component_arg;
|
|
iopts.export_cmake_config = !opts.no_cmake_export;
|
|
iopts.export_native_metadata = !opts.no_native_export;
|
|
|
|
std::error_code ec;
|
|
if (opts.pkg_name_arg && !opts.pkg_name_arg->empty()) {
|
|
iopts.package_name = *opts.pkg_name_arg;
|
|
} else {
|
|
const auto canon = std::filesystem::weakly_canonical(project_root, ec);
|
|
iopts.package_name = canon.filename().string();
|
|
if (iopts.package_name.empty()) {
|
|
iopts.package_name = "package";
|
|
}
|
|
}
|
|
|
|
Void::Log::info("Install", "Building profile '{}' before staging into '{}'...",
|
|
opts.profile_name, iopts.prefix.string());
|
|
const auto fopts = make_frontend_opts(project_root, build_root, {});
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs install: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto profs = Config::discover_profiles(*compiled, fopts);
|
|
if (!profs) {
|
|
std::cerr << "vbs install: " << profs.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
ProfileContext ctx;
|
|
try {
|
|
ctx = profs->resolve(opts.profile_name, compiler_override, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs install: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
auto tc = Toolchain::discover_and_probe(ctx, build_root / ".vbs" / "probes");
|
|
if (!tc) {
|
|
std::cerr << "vbs install: " << tc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto desc = Config::evaluate_project(*compiled, ctx, fopts);
|
|
if (!desc) {
|
|
std::cerr << "vbs install: " << desc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
const auto snap_inputs = collect_snapshot_inputs(*desc, project_root);
|
|
Snapshot::SnapshotOptions sopts;
|
|
sopts.project_root = project_root;
|
|
sopts.snapshot_store_dir = build_root / ".vbs" / "snapshots";
|
|
sopts.reproducibility_epoch = ctx.reproducibility_epoch;
|
|
auto snap =
|
|
Snapshot::capture_snapshot(sopts, snap_inputs.explicit_files, snap_inputs.scan_roots);
|
|
if (!snap) {
|
|
std::cerr << "vbs install: " << snap.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto graph =
|
|
Graph::lower_project_graph(*desc, *tc, {project_root, build_root, &(*snap), {}, 32768});
|
|
if (!graph) {
|
|
std::cerr << "vbs install: " << graph.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto state = State::StateStore::open(build_root / ".vbs");
|
|
if (!state) {
|
|
std::cerr << "vbs install: " << state.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto exec = Execution::execute_graph(*graph, *state);
|
|
if (!exec || !exec->succeeded) {
|
|
std::cerr << "vbs install: build failed before staging\n";
|
|
if (!exec) {
|
|
std::cerr << exec.error().message() << '\n';
|
|
}
|
|
return 1;
|
|
}
|
|
Void::Log::info("Install", "Staging and installing package '{}' ({}) to '{}'...",
|
|
iopts.package_name, iopts.package_version, iopts.prefix.string());
|
|
auto inst = Install::install_project(*desc, *graph, iopts);
|
|
if (!inst) {
|
|
std::cerr << "vbs install: " << inst.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "installed " << inst->installed_files.size() << " files to "
|
|
<< inst->prefix.string() << " (manifest=" << inst->manifest_path.string() << ")\n";
|
|
return 0;
|
|
}
|
|
|
|
struct UninstallCmdOptions {
|
|
std::optional<std::string> prefix_arg;
|
|
};
|
|
|
|
int execute_uninstall_command(const UninstallCmdOptions &opts) {
|
|
if (!opts.prefix_arg || opts.prefix_arg->empty()) {
|
|
std::cerr << "vbs uninstall: --prefix is required\n";
|
|
return 2;
|
|
}
|
|
Void::Log::info("Install", "Uninstalling VBS-owned files from '{}'...", *opts.prefix_arg);
|
|
auto removed = Install::uninstall_prefix(*opts.prefix_arg);
|
|
if (!removed) {
|
|
std::cerr << "vbs uninstall: " << removed.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "uninstalled " << *removed << " VBS-owned files from " << *opts.prefix_arg << '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct GcCmdOptions {
|
|
std::string build_arg;
|
|
std::string max_bytes_arg{"0"};
|
|
};
|
|
|
|
int execute_gc_command(const GcCmdOptions &opts) {
|
|
std::uint64_t max_bytes = 0;
|
|
const auto [ptr, ec] =
|
|
std::from_chars(opts.max_bytes_arg.data(),
|
|
opts.max_bytes_arg.data() + opts.max_bytes_arg.size(), max_bytes);
|
|
if (ec != std::errc{} || ptr != opts.max_bytes_arg.data() + opts.max_bytes_arg.size()) {
|
|
std::cerr << "vbs gc: invalid --max-bytes value '" << opts.max_bytes_arg << "'\n";
|
|
return 2;
|
|
}
|
|
Void::Log::info("GC", "Running state recovery and CAS garbage collection in '{}'...",
|
|
opts.build_arg);
|
|
auto st = State::StateStore::open(std::filesystem::path{opts.build_arg} / ".vbs");
|
|
if (!st) {
|
|
std::cerr << "vbs gc: " << st.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto stats = st->gc(max_bytes);
|
|
if (!stats) {
|
|
std::cerr << "vbs gc: " << stats.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "gc complete: removed=" << stats->removed_blobs
|
|
<< " freed_bytes=" << stats->freed_bytes << " retained=" << stats->retained_blobs
|
|
<< '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct DaemonCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::optional<std::string> socket_arg;
|
|
std::string profile_arg{"debug"};
|
|
std::vector<std::string> targets;
|
|
std::string jobs_arg{"0"};
|
|
std::string editor_arg{"all"};
|
|
std::optional<std::string> member_arg;
|
|
std::string graph_format_arg{"dot"};
|
|
std::optional<std::string> file_filter_arg;
|
|
std::string limit_arg{"50"};
|
|
std::string timeout_ms_arg{"2000"};
|
|
bool force{false};
|
|
std::string max_bytes_arg{"0"};
|
|
std::string max_requests_arg{"0"};
|
|
};
|
|
|
|
std::filesystem::path resolve_daemon_socket(const DaemonCmdOptions &opts,
|
|
const std::filesystem::path &build_root) {
|
|
return opts.socket_arg ? std::filesystem::path{*opts.socket_arg}
|
|
: build_root / ".vbs" / "vbs-daemon.sock";
|
|
}
|
|
|
|
int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
const auto socket_path = resolve_daemon_socket(opts, build_root);
|
|
|
|
if (sub == "run") {
|
|
std::size_t max_requests = 0;
|
|
const auto [ptr, ec] = std::from_chars(
|
|
opts.max_requests_arg.data(),
|
|
opts.max_requests_arg.data() + opts.max_requests_arg.size(), max_requests);
|
|
if (ec != std::errc{} ||
|
|
ptr != opts.max_requests_arg.data() + opts.max_requests_arg.size()) {
|
|
std::cerr << "vbs daemon run: invalid --max-requests value '" << opts.max_requests_arg
|
|
<< "'\n";
|
|
return 2;
|
|
}
|
|
Daemon::DaemonOptions daemon_opts;
|
|
daemon_opts.project_root = project_root;
|
|
daemon_opts.build_root = build_root;
|
|
daemon_opts.socket_path = socket_path;
|
|
daemon_opts.max_requests = max_requests;
|
|
auto server = Daemon::DaemonServer::create(std::move(daemon_opts));
|
|
if (!server) {
|
|
std::cerr << "vbs daemon: " << server.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
Void::Log::info("Daemon", "Serving '{}' (Ctrl+C to stop)...", socket_path.string());
|
|
std::stop_source stop_src;
|
|
auto sig = Void::Process::SignalWatcher::create();
|
|
std::jthread sig_thread([&] {
|
|
if (sig) {
|
|
(void)sig->wait();
|
|
stop_src.request_stop();
|
|
}
|
|
});
|
|
auto result = server->run(stop_src.get_token());
|
|
if (!result) {
|
|
std::cerr << "vbs daemon: " << result.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::string request_json;
|
|
if (sub == "status") {
|
|
request_json = R"({"op":"status"})";
|
|
} else if (sub == "stop") {
|
|
request_json = R"({"op":"shutdown"})";
|
|
} else if (sub == "recover") {
|
|
request_json = R"({"op":"recover"})";
|
|
} else if (sub == "gc") {
|
|
std::uint64_t max_bytes = 0;
|
|
const auto [ptr, ec] =
|
|
std::from_chars(opts.max_bytes_arg.data(),
|
|
opts.max_bytes_arg.data() + opts.max_bytes_arg.size(), max_bytes);
|
|
if (ec != std::errc{} || ptr != opts.max_bytes_arg.data() + opts.max_bytes_arg.size()) {
|
|
std::cerr << "vbs daemon gc: invalid --max-bytes value '" << opts.max_bytes_arg
|
|
<< "'\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(R"({{"op":"gc","max_bytes":{}}})", max_bytes);
|
|
} else if (sub == "describe") {
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon describe: --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
request_json =
|
|
std::format(R"({{"op":"describe","profile":{}}})", Util::json_quote(opts.profile_arg));
|
|
} else if (sub == "graph") {
|
|
request_json = std::format(R"({{"op":"graph","profile":{},"format":{}}})",
|
|
Util::json_quote(opts.profile_arg),
|
|
Util::json_quote(opts.graph_format_arg));
|
|
} else if (sub == "editor_status") {
|
|
request_json = R"({"op":"editor_status"})";
|
|
} else if (sub == "workspace_members") {
|
|
request_json = R"({"op":"workspace_members"})";
|
|
} else if (sub == "ide_configs") {
|
|
request_json = R"({"op":"ide_configs"})";
|
|
} else if (sub == "compdb_generations") {
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon compdb generations: --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(R"({{"op":"compdb_generations","profile":{}}})",
|
|
Util::json_quote(opts.profile_arg));
|
|
} else if (sub == "compdb_entries") {
|
|
request_json =
|
|
std::format(R"({{"op":"compdb_entries","limit":{}{}}})", opts.limit_arg,
|
|
opts.file_filter_arg
|
|
? std::format(",\"file\":{}", Util::json_quote(*opts.file_filter_arg))
|
|
: std::string{});
|
|
} else if (sub == "watch_poll") {
|
|
request_json = R"({"op":"watch_poll"})";
|
|
} else if (sub == "watch_wait") {
|
|
std::int64_t timeout_ms = 0;
|
|
const auto [ptr, ec] =
|
|
std::from_chars(opts.timeout_ms_arg.data(),
|
|
opts.timeout_ms_arg.data() + opts.timeout_ms_arg.size(), timeout_ms);
|
|
if (ec != std::errc{} || ptr != opts.timeout_ms_arg.data() + opts.timeout_ms_arg.size() ||
|
|
timeout_ms < 0 || timeout_ms > 5000) {
|
|
std::cerr << "vbs daemon ide wait: invalid --timeout-ms value '" << opts.timeout_ms_arg
|
|
<< "' (0..5000)\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(R"({{"op":"watch_wait","timeout_ms":{}}})", timeout_ms);
|
|
} else if (sub == "ide_generate") {
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon ide generate: --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(
|
|
R"({{"op":"ide_generate","profile":{},"editor":{},"force":{}{}}})",
|
|
Util::json_quote(opts.profile_arg), Util::json_quote(opts.editor_arg),
|
|
opts.force ? "true" : "false",
|
|
opts.member_arg ? std::format(",\"member\":{}", Util::json_quote(*opts.member_arg))
|
|
: std::string{});
|
|
} else if (sub == "compdb_use") {
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon compdb use: --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(R"({{"op":"compdb_use","profile":{}}})",
|
|
Util::json_quote(opts.profile_arg));
|
|
} else if (sub == "compdb_export") {
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon compdb export: --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(
|
|
R"({{"op":"compdb_export","profile":{}{}}})", Util::json_quote(opts.profile_arg),
|
|
opts.member_arg ? std::format(",\"member\":{}", Util::json_quote(*opts.member_arg))
|
|
: std::string{});
|
|
} else if (sub == "ide_query") {
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon ide query: --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
request_json = std::format(
|
|
R"({{"op":"ide_query","profile":{}{}}})", Util::json_quote(opts.profile_arg),
|
|
opts.member_arg ? std::format(",\"member\":{}", Util::json_quote(*opts.member_arg))
|
|
: std::string{});
|
|
} else if (sub == "build" || sub == "test") {
|
|
std::size_t jobs = 0;
|
|
const auto [ptr, ec] = std::from_chars(opts.jobs_arg.data(),
|
|
opts.jobs_arg.data() + opts.jobs_arg.size(), jobs);
|
|
if (ec != std::errc{} || ptr != opts.jobs_arg.data() + opts.jobs_arg.size() || jobs > 256) {
|
|
std::cerr << "vbs daemon " << sub << ": invalid --jobs value '" << opts.jobs_arg
|
|
<< "'\n";
|
|
return 2;
|
|
}
|
|
if (opts.profile_arg.empty()) {
|
|
std::cerr << "vbs daemon " << sub << ": --profile must not be empty\n";
|
|
return 2;
|
|
}
|
|
const std::string targets_json = Util::json_array(
|
|
opts.targets, [](const std::string &target) { return Util::json_quote(target); });
|
|
request_json = std::format(R"({{"op":"{}","profile":{},"targets":{},"jobs":{}}})", sub,
|
|
Util::json_quote(opts.profile_arg), targets_json, jobs);
|
|
} else {
|
|
std::cerr << "vbs daemon: unknown command '" << sub << "'\n";
|
|
return 2;
|
|
}
|
|
|
|
// Describe evaluates the whole project graph (descriptor compile, toolchain probe, lowering),
|
|
// which can exceed the 2 s default; the daemon bounds evaluation at 10 s.
|
|
const auto request_timeout =
|
|
(sub == "build" || sub == "test") ? std::chrono::milliseconds{std::chrono::hours{1}}
|
|
: (sub == "watch_poll" || sub == "editor_status") ? std::chrono::milliseconds{5000}
|
|
: sub == "compdb_use" ? std::chrono::milliseconds{10000}
|
|
: sub == "watch_wait" ? std::chrono::milliseconds{10000}
|
|
: sub == "describe" ? std::chrono::milliseconds{15000}
|
|
: (sub == "ide_query" || sub == "ide_generate" || sub == "compdb_export")
|
|
? std::chrono::milliseconds{30000}
|
|
: std::chrono::milliseconds{2000};
|
|
auto response = Daemon::request(socket_path, request_json, request_timeout);
|
|
if (!response) {
|
|
std::cerr << "vbs daemon: " << response.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << *response << '\n';
|
|
const auto parsed = Void::Json::Strict::parse(*response);
|
|
if (!parsed) {
|
|
return 1;
|
|
}
|
|
const auto *object = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
|
|
if (object == nullptr) {
|
|
return 1;
|
|
}
|
|
const auto ok = object->find("ok");
|
|
if (ok == object->end()) {
|
|
return 1;
|
|
}
|
|
const auto *flag = std::get_if<bool>(&ok->second.value);
|
|
return (flag != nullptr && *flag) ? 0 : 1;
|
|
}
|
|
|
|
struct GenTestsCmdOptions {
|
|
std::optional<std::string> input_arg;
|
|
std::optional<std::string> out_arg;
|
|
};
|
|
|
|
int execute_gen_tests_command(const GenTestsCmdOptions &opts) {
|
|
if (!opts.input_arg || !opts.out_arg) {
|
|
std::cerr << "vbs gen-tests: --input and --output-dir are required\n";
|
|
return 2;
|
|
}
|
|
Void::Log::info("GenTests", "Scanning test cases in '{}'...", *opts.input_arg);
|
|
auto gen = SelfHost::generate_test_registration_unit(*opts.input_arg, *opts.out_arg);
|
|
if (!gen) {
|
|
std::cerr << "vbs gen-tests: " << gen.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << gen->generated_cpp.string() << " (" << gen->cases.size() << " tests)\n";
|
|
return 0;
|
|
}
|
|
|
|
struct DepsCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::string profile_name{"debug"};
|
|
bool offline{false};
|
|
};
|
|
|
|
int execute_deps_command(std::string_view sub, const DepsCmdOptions &opts) {
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
const auto fopts = make_frontend_opts(project_root, build_root, {});
|
|
Void::Log::info("Deps", "{} dependencies for profile '{}' in '{}'...",
|
|
sub == "update" ? "Updating" : "Resolving", opts.profile_name,
|
|
project_root.string());
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs deps: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto profs = Config::discover_profiles(*compiled, fopts);
|
|
if (!profs) {
|
|
std::cerr << "vbs deps: " << profs.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto ctx = profs->resolve(opts.profile_name, std::nullopt, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
auto desc = Config::evaluate_project(*compiled, ctx, fopts);
|
|
if (!desc) {
|
|
std::cerr << "vbs deps: " << desc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::vector<Package::NormalizedDependency> toml_deps;
|
|
for (const auto &df : desc->dependency_files) {
|
|
auto loaded = Package::load_dependencies_toml(project_root / df);
|
|
if (!loaded) {
|
|
std::cerr << "vbs deps: " << loaded.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
toml_deps.insert(toml_deps.end(), loaded->begin(), loaded->end());
|
|
}
|
|
auto merged = Package::merge_dependencies(desc->dependencies, toml_deps);
|
|
if (!merged) {
|
|
std::cerr << "vbs deps: " << merged.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
const auto lock_path = project_root / "vbs.lock";
|
|
auto existing = Package::load_pinned_lock(lock_path);
|
|
if (!existing) {
|
|
std::cerr << "vbs deps: " << existing.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
const std::string base_rev = existing->has_value() ? (*existing)->revision_hash : std::string{};
|
|
|
|
auto resolved = resolve_and_materialize_deps(ctx, *merged, project_root, build_root, *existing,
|
|
opts.offline, sub == "update", {});
|
|
if (!resolved) {
|
|
std::cerr << "vbs deps: " << resolved.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto pub =
|
|
Package::publish_lock_context_cas(lock_path, base_rev, resolved->lock_context, false);
|
|
if (!pub) {
|
|
std::cerr << "vbs deps: " << pub.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "updated vbs.lock revision=" << pub->revision_hash.substr(0, 12) << '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct InitCmdOptions {
|
|
std::string dir_arg;
|
|
std::optional<std::string> name_arg;
|
|
bool library{false};
|
|
bool with_toml{false};
|
|
bool force{false};
|
|
};
|
|
|
|
int execute_init_command(const InitCmdOptions &opts) {
|
|
std::error_code ec;
|
|
const std::filesystem::path target_dir = std::filesystem::path{opts.dir_arg}.lexically_normal();
|
|
std::filesystem::create_directories(target_dir, ec);
|
|
if (ec) {
|
|
std::cerr << "vbs init: cannot create directory '" << target_dir.string()
|
|
<< "': " << ec.message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
const auto canon = std::filesystem::weakly_canonical(target_dir, ec);
|
|
std::string raw_name =
|
|
(opts.name_arg && !opts.name_arg->empty()) ? *opts.name_arg : canon.filename().string();
|
|
if (raw_name.empty()) {
|
|
raw_name = "app";
|
|
}
|
|
std::string ident;
|
|
for (const char c : raw_name) {
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
|
c == '_') {
|
|
ident.push_back(c);
|
|
} else if (c == '-' || c == ' ') {
|
|
ident.push_back('_');
|
|
}
|
|
}
|
|
if (ident.empty() || (ident.front() >= '0' && ident.front() <= '9')) {
|
|
ident = "vbs_" + ident;
|
|
}
|
|
|
|
const auto build_cpp = target_dir / "build.cpp";
|
|
if (!opts.force && std::filesystem::exists(build_cpp, ec)) {
|
|
std::cerr << "vbs init: '" << build_cpp.string()
|
|
<< "' already exists (pass --force to overwrite)\n";
|
|
return 2;
|
|
}
|
|
|
|
auto write_checked = [&](const std::filesystem::path &p, std::string_view content) -> bool {
|
|
auto w = Platform::atomic_write_file(p, content, false);
|
|
if (!w) {
|
|
std::cerr << "vbs init: failed writing '" << p.string() << "': " << w.error().message()
|
|
<< '\n';
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
std::string build_src =
|
|
"#include <Void/Build.hpp>\n\nvoid build(Void::Build::Project &project) {\n";
|
|
if (opts.with_toml) {
|
|
build_src += " project.dependencies_file(\"vbs.deps.toml\");\n\n";
|
|
}
|
|
if (opts.library) {
|
|
build_src += " auto lib = project.static_library(\"" + raw_name +
|
|
"\");\n"
|
|
" lib.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
|
|
" .sources({\"src/" +
|
|
ident +
|
|
".cpp\"})\n"
|
|
" .include_directory(\"include\", Void::Build::Visibility::Public)\n"
|
|
" .install(\"lib\");\n"
|
|
" project.install_directory(\"include\", \"include\");\n\n"
|
|
" auto t = project.test(\"" +
|
|
ident +
|
|
"_tests\");\n"
|
|
" t.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
|
|
" .sources({\"tests/test_main.cpp\"})\n"
|
|
" .link(lib);\n";
|
|
} else {
|
|
build_src += " auto lib = project.static_library(\"" + ident +
|
|
"_lib\");\n"
|
|
" lib.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
|
|
" .sources({\"src/" +
|
|
ident +
|
|
".cpp\"})\n"
|
|
" .include_directory(\"include\", Void::Build::Visibility::Public);\n\n"
|
|
" auto app = project.executable(\"" +
|
|
raw_name +
|
|
"\");\n"
|
|
" app.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
|
|
" .sources({\"src/main.cpp\"})\n"
|
|
" .link(lib)\n"
|
|
" .install(\"bin\");\n\n"
|
|
" auto t = project.test(\"" +
|
|
ident +
|
|
"_tests\");\n"
|
|
" t.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
|
|
" .sources({\"tests/test_main.cpp\"})\n"
|
|
" .link(lib);\n";
|
|
}
|
|
build_src += "}\n";
|
|
|
|
if (!write_checked(build_cpp, build_src)) {
|
|
return 1;
|
|
}
|
|
if (!write_checked(target_dir / "include" / (ident + ".hpp"),
|
|
"#pragma once\n\nnamespace " + ident + " {\nint answer();\n}\n")) {
|
|
return 1;
|
|
}
|
|
if (!write_checked(target_dir / "src" / (ident + ".cpp"),
|
|
"#include \"" + ident + ".hpp\"\n\nnamespace " + ident +
|
|
" {\nint answer() { return 42; }\n}\n")) {
|
|
return 1;
|
|
}
|
|
if (!opts.library) {
|
|
if (!write_checked(target_dir / "src" / "main.cpp",
|
|
"#include \"" + ident +
|
|
".hpp\"\n#include <iostream>\n\nint main() {\n std::cout << \"" +
|
|
raw_name + ": answer=\" << " + ident +
|
|
"::answer() << '\\n';\n return 0;\n}\n")) {
|
|
return 1;
|
|
}
|
|
}
|
|
if (!write_checked(target_dir / "tests" / "test_main.cpp",
|
|
"#include \"" + ident + ".hpp\"\n\nint main() { return " + ident +
|
|
"::answer() == 42 ? 0 : 1; }\n")) {
|
|
return 1;
|
|
}
|
|
if (!write_checked(target_dir / ".gitignore",
|
|
"/build/\n/.vbs/\n/compile_commands.json\n.env\n")) {
|
|
return 1;
|
|
}
|
|
if (opts.with_toml) {
|
|
if (!write_checked(target_dir / "vbs.deps.toml",
|
|
"# Declarative dependency manifest for " + raw_name + "\n")) {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
Void::Log::info("Init", "Initialized VBS {} project '{}' in '{}'",
|
|
opts.library ? "library" : "application", raw_name, target_dir.string());
|
|
std::cout << "initialized " << raw_name << " in " << target_dir.string() << '\n';
|
|
return 0;
|
|
}
|
|
|
|
struct CleanCmdOptions {
|
|
std::string build_arg;
|
|
bool clean_all{false};
|
|
};
|
|
|
|
int execute_clean_command(const CleanCmdOptions &opts) {
|
|
const std::filesystem::path build_root{opts.build_arg};
|
|
std::error_code ec;
|
|
if (!std::filesystem::exists(build_root, ec)) {
|
|
std::cout << "clean: nothing to remove in " << build_root.string() << '\n';
|
|
return 0;
|
|
}
|
|
if (opts.clean_all) {
|
|
Void::Log::info("Clean", "Removing entire build directory '{}'...", build_root.string());
|
|
std::filesystem::remove_all(build_root, ec);
|
|
if (ec) {
|
|
std::cerr << "vbs clean: failed removing '" << build_root.string()
|
|
<< "': " << ec.message() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "cleaned " << build_root.string() << " (all)\n";
|
|
return 0;
|
|
}
|
|
const auto profiles_dir = build_root / "profiles";
|
|
std::size_t removed = 0;
|
|
if (std::filesystem::exists(profiles_dir, ec)) {
|
|
Void::Log::info("Clean", "Removing profile outputs under '{}' (preserving .vbs/cache)...",
|
|
profiles_dir.string());
|
|
removed += static_cast<std::size_t>(std::filesystem::remove_all(profiles_dir, ec));
|
|
}
|
|
const auto compdb = build_root / "compile_commands.json";
|
|
if (std::filesystem::exists(compdb, ec) || std::filesystem::is_symlink(compdb, ec)) {
|
|
std::filesystem::remove(compdb, ec);
|
|
++removed;
|
|
}
|
|
std::cout << "cleaned " << removed << " path(s) in " << build_root.string()
|
|
<< " (preserved .vbs cache/state)\n";
|
|
return 0;
|
|
}
|
|
|
|
int execute_doctor_command() {
|
|
std::cout << "VoidBuildSystem " << Bootstrap::version << " (" << Bootstrap::os << "/"
|
|
<< Bootstrap::architecture << ")\n";
|
|
auto check_tool = [](std::string_view label, const char *bin, bool required) {
|
|
auto found = Platform::resolve_executable(bin);
|
|
std::cout << " " << (found ? "[ok] " : (required ? "[FAIL]" : "[opt] ")) << " " << label
|
|
<< ": " << (found ? found->string() : std::string{"not found"}) << '\n';
|
|
return found.has_value();
|
|
};
|
|
const bool has_gcc = check_tool("g++", "g++", true);
|
|
(void)check_tool("clang++", "clang++", false);
|
|
(void)check_tool("clang-scan-deps", "clang-scan-deps", false);
|
|
(void)check_tool("ar", "ar", true);
|
|
(void)check_tool("git", "git", false);
|
|
(void)check_tool("cmake", "cmake", false);
|
|
(void)check_tool("ninja", "ninja", false);
|
|
(void)check_tool("meson", "meson", false);
|
|
(void)check_tool("conan", "conan", false);
|
|
(void)check_tool("clang-format", "clang-format", false);
|
|
|
|
std::cout << " [ok] Void::Crypto built-in SHA-256: enabled\n";
|
|
std::cout << " [ok] Void::Archive built-in USTAR/PAX: enabled\n";
|
|
|
|
if (has_gcc) {
|
|
Void::FileSystem::TempDir tmp{"vbs_doc_"};
|
|
auto ctx =
|
|
default_profiles().resolve("debug", CompilerFamily::Gcc, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
if (auto tc = Toolchain::discover_and_probe(ctx, tmp.path()); tc) {
|
|
std::cout << " [ok] GCC probe: " << tc->version_banner
|
|
<< " (c++26=" << (tc->supports_cxx26 ? "yes" : "no")
|
|
<< ", reflection=" << (tc->supports_reflection ? "yes" : "no")
|
|
<< ", p1689=" << (tc->supports_p1689_scan ? "yes" : "no") << ")\n";
|
|
}
|
|
}
|
|
return has_gcc ? 0 : 1;
|
|
}
|
|
|
|
struct GraphCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::string profile_name{"debug"};
|
|
std::optional<std::string> compiler_arg;
|
|
std::string format{"dot"};
|
|
};
|
|
|
|
int execute_graph_command(const GraphCmdOptions &opts) {
|
|
if (opts.format != "dot" && opts.format != "json") {
|
|
std::cerr << "vbs graph: unsupported --format '" << opts.format
|
|
<< "' (expected 'dot' or 'json')\n";
|
|
return 2;
|
|
}
|
|
std::optional<CompilerFamily> compiler_override;
|
|
if (opts.compiler_arg) {
|
|
try {
|
|
compiler_override = parse_compiler(*opts.compiler_arg);
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs graph: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
}
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
const auto fopts = make_frontend_opts(project_root, build_root, {});
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs graph: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto profs = Config::discover_profiles(*compiled, fopts);
|
|
if (!profs) {
|
|
std::cerr << "vbs graph: " << profs.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
ProfileContext ctx;
|
|
try {
|
|
ctx = profs->resolve(opts.profile_name, compiler_override, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs graph: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
auto desc = Config::evaluate_project(*compiled, ctx, fopts);
|
|
if (!desc) {
|
|
std::cerr << "vbs graph: " << desc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
if (opts.format == "json") {
|
|
std::cout << "{\"profile\":" << Util::json_quote(desc->profile.name) << ",\"targets\":";
|
|
std::cout << Util::json_array(desc->targets, [](const TargetRecord &t) {
|
|
return "{\"name\":" + Util::json_quote(t.name) +
|
|
",\"kind\":" + Util::json_quote(target_kind_name(t.kind)) + ",\"links\":" +
|
|
Util::json_array(t.links,
|
|
[](const LinkRecord &l) {
|
|
return Util::json_quote(
|
|
l.kind == LinkKind::Imported
|
|
? (l.dependency + "::" + l.target)
|
|
: l.target);
|
|
}) +
|
|
'}';
|
|
});
|
|
std::cout << "}\n";
|
|
return 0;
|
|
}
|
|
|
|
std::cout << "digraph vbs_targets {\n";
|
|
std::cout << " rankdir=LR;\n";
|
|
for (const auto &t : desc->targets) {
|
|
std::cout << " " << Util::json_quote(t.name) << " [label="
|
|
<< Util::json_quote(t.name + "\n(" + std::string{target_kind_name(t.kind)} + ")")
|
|
<< "];\n";
|
|
}
|
|
for (const auto &t : desc->targets) {
|
|
for (const auto &pre : t.prerequisites) {
|
|
std::cout << " " << Util::json_quote(t.name) << " -> " << Util::json_quote(pre)
|
|
<< " [style=dashed];\n";
|
|
}
|
|
for (const auto &l : t.links) {
|
|
const std::string dst =
|
|
l.kind == LinkKind::Imported ? (l.dependency + "::" + l.target) : l.target;
|
|
std::cout << " " << Util::json_quote(t.name) << " -> " << Util::json_quote(dst)
|
|
<< ";\n";
|
|
}
|
|
}
|
|
std::cout << "}\n";
|
|
return 0;
|
|
}
|
|
|
|
struct IdeCmdOptions {
|
|
std::string project_arg;
|
|
std::optional<std::string> build_arg;
|
|
std::string profile_name{"debug"};
|
|
std::optional<std::string> compiler_arg;
|
|
std::string editor_name{"all"};
|
|
bool force{false};
|
|
};
|
|
|
|
int execute_ide_command(std::string_view sub, const IdeCmdOptions &opts) {
|
|
Editor::IdeKind ide_kind = Editor::IdeKind::All;
|
|
if (sub == "generate") {
|
|
auto parsed_kind = Editor::parse_ide_kind(opts.editor_name);
|
|
if (!parsed_kind) {
|
|
std::cerr << "vbs ide: " << parsed_kind.error().message() << '\n';
|
|
return 2;
|
|
}
|
|
ide_kind = *parsed_kind;
|
|
}
|
|
|
|
std::optional<CompilerFamily> compiler_override;
|
|
if (opts.compiler_arg) {
|
|
try {
|
|
compiler_override = parse_compiler(*opts.compiler_arg);
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs ide: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
const std::filesystem::path project_root{opts.project_arg};
|
|
const std::filesystem::path build_root =
|
|
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
|
const auto fopts = make_frontend_opts(project_root, build_root, {});
|
|
|
|
auto compiled = Config::compile_descriptor(fopts);
|
|
if (!compiled) {
|
|
std::cerr << "vbs ide: " << compiled.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
auto profs = Config::discover_profiles(*compiled, fopts);
|
|
if (!profs) {
|
|
std::cerr << "vbs ide: " << profs.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
ProfileContext ctx;
|
|
try {
|
|
ctx = profs->resolve(opts.profile_name, compiler_override, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
} catch (const std::exception &ex) {
|
|
std::cerr << "vbs ide: " << ex.what() << '\n';
|
|
return 2;
|
|
}
|
|
|
|
auto tc = Toolchain::discover_and_probe(ctx, build_root / ".vbs" / "probes");
|
|
if (!tc) {
|
|
std::cerr << "vbs ide: " << tc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
auto desc = Config::evaluate_project(*compiled, ctx, fopts);
|
|
if (!desc) {
|
|
std::cerr << "vbs ide: " << desc.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
// MetadataOnly provider loading: inspect cached provider exports if already built; never
|
|
// run foreign configure/build/fetch. On unbuilt checkouts, synthesize empty placeholders so
|
|
// IDE configuration still succeeds before the initial build.
|
|
std::map<std::string, Graph::ImportedTargetInterface, std::less<>> imported_targets;
|
|
std::vector<Package::NormalizedDependency> toml_deps;
|
|
std::error_code ec;
|
|
for (const auto &df : desc->dependency_files) {
|
|
const auto p = std::filesystem::path{df}.is_absolute() ? std::filesystem::path{df}
|
|
: (project_root / df);
|
|
if (auto loaded = Package::load_dependencies_toml(p); loaded) {
|
|
toml_deps.insert(toml_deps.end(), loaded->begin(), loaded->end());
|
|
}
|
|
}
|
|
auto merged_deps = Package::merge_dependencies(desc->dependencies, toml_deps);
|
|
if (merged_deps && !merged_deps->empty()) {
|
|
std::optional<Package::PinnedLock> pinned_lock;
|
|
if (auto pl = Package::load_pinned_lock(project_root / "vbs.lock"); pl) {
|
|
pinned_lock = *pl;
|
|
}
|
|
auto resolved = resolve_and_materialize_deps(ctx, *merged_deps, project_root, build_root,
|
|
pinned_lock, true, false, {});
|
|
if (resolved) {
|
|
for (const auto &[dep_rec, mat] : resolved->materialized) {
|
|
DependencyRecord effective_dep = dep_rec;
|
|
if (effective_dep.builder.empty()) {
|
|
if (std::filesystem::is_regular_file(mat.source_dir / "build.cpp", ec)) {
|
|
effective_dep.builder = "native";
|
|
} else if (std::filesystem::is_regular_file(mat.source_dir / "CMakeLists.txt",
|
|
ec)) {
|
|
effective_dep.builder = "cmake";
|
|
} else if (std::filesystem::is_regular_file(mat.source_dir / "meson.build",
|
|
ec)) {
|
|
effective_dep.builder = "meson";
|
|
}
|
|
}
|
|
if (!effective_dep.builder.empty()) {
|
|
Provider::ProviderRequest preq{effective_dep,
|
|
mat,
|
|
ctx,
|
|
*tc,
|
|
build_root / ".vbs" / "providers",
|
|
Provider::ProviderMode::MetadataOnly,
|
|
{},
|
|
fopts.sdk_include_dir,
|
|
fopts.sdk_source_dir,
|
|
fopts.sdk_library_path.empty()
|
|
? std::nullopt
|
|
: std::optional{fopts.sdk_library_path},
|
|
nullptr,
|
|
0};
|
|
auto exp = Provider::resolve_foreign_provider(preq);
|
|
if (exp && !exp->stale_needs_build) {
|
|
for (const auto &[k, v] : exp->targets) {
|
|
imported_targets[k] = v;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (const auto &t : desc->targets) {
|
|
for (const auto &link : t.links) {
|
|
if (link.kind == LinkKind::Imported) {
|
|
const std::string key = link.dependency + "::" + link.target;
|
|
if (!imported_targets.contains(key) && !imported_targets.contains(link.target)) {
|
|
Void::Log::warn(
|
|
"Ide",
|
|
"Dependency target '{}' is not built yet; run 'vbs build' to populate "
|
|
"imported include paths",
|
|
key);
|
|
imported_targets.emplace(
|
|
key, Graph::ImportedTargetInterface{
|
|
link.dependency, link.target, {}, {}, {}, {}, {}, {}, {}, {}, {}});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const auto snap_inputs = collect_snapshot_inputs(*desc, project_root);
|
|
Snapshot::SnapshotOptions sopts;
|
|
sopts.project_root = project_root;
|
|
sopts.snapshot_store_dir = build_root / ".vbs" / "snapshots";
|
|
sopts.reproducibility_epoch = ctx.reproducibility_epoch;
|
|
sopts.materialize = false;
|
|
auto snap =
|
|
Snapshot::capture_snapshot(sopts, snap_inputs.explicit_files, snap_inputs.scan_roots);
|
|
if (!snap) {
|
|
std::cerr << "vbs ide: " << snap.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
Graph::GraphLowerOptions gopts{project_root, build_root, &(*snap), std::move(imported_targets),
|
|
32768};
|
|
auto graph = Graph::lower_project_graph(*desc, *tc, gopts);
|
|
if (!graph) {
|
|
std::cerr << "vbs ide: " << graph.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
const auto existing_sel = Editor::read_selection_state(project_root).value_or(std::nullopt);
|
|
Editor::PublishOptions popts;
|
|
popts.project_root = project_root;
|
|
popts.build_root = build_root;
|
|
popts.update_selection = !existing_sel.has_value();
|
|
popts.sdk_include_dir = fopts.sdk_include_dir;
|
|
auto pub = Editor::publish_profile_compdb(*graph, popts);
|
|
if (!pub) {
|
|
std::cerr << "vbs ide: " << pub.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
const auto root_compdb = build_root / "compile_commands.json";
|
|
const auto compdb_path =
|
|
std::filesystem::exists(root_compdb, ec) ? root_compdb : pub->compdb_path;
|
|
|
|
if (sub == "query") {
|
|
std::cout << Editor::format_ide_query_json(*graph, compdb_path, fopts.sdk_include_dir);
|
|
return 0;
|
|
}
|
|
|
|
std::filesystem::path vbs_exe{"vbs"};
|
|
if (const auto self_exe = std::filesystem::read_symlink("/proc/self/exe", ec);
|
|
!ec && !self_exe.empty()) {
|
|
vbs_exe = self_exe;
|
|
}
|
|
|
|
Editor::IdeConfigOptions ide_opts;
|
|
ide_opts.project_root = project_root;
|
|
ide_opts.build_root = build_root;
|
|
ide_opts.sdk_include_dir = fopts.sdk_include_dir;
|
|
ide_opts.vbs_executable = vbs_exe;
|
|
ide_opts.compdb_path = compdb_path;
|
|
ide_opts.editor = ide_kind;
|
|
ide_opts.force = opts.force;
|
|
|
|
auto rep = Editor::generate_ide_configs(*graph, ide_opts);
|
|
if (!rep) {
|
|
std::cerr << "vbs ide: " << rep.error().message() << '\n';
|
|
return 1;
|
|
}
|
|
for (const auto &w : rep->written_files) {
|
|
std::cout << "wrote " << w.string() << '\n';
|
|
}
|
|
for (const auto &s : rep->skipped_existing_files) {
|
|
std::cout << "preserved " << s.string() << " (pass --force to overwrite)\n";
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
const std::string cwd = std::filesystem::current_path().string();
|
|
bool show_version = false;
|
|
bool verbose = false;
|
|
bool quiet = false;
|
|
|
|
BuildCmdOptions build_opts;
|
|
build_opts.project_arg = cwd;
|
|
CompDbCmdOptions compdb_opts;
|
|
compdb_opts.project_arg = cwd;
|
|
DescribeCmdOptions describe_opts;
|
|
describe_opts.project_arg = cwd;
|
|
WatchCmdOptions watch_opts;
|
|
watch_opts.project_arg = cwd;
|
|
DepsCmdOptions deps_opts;
|
|
deps_opts.project_arg = cwd;
|
|
InstallCmdOptions install_opts;
|
|
install_opts.project_arg = cwd;
|
|
UninstallCmdOptions uninstall_opts;
|
|
PackCmdOptions pack_opts;
|
|
pack_opts.project_arg = cwd;
|
|
PublishCmdOptions publish_opts;
|
|
PublishBinaryCmdOptions publish_binary_opts;
|
|
BinariesCmdOptions binaries_opts;
|
|
publish_opts.project_arg = cwd;
|
|
BridgeCmdOptions bridge_opts;
|
|
GcCmdOptions gc_opts;
|
|
gc_opts.build_arg = (std::filesystem::path{cwd} / "build").string();
|
|
GenTestsCmdOptions gen_tests_opts;
|
|
InitCmdOptions init_opts;
|
|
init_opts.dir_arg = cwd;
|
|
CleanCmdOptions clean_opts;
|
|
clean_opts.build_arg = (std::filesystem::path{cwd} / "build").string();
|
|
GraphCmdOptions graph_opts;
|
|
graph_opts.project_arg = cwd;
|
|
IdeCmdOptions ide_opts;
|
|
ide_opts.project_arg = cwd;
|
|
DaemonCmdOptions daemon_opts;
|
|
daemon_opts.project_arg = cwd;
|
|
|
|
auto add_log_flags = [&](Void::Argparse::Parser &p, bool allow_short_v = true) {
|
|
p.flag("--verbose", allow_short_v ? "-v" : "", "Enable verbose debug logs", verbose)
|
|
.flag("--quiet", "-q", "Suppress progress logs (warnings and errors only)", quiet);
|
|
};
|
|
|
|
auto configure_build_or_test = [&](Void::Argparse::Parser &p) {
|
|
p.option_default("--project", "-C", "Project directory", build_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", build_opts.build_arg)
|
|
.option_many("--profile", "-p", "Profile to build", build_opts.profiles)
|
|
.option_many("--target", "-t", "Target to build/test", build_opts.targets)
|
|
.option("--compiler", "", "Override compiler family (gcc|clang)",
|
|
build_opts.compiler_arg)
|
|
.option("--editor-profile", "", "Editor profile for compile_commands.json",
|
|
build_opts.editor_profile)
|
|
.option_many("--build-source", "", "Additional build description source",
|
|
build_opts.extra_build_sources)
|
|
.option("--jobs", "-j", "Maximum parallel compilation/link jobs", build_opts.jobs_arg)
|
|
.option_many("--member", "", "Workspace member to build/test", build_opts.members)
|
|
.option("--changed-since", "", "Build/test workspace members changed since a git ref",
|
|
build_opts.changed_since)
|
|
.option("--sbom", "", "Write an SBOM after a successful build", build_opts.sbom_arg)
|
|
.option("--sbom-format", "", "SBOM format: spdx (default) or cyclonedx",
|
|
build_opts.sbom_format_arg)
|
|
.option("--remote-cache", "", "Shared HTTP action cache URL (or VBS_REMOTE_CACHE)",
|
|
build_opts.remote_cache_arg)
|
|
.option("--remote-cache-token-env", "",
|
|
"Environment variable holding the remote cache bearer token",
|
|
build_opts.remote_cache_token_env)
|
|
.flag("--locked", "", "Require existing up-to-date vbs.lock", build_opts.locked)
|
|
.flag("--offline", "", "Disallow network fetches", build_opts.offline)
|
|
.flag("--no-cache", "", "Bypass action cache reads", build_opts.no_cache)
|
|
.flag("--native-foreign", "",
|
|
"Translate supported CMake/Meson dependencies to native VBS actions (Plan 20)",
|
|
build_opts.native_foreign)
|
|
.option_many("--trusted-public-key", "",
|
|
"Trusted Ed25519 package public key (hex; repeatable)",
|
|
build_opts.trusted_public_keys)
|
|
.option_many("--trusted-key-file", "",
|
|
"File with trusted Ed25519 public keys (repeatable)",
|
|
build_opts.trusted_key_files);
|
|
add_log_flags(p);
|
|
};
|
|
|
|
Void::Argparse::Parser root{"vbs",
|
|
"VoidBuildSystem — native C++ build system & package manager."};
|
|
root.require_subcommand(false).flag("--version", "-V", "Show version", show_version);
|
|
add_log_flags(root);
|
|
|
|
configure_build_or_test(
|
|
root.subcommand("build", "Configure and build selected profiles and targets"));
|
|
configure_build_or_test(root.subcommand("test", "Build and run declared test targets"));
|
|
|
|
auto &describe_cmd =
|
|
root.subcommand("describe", "Evaluate build.cpp and emit versioned graph summary JSON");
|
|
describe_cmd
|
|
.option_default("--project", "-C", "Project directory", describe_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", describe_opts.build_arg)
|
|
.option_default("--profile", "-p", "Profile name", describe_opts.profile_name, "debug")
|
|
.option("--compiler", "", "Override compiler family (gcc|clang)",
|
|
describe_opts.compiler_arg);
|
|
add_log_flags(describe_cmd);
|
|
|
|
auto &compdb_cmd = root.subcommand(
|
|
"compdb", "Manage per-profile compile_commands.json selection and metadata projection");
|
|
auto configure_compdb_common = [&](Void::Argparse::Parser &p, bool with_profile) {
|
|
p.option_default("--project", "-C", "Project directory", compdb_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", compdb_opts.build_arg);
|
|
if (with_profile) {
|
|
p.option("--profile", "-p", "Profile name", compdb_opts.profile_name);
|
|
}
|
|
add_log_flags(p);
|
|
};
|
|
auto &compdb_use =
|
|
compdb_cmd.subcommand("use", "Select active editor compilation database profile");
|
|
configure_compdb_common(compdb_use, true);
|
|
compdb_use.positional_many("profile", "Profile name (optional if --profile is given)",
|
|
compdb_opts.positional_profiles, false);
|
|
configure_compdb_common(
|
|
compdb_cmd.subcommand("show", "Show active editor compilation database selection"), false);
|
|
configure_compdb_common(
|
|
compdb_cmd.subcommand("export",
|
|
"Evaluate and export compile_commands.json without compiling"),
|
|
true);
|
|
configure_compdb_common(
|
|
compdb_cmd.subcommand("watch", "Watch project metadata and refresh compile_commands.json"),
|
|
true);
|
|
|
|
auto &watch_cmd =
|
|
root.subcommand("watch", "Watch project sources and rebuild on verified changes");
|
|
watch_cmd.option_default("--project", "-C", "Project directory", watch_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", watch_opts.build_arg)
|
|
.option("--profile", "-p", "Profile to watch", watch_opts.profile_arg)
|
|
.option_many("--target", "-t", "Target to rebuild", watch_opts.targets);
|
|
add_log_flags(watch_cmd);
|
|
|
|
auto &deps_cmd = root.subcommand("deps", "Resolve or upgrade dependencies and update vbs.lock");
|
|
auto configure_deps_sub = [&](Void::Argparse::Parser &p) {
|
|
p.option_default("--project", "-C", "Project directory", deps_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", deps_opts.build_arg)
|
|
.option_default("--profile", "-p", "Profile name", deps_opts.profile_name, "debug")
|
|
.flag("--offline", "", "Disallow network fetches", deps_opts.offline);
|
|
add_log_flags(p);
|
|
};
|
|
configure_deps_sub(deps_cmd.subcommand(
|
|
"resolve", "Resolve dependencies preserving pinned compatible versions"));
|
|
configure_deps_sub(
|
|
deps_cmd.subcommand("update", "Re-solve dependencies allowing compatible SemVer upgrades"));
|
|
|
|
auto &install_cmd =
|
|
root.subcommand("install", "Build, stage, and install declared products and exports");
|
|
install_cmd
|
|
.option_default("--project", "-C", "Project directory", install_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", install_opts.build_arg)
|
|
.option_default("--profile", "-p", "Profile name", install_opts.profile_name, "debug")
|
|
.option("--compiler", "", "Override compiler family (gcc|clang)", install_opts.compiler_arg)
|
|
.option("--prefix", "", "Install destination prefix (required)", install_opts.prefix_arg)
|
|
.option("--package-name", "", "Exported package name", install_opts.pkg_name_arg)
|
|
.option_default("--package-version", "", "Exported package version", install_opts.pkg_ver,
|
|
"0.1.0")
|
|
.option("--component", "", "Install only a specific component", install_opts.component_arg)
|
|
.flag("--no-cmake-export", "", "Disable CMake Config.cmake export",
|
|
install_opts.no_cmake_export)
|
|
.flag("--no-native-export", "", "Disable native VBS package metadata export",
|
|
install_opts.no_native_export);
|
|
add_log_flags(install_cmd);
|
|
|
|
auto &uninstall_cmd =
|
|
root.subcommand("uninstall", "Safely remove only VBS-owned files recorded in the manifest");
|
|
uninstall_cmd.option("--prefix", "", "Installed prefix directory (required)",
|
|
uninstall_opts.prefix_arg);
|
|
add_log_flags(uninstall_cmd);
|
|
|
|
auto &publish_binary_cmd = root.subcommand(
|
|
"publish-binary", "Publish a prebuilt binary variant to a VBS package registry");
|
|
publish_binary_cmd
|
|
.option("--registry", "-r", "Registry base URL (or VBS_REGISTRY_URL)",
|
|
publish_binary_opts.registry_arg)
|
|
.option("--name", "-n", "Package name", publish_binary_opts.name_arg)
|
|
.option("--version", "-v", "Release version", publish_binary_opts.version_arg)
|
|
.option("--variant", "", "Binary variant id", publish_binary_opts.variant_arg)
|
|
.option("--file", "-f", "Prebuilt artifact file", publish_binary_opts.file_arg)
|
|
.option("--signing-key", "", "Ed25519 signing seed file (64 hex characters)",
|
|
publish_binary_opts.signing_key_arg)
|
|
.option_default("--token-env", "", "Environment variable holding bearer token",
|
|
publish_binary_opts.token_env, "VBS_REGISTRY_TOKEN");
|
|
add_log_flags(publish_binary_cmd, false); // `-v` is `--version` on publish-binary.
|
|
|
|
auto &binaries_cmd =
|
|
root.subcommand("binaries", "Inspect prebuilt binary variants in a package registry");
|
|
auto configure_binaries = [&](Void::Argparse::Parser &p) {
|
|
p.option("--registry", "-r", "Registry base URL (or VBS_REGISTRY_URL)",
|
|
binaries_opts.registry_arg)
|
|
.option("--name", "-n", "Package name", binaries_opts.name_arg)
|
|
.option("--version", "-v", "Release version", binaries_opts.version_arg)
|
|
.option_default("--token-env", "", "Environment variable holding bearer token",
|
|
binaries_opts.token_env, "VBS_REGISTRY_TOKEN");
|
|
add_log_flags(p, false);
|
|
};
|
|
configure_binaries(binaries_cmd.subcommand(
|
|
"list", "List a release's binary variants (id, digest, size, signature state)"));
|
|
auto &binaries_fetch_cmd =
|
|
binaries_cmd.subcommand("fetch", "Download one binary variant artifact");
|
|
configure_binaries(binaries_fetch_cmd);
|
|
binaries_fetch_cmd.option("--variant", "", "Binary variant id", binaries_opts.variant_arg)
|
|
.option("--output", "-o", "Destination artifact path", binaries_opts.output_arg);
|
|
|
|
auto &pack_cmd = root.subcommand("pack", "Create a deterministic USTAR source archive (.tar)");
|
|
pack_cmd.option_default("--project", "-C", "Project directory", pack_opts.project_arg, cwd)
|
|
.option_default("--output", "-o", "Output archive path", pack_opts.out_arg, "package.tar");
|
|
add_log_flags(pack_cmd);
|
|
|
|
auto &publish_cmd =
|
|
root.subcommand("publish", "Pack and publish a release to a VBS package registry");
|
|
publish_cmd
|
|
.option_default("--project", "-C", "Project directory", publish_opts.project_arg, cwd)
|
|
.option("--registry", "-r", "Registry base URL (or VBS_REGISTRY_URL)",
|
|
publish_opts.registry_arg)
|
|
.option("--name", "-n", "Package name", publish_opts.name_arg)
|
|
.option("--version", "-v", "Strict SemVer release version", publish_opts.version_arg)
|
|
.option_default("--description", "-d", "Package description", publish_opts.description, "")
|
|
.option_default("--token-env", "", "Environment variable holding bearer token",
|
|
publish_opts.token_env, "VBS_REGISTRY_TOKEN")
|
|
.option("--signing-key", "", "Ed25519 signing seed file (64 hex characters)",
|
|
publish_opts.signing_key_arg)
|
|
.flag("--private", "", "Publish package as private", publish_opts.is_private);
|
|
add_log_flags(publish_cmd, false); // `-v` is `--version` on `publish`.
|
|
|
|
auto &bridge_cmd = root.subcommand("bridge", "CMake VbsDeps.cmake dependency bridge backend");
|
|
bridge_cmd.option("--request", "", "Path to bridge request JSON", bridge_opts.req_arg)
|
|
.option("--output", "", "Path to output CMake fragment", bridge_opts.out_arg);
|
|
add_log_flags(bridge_cmd);
|
|
|
|
auto &gc_cmd =
|
|
root.subcommand("gc", "Recover interrupted state and garbage-collect unreferenced blobs");
|
|
gc_cmd
|
|
.option_default("--build-dir", "-B", "Build directory", gc_opts.build_arg,
|
|
gc_opts.build_arg)
|
|
.option_default("--max-bytes", "",
|
|
"Target maximum retained CAS bytes (0=unreferenced only)",
|
|
gc_opts.max_bytes_arg, "0");
|
|
add_log_flags(gc_cmd);
|
|
|
|
auto &gen_tests_cmd =
|
|
root.subcommand("gen-tests", "Generate native Void::Testing registration unit (no CMake)");
|
|
gen_tests_cmd.option("--input", "", "Input test .cpp file", gen_tests_opts.input_arg)
|
|
.option("--output-dir", "", "Output directory for generated registration .cpp",
|
|
gen_tests_opts.out_arg);
|
|
add_log_flags(gen_tests_cmd);
|
|
|
|
auto &init_cmd =
|
|
root.subcommand("init", "Scaffold a new ready-to-build C++26 VoidBuildSystem project");
|
|
init_cmd.option_default("--dir", "-C", "Target project directory", init_opts.dir_arg, cwd)
|
|
.option("--name", "-n", "Project/target name (defaults to directory name)",
|
|
init_opts.name_arg)
|
|
.flag("--library", "", "Scaffold a static library project instead of an executable",
|
|
init_opts.library)
|
|
.flag("--with-toml", "", "Include a vbs.deps.toml dependency manifest", init_opts.with_toml)
|
|
.flag("--force", "", "Overwrite existing build.cpp if present", init_opts.force);
|
|
add_log_flags(init_cmd);
|
|
|
|
auto &clean_cmd =
|
|
root.subcommand("clean", "Remove generated profile build artifacts (preserving cache)");
|
|
clean_cmd
|
|
.option_default("--build-dir", "-B", "Build directory", clean_opts.build_arg,
|
|
clean_opts.build_arg)
|
|
.flag("--all", "", "Also delete .vbs cache, snapshots, and state store",
|
|
clean_opts.clean_all);
|
|
add_log_flags(clean_cmd);
|
|
|
|
auto &doctor_cmd =
|
|
root.subcommand("doctor", "Check host compilers, tools, and VBS runtime capabilities");
|
|
add_log_flags(doctor_cmd);
|
|
|
|
auto &graph_cmd =
|
|
root.subcommand("graph", "Export target dependency graph in Graphviz DOT or JSON format");
|
|
graph_cmd.option_default("--project", "-C", "Project directory", graph_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", graph_opts.build_arg)
|
|
.option_default("--profile", "-p", "Profile name", graph_opts.profile_name, "debug")
|
|
.option("--compiler", "", "Override compiler family (gcc|clang)", graph_opts.compiler_arg)
|
|
.option_default("--format", "-f", "Output format (dot|json)", graph_opts.format, "dot");
|
|
add_log_flags(graph_cmd);
|
|
|
|
auto &ide_cmd =
|
|
root.subcommand("ide", "Generate editor/IDE configs and query workspace metadata");
|
|
auto configure_ide_common = [&](Void::Argparse::Parser &p) {
|
|
p.option_default("--project", "-C", "Project directory", ide_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", ide_opts.build_arg)
|
|
.option_default("--profile", "-p", "Profile name", ide_opts.profile_name, "debug")
|
|
.option("--compiler", "", "Override compiler family (gcc|clang)",
|
|
ide_opts.compiler_arg);
|
|
add_log_flags(p);
|
|
};
|
|
auto &ide_gen_cmd =
|
|
ide_cmd.subcommand("generate", "Generate .clangd, .vscode/, .zed/, and .idea/ IDE configs");
|
|
configure_ide_common(ide_gen_cmd);
|
|
ide_gen_cmd
|
|
.option_default("--editor", "-e", "Editor preset (all|clangd|vscode|clion|zed)",
|
|
ide_opts.editor_name, "all")
|
|
.flag("--force", "", "Overwrite existing IDE configuration files", ide_opts.force);
|
|
configure_ide_common(
|
|
ide_cmd.subcommand("query", "Emit versioned JSON workspace, target, and compdb metadata"));
|
|
|
|
auto &daemon_cmd = root.subcommand("daemon", "Run or control the persistent build daemon");
|
|
auto configure_daemon_common = [&](Void::Argparse::Parser &p) {
|
|
p.option_default("--project", "-C", "Project directory", daemon_opts.project_arg, cwd)
|
|
.option("--build-dir", "-B", "Build output directory", daemon_opts.build_arg)
|
|
.option("--socket", "", "Unix socket path (default: <build>/.vbs/vbs-daemon.sock)",
|
|
daemon_opts.socket_arg);
|
|
add_log_flags(p);
|
|
};
|
|
auto &daemon_run_cmd = daemon_cmd.subcommand("run", "Run the daemon in the foreground");
|
|
configure_daemon_common(daemon_run_cmd);
|
|
daemon_run_cmd.option_default("--max-requests", "", "Stop after N requests (0 = unlimited)",
|
|
daemon_opts.max_requests_arg, "0");
|
|
configure_daemon_common(daemon_cmd.subcommand("status", "Query the running daemon's status"));
|
|
configure_daemon_common(daemon_cmd.subcommand("stop", "Ask the running daemon to shut down"));
|
|
configure_daemon_common(
|
|
daemon_cmd.subcommand("recover", "Run interrupted-state recovery in the daemon"));
|
|
auto &daemon_gc_cmd = daemon_cmd.subcommand("gc", "Run CAS garbage collection in the daemon");
|
|
configure_daemon_common(daemon_gc_cmd);
|
|
daemon_gc_cmd.option_default("--max-bytes", "",
|
|
"Target maximum retained CAS bytes (0=unreferenced only)",
|
|
daemon_opts.max_bytes_arg, "0");
|
|
auto &daemon_describe_cmd =
|
|
daemon_cmd.subcommand("describe", "Describe a project's build graph via the daemon");
|
|
configure_daemon_common(daemon_describe_cmd);
|
|
daemon_describe_cmd.option_default("--profile", "-p", "Profile to evaluate",
|
|
daemon_opts.profile_arg, "debug");
|
|
auto &daemon_build_cmd = daemon_cmd.subcommand("build", "Build a project graph via the daemon");
|
|
configure_daemon_common(daemon_build_cmd);
|
|
daemon_build_cmd
|
|
.option_default("--profile", "-p", "Profile to build", daemon_opts.profile_arg, "debug")
|
|
.option_many("--target", "-t", "Target to build", daemon_opts.targets)
|
|
.option_default("--jobs", "-j", "Maximum parallel jobs (0 = hardware concurrency)",
|
|
daemon_opts.jobs_arg, "0");
|
|
auto &daemon_test_cmd =
|
|
daemon_cmd.subcommand("test", "Build and run declared test targets via the daemon");
|
|
configure_daemon_common(daemon_test_cmd);
|
|
daemon_test_cmd
|
|
.option_default("--profile", "-p", "Profile to build", daemon_opts.profile_arg, "debug")
|
|
.option_many("--target", "-t", "Target to build/test", daemon_opts.targets)
|
|
.option_default("--jobs", "-j", "Maximum parallel jobs (0 = hardware concurrency)",
|
|
daemon_opts.jobs_arg, "0");
|
|
auto &daemon_compdb_cmd =
|
|
daemon_cmd.subcommand("compdb", "Editor compilation database served by the daemon");
|
|
auto &daemon_compdb_generations_cmd = daemon_compdb_cmd.subcommand(
|
|
"generations", "List immutable editor generations for a profile");
|
|
configure_daemon_common(daemon_compdb_generations_cmd);
|
|
daemon_compdb_generations_cmd.option_default("--profile", "-p", "Profile to inspect",
|
|
daemon_opts.profile_arg, "debug");
|
|
auto &daemon_compdb_entries_cmd = daemon_compdb_cmd.subcommand(
|
|
"entries", "Query bounded compile_commands entries for editor extensions");
|
|
configure_daemon_common(daemon_compdb_entries_cmd);
|
|
daemon_compdb_entries_cmd
|
|
.option("--file", "", "Only entries whose source path ends with this value",
|
|
daemon_opts.file_filter_arg)
|
|
.option_default("--limit", "", "Maximum entries returned (0-500)", daemon_opts.limit_arg,
|
|
"50");
|
|
auto &daemon_compdb_use_cmd =
|
|
daemon_compdb_cmd.subcommand("use", "Select an already-published editor profile");
|
|
configure_daemon_common(daemon_compdb_use_cmd);
|
|
daemon_compdb_use_cmd.option_default("--profile", "-p", "Profile to select",
|
|
daemon_opts.profile_arg, "debug");
|
|
auto &daemon_compdb_export_cmd =
|
|
daemon_compdb_cmd.subcommand("export", "Publish the profile compilation database");
|
|
configure_daemon_common(daemon_compdb_export_cmd);
|
|
daemon_compdb_export_cmd
|
|
.option_default("--profile", "-p", "Profile to export", daemon_opts.profile_arg, "debug")
|
|
.option("--member", "", "Workspace member (required for workspace roots)",
|
|
daemon_opts.member_arg);
|
|
auto &daemon_workspace_cmd =
|
|
daemon_cmd.subcommand("workspace", "Workspace metadata served by the daemon");
|
|
auto &daemon_workspace_members_cmd =
|
|
daemon_workspace_cmd.subcommand("members", "List declared workspace members");
|
|
configure_daemon_common(daemon_workspace_members_cmd);
|
|
auto &daemon_graph_cmd =
|
|
daemon_cmd.subcommand("graph", "Export the target dependency graph via the daemon");
|
|
configure_daemon_common(daemon_graph_cmd);
|
|
daemon_graph_cmd
|
|
.option_default("--profile", "-p", "Profile to evaluate", daemon_opts.profile_arg, "debug")
|
|
.option_default("--format", "-f", "Output format (dot|json)", daemon_opts.graph_format_arg,
|
|
"dot")
|
|
.option("--member", "", "Workspace member (required for workspace roots)",
|
|
daemon_opts.member_arg);
|
|
auto &daemon_ide_cmd = daemon_cmd.subcommand("ide", "Editor/IDE metadata served by the daemon");
|
|
configure_daemon_common(daemon_ide_cmd);
|
|
auto &daemon_ide_query_cmd =
|
|
daemon_ide_cmd.subcommand("query", "Emit IDE workspace/target/compdb metadata JSON");
|
|
configure_daemon_common(daemon_ide_query_cmd);
|
|
daemon_ide_query_cmd
|
|
.option_default("--profile", "-p", "Profile to query", daemon_opts.profile_arg, "debug")
|
|
.option("--member", "", "Workspace member (required for workspace roots)",
|
|
daemon_opts.member_arg);
|
|
auto &daemon_ide_status_cmd =
|
|
daemon_ide_cmd.subcommand("status", "Report the active editor selection and compdb state");
|
|
configure_daemon_common(daemon_ide_status_cmd);
|
|
auto &daemon_ide_configs_cmd =
|
|
daemon_ide_cmd.subcommand("configs", "Report which editor configuration files exist");
|
|
configure_daemon_common(daemon_ide_configs_cmd);
|
|
auto &daemon_ide_poll_cmd = daemon_ide_cmd.subcommand(
|
|
"poll", "Poll pending project file changes (editor extension protocol)");
|
|
configure_daemon_common(daemon_ide_poll_cmd);
|
|
auto &daemon_ide_wait_cmd = daemon_ide_cmd.subcommand(
|
|
"wait", "Long-poll for project file changes (bounded, editor extension protocol)");
|
|
configure_daemon_common(daemon_ide_wait_cmd);
|
|
daemon_ide_wait_cmd.option_default("--timeout-ms", "", "Maximum wait in milliseconds (0..5000)",
|
|
daemon_opts.timeout_ms_arg, "2000");
|
|
auto &daemon_ide_generate_cmd =
|
|
daemon_ide_cmd.subcommand("generate", "Write editor configuration files");
|
|
configure_daemon_common(daemon_ide_generate_cmd);
|
|
daemon_ide_generate_cmd
|
|
.option_default("--profile", "-p", "Profile to evaluate", daemon_opts.profile_arg, "debug")
|
|
.option_default("--editor", "-e", "Editor preset (all|clangd|vscode|clion|zed)",
|
|
daemon_opts.editor_arg, "all")
|
|
.option("--member", "", "Workspace member (required for workspace roots)",
|
|
daemon_opts.member_arg)
|
|
.flag("--force", "", "Overwrite existing IDE configuration files", daemon_opts.force);
|
|
|
|
const auto parsed = root.parse_detailed(argc, argv);
|
|
if (parsed.is_help()) {
|
|
std::cout << parsed.message;
|
|
return 0;
|
|
}
|
|
if (parsed.is_error()) {
|
|
std::cerr << "vbs: " << parsed.message << '\n';
|
|
return 2;
|
|
}
|
|
if (show_version) {
|
|
if (!parsed.matched_command_path.empty()) {
|
|
std::cerr << "vbs: --version cannot be combined with subcommands\n";
|
|
return 2;
|
|
}
|
|
std::cout << "vbs " << Bootstrap::version << '\n';
|
|
return 0;
|
|
}
|
|
if (parsed.matched_command_path.empty()) {
|
|
std::cout << root.help();
|
|
return 0;
|
|
}
|
|
|
|
const std::string_view cmd = parsed.matched_command_path.front();
|
|
const bool machine_quiet_default =
|
|
(cmd == "describe") || (cmd == "graph") || (cmd == "doctor") ||
|
|
(cmd == "compdb" && parsed.matched_command_path.size() >= 2 &&
|
|
parsed.matched_command_path[1] == "show") ||
|
|
(cmd == "ide" && parsed.matched_command_path.size() >= 2 &&
|
|
parsed.matched_command_path[1] == "query");
|
|
const Void::Log::Level log_level =
|
|
verbose ? Void::Log::Level::Debug
|
|
: ((quiet || machine_quiet_default) ? Void::Log::Level::Warning
|
|
: Void::Log::Level::Info);
|
|
ConsoleLogGuard log_guard{log_level};
|
|
|
|
if (cmd == "build" || cmd == "test") {
|
|
const bool run_tests = cmd == "test";
|
|
if (Config::is_workspace_root(std::filesystem::path{build_opts.project_arg})) {
|
|
return execute_workspace(std::move(build_opts), run_tests);
|
|
}
|
|
return execute_build_or_test(std::move(build_opts), run_tests);
|
|
}
|
|
if (cmd == "describe") {
|
|
return execute_describe_command(describe_opts);
|
|
}
|
|
if (cmd == "compdb") {
|
|
return execute_compdb_command(parsed.matched_command_path.at(1), std::move(compdb_opts));
|
|
}
|
|
if (cmd == "watch") {
|
|
return execute_watch_command(std::move(watch_opts));
|
|
}
|
|
if (cmd == "deps") {
|
|
return execute_deps_command(parsed.matched_command_path.at(1), deps_opts);
|
|
}
|
|
if (cmd == "install") {
|
|
return execute_install_command(install_opts);
|
|
}
|
|
if (cmd == "uninstall") {
|
|
return execute_uninstall_command(uninstall_opts);
|
|
}
|
|
if (cmd == "pack") {
|
|
return execute_pack_command(pack_opts);
|
|
}
|
|
if (cmd == "publish") {
|
|
return execute_publish_command(publish_opts);
|
|
}
|
|
if (cmd == "publish-binary") {
|
|
return execute_publish_binary_command(publish_binary_opts);
|
|
}
|
|
if (cmd == "binaries") {
|
|
return execute_binaries_command(parsed.matched_command_path.at(1), binaries_opts);
|
|
}
|
|
if (cmd == "bridge") {
|
|
return execute_bridge_command(bridge_opts);
|
|
}
|
|
if (cmd == "gc") {
|
|
return execute_gc_command(gc_opts);
|
|
}
|
|
if (cmd == "gen-tests") {
|
|
return execute_gen_tests_command(gen_tests_opts);
|
|
}
|
|
if (cmd == "init") {
|
|
return execute_init_command(init_opts);
|
|
}
|
|
if (cmd == "clean") {
|
|
return execute_clean_command(clean_opts);
|
|
}
|
|
if (cmd == "doctor") {
|
|
return execute_doctor_command();
|
|
}
|
|
if (cmd == "graph") {
|
|
return execute_graph_command(graph_opts);
|
|
}
|
|
if (cmd == "ide") {
|
|
return execute_ide_command(parsed.matched_command_path.at(1), ide_opts);
|
|
}
|
|
if (cmd == "daemon") {
|
|
std::string daemon_sub = parsed.matched_command_path.at(1);
|
|
if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "query") {
|
|
daemon_sub = "ide_query";
|
|
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "generate") {
|
|
daemon_sub = "ide_generate";
|
|
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "status") {
|
|
daemon_sub = "editor_status";
|
|
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "poll") {
|
|
daemon_sub = "watch_poll";
|
|
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "wait") {
|
|
daemon_sub = "watch_wait";
|
|
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "export") {
|
|
daemon_sub = "compdb_export";
|
|
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "use") {
|
|
daemon_sub = "compdb_use";
|
|
} else if (daemon_sub == "workspace" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "members") {
|
|
daemon_sub = "workspace_members";
|
|
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "configs") {
|
|
daemon_sub = "ide_configs";
|
|
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
|
parsed.matched_command_path[2] == "generations") {
|
|
daemon_sub = "compdb_generations";
|
|
}
|
|
return execute_daemon_command(daemon_sub, daemon_opts);
|
|
}
|
|
|
|
std::cerr << "vbs: unhandled subcommand '" << cmd << "'\n";
|
|
return 2;
|
|
}
|