All Projects → analytech-solutions → CBindingGen.jl

analytech-solutions / CBindingGen.jl

Licence: MIT License
Automatically generate Julia-C bindings!

Programming Languages

julia
2034 projects
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to CBindingGen.jl

libzahl
Suckless big integer library
Stars: ✭ 24 (+60%)
Mutual labels:  c-library
seabolt
Neo4j Bolt Connector for C
Stars: ✭ 37 (+146.67%)
Mutual labels:  c-library
nopoll
OpenSource WebSocket toolkit
Stars: ✭ 114 (+660%)
Mutual labels:  c-library
beautiful-capi
Beautiful Capi is a tool which automates the creation of compiler-independent and binary compatible C++ libraries across different C++ compilers
Stars: ✭ 31 (+106.67%)
Mutual labels:  c-api
zig-header-gen
Automatically generate headers/bindings for other languages from Zig code
Stars: ✭ 40 (+166.67%)
Mutual labels:  binding-generator
wrenbind17
A header only library for binding C++17 classes and functions to Wren, an embeddable programming language
Stars: ✭ 44 (+193.33%)
Mutual labels:  binding-generator
liberasurecode
A Rust wrapper for `openstack/liberasurecode`
Stars: ✭ 19 (+26.67%)
Mutual labels:  c-bindings
libgen
Automatic C-bindings generator for the Crystal language
Stars: ✭ 71 (+373.33%)
Mutual labels:  binding-generator
Fortress-of-Solitude
This Library has resources to solve common data structure algorithm problems like a Doubly linked list, Generic trees, Queue, Stack, and other algorithms. Each lib has an option to carry your custom data in elements. Custom data in detail, other fantastic resources.
Stars: ✭ 53 (+253.33%)
Mutual labels:  c-library
scala-native-bindgen
Scala Native Binding Generator
Stars: ✭ 29 (+93.33%)
Mutual labels:  binding-generator
libkeccak
[Basically feature complete] Keccak-family hashing library
Stars: ✭ 53 (+253.33%)
Mutual labels:  c-library
pywrap
C++ binding generator based on libclang and pybind11
Stars: ✭ 17 (+13.33%)
Mutual labels:  binding-generator
masterkeys-linux
MasterKeys SDK for Linux
Stars: ✭ 22 (+46.67%)
Mutual labels:  c-library
extensions
Code Generators and Extensions for vanilla-rtb stack
Stars: ✭ 16 (+6.67%)
Mutual labels:  binding-generator
Wasm Bindgen
Facilitating high-level interactions between Wasm modules and JavaScript
Stars: ✭ 4,695 (+31200%)
Mutual labels:  binding-generator
Toaruos
A completely-from-scratch hobby operating system: bootloader, kernel, drivers, C library, and userspace including a composited graphical UI, dynamic linker, syntax-highlighting text editor, network stack, etc.
Stars: ✭ 4,687 (+31146.67%)
Mutual labels:  c-library
Libwebsockets
canonical libwebsockets.org networking library
Stars: ✭ 3,314 (+21993.33%)
Mutual labels:  c-library

CBinding.jl

Features of this package are now available as https://github.com/analytech-solutions/CBinding.jl

CBindingGen.jl

Build Status

Automatically generate Julia bindings to C API's! 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.

Usage

CBindingGen.jl seeks to be a comprehensive and automatic C bindings generation framework. The bindings utilize the CBinding.jl capabilities to precisely interface Julia with C.

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. The generated bindings file can then be included from your package code. 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. Se let's get started with an example!

Generating

To start, you must add CBinding = "^0.9" as a dependency to your package, and CBindingGen = "^0.3,^0.4" as a build dependency. CBindingGen.jl relies on the artifacts distributed with LLVM_jll for providing a libclang.so library and header files for your system, so we will use those to demonstrate. 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. (another example found in PLCTag.jl)

using CBindingGen
import LLVM_jll

incdir = joinpath(dirname(dirname(LLVM_jll.libclang_path)), "include")
hdrs = map(hdr -> joinpath("clang-c", hdr), readdir(joinpath(incdir, "clang-c")))

cvts = convert_headers(hdrs, args = ["-I", incdir]) do cursor
	header = CodeLocation(cursor).file
	name   = string(cursor)
	
	# only wrap the libclang headers
	startswith(header, "$(incdir)/") || return false
	
	# ignore function that uses time_t since we don't know what time_t is yet
	name == "clang_getFileTime" && return false
	
	return true
end

open("bindings.jl", "w+") do io
	generate(io, LLVM_jll.libclang_path => cvts)
end

The convert_headers function takes an array of header files and the command line arguments, args. 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. An important detail of convert_headers is the filter function provided, here provided with the do syntax. The filter function allows you fine-grained control over what is converted to Julia as the C AST is traversed. In our example, we filter out any C constructs not defined within the header files we are interested in.

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. Additionally, string(cursor) will get the "spelling" of the expression if you wish to filter particular C symbols.

The result of convert_headers is an array of Converted objects. Converted objects contain the Julia expression strings, as expr, and comments for storing exportable symbols an their comments ported from C.

Finally, the generate function is used to write the converted expressions for one or more libraries into a bindings file.

Loading

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. The namespace within the baremodule will have only a very few symbols that could conflict with those from C. CBinding provides 𝐣𝐥 (\bfj<tab>\bfl<tab>) to provide access to the CBinding types and macros without increasing the chance of naming conflicts. (another example found in PLCTag.jl)

module MyModule
	baremodule LibClang
		using CBinding: 𝐣𝐥
		
		const size_t = 𝐣𝐥.Csize_t
		𝐣𝐥.Base.include((𝐣𝐥.@__MODULE__), "pre-bindings.jl"))  # <-handwritten
		𝐣𝐥.Base.include((𝐣𝐥.@__MODULE__), 𝐣𝐥.joinpath(𝐣𝐥.dirname(𝐣𝐥.@__DIR__), "deps", "libclang.jl"))  # <-generated
		𝐣𝐥.Base.include((𝐣𝐥.@__MODULE__), "post-bindings.jl"))  # <-handwritten
	end
	
	# other module code, such as high-level Julian code wrapping the bindings...
end

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. Finishing the bindings module is the inclusion of the bindings file generated at build-time.

Help/Comments

CBindingGen.jl automatically imports comments from the C header files into Julia's @doc string syntax. If you find that C header comments are not imported, you should try adding -fparse-all-comments to the list of args in your call to convert_headers.

julia> import MyModule

help?> MyModule.LibClang.clang_getClangVersion
  𝐣𝐥.@cextern clang_getClangVersion()::CXString

  Return a version string, suitable for showing to a user, but not intended to be parsed (the format is not guaranteed to be stable).

  Reference
  ===========

  Index.h:5482 (./include/clang-c/Index.h:5482:25)

Using

Use the generated bindings as you would any hand-written or generated ccall and cglobal wrappers. Remember, this is very low-level interfacing, and segmentation faults can result from misuse.

julia> cxstr = MyModule.LibClang.clang_getClangVersion();

julia> unsafe_string(MyModule.LibClang.clang_getCString(cxstr))
"clang version 8.0.1 "

julia> MyModule.LibClang.clang_disposeString(cxstr)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].