Add WITH-POINTER-TO-VECTOR-DATA for Clasp (#385)
[cffi.git] / README.md
blobf7611838c0a3a1e47f3eb0a3df306372a718b5f6
1 [![Build Status](https://travis-ci.org/cffi/cffi.svg?branch=master)](https://travis-ci.org/cffi/cffi)
3 # What
5 CFFI, the Common Foreign Function Interface, purports
6 to be a portable FFI for Common Lisp. It abstracts away the
7 differences between the API of the native FFI's of the various Common
8 Lisp implementations.
10 # How
12 The CFFI library is composed of a Lisp-implementation-specific backend
13 in the CFFI-SYS package, and a portable frontend in the CFFI package.
15 The CFFI-SYS backend package defines a low-level interface to the
16 native FFI support in the Lisp implementation. It offers operators for
17 allocating and dereferencing foreign memory, calling foreign
18 functions, and loading shared libraries.
20 The CFFI frontend provides a more comfortable, declarative interface
21 for defining foreign functions, structures, typedefs, enumerated
22 types, etc. It is implemented in portable ANSI CL making use of the
23 low-level operators exported by CFFI-SYS.
25 The CFFI-LIBFFI subsystem loads support for passing structs by
26 value. It requires [libffi](https://sourceware.org/libffi/) for that.
28 Please consult [the
29 manual](http://common-lisp.net/project/cffi/manual/html_node/) for
30 further details, including installation instructions.
32 # Where
34 Please visit [Github](https://github.com/cffi/cffi/issues) for bug
35 reports, feature suggestions, the latest version, and to send your
36 contributions. CFFI also has a [mailing
37 list](https://mailman.common-lisp.net/listinfo/cffi-devel), and a
38 project page at [cffi.common-lisp.dev](https://cffi.common-lisp.dev/).
40 # Notes
42 ### CFFI/C2FFI
44 CFFI/C2FFI is an ASDF-integrated mechanism to automatically generate a
45 complete CFFI binding from C header files.
47 Its input is one `.h` file (with possible `#include`s of course), and
48 its final output is a lisp file with the relevant CFFI binding forms.
50 It requires a CLI tool called [c2ffi](https://github.com/rpav/c2ffi),
51 but only for the developers of the C binding libraries, not their
52 users. `c2ffi` is written in C++, and it uses Clang as a library to
53 parse the C code, and emit the result as JSON. To skip this step,
54 these host-specific JSON files can be checked into the repos of the
55 binding libraries. This breaks the dependence on a working c2ffi
56 binary and the C header files, which can be a hurdle.
58 These JSON files are then used to automatically generate a CL file
59 with the corresponding CFFI forms. The generated bindings mirror the C
60 namespace into an empty CL package as closely as possible. This means
61 that the upper/lower case of the C names are retained. It helps with
62 reading the original docs and with rewriting C examples into
63 lisp. `#define`s are also mirrored as CL `defconstant`s.
65 Binding library developers are advised to introduce another package on
66 top of this raw layer to add more lispy constructs where appropriate
67 (e.g. `with-` macros that manage resources, etc).
69 Until CFFI/C2FFI is properly documented, you may check out these
70 projects as examples:
71 [hu.dwim.zlib](https://github.com/hu-dwim/hu.dwim.zlib),
72 [hu.dwim.sdl](https://github.com/hu-dwim/hu.dwim.sdl),
73 [hu.dwim.bluez](https://github.com/hu-dwim/hu.dwim.bluez), and
74 [hu.dwim.mosquitto](https://github.com/attila-lendvai/hu.dwim.mosquitto).
76 ### Related projects
78 - [cl-autowrap](https://github.com/rpav/cl-autowrap) is another
79   project that uses `c2ffi` to generate CFFI bindings.