feat(translation,ide): add native C driver, Ninja custom commands, CMake CXX_MODULES, and vbs ide integration
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s

This commit is contained in:
faumaray committed 2026-11-27 07:50:54 +03:00
1 parent 5e1916dc5c
commit 6c225c2dff
27 files changed
+3443 -331

No files matched your search

+522 -4
View File
@@ -46,6 +46,20 @@ std::string_view status_name(GenerationStatus s) {
return "invalid";
}
std::string c_standard_flag(CStandard standard) {
switch (standard) {
case CStandard::Default:
return {};
case CStandard::C11:
return "-std=c11";
case CStandard::C17:
return "-std=c17";
case CStandard::C23:
return "-std=c23";
}
return {};
}
std::string cxx_standard_flag(CxxStandard standard) {
switch (standard) {
case CxxStandard::Default:
@@ -193,6 +207,48 @@ update_managed_root_alias(const std::filesystem::path &project_root,
return true;
}
std::string escape_xml_attr(std::string_view sv) {
std::string out;
out.reserve(sv.size());
for (const char c : sv) {
switch (c) {
case '&':
out += "&";
break;
case '<':
out += "&lt;";
break;
case '>':
out += "&gt;";
break;
case '"':
out += "&quot;";
break;
case '\'':
out += "&apos;";
break;
default:
out.push_back(c);
break;
}
}
return out;
}
std::string workspace_var_path(const std::filesystem::path &project_root,
const std::filesystem::path &target, std::string_view prefix_var) {
const auto norm_root = project_root.lexically_normal();
const auto norm_target = target.lexically_normal();
if (!norm_root.empty() && Platform::path_is_within(norm_root, norm_target)) {
const auto rel = norm_target.lexically_relative(norm_root);
if (!rel.empty() && rel != ".") {
return std::string{prefix_var} + "/" + rel.generic_string();
}
return std::string{prefix_var};
}
return norm_target.string();
}
} // namespace
std::vector<EditorCommandEntry> project_editor_commands(const Graph::BuildGraph &graph) {
@@ -228,8 +284,14 @@ std::vector<EditorCommandEntry> project_editor_commands(const Graph::BuildGraph
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.is_c_source) {
if (const auto std_flag = c_standard_flag(act.c_standard); !std_flag.empty()) {
entry.arguments.push_back(std_flag);
}
} else {
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");
@@ -250,7 +312,7 @@ std::vector<EditorCommandEntry> project_editor_commands(const Graph::BuildGraph
entry.arguments.push_back("-fsanitize=" + list);
entry.arguments.push_back("-fno-omit-frame-pointer");
}
if (act.precompiled_header) {
if (act.precompiled_header && !act.is_c_source) {
entry.arguments.push_back("-include");
entry.arguments.push_back(act.precompiled_header->string());
}
@@ -292,6 +354,76 @@ std::vector<EditorCommandEntry> project_editor_commands(const Graph::BuildGraph
return entries;
}
std::vector<EditorCommandEntry> project_build_descriptor_commands(
const std::filesystem::path &project_root, const std::filesystem::path &build_root,
const std::filesystem::path &sdk_include_dir, const std::filesystem::path &host_cxx_compiler,
const std::vector<std::filesystem::path> &extra_build_sources) {
std::vector<EditorCommandEntry> entries;
if (sdk_include_dir.empty() || host_cxx_compiler.empty()) {
return entries;
}
std::error_code ec;
const auto raw_canon = std::filesystem::weakly_canonical(project_root, ec);
const auto canon_root = (ec ? project_root : raw_canon).lexically_normal();
const auto desc_obj_root = build_root / ".vbs" / "editor-desc";
std::vector<std::filesystem::path> candidates;
std::set<std::filesystem::path> seen;
auto push_if_file = [&](const std::filesystem::path &p) {
const auto norm = (p.is_absolute() ? p : (canon_root / p)).lexically_normal();
if (std::filesystem::is_regular_file(norm, ec) && seen.insert(norm).second) {
candidates.push_back(norm);
}
};
push_if_file("build.cpp");
push_if_file("build.profiles.cpp");
const auto support_dir = canon_root / "build-support";
if (std::filesystem::is_directory(support_dir, ec)) {
std::vector<std::filesystem::path> support_files;
for (const auto &ent : std::filesystem::recursive_directory_iterator(support_dir, ec)) {
if (!ent.is_regular_file(ec)) {
continue;
}
const auto ext = ent.path().extension().string();
if (ext == ".cpp" || ext == ".cc" || ext == ".cxx") {
support_files.push_back(ent.path().lexically_normal());
}
}
std::ranges::sort(support_files);
for (const auto &sf : support_files) {
push_if_file(sf);
}
}
for (const auto &extra : extra_build_sources) {
push_if_file(extra);
}
const bool has_support = std::filesystem::is_directory(support_dir, ec);
for (const auto &src : candidates) {
const std::string tag = Util::sha256_hex(src.string()).substr(0, 12);
const auto out_obj = desc_obj_root / (src.filename().string() + "_" + tag + ".o");
EditorCommandEntry entry;
entry.directory = canon_root;
entry.file = src;
entry.output = out_obj;
entry.target_name = "__vbs_build_description";
entry.arguments = {host_cxx_compiler.string(), "-std=c++26", "-I",
sdk_include_dir.lexically_normal().string()};
if (has_support) {
entry.arguments.push_back("-I");
entry.arguments.push_back(support_dir.string());
}
entry.arguments.push_back("-c");
entry.arguments.push_back(src.string());
entry.arguments.push_back("-o");
entry.arguments.push_back(out_obj.string());
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;
@@ -339,7 +471,20 @@ reserve_selection_ticket(const std::filesystem::path &project_root,
Void::Result::Result<EditorGeneration, Void::Result::Error>
publish_profile_compdb(const Graph::BuildGraph &graph, const PublishOptions &options) {
const auto entries = project_editor_commands(graph);
auto entries = project_editor_commands(graph);
if (!options.sdk_include_dir.empty()) {
auto desc_entries = project_build_descriptor_commands(
options.project_root, options.build_root, options.sdk_include_dir,
graph.toolchain.cxx_compiler, options.extra_build_sources);
for (auto &de : desc_entries) {
const bool already = std::ranges::any_of(entries, [&](const EditorCommandEntry &ex) {
return ex.file.lexically_normal() == de.file.lexically_normal();
});
if (!already) {
entries.push_back(std::move(de));
}
}
}
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);
@@ -571,4 +716,377 @@ recover_editor_pointers(const std::filesystem::path &project_root) {
return {};
}
Void::Result::Result<IdeKind, Void::Result::Error> parse_ide_kind(std::string_view name) {
if (name == "all" || name == "All") {
return IdeKind::All;
}
if (name == "clangd" || name == "Clangd") {
return IdeKind::Clangd;
}
if (name == "vscode" || name == "VsCode") {
return IdeKind::VsCode;
}
if (name == "clion" || name == "CLion") {
return IdeKind::CLion;
}
if (name == "zed" || name == "Zed") {
return IdeKind::Zed;
}
return std::unexpected{make_error(
std::format("unknown IDE kind '{}' (expected: all, clangd, vscode, clion, zed)", name))};
}
std::string_view ide_kind_name(IdeKind kind) {
switch (kind) {
case IdeKind::Clangd:
return "clangd";
case IdeKind::VsCode:
return "vscode";
case IdeKind::CLion:
return "clion";
case IdeKind::Zed:
return "zed";
case IdeKind::All:
return "all";
}
return "all";
}
Void::Result::Result<IdeConfigReport, Void::Result::Error>
generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &options) {
using Util::json_array;
using Util::json_quote;
const auto proj_root =
(options.project_root.empty() ? graph.project_root : options.project_root)
.lexically_normal();
const auto build_root =
(options.build_root.empty() ? graph.profile_build_dir.parent_path().parent_path()
: options.build_root)
.lexically_normal();
const auto vbs_bin =
options.vbs_executable.empty() ? std::string{"vbs"} : options.vbs_executable.string();
std::filesystem::path compdb_dir = build_root;
if (!options.compdb_path.empty()) {
compdb_dir =
options.compdb_path.has_parent_path() ? options.compdb_path.parent_path() : compdb_dir;
}
compdb_dir = compdb_dir.lexically_normal();
std::string compdb_rel_or_abs = compdb_dir.string();
if (Platform::path_is_within(proj_root, compdb_dir)) {
const auto rel = compdb_dir.lexically_relative(proj_root);
compdb_rel_or_abs = rel.empty() ? "." : rel.generic_string();
}
IdeConfigReport report;
std::error_code ec;
auto write_ide_file =
[&](const std::filesystem::path &path,
std::string_view content) -> Void::Result::Result<void, Void::Result::Error> {
if (!options.force &&
(std::filesystem::exists(path, ec) || std::filesystem::is_symlink(path, ec))) {
report.skipped_existing_files.push_back(path);
return {};
}
auto w = Platform::atomic_write_file(path, content, false);
if (!w) {
return std::unexpected{w.error()};
}
report.written_files.push_back(path);
return {};
};
const bool want_all = (options.editor == IdeKind::All);
// 1. .clangd configuration (GCC-16 flag sanitization + CompilationDatabase pointer).
if (want_all || options.editor == IdeKind::Clangd) {
std::string yaml = "# Generated by VoidBuildSystem (`vbs ide generate`). Re-run with "
"`--force` to update.\n"
"CompileFlags:\n"
" CompilationDatabase: " +
json_quote(compdb_rel_or_abs) +
"\n"
" Remove:\n"
" - -freflection\n"
" - -fmodules\n"
" - -fmodules-ts\n"
" - \"-fmodule-mapper=*\"\n"
" - \"-frandom-seed=*\"\n"
" - \"-fdeps-format=*\"\n"
" - \"-fdeps-file=*\"\n"
" - \"-fdeps-target=*\"\n";
if (auto r = write_ide_file(proj_root / ".clangd", yaml); !r) {
return std::unexpected{r.error()};
}
}
struct RunnableTarget {
std::string name;
bool is_test{false};
std::filesystem::path program;
};
std::vector<RunnableTarget> runnables;
std::vector<std::string> buildable_targets;
for (const auto &t : graph.targets) {
if (!t.enabled) {
continue;
}
if (t.kind == TargetKind::Executable || t.kind == TargetKind::Test ||
t.kind == TargetKind::StaticLibrary || t.kind == TargetKind::SharedLibrary) {
buildable_targets.push_back(t.name);
}
if ((t.kind == TargetKind::Executable || t.kind == TargetKind::Test) &&
!t.primary_outputs.empty()) {
runnables.push_back({t.name, t.kind == TargetKind::Test, t.primary_outputs.front()});
}
}
// 2. VS Code / Cursor / Antigravity (.vscode/tasks.json, launch.json, settings.json).
if (want_all || options.editor == IdeKind::VsCode) {
auto make_vs_task = [&](const std::string &label, const std::vector<std::string> &args,
const std::string &group, bool is_default) {
std::string t = " {\n";
t += " \"label\": " + json_quote(label) + ",\n";
t += " \"type\": \"process\",\n";
t += " \"command\": " + json_quote(vbs_bin) + ",\n";
t += " \"args\": " + json_array(args, json_quote) + ",\n";
t += " \"options\": {\"cwd\": \"${workspaceFolder}\"},\n";
if (!group.empty()) {
if (is_default) {
t += " \"group\": {\"kind\": " + json_quote(group) +
", \"isDefault\": true},\n";
} else {
t += " \"group\": " + json_quote(group) + ",\n";
}
}
t += " \"problemMatcher\": [\"$gcc\"]\n";
t += " }";
return t;
};
std::vector<std::string> vs_tasks;
vs_tasks.push_back(make_vs_task("vbs: build (" + graph.profile.name + ")",
{"build", "--profile", graph.profile.name}, "build", true));
vs_tasks.push_back(make_vs_task("vbs: test (" + graph.profile.name + ")",
{"test", "--profile", graph.profile.name}, "test", true));
vs_tasks.push_back(make_vs_task("vbs: clean", {"clean"}, "", false));
vs_tasks.push_back(make_vs_task("vbs: refresh compile_commands.json",
{"compdb", "export", "--profile", graph.profile.name}, "",
false));
for (const auto &tname : buildable_targets) {
vs_tasks.push_back(make_vs_task(
"vbs: build " + tname,
{"build", "--profile", graph.profile.name, "--target", tname}, "build", false));
}
for (const auto &rt : runnables) {
if (rt.is_test) {
vs_tasks.push_back(make_vs_task(
"vbs: test " + rt.name,
{"test", "--profile", graph.profile.name, "--target", rt.name}, "test", false));
}
}
std::string tasks_json = "{\n \"version\": \"2.0.0\",\n \"tasks\": [\n";
for (std::size_t i = 0; i < vs_tasks.size(); ++i) {
tasks_json += vs_tasks[i];
if (i + 1 < vs_tasks.size()) {
tasks_json += ',';
}
tasks_json += '\n';
}
tasks_json += " ]\n}\n";
if (auto r = write_ide_file(proj_root / ".vscode" / "tasks.json", tasks_json); !r) {
return std::unexpected{r.error()};
}
std::string launch_json = "{\n \"version\": \"0.2.0\",\n \"configurations\": [\n";
for (std::size_t i = 0; i < runnables.size(); ++i) {
const auto &rt = runnables[i];
const std::string prog =
workspace_var_path(proj_root, rt.program, "${workspaceFolder}");
launch_json += " {\n";
launch_json += " \"name\": " + json_quote("Debug " + rt.name + " (vbs)") + ",\n";
launch_json += " \"type\": \"cppdbg\",\n";
launch_json += " \"request\": \"launch\",\n";
launch_json += " \"program\": " + json_quote(prog) + ",\n";
launch_json += " \"args\": [],\n";
launch_json += " \"stopAtEntry\": false,\n";
launch_json += " \"cwd\": \"${workspaceFolder}\",\n";
launch_json += " \"MIMode\": \"gdb\",\n";
launch_json += " \"preLaunchTask\": " + json_quote("vbs: build " + rt.name) + "\n";
launch_json += " }";
if (i + 1 < runnables.size()) {
launch_json += ',';
}
launch_json += '\n';
}
launch_json += " ]\n}\n";
if (auto r = write_ide_file(proj_root / ".vscode" / "launch.json", launch_json); !r) {
return std::unexpected{r.error()};
}
const std::filesystem::path compdb_file = options.compdb_path.empty()
? (compdb_dir / "compile_commands.json")
: options.compdb_path;
const std::string vs_compdb =
workspace_var_path(proj_root, compdb_file, "${workspaceFolder}");
const std::string vs_compdb_dir =
workspace_var_path(proj_root, compdb_dir, "${workspaceFolder}");
std::string settings_json =
"{\n"
" \"C_Cpp.default.compileCommands\": " +
json_quote(vs_compdb) +
",\n"
" \"C_Cpp.intelliSenseEngine\": \"disabled\",\n"
" \"clangd.arguments\": " +
json_array(std::vector<std::string>{"--compile-commands-dir=" + vs_compdb_dir,
"--header-insertion=never"},
json_quote) +
",\n"
" \"files.exclude\": {\n"
" \"**/.vbs\": true\n"
" }\n"
"}\n";
if (auto r = write_ide_file(proj_root / ".vscode" / "settings.json", settings_json); !r) {
return std::unexpected{r.error()};
}
}
// 3. Zed (.zed/tasks.json, .zed/debug.json).
if (want_all || options.editor == IdeKind::Zed) {
auto make_zed_task = [&](const std::string &label, const std::vector<std::string> &args) {
return " {\"label\": " + json_quote(label) + ", \"command\": " + json_quote(vbs_bin) +
", \"args\": " + json_array(args, json_quote) +
", \"cwd\": \"$ZED_WORKTREE_ROOT\"}";
};
std::vector<std::string> ztasks;
ztasks.push_back(make_zed_task("vbs: build", {"build", "--profile", graph.profile.name}));
ztasks.push_back(make_zed_task("vbs: test", {"test", "--profile", graph.profile.name}));
ztasks.push_back(make_zed_task("vbs: clean", {"clean"}));
for (const auto &rt : runnables) {
ztasks.push_back(
make_zed_task("vbs: build " + rt.name,
{"build", "--profile", graph.profile.name, "--target", rt.name}));
if (rt.is_test) {
ztasks.push_back(
make_zed_task("vbs: test " + rt.name,
{"test", "--profile", graph.profile.name, "--target", rt.name}));
}
}
std::string ztasks_json = "[\n";
for (std::size_t i = 0; i < ztasks.size(); ++i) {
ztasks_json += ztasks[i];
if (i + 1 < ztasks.size()) {
ztasks_json += ',';
}
ztasks_json += '\n';
}
ztasks_json += "]\n";
if (auto r = write_ide_file(proj_root / ".zed" / "tasks.json", ztasks_json); !r) {
return std::unexpected{r.error()};
}
std::string zdebug_json = "[\n";
for (std::size_t i = 0; i < runnables.size(); ++i) {
const auto &rt = runnables[i];
const std::string prog =
workspace_var_path(proj_root, rt.program, "$ZED_WORKTREE_ROOT");
zdebug_json += " {\n";
zdebug_json += " \"label\": " + json_quote("Debug " + rt.name + " (vbs)") + ",\n";
zdebug_json += " \"adapter\": \"CodeLLDB\",\n";
zdebug_json += " \"request\": \"launch\",\n";
zdebug_json += " \"program\": " + json_quote(prog) + ",\n";
zdebug_json += " \"cwd\": \"$ZED_WORKTREE_ROOT\"\n";
zdebug_json += " }";
if (i + 1 < runnables.size()) {
zdebug_json += ',';
}
zdebug_json += '\n';
}
zdebug_json += "]\n";
if (auto r = write_ide_file(proj_root / ".zed" / "debug.json", zdebug_json); !r) {
return std::unexpected{r.error()};
}
}
// 4. CLion Custom Build Targets (.idea/customTargets.xml).
if (want_all || options.editor == IdeKind::CLion) {
const std::string vbs_xml = escape_xml_attr(vbs_bin);
const std::string prof_xml = escape_xml_attr(graph.profile.name);
std::string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<project version=\"4\">\n"
" <component name=\"CLionExternalBuildManager\">\n"
" <target id=\"vbs-build\" name=\"vbs build\" defaultType=\"TOOL\">\n"
" <configuration id=\"vbs-build-cfg\" name=\"" +
prof_xml +
"\">\n"
" <build type=\"TOOL\">\n"
" <tool name=\"vbs build\" program=\"" +
vbs_xml + "\" arguments=\"build --profile " + prof_xml +
"\" workingDir=\"$PROJECT_DIR$\" />\n"
" </build>\n"
" <clean type=\"TOOL\">\n"
" <tool name=\"vbs clean\" program=\"" +
vbs_xml +
"\" arguments=\"clean\" workingDir=\"$PROJECT_DIR$\" />\n"
" </clean>\n"
" </configuration>\n"
" </target>\n"
" </component>\n"
"</project>\n";
if (auto r = write_ide_file(proj_root / ".idea" / "customTargets.xml", xml); !r) {
return std::unexpected{r.error()};
}
}
return report;
}
std::string format_ide_query_json(const Graph::BuildGraph &graph,
const std::filesystem::path &compdb_path,
const std::filesystem::path &sdk_include_dir) {
using Util::json_array;
using Util::json_quote;
const auto build_root = graph.profile_build_dir.parent_path().parent_path().lexically_normal();
std::string out = "{";
out += "\"schema_version\":1";
out += ",\"project_root\":" + json_quote(graph.project_root.string());
out += ",\"build_root\":" + json_quote(build_root.string());
out += ",\"profile\":" + json_quote(graph.profile.name);
out += ",\"profile_identity\":" + json_quote(graph.profile_identity);
out += ",\"compiler_family\":" + json_quote(compiler_name(graph.toolchain.family));
out += ",\"c_compiler\":" + json_quote(graph.toolchain.c_compiler.string());
out += ",\"cxx_compiler\":" + json_quote(graph.toolchain.cxx_compiler.string());
out += ",\"sdk_include_dir\":" + json_quote(sdk_include_dir.string());
out += ",\"compdb_path\":" + json_quote(compdb_path.string());
out += ",\"targets\":[";
for (std::size_t i = 0; i < graph.targets.size(); ++i) {
const auto &t = graph.targets[i];
if (i > 0) {
out += ',';
}
const bool is_exec = (t.kind == TargetKind::Executable);
const bool is_test = (t.kind == TargetKind::Test);
out += "{\"name\":" + json_quote(t.name);
out += ",\"kind\":" + json_quote(target_kind_name(t.kind));
out += ",\"enabled\":" + std::string{t.enabled ? "true" : "false"};
out += ",\"is_executable\":" + std::string{is_exec ? "true" : "false"};
out += ",\"is_test\":" + std::string{is_test ? "true" : "false"};
out += ",\"sources\":" + json_array(t.expanded_sources, [](const std::filesystem::path &p) {
return json_quote(p.string());
});
out += ",\"primary_outputs\":" +
json_array(t.primary_outputs,
[](const std::filesystem::path &p) { return json_quote(p.string()); });
out += ",\"runtime_shared_libraries\":" +
json_array(t.runtime_shared_libraries,
[](const std::filesystem::path &p) { return json_quote(p.string()); });
out += '}';
}
out += "]}\n";
return out;
}
} // namespace Void::Build::Editor