Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
This commit is contained in:
1 parent
3cac890ce9
commit
ab64147f9d
114 files changed
+23777
-1392
No files matched your search
@@ -0,0 +1,500 @@
|
||||
#include <Void/Build/Watch/Watcher.hpp>
|
||||
|
||||
#include <Void/Build/Toolchain/Driver.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <format>
|
||||
#include <poll.h>
|
||||
#include <set>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Build::Watch {
|
||||
namespace {
|
||||
|
||||
Void::Result::Error sys_err(int err, std::string msg) {
|
||||
return Void::System::make_system_error(Void::System::map_errno_to_system_code(err),
|
||||
std::move(msg), err);
|
||||
}
|
||||
|
||||
constexpr std::uint32_t kWatchMask = IN_CREATE | IN_DELETE | IN_MODIFY | IN_CLOSE_WRITE |
|
||||
IN_MOVED_FROM | IN_MOVED_TO | IN_ATTRIB | IN_DELETE_SELF |
|
||||
IN_MOVE_SELF;
|
||||
|
||||
} // namespace
|
||||
|
||||
InotifyWatcher::InotifyWatcher(int fd, std::filesystem::path root,
|
||||
std::vector<std::string> excluded)
|
||||
: fd_{fd}, root_{std::move(root)}, excluded_{std::move(excluded)} {}
|
||||
|
||||
InotifyWatcher::~InotifyWatcher() noexcept { close_fd(); }
|
||||
|
||||
InotifyWatcher::InotifyWatcher(InotifyWatcher &&other) noexcept
|
||||
: fd_{std::exchange(other.fd_, -1)}, root_{std::move(other.root_)},
|
||||
excluded_{std::move(other.excluded_)}, wd_to_path_{std::move(other.wd_to_path_)} {}
|
||||
|
||||
InotifyWatcher &InotifyWatcher::operator=(InotifyWatcher &&other) noexcept {
|
||||
if (this != &other) {
|
||||
close_fd();
|
||||
fd_ = std::exchange(other.fd_, -1);
|
||||
root_ = std::move(other.root_);
|
||||
excluded_ = std::move(other.excluded_);
|
||||
wd_to_path_ = std::move(other.wd_to_path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void InotifyWatcher::close_fd() noexcept {
|
||||
if (fd_ >= 0) {
|
||||
(void)::close(fd_);
|
||||
fd_ = -1;
|
||||
}
|
||||
wd_to_path_.clear();
|
||||
}
|
||||
|
||||
bool InotifyWatcher::is_excluded(const std::filesystem::path &rel) const {
|
||||
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;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
InotifyWatcher::watch_tree(const std::filesystem::path &dir) {
|
||||
std::error_code ec;
|
||||
if (!std::filesystem::is_directory(dir, ec)) {
|
||||
return {};
|
||||
}
|
||||
auto add_dir =
|
||||
[&](const std::filesystem::path &p) -> Void::Result::Result<void, Void::Result::Error> {
|
||||
const int wd = ::inotify_add_watch(fd_, p.c_str(), kWatchMask);
|
||||
if (wd < 0) {
|
||||
const int err = errno;
|
||||
if (err == ENOENT || err == ENOTDIR) {
|
||||
return {};
|
||||
}
|
||||
return std::unexpected{sys_err(
|
||||
err, std::format("InotifyWatcher: inotify_add_watch failed for '{}'", p.string()))};
|
||||
}
|
||||
wd_to_path_[wd] = p;
|
||||
return {};
|
||||
};
|
||||
|
||||
if (auto r = add_dir(dir); !r) {
|
||||
return r;
|
||||
}
|
||||
std::filesystem::recursive_directory_iterator it{
|
||||
dir, std::filesystem::directory_options::skip_permission_denied, ec};
|
||||
for (; !ec && it != std::filesystem::recursive_directory_iterator{}; it.increment(ec)) {
|
||||
const auto rel = it->path().lexically_relative(root_);
|
||||
if (is_excluded(rel)) {
|
||||
if (it->is_directory(ec)) {
|
||||
it.disable_recursion_pending();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (it->is_directory(ec)) {
|
||||
if (auto r = add_dir(it->path()); !r) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
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()))};
|
||||
}
|
||||
const int fd = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
const int err = errno;
|
||||
return std::unexpected{sys_err(err, "InotifyWatcher: inotify_init1 failed")};
|
||||
}
|
||||
InotifyWatcher watcher{fd, canon, std::move(excluded_names)};
|
||||
if (auto r = watcher.watch_tree(canon); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
return watcher;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> InotifyWatcher::rescan_watches() {
|
||||
for (const auto &[wd, path] : wd_to_path_) {
|
||||
(void)::inotify_rm_watch(fd_, wd);
|
||||
}
|
||||
wd_to_path_.clear();
|
||||
return watch_tree(root_);
|
||||
}
|
||||
|
||||
Void::Result::Result<WatchBatch, Void::Result::Error>
|
||||
InotifyWatcher::poll_events(std::chrono::milliseconds timeout, std::chrono::milliseconds debounce) {
|
||||
WatchBatch batch;
|
||||
if (fd_ < 0) {
|
||||
batch.rescan_required = true;
|
||||
return batch;
|
||||
}
|
||||
|
||||
struct pollfd pfd{fd_, POLLIN, 0};
|
||||
const int ready = ::poll(&pfd, 1, static_cast<int>(std::max<long long>(0, timeout.count())));
|
||||
if (ready < 0) {
|
||||
if (errno == EINTR) {
|
||||
return batch;
|
||||
}
|
||||
return std::unexpected{sys_err(errno, "InotifyWatcher: poll failed")};
|
||||
}
|
||||
if (ready == 0) {
|
||||
return batch;
|
||||
}
|
||||
|
||||
std::set<std::filesystem::path> unique_paths;
|
||||
alignas(struct inotify_event) std::array<char, 32768> buf{};
|
||||
|
||||
auto drain_now = [&]() {
|
||||
for (;;) {
|
||||
const ssize_t n = ::read(fd_, buf.data(), buf.size());
|
||||
if (n <= 0) {
|
||||
break;
|
||||
}
|
||||
ssize_t off = 0;
|
||||
while (off < n) {
|
||||
const auto *ev = reinterpret_cast<const struct inotify_event *>(buf.data() + off);
|
||||
off += static_cast<ssize_t>(sizeof(struct inotify_event) + ev->len);
|
||||
|
||||
if (ev->mask & IN_Q_OVERFLOW) {
|
||||
batch.overflowed = true;
|
||||
batch.rescan_required = true;
|
||||
continue;
|
||||
}
|
||||
if (ev->mask & IN_IGNORED) {
|
||||
wd_to_path_.erase(ev->wd);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto dir_it = wd_to_path_.find(ev->wd);
|
||||
if (dir_it == wd_to_path_.end()) {
|
||||
batch.rescan_required = true;
|
||||
continue;
|
||||
}
|
||||
std::filesystem::path full = dir_it->second;
|
||||
if (ev->len > 0 && ev->name[0] != '\0') {
|
||||
full /= ev->name;
|
||||
}
|
||||
const auto rel = full.lexically_relative(root_);
|
||||
if (is_excluded(rel)) {
|
||||
continue;
|
||||
}
|
||||
unique_paths.insert(full);
|
||||
|
||||
if ((ev->mask & (IN_CREATE | IN_MOVED_TO)) && (ev->mask & IN_ISDIR)) {
|
||||
auto attached = watch_tree(full);
|
||||
if (!attached) {
|
||||
batch.rescan_required = true;
|
||||
}
|
||||
}
|
||||
if (ev->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
|
||||
batch.rescan_required = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
drain_now();
|
||||
|
||||
// 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;
|
||||
}
|
||||
struct pollfd spfd{fd_, POLLIN, 0};
|
||||
const int sr = ::poll(&spfd, 1, static_cast<int>(rem.count()));
|
||||
if (sr > 0) {
|
||||
drain_now();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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
|
||||
Reference in new issue
Block a user