edubart / nelua-decl

C binding generator for Nelua using GCC Lua plugin.
46 stars 10 forks source link
binder c gcc lua nelua

C binding generator for Nelua using GCC Lua plugin.

This tool assists creating C bindings for any C library for Nelua in a few steps, also enables the possibility to customize each library bindings via Lua scripts. Requires GCC to run.

Note: You need GCC plugin mechanism to use this (only available in Linux systems).

Bindings example

The following cross-platform libraries bindings are ready and generated as example:

How to generate bindings

Make sure you are using Lua 5.3+, GCC 9+ and have GCC plugin installed.

First clone and compile the gcc-lua plugin and compile it:

git clone --recurse-submodules https://github.com/edubart/nelua-decl.git
make -C gcc-lua

Some libraries such as SDL2, GLFW must be installed on your system.

To generate bindings for a library simply do make generate in its folder. For example, the following will generate libs/sdl2/sdl2.nelua and then run a test to check if it's working:

cd libs/sdl2
make generate
make test

Some single header libraries you must be downloaded with make download, for example:

cd libs/minilua
make download
make generate
make test

How to generate bindings for a new library

Suppose you want generate bindings for mylib, create a new folder mylib in libs/, then you must create the following files:

For a quick start, see the Makefile, .lua and .c files of the current bundled libraries as an example.

How it works

The bindings are generated using the following command:

gcc -S libs/lua/lua.c \
    -fplugin=./gcc-lua/gcc/gcclua.so \
    -fplugin-arg-gcclua-script=libs/lua/lua.lua \
    > libs/lua/lua.nelua

Command explanation:

Caveats

Some limitations:

Usually to create bindings for a new library requires little manual work for C libraries that are binding friendly to other languages. A binding friendly C libraries have the following characteristics: