cffi
2024-10-12
The Common Foreign Function Interface
Upstream URL
Author
Maintainer
License
What
CFFI, the Common Foreign Function Interface, purports to be a portable FFI for Common Lisp. It abstracts away the differences between the API of the native FFI's of the various Common Lisp implementations.
How
The CFFI library is composed of a Lisp-implementation-specific backend in the CFFI-SYS package, and a portable frontend in the CFFI package.
The CFFI-SYS backend package defines a low-level interface to the native FFI support in the Lisp implementation. It offers operators for allocating and dereferencing foreign memory, calling foreign functions, and loading shared libraries.
The CFFI frontend provides a more comfortable, declarative interface for defining foreign functions, structures, typedefs, enumerated types, etc. It is implemented in portable ANSI CL making use of the low-level operators exported by CFFI-SYS.
The CFFI-LIBFFI subsystem loads support for passing structs by value. It requires libffi for that.
Please consult the manual for further details, including installation instructions.
Where
Please visit Github for bug reports, feature suggestions, the latest version, and to send your contributions. CFFI also has a mailing list, and a project page at cffi.common-lisp.dev.
Notes
CFFI/C2FFI
CFFI/C2FFI is an ASDF-integrated mechanism to automatically generate a complete CFFI binding from C header files.
Its input is one .h
file (with possible #include
s of course), and
its final output is a lisp file with the relevant CFFI binding forms.
It requires a CLI tool called c2ffi,
but only for the developers of the C binding libraries, not their
users. c2ffi
is written in C++, and it uses Clang as a library to
parse the C code, and emit the result as JSON. To skip this step,
these host-specific JSON files can be checked into the repos of the
binding libraries. This breaks the dependence on a working c2ffi
binary and the C header files, which can be a hurdle.
These JSON files are then used to automatically generate a CL file
with the corresponding CFFI forms. The generated bindings mirror the C
namespace into an empty CL package as closely as possible. This means
that the upper/lower case of the C names are retained. It helps with
reading the original docs and with rewriting C examples into
lisp. #define
s are also mirrored as CL defconstant
s.
Binding library developers are advised to introduce another package on
top of this raw layer to add more lispy constructs where appropriate
(e.g. with-
macros that manage resources, etc).
Until CFFI/C2FFI is properly documented, you may check out these projects as examples: hu.dwim.zlib, hu.dwim.sdl, hu.dwim.bluez, and hu.dwim.mosquitto.
Related projects
- cl-autowrap is another
project that uses
c2ffi
to generate CFFI bindings.
Dependents (283)
- 3bz
- also-alsa
- antik
- async-process
- basic-binary-ipc
- bike
- binary-structures
- bodge-blobs-support
- bodge-chipmunk
- bodge-glad
- bodge-glfw
- bodge-nanovg
- bodge-nuklear
- bodge-ode
- bodge-openal
- bodge-sndfile
- bodge-utilities
- bp
- burgled-batteries
- cari3s
- cephes.cl
- cepl
- cffi-c-ref
- cffi-object
- cffi-ops
- clack-prometheus
- cl-ana
- classimp
- cl-async
- cl-aubio
- cl-autowrap
- claw
- claw-olm
- claw-utils
- cl-bayesnet
- cl-bmas
- cl-cairo2
- cl-capstone
- cl-cblas
- cl-ceigen-lite
- cl-cerf
- cl-cffi-gtk
- cl-charms
- cl-clblas
- cl-clon
- cl-clsparse
- cl-cmark
- cl-collider
- cl-cpus
- cl-cuda
- cl-cxx
- cl-cxx-jit
- cl-devil
- cl-diskspace
- cl-drawille
- cl-drm
- cl-duckdb
- cl-egl
- cl-enchant
- cl-ev
- cl-fam
- cl-fastcgi
- cl-fbclient
- cl-fbx
- cl-flac
- cl-fond
- cl-freeimage
- cl-freetype2
- cl-fsnotify
- cl-gamepad
- cl-gbm
- cl-gcrypt
- cl-geoip
- cl-geos
- cl-getopt
- cl-git
- cl-glfw
- cl-glfw3
- cl-gltf
- cl-gobject-introspection
- cl-gog-galaxy
- cl-gpio
- cl-gss
- cl-gtk2
- cl-iconv
- clinch
- clinenoise
- cl-inotify
- cl-k8055
- cl-kyoto-cabinet
- cl-liballegro
- cl-liballegro-nuklear
- cl-libevent2
- cl-libiio
- cl-libinput
- cl-libpuzzle
- cl-libssh2
- cl-libsvm
- cl-libusb
- cl-libuv
- cl-libxml2
- cl-libyaml
- cl-lzlib
- cl-lzma
- cl-m4
- cl-maxminddb
- cl-mdb
- cl-mecab
- cl-megolm
- cl-messagepack-rpc
- cl-mixed
- clml
- cl-mlep
- cl-monitors
- cl-mount-info
- cl-mpg123
- cl-mpi
- cl-mssql
- cl-mw
- cl-mysql
- cl-nxt
- cl-oclapi
- cl-ode
- cl-openal
- cl-opencl
- cl-opengl
- cl-opsresearch
- cl-opus
- cl-out123
- cl-pango
- cl-pixman
- cl-plplot
- cl-png
- cl-portaudio
- cl-pslib
- cl-pslib-barcode
- cl-rabbit
- cl-rdkafka
- cl-readline
- cl-resvg
- cl-rlimit
- cl-rmath
- cl-rrd
- cl-rsvg2
- cl-sdl2-ttf
- cl-secp256k1
- cl-setlocale
- cl-skip-list
- cl-slp
- cl-soil
- cl-soloud
- cl-sophia
- cl-spidev
- clsql
- cl-sqlite
- cl+ssl
- cl-steamworks
- cl-strftime
- cl-syslog
- cl-tcod
- cl-termbox
- cl-tesseract
- cl-tidy
- cl-tk
- cl-tokyo-cabinet
- cl-trivial-clock
- cl-unac
- cl-unix-sockets
- cl-vorbis
- cl-wayland
- cl-webkit
- cl-with
- cl-xkb
- cl-zmq
- cl-zstd
- cl-zyre
- cocoas
- commonqt
- com-on
- consfigurator
- croatoan
- cserial-port
- dendrite
- deoxybyte-unix
- deploy
- docparser
- eazy-process
- femlisp
- ffa
- file-attributes
- file-lock
- file-notify
- file-select
- filesystem-utils
- font-discovery
- freebsd-sysctl
- fsocket
- glfw
- glop
- glu-tessellate
- gooptest
- grovel-locally
- hdf5-cffi
- hu.dwim.bluez
- hu.dwim.graphviz
- hu.dwim.quasi-quote
- hu.dwim.rdbms
- hu.dwim.sdl
- hu.dwim.web-server
- hu.dwim.zlib
- hyperluminal-mem
- inotify
- iolib
- ip-interfaces
- jpeg-turbo
- keystone
- laap
- lev
- leveldb
- liblmdb
- linear-programming-glpk
- linedit
- linux-packaging
- lisp-binary
- lispbuilder
- lisp-zmq
- lla
- machine-state
- magicffi
- magicl
- mcclim
- memory-regions
- messagebox
- mixalot
- mmap
- mtif
- nlopt
- oclcl
- omglib
- open-with
- osicat
- pal
- petalisp
- plokami
- pngload
- posix-shm
- pounds
- ppath
- precise-time
- prometheus.cl
- py4cl2-cffi
- pzmq
- qt-libs
- qvm
- rcl
- reblocks-prometheus
- restas
- schannel
- sel
- shadow
- simple-finalizer
- static-vectors
- sucle
- system-locale
- teepeedee2
- tesseract-capi
- thnappy
- trace-db
- trivia
- trivial-debug-console
- trivial-features
- trivial-signal
- trivial-ssh
- unboxables
- uncursed
- verrazano
- vk
- wayflan
- winhttp
- winlock
- woo
- workout-timer
- zacl