This is a C plugin for [Cap'n Proto](http://kentonv.github.io/capnproto), an efficient protocol for sharing data and capabilities.
Find a file
2023-09-23 22:21:14 -07:00
.github/workflows Add GitHub CI 2023-04-23 17:32:46 +08:00
.vscode Add C++ standard to CMake for MSVC 2023-08-07 20:55:41 -07:00
ac tack on autoconf/automake build 2016-02-28 13:55:23 +01:00
ci Add CMake and tests 2023-08-07 20:28:42 -07:00
cmake Do ./dk dkml.wrapper.upgrade 2023-08-10 16:27:55 -07:00
compiler Fix clang-tidy bugprone-macro-parentheses 2023-08-03 18:19:55 -07:00
lib Avoid undefined left shift of negatives 2023-08-07 17:26:44 -07:00
m4 tack on autoconf/automake build 2016-02-28 13:55:23 +01:00
subprojects Initial Meson build support 2023-04-23 17:10:53 +08:00
tests Add C++ standard to CMake for MSVC 2023-08-07 20:55:41 -07:00
.gitignore Add CMake and tests 2023-08-07 20:28:42 -07:00
.gitlab-ci.yml Cache .ci for Conda CI machines 2023-08-08 09:06:00 -07:00
.gitmodules Add CMake and tests 2023-08-07 20:28:42 -07:00
.travis.yml Travis CI: whatever. 2016-02-28 18:38:41 +01:00
c-capnproto.pc.in add & install pkgconfig file 2016-03-19 00:39:30 +01:00
CHANGES.md Add C_CAPNPROTO_ENABLE_INSTALL 2023-09-23 22:21:14 -07:00
CMakeLists.txt Add C_CAPNPROTO_ENABLE_INSTALL 2023-09-23 22:21:14 -07:00
CMakePresets.json Use dksdk.android.ndk.download location 2023-08-10 16:32:35 -07:00
configure.ac c-capnproto 0.3 release 2017-05-19 19:30:58 +02:00
COPYING tack on autoconf/automake build 2016-02-28 13:55:23 +01:00
CTestConfig.cmake Add CMake and tests 2023-08-07 20:28:42 -07:00
dk Do ./dk dkml.wrapper.upgrade 2023-08-10 16:27:55 -07:00
dk.cmd Do ./dk dkml.wrapper.upgrade 2023-08-10 16:27:55 -07:00
environment.yml Add CMake and tests 2023-08-07 20:28:42 -07:00
Makefile.am Add comiler/c.capnp.h to Makefile.am noinst_HEADERS. 2017-03-27 03:33:34 +01:00
meson.build Initial Meson build support 2023-04-23 17:10:53 +08:00
meson_options.txt Initial Meson build support 2023-04-23 17:10:53 +08:00
README.md Fix repository links in README 2023-09-07 05:29:32 +00:00

capnpc-c

This is a C plugin for Cap'n Proto, an efficient protocol for sharing data and capabilities.

This is a fork of https://github.com/opensourcerouting/c-capnproto with support for CMake and Windows, a build matrix that includes Android and Windows, and fixes several bugs that start with the CHANGES for version 0.9.0.

The fork is maintained as part of DkML and DkSDK, but PRs are welcome from anybody. If you are looking for your first PR, fixing the false positive memory leaks in the test code would be great!

Security warning

The generated code assumes all input to be trusted. Do NOT use with untrusted input! There is currently no code in place to check if structures/pointers are within bounds.

This is only the code generator plugin, to properly make use of it you need to download, build and install capnpc and then build and install this project and then you can utilize it as:

capnpc compiler/test.capnp -oc

Building on Linux

git clone https://gitlab.com/dkml/ext/c-capnproto.git
cd c-capnproto
cmake --preset=ci-linux_x86_64
cmake --build --preset=ci-tests

Building on Windows

You will need Visual Studio 2019 installed. Other versions of Visual Studio may work; they simply haven't been tested.

Once you have Visual Studio, run the x64 Native Tools Command Prompt for VS 2019 and type the following:

git clone https://gitlab.com/dkml/ext/c-capnproto.git
cd c-capnproto
cmake --preset=ci-windows_x86_64
cmake --build --preset=ci-tests

Usage

Generating C code from a .capnp schema file

The compiler directory contains the C language plugin (capnpc-c) for use with the capnp tool: https://capnproto.org/capnp-tool.html.

capnp will by default search $PATH for capnpc-c - if it's on your PATH, you can generate code for your schema as follows:

capnp compile -o c myschema.capnp

Otherwise, you can specify the path to the c plugin:

capnp compile -o ./capnpc-c myschema.capnp

capnp generates a C struct that corresponds to each capn proto struct, along with read/write functions that convert to/from capn proto form.

If you want accessor functions for struct members, use attribute fieldgetset in your .capnp file as follows:

using C = import "${c-capnproto}/compiler/c.capnp";

$C.fieldgetset;

struct MyStruct {}

Example C code

See the unit tests in tests/example-test.cpp. The example schema file is tests/addressbook.capnp. The tests are written in C++, but only use C features.

You need to compile these runtime library files and link them into your own project's binaries:

Your include path must contain the runtime library directory lib. Header file lib/capnp_c.h contains the public interfaces of the library.

Using make-based builds, make may try to compile ${x}.capnp from ${x}.capnp.c using its built-in rule for compiling ${y} from ${y}.c. You can either disable make's built-in compile rules or just this specific case with the no-op rule: %.capnp: ;.

For further reference, please see the other unit tests in tests, and header file lib/capnp_c.h.

The project quagga-capnproto uses c-capnproto and contains some good examples, as found with this github repository search:

  • Serialization in function bgp_notify_send() in file quagga-capnproto/bgpd/bgp_zmq.c
  • Deserialization in function qzc_callback() in file quagga-capnproto/lib/qzc.c

Example CMake Usage

The minimum CMake version is 3.22. The true CMake minimum version could be lower; you are welcome to test and submit a PR to CMakeLists.txt.

FetchContent_Declare(c-capnproto
        GIT_REPOSITORY https://gitlab.com/dkml/ext/c-capnproto.git
        # For reproducibility use a commit id rather than a tag
        GIT_TAG f07596dbb516329d27ea95060a758f5d48d26366)
FetchContent_MakeAvailable(c-capnproto)

# Creating an executable or library that uses c-capnproto?
# --------------------------------------------------------

# filename: yourcode.c
#
#   #include "capnp_c.h"
#   ...

add_executable(YourExecutable ... yourcode.c)
target_link_libraries(YourExecutable PRIVATE CapnC::Runtime)

Status

https://github.com/opensourcerouting/c-capnproto was a merge of 3 forks of James McKaskill's great work, which had been untouched for a while: