Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s

This commit is contained in:
faumaray committed 2026-11-26 20:45:54 +03:00
1 parent 3cac890ce9
commit ab64147f9d
114 files changed
+23777 -1392

No files matched your search

+574
View File
@@ -0,0 +1,574 @@
#include <Void/Build/Editor/CompDb.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Util/Json.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/Types.hpp>
#include <algorithm>
#include <format>
namespace Void::Build::Editor {
namespace {
using JsonVal = Void::Json::Strict::Value;
Void::Result::Error make_error(std::string msg) {
return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
std::move(msg));
}
std::string sanitize_profile_dir(std::string_view name) {
std::string out;
out.reserve(name.size());
for (const char c : name) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == '_' || c == '-' || c == '.') {
out.push_back(c);
} else {
out.push_back('_');
}
}
return out;
}
std::string_view status_name(GenerationStatus s) {
switch (s) {
case GenerationStatus::Valid:
return "valid";
case GenerationStatus::Stale:
return "stale";
case GenerationStatus::Invalid:
return "invalid";
}
return "invalid";
}
std::string cxx_standard_flag(CxxStandard standard) {
switch (standard) {
case CxxStandard::Default:
return {};
case CxxStandard::Cxx17:
return "-std=c++17";
case CxxStandard::Cxx20:
return "-std=c++20";
case CxxStandard::Cxx23:
return "-std=c++23";
case CxxStandard::Cxx26:
return "-std=c++26";
}
return {};
}
std::string sanitizer_flag(Sanitizer s) {
switch (s) {
case Sanitizer::Address:
return "address";
case Sanitizer::Undefined:
return "undefined";
case Sanitizer::Thread:
return "thread";
case Sanitizer::Memory:
return "memory";
case Sanitizer::Leak:
return "leak";
}
return {};
}
const JsonVal *find_key(const JsonVal::Object &obj, const std::string &k) {
const auto it = obj.find(k);
return it == obj.end() ? nullptr : &it->second;
}
std::string get_str(const JsonVal::Object &obj, const std::string &k) {
const auto *v = find_key(obj, k);
if (!v) {
return {};
}
const auto *s = std::get_if<std::string>(&v->value);
return s ? *s : std::string{};
}
std::int64_t get_i64(const JsonVal::Object &obj, const std::string &k, std::int64_t def = 0) {
const auto *v = find_key(obj, k);
if (!v) {
return def;
}
const auto *i = std::get_if<std::int64_t>(&v->value);
return i ? *i : def;
}
bool get_bool(const JsonVal::Object &obj, const std::string &k, bool def = false) {
const auto *v = find_key(obj, k);
if (!v) {
return def;
}
const auto *b = std::get_if<bool>(&v->value);
return b ? *b : def;
}
std::filesystem::path selection_file_path(const std::filesystem::path &project_root) {
return project_root / ".vbs" / "editor_selection.json";
}
std::filesystem::path selection_lock_path(const std::filesystem::path &project_root) {
return project_root / ".vbs" / "editor_selection.lock";
}
std::string serialize_selection(const SelectionState &st) {
using Util::json_quote;
return "{\"schema_version\":1,\"ticket\":" + std::to_string(st.ticket) +
",\"selected_profile\":" + json_quote(st.selected_profile) +
",\"selected_build_root\":" + json_quote(st.selected_build_root.string()) +
",\"selected_generation_id\":" + json_quote(st.selected_generation_id) +
",\"selected_compdb_path\":" + json_quote(st.selected_compdb_path.string()) +
",\"managed_root_alias_active\":" +
std::string{st.managed_root_alias_active ? "true" : "false"} +
",\"root_alias_warning\":" + json_quote(st.root_alias_warning) + "}\n";
}
std::optional<SelectionState> load_selection_locked(const std::filesystem::path &project_root) {
auto text = Void::FileSystem::read_file(selection_file_path(project_root));
if (!text) {
return std::nullopt;
}
auto parsed = Void::Json::Strict::parse(*text);
if (!parsed) {
return std::nullopt;
}
const auto *obj = std::get_if<JsonVal::Object>(&parsed->value);
if (!obj) {
return std::nullopt;
}
SelectionState st;
st.ticket = static_cast<std::uint64_t>(get_i64(*obj, "ticket", 0));
st.selected_profile = get_str(*obj, "selected_profile");
st.selected_build_root = get_str(*obj, "selected_build_root");
st.selected_generation_id = get_str(*obj, "selected_generation_id");
st.selected_compdb_path = get_str(*obj, "selected_compdb_path");
st.managed_root_alias_active = get_bool(*obj, "managed_root_alias_active", false);
st.root_alias_warning = get_str(*obj, "root_alias_warning");
return st;
}
// Updates `<project_root>/compile_commands.json` ONLY if it does not exist or is already
// a VBS-managed symlink. Preserves any unrelated user-owned file!
Void::Result::Result<bool, Void::Result::Error>
update_managed_root_alias(const std::filesystem::path &project_root,
const std::filesystem::path &build_root,
const std::filesystem::path &target_compdb, std::string &warning_out) {
const auto root_alias = project_root / "compile_commands.json";
auto meta = Platform::stat_lstat(root_alias);
if (!meta) {
return std::unexpected{meta.error()};
}
if (meta->exists) {
if (!meta->is_symlink) {
warning_out = "preserved existing user-owned project-root compile_commands.json";
return false;
}
// Check if symlink points into build_root or is recorded as managed.
std::filesystem::path target_p{meta->symlink_target};
if (!target_p.is_absolute()) {
target_p = (project_root / target_p).lexically_normal();
}
std::error_code ec;
const auto canon_build = std::filesystem::weakly_canonical(build_root, ec);
if (!Platform::path_is_within(canon_build, target_p)) {
warning_out =
"preserved existing non-VBS symlink at project-root compile_commands.json";
return false;
}
}
auto swapped = Platform::atomic_swap_symlink(root_alias, target_compdb);
if (!swapped) {
return std::unexpected{swapped.error()};
}
warning_out.clear();
return true;
}
} // namespace
std::vector<EditorCommandEntry> project_editor_commands(const Graph::BuildGraph &graph) {
std::vector<EditorCommandEntry> entries;
std::error_code ec;
// Identify enabled targets so disabled targets are omitted, while ALL enabled targets
// are included even if a build only executed a subset.
std::set<std::string> enabled_targets;
for (const auto &t : graph.targets) {
if (t.enabled) {
enabled_targets.insert(t.name);
}
}
for (const auto &act : graph.actions) {
if (act.kind != Graph::ActionKind::Compile) {
continue;
}
if (!enabled_targets.contains(act.target_name)) {
continue;
}
// Reconstruct structured logical argv directly from typed operands —
// NEVER regex/string-replace arbitrary flags or macro definitions!
EditorCommandEntry entry;
entry.directory = graph.project_root;
entry.file = act.logical_source_file;
entry.output = act.logical_output_file;
entry.target_name = act.target_name;
entry.arguments.push_back(act.executable.string());
if (!graph.toolchain.sysroot.empty()) {
entry.arguments.push_back("--sysroot=" + graph.toolchain.sysroot);
}
if (const auto std_flag = cxx_standard_flag(act.cxx_standard); !std_flag.empty()) {
entry.arguments.push_back(std_flag);
}
if (act.position_independent) {
entry.arguments.push_back("-fPIC");
}
if (act.lto == LtoMode::Full) {
entry.arguments.push_back("-flto");
} else if (act.lto == LtoMode::Thin) {
entry.arguments.push_back("-flto=thin");
}
if (!act.sanitizers.empty()) {
std::string list;
for (std::size_t i = 0; i < act.sanitizers.size(); ++i) {
if (i > 0) {
list += ',';
}
list += sanitizer_flag(act.sanitizers[i]);
}
entry.arguments.push_back("-fsanitize=" + list);
entry.arguments.push_back("-fno-omit-frame-pointer");
}
if (act.precompiled_header) {
entry.arguments.push_back("-include");
entry.arguments.push_back(act.precompiled_header->string());
}
for (const auto &[inc_path, is_system] : act.logical_include_dirs) {
entry.arguments.push_back(is_system ? "-isystem" : "-I");
entry.arguments.push_back(inc_path.string());
}
for (const auto &def : act.defines) {
entry.arguments.push_back("-D" + def);
}
for (const auto &opt : act.compile_options) {
entry.arguments.push_back(opt);
}
entry.arguments.push_back("-c");
entry.arguments.push_back(act.logical_source_file.string());
entry.arguments.push_back("-o");
entry.arguments.push_back(act.logical_output_file.string());
// Check generated prerequisites readiness (Plan 09/10).
for (const auto &dep_id : act.dependencies) {
const auto dit = graph.action_index_by_id.find(dep_id);
if (dit == graph.action_index_by_id.end()) {
continue;
}
const auto &producer = graph.actions[dit->second];
if (producer.kind == Graph::ActionKind::Custom ||
producer.kind == Graph::ActionKind::PrecompiledHeader) {
for (const auto &out : producer.declared_outputs) {
if (!std::filesystem::exists(out, ec)) {
entry.missing_generated_prerequisites.push_back(out);
}
}
}
}
entries.push_back(std::move(entry));
}
return entries;
}
std::string format_compile_commands_json(const std::vector<EditorCommandEntry> &entries) {
using Util::json_array;
using Util::json_quote;
std::string out{"[\n"};
for (std::size_t i = 0; i < entries.size(); ++i) {
const auto &e = entries[i];
out += " {\"directory\":" + json_quote(e.directory.string()) +
",\"file\":" + json_quote(e.file.string()) +
",\"output\":" + json_quote(e.output.string()) +
",\"arguments\":" + json_array(e.arguments, json_quote) + "}";
if (i + 1 < entries.size()) {
out += ',';
}
out += '\n';
}
out += "]\n";
return out;
}
Void::Result::Result<std::uint64_t, Void::Result::Error>
reserve_selection_ticket(const std::filesystem::path &project_root,
const std::filesystem::path &build_root, std::string_view profile_name) {
auto lock = Platform::FileLock::acquire(selection_lock_path(project_root),
Platform::LockMode::Exclusive);
if (!lock) {
return std::unexpected{lock.error()};
}
SelectionState st = load_selection_locked(project_root).value_or(SelectionState{});
st.ticket += 1;
// Record reservation ticket monotonically without pointing to an unvalidated generation yet.
if (st.selected_build_root.empty()) {
st.selected_build_root = build_root;
}
if (st.selected_profile.empty()) {
st.selected_profile = std::string{profile_name};
}
auto wrote = Platform::atomic_write_file(selection_file_path(project_root),
serialize_selection(st), true);
if (!wrote) {
return std::unexpected{wrote.error()};
}
return st.ticket;
}
Void::Result::Result<EditorGeneration, Void::Result::Error>
publish_profile_compdb(const Graph::BuildGraph &graph, const PublishOptions &options) {
const auto entries = project_editor_commands(graph);
const std::string compdb_json = format_compile_commands_json(entries);
const std::string gen_id =
Util::sha256_hex(graph.profile_identity + "\n" + compdb_json).substr(0, 16);
const auto profile_dir =
options.build_root / "profiles" / sanitize_profile_dir(graph.profile.name);
const auto gen_dir = profile_dir / "editor" / "generations" / gen_id;
const auto gen_compdb = gen_dir / "compile_commands.json";
const auto profile_compdb = profile_dir / "compile_commands.json";
const auto status_file = profile_dir / "editor" / "status.json";
EditorGeneration gen;
gen.generation_id = gen_id;
gen.profile_name = graph.profile.name;
gen.profile_identity = graph.profile_identity;
gen.status = GenerationStatus::Valid;
gen.compdb_path = gen_compdb;
gen.entry_count = entries.size();
for (const auto &e : entries) {
for (const auto &missing : e.missing_generated_prerequisites) {
gen.readiness_warnings.push_back(std::format("target '{}' missing generated input '{}'",
e.target_name, missing.string()));
}
}
// Avoid needless rewrite when identical immutable generation bytes already exist.
bool bytes_changed = true;
if (auto existing = Void::FileSystem::read_file(gen_compdb);
existing && *existing == compdb_json) {
bytes_changed = false;
}
if (bytes_changed) {
if (auto w = Platform::atomic_write_file(gen_compdb, compdb_json, true); !w) {
return std::unexpected{w.error()};
}
}
// Atomically update per-profile compile_commands.json pointer.
if (auto sw = Platform::atomic_swap_symlink(profile_compdb, gen_compdb); !sw) {
return std::unexpected{sw.error()};
}
// Write per-profile status record atomically.
const std::string status_json =
"{\"schema_version\":1,\"profile\":" + Util::json_quote(graph.profile.name) +
",\"generation_id\":" + Util::json_quote(gen_id) +
",\"status\":\"valid\",\"diagnostic\":\"\"" +
",\"compdb_path\":" + Util::json_quote(gen_compdb.string()) +
",\"entry_count\":" + std::to_string(entries.size()) +
",\"readiness_warnings\":" + Util::json_array(gen.readiness_warnings, Util::json_quote) +
"}\n";
if (auto sw = Platform::atomic_write_file(status_file, status_json, true); !sw) {
return std::unexpected{sw.error()};
}
// Coordinate project-level editor selection under short exclusive lock with ticket/revision
// check!
auto lock = Platform::FileLock::acquire(selection_lock_path(options.project_root),
Platform::LockMode::Exclusive);
if (!lock) {
return std::unexpected{lock.error()};
}
SelectionState st = load_selection_locked(options.project_root).value_or(SelectionState{});
bool should_select = false;
if (options.update_selection) {
const std::uint64_t my_ticket = options.reserved_ticket.value_or(st.ticket + 1);
// Reject stale late completions whose reserved ticket was superseded by a newer request!
if (my_ticket >= st.ticket) {
st.ticket = my_ticket;
should_select = true;
}
} else if (st.selected_profile == graph.profile.name &&
st.selected_build_root == options.build_root) {
// Watchers refresh the ALREADY-SELECTED profile without stealing selection!
should_select = true;
}
if (should_select) {
st.selected_profile = graph.profile.name;
st.selected_build_root = options.build_root;
st.selected_generation_id = gen_id;
st.selected_compdb_path = gen_compdb;
const auto build_root_compdb = options.build_root / "compile_commands.json";
if (auto sw = Platform::atomic_swap_symlink(build_root_compdb, gen_compdb); !sw) {
return std::unexpected{sw.error()};
}
if (options.manage_project_root_alias) {
std::string warn;
auto alias_res = update_managed_root_alias(options.project_root, options.build_root,
gen_compdb, warn);
if (!alias_res) {
return std::unexpected{alias_res.error()};
}
st.managed_root_alias_active = *alias_res;
st.root_alias_warning = std::move(warn);
}
if (auto w = Platform::atomic_write_file(selection_file_path(options.project_root),
serialize_selection(st), true);
!w) {
return std::unexpected{w.error()};
}
}
return gen;
}
Void::Result::Result<void, Void::Result::Error>
mark_profile_stale(const std::filesystem::path &build_root, std::string_view profile_name,
GenerationStatus status, std::string_view diagnostic) {
const auto profile_dir = build_root / "profiles" / sanitize_profile_dir(profile_name);
const auto status_file = profile_dir / "editor" / "status.json";
// Preserve previous valid generation ID/path if present so editors see clearly stale DB.
std::string prev_gen;
std::string prev_path;
if (auto existing = Void::FileSystem::read_file(status_file); existing) {
if (auto parsed = Void::Json::Strict::parse(*existing); parsed) {
if (const auto *obj = std::get_if<JsonVal::Object>(&parsed->value)) {
prev_gen = get_str(*obj, "generation_id");
prev_path = get_str(*obj, "compdb_path");
}
}
}
const std::string json = "{\"schema_version\":1,\"profile\":" + Util::json_quote(profile_name) +
",\"generation_id\":" + Util::json_quote(prev_gen) +
",\"status\":" + Util::json_quote(status_name(status)) +
",\"diagnostic\":" + Util::json_quote(diagnostic) +
",\"compdb_path\":" + Util::json_quote(prev_path) + "}\n";
return Platform::atomic_write_file(status_file, json, true);
}
Void::Result::Result<SelectionState, Void::Result::Error>
select_editor_profile(const std::filesystem::path &project_root,
const std::filesystem::path &build_root, std::string_view profile_name,
bool manage_project_root_alias) {
const auto profile_dir = build_root / "profiles" / sanitize_profile_dir(profile_name);
const auto status_file = profile_dir / "editor" / "status.json";
auto text = Void::FileSystem::read_file(status_file);
if (!text) {
return std::unexpected{make_error(
std::format("no published compilation database for profile '{}'", profile_name))};
}
auto parsed = Void::Json::Strict::parse(*text);
if (!parsed) {
return std::unexpected{make_error("corrupt editor status metadata")};
}
const auto *obj = std::get_if<JsonVal::Object>(&parsed->value);
if (!obj) {
return std::unexpected{make_error("invalid editor status metadata")};
}
const auto gen_id = get_str(*obj, "generation_id");
const auto compdb_str = get_str(*obj, "compdb_path");
if (compdb_str.empty() || !std::filesystem::exists(compdb_str)) {
return std::unexpected{make_error(std::format(
"profile '{}' has no valid compilation database generation", profile_name))};
}
auto lock = Platform::FileLock::acquire(selection_lock_path(project_root),
Platform::LockMode::Exclusive);
if (!lock) {
return std::unexpected{lock.error()};
}
SelectionState st = load_selection_locked(project_root).value_or(SelectionState{});
st.ticket += 1;
st.selected_profile = std::string{profile_name};
st.selected_build_root = build_root;
st.selected_generation_id = gen_id;
st.selected_compdb_path = compdb_str;
if (auto sw = Platform::atomic_swap_symlink(build_root / "compile_commands.json",
st.selected_compdb_path);
!sw) {
return std::unexpected{sw.error()};
}
if (manage_project_root_alias) {
std::string warn;
auto alias_res =
update_managed_root_alias(project_root, build_root, st.selected_compdb_path, warn);
if (!alias_res) {
return std::unexpected{alias_res.error()};
}
st.managed_root_alias_active = *alias_res;
st.root_alias_warning = std::move(warn);
}
if (auto w = Platform::atomic_write_file(selection_file_path(project_root),
serialize_selection(st), true);
!w) {
return std::unexpected{w.error()};
}
return st;
}
Void::Result::Result<std::optional<SelectionState>, Void::Result::Error>
read_selection_state(const std::filesystem::path &project_root) {
auto lock =
Platform::FileLock::acquire(selection_lock_path(project_root), Platform::LockMode::Shared);
if (!lock) {
return std::unexpected{lock.error()};
}
return load_selection_locked(project_root);
}
Void::Result::Result<void, Void::Result::Error>
recover_editor_pointers(const std::filesystem::path &project_root) {
auto lock = Platform::FileLock::acquire(selection_lock_path(project_root),
Platform::LockMode::Exclusive);
if (!lock) {
return std::unexpected{lock.error()};
}
auto st = load_selection_locked(project_root);
if (!st || st->selected_compdb_path.empty() ||
!std::filesystem::exists(st->selected_compdb_path)) {
return {};
}
if (!st->selected_build_root.empty()) {
(void)Platform::atomic_swap_symlink(st->selected_build_root / "compile_commands.json",
st->selected_compdb_path);
}
if (st->managed_root_alias_active) {
std::string warn;
(void)update_managed_root_alias(project_root, st->selected_build_root,
st->selected_compdb_path, warn);
}
return {};
}
} // namespace Void::Build::Editor