Skip to content

Commit 8ef2b23

Browse files
committed
Add a bit of documentation on cross-compiling
1 parent fbfbf32 commit 8ef2b23

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Cross-compiling from Linux/Windows
2+
3+
When compiling for e.g. iOS, you're always doing cross-compilation, since the host platform (macOS) is different from the target platform (iOS). This means that Apple's tooling generally has pretty good support for cross-compilation.
4+
5+
To cross-compile Rust applications, you need a few things:
6+
1. A cross-compiler.
7+
- Rust is a cross-compiler by default, though you'll need to install the standard library for [the relevant target](https://doc.rust-lang.org/rustc/platform-support.html) via. `rustup target add $TARGET`, or use [Cargo's unstable `-Zbuild-std`](https://doc.rust-lang.org/cargo/reference/unstable.html#build-std).
8+
2. A cross-linker.
9+
- [`ld64`](https://github.com/apple-oss-distributions/ld64) is Apple's native linker. It used to be open source, though the new implementation [hasn't been open sourced](https://developer.apple.com/forums/thread/749558).
10+
- [LLVM's `lld`](https://lld.llvm.org/) is a fairly good alternative, it comes bundled with Rust as `rust-lld`.
11+
3. Linker stubs (`*.tbd` files).
12+
- Used to figure out which dynamic system library each symbol comes from.
13+
- These stubs are located inside the SDK.
14+
4. If compiling C code using e.g. `cc-rs`, you will need:
15+
- A C cross-compiler (Clang is a good choice here).
16+
- C headers. Also located inside the SDK.
17+
18+
In the end, you should end up with something like the following:
19+
```console
20+
$ rustup target add aarch64-apple-darwin
21+
$ export CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=rust-lld
22+
$ export SDKROOT=$(pwd)/MacOSX.sdk
23+
$ export CC=clang CXX=clang++ AR=llvm-ar # If compiling C code
24+
$ cargo build --target aarch64-apple-darwin
25+
```
26+
27+
For more details, see [Rust Cross](https://github.com/japaric/rust-cross), this has a good explanation of how cross-compiling Rust works in general. [OSXCross](https://github.com/tpoechtrager/osxcross) is also a good resource, it outlines how to create a complete compiler toolchain (including `ld64`).
28+
29+
## SDK
30+
31+
The required `MacOSX.sdk`, `iPhoneOS.sdk` etc. are bundled with Xcode. Please make sure to review [the Xcode SLA](https://www.apple.com/legal/sla/docs/xcode.pdf), especially the sections regarding required use on Apple-branded computers.
32+
33+
Once you've done that, you can [download Xcode here](https://developer.apple.com/download/all/?q=xcode) (requires logging in with an Apple ID). If you only need to cross-compile to macOS, downloading just the Command Line Tools for Xcode will suffice.
34+
35+
Once downloaded, you need to extract and find the `MacOSX.sdk` folder, and pass that to `rustc` using the `SDKROOT` environment variable.
36+
37+
It may also be possible to find these SDKs elsewhere on the web by searching for e.g. "MacOSX.sdk", though we will not endorse any such links here, as the legality of such a re-distribution is a grey area.

crates/objc2/src/topics/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub mod alternatives {}
88
#[cfg(not(feature = "gnustep-1-7"))]
99
#[doc = include_str!("crate_interop.md")]
1010
pub mod crate_interop {}
11+
#[doc = include_str!("cross_compiling.md")]
12+
pub mod cross_compiling {}
1113
#[doc = include_str!("kvo.md")]
1214
pub mod kvo {}
1315
#[doc = include_str!("layered_safety.md")]

0 commit comments

Comments
 (0)