mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.4k stars 311 forks source link

Support C# and/or expose intermediate representation? #846

Open yanchith opened 1 year ago

yanchith commented 1 year ago

We have a project, where we generate both C and C# bindings for a Rust library. The API is very C-like, and we take care not to use any fancy features in it.

C# bindings are currently generated for us by csbindgen, which is not on par with cbindgen feature-wise (and its authors don't seem like they want it to be). We thus have to employ a lot of hacks to make it work.

cbindgen, on the other hand already supports most of what we need. Is either adding a new C# backend to cbindgen or exposing the intermediate representation so that people can write their own backends possible and desirable?

We might have to do this anyway, but we'll gladly contribute back.

fredszaq commented 1 year ago

Hi there ! I have a project where I'm exposing a rust lib to java/kotlin using JNA, and I'm toying with the idea of adding support inside cbindgen as well.

From what I see in the current code, the swich between the various supported languages is done via ifs in the implementation of Write for the various types in the ir module. This approach worked well for C and C++ but is already showing its limits with Cython, as there are quite a few cases where the Write impls are filled with if config.language == Language::Cython {

Adding a proper backend abstraction seems the way to go especially if we add C# and Java as supported language, maybe a trait with default impls called at relevant places in the Write impls and implemented for each language ?

What do you think @emilio, is even adding support for new languages something you would consider ? I'm happy to do this refactoring and adding java support if you're OK with it

emilio commented 1 year ago

Yeah, I'd say a general back-end abstraction (probably an initial refactor would be to add CLike and Cython backends) would be a nice path forward.

fredszaq commented 1 year ago

@yanchith @emilio I just opened #853 that contains a refactoring adding language backend abstraction ! Would be glad to have you thoughts on this, this should be at a point where adding new languages should be doable

fredszaq commented 1 year ago

And here is https://github.com/mozilla/cbindgen/pull/857 that contains the java backend, can be some inspiration to add the C# one

Tacodiva commented 11 months ago

@fredszaq @yanchith Is somebody else working on the C# backend or are you looking for contributers? I'd be happy to give it a crack if you liked. I'm a bit of a Rust novice, but know C# and it's interop stuff very well. I think I'd be able to do it but don't want to jump the gun if somebody else has already made progress on it or the language backend is too unstable for now.

fredszaq commented 11 months ago

Hi @Tacodiva I have no plans to work on the C# backend (I don't know the language). If you use the work in #853 adding a new language should be pretty straightforward if you're familiar with the language ffi :P

By the way ping @emilio I would love to see #853 merged at some point (as well as #857) please tell me if there is anything I can do to make this happen. I understand it is quite a big change and could take some time to review, but it seems like adding new languages support to cbindgen is a hot subject these days (see #867 as well) and it would greatly facilitate this !

yanchith commented 11 months ago

Hi @Tacodiva , I am not working on the C# backend yet. I was waiting for #853. If you want to give it a go, do it, otherwise I'll get to it eventually :)

ZhaoXiangXML commented 6 months ago

Hello @yanchith @Tacodiva, #853 is merged now, is anyone working on the C# backend? I'm also interested in it especially for Unity.

Tacodiva commented 5 months ago

@ZhaoXiangXML I am not currently working on it, I have moved onto other things. Eventually I might circle back around to it and when/if I do I'll open a PR here straight away to indicate I am working on it, but for now I am not.

yanchith commented 5 months ago

@ZhaoXiangXML Neither am I. Feel free to take it, if you are feeling the initiative.

ZhaoXiangXML commented 5 months ago

Well in that case I'll see if I can do it myself. Thanks.