feat(ide): emit a CMake File API codemodel reply
This commit is contained in:
1 parent
083af6af79
commit
6cf8c53a02
6 files changed
+334
-19
No files matched your search
+221
-18
@@ -1,3 +1,4 @@
|
||||
#include <Void/Build/BootstrapConfig.hpp>
|
||||
#include <Void/Build/Editor/CompDb.hpp>
|
||||
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
@@ -9,6 +10,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <ranges>
|
||||
|
||||
namespace Void::Build::Editor {
|
||||
namespace {
|
||||
@@ -752,6 +754,12 @@ std::string_view ide_kind_name(IdeKind kind) {
|
||||
return "all";
|
||||
}
|
||||
|
||||
namespace {
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
emit_cmake_file_api(const Graph::BuildGraph &graph, const std::filesystem::path &project_root,
|
||||
const std::filesystem::path &build_root);
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<IdeConfigReport, Void::Result::Error>
|
||||
generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &options) {
|
||||
using Util::json_array;
|
||||
@@ -797,6 +805,11 @@ generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &opt
|
||||
return {};
|
||||
};
|
||||
|
||||
// CMake File API metadata is always regenerated: it is IDE/build metadata owned by VBS.
|
||||
if (auto emitted = emit_cmake_file_api(graph, proj_root, build_root); !emitted) {
|
||||
return std::unexpected{emitted.error()};
|
||||
}
|
||||
|
||||
const bool want_all = (options.editor == IdeKind::All);
|
||||
|
||||
// 1. .clangd configuration (GCC-16 flag sanitization + CompilationDatabase pointer).
|
||||
@@ -874,20 +887,17 @@ generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &opt
|
||||
vs_tasks.push_back(make_vs_task("vbs: refresh compile_commands.json",
|
||||
{"compdb", "export", "--profile", graph.profile.name}, "",
|
||||
false));
|
||||
vs_tasks.push_back(make_vs_task(
|
||||
"vbs: install",
|
||||
{"install", "--profile", graph.profile.name, "--prefix",
|
||||
(options.build_root / "install").string(),
|
||||
"--bundle-dependencies",
|
||||
"--archive", (options.build_root / "install" / "installer.tar").string()},
|
||||
"", false));
|
||||
vs_tasks.push_back(
|
||||
make_vs_task("vbs: install",
|
||||
{"install", "--profile", graph.profile.name, "--prefix",
|
||||
(options.build_root / "install").string(), "--bundle-dependencies",
|
||||
"--archive", (options.build_root / "install" / "installer.tar").string()},
|
||||
"", false));
|
||||
vs_tasks.push_back(make_vs_task(
|
||||
"vbs: package installer",
|
||||
{"install", "--profile", graph.profile.name, "--prefix",
|
||||
(options.build_root / "install").string(),
|
||||
"--bundle-dependencies",
|
||||
"--archive", (options.build_root / "install" / "installer.sh").string(),
|
||||
"--archive-format", "sh"},
|
||||
(options.build_root / "install").string(), "--bundle-dependencies", "--archive",
|
||||
(options.build_root / "install" / "installer.sh").string(), "--archive-format", "sh"},
|
||||
"", false));
|
||||
for (const auto &tname : buildable_targets) {
|
||||
vs_tasks.push_back(make_vs_task(
|
||||
@@ -980,9 +990,8 @@ generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &opt
|
||||
ztasks.push_back(make_zed_task("vbs: test", {"test", "--profile", graph.profile.name}));
|
||||
ztasks.push_back(make_zed_task("vbs: clean", {"clean"}));
|
||||
ztasks.push_back(make_zed_task(
|
||||
"vbs: install",
|
||||
{"install", "--profile", graph.profile.name, "--prefix",
|
||||
(options.build_root / "install").string(), "--bundle-dependencies"}));
|
||||
"vbs: install", {"install", "--profile", graph.profile.name, "--prefix",
|
||||
(options.build_root / "install").string(), "--bundle-dependencies"}));
|
||||
for (const auto &rt : runnables) {
|
||||
ztasks.push_back(
|
||||
make_zed_task("vbs: build " + rt.name,
|
||||
@@ -1080,8 +1089,7 @@ query_compdb_entries(const std::filesystem::path &compdb_path, const CompdbQuery
|
||||
const auto *entries = std::get_if<Void::Json::Strict::Value::Array>(&parsed->value);
|
||||
if (!entries) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
"compilation database must be a JSON array")};
|
||||
Void::System::SystemErrorCode::io_error, "compilation database must be a JSON array")};
|
||||
}
|
||||
|
||||
CompdbQueryResult result;
|
||||
@@ -1099,8 +1107,7 @@ query_compdb_entries(const std::filesystem::path &compdb_path, const CompdbQuery
|
||||
if (file == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (!query.file_filter.empty() &&
|
||||
!std::string_view{*file}.ends_with(query.file_filter)) {
|
||||
if (!query.file_filter.empty() && !std::string_view{*file}.ends_with(query.file_filter)) {
|
||||
continue;
|
||||
}
|
||||
++result.total;
|
||||
@@ -1141,6 +1148,202 @@ query_compdb_entries(const std::filesystem::path &compdb_path, const CompdbQuery
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::string file_api_target_type(TargetKind kind) {
|
||||
switch (kind) {
|
||||
case TargetKind::Executable:
|
||||
case TargetKind::Test:
|
||||
return "EXECUTABLE";
|
||||
case TargetKind::StaticLibrary:
|
||||
return "STATIC_LIBRARY";
|
||||
case TargetKind::SharedLibrary:
|
||||
return "SHARED_LIBRARY";
|
||||
case TargetKind::HeaderOnly:
|
||||
return "INTERFACE_LIBRARY";
|
||||
case TargetKind::ObjectLibrary:
|
||||
return "OBJECT_LIBRARY";
|
||||
case TargetKind::CustomAction:
|
||||
return "UTILITY";
|
||||
}
|
||||
return "UTILITY";
|
||||
}
|
||||
|
||||
std::string safe_file_token(std::string_view text) {
|
||||
std::string out;
|
||||
for (const char c : text) {
|
||||
out.push_back(
|
||||
(std::isalnum(static_cast<unsigned char>(c)) != 0 || c == '.' || c == '_' || c == '-')
|
||||
? c
|
||||
: '_');
|
||||
}
|
||||
return out.empty() ? std::string{"project"} : out;
|
||||
}
|
||||
|
||||
// Emits a CMake File API (codemodel-v2) reply tree so CMake-aware IDEs can consume VBS
|
||||
// projects. The reply directory is entirely VBS-owned and regenerated on every run.
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
emit_cmake_file_api(const Graph::BuildGraph &graph, const std::filesystem::path &project_root,
|
||||
const std::filesystem::path &build_root) {
|
||||
using Util::json_array;
|
||||
using Util::json_quote;
|
||||
|
||||
const auto reply_dir = build_root / ".cmake" / "api" / "v1" / "reply";
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(reply_dir, ec);
|
||||
std::filesystem::create_directories(reply_dir, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("cannot create CMake File API reply directory '{}': {}", reply_dir.string(),
|
||||
ec.message()))};
|
||||
}
|
||||
|
||||
auto write_reply =
|
||||
[&](const std::string &file_name,
|
||||
std::string_view content) -> Void::Result::Result<void, Void::Result::Error> {
|
||||
if (auto written = Platform::atomic_write_file(reply_dir / file_name, content, false);
|
||||
!written) {
|
||||
return std::unexpected{written.error()};
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
const std::string version_block = "{\"major\":2,\"minor\":0}";
|
||||
std::vector<std::string> target_json_files;
|
||||
std::vector<std::string> target_ids;
|
||||
|
||||
for (const auto &target : graph.targets) {
|
||||
std::string compile_groups = "[";
|
||||
bool first_group = true;
|
||||
for (const auto &action : graph.actions) {
|
||||
if (action.target_name != target.name) {
|
||||
continue;
|
||||
}
|
||||
if (action.kind != Graph::ActionKind::Compile &&
|
||||
action.kind != Graph::ActionKind::PrecompiledHeader &&
|
||||
action.kind != Graph::ActionKind::HeaderUnit) {
|
||||
continue;
|
||||
}
|
||||
std::vector<std::size_t> source_indexes;
|
||||
const auto source_it =
|
||||
std::ranges::find(target.expanded_sources, action.logical_source_file);
|
||||
if (source_it != target.expanded_sources.end()) {
|
||||
source_indexes.push_back(static_cast<std::size_t>(
|
||||
std::distance(target.expanded_sources.begin(), source_it)));
|
||||
}
|
||||
if (!first_group) {
|
||||
compile_groups += ',';
|
||||
}
|
||||
first_group = false;
|
||||
compile_groups += "{\"sourceIndexes\":" + json_array(source_indexes, [](std::size_t i) {
|
||||
return std::to_string(i);
|
||||
});
|
||||
compile_groups += ",\"language\":" + json_quote(action.is_c_source ? "C" : "CXX");
|
||||
compile_groups += ",\"compileCommandFragments\":" +
|
||||
json_array(action.arguments, [](const std::string &argument) {
|
||||
return "{\"fragment\":" + Util::json_quote(argument) + '}';
|
||||
});
|
||||
compile_groups +=
|
||||
",\"defines\":" + json_array(action.defines, [](const std::string &d) {
|
||||
return "{\"define\":" + Util::json_quote(d) + '}';
|
||||
});
|
||||
compile_groups +=
|
||||
",\"includes\":" +
|
||||
json_array(action.physical_include_dirs, [](const auto &include) {
|
||||
return "{\"path\":" + Util::json_quote(include.first.string()) + '}';
|
||||
});
|
||||
compile_groups += '}';
|
||||
}
|
||||
compile_groups += ']';
|
||||
|
||||
std::string target_json =
|
||||
"{\"kind\":\"target\",\"version\":" + version_block +
|
||||
",\"name\":" + json_quote(target.name) +
|
||||
",\"type\":" + json_quote(file_api_target_type(target.kind)) + ",\"artifacts\":" +
|
||||
json_array(target.primary_outputs,
|
||||
[](const std::filesystem::path &p) {
|
||||
return "{\"path\":" + Util::json_quote(p.string()) + '}';
|
||||
}) +
|
||||
",\"nameOnDisk\":" +
|
||||
json_quote(target.primary_outputs.empty()
|
||||
? std::string{}
|
||||
: target.primary_outputs.front().filename().string()) +
|
||||
",\"paths\":{\"source\":" + json_quote(graph.project_root.string()) +
|
||||
",\"build\":" + json_quote(graph.profile_build_dir.string()) + "},\"sources\":" +
|
||||
json_array(target.expanded_sources,
|
||||
[](const std::filesystem::path &p) {
|
||||
return "{\"path\":" + Util::json_quote(p.string()) + '}';
|
||||
}) +
|
||||
",\"compileGroups\":" + compile_groups + '}';
|
||||
|
||||
const std::string file_name = "target-" + safe_file_token(target.name) + "-" +
|
||||
Util::sha256_hex(target_json).substr(0, 16) + ".json";
|
||||
if (auto written = write_reply(file_name, target_json); !written) {
|
||||
return written;
|
||||
}
|
||||
target_json_files.push_back(file_name);
|
||||
target_ids.push_back(target.name);
|
||||
}
|
||||
|
||||
std::string target_refs;
|
||||
for (std::size_t i = 0; i < target_json_files.size(); ++i) {
|
||||
if (i > 0) {
|
||||
target_refs += ',';
|
||||
}
|
||||
target_refs += "{\"directoryIndex\":0,\"id\":" + json_quote(":" + target_ids[i]) +
|
||||
",\"jsonFile\":" + json_quote(target_json_files[i]) +
|
||||
",\"name\":" + json_quote(target_ids[i]) +
|
||||
",\"projectIndex\":0,\"targetIndex\":" + std::to_string(i) + '}';
|
||||
}
|
||||
const std::string directory_json =
|
||||
"{\"kind\":\"directory\",\"version\":" + version_block +
|
||||
",\"paths\":{\"build\":" + json_quote(build_root.string()) +
|
||||
",\"source\":" + json_quote(project_root.string()) +
|
||||
"},\"projectIndexes\":[0],\"targetIndexes\":" +
|
||||
json_array(std::views::iota(std::size_t{0}, target_json_files.size()),
|
||||
[](std::size_t i) { return std::to_string(i); }) +
|
||||
'}';
|
||||
const std::string directory_file =
|
||||
"directory-.-" + Util::sha256_hex(directory_json).substr(0, 16) + ".json";
|
||||
if (auto written = write_reply(directory_file, directory_json); !written) {
|
||||
return written;
|
||||
}
|
||||
|
||||
const std::string codemodel_json =
|
||||
"{\"kind\":\"codemodel\",\"version\":" + version_block +
|
||||
",\"paths\":{\"source\":" + json_quote(project_root.string()) +
|
||||
",\"build\":" + json_quote(build_root.string()) +
|
||||
"},\"configurations\":[{\"name\":" + json_quote(graph.profile.name) +
|
||||
",\"projects\":[{\"name\":" + json_quote(project_root.filename().string()) +
|
||||
",\"targetIndexes\":" +
|
||||
json_array(std::views::iota(std::size_t{0}, target_json_files.size()),
|
||||
[](std::size_t i) { return std::to_string(i); }) +
|
||||
"}],\"directories\":[{\"source\":" + json_quote(project_root.string()) +
|
||||
",\"build\":" + json_quote(build_root.string()) +
|
||||
",\"jsonFile\":" + json_quote(directory_file) +
|
||||
",\"projectIndexes\":[0],\"targetIndexes\":" +
|
||||
json_array(std::views::iota(std::size_t{0}, target_json_files.size()),
|
||||
[](std::size_t i) { return std::to_string(i); }) +
|
||||
"}],\"targets\":[" + target_refs + "]}]}";
|
||||
const std::string codemodel_file =
|
||||
"codemodel-v2-" + Util::sha256_hex(codemodel_json).substr(0, 16) + ".json";
|
||||
if (auto written = write_reply(codemodel_file, codemodel_json); !written) {
|
||||
return written;
|
||||
}
|
||||
|
||||
const std::string index_json =
|
||||
"{\"cmake\":{\"generator\":{\"multiConfig\":false},\"version\":{\"major\":0,"
|
||||
"\"minor\":1,\"patch\":0,\"string\":\"" +
|
||||
std::string{Bootstrap::version} +
|
||||
"\"}},\"reply\":{\"codemodel-v2\":{\"jsonFile\":" + json_quote(codemodel_file) +
|
||||
",\"kind\":\"codemodel\",\"version\":" + version_block + "}}}";
|
||||
const std::string index_file = "index-" + Util::sha256_hex(index_json).substr(0, 16) + ".json";
|
||||
return write_reply(index_file, index_json);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string format_ide_query_json(const Graph::BuildGraph &graph,
|
||||
const std::filesystem::path &compdb_path,
|
||||
const std::filesystem::path &sdk_include_dir) {
|
||||
|
||||
Reference in new issue
Block a user