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

Using metal-cpp for auto-generating bindings #220

Open
aclysma opened this issue Nov 21, 2021 · 2 comments
Open

Using metal-cpp for auto-generating bindings #220

aclysma opened this issue Nov 21, 2021 · 2 comments

Comments

@aclysma
Copy link
Contributor

aclysma commented Nov 21, 2021

Apple recently announced official C++ bindings for metal. https://developer.apple.com/metal/cpp/

I was curious if it would be possible to use bindgen on it to autogenerate bindings to metal. If bindings are autogenerated, it might help avoid undefined behavior caused by incorrectly implemented bindings (there is no type checking for the objc calls from metal-rs into metal, but there could be type checking from metal-rs into code produced by bindgen)

The experience was mixed. The library includes a way to generate a single header file, and bindgen is able to parse it (use something like bindgen metal.cpp -o src/bindings.rs -- -x c++ -std=c++17 -isysroot$(xcrun --sdk macosx --show-sdk-path). The produced rust bindings appear to be usable and some additional args passed to bindgen would permit allow-listing certain types/functions via matching their name prefixes.

However, the downside is that all of the code in metal-cpp is meant to be inlined. So it would not be possible to link against functions in it from rust. Even if this were fixed by removing inline keywords (easy to do because it's a #define that can be modified), the name mangling is C++ based which I don't think is stable/portable in the same way that C functions would be.

So, for this to be useful, I think ideally the library should be wrapped in C functions. I imagine a determined person could automate generating them, and that it would be easier than automating generating C wrappers for objective-c. (cimgui might be a useful reference https://github.com/cimgui/cimgui).

(To be clear, I'm not strongly suggesting this for metal-rs. Just thought it was an interesting option and wanted to share in case others find it interesting.)

@dobkeratops
Copy link

this is pretty interesting but I'd guess that Rust -> ObjC will be lower friction than Rust -> C++ -> ObjC. It's a shame they dont have some underlying C api (or do they?)

@madsmtm
Copy link
Contributor

madsmtm commented Sep 20, 2022

If it's of any interest, I'm working on automatically generating the Rust -> ObjC bindings: madsmtm/objc2#264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants