382 lines
15 KiB
C++
382 lines
15 KiB
C++
#include <Void/Build/Watch/Watcher.hpp>
|
|
|
|
#include <Void/Build/Toolchain/Driver.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <set>
|
|
|
|
namespace Void::Build::Watch {
|
|
|
|
Void::Result::Result<InotifyWatcher, Void::Result::Error>
|
|
InotifyWatcher::create(const std::filesystem::path &root_dir,
|
|
std::vector<std::string> excluded_names) {
|
|
std::error_code ec;
|
|
const auto canon = std::filesystem::weakly_canonical(root_dir, ec);
|
|
if (ec || !std::filesystem::is_directory(canon)) {
|
|
return std::unexpected{Void::System::make_system_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("InotifyWatcher: root '{}' is not a directory", root_dir.string()))};
|
|
}
|
|
|
|
Void::FileSystem::DirectoryWatcherOptions opts;
|
|
opts.ignore = [canon, excluded = std::move(excluded_names)](const std::filesystem::path &path,
|
|
bool /*is_directory*/) {
|
|
const auto rel = path.lexically_relative(canon);
|
|
for (const auto &part : rel) {
|
|
const auto s = part.string();
|
|
if (s.empty() || s == ".") {
|
|
continue;
|
|
}
|
|
if (std::ranges::contains(excluded, s)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
auto watcher = Void::FileSystem::DirectoryWatcher::create(canon, std::move(opts));
|
|
if (!watcher) {
|
|
return std::unexpected{std::move(watcher.error())};
|
|
}
|
|
return InotifyWatcher{std::move(*watcher)};
|
|
}
|
|
|
|
Void::Result::Result<void, Void::Result::Error> InotifyWatcher::rescan_watches() {
|
|
if (!watcher_) {
|
|
return {};
|
|
}
|
|
return watcher_->rescan();
|
|
}
|
|
|
|
Void::Result::Result<WatchBatch, Void::Result::Error>
|
|
InotifyWatcher::poll_events(std::chrono::milliseconds timeout, std::chrono::milliseconds debounce) {
|
|
WatchBatch batch;
|
|
if (!watcher_) {
|
|
batch.rescan_required = true;
|
|
return batch;
|
|
}
|
|
|
|
auto events = watcher_->poll(timeout);
|
|
if (!events) {
|
|
return std::unexpected{std::move(events.error())};
|
|
}
|
|
if (events->empty()) {
|
|
return batch;
|
|
}
|
|
|
|
std::set<std::filesystem::path> unique_paths;
|
|
auto ingest = [&](const std::vector<Void::FileSystem::DirectoryEvent> &evs) {
|
|
for (const auto &ev : evs) {
|
|
if (ev.kind == Void::FileSystem::DirectoryEventKind::RescanRequired) {
|
|
batch.overflowed = true;
|
|
batch.rescan_required = true;
|
|
continue;
|
|
}
|
|
if (!ev.path.empty()) {
|
|
unique_paths.insert(ev.path);
|
|
}
|
|
if (!ev.old_path.empty()) {
|
|
unique_paths.insert(ev.old_path);
|
|
}
|
|
}
|
|
};
|
|
ingest(*events);
|
|
|
|
// Coalesce burst edits across the debounce window.
|
|
if (debounce.count() > 0 && (!unique_paths.empty() || batch.rescan_required)) {
|
|
const auto settle_deadline = std::chrono::steady_clock::now() + debounce;
|
|
while (std::chrono::steady_clock::now() < settle_deadline) {
|
|
const auto rem = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
settle_deadline - std::chrono::steady_clock::now());
|
|
if (rem.count() <= 0) {
|
|
break;
|
|
}
|
|
auto more = watcher_->poll(rem);
|
|
if (!more || more->empty()) {
|
|
break;
|
|
}
|
|
ingest(*more);
|
|
}
|
|
}
|
|
|
|
if (batch.rescan_required) {
|
|
(void)rescan_watches();
|
|
}
|
|
batch.changed_paths.assign(unique_paths.begin(), unique_paths.end());
|
|
return batch;
|
|
}
|
|
|
|
WatchCycleReport run_reconcile_cycle(const WatchLoopOptions &options, std::size_t cycle_index,
|
|
bool allow_selection_update,
|
|
std::optional<std::uint64_t> reserved_ticket,
|
|
std::stop_token stop) {
|
|
WatchCycleReport rep;
|
|
rep.cycle_index = cycle_index;
|
|
|
|
// Resolve profile name: explicit profile, or active editor selection in MetadataOnly, or
|
|
// "debug".
|
|
std::string profile_name = options.explicit_profile.value_or("");
|
|
if (profile_name.empty()) {
|
|
if (auto sel = Editor::read_selection_state(options.frontend.project_root);
|
|
sel && sel->has_value() && !(*sel)->selected_profile.empty()) {
|
|
profile_name = (*sel)->selected_profile;
|
|
} else {
|
|
profile_name = "debug";
|
|
}
|
|
}
|
|
rep.profile_name = profile_name;
|
|
|
|
// 1. Compile or reuse host build description (allowed in both FullBuild and MetadataOnly).
|
|
auto compiled = Config::compile_descriptor(options.frontend);
|
|
if (!compiled) {
|
|
rep.diagnostic = compiled.error().message();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
// 2. Discover profiles and resolve target ProfileContext.
|
|
auto profs = Config::discover_profiles(*compiled, options.frontend);
|
|
if (!profs) {
|
|
rep.diagnostic = profs.error().message();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
ProfileContext ctx;
|
|
try {
|
|
ctx = profs->resolve(profile_name, options.compiler_override, Toolchain::host_os_name(),
|
|
Toolchain::host_architecture_name());
|
|
} catch (const std::exception &ex) {
|
|
rep.diagnostic = ex.what();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
// 3. Probe target toolchain (safe syntax-only probes, never builds targets).
|
|
auto tc = Toolchain::discover_and_probe(ctx, options.frontend.build_root / ".vbs" / "probes");
|
|
if (!tc) {
|
|
rep.diagnostic = tc.error().message();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
// 4. Evaluate build(Project&) for this ProfileContext.
|
|
auto desc = Config::evaluate_project(*compiled, ctx, options.frontend);
|
|
if (!desc) {
|
|
rep.diagnostic = desc.error().message();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
// 5. Provider metadata boundary: in MetadataOnly mode, read ONLY validated cached exports;
|
|
// NEVER invoke CMake configure, Meson setup, Conan install, fetch, or lock writes!
|
|
CachedProviderSnapshot provider_meta;
|
|
if (options.provider_reader) {
|
|
provider_meta = options.provider_reader(*desc, options.mode);
|
|
}
|
|
if (!provider_meta.valid) {
|
|
rep.provider_stale = true;
|
|
rep.diagnostic = provider_meta.stale_diagnostic.empty()
|
|
? "cached dependency metadata is stale; run 'vbs build' to refresh"
|
|
: provider_meta.stale_diagnostic;
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Stale, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
// 6. Capture input snapshot (content-verified; materialize only for FullBuild execution).
|
|
std::vector<std::filesystem::path> scan_roots;
|
|
std::error_code ec;
|
|
for (const char *d : {"src", "source", "include", "tests"}) {
|
|
if (std::filesystem::is_directory(options.frontend.project_root / d, ec)) {
|
|
scan_roots.push_back(options.frontend.project_root / d);
|
|
}
|
|
}
|
|
for (const auto &t : desc->targets) {
|
|
for (const auto &inc : t.include_directories) {
|
|
if (!inc.path.starts_with("$GEN/") && !inc.path.starts_with("gen/")) {
|
|
const auto p = std::filesystem::path{inc.path}.is_absolute()
|
|
? std::filesystem::path{inc.path}
|
|
: (options.frontend.project_root / inc.path);
|
|
if (std::filesystem::is_directory(p, ec) && !std::ranges::contains(scan_roots, p)) {
|
|
scan_roots.push_back(p);
|
|
}
|
|
}
|
|
}
|
|
for (const auto &src : t.sources) {
|
|
if (src.kind == SourceKind::File && !src.path.starts_with("$GEN/") &&
|
|
!src.path.starts_with("gen/")) {
|
|
const auto p = std::filesystem::path{src.path}.is_absolute()
|
|
? std::filesystem::path{src.path}
|
|
: (options.frontend.project_root / src.path);
|
|
const auto dir = p.parent_path();
|
|
if (!dir.empty() && std::filesystem::is_directory(dir, ec) &&
|
|
!std::ranges::contains(scan_roots, dir)) {
|
|
scan_roots.push_back(dir);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Snapshot::SnapshotOptions sopts;
|
|
sopts.project_root = options.frontend.project_root;
|
|
sopts.snapshot_store_dir = options.frontend.build_root / ".vbs" / "snapshots";
|
|
sopts.reproducibility_epoch = ctx.reproducibility_epoch;
|
|
sopts.materialize = (options.mode == WatchMode::FullBuild);
|
|
|
|
auto snap = Snapshot::capture_snapshot(sopts, {}, scan_roots);
|
|
if (!snap) {
|
|
rep.diagnostic = snap.error().message();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
|
|
// 7. Lower target graph.
|
|
Graph::GraphLowerOptions gopts;
|
|
gopts.project_root = options.frontend.project_root;
|
|
gopts.build_root = options.frontend.build_root;
|
|
gopts.snapshot = &(*snap);
|
|
gopts.imported_targets = std::move(provider_meta.imported_targets);
|
|
|
|
auto graph = Graph::lower_project_graph(*desc, *tc, gopts);
|
|
if (!graph) {
|
|
rep.diagnostic = graph.error().message();
|
|
(void)Editor::mark_profile_stale(options.frontend.build_root, profile_name,
|
|
Editor::GenerationStatus::Invalid, rep.diagnostic);
|
|
return rep;
|
|
}
|
|
rep.config_succeeded = true;
|
|
|
|
// 8. Publish compilation database BEFORE target compilation so valid source/flag edits
|
|
// refresh editors immediately even if target compilation fails (Plan 09/10)!
|
|
Editor::PublishOptions popts;
|
|
popts.project_root = options.frontend.project_root;
|
|
popts.build_root = options.frontend.build_root;
|
|
popts.update_selection = allow_selection_update;
|
|
popts.reserved_ticket = reserved_ticket;
|
|
popts.sdk_include_dir = options.frontend.sdk_include_dir;
|
|
popts.extra_build_sources = options.frontend.extra_build_sources;
|
|
auto pub = Editor::publish_profile_compdb(*graph, popts);
|
|
if (!pub) {
|
|
rep.diagnostic = pub.error().message();
|
|
return rep;
|
|
}
|
|
rep.compdb_published = true;
|
|
|
|
// Record graph generation in state store.
|
|
if (auto state = State::StateStore::open(options.frontend.build_root / ".vbs"); state) {
|
|
State::GraphGenerationRecord gen_rec;
|
|
gen_rec.generation_id = pub->generation_id;
|
|
gen_rec.profile_name = profile_name;
|
|
gen_rec.profile_identity = graph->profile_identity;
|
|
gen_rec.snapshot_id = snap->snapshot_id;
|
|
gen_rec.status = "valid";
|
|
(void)state->record_generation(gen_rec);
|
|
|
|
// 9. In MetadataOnly mode: STOP HERE! Never schedule target builds or generators!
|
|
if (options.mode == WatchMode::MetadataOnly) {
|
|
rep.build_succeeded = true;
|
|
return rep;
|
|
}
|
|
|
|
// 10. FullBuild mode: execute selected actions.
|
|
Execution::ExecutionOptions eopts;
|
|
eopts.jobs = options.jobs;
|
|
eopts.selected_targets = options.selected_targets;
|
|
auto exec = Execution::execute_graph(*graph, *state, eopts, stop);
|
|
if (!exec) {
|
|
rep.diagnostic = exec.error().message();
|
|
return rep;
|
|
}
|
|
rep.executed_actions = exec->executed_count;
|
|
rep.cached_actions = exec->cached_count;
|
|
rep.build_succeeded = exec->succeeded;
|
|
rep.diagnostic = exec->diagnostic;
|
|
}
|
|
return rep;
|
|
}
|
|
|
|
Void::Result::Result<std::vector<WatchCycleReport>, Void::Result::Error>
|
|
run_watch_loop(const WatchLoopOptions &options, std::stop_token stop) {
|
|
auto watcher = InotifyWatcher::create(options.frontend.project_root);
|
|
if (!watcher) {
|
|
return std::unexpected{watcher.error()};
|
|
}
|
|
|
|
std::vector<WatchCycleReport> reports;
|
|
std::size_t cycle = 0;
|
|
|
|
// Initial reconciliation pass (watchers NEVER steal editor selection:
|
|
// allow_selection_update=false).
|
|
{
|
|
auto rep = run_reconcile_cycle(options, cycle++, false, std::nullopt, stop);
|
|
if (options.on_cycle) {
|
|
options.on_cycle(rep);
|
|
}
|
|
reports.push_back(std::move(rep));
|
|
if (options.max_cycles > 0 && reports.size() >= options.max_cycles) {
|
|
return reports;
|
|
}
|
|
}
|
|
|
|
auto last_reconcile = std::chrono::steady_clock::now();
|
|
std::string last_observed_selection;
|
|
if (auto sel = Editor::read_selection_state(options.frontend.project_root);
|
|
sel && sel->has_value()) {
|
|
last_observed_selection = (*sel)->selected_profile;
|
|
}
|
|
|
|
while (!stop.stop_requested()) {
|
|
const auto now = std::chrono::steady_clock::now();
|
|
const auto elapsed_since_reconcile =
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now - last_reconcile);
|
|
const auto poll_wait = std::min<std::chrono::milliseconds>(
|
|
std::chrono::milliseconds{100},
|
|
options.reconcile_interval > elapsed_since_reconcile
|
|
? (options.reconcile_interval - elapsed_since_reconcile)
|
|
: std::chrono::milliseconds{10});
|
|
|
|
auto batch = watcher->poll_events(poll_wait, options.debounce);
|
|
if (stop.stop_requested()) {
|
|
break;
|
|
}
|
|
|
|
bool selection_changed = false;
|
|
if (!options.explicit_profile) {
|
|
if (auto sel = Editor::read_selection_state(options.frontend.project_root);
|
|
sel && sel->has_value() && (*sel)->selected_profile != last_observed_selection) {
|
|
last_observed_selection = (*sel)->selected_profile;
|
|
selection_changed = true;
|
|
}
|
|
}
|
|
|
|
const bool periodic_due =
|
|
(std::chrono::steady_clock::now() - last_reconcile) >= options.reconcile_interval;
|
|
const bool event_triggered =
|
|
!batch || batch->overflowed || batch->rescan_required || !batch->changed_paths.empty();
|
|
|
|
if (event_triggered || periodic_due || selection_changed) {
|
|
last_reconcile = std::chrono::steady_clock::now();
|
|
auto rep = run_reconcile_cycle(options, cycle++, false, std::nullopt, stop);
|
|
if (options.on_cycle) {
|
|
options.on_cycle(rep);
|
|
}
|
|
reports.push_back(std::move(rep));
|
|
if (options.max_cycles > 0 && reports.size() >= options.max_cycles) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return reports;
|
|
}
|
|
|
|
} // namespace Void::Build::Watch
|