Skip to content

node-ffi-packager/node-ffi-generate

 
 

Repository files navigation

node-ffi-packager logotype, impossible cubes in green

README · Changelog · Development


node-ffi-generate

A tool to generate Node.js Foreign Function Interface (FFI) bindings for a given C/C++ header file. Based on node-libclang.

Installation

For command line usage.

# NOTE: global installation is not necessary, but convenient for command line usage.
npm install --global github:node-ffi-packager/node-ffi-generate#semver:^v2.0.2

For programmatic usage.

npm install --save github:node-ffi-packager/node-ffi-generate#semver:^v2.0.2

Requirements

  • Selected parts of the The LLVM Compiler Infrastructure.
    • The llvm-config tool.
      • Used to automatically look up the location of libclang, but it can be manually configured.
      • Try symlinking versioned executables (such as llvm-config-9) to llvm-config in your PATH.
    • The libclang library.
    • Installing using a package manager is strongly recommended.

CLI usage

Parse the given header file and print the rendered javascript to standard. Fast and simple to use.

ffi-generate --file '/path/to/myLibrary/header.h' --library 'libmyLibrary'

Usage

Generate node-ffi-napi javascript bindings for a given C/C++ header file

Options:
  -f, --file         The header file to parse                                                              [required]
  -l, --library      The name of the library to dlopen                                                     [required]
  -x, --single-file  Only export functions found in this file
  -p, --prefix       Only import functions whose name start with prefix. Can be specified multiple times.

Extra libclang arguments

It may be necessary to pass additional flags/arguments to libclang so it can better parse the header (for example include paths). To pass options directly to libclang use -- so ffi-generate knows to stop parsing arguments. The rest will be passed to libclang without modification.

ffi-generate --file '/usr/include/ImageMagick/wand/MagickWand.h' --library 'libMagickWand' --prefix 'Magick' -- $(Magick-config --cflags)

Programmatic usage

Location of libclang

Setting LD_LIBRARY_PATH (or DYLD_LIBRARY_PATH on macOS) might be necessary for ffi-generate to find the libclang dynamic library. For the ffi-generate command, this is performed internally by calling llvm-config. For programmatic use try setting it manually as an environment variable.

LD_LIBRARY_PATH="$(llvm-config --libdir)" node my-generator-code.js

Debugging

Get additional debug messages by setting the DEBUG environment variable.

 DEBUG='ffi-generate:*' ffi-generate <other arguments as usual>

node-ffi-generate Copyright © 2011, 2012, 2013, 2014 Timothy J Fontaine, © 2020, 2021 Joel Purra. Released under MIT License.

Languages

  • C 53.9%
  • JavaScript 45.7%
  • Mustache 0.4%