github.com/j8r/crystal-autobind

Automatic C binding generator for Crystal

15 stars
2 dependents
License: Apache-2.0

Nothing has been indexed for 0.3.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  autobind:
    github: j8r/crystal-autobind
    version: ~> 0.3.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.3.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Autobind

Apache2.0

Automatic C bindings generator for Crystal

Built from the awesome clang.cr done by ysbaddaden

How to use (recommended)

Installation

Add this block to your application's shard.yml:

dependencies:
  autobind:
    github: j8r/crystal-autobind

scripts:
  postinstall: ./gen-bindings.sh

You can create a gen-bindings.sh script, with the permissions chmod 750

#!/bin/sh

# Example of generating the bindings for `errno` at `src/libc/errno.cr`
mkdir -p src/libc && lib/autobind/bin/autobind -I/usr/include errno.h > src/libc/errno.cr

The postinstall script will only be runned when included as a dependency on a project.

Usage

The newly generated .cr bindings can be used to create a documented shard wrapper, that can be then require and used as a shard in your application.

The development headers of the library are required whether the bindings are previously generated or not.

The only caveat is to have clang/libclang installed to regenerate the bindings.

The bindings directory can thus be ignored in .gitignore to avoid versioning:

/bin
/lib
/shard.lock
/src/libc

Build

Ensure to have libclang installed

Install dependencies:

shards install

Build autobind:

crystal build src/autobind.cr

Usage examples

You will need the development headers of your targeted library, usually coming inside the dev packages of you distribution.

./autobind -I/usr/include errno.h > errno.cr

./autobind -I/usr/lib/llvm-6.0/include llvm-c/Core.h --remove-enum-prefix=LLVM --remove-enum-suffix > Core.cr

./autobind -I/usr/lib/llvm-6.0/include clang-c/Index.h --remove-enum-prefix > Index.cr

Reference

License

Distributed under the Apache 2.0 license.