9#include <capnp/schema.h>
10#include <capnp/schema-parser.h>
17#include <initializer_list>
21#include <kj/filesystem.h>
29#include <system_error>
34#define PROXY_BIN "mpgen"
35#define PROXY_DECL "mp/proxy.h"
36#define PROXY_TYPES "mp/proxy-types.h"
47template <
typename Reader>
58template <
typename Reader>
70template <
typename Reader>
82static void ForEachMethod(
const capnp::InterfaceSchema&
interface,
const std::function<
void(
const capnp::InterfaceSchema&
interface,
const capnp::InterfaceSchema::Method)>& callback)
87 for (
const auto method :
interface.getMethods()) {
104 template <
typename Value>
110 operator std::string()
const {
return m_os.str(); }
114static std::string
Cap(kj::StringPtr str)
116 std::string result = str;
117 if (!result.empty() &&
'a' <= result[0] && result[0] <=
'z') result[0] -=
'a' -
'A';
123 return !(type.isVoid() || type.isBool() || type.isInt8() || type.isInt16() || type.isInt32() || type.isInt64() ||
124 type.isUInt8() || type.isUInt16() || type.isUInt32() || type.isUInt64() || type.isFloat32() ||
125 type.isFloat64() || type.isEnum());
145 const kj::ReadableDirectory&
src_dir,
146 const std::vector<kj::Own<const kj::ReadableDirectory>>&
import_dirs)
153 throw std::runtime_error(
"src_prefix is not src_file prefix");
163 throw std::runtime_error(
"include_prefix is not src_file prefix");
172 std::vector<std::string>
args;
174 args.emplace_back(
"compile");
175 args.emplace_back(
"--src-prefix=");
178 args.emplace_back(
"--import-path=");
185 throw std::system_error(
errno, std::system_category(),
"fork");
192 throw std::runtime_error(
"Invoking " capnp_PREFIX "/bin/capnp failed");
195 const capnp::SchemaParser
parser;
204 cpp_server <<
"// IWYU pragma: no_include <kj/memory.h>\n";
205 cpp_server <<
"// IWYU pragma: no_include <memory>\n";
209 cpp_server <<
"#include <capnp/generated-header-support.h>\n";
223 cpp_client <<
"// IWYU pragma: no_include <kj/memory.h>\n";
224 cpp_client <<
"// IWYU pragma: no_include <memory>\n";
229 cpp_client <<
"#include <capnp/generated-header-support.h>\n";
241 cpp_types <<
"// IWYU pragma: no_include \"mp/proxy.h\"\n";
242 cpp_types <<
"// IWYU pragma: no_include \"mp/proxy-io.h\"\n";
249 std::ranges::transform(
guard,
guard.begin(), [](
unsigned char c) ->
unsigned char {
250 if (
'0' <= c && c <=
'9') return c;
251 if (
'A' <= c && c <=
'Z') return c;
252 if (
'a' <= c && c <=
'z') return c -
'a' +
'A';
258 inl <<
"#ifndef " <<
guard <<
"_PROXY_TYPES_H\n";
259 inl <<
"#define " <<
guard <<
"_PROXY_TYPES_H\n\n";
260 inl <<
"// IWYU pragma: no_include \"mp/proxy.h\"\n";
261 inl <<
"#include <mp/proxy.h> // IWYU pragma: keep\n";
262 inl <<
"#include <" <<
include_path <<
".proxy.h> // IWYU pragma: keep\n";
265 inl <<
"#include \"" <<
annotation.getValue().getText() <<
"\" // IWYU pragma: export\n";
268 inl <<
"namespace mp {\n";
272 h <<
"#ifndef " <<
guard <<
"_PROXY_H\n";
273 h <<
"#define " <<
guard <<
"_PROXY_H\n\n";
274 h <<
"#include <" <<
include_path <<
".h> // IWYU pragma: keep\n";
277 h <<
"#include \"" <<
annotation.getValue().getText() <<
"\" // IWYU pragma: export\n";
281 h <<
"#if defined(__GNUC__)\n";
282 h <<
"#pragma GCC diagnostic push\n";
283 h <<
"#if !defined(__has_warning)\n";
284 h <<
"#pragma GCC diagnostic ignored \"-Wsuggest-override\"\n";
285 h <<
"#elif __has_warning(\"-Wsuggest-override\")\n";
286 h <<
"#pragma GCC diagnostic ignored \"-Wsuggest-override\"\n";
289 h <<
"namespace mp {\n";
303 std::ostringstream dec;
314 accessors <<
" template<typename S> static auto get(S&& s) -> decltype(s.get" <<
cap <<
"()) { return s.get" <<
cap <<
"(); }\n";
315 accessors <<
" template<typename S> static bool has(S&& s) { return s.has" <<
cap <<
"(); }\n";
316 accessors <<
" template<typename S, typename A> static void set(S&& s, A&& a) { s.set" <<
cap
317 <<
"(std::forward<A>(a)); }\n";
318 accessors <<
" template<typename S, typename... A> static decltype(auto) init(S&& s, A&&... a) { return s.init"
319 <<
cap <<
"(std::forward<A>(a)...); }\n";
320 accessors <<
" template<typename S> static bool getWant(S&& s) { return s.getWant" <<
cap <<
"(); }\n";
321 accessors <<
" template<typename S> static void setWant(S&& s) { s.setWant" <<
cap <<
"(true); }\n";
322 accessors <<
" template<typename S> static bool getHas(S&& s) { return s.getHas" <<
cap <<
"(); }\n";
323 accessors <<
" template<typename S> static void setHas(S&& s) { s.setHas" <<
cap <<
"(true); }\n";
333 if (
node.getProto().isStruct()) {
339 for (
const auto param :
node.getProto().getParameters()) {
347 dec <<
"typename " << param.getName();
359 <<
"_fields::" <<
Cap(
field_name) <<
", FIELD_IN | FIELD_OUT";
363 dec <<
" using Accessors = std::tuple<";
370 dec <<
Cap(
field.getProto().getName()) <<
"Accessor";
374 dec <<
" static constexpr size_t fields = " << i <<
";\n";
378 inl <<
"template<>\n";
391 inl <<
" static decltype(auto) get(std::integral_constant<size_t, " << i <<
">) { return "
395 inl <<
" static constexpr size_t fields = " << i <<
";\n";
403 std::ostringstream client;
407 client <<
"public:\n";
408 client <<
" using ProxyClientCustom::ProxyClientCustom;\n";
409 client <<
" ~ProxyClient();\n";
411 std::ostringstream server;
415 server <<
"public:\n";
416 server <<
" using ProxyServerCustom::ProxyServerCustom;\n";
417 server <<
" ~ProxyServer();\n";
424 const kj::StringPtr
method_name = method.getProto().getName();
435 ::capnp::StructSchema::Field param;
437 ::capnp::StructSchema::Field result;
441 bool optional =
false;
442 bool requested =
false;
444 kj::StringPtr exception;
447 std::vector<Field>
fields;
464 field.param_is_set =
true;
467 field.result_is_set =
true;
470 if (!param &&
field_name == kj::StringPtr{
"result"}) {
494 for (
const auto schema_field : method.getParamType().getFields()) {
497 for (
const auto schema_field : method.getResultType().getFields()) {
528 if (
field.skip)
continue;
535 if (!
field.param_is_set) {
537 }
else if (
field.result_is_set) {
549 for (
int i = 0; i <
field.args; ++i) {
566 if (
field.exception.size()) {
582 if (
field.exception.size()) {
584 }
else if (
field.retval) {
615 <<
"Context call_context) override;\n";
619 <<
"Context call_context) {\n"
620 " return serverInvoke(*this, call_context, "
633 dec <<
"\n" << client.str() <<
"\n" << server.str() <<
"\n";
642 dec <<
" using Client = ProxyClient<Message>;\n";
643 dec <<
" using Server = ProxyServer<Message>;\n";
648 <<
">::~ProxyClient() { clientDestroy(*this); " <<
client_destroy.str() <<
" }\n";
650 <<
">::~ProxyServer() { serverDestroy(*this); }\n";
668 inl <<
"} // namespace mp\n";
671 h <<
"} // namespace mp\n";
672 h <<
"#if defined(__GNUC__)\n";
673 h <<
"#pragma GCC diagnostic pop\n";
681 std::cerr <<
"Usage: " <<
PROXY_BIN <<
" SRC_PREFIX INCLUDE_PREFIX SRC_FILE [IMPORT_PATH...]\n";
685 std::vector<kj::Own<const kj::ReadableDirectory>>
import_dirs;
686 auto fs = kj::newDiskFilesystem();
687 auto cwd = fs->getCurrentPath();
688 kj::Own<const kj::ReadableDirectory>
src_dir;
692 throw std::runtime_error(std::string(
"Failed to open src_prefix prefix directory: ") +
argv[1]);
694 for (
int i = 4; i <
argc; ++i) {
699 throw std::runtime_error(std::string(
"Failed to open import directory: ") +
argv[i]);
kj::ArrayPtr< const char > CharSlice
constexpr uint64_t EXCEPTION_ANNOTATION_ID
constexpr uint64_t NAME_ANNOTATION_ID
static OutputStream & operator<<(OutputStream &os, const Array &array)
static void ForEachMethod(const capnp::InterfaceSchema &interface, const std::function< void(const capnp::InterfaceSchema &interface, const capnp::InterfaceSchema::Method)> &callback)
constexpr uint64_t SKIP_ANNOTATION_ID
constexpr uint64_t WRAP_ANNOTATION_ID
constexpr uint64_t INCLUDE_ANNOTATION_ID
static void Generate(kj::StringPtr src_prefix, kj::StringPtr include_prefix, kj::StringPtr src_file, const std::vector< kj::StringPtr > &import_paths, const kj::ReadableDirectory &src_dir, const std::vector< kj::Own< const kj::ReadableDirectory > > &import_dirs)
static bool GetAnnotationInt32(const Reader &reader, uint64_t id, int32_t *result)
static bool BoxedType(const ::capnp::Type &type)
constexpr uint64_t INCLUDE_TYPES_ANNOTATION_ID
constexpr uint64_t NAMESPACE_ANNOTATION_ID
static bool AnnotationExists(const Reader &reader, uint64_t id)
static std::string Cap(kj::StringPtr str)
static bool GetAnnotationText(const Reader &reader, uint64_t id, kj::StringPtr *result)
constexpr uint64_t COUNT_ANNOTATION_ID
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.