rust-bindgen
rust-bindgen copied to clipboard
how to generate bindings to apple frameworks
I'm trying to generate bindings for apple frameworks.
Tried multiple combinations.
Here is an example:
bindgen /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Accounts.framework/Headers/Accounts.h -o bindings.rs -- -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/
getting an error:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:492:1: error: expected identifier or '('
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:494:9: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:495:9: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:497:19: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:498:44: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:500:19: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:501:53: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:503:19: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:503:50: error: unknown type name 'Protocol'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:504:19: error: unknown type name 'Protocol'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:504:61: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:508:30: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:508:53: error: format argument not an NSString
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:509:31: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:509:63: error: format argument not an NSString
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: error: expected identifier or '('
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:19:63: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:20:19: error: unknown type name 'NSString'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:10:1: error: expected identifier or '('
fatal error: too many errors emitted, stopping now [-ferror-limit=]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:492:1: error: expected identifier or '(', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:494:9: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:495:9: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:497:19: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:498:44: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:500:19: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:501:53: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:503:19: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:503:50: error: unknown type name 'Protocol', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:504:19: error: unknown type name 'Protocol', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:504:61: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:508:30: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:508:53: error: format argument not an NSString, err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:509:31: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:509:63: error: format argument not an NSString, err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: error: expected identifier or '(', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:19:63: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:20:19: error: unknown type name 'NSString', err: true
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:10:1: error: expected identifier or '(', err: true
fatal error: too many errors emitted, stopping now [-ferror-limit=], err: true
Those errors are because the Account.Framework uses objective-c in the headers. I've not worked on the objective-c support in quite a while so I can't say if it'll work. You'll definitely have to add some stuff to the blocklist.
https://rust-lang.github.io/rust-bindgen/objc.html has some words on what it builds.
This is a bit out of date because I think blacklist was renamed to blocklist but here's how (I did it with uikit-sys](https://github.com/simlay/uikit-sys/blob/8b6befb3dff4d5562aad778db32c9c19d0375374/build.rs#L63-L67).
It seems that's failing before trying to use bindgen, actually. Looks like some sort of include path problem, but I'm not an expert on Mac shenanigans :/
Ah, does passing -x objective-c help to the clang flags?
So:
bindgen /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Accounts.framework/Headers/Accounts.h -o bindings.rs -- -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/ -x objective-c
Or somesuch.
working command, but need more editing to build:
$ bindgen x.h --rust-target 1.47 --opaque-type FndrOpaqueInfo --opaque-type HFSCatalogFolder --opaque-type HFSPlusCatalogFolder --opaque-type HFSCatalogFile --opaque-type HFSPlusCatalogFile --no-layout-tests --no-derive-default --generate functions,types,vars,methods,constructors,destructors -- -x objective-c -fblocks -fmodules -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk
content of x.h
@import Accounts;
I got the command from apple-sys build script with command: cargo build --features=Accounts
https://github.com/youknowone/apple-sys
Those errors are because the
Account.Frameworkuses objective-c in the headers. I've not worked on the objective-c support in quite a while so I can't say if it'll work. You'll definitely have to add some stuff to the blocklist.https://rust-lang.github.io/rust-bindgen/objc.html has some words on what it builds.
This is a bit out of date because I think
blacklistwas renamed toblocklistbut here's how (I did it with uikit-sys](https://github.com/simlay/uikit-sys/blob/8b6befb3dff4d5562aad778db32c9c19d0375374/build.rs#L63-L67).
@simlay I'm trying to do this and it keeps saying this:
Caused by:
process didn't exit successfully: `/Users/user/corehaptics-sys/target/debug/build/corehaptics-sys-x/build-script-build` (exit status: 101)
--- stdout
cargo:rustc-link-lib=framework=CoreHaptics
--- stderr
thread 'main' panicked at 'Invalid identifier: Error("expected identifier")', /Users/user/.cargo/registry/src/github.com-x/bindgen-0.64.0/ir/objc.rs:272:30
I copied your code exactly as-is from your blog post aside from changing the sysroot path and UIKit to CoreHaptics. let me know if you want me to upload a gist of the code i'm using.
and yeah, blacklist was renamed to blocklist. I replaced that in the code accordingly too
@duckfromdiscord the issue you describe is not the same as the one discussed here. Yours is a proper bindgen error so I'd recommend you to open a new issue or discussion instead.
Got it