### Install with Meson (recommended) Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/install.org Recommended build method. Clones the repository with submodules, sets up a Meson build directory, compiles, and optionally installs. The plugin is build/capnpc-c and the runtime is build/libcapnp.a (or shared). ```sh git clone --recurse-submodules https://github.com/HaoZeke/c-capnproto.git cd c-capnproto meson setup build meson compile -C build # plugin: build/capnpc-c runtime: build/libcapnp.a (or shared) meson install -C build # optional: put capnpc-c on PATH ``` -------------------------------- ### Install with Autotools Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/install.org Build method using autotools. Clones the repository with submodules, runs autoreconf, configures, builds, and runs checks. Use this for the full test suite with the existing gtest/ submodule. ```sh git clone --recurse-submodules https://github.com/HaoZeke/c-capnproto.git cd c-capnproto autoreconf -f -i -s ./configure make make check ``` -------------------------------- ### Install with CMake (optional) Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/install.org Alternative install path for MSVC/IDE/packagers. Does not use the DKML dk wrapper and does not fetch or delete the gtest/ submodule. Tests are skipped when GTest is not installed. ```sh cmake -S . -B build-cmake cmake --build build-cmake cmake --install build-cmake ctest --test-dir build-cmake # only if CMake found an installed GTest ``` -------------------------------- ### System install with autotools Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Installs after ./configure --prefix=/usr/local and make. Part of the 'Install and consume' section. ```sh make install ``` -------------------------------- ### System install with Meson Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Installs into /usr/local prefix using Meson. Part of the 'Install and consume' section. ```sh meson setup build --prefix=/usr/local meson compile -C build meson install -C build ``` -------------------------------- ### System install with CMake Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Installs into /usr/local prefix using CMake. Part of the 'Install and consume' section. ```sh cmake -S . -B build-cmake -DCMAKE_INSTALL_PREFIX=/usr/local cmake --build build-cmake cmake --install build-cmake ``` -------------------------------- ### Install c-capnproto with Meson Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/index.org Clone the repository with submodules, then build and test using Meson. Requires git and Meson. ```console $ git clone --recurse-submodules https://github.com/HaoZeke/c-capnproto.git $ cd c-capnproto $ meson setup build && meson compile -C build $ meson test -C build ``` -------------------------------- ### Install c-capnproto with Meson Source: https://github.com/haozeke/c-capnproto/blob/main/docs/source/llms.txt Clones the HaoZeke fork with submodules, configures and compiles with Meson, then runs the test suite. ```sh git clone --recurse-submodules https://github.com/HaoZeke/c-capnproto.git cd c-capnproto meson setup build && meson compile -C build meson test -C build ``` -------------------------------- ### Use pkg-config Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Uses pkg-config to get version, codegen variable, and compile flags. c-capnproto.pc is generated from c-capnproto.pc.in. codegen expands to installed capnpc-c. For multiarch Meson installs, add lib//pkgconfig to PKG_CONFIG_PATH. ```sh export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" pkg-config --modversion c-capnproto pkg-config --variable=codegen c-capnproto cc -o app app.c $(pkg-config --cflags --libs c-capnproto) ``` -------------------------------- ### Read AddressBook with capn_init_mem Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/tutorial.org Reads the message written by the write example. Uses capn_init_mem with unpacked flag 0, capn_getp to get root Person, read_Person, get_Person_PhoneNumber, and capn_free. Comments show expected values and behavior for wire-null Text. ```c struct capn rc; capn_init_mem(&rc, buf, sz, 0); Person_ptr rroot; rroot.p = capn_getp(capn_root(&rc), 0, 1); struct Person rp; read_Person(&rp, rroot); /* rp.id == 17 */ /* rp.name.str / rp.name.len is "Firstname Lastname" */ /* Person_has_name(rroot) is 1; a wire-null Text has has_=0 and get_ "" */ struct Person_PhoneNumber rpn0; get_Person_PhoneNumber(&rpn0, rp.phones, 0); capn_free(&rc); ``` -------------------------------- ### Install capnp_c and capnpc-c targets and headers Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Installs the capnp_c and capnpc-c targets into the c_capnprotoTargets export set, placing runtime, library, and archive files in the configured bindir, libdir, and archdir. Also installs the public headers lib/capnp_c.h and lib/capn-rpc.h into the include directory. Only runs when C_CAPNPROTO_INSTALL is enabled. ```cmake if(C_CAPNPROTO_INSTALL) install(TARGETS capnp_c capnpc-c EXPORT c_capnprotoTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) install(FILES lib/capnp_c.h lib/capn-rpc.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) endif() ``` -------------------------------- ### Compile schema with capnp after meson install Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/usage.org Run after meson install. capnp searches $PATH for capnpc-c. Uses -I${prefix}/share/c-capnproto to find c.capnp. ```sh capnp compile -I${prefix}/share/c-capnproto -o c schema.capnp ``` -------------------------------- ### Install generated headers and data files Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Installs the generated c.capnp.h header and c.capnp schema file to the appropriate include and data directories. ```CMake install(FILES compiler/c.capnp.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES compiler/c.capnp DESTINATION ${CMAKE_INSTALL_DATADIR}/c-capnproto) ``` -------------------------------- ### FetchContent usage example for c-capnproto Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Shows how to consume c-capnproto via CMake FetchContent. Declare the repository with GIT_TAG v0.4.2, then link against the c_capnproto::capnp_c target. This is an optional alternative to Meson or autotools. ```cmake include(FetchContent) FetchContent_Declare( c_capnproto GIT_REPOSITORY https://github.com/HaoZeke/c-capnproto.git GIT_TAG v0.4.2 ) FetchContent_MakeAvailable(c_capnproto) target_link_libraries(myapp PRIVATE c_capnproto::capnp_c) ``` -------------------------------- ### Run Meson build and test Source: https://github.com/haozeke/c-capnproto/blob/main/MAINTAINING.md Initial release checklist step: setup, compile, and test with Meson. Heavy compile happens on the remote builder, not the maintainer laptop. ```shell meson setup build && meson compile -C build && meson test -C build ``` -------------------------------- ### Meson wrap file for c-capnproto Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Wrap file to build this tree as a subproject when no install is present. Tests stay off in that subproject build. Provides c-capnproto as libcapnp_dep. ```ini [wrap-git] url = https://github.com/HaoZeke/c-capnproto.git revision = v0.4.2 depth = 1 [provide] c-capnproto = libcapnp_dep ``` -------------------------------- ### Compile schema from source checkout Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/usage.org Point at the plugin and compiler/ for c.capnp when building from a source checkout. ```sh capnp compile -Icompiler -o ./capnpc-c schema.capnp ``` -------------------------------- ### Generate pkg-config file Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Configures a pkg-config file with absolute or relative paths for prefix, libdir, bindir, and includedir, then installs it to the pkgconfig directory. ```CMake set(prefix "${CMAKE_INSTALL_PREFIX}") set(exec_prefix "\${prefix}") if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}") set(libdir "${CMAKE_INSTALL_LIBDIR}") else() set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}") endif() if(IS_ABSOLUTE "${CMAKE_INSTALL_BINDIR}") set(bindir "${CMAKE_INSTALL_BINDIR}") else() set(bindir "\${prefix}/${CMAKE_INSTALL_BINDIR}") endif() if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") set(includedir "${CMAKE_INSTALL_INCLUDEDIR}") else() set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") endif() set(PACKAGE_VERSION "${PROJECT_VERSION}") configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/c-capnproto.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/c-capnproto.pc" @ONLY ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/c-capnproto.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) ``` -------------------------------- ### Build with CMake Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Optional install-path alternative for MSVC/IDE/packagers. Does not use DKML dk wrapper and does not fetch or delete gtest/ submodule. Tests are skipped when GTest is not installed. ```sh cmake -S . -B build-cmake cmake --build build-cmake cmake --install build-cmake # optional: libcapnp_c, capnpc-c, header, c.capnp, pc ctest --test-dir build-cmake # only if CMake found an installed GTest ``` -------------------------------- ### Build and load the sample kernel module Source: https://github.com/haozeke/c-capnproto/blob/main/KERNEL.md Commands to build the example kernel module in examples/kernel/ and load it with insmod. Requires a built capnpc-c only if regenerating tests/addressbook.capnp.c. Uses /lib/modules/$(uname -r)/build for the build. ```sh # needs a built capnpc-c only if you regenerate tests/addressbook.capnp.c cd examples/kernel make # uses /lib/modules/$(uname -r)/build sudo insmod capnp_addressbook.ko dmesg | tail sudo rmmod capnp_addressbook ``` -------------------------------- ### Use installed package in CMake project Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Consumes the installed c-capnproto package from another CMake project. Requires CMake package files, which only the CMake install provides. Pass -DCMAKE_PREFIX_PATH=/usr/local if prefix is not on default search path. ```cmake find_package(c_capnproto REQUIRED) target_link_libraries(app PRIVATE c_capnproto::capnp_c) ``` -------------------------------- ### Typical write path in C Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/usage.org Initialize a capn context, create a Person, write it, set the root, write to memory, and free. Requires linking lib/capn.c, lib/capn-malloc.c, lib/capn-stream.c and including lib/ in the include path. ```c struct capn c; capn_init_malloc(&c); Person_ptr pp = new_Person(capn_root(&c).seg); write_Person(&person, pp); capn_set_root(&c, pp.p); /* required; otherwise the message is empty */ sz = capn_write_mem(&c, buf, sizeof(buf), 0); capn_free(&c); ``` -------------------------------- ### Export CMake package targets Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Exports the c_capnproto targets with a namespace and installs the package configuration files for find_package support. ```CMake install(EXPORT c_capnprotoTargets FILE c_capnprotoTargets.cmake NAMESPACE c_capnproto:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/c_capnproto ) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/c_capnprotoConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/c_capnprotoConfig.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/c_capnproto ) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/c_capnprotoConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/c_capnprotoConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/c_capnprotoConfigVersion.cmake" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/c_capnproto ) ``` -------------------------------- ### Define capnp_c library with sources and properties Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Defines the capnp_c library with the same source set as meson.build, including RPC vat readers for rpc.capnp and the three-party network layer checked in under compiler/. Sets output name, position-independent code, Windows export symbols, version, and SOVERSION. Creates an alias target c_capnproto::capnp_c and sets include directories for build and install interfaces. ```cmake # Same source set as meson.build. The RPC vat needs the readers for # rpc.capnp and the three-party network layer, which are checked in # under compiler/ rather than generated at build time. add_library(capnp_c lib/capn.c lib/capn-malloc.c lib/capn-stream.c lib/capn-rpc.c compiler/rpc.capnp.c compiler/rpc-threeparty.capnp.c ) set_target_properties(capnp_c PROPERTIES OUTPUT_NAME capnp_c POSITION_INDEPENDENT_CODE ON WINDOWS_EXPORT_ALL_SYMBOLS ON VERSION ${PROJECT_VERSION} SOVERSION 0 ) add_library(c_capnproto::capnp_c ALIAS capnp_c) set_target_properties(capnp_c PROPERTIES EXPORT_NAME capnp_c) target_include_directories(capnp_c PUBLIC $ $ $ ) if(BUILD_SHARED_LIBS) target_compile_definitions(capnp_c PRIVATE CAPNP_C_BUILDING) else() target_compile_definitions(capnp_c PUBLIC CAPNP_C_STATIC) endif() ``` -------------------------------- ### Enable field getters and has_ with $C.fieldgetset Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/usage.org Import the C annotations and use $C.fieldgetset to generate field getters and has_ functions for pointer fields. ```capnp using C = import "/c.capnp"; $C.fieldgetset; struct MyStruct {} ``` -------------------------------- ### Write a Person message with capn_set_root Source: https://github.com/haozeke/c-capnproto/blob/main/docs/source/llms.txt Initializes a Cap'n Proto buffer, constructs a Person struct with text fields and a union, writes it to memory, and frees the buffer. Requires calling capn_set_root before writing. ```c #include "capnp_c.h" #include "addressbook.capnp.h" static capn_text txt(const char *s) { return (capn_text){.len = (int)strlen(s), .str = s, .seg = NULL}; } uint8_t buf[4096]; struct capn c; capn_init_malloc(&c); struct Person p = { .id = 17, .name = txt("Firstname Lastname"), .email = txt("username@domain.com"), }; p.employment_which = Person_employment_school; p.employment.school = txt("of life"); Person_ptr pp = new_Person(capn_root(&c).seg); write_Person(&p, pp); capn_set_root(&c, pp.p); ssize_t sz = capn_write_mem(&c, buf, sizeof buf, 0); capn_free(&c); ``` -------------------------------- ### Generate C code with capnp compile Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/tutorial.org Run from a source checkout after meson compile -C build. Writes addressbook.capnp.c/.h next to the schema. Generated headers #include "c.capnp.h". Link lib/capn.c, lib/capn-malloc.c, lib/capn-stream.c. ```console $ capnp compile -Icompiler -o./build/capnpc-c tests/addressbook.capnp ``` -------------------------------- ### AddressBook schema in Cap'n Proto Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/tutorial.org Schema for the AddressBook sample from tests/addressbook.capnp, same as Cap'n C++. Uses import "/c.capnp" and $C.fieldgetset. Defines Person with union and AddressBook with list of Person. ```capnp using C = import "/c.capnp"; $C.fieldgetset; struct Person { id @0 :UInt32; name @1 :Text; email @2 :Text; phones @3 :List(PhoneNumber); employment :union { unemployed @4 :Void; employer @5 :Text; school @6 :Text; selfEmployed @7 :Void; } } struct AddressBook { people @0 :List(Person); } ``` -------------------------------- ### Write AddressBook with capn_set_root Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/tutorial.org Zero-init optional pointer fields. Call capn_set_root after write_* or the framed bytes are a legal empty message. Uses capn_init_malloc, new_Person_PhoneNumber_list, set_Person_PhoneNumber, write_Person, capn_write_mem with unpacked flag 0, then capn_free. ```c static capn_text txt(const char *s) { return (capn_text){.len = (int)strlen(s), .str = s, .seg = NULL}; } struct capn c; capn_init_malloc(&c); struct capn_segment *cs = capn_root(&c).seg; struct Person p = { .id = 17, .name = txt("Firstname Lastname"), .email = txt("username@domain.com"), }; p.employment_which = Person_employment_school; p.employment.school = txt("of life"); p.phones = new_Person_PhoneNumber_list(cs, 2); struct Person_PhoneNumber pn0 = { .number = txt("123"), .type = Person_PhoneNumber_Type_work, }; set_Person_PhoneNumber(&pn0, p.phones, 0); Person_ptr pp = new_Person(cs); write_Person(&p, pp); capn_set_root(&c, pp.p); uint8_t buf[4096]; ssize_t sz = capn_write_mem(&c, buf, sizeof buf, 0); /* unpacked */ capn_free(&c); ``` -------------------------------- ### Meson fuzz build and smoke test Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Builds the fuzzing harnesses with Meson when -Dfuzz=true is set. The harnesses are build-fuzz/fuzz-mem and build-fuzz/fuzz-fp, and meson test runs fuzz-mem-smoke / fuzz-fp-smoke on Person.bin. The same one-shot run is ./build-fuzz/fuzz-mem fuzz/in/Person.bin. ```sh meson setup build-fuzz -Dfuzz=true -Denable_tests=disabled meson compile -C build-fuzz # harnesses: build-fuzz/fuzz-mem build-fuzz/fuzz-fp meson test -C build-fuzz # fuzz-mem-smoke / fuzz-fp-smoke on Person.bin # same one-shot: ./build-fuzz/fuzz-mem fuzz/in/Person.bin ``` -------------------------------- ### CMakeLists.txt for consumer smoke test using add_subdirectory Source: https://github.com/haozeke/c-capnproto/blob/main/cmake/consumer_smoke/CMakeLists.txt Builds a tiny program against capnp_c via add_subdirectory, the same path FetchContent_MakeAvailable takes. Forces BUILD_TESTING and C_CAPNPROTO_INSTALL to OFF, then links the executable to c_capnproto::capnp_c. ```CMake # Smoke: build a tiny program against capnp_c via add_subdirectory, the # same path FetchContent_MakeAvailable takes. cmake_minimum_required(VERSION 3.16) project(c_capnproto_consumer_smoke LANGUAGES C) set(BUILD_TESTING OFF CACHE BOOL "" FORCE) set(C_CAPNPROTO_INSTALL OFF CACHE BOOL "" FORCE) add_subdirectory("${C_CAPNPROTO_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/c_capnproto-build") add_executable(smoke smoke.c) target_link_libraries(smoke PRIVATE c_capnproto::capnp_c) ``` -------------------------------- ### Compile Cap'n Proto schema with capnp compile Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/index.org Generate C accessors from the addressbook schema. The output is placed in the build directory. ```console $ capnp compile -Icompiler -oc:build tests/addressbook.capnp ``` -------------------------------- ### Use fallback dependency in Meson Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Uses dependency('c-capnproto') with fallback to a local checkout. Sources must be at subprojects/c-capnproto (directory or git submodule) when using fallback without a wrap file. ```meson project('myapp', 'c') dep = dependency('c-capnproto', fallback: ['c-capnproto', 'libcapnp_dep']) executable('app', 'app.c', dependencies: dep) ``` -------------------------------- ### Build capnpc-c executable from sources Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Defines the capnpc-c executable using capnpc_c_sources, which includes compiler/capnpc-c.c, compiler/schema.capnp.c, and compiler/str.c, plus compiler/codecgen.c if it exists. Links against capnp_c and sets private include directories for lib and compiler. ```cmake set(capnpc_c_sources compiler/capnpc-c.c compiler/schema.capnp.c compiler/str.c ) if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/compiler/codecgen.c") list(APPEND capnpc_c_sources compiler/codecgen.c) endif() add_executable(capnpc-c ${capnpc_c_sources}) target_link_libraries(capnpc-c PRIVATE capnp_c) target_include_directories(capnpc-c PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/lib" "${CMAKE_CURRENT_SOURCE_DIR}/compiler" ) ``` -------------------------------- ### Autotools fuzz targets with AFL Source: https://github.com/haozeke/c-capnproto/blob/main/README.md Builds the fuzz harnesses using the autotools make targets. Requires afl-clang and capnp on PATH. make fuzz-mem builds the packed-memory reader, and make fuzz-fp builds the FILE* reader. ```sh make fuzz-mem # packed-memory reader make fuzz-fp # FILE* reader ``` -------------------------------- ### Build and register capn-test with GTest Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Adds a test executable linking against capnp_c and GTest, with include directories and compile definitions, and registers it with CTest. Skips if GTest is not found. ```CMake if(BUILD_TESTING) include(CTest) find_package(GTest QUIET) if(GTest_FOUND) if(TARGET GTest::gtest) set(capn_gtest_lib GTest::gtest) elseif(TARGET GTest::GTest) set(capn_gtest_lib GTest::GTest) else() set(capn_gtest_lib ${GTEST_LIBRARIES}) endif() find_package(Threads REQUIRED) add_executable(capn-test tests/capn-test.cpp tests/capn-stream-test.cpp tests/example-test.cpp tests/fortran-golden-test.cpp tests/interface-test.cpp tests/list-c6-upgrade-test.cpp tests/addressbook.capnp.c compiler/test.capnp.c compiler/schema-test.cpp compiler/schema.capnp.c ) target_include_directories(capn-test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/lib" "${CMAKE_CURRENT_SOURCE_DIR}/tests" "${CMAKE_CURRENT_SOURCE_DIR}/compiler" ) target_compile_definitions(capn-test PRIVATE "FORTRAN_GOLDEN_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures\"" ) target_link_libraries(capn-test PRIVATE capnp_c ${capn_gtest_lib} Threads::Threads ) add_test(NAME capn-test COMMAND capn-test) else() message(STATUS "GTest not found; skipping capn-test (install GTest or use meson)") endif() endif() ``` -------------------------------- ### Canonicalize with capn_canonicalize Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/tutorial.org Produces canonical form: one segment, no far pointers; empty struct B = -1. Returns 0 on success. memcmp equals capnp convert binary:canonical on the AddressBook fixture. ```c struct capn canon; capn_init_malloc(&canon); if (capn_canonicalize(&c, &canon) == 0) { /* one segment, no far pointers; empty struct B = -1 */ } capn_free(&canon); ``` -------------------------------- ### CMake project configuration for c-capnproto Source: https://github.com/haozeke/c-capnproto/blob/main/CMakeLists.txt Configures the c-capnproto CMake build. Requires CMake 3.16+, reads the version from VERSION.txt, sets C99 and C++14 standards, and defines options BUILD_TESTING and C_CAPNPROTO_INSTALL. The package name is c_capnproto to avoid shadowing Cap'n Proto C++. ```cmake cmake_minimum_required(VERSION 3.16) file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt" C_CAPNPROTO_VERSION) string(STRIP "${C_CAPNPROTO_VERSION}" C_CAPNPROTO_VERSION) project(c-capnproto VERSION ${C_CAPNPROTO_VERSION} DESCRIPTION "Cap'n Proto pure-C runtime and capnpc-c plugin" HOMEPAGE_URL "https://github.com/HaoZeke/c-capnproto" LANGUAGES C CXX ) include(GNUInstallDirs) include(CMakePackageConfigHelpers) if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(_c_capnproto_is_toplevel ON) else() set(_c_capnproto_is_toplevel OFF) endif() option(BUILD_TESTING "Build unit tests when an installed GTest is found" ${_c_capnproto_is_toplevel}) option(C_CAPNPROTO_INSTALL "Install targets and package config" ${_c_capnproto_is_toplevel}) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` -------------------------------- ### Write and read a Person message in C Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/index.org Initialize a Cap'n Proto message, write a Person struct, serialize to memory, then read it back. Requires including capnp_c.h and the generated header. Note that capn_set_root is required before writing; otherwise the message is empty. ```c #include "capnp_c.h" #include "addressbook.capnp.h" static capn_text txt(const char *s) { return (capn_text){.len = (int)strlen(s), .str = s, .seg = NULL}; } uint8_t buf[4096]; struct capn c; capn_init_malloc(&c); struct Person p = { .id = 17, .name = txt("Firstname Lastname"), .email = txt("username@domain.com"), }; p.employment_which = Person_employment_school; p.employment.school = txt("of life"); Person_ptr pp = new_Person(capn_root(&c).seg); write_Person(&p, pp); capn_set_root(&c, pp.p); /* required; otherwise the message is empty */ ssize_t sz = capn_write_mem(&c, buf, sizeof buf, 0); capn_free(&c); struct capn rc; capn_init_mem(&rc, buf, sz, 0); struct Person rp; Person_ptr rp_ptr = {.p = capn_getp(capn_root(&rc), 0, 1)}; read_Person(&rp, rp_ptr); /* rp.id == 17, rp.name.str == "Firstname Lastname" */ capn_free(&rc); ``` -------------------------------- ### Run 32-bit alignment smoke test Source: https://github.com/haozeke/c-capnproto/blob/main/MAINTAINING.md Optional host smoke test for 32-bit alignment. Recompiles lib/capn*.c with _Static_assert companions under -m32. Exit 2 means N/A (no ILP32 headers or no -m32). Requires gcc-multilib (Debian/Ubuntu) or lib32-glibc (Arch). ```shell sh scripts/smoke-m32.sh ``` -------------------------------- ### Copy upstream capnp schema files Source: https://github.com/haozeke/c-capnproto/blob/main/compiler/update-notes.md Copy the updated test.capnp, schema.capnp, and c++.capnp from the upstream capnproto repository into the current directory. Assumes the updated repo is at CAPNP_CPP. ```bash $ cp ${CAPNP_CPP}/c++/src/capnp/test.capnp . $ cp ${CAPNP_CPP}/c++/src/capnp/schema.capnp . $ cp ${CAPNP_CPP}/c++/src/capnp/c++.capnp . ``` -------------------------------- ### Check capnpc formatted output Source: https://github.com/haozeke/c-capnproto/blob/main/compiler/update-notes.md Use this command during debugging to inspect the capnpc formatted output. ```bash $ capnp compile -ocapnpc-capnpc compiler/schema.capnp ``` -------------------------------- ### Regenerate schema support with capnp compile Source: https://github.com/haozeke/c-capnproto/blob/main/compiler/update-notes.md Regenerate the schema support after fixing schema.capnp to reference the in-tree c++.capnp. Run this command from the compiler directory. ```bash $ capnp compile -o ./capnpc-c compiler/schema.capnp ``` -------------------------------- ### Canonicalize a message with capn_canonicalize Source: https://github.com/haozeke/c-capnproto/blob/main/docs/orgmode/wire.org Rewrites a message to canonical form: single segment, no far pointers, no holes, preorder layout, trailing zero data and null pointer words truncated. Empty structs remain B=-1. Returns 0 on success; the result is unframed in canon.seglist->data[0..len) or can be written as a 1-segment stream with capn_write_mem(&canon, buf, sz, 0). Must free with capn_free. ```c struct capn canon; capn_init_malloc(&canon); if (capn_canonicalize(&c, &canon) == 0) { /* unframed: canon.seglist->data[0 .. len) */ /* 1-segment stream: capn_write_mem(&canon, buf, sz, 0) */ } capn_free(&canon); ```