|
| 1 | +# Generating Bindings to Objective-C |
| 2 | + |
| 3 | +`bindgen` does not (yet) have full objective-c support but it can generate bindings |
| 4 | +for a lot of the apple frameworks without too much blacklisting. |
| 5 | + |
| 6 | +In order to generate bindings, you will need `-x objective-c` as the clang |
| 7 | +args. If you'd like to use [block](https://crates.io/crates/block) you will need |
| 8 | +`-fblocks` as a clang arg as well. |
| 9 | + |
| 10 | +Depending on your setup, you may need `--generate-block` to generate the block |
| 11 | +function aliases and `--block-extern-crate` to insert a `extern crate block` at |
| 12 | +the beginning of the generated bindings. The same logic applies to the |
| 13 | +`--objc-extern-crate` parameter. |
| 14 | + |
| 15 | +The objective-c classes will be represented as a `struct Foo(id)` and a trait |
| 16 | +`IFoo` where `Foo` is the objective-c class and `id` is an alias for `*mut |
| 17 | +objc::runtime::Object` (the pointer to the objective-c instance). The trait |
| 18 | +`IFoo` is needed to allow for the generated inheritance. |
| 19 | + |
| 20 | +Each class (struct) has an `alloc` and a `dealloc` to match that of some of the alloc |
| 21 | +methods found in `NSObject`. |
| 22 | + |
| 23 | +In order to initialize a class `Foo`, you will have to do something like `let |
| 24 | +foo = Foo(Foo::alloc().initWithStuff())`. |
| 25 | + |
| 26 | + |
| 27 | +## Supported Features |
| 28 | +* Inheritance matched to rust traits with prefixes of `I` which |
| 29 | +stands for interface. |
| 30 | +* Protocols which match to rust traits with prefixes of `P` which |
| 31 | +stands for Protocol. |
| 32 | +* Classes will generate `struct Foo(id)` where `Foo` is the class |
| 33 | +name and `id` is a pointer to the objective-c Object. |
| 34 | +* Blocks |
| 35 | + |
| 36 | +## Useful Notes |
| 37 | +* If you're targeting `aarch64-apple-ios`, you'll need to have the clang arg |
| 38 | +`--target=arm64-apple-ios` as mentioned |
| 39 | +[here](https://github.com/rust-lang/rust-bindgen/issues/1211#issuecomment-569804287). |
| 40 | +* The generated bindings will almost certainly have some conflicts so you will |
| 41 | +have to blacklist a few things. There are a few cases of the parameters being |
| 42 | +poorly named in the objective-c headers. But if you're using anything with |
| 43 | +Core Foundation, you'll find that `time.h` as has a variable called timezone that |
| 44 | +conflicts with some of the things in `NSCalendar.h`. |
| 45 | +* Some small subset of the function headers in the apple frameworks go against |
| 46 | +apple's guidelines for parameter names and duplicate the names in the header |
| 47 | +which won't compile as mentioned |
| 48 | +[here](https://github.com/rust-lang/rust-bindgen/issues/1705). |
| 49 | +* instancetype return methods does not return `Self` for you given class, it |
| 50 | +returns a `mut * objc::runtime::Objc` which is aliased as `id`. This is because |
| 51 | +objective-c's inheritance doesn't perfectly match that of rusts. |
| 52 | +* Depending on what you're trying `bindgen` against, you may end up including |
| 53 | +all of Core Foundation and any other frameworks. This will result in a very |
| 54 | +long compile time. |
| 55 | + |
| 56 | +## Not (yet) Supported |
| 57 | +* Nullablibility attributes which return `Option`s. |
| 58 | +* Probably many other things. Feel free to [open an issue](https://github.com/rust-lang/rust-bindgen/issues). |
| 59 | + |
| 60 | +# Example crate(s) |
| 61 | +* [uikit-sys](https://github.com/simlay/uikit-sys) |
0 commit comments