9#include <capnp/schema.capnp.h>
10#include <capnp/schema.h>
11#include <capnp/schema-parser.h>
18#include <initializer_list>
22#include <kj/filesystem.h>
30#include <system_error>
35#define PROXY_BIN "mpgen"
36#define PROXY_DECL "mp/proxy.h"
37#define PROXY_TYPES "mp/proxy-types.h"
48template <
typename Reader>
59template <
typename Reader>
71template <
typename Reader>
83static void ForEachMethod(
const capnp::InterfaceSchema&
interface,
const std::function<
void(
const capnp::InterfaceSchema&
interface,
const capnp::InterfaceSchema::Method)>& callback)
88 for (
const auto method :
interface.getMethods()) {
105 template <
typename Value>
111 operator std::string()
const {
return m_os.str(); }
115static std::string
Cap(kj::StringPtr str)
117 std::string result = str;
118 if (!result.empty() &&
'a' <= result[0] && result[0] <=
'z') result[0] -=
'a' -
'A';
124 return !(type.isVoid() || type.isBool() || type.isInt8() || type.isInt16() || type.isInt32() || type.isInt64() ||
125 type.isUInt8() || type.isUInt16() || type.isUInt32() || type.isUInt64() || type.isFloat32() ||
126 type.isFloat64() || type.isEnum());
159 field.param_is_set =
true;
163 field.result_is_set =
true;
166 if (!param &&
field_name == kj::StringPtr{
"result"}) {
216 std::ostringstream out;
218 if (!
field.param_is_set) {
220 }
else if (
field.result_is_set) {
221 out <<
"FIELD_IN | FIELD_OUT";
225 if (
field.optional) out <<
" | FIELD_OPTIONAL";
226 if (
field.requested) out <<
" | FIELD_REQUESTED";
249 const kj::ReadableDirectory&
src_dir,
250 const std::vector<kj::Own<const kj::ReadableDirectory>>&
import_dirs)
257 throw std::runtime_error(
"src_prefix is not src_file prefix");
267 throw std::runtime_error(
"include_prefix is not src_file prefix");
276 std::vector<std::string>
args;
278 args.emplace_back(
"compile");
279 args.emplace_back(
"--src-prefix=");
282 args.emplace_back(
"--import-path=");
289 throw std::system_error(
errno, std::system_category(),
"fork");
296 throw std::runtime_error(
"Invoking " capnp_PREFIX "/bin/capnp failed");
299 const capnp::SchemaParser
parser;
308 cpp_server <<
"// IWYU pragma: no_include <kj/memory.h>\n";
309 cpp_server <<
"// IWYU pragma: no_include <memory>\n";
313 cpp_server <<
"#include <capnp/generated-header-support.h>\n";
327 cpp_client <<
"// IWYU pragma: no_include <kj/memory.h>\n";
328 cpp_client <<
"// IWYU pragma: no_include <memory>\n";
333 cpp_client <<
"#include <capnp/capability.h>\n";
335 cpp_client <<
"#include <capnp/generated-header-support.h>\n";
350 cpp_types <<
"// IWYU pragma: no_include \"mp/proxy.h\"\n";
351 cpp_types <<
"// IWYU pragma: no_include \"mp/proxy-io.h\"\n";
359 std::ranges::transform(guard, guard.begin(), [](
unsigned char c) ->
unsigned char {
360 if (
'0' <= c && c <=
'9') return c;
361 if (
'A' <= c && c <=
'Z') return c;
362 if (
'a' <= c && c <=
'z') return c -
'a' +
'A';
368 inl <<
"#ifndef " << guard <<
"_PROXY_TYPES_H\n";
369 inl <<
"#define " << guard <<
"_PROXY_TYPES_H\n\n";
370 inl <<
"// IWYU pragma: no_include \"mp/proxy.h\"\n";
371 inl <<
"#include <mp/proxy.h> // IWYU pragma: keep\n";
372 inl <<
"#include <" <<
include_path <<
".proxy.h> // IWYU pragma: keep\n";
375 inl <<
"#include \"" <<
annotation.getValue().getText() <<
"\" // IWYU pragma: export\n";
378 inl <<
"namespace mp {\n";
382 h <<
"#ifndef " << guard <<
"_PROXY_H\n";
383 h <<
"#define " << guard <<
"_PROXY_H\n\n";
384 h <<
"#include <" <<
include_path <<
".h> // IWYU pragma: keep\n";
387 h <<
"#include \"" <<
annotation.getValue().getText() <<
"\" // IWYU pragma: export\n";
391 h <<
"#if defined(__GNUC__)\n";
392 h <<
"#pragma GCC diagnostic push\n";
393 h <<
"#if !defined(__has_warning)\n";
394 h <<
"#pragma GCC diagnostic ignored \"-Wsuggest-override\"\n";
395 h <<
"#elif __has_warning(\"-Wsuggest-override\")\n";
396 h <<
"#pragma GCC diagnostic ignored \"-Wsuggest-override\"\n";
399 h <<
"namespace mp {\n";
413 std::ostringstream dec;
424 accessors <<
" template<typename S> static auto get(S&& s) -> decltype(s.get" <<
cap <<
"()) { return s.get" <<
cap <<
"(); }\n";
425 accessors <<
" template<typename S> static bool has(S&& s) { return s.has" <<
cap <<
"(); }\n";
426 accessors <<
" template<typename S, typename A> static void set(S&& s, A&& a) { s.set" <<
cap
427 <<
"(std::forward<A>(a)); }\n";
428 accessors <<
" template<typename S, typename... A> static decltype(auto) init(S&& s, A&&... a) { return s.init"
429 <<
cap <<
"(std::forward<A>(a)...); }\n";
430 accessors <<
" template<typename S> static bool getWant(S&& s) { return s.getWant" <<
cap <<
"(); }\n";
431 accessors <<
" template<typename S> static void setWant(S&& s) { s.setWant" <<
cap <<
"(true); }\n";
432 accessors <<
" template<typename S> static bool getHas(S&& s) { return s.getHas" <<
cap <<
"(); }\n";
433 accessors <<
" template<typename S> static void setHas(S&& s) { s.setHas" <<
cap <<
"(true); }\n";
443 if (
node.getProto().isStruct()) {
456 for (
const auto param :
node.getProto().getParameters()) {
464 dec <<
"typename " << param.getName();
478 dec <<
" using Accessors = std::tuple<";
481 if (
field.skip)
continue;
483 dec <<
Cap(
field.param.getProto().getName()) <<
"Accessor";
487 dec <<
" static constexpr size_t fields = " << i <<
";\n";
491 inl <<
"template<>\n";
498 if (
field.skip)
continue;
502 inl <<
" static decltype(auto) get(std::integral_constant<size_t, " << i <<
">) { return "
506 inl <<
" static constexpr size_t fields = " << i <<
";\n";
514 std::ostringstream
client;
519 client <<
" using ProxyClientCustom::ProxyClientCustom;\n";
520 client <<
" ~ProxyClient();\n";
522 std::ostringstream server;
526 server <<
"public:\n";
527 server <<
" using ProxyServerCustom::ProxyServerCustom;\n";
528 server <<
" ~ProxyServer();\n";
535 const kj::StringPtr
method_name = method.getProto().getName();
545 for (
const auto schema_field : method.getParamType().getFields()) {
548 for (
const auto schema_field : method.getResultType().getFields()) {
568 if (
field.skip)
continue;
575 for (
int i = 0; i <
field.args; ++i) {
592 if (
field.exception.size()) {
607 if (
field.exception.size()) {
609 }
else if (
field.retval) {
639 <<
"Context call_context) override;\n";
643 <<
"Context call_context) {\n"
644 " return serverInvoke(*this, call_context, "
657 dec <<
"\n" <<
client.str() <<
"\n" << server.str() <<
"\n";
666 dec <<
" using Client = ProxyClient<Message>;\n";
667 dec <<
" using Server = ProxyServer<Message>;\n";
672 <<
">::~ProxyClient() { clientDestroy(*this); " <<
client_destroy.str() <<
" }\n";
674 <<
">::~ProxyServer() { serverDestroy(*this); }\n";
692 inl <<
"} // namespace mp\n";
695 h <<
"} // namespace mp\n";
696 h <<
"#if defined(__GNUC__)\n";
697 h <<
"#pragma GCC diagnostic pop\n";
705 std::cerr <<
"Usage: " <<
PROXY_BIN <<
" SRC_PREFIX INCLUDE_PREFIX SRC_FILE [IMPORT_PATH...]\n";
709 std::vector<kj::Own<const kj::ReadableDirectory>>
import_dirs;
710 auto fs = kj::newDiskFilesystem();
711 auto cwd = fs->getCurrentPath();
712 kj::Own<const kj::ReadableDirectory>
src_dir;
716 throw std::runtime_error(std::string(
"Failed to open src_prefix prefix directory: ") +
argv[1]);
718 for (
int i = 4; i <
argc; ++i) {
723 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
std::string AccessorType(kj::StringPtr base_name, const Field &field)
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
std::unique_ptr< ProxyClient< messages::FooInterface > > client
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.
::capnp::StructSchema::Field result
::capnp::StructSchema::Field param
std::vector< Field > fields
std::map< kj::StringPtr, int > field_idx
void addField(const ::capnp::StructSchema::Field &schema_field, bool param, bool result)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.