Skip to content
This repository was archived by the owner on Nov 4, 2023. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jmckaskill/c-capnproto
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: opensourcerouting/c-capnproto
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
Loading
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: "CI Unit Tests"

on:
push:
branches:
- master
pull_request:

jobs:
Normal:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install meson and ninja
run: pip3 install --user meson ninja
- name: Run Unit Tests
run: |
export PATH=${HOME}/.local/bin:${PATH}
meson setup -Dbuildtype=release build
meson compile -C build
build/capn-test
Sanitizers:
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install meson and ninja
run: pip3 install --user meson ninja
- name: Run Unit Tests with ASAN
env:
ASAN_OPTIONS: detect_leaks=0,detect_odr_violation=0,allocator_may_return_null=1
run: |
export PATH=${HOME}/.local/bin:${PATH}
meson setup -Dbuildtype=debugoptimized -Db_sanitize=address,undefined build
meson compile -C build
build/capn-test
35 changes: 33 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
aclocal.m4
autom4te*.cache
configure
config.h*
confdefs.h
config.status
libtool
stamp-h*

Makefile
Makefile.in
.deps
.libs
.dirstamp

test-driver
*.log
*.trs

tests/*.out

*.o
*.a
*.so
/capn-test
/capnpc-c
*.lo
*.la

capn-test
capnpc-c
c-capnproto.pc

*.tar.gz

subprojects/googletest-*
subprojects/packagecache
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "gtest"]
path = gtest
url = https://github.com/google/googletest.git
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: cpp

# need at least gcc 4.8 for -std=c++11
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.8
- g++-4.8

# use gcc-4.8
install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi

script: autoreconf -f -i -s && ./configure --without-gtest && make && make check

compiler:
- clang
- gcc
File renamed without changes.
28 changes: 0 additions & 28 deletions Makefile

This file was deleted.

94 changes: 94 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = foreign subdir-objects

lib_LTLIBRARIES =
bin_PROGRAMS =
check_PROGRAMS =
EXTRA_DIST =
noinst_HEADERS =
include_HEADERS =


EXTRA_DIST += README.md

EXTRA_DIST += c-capnproto.pc.in
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = c-capnproto.pc

EXTRA_DIST += compiler/c.capnp
capnp_DATA = compiler/c.capnp
AM_CPPFLAGS = \
-I${srcdir}/compiler \
-I${srcdir}/lib

lib_LTLIBRARIES += libcapnp_c.la
libcapnp_c_la_LDFLAGS = -version-info 0:0:0
libcapnp_c_la_SOURCES = \
lib/capn-malloc.c \
lib/capn-stream.c \
lib/capn.c
EXTRA_DIST += \
lib/capn-list.inc

bin_PROGRAMS += capnpc-c
capnpc_c_SOURCES = \
compiler/capnpc-c.c \
compiler/schema.capnp.c \
compiler/str.c
capnpc_c_LDADD = libcapnp_c.la
include_HEADERS += \
lib/capnp_c.h

noinst_HEADERS += \
lib/capnp_priv.h \
compiler/str.h \
compiler/schema.capnp.h \
compiler/c.capnp.h \
compiler/c++.capnp.h

# Don't try to generate any *.capnp files. Otherwise make wants to compile them
# from *.capnp.c and fails.
%.capnp: ;

# googletest
GTEST_LDADD = gtest/googletest/lib/libgtest.la
gtest/googletest/lib/libgtest.la:
make -C gtest/googletest lib/libgtest.la
GTEST_CPPFLAGS = -I${srcdir}/gtest/googletest/include
DIST_SUBDIRS = gtest/googletest

# Tests
check_PROGRAMS += \
capn-test
capn_test_SOURCES = \
tests/capn-test.cpp \
tests/capn-stream-test.cpp \
tests/example-test.cpp \
tests/addressbook.capnp.c \
compiler/test.capnp.c \
compiler/schema-test.cpp \
compiler/schema.capnp.c
noinst_HEADERS += \
compiler/test.capnp.h \
tests/addressbook.capnp.h
EXTRA_DIST += \
compiler/c.capnp \
compiler/c++.capnp \
compiler/schema.capnp \
compiler/test.capnp \
tests/addressbook.capnp
capn_test_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_CPPFLAGS)
capn_test_CXXFLAGS = -std=gnu++11 -pthread
capn_test_LDADD = libcapnp_c.la $(GTEST_LDADD)
capn_test_LDFLAGS = -pthread
TESTS = capn-test

CAPNP_SCHEMA_FILES := $(shell find . -type f -name \*.capnp)

CAPNP ?= capnp
.PHONY: capnp-compile
capnp-compile:
$(CAPNP) compile \
--output=./capnpc-c \
-Icompiler \
$(CAPNP_SCHEMA_FILES)
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
capnpc-c
========

This is a C plugin for [Cap'n Proto](http://kentonv.github.io/capnproto), an
efficient protocol for sharing data and capabilities.

## UNMAINTAINED

This project is currently **NOT MAINTAINED**. If you are interested in
taking over maintenance and/or need this for some project, please look at
issue https://github.com/opensourcerouting/c-capnproto/issues/55

No releases will be made. PRs may sit unreviewed for multiple years. **PRs
MAY get merged WITHOUT ANY REVIEW, as a last ditch attempt to not waste
people's efforts on PRs. This means things may break completely.**

> ## 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:

```sh
capnpc compiler/test.capnp -oc
```

[![Build Status](https://travis-ci.org/opensourcerouting/c-capnproto.svg?branch=master)](https://travis-ci.org/opensourcerouting/c-capnproto)

## Building on Linux

```sh
git clone --recurse-submodules https://github.com/opensourcerouting/c-capnproto
cd c-capnproto
autoreconf -f -i -s
./configure
make
make check
```

## Building with Meson

```sh
git clone --recurse-submodules https://github.com/opensourcerouting/c-capnproto
cd c-capnproto
meson setup build
meson compile -C build
build/capn-test
```

## 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:

```sh
capnp compile -o c myschema.capnp
```

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

```sh
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:

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

### Example C code

See the unit tests in [`tests/example-test.cpp`](tests/example-test.cpp).
The example schema file is [`tests/addressbook.capnp`](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:

* [`lib/capn.c`](lib/capn.c)
* [`lib/capn-malloc.c`](lib/capn-malloc.c)
* [`lib/capn-stream.c`](lib/capn-stream.c)

Your include path must contain the runtime library directory
[`lib`](lib). Header file [`lib/capnp_c.h`](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`](tests), and header file [`lib/capnp_c.h`](lib/capnp_c.h).

The project [`quagga-capnproto`](https://github.com/opensourcerouting/quagga-capnproto) uses `c-capnproto` and contains some good examples, as found with [this github repository search](https://github.com/opensourcerouting/quagga-capnproto/search?utf8=%E2%9C%93&q=capn&type=):

* Serialization in function [`bgp_notify_send()`](https://github.com/opensourcerouting/quagga-capnproto/blob/27061648f3418fac0d217b16a46add534343e841/bgpd/bgp_zmq.c#L81-L96) in file `quagga-capnproto/bgpd/bgp_zmq.c`
* Deserialization in function [`qzc_callback()`](https://github.com/opensourcerouting/quagga-capnproto/blob/27061648f3418fac0d217b16a46add534343e841/lib/qzc.c#L249-L257) in file `quagga-capnproto/lib/qzc.c`

## Status

This is a merge of 3 forks of [James McKaskill's great
work](https://github.com/jmckaskill/c-capnproto), which has been untouched for
a while:

- [liamstask's fork](https://github.com/liamstask/c-capnproto)
- [baruch's fork](https://github.com/baruch/c-capnproto)
- [kylemanna's fork](https://github.com/kylemanna/c-capnproto)
2 changes: 2 additions & 0 deletions ac/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
12 changes: 12 additions & 0 deletions c-capnproto.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
bindir=@bindir@
includedir=@includedir@
codegen=${bindir}/capnpc-c

Name: c-capnproto
Description: Cap'n Proto C bindings
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lcapnp_c
Cflags: -I${includedir}
Loading