feat: adopt VoidLibrary VL-001..VL-013, self-building build.cpp, live logs, and generated CLI help
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s

This commit is contained in:
faumaray committed 2026-11-27 01:43:06 +03:00
1 parent ab64147f9d
commit 363fb8200e
33 files changed
+1754 -3155

No files matched your search

+57 -178
View File
@@ -4,113 +4,10 @@
#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,
@@ -122,99 +19,70 @@ InotifyWatcher::create(const std::filesystem::path &root_dir,
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")};
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())};
}
InotifyWatcher watcher{fd, canon, std::move(excluded_names)};
if (auto r = watcher.watch_tree(canon); !r) {
return std::unexpected{r.error()};
}
return watcher;
return InotifyWatcher{std::move(*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);
if (!watcher_) {
return {};
}
wd_to_path_.clear();
return watch_tree(root_);
return watcher_->rescan();
}
Void::Result::Result<WatchBatch, Void::Result::Error>
InotifyWatcher::poll_events(std::chrono::milliseconds timeout, std::chrono::milliseconds debounce) {
WatchBatch batch;
if (fd_ < 0) {
if (!watcher_) {
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")};
auto events = watcher_->poll(timeout);
if (!events) {
return std::unexpected{std::move(events.error())};
}
if (ready == 0) {
if (events->empty()) {
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;
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;
}
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;
}
if (!ev.path.empty()) {
unique_paths.insert(ev.path);
}
if (!ev.old_path.empty()) {
unique_paths.insert(ev.old_path);
}
}
};
drain_now();
ingest(*events);
// Coalesce burst edits across the debounce window.
if (debounce.count() > 0 && (!unique_paths.empty() || batch.rescan_required)) {
@@ -225,13 +93,11 @@ InotifyWatcher::poll_events(std::chrono::milliseconds timeout, std::chrono::mill
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 {
auto more = watcher_->poll(rem);
if (!more || more->empty()) {
break;
}
ingest(*more);
}
}
@@ -344,6 +210,19 @@ WatchCycleReport run_reconcile_cycle(const WatchLoopOptions &options, std::size_
}
}
}
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;