|
| 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. |
0 commit comments