feat(daemon): export the profile compilation database over the Unix socket
This commit is contained in:
1 parent
8001d98bb7
commit
72f4237af2
9 files changed
+153
-21
No files matched your search
+58
-15
@@ -559,6 +559,56 @@ std::string build_project(const DaemonOptions &options, State::StateStore &state
|
||||
return serialize_response(JsonValue{std::move(object)});
|
||||
}
|
||||
|
||||
// Publishes the profile compilation database, selecting it when no editor selection exists.
|
||||
Void::Result::Result<Editor::EditorGeneration, std::string>
|
||||
publish_compdb(const DaemonOptions &options, const Graph::BuildGraph &graph) {
|
||||
const auto frontend = make_frontend_options(options);
|
||||
const auto existing_selection =
|
||||
Editor::read_selection_state(options.project_root).value_or(std::nullopt);
|
||||
Editor::PublishOptions publish;
|
||||
publish.project_root = options.project_root;
|
||||
publish.build_root = options.build_root;
|
||||
publish.update_selection = !existing_selection.has_value();
|
||||
publish.sdk_include_dir = frontend.sdk_include_dir;
|
||||
auto published = Editor::publish_profile_compdb(graph, publish);
|
||||
if (!published) {
|
||||
return std::unexpected{published.error().format()};
|
||||
}
|
||||
return *published;
|
||||
}
|
||||
|
||||
std::filesystem::path effective_compdb_path(const DaemonOptions &options,
|
||||
const Editor::EditorGeneration &published) {
|
||||
std::error_code ec;
|
||||
const auto root_compdb = options.build_root / "compile_commands.json";
|
||||
return std::filesystem::exists(root_compdb, ec) ? root_compdb : published.compdb_path;
|
||||
}
|
||||
|
||||
// Publishes the compilation database and reports its path and entry count.
|
||||
std::string compdb_export_project(const DaemonOptions &options, std::string_view profile_name,
|
||||
std::optional<CompilerFamily> compiler_override) {
|
||||
auto evaluated = evaluate_project_graph(options, profile_name, compiler_override, "compdb");
|
||||
if (!evaluated) {
|
||||
return error_response(evaluated.error());
|
||||
}
|
||||
auto graph = lower_graph(options, *evaluated, {}, "compdb");
|
||||
if (!graph) {
|
||||
return error_response(graph.error());
|
||||
}
|
||||
auto published = publish_compdb(options, *graph);
|
||||
if (!published) {
|
||||
return error_response(std::format("compdb: {}", published.error()));
|
||||
}
|
||||
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
object.emplace("op", JsonValue{std::string{"compdb"}});
|
||||
object.emplace("profile", JsonValue{evaluated->context.name});
|
||||
object.emplace("compdb", JsonValue{effective_compdb_path(options, *published).string()});
|
||||
object.emplace("entries", JsonValue{static_cast<std::int64_t>(published->entry_count)});
|
||||
return serialize_response(JsonValue{std::move(object)});
|
||||
}
|
||||
|
||||
// Publishes the profile compilation database and returns the IDE query metadata JSON.
|
||||
std::string ide_query_project(const DaemonOptions &options, std::string_view profile_name,
|
||||
std::optional<CompilerFamily> compiler_override) {
|
||||
@@ -572,23 +622,12 @@ std::string ide_query_project(const DaemonOptions &options, std::string_view pro
|
||||
}
|
||||
|
||||
const auto frontend = make_frontend_options(options);
|
||||
const auto existing_selection =
|
||||
Editor::read_selection_state(options.project_root).value_or(std::nullopt);
|
||||
|
||||
Editor::PublishOptions publish;
|
||||
publish.project_root = options.project_root;
|
||||
publish.build_root = options.build_root;
|
||||
publish.update_selection = !existing_selection.has_value();
|
||||
publish.sdk_include_dir = frontend.sdk_include_dir;
|
||||
auto published = Editor::publish_profile_compdb(*graph, publish);
|
||||
auto published = publish_compdb(options, *graph);
|
||||
if (!published) {
|
||||
return error_response(std::format("ide_query: {}", published.error().format()));
|
||||
return error_response(std::format("ide_query: {}", published.error()));
|
||||
}
|
||||
|
||||
std::error_code ec;
|
||||
const auto root_compdb = options.build_root / "compile_commands.json";
|
||||
const auto compdb_path =
|
||||
std::filesystem::exists(root_compdb, ec) ? root_compdb : published->compdb_path;
|
||||
const auto compdb_path = effective_compdb_path(options, *published);
|
||||
std::string query =
|
||||
Editor::format_ide_query_json(*graph, compdb_path, frontend.sdk_include_dir);
|
||||
// The socket protocol is one JSON object per line: the embedded query must not contain
|
||||
@@ -756,7 +795,8 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "status") {
|
||||
return {status_response(), false};
|
||||
}
|
||||
if (*name == "describe" || *name == "build" || *name == "ide_query") {
|
||||
if (*name == "describe" || *name == "build" || *name == "ide_query" ||
|
||||
*name == "compdb_export") {
|
||||
std::string profile_name{"debug"};
|
||||
if (const auto entry = object->find("profile"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
@@ -789,6 +829,9 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "ide_query") {
|
||||
return {ide_query_project(options, profile_name, compiler_override), false};
|
||||
}
|
||||
if (*name == "compdb_export") {
|
||||
return {compdb_export_project(options, profile_name, compiler_override), false};
|
||||
}
|
||||
|
||||
std::vector<std::string> selected_targets;
|
||||
if (const auto entry = object->find("targets"); entry != object->end()) {
|
||||
|
||||
+21
-3
@@ -1443,6 +1443,13 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
}
|
||||
request_json =
|
||||
std::format(R"({{"op":"describe","profile":{}}})", Util::json_quote(opts.profile_arg));
|
||||
} else if (sub == "compdb_export") {
|
||||
if (opts.profile_arg.empty()) {
|
||||
std::cerr << "vbs daemon compdb export: --profile must not be empty\n";
|
||||
return 2;
|
||||
}
|
||||
request_json = std::format(R"({{"op":"compdb_export","profile":{}}})",
|
||||
Util::json_quote(opts.profile_arg));
|
||||
} else if (sub == "ide_query") {
|
||||
if (opts.profile_arg.empty()) {
|
||||
std::cerr << "vbs daemon ide query: --profile must not be empty\n";
|
||||
@@ -1474,9 +1481,10 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
// Describe evaluates the whole project graph (descriptor compile, toolchain probe, lowering),
|
||||
// which can exceed the 2 s default; the daemon bounds evaluation at 10 s.
|
||||
const auto request_timeout = sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
|
||||
: sub == "describe" ? std::chrono::milliseconds{15000}
|
||||
: sub == "ide_query" ? std::chrono::milliseconds{30000}
|
||||
: std::chrono::milliseconds{2000};
|
||||
: sub == "describe" ? std::chrono::milliseconds{15000}
|
||||
: (sub == "ide_query" || sub == "compdb_export")
|
||||
? std::chrono::milliseconds{30000}
|
||||
: std::chrono::milliseconds{2000};
|
||||
auto response = Daemon::request(socket_path, request_json, request_timeout);
|
||||
if (!response) {
|
||||
std::cerr << "vbs daemon: " << response.error().message() << '\n';
|
||||
@@ -2419,6 +2427,13 @@ int main(int argc, char **argv) {
|
||||
.option_many("--target", "-t", "Target to build", daemon_opts.targets)
|
||||
.option_default("--jobs", "-j", "Maximum parallel jobs (0 = hardware concurrency)",
|
||||
daemon_opts.jobs_arg, "0");
|
||||
auto &daemon_compdb_cmd =
|
||||
daemon_cmd.subcommand("compdb", "Editor compilation database served by the daemon");
|
||||
auto &daemon_compdb_export_cmd =
|
||||
daemon_compdb_cmd.subcommand("export", "Publish the profile compilation database");
|
||||
configure_daemon_common(daemon_compdb_export_cmd);
|
||||
daemon_compdb_export_cmd.option_default("--profile", "-p", "Profile to export",
|
||||
daemon_opts.profile_arg, "debug");
|
||||
auto &daemon_ide_cmd = daemon_cmd.subcommand("ide", "Editor/IDE metadata served by the daemon");
|
||||
configure_daemon_common(daemon_ide_cmd);
|
||||
auto &daemon_ide_query_cmd =
|
||||
@@ -2522,6 +2537,9 @@ int main(int argc, char **argv) {
|
||||
if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "query") {
|
||||
daemon_sub = "ide_query";
|
||||
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "export") {
|
||||
daemon_sub = "compdb_export";
|
||||
}
|
||||
return execute_daemon_command(daemon_sub, daemon_opts);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user