inclyc / c2wit

Convert C declarations to .wit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C2Wit

A language bindings generator for C

About

This is a tool that generate C declarations to .wit. For existing C projects, you can use this project to generate declarations to corresponding wit file.

Note that we provide a standalone clang-tool c2wit and a clang plugin libC2WitPlugin.so. They share the same code about wit-codegen, but expose different user interfaces, for your choice.

Installation

OCI-container (namely, docker) images are provided on github packages. This is a standalone tool wrapper, you can mount local filesystem into the container and run our tool.

Note: the images is fairly large due to LLVM/Clang dependency (1GB+), if you can not stand this, you can build from source.

Usage

Declaration having attribute __attribute__((annotate("wit-export"))) will be exported.

An example translation unit:

/* test.c */
struct
__attribute__((annotate("wit-export")))
Floats {
    float F32;
    double F64;
};
(a) Use a clang plugin

Invoke your system clang and load the plugin

clang -fsyntax-only -fplugin=build/C2Wit.so -Xclang -plugin -Xclang c2wit test.c

And this plugin converts "Floats" to a .wit record.

record Floats {
F32: f32,
F64: f64,
}
(b) Use the standalone tool

Here we provide a standalone executable that could be invoked directly.

c2wit test.c

Based on libTooling, you may specify a compilation database, used to find header files & definitions the translation unit.

Features

Record renaming

Annotate the record with wit-define:

__attribute__((annotate("wit-define", <name>)))

gives this record a name, types desugared (i.e. no typedefs, typeofs) are considered the same. This is useful to mark a struct a "string", because we do not have native strings in C/C++.

struct
__attribute__((annotate("wit-define", "string")))
foo {
    int length;
    char *data;
};


typedef struct foo sugared_foo;

struct
__attribute__((annotate("wit-export")))
bar {
    int a;

    struct foo b;

    sugared_foo c;
};
record bar {
a: i32,
b: string,
c: string,
}

Developer Guide

See docs/developer-guide.

About

Convert C declarations to .wit


Languages

Language:C++ 62.3%Language:C 18.2%Language:Nix 8.0%Language:Meson 6.3%Language:Python 5.2%