=============== LIBRARY RULES =============== From library maintainers: - This is a native F2018 runtime. Do not wrap c-capnproto; only iso_fortran_env and optional iso_c_binding. - Generated handles need a TARGET message. Declare type(capnp_message_t), target :: msg. - Check the integer err argument after every call. Nonzero is failure. - Allocate and set fields in schema declaration order for official capnp encode byte parity. - capnpc-fortran is a capnp compile -o plugin. Plugin binary: build/gfortran_*/app/capnpc-fortran. - Sibling runtimes: HaoZeke/c-capnproto and HaoZeke/capnp-janet. Interop goldens live under interop/. ### Generate and install pkg-config file for capnp-fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Sets up pkg-config variables (prefix, libdir, bindir, includedir) and uses configure_file to generate capnp-fortran.pc from a template, then installs it to the pkgconfig directory. Handles absolute and relative install paths. ```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() configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/capnp-fortran.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/capnp-fortran.pc" @ONLY ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/capnp-fortran.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) ``` -------------------------------- ### Install capnp_fortran and capnpc-fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Installs the library and optionally the plugin, exports targets, installs Fortran module files, and generates and installs CMake package configuration files. ```cmake if(CAPNP_FORTRAN_INSTALL) install(TARGETS capnp_fortran EXPORT capnp_fortranTargets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if(CAPNP_FORTRAN_BUILD_PLUGIN) install(TARGETS capnpc-fortran EXPORT capnp_fortranTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) endif() install(DIRECTORY "${_capnp_fortran_mod_dir}/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/capnp_fortran FILES_MATCHING PATTERN "*.mod" ) install(EXPORT capnp_fortranTargets FILE capnp_fortranTargets.cmake NAMESPACE capnp_fortran:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/capnp_fortran ) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/capnp_fortranConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/capnp_fortranConfig.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/capnp_fortran ) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/capnp_fortranConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/capnp_fortranConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/capnp_fortranConfigVersion.cmake" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/capnp_fortran ) endif() ``` -------------------------------- ### Install and build with pixi Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Commands to install dependencies, build, and run tests using the pixi tool. The toolchain (gfortran, fpm, fypp, capnp) is pinned in pixi.toml. ```console $ pixi install $ pixi run build $ pixi run test ``` -------------------------------- ### Install capnp-fortran with fpm Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/source/llms.txt Clones the repository and builds and tests it using fpm. Run these commands in a terminal to set up the project. ```sh git clone https://github.com/HaoZeke/capnp-fortran.git cd capnp-fortran fpm build && fpm test ``` -------------------------------- ### Build and test with fpm Source: https://github.com/haozeke/capnp-fortran/blob/main/README.md Build and test the repository with fpm. Run these commands from the project root after installing fpm. ```console $ fpm build $ fpm test ``` -------------------------------- ### Install interop pixi environment Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/interop.org Installs the interop pixi environment, which includes Meson, Ninja, cmocka, pkg-config, and C/C++ compilers on top of the default capnproto dependency. ```console $ pixi install -e interop ``` -------------------------------- ### Build and test with pixi Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/index.org Install dependencies and build/test with pixi, using a pinned toolchain. ```console $ pixi install $ pixi run build && pixi run test ``` -------------------------------- ### Wire up a connection and call it Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Fortran program that sets up a socket pair, creates a client and server, and makes an RPC call. Uses capnp_posix and capnp_rpc modules. The code is incomplete in the source; it shows the setup but not the full call sequence. ```fortran program adder_demo use capnp use adder_capnp use capnp_posix use capnp_rpc use rpc_adder_impl implicit none type(rpc_conn_t), target :: cli, srv type(my_adder_t), target :: impl class(rpc_server_t), pointer :: boot type(adder_client_t) :: client type(capnp_message_t), target :: m type(adder_add_params_t) :: params type(adder_add_results_t) :: results integer(int64) :: qid integer :: fda, fdb, err ``` -------------------------------- ### Install and run with pixi Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Uses pixi to install dependencies and run generation, build, and test. pixi.toml pins gfortran, fpm, fypp, and capnproto on linux-64. The 'gen' command regenerates src/*.f90 from .fypp sources; commit changes if templates change. ```console $ pixi install $ pixi run gen # fypp → src/*.f90 (commit if templates change) $ pixi run build $ pixi run test ``` -------------------------------- ### Clone and test capnp-fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CONTRIBUTING.md Clone the repository and run the test suite using either pixi (preferred for the full matrix) or fpm. The pixi commands install dependencies and run tests, while the fpm command runs the tests directly. ```bash git clone https://github.com/HaoZeke/capnp-fortran.git cd capnp-fortran # pixi (preferred for full matrix): pixi install pixi run test # or fpm only: fpm test ``` -------------------------------- ### Clone c-capnproto reference sources Source: https://github.com/haozeke/capnp-fortran/blob/main/interop/README.md Clones the c-capnproto reference implementation into the vendored third_party directory. The sources must end up under third_party/c-capnproto/lib/ for the build; otherwise meson setup succeeds but the golden-master executable is skipped. ```console $ git clone https://github.com/HaoZeke/c-capnproto third_party/c-capnproto ``` -------------------------------- ### Include GNUInstallDirs and CMakePackageConfigHelpers Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Includes the CMake modules for standard installation directories and package configuration helpers. ```cmake include(GNUInstallDirs) include(CMakePackageConfigHelpers) ``` -------------------------------- ### Fortran RPC client call sequence with rpc_bootstrap_send and rpc_call_begin Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Shows the untyped client call sequence for a Cap'n Proto RPC: bootstrap to get a pipeline capability, begin a call, fill parameters, send, wait for the result, and read the result content. Schema-generated client procedures wrap this exact sequence with typed handles and should be used when a schema is available. ```fortran call rpc_bootstrap_send(conn, cap, err) ! pipeline cap, usable now call rpc_call_begin(conn, target, iface_id, method_id, m, params, qid, err) ! ... fill params content ... call rpc_call_send(conn, m, err) call rpc_wait(conn, qid, err) ! pumps until qid returns call rpc_result_content(conn, qid, content, err) ``` -------------------------------- ### Build standalone with CMake Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Configures and builds capnp-fortran standalone with CMake. The library is produced as build-cmake/libcapnp_fortran.a (or .so if CAPNP_FORTRAN_BUILD_SHARED=ON) and the plugin as build-cmake/capnpc-fortran when CAPNP_FORTRAN_BUILD_PLUGIN=ON. An installable package config (find_package(capnp_fortran)) is generated when CAPNP_FORTRAN_INSTALL is ON (default for top-level builds). ```console $ cmake -S . -B build-cmake -DCMAKE_BUILD_TYPE=Release $ cmake --build build-cmake # library: build-cmake/libcapnp_fortran.a (or .so if CAPNP_FORTRAN_BUILD_SHARED=ON) # plugin: build-cmake/capnpc-fortran when CAPNP_FORTRAN_BUILD_PLUGIN=ON ``` -------------------------------- ### Typed RPC adder demo with capnp-fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Complete Fortran example of a typed RPC client/server using capnp-fortran. It sets up a socketpair, initializes both connections, sends a bootstrap request, and performs a pipelined adder call. The output is 'sum = 42'. Note that rpc_pump_once is meant to be called in a loop in a real server event loop. ```fortran call px_socketpair(fda, fdb, err) boot => impl call rpc_conn_init(srv, fdb, boot) boot => null() call rpc_conn_init(cli, fda, boot) ! Bootstrap resolves the typed client; the pipeline cap works ! before the bootstrap call even returns. call rpc_bootstrap_send(cli, client%cap, err) call rpc_pump_once(srv, err) call adder_add_begin(cli, client, m, params, qid, err) call adder_add_params_a_set(params, 19_int64, err) call adder_add_params_b_set(params, 23_int64, err) call rpc_call_send(cli, m, err) call rpc_pump_once(srv, err) call adder_add_wait(cli, qid, results, err) print '(a,i0)', 'sum = ', adder_add_results_sum_get(results) ! 42 call rpc_conn_close(cli) call rpc_conn_close(srv) end program adder_demo ``` -------------------------------- ### capnp_list_getp Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Gets an element of a pointer list. ```APIDOC ## capnp_list_getp ### Description Gets an element of a pointer list. ### Signature `(l, i, err) result(q)` ### Parameters - **l** - List pointer. - **i** - Element index. - **err** - Error output. ### Returns - **q** - The pointer at the specified index. ``` -------------------------------- ### capnp_list_get_struct Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Gets a composite element, upgrading primitive lists to struct views. ```APIDOC ## capnp_list_get_struct ### Description Gets a composite element, upgrading primitive lists to struct views. ### Signature `(l, i, err) result(q)` ### Parameters - **l** - List pointer. - **i** - Element index. - **err** - Error output. ### Returns - **q** - The struct pointer at the specified index. ``` -------------------------------- ### Build and test with fpm Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/index.org Clone the repository and build/test with fpm and gfortran. ```console $ git clone https://github.com/HaoZeke/capnp-fortran.git $ cd capnp-fortran $ fpm build && fpm test ``` -------------------------------- ### Build documentation with pixi Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Builds the documentation using the 'docs' pixi environment. The HTML output is placed under docs/build/. ```console $ pixi run -e docs docs # HTML under docs/build/ ``` -------------------------------- ### First message tutorial in Fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/source/llms.txt Demonstrates creating a Cap'n Proto message, serializing it to bytes, and deserializing it back. Uses the addressbook schema. Declare `type(capnp_message_t), target :: msg` and check `err` after every call. ```fortran program tutorial use capnp use addressbook_capnp implicit none type(capnp_message_t), target :: msg, rmsg type(address_book_t) :: book type(person_t) :: alice type(capnp_ptr_t) :: people integer(int8), allocatable :: bytes(:) character(len=:), allocatable :: name integer :: err call capnp_message_init_builder(msg, err) book = address_book_new_root(msg, err) people = address_book_people_init(book, 1_int64, err) alice%p = capnp_list_get_struct(people, 0, err) call person_id_set(alice, 123_int64, err) call person_name_set(alice, 'Alice', err) call capnp_serialize_bytes(msg, bytes, err) call capnp_deserialize_bytes(bytes, rmsg, err) book = address_book_read_root(rmsg, err) people = address_book_people_get(book, err) alice%p = capnp_list_get_struct(people, 0, err) call person_name_get(alice, name, err) print '(a)', name end program tutorial ``` -------------------------------- ### C ABI (`capnp_cabi`) Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md C ABI entry points mirroring c-capnproto's surface for interop. ```APIDOC ## C ABI (`cabi_*`) ### Description `bind(c)` entry points named `cabi_*` mirror c-capnproto's `capn_*` surface over pooled integer handles. They cover builder lifecycle, struct/list allocation (composite and primitive), pointer wiring, scalar and text/data accessors, union discriminants, serialize/deserialize (flat and packed), and canonicalization. ### Usage See `interop/README.md` and the declarations at the top of `interop/golden_master.c` for the full list of entry points. ``` -------------------------------- ### Run interop suite with meson Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Clones the C Cap'n Proto repository and runs the interop test suite using meson. Requires the 'interop' pixi environment with meson, ninja, a C/C++ toolchain, and cmocka. When libcapnp-rpc is available, meson also builds the Fortran RPC client against a C++ EzRpcServer (schema adder.capnp). ```console $ git clone --depth 1 https://github.com/HaoZeke/c-capnproto third_party/c-capnproto $ pixi run -e interop meson setup build-interop . $ pixi run -e interop meson compile -C build-interop $ pixi run -e interop meson test -C build-interop -v ``` -------------------------------- ### Build and run interop tests with Meson Source: https://github.com/haozeke/capnp-fortran/blob/main/interop/README.md Build and run the C-interop golden-master tests with Meson in the interop pixi environment. meson test -C build-interop -v prints per-assertion cmocka output. ```console $ pixi run -e interop meson setup build-interop . $ pixi run -e interop meson compile -C build-interop $ pixi run -e interop meson test -C build-interop ``` -------------------------------- ### rpc_call_begin Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Begins a call by filling the parameters content and then sending it. ```APIDOC ## rpc_call_begin ### Description Begins a call by filling the parameters content and then sending it. ### Signature `(conn, target, interface_id, method_id, m, params, qid, err)` ### Parameters - **conn** - The connection. - **target** - Target capability. - **interface_id** - Interface identifier. - **method_id** - Method identifier. - **m** - Message object. - **params** - Parameters content to fill. - **qid** - Question ID output. - **err** - Error output. ``` -------------------------------- ### Run rpc_interop_cpp test with Meson Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/interop.org Use this command to run the rpc_interop_cpp test via Meson in the interop environment. It exercises the vat against an independent Cap'n Proto RPC implementation, covering bootstrap plus pipelined and settled add() calls. Level 1 embargo echo is not covered by this test. ```console $ pixi run -e interop meson test -C build-interop rpc_interop_cpp -v ``` -------------------------------- ### Define build options for capnp_fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Defines three options: CAPNP_FORTRAN_BUILD_PLUGIN (defaults to top-level), CAPNP_FORTRAN_BUILD_SHARED (default OFF), and CAPNP_FORTRAN_INSTALL (defaults to top-level). The prefix CAPNP_FORTRAN_* avoids conflicts with Cap'n Proto C++'s CAPNP_* variables. ```cmake option(CAPNP_FORTRAN_BUILD_PLUGIN "Build the capnpc-fortran schema plugin" ${_capnp_fortran_is_toplevel}) option(CAPNP_FORTRAN_BUILD_SHARED "Build shared library instead of static" OFF) option(CAPNP_FORTRAN_INSTALL "Install targets and package config" ${_capnp_fortran_is_toplevel}) ``` -------------------------------- ### rpc_conn_init Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Initializes an RPC connection with a file descriptor and an optional bootstrap server capability. ```APIDOC ## rpc_conn_init ### Description Initializes an RPC connection with a file descriptor and an optional bootstrap server capability. ### Signature `(conn, fd, bootstrap)` ### Parameters - **conn** - The connection object to initialize. - **fd** - File descriptor for the connection. - **bootstrap** - A `class(rpc_server_t), pointer` (may be null). ``` -------------------------------- ### Integrate with CMake FetchContent Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Fetches capnp-fortran via FetchContent and links it to your target. The CMake project name is 'capnp_fortran' and the link target is 'capnp_fortran::capnp_fortran'. The plugin is OFF by default under FetchContent; set CAPNP_FORTRAN_BUILD_PLUGIN to ON to enable it. ```cmake include(FetchContent) FetchContent_Declare( capnp_fortran GIT_REPOSITORY https://github.com/HaoZeke/capnp-fortran.git GIT_TAG v0.1.1 ) # Optional: set(CAPNP_FORTRAN_BUILD_PLUGIN ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(capnp_fortran) target_link_libraries(myapp PRIVATE capnp_fortran::capnp_fortran) ``` -------------------------------- ### Build capnpc-fortran plugin Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Creates the capnpc-fortran executable from app sources, links it to capnp_fortran, sets its module directory, and applies the same Fortran compile options when the compiler matches GNU, LLVMFlang, or Flang. ```cmake if(CAPNP_FORTRAN_BUILD_PLUGIN) add_executable(capnpc-fortran app/capnpc_emit.f90 app/main.f90 ) target_link_libraries(capnpc-fortran PRIVATE capnp_fortran::capnp_fortran) set_target_properties(capnpc-fortran PROPERTIES Fortran_MODULE_DIRECTORY "${_capnp_fortran_mod_dir}" ) if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU|LLVMFlang|Flang") target_compile_options(capnpc-fortran PRIVATE -std=f2018 -ffree-line-length-none) endif() endif() ``` -------------------------------- ### Run coverage with pixi Source: https://github.com/haozeke/capnp-fortran/blob/main/README.md Run the coverage environment with pixi. Generates a coverage report in `coverage-report/`. ```console $ pixi run -e coverage coverage # gfortran --coverage → coverage-report/ ``` -------------------------------- ### Build and test with fpm Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Clones the repository and runs fpm build and test. Requires a Fortran 2018 compiler (gfortran 13+), fpm, and fypp. The package name in fpm.toml is 'capnp' and the executable is 'capnpc-fortran'. ```console $ git clone https://github.com/HaoZeke/capnp-fortran.git $ cd capnp-fortran $ fpm build $ fpm test ``` -------------------------------- ### Implement the RPC server Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Fortran module extending adder_server_t and implementing the add procedure. The generated dispatch procedure on adder_server_t decodes interface and method ordinals and routes to add; application code never touches ordinals directly. ```fortran module rpc_adder_impl use capnp use adder_capnp implicit none private public :: my_adder_t type, extends(adder_server_t) :: my_adder_t contains procedure :: add => my_add end type my_adder_t contains subroutine my_add(self, params, results, err) class(my_adder_t), intent(inout) :: self type(adder_add_params_t), intent(in) :: params type(adder_add_results_t), intent(in) :: results integer, intent(out) :: err err = CAPNP_OK call adder_add_results_sum_set(results, & adder_add_params_a_get(params) + & adder_add_params_b_get(params), err) end subroutine my_add end module rpc_adder_impl ``` -------------------------------- ### Add public include directories for capnp_fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Exposes the build-time module directory and install-time include directory as PUBLIC include paths. ```cmake target_include_directories(capnp_fortran PUBLIC $ $ ) ``` -------------------------------- ### Module dependency graph Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/architecture.org Shows the dependency hierarchy of capnp-fortran modules, from low-level layers (capnp_kinds, capnp_endian, capnp_pointer, capnp_arena, capnp_message) up to the umbrella module capnp, with separate RPC and C ABI shim modules. ```text capnp_kinds -> capnp_endian -> capnp_pointer -> capnp_arena -> capnp_message | +---------------------+---------------------+ | | | capnp_serialize capnp_union capnp_canonical | capnp_packed | capnp_stream capnp (umbrella: everything above) capnp_posix -> capnp_rpc_transport -+ rpc_capnp (generated) --------------+-> capnp_rpc capnp_cabi (bind(c) shim over capnp) ``` -------------------------------- ### Create capnp_fortran library target Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Builds capnp_fortran as SHARED or STATIC based on CAPNP_FORTRAN_BUILD_SHARED, and creates an ALIAS target capnp_fortran::capnp_fortran. ```cmake if(CAPNP_FORTRAN_BUILD_SHARED) add_library(capnp_fortran SHARED ${CAPNP_FORTRAN_SOURCES}) else() add_library(capnp_fortran STATIC ${CAPNP_FORTRAN_SOURCES}) endif() add_library(capnp_fortran::capnp_fortran ALIAS capnp_fortran) ``` -------------------------------- ### rpc_make_cap_ptr Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Creates a capability pointer for content slots. ```APIDOC ## rpc_make_cap_ptr ### Description Creates a capability pointer for content slots. ### Signature `(m, idx) result(p)` ### Parameters - **m** - Message. - **idx** - Index. - **p** - Capability pointer. ``` -------------------------------- ### Add capnp-fortran via CMake FetchContent Source: https://github.com/haozeke/capnp-fortran/blob/main/README.md Use CMake's FetchContent to add capnp-fortran. The CMake target is `capnp_fortran::capnp_fortran`; the Fortran module API is still `use capnp`. ```cmake include(FetchContent) FetchContent_Declare( capnp_fortran GIT_REPOSITORY https://github.com/HaoZeke/capnp-fortran.git GIT_TAG v0.3.1 ) FetchContent_MakeAvailable(capnp_fortran) target_link_libraries(myapp PRIVATE capnp_fortran::capnp_fortran) ``` -------------------------------- ### Write and read a message Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Fortran program that builds a message, serializes it, deserializes the bytes, and reads a field back. Demonstrates the use of capnp_message_t, address_book_t, person_t, and generated setters/getters. Note that messages carry target and every fallible call returns err; production code should check err after each call. ```fortran program tutorial use capnp use addressbook_capnp implicit none type(capnp_message_t), target :: msg, rmsg type(address_book_t) :: book type(person_t) :: alice type(capnp_ptr_t) :: people integer(int8), allocatable :: bytes(:) character(len=:), allocatable :: name integer :: err call capnp_message_init_builder(msg, err) book = address_book_new_root(msg, err) people = address_book_people_init(book, 1_int64, err) alice%p = capnp_list_get_struct(people, 0, err) call person_id_set(alice, 123_int64, err) call person_name_set(alice, 'Alice', err) call capnp_serialize_bytes(msg, bytes, err) call capnp_deserialize_bytes(bytes, rmsg, err) book = address_book_read_root(rmsg, err) people = address_book_people_get(book, err) alice%p = capnp_list_get_struct(people, 0, err) call person_name_get(alice, name, err) print '(a)', name ! Alice end program tutorial ``` -------------------------------- ### rpc_bootstrap_send Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Sends a bootstrap request and returns a pipeline capability that can be used immediately. ```APIDOC ## rpc_bootstrap_send ### Description Sends a bootstrap request and returns a pipeline capability that can be used immediately. ### Signature `(conn, cap, err)` ### Parameters - **conn** - The connection. - **cap** - The returned pipeline capability. - **err** - Error output. ``` -------------------------------- ### capnp_message_init_builder Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Initializes a fresh message builder, optionally reserving a number of words. The message variable must carry the target attribute. ```APIDOC ## capnp_message_init_builder ### Description Initializes a fresh message builder, optionally reserving a number of words. The message variable must carry the target attribute. ### Procedure `capnp_message_init_builder` ### Signature `(msg, err [, first_words])` ### Parameters - **msg** - The message variable (must have the `target` attribute). - **err** - Error code output; `CAPNP_OK` (0) means success. - **first_words** (optional) - Number of words to reserve initially. ### Notes Reserves the zeroed root word. ``` -------------------------------- ### Add capnp-fortran as an fpm dependency Source: https://github.com/haozeke/capnp-fortran/blob/main/README.md Add this dependency block to your fpm.toml to depend on the capnp-fortran library. Then use `use capnp` in your Fortran code. ```toml [dependencies] capnp = { git = "https://github.com/HaoZeke/capnp-fortran", tag = "v0.3.1" } ``` -------------------------------- ### Compile schema with capnpc-fortran plugin Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/codegen.org Invokes the capnpc-fortran plugin through capnp compile. The plugin writes one Fortran module per schema file, e.g., addressbook_capnp.f90, next to the schema or in the working directory. ```console $ capnp compile -o build/gfortran_*/app/capnpc-fortran schema/addressbook.capnp ``` -------------------------------- ### Define capnp_fortran source list Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Lists all Fortran source files for the capnp_fortran library. The comment notes the order is irrelevant for modern CMake+gfortran. ```cmake set(CAPNP_FORTRAN_SOURCES src/capnp_kinds.f90 src/capnp_endian.f90 src/capnp_pointer.f90 src/capnp_arena.f90 src/capnp_message.f90 src/capnp_packed.f90 src/capnp_union.f90 src/capnp_serialize.f90 src/capnp_stream.f90 src/capnp_canonical.f90 src/capnp.f90 src/capnp_cabi.f90 src/capnp_posix.f90 src/capnp_rpc_transport.f90 src/rpc_capnp.f90 src/rpc_twoparty_capnp.f90 src/rpc_threeparty_capnp.f90 src/capnp_rpc.f90 src/capnp_schema.f90 src/capnp_dynamic.f90 src/stream_capnp.f90 ) ``` -------------------------------- ### Adder interface schema Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Cap'n Proto interface declaration for Adder with an add method that takes two Int64 and returns a sum. ```capnp interface Adder @0xea01e10cbc414411 { add @0 (a :Int64, b :Int64) -> (sum :Int64); } ``` -------------------------------- ### Set capnp_fortran target properties Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Configures output name, version, Fortran module directory, position-independent code, and export name for the capnp_fortran target. ```cmake set(_capnp_fortran_mod_dir "${CMAKE_CURRENT_BINARY_DIR}/fortran_mods") set_target_properties(capnp_fortran PROPERTIES OUTPUT_NAME capnp_fortran VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} Fortran_MODULE_DIRECTORY "${_capnp_fortran_mod_dir}" POSITION_INDEPENDENT_CODE ON EXPORT_NAME capnp_fortran ) ``` -------------------------------- ### Use capnp module in Fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/install.org Adds a dependency on the capnp-fortran repository (or the fpm registry package 'capnp' once published) and uses the 'capnp' module. Generated schema modules are ordinary Fortran sources compiled alongside your program after running capnpc-fortran. ```fortran use capnp ``` -------------------------------- ### Set Fortran compile options for capnp_fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Applies -std=f2018 and -ffree-line-length-none for GNU, LLVMFlang, and Flang compilers. The comment notes these are free-form F2018 options accepted by gfortran and typical flang. ```cmake if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU|LLVMFlang|Flang") target_compile_options(capnp_fortran PRIVATE -std=f2018 -ffree-line-length-none) endif() ``` -------------------------------- ### capnp_unpacker_t + capnp_unpack_push Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Incremental unpacking using an unpacker object. Chunks may split anywhere. ```APIDOC ## capnp_unpacker_t + capnp_unpack_push ### Description Incremental unpacking using an unpacker object. Chunks may split anywhere. ### Method Subroutine ### Signature `(u, chunk, out, outn, err)` ### Parameters - **u** - Unpacker object. - **chunk** - Input chunk. - **out** - Output buffer. - **outn** - Output length. - **err** - Error output. ``` -------------------------------- ### AddressBook schema definition Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/tutorial.org Cap'n Proto schema declaring Person and AddressBook structs. Person has id, name, email, and phones fields; AddressBook has a list of people. ```capnp struct Person { id @0 :UInt32; name @1 :Text; email @2 :Text; phones @3 :List(PhoneNumber); # ... } struct AddressBook { people @0 :List(Person); } ``` -------------------------------- ### Detect top-level build for capnp_fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Sets a variable indicating whether the project is the top-level CMake source directory. This is used to default options appropriately when consumed via FetchContent or add_subdirectory. ```cmake if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(_capnp_fortran_is_toplevel ON) else() set(_capnp_fortran_is_toplevel OFF) endif() ``` -------------------------------- ### rpc_wait Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Pumps the connection until the specified question returns. ```APIDOC ## rpc_wait ### Description Pumps the connection until the specified question returns. ### Signature `(conn, qid, err)` ### Parameters - **conn** - The connection. - **qid** - Question ID. - **err** - Error output. ``` -------------------------------- ### rpc_pump_once Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Handles exactly one incoming message; servers loop on this. ```APIDOC ## rpc_pump_once ### Description Handles exactly one incoming message; servers loop on this. ### Signature `(conn, err)` ### Parameters - **conn** - The connection. - **err** - Error output. ``` -------------------------------- ### capnp_getp Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Reads a pointer slot, downgrading composite lists when asked for pointers. ```APIDOC ## capnp_getp ### Description Reads a pointer slot, downgrading composite lists when asked for pointers. ### Signature `(p, slot, err) result(q)` ### Parameters - **p** - Source pointer. - **slot** - Slot index. - **err** - Error output. ### Returns - **q** - The pointer read from the slot. ``` -------------------------------- ### CMake project definition for capnp_fortran Source: https://github.com/haozeke/capnp-fortran/blob/main/CMakeLists.txt Defines the capnp_fortran project with version 0.3.1 and description. Sets the minimum CMake version to 3.16 and specifies Fortran as the only language. ```cmake cmake_minimum_required(VERSION 3.16) project( capnp_fortran VERSION 0.3.1 DESCRIPTION "Native modern-Fortran Cap'n Proto (wire, packed, canonical, RPC, capnpc-fortran)" LANGUAGES Fortran ) ``` -------------------------------- ### capnp_new_composite_list Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Creates a new composite list pointer, writing the tag word. ```APIDOC ## capnp_new_composite_list ### Description Creates a new composite list pointer, writing the tag word. ### Signature `(msg, count, dwords, pwords, err) result(p)` ### Parameters - **msg** - Input message handle. - **count** - Number of elements. - **dwords** - Number of data words per element. - **pwords** - Number of pointer words per element. - **err** - Error output. ### Returns - **p** - The new composite list pointer. ``` -------------------------------- ### capnp_packer_t + capnp_pack_push / capnp_pack_finish Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Incremental packing using a packer object. Produces byte-identical output to capnp_pack. ```APIDOC ## capnp_packer_t + capnp_pack_push / capnp_pack_finish ### Description Incremental packing using a packer object. Produces byte-identical output to capnp_pack. ### Method Subroutine ### Signature `(pk, chunk, out, outn, err)` for push; `(pk, out, outn, err)` for finish ### Parameters - **pk** - Packer object. - **chunk** - Input chunk (for push). - **out** - Output buffer. - **outn** - Output length. - **err** - Error output. ``` -------------------------------- ### capnp_setp Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Writes a pointer slot, handling same-segment relative, cross-segment far, and cross-message deep copy. ```APIDOC ## capnp_setp ### Description Writes a pointer slot, handling same-segment relative, cross-segment far, and cross-message deep copy. ### Signature `(p, slot, q, err)` ### Parameters - **p** - Destination pointer. - **slot** - Slot index. - **q** - Pointer to write. - **err** - Error output. ``` -------------------------------- ### capnp_new_list Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Creates a new list pointer (primitive or pointer list) in a message. ```APIDOC ## capnp_new_list ### Description Creates a new list pointer (primitive or pointer list) in a message. ### Signature `(msg, esize, count, err) result(p)` ### Parameters - **msg** - Input message handle. - **esize** - Element size. - **count** - Number of elements. - **err** - Error output. ### Returns - **p** - The new list pointer. ``` -------------------------------- ### rpc_call_send Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Sends a previously prepared call message. ```APIDOC ## rpc_call_send ### Description Sends a previously prepared call message. ### Signature `(conn, m, err)` ### Parameters - **conn** - The connection. - **m** - The message to send. - **err** - Error output. ``` -------------------------------- ### Wire-level helpers Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Wire-level helpers for endianness-independent scalar composition and pointer word manipulation. ```APIDOC ## `cp_get_*` / `cp_put_*` ### Description Compose little-endian scalars from individual bytes, endianness-independent by construction. Used for wire-level scalar access. ### Usage These are low-level helpers for reading and writing scalar values from byte buffers. ``` ```APIDOC ## `cp_f32_bits` / `cp_bits_f32` and f64 pair ### Description Convert between reals and their bit patterns for wire-level encoding. ### Usage Used for converting floating-point values to and from their bit representations. ``` ```APIDOC ## `wp_*` ### Description Build and field raw pointer words. These exist for tests and the code generator; generated code does not need them. ### Usage Low-level pointer word manipulation for internal use. ``` -------------------------------- ### capnp_pack / capnp_unpack Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Packs and unpacks entire buffers using the packed codec. ```APIDOC ## capnp_pack / capnp_unpack ### Description Packs and unpacks entire buffers using the packed codec. ### Method Subroutine ### Signature `(in, out, err)` ### Parameters - **in** - Input buffer. - **out** - Output buffer. - **err** - Error output. ``` -------------------------------- ### capnp_set_root Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Writes the root pointer of a message. ```APIDOC ## capnp_set_root ### Description Writes the root pointer of a message. ### Signature `(msg, p, err)` ### Parameters - **msg** - Input message handle. - **p** - Pointer to set as root. - **err** - Error output. ``` -------------------------------- ### rpc_release_send Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Releases a capability. ```APIDOC ## rpc_release_send ### Description Releases a capability. ### Signature `(conn, cap, err)` ### Parameters - **conn** - The connection. - **cap** - Capability to release. - **err** - Error output. ``` -------------------------------- ### POSIX Socket Interfaces Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md POSIX socket interfaces for the RPC layer. ```APIDOC ## `capnp_posix` ### Description Provides the socket surface (socketpair, TCP listen/accept/connect, poll) as pure `iso_c_binding` interfaces into libc. ### Usage Use these interfaces for network operations in the RPC layer. ``` -------------------------------- ### rpc_pipeline_cap Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Creates a pipeline capability for promise pipelining into unreturned results. ```APIDOC ## rpc_pipeline_cap ### Description Creates a pipeline capability for promise pipelining into unreturned results. ### Signature `(qid, field_indices) result(cap)` ### Parameters - **qid** - Question ID. - **field_indices** - Field indices for the pipeline. - **cap** - Result capability. ``` -------------------------------- ### capnp_disown Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Creates an orphan by zeroing the slot and returning the object; re-link with capnp_setp. ```APIDOC ## capnp_disown ### Description Creates an orphan by zeroing the slot and returning the object; re-link with capnp_setp. ### Signature `(p, i, err) result(q)` ### Parameters - **p** - Source pointer. - **i** - Slot index. - **err** - Error output. ### Returns - **q** - The orphaned object pointer. ``` -------------------------------- ### capnp_write_file / capnp_read_file Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Writes and reads raw byte arrays to and from files. ```APIDOC ## capnp_write_file / capnp_read_file ### Description Writes and reads raw byte arrays to and from files. ### Method Subroutine ### Signature `(path, bytes, err)` ### Parameters - **path** - File path. - **bytes** - Byte array (input for write, output for read). - **err** - Error output. ``` -------------------------------- ### capnp_copy Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Recursively deep copies a pointer into another message. ```APIDOC ## capnp_copy ### Description Recursively deep copies a pointer into another message. ### Signature `(dstmsg, src, err) result(q)` ### Parameters - **dstmsg** - Destination message handle. - **src** - Source pointer. - **err** - Error output. ### Returns - **q** - The copied pointer in the destination message. ``` -------------------------------- ### capnp_canonicalize Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Canonical form helper for producing canonical serialization of a Cap'n Proto message. ```APIDOC ## `capnp_canonicalize` ### Description Produces the canonical single-segment serialization of a message, with preorder layout, trailing-zero truncation of data sections, and uniform composite-element trimming. Byte-compatible with `capnp convert binary:canonical`. ### Signature `capnp_canonicalize (msg, bytes, err)` ### Parameters - **msg** - Input message to canonicalize. - **bytes** - Output buffer for the canonical bytes. - **err** - Error status output. ### Returns None explicitly documented. ``` -------------------------------- ### Pointer word bit layouts Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/architecture.org Shows the bit layout for the four pointer kinds (struct, list, far, cap) as encoded by capnp_pointer. Bit 0 is the least significant bit. ```text struct: kind(0-1)=0 | offset(2-31, signed) | dwords(32-47) | pwords(48-63) list: kind(0-1)=1 | offset(2-31, signed) | esize(32-34) | count(35-63) far: kind(0-1)=2 | two(2) | pad word offset(3-31) | segment id(32-63) cap: kind(0-1)=3 | zero(2-31) | capability index(32-63) ``` -------------------------------- ### capnp_root Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Resolves the root pointer of a message. ```APIDOC ## capnp_root ### Description Resolves the root pointer of a message. ### Signature `(msg, err) result(p)` ### Parameters - **msg** - Input message handle. - **err** - Error output. ### Returns - **p** - The root pointer. ``` -------------------------------- ### rpc_result_cap Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Settles a result capability into an import. ```APIDOC ## rpc_result_cap ### Description Settles a result capability into an import. ### Signature `(conn, qid, field_indices, cap, err)` ### Parameters - **conn** - The connection. - **qid** - Question ID. - **field_indices** - Field indices. - **cap** - Result capability. - **err** - Error output. ``` -------------------------------- ### capnp_new_struct Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Creates a new struct pointer in a message. ```APIDOC ## capnp_new_struct ### Description Creates a new struct pointer in a message. ### Signature `(msg, dwords, pwords, err) result(p)` ### Parameters - **msg** - Input message handle. - **dwords** - Number of data words. - **pwords** - Number of pointer words. - **err** - Error output. ### Returns - **p** - The new struct pointer. ``` -------------------------------- ### capnp_write_message_packed / capnp_read_message_packed Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/reference.md Writes and reads framed messages to and from files using packed framing. ```APIDOC ## capnp_write_message_packed / capnp_read_message_packed ### Description Writes and reads framed messages to and from files using packed framing. ### Method Subroutine ### Signature `(path, msg, err)` ### Parameters - **path** - File path. - **msg** - Message (input for write, output for read). - **err** - Error output. ``` -------------------------------- ### rpc_conn_close Source: https://github.com/haozeke/capnp-fortran/blob/main/docs/orgmode/rpc.org Half-closes the connection and frees associated tables. ```APIDOC ## rpc_conn_close ### Description Half-closes the connection and frees associated tables. ### Signature `(conn)` ### Parameters - **conn** - The connection to close. ```