Skip to content

Generate Iox2NativeMethods constants and struct sizes from the cbindgen C header #21

Open

Description

@patdhlk

Background

src/Iceoryx2/Native/Iox2NativeMethods.cs manually mirrors C constants from the cbindgen-generated iceoryx2.h (IOX2_OK, IOX2_NODE_NAME_LENGTH, IOX2_SERVICE_NAME_LENGTH, IOX2_SERVICE_ID_LENGTH, …) and each interop struct hard-codes its [StructLayout(Size = N)]. Both must be updated by hand whenever the FFI bumps a value, and IOX2_SERVICE_ID_LENGTH going stale (32 → 64) is exactly what motivated the Node.List() marshal-offset bugfix in #12 / #20.

PR #20 added tests/Ffi/NativeStructSizesTests as a tripwire so future drift is at least loud, but the underlying duplication is unsolved.

Goal

Generate Iox2NativeMethods.Generated.cs (constants + struct sizes) directly from the cbindgen-emitted C header so the binding cannot drift from the FFI.

Options (ranked by realism)

  1. ClangSharp / CppSharp source generator. Pipeline: cargo build → cbindgen → iceoryx2.h → ClangSharp → Iox2NativeMethods.Generated.cs. Proper fix. Adds an LLVM/libclang build dependency and a generator project.
  2. cbindgen sidecar + tiny consumer. Have the FFI build also emit a .json of constants + struct sizes that a small C# T4 / source generator consumes. Lower-weight than ClangSharp but still introduces a new tool.
  3. Status quo + tripwires. Today: manual const mirror plus NativeStructSizesTests to catch drift. Cheap, but only catches what we explicitly bookkeep — adding a new constant means the maintainer must remember to also add a tripwire row.

Suggested next step

Pick option (1) or (2). I'd lean toward (1) — once the generator project exists, every drop-in cbindgen update gives a fresh .Generated.cs for free. The implementation effort is mostly the build glue (running the generator at restore time, deciding whether the generated file is committed or built).

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions