Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compile Rust tests with cargo for the iOS simulator
Prerequisites:
- rustup target install x86_64-apple-ios
Steps:
DOCUMENTATION ON COMPILING FOR IOS SIMULATOR
1) Create a shim to the cross-compiling linker for the simulator:
xcrun --sdk iphonesimulator --show-sdk-path
a) With the given path, create a shim called `linker_shim.sh`:
#!/bin/sh
cc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.2.sdk "$@"
b) Make it executable:
chmod 777 linker_shim.sh
2) Set an environment variable so that cargo doesn't use the default linker:
export CARGO_TARGET_X86_64_APPLE_IOS_LINKER=~/<path>/linker_shim.sh
3) Compile tests:
cargo test --no-run --target=x86_64-apple-ios
4) Wrap as an app:
a) Copy executable to a separate empty directory.
mkdir app
cp target/x86_64-apple-ios/debug/hello-a254f99d8b1ee1bc app/hello
b) Create an Info.plist file with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>hello</string>
<key>CFBundleIdentifier</key>
<string>com.rust.tests</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>x86_64</string>
</array>
</dict>
</plist>
5) Start the iOS simulator:
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app
6) Install app:
xcrun simctl install booted app/
7) Running the app and seeing output.
There are several alternatives here. Since we're running on the simulator, we can just do this:
xcrun simctl launch --console booted com.rust.tests
We could also use LLDB:
Get the path of the app on the simulator:
xcrun simctl get_app_container booted com.rust.tests
Now use LLDB to run the binary:
lldb
platform select ios-simulator
platform connect <device-id>
target create /Users/<user>/Library/Developer/CoreSimulator/Devices/<device-id>/data/Containers/Bundle/Application/<app-id>/app.app/hello
run
SOURCE: Dinghy source, StackOverflow, other articles found through Google.
ISSUES:
Why can't we pass the linker when compiling the tests? Or why isn't the target doing this? Is there a way to do this? This doesn't seem to be well documented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.