|
1 | 1 | # CBindingGen.jl
|
2 | 2 |
|
3 | 3 | Automatically generate Julia bindings to C API's!
|
| 4 | +We are developing CBindingGen.jl and CBinding.jl to support the use of arbitrary local C libraries, such as those provided by your Linux distribution, from Julia. |
| 5 | + |
4 | 6 |
|
5 | 7 | # Usage
|
6 | 8 |
|
7 |
| -CBindingGen.jl builds upon some of the processing capabilities of Clang.jl. |
8 |
| -Our approach also utilizes the components provided by the CBinding.jl package to more precisely and more completely interface with C API's. |
| 9 | +CBindingGen.jl seeks to be a comprehensive and automatic C bindings generation framework. |
| 10 | +The bindings utilize the CBinding.jl capabilities to precisely interface Julia with C. |
9 | 11 |
|
10 |
| -This package is primarily used at package build time when you wish to generate bindings to a C library on the system or one installed at build time. |
11 |
| -There are several important details about the process to point out. |
| 12 | +This package should only be used at package build time when you wish to generate bindings to a particular C library on the system or one built and installed at build time. |
| 13 | +The generated bindings file can then be included from your package code. |
| 14 | +Bindings files created by this package should _not_ be committed with your package and they are _not_ meant for editing by lowly humans once generated. |
| 15 | +Se let's get started with an example! |
12 | 16 |
|
13 |
| -First, a filtering function must be defined to provide a means to limit the amount of material interfaced with the bindings. |
14 |
| -Remember that C `#include` statements insert files into the AST (and then every file they include gets inserted as well), so it is important to have an effective and accurate filter function. |
15 | 17 |
|
16 |
| -Second, the proper compile flags must be specified. |
17 |
| -If you expect a C user's code to rely on `pkg-config` to get what flags are needed to include and link against your library, then those flags must also be provided to the `ConverterContext`. |
| 18 | +## Generating Bindings |
18 | 19 |
|
19 |
| -The last detail required is to accurately and completely specify the library (or libraries) you want the bindings to link to as well as the list of header files to use to start the wrapping process (NOTICE: bindings are generated according to the filter function, so this list of header files only ensures that they are seen by the parser). |
20 |
| -An example of the process for generating bindings is shown below. |
| 20 | +CBindingGen.jl relies on the artifacts distributed with `LLVM_jll` for providing a `libclang.so` library and header files for your system. |
| 21 | +The following code shows what is necessary to generate bindings to `libclang.so`, and something like it would normally be placed in your package's `deps/build.jl` file. |
21 | 22 |
|
22 |
| -```jl |
| 23 | +```julia |
23 | 24 | using CBindingGen
|
24 |
| -using CBindingGen.Clang |
25 | 25 |
|
26 |
| -const hdrs = [ |
27 |
| - "hdr1.h", |
28 |
| - "hdr2.h", |
29 |
| - "hdr3.h", |
30 |
| -] |
| 26 | +incdir = joinpath(dirname(dirname(LLVM_jll.libclang_path)), "include") |
| 27 | +hdrs = map(hdr -> joinpath("clang-c", hdr), readdir(joinpath(incdir, "clang-c"))) |
31 | 28 |
|
32 |
| -ctx = ConverterContext(["libexample"]) do decl |
33 |
| - header = filename(decl) |
34 |
| - name = spelling(decl) |
35 |
| - |
36 |
| - # ignore anything not in the library's headers, e.g. "LibExample/hdr1.h" |
37 |
| - any(hdr -> endswith(header, joinpath("LibExample", hdr)), hdrs) || return false |
38 |
| - |
39 |
| - # ignore the particular functions listed below (usually because they are in a header but not exposed with the library) |
40 |
| - decl isa CLFunctionDecl && name in ( |
41 |
| - "missing1", |
42 |
| - "missing2", |
43 |
| - ) && return false |
| 29 | +cvts = convert_headers(hdrs, args = ["-I", incdir]) do cursor |
| 30 | + header = CodeLocation(cursor).file |
| 31 | + name = string(cursor) |
44 | 32 |
|
45 |
| - # ignore functions, variables, and macros starting with double-underscore |
46 |
| - startswith(name, "__") && (decl isa CLFunctionDecl || decl isa CLVarDecl || decl isa CLMacroDefinition) && return false |
| 33 | + # only wrap the libclang headers |
| 34 | + startswith(header, "$(incdir)/") || return false |
47 | 35 |
|
48 | 36 | return true
|
49 | 37 | end
|
50 | 38 |
|
51 |
| -parse_headers!(ctx, hdrs, args = ["-std=gnu99", "-DUSE_DEF=1]) |
52 |
| -generate(ctx, joinpath(dirname(@__DIR__), "gen"), "example") |
| 39 | +open("bindings.jl", "w+") do io |
| 40 | + generate(io, LLVM_jll.libclang_path => cvts) |
| 41 | +end |
| 42 | + |
53 | 43 | ```
|
54 | 44 |
|
55 |
| -The call to `generate(...)` in the example above will create several files in the package's "gen" directory which are prefixed with "example". |
56 |
| -Suffixes on the files help to indicate when each file can be included into a package. |
57 |
| -In a use case where the bindings are generated at build-time and the package is precompiled, the following example represents a template for providing the bindings as a Julia package. |
| 45 | +The `convert_headers` function takes an array of header files and the command line arguments, `args`. |
| 46 | +Any include directories, compiler options, or preprocessor definitions would be provided in `args` in the same way they would be used in your `clang` command line. |
| 47 | +An important detail of `convert_headers` is the filter function provided, here provided with the `do` syntax. |
| 48 | +The filter function allows you fine-grained control over what is converted to Julia as the C AST is traversed. |
| 49 | +In our example, we filter out any C constructs not defined within the header files we are interested in. |
58 | 50 |
|
59 |
| -```jl |
60 |
| -module ExampleBindings |
61 |
| - # bring in dependencies |
62 |
| - using DepPkg1, DepPkg2 |
63 |
| - |
64 |
| - # CBinding is required by the CBindingGen-generated files |
65 |
| - import CBinding |
66 |
| - |
67 |
| - # define opaque types (currently needs to be explicitly done) |
68 |
| - CBinding.@cstruct MyOpaqueType |
69 |
| - |
70 |
| - # use of fully qualified names is a must if the bindings define a symbol from Base! |
71 |
| - Base.include(Base.@__MODULE__, Base.joinpath(Base.dirname(Base.@__DIR__), "gen", "example-atdevelopop.jl")) |
72 |
| - Base.include(Base.@__MODULE__, Base.joinpath(Base.dirname(Base.@__DIR__), "gen", "example-atcompile.jl")) |
73 |
| - Base.include(Base.@__MODULE__, Base.joinpath(Base.dirname(Base.@__DIR__), "gen", "example-atcompile_typedefs.jl")) |
74 |
| - Base.include(Base.@__MODULE__, Base.joinpath(Base.dirname(Base.@__DIR__), "gen", "example-atcompile_bindings.jl")) |
75 |
| - |
76 |
| - function __init__() |
77 |
| - Base.include(Base.@__MODULE__, Base.joinpath(Base.dirname(Base.@__DIR__), "gen", "example-atload.jl")) |
78 |
| - end |
79 |
| -end |
80 |
| -``` |
| 51 | +We use `CodeLocation(cursor)` to get the `file`, `line`, and `col` for the start of the C expression, while `CodeRange(cursor)` can be used to get the `start` and `stop` locations of the expression. |
| 52 | +Additionally, `string(cursor)` will get the "spelling" of the expression if you wish to filter particular C symbols. |
81 | 53 |
|
82 |
| -It is also possible, completely at package load-time, to generate bindings and load them dynamically. |
| 54 | +The result of `convert_headers` is an array of `Converted` objects. |
| 55 | +`Converted` objects contain the Julia expression strings, as `expr`, and `comments` for storing exportable symbols an their comments ported from C. |
83 | 56 |
|
84 |
| -```jl |
85 |
| -module ExampleBindings |
86 |
| - # bring in dependencies |
87 |
| - using DepPkg1, DepPkg2 |
88 |
| - |
89 |
| - # CBinding is required by the CBindingGen-generated files |
90 |
| - import CBinding, CBindingGen |
91 |
| - |
92 |
| - # define opaque types (currently needs to be explicitly done) |
93 |
| - CBinding.@cstruct MyOpaqueType |
94 |
| - |
95 |
| - function __init__() |
96 |
| - hdrs = [ |
97 |
| - "hdr1.h", |
98 |
| - "hdr2.h", |
99 |
| - "hdr3.h", |
100 |
| - ] |
| 57 | +Finally, the `generate` function is used to write the converted expressions for one or more libraries into a bindings file. |
| 58 | + |
| 59 | + |
| 60 | +## Loading Bindings |
| 61 | + |
| 62 | +In order to load the bindings file within your package, it is best to define a `baremodule` within your package module to encapsulate the bindings. |
| 63 | +The namespace within the `baremodule` will have only a very few symbols that could conflict with those from C. |
| 64 | +We rely on macro usage within Julia to carefully avoid introducing any more conflicting symbols. |
| 65 | +CBinding provides `@macros` to define macros for many of the C-types in Julia, and it also defines `@CBinding()` to allow for unrestrained, but less-concise, access to Julia symbols. |
| 66 | + |
| 67 | +```julia |
| 68 | +module MyModule |
| 69 | + baremodule LibClang |
| 70 | + using CBinding: @macros |
| 71 | + @macros |
101 | 72 |
|
102 |
| - ctx = CBindingGen.ConverterContext(["libexample"]) do decl |
103 |
| - header = CBindingGen.Clang.filename(decl) |
104 |
| - name = CBindingGen.Clang.spelling(decl) |
105 |
| - |
106 |
| - # ignore anything not in the library's headers, e.g. "LibExample/hdr1.h" |
107 |
| - Base.any(hdr -> Base.endswith(header, Base.joinpath("LibExample", hdr)), hdrs) || return false |
108 |
| - |
109 |
| - # ignore the particular functions listed below (usually because they are in a header but not exposed with the library) |
110 |
| - decl isa CBindingGen.Clang.CLFunctionDecl && name in ( |
111 |
| - "missing1", |
112 |
| - "missing2", |
113 |
| - ) && return false |
114 |
| - |
115 |
| - # ignore functions, variables, and macros starting with double-underscore |
116 |
| - Base.startswith(name, "__") && (decl isa CBindingGen.Clang.CLFunctionDecl || decl isa CBindingGen.Clang.CLVarDecl || decl isa CBindingGen.Clang.CLMacroDefinition) && return false |
117 |
| - |
118 |
| - return true |
119 |
| - end |
| 73 | + const size_t = @Csize_t |
| 74 | + @include("bindings-deps.jl")) |
120 | 75 |
|
121 |
| - CBindingGen.Clang.parse_headers!(ctx, hdrs, args = ["-std=gnu99", "-DUSE_DEF=1]) |
122 |
| - @eval $(CBindingGen.generate(ctx)) |
| 76 | + @include(@CBinding().joinpath(@CBinding().dirname(@CBinding().@__DIR__), "deps", "bindings.jl")) |
123 | 77 | end
|
| 78 | + |
| 79 | + # other module code, such as high-level Julian code wrapping the bindings... |
124 | 80 | end
|
125 | 81 | ```
|
126 | 82 |
|
127 |
| -Once the bindings are generated, your next step would be to provide some higher-level Julia constructs to wrap the bindings with. |
| 83 | +Next is a section defining dependencies of the bindings and should be composed of hand-written code or imported packages that export the required symbols. |
| 84 | +Finishing the bindings module is the inclusion of the bindings file generated at build-time. |
| 85 | + |
0 commit comments