Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust cbindgen Overrides for CFFI #1957

Merged
merged 2 commits into from Feb 29, 2024
Merged

Adjust cbindgen Overrides for CFFI #1957

merged 2 commits into from Feb 29, 2024

Conversation

nhusung
Copy link
Contributor

@nhusung nhusung commented Feb 23, 2024

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 same cbindgen.toml modulo the overrides for defines and include_guard, and passes the resulting header to CFFI. Unfortunately, CFFI's cdef 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 = [] and cpp_compat = false are the relevant ones for my use-case. I also disabled pragma_once (I'm not sure if the #pragma once can cause issues, though). language = C and no_includes = true were specified in the eprintln! message above but not actually set, so I added them as well. (In my tests, no_includes = true does not make sys_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 as NO_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 the cbindgen.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.

Copy link

netlify bot commented Feb 23, 2024

Deploy Preview for maturin-guide ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ac840e6
🔍 Latest deploy log https://app.netlify.com/sites/maturin-guide/deploys/65df9135332ab6000853239f
😎 Deploy Preview https://deploy-preview-1957--maturin-guide.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@messense
Copy link
Member

I'm not totally sure about this, please review this if you have time @konstin, thanks!

src/module_writer.rs Outdated Show resolved Hide resolved
@messense messense merged commit d4e881e into PyO3:main Feb 29, 2024
24 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants