Adjust cbindgen Overrides for CFFI #1957
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
My Use-case
I'm working on a project for which I'd like to create C/C++ and Python bindings. My gut feeling says that having a simple hand-written C FFI with a C header generated by cbindgen is the best option. On top of that, I'd like to provide user-friendly, idiomatic (hand-written) C++ and Python interfaces. Maturin seems to be a good fit for simplifying the build process of the Python bindings.
Issue
For a good integration in C/C++ projects, I use cbindgen options such as
sys_includes
(generating#include <stdlib.h>
, …)cpp_compat
(#ifdef __cplusplus
etc.). Currently, Maturin uses the samecbindgen.toml
modulo the overrides fordefines
andinclude_guard
, and passes the resulting header to CFFI. Unfortunately, CFFI'scdef
method cannot deal with those C preprocessor directives generated under my configuration, resulting in a crash.Proposed Change, Alternatives
This PR adds a few more overrides:
sys_includes = []
andcpp_compat = false
are the relevant ones for my use-case. I also disabledpragma_once
(I'm not sure if the#pragma once
can cause issues, though).language = C
andno_includes = true
were specified in theeprintln!
message above but not actually set, so I added them as well. (In my tests,no_includes = true
does not makesys_includes = []
obsolete.)The proposed change does the job for me, but I'm not sure if others might need additional overrides. In the
cbindgen.toml
documentation at https://github.com/mozilla/cbindgen/blob/master/docs.md#cbindgentoml, there are various options that might cause problems, e.g., function attributes such asNO_RETURN
, which is emitted for Rust functions returning!
. I could imagine two alternatives/additions to this PR that might help in such situations: (1) Providing an option to manually set thecbindgen.toml
path or (2) exposing the override settings. I'm not sure if this is needed, though; this PR makes things work out of the box in my scenario.