|
1 | 1 | # Rust Catalyst Example
|
2 | 2 |
|
3 |
| -This is an example that shows how to build a static library supporting Catalyst from Rust. |
| 3 | +This is an example that shows how to build a static library supporting Mac Catalyst (X86_64 and ARM64) from Rust. |
4 | 4 |
|
5 |
| -## Requirements |
6 |
| - |
7 |
| -1. [Xargo](https://github.com/japaric/xargo) |
8 |
| -2. Rust Nightly |
9 |
| -3. Rust Source (after switching to nightly) |
10 |
| -4. Xcode 11+ |
11 | 5 |
|
12 |
| -## Not Required |
13 |
| -- the `XARGO_RUST_SRC` env variable |
14 |
| - |
15 |
| -## Full Local Installation |
16 |
| - |
17 |
| -``` bash |
18 |
| -cargo install xargo |
19 |
| -rustup toolchain install nightly |
20 |
| -rustup toolchain default nightly |
| 6 | +# Usage |
21 | 7 |
|
22 |
| -# Or use a directory override |
23 |
| -rustup override set nightly |
24 |
| -``` |
| 8 | +Until [this PR that adds Catalyst ARM64 support to Rust](https://github.com/rust-lang/rust/pull/77484) is merged, you need to build the Rust compiler. Hence, jump to the end to see how to build and use a custom Rust toolchain based on 77484. |
25 | 9 |
|
26 |
| -## Using It |
| 10 | +This repo contains an Xcode project that sets up everything to include a library written in Rust and call functions in it. The repo is set up to use ARM64 and X86_64 Catalyst. It should compile fine if you select the `My Mac` target. If you select the `Any Mac` target, it requires the above mentioned PR / a custom toolchain. |
27 | 11 |
|
28 |
| -Just do a release build with the correct target |
| 12 | +The Xcode project also contains all the required binaries to try this out immediately. You can just open `XcodeIntegration.xcodeproj` and build. |
29 | 13 |
|
30 |
| -``` bash |
31 |
| -xargo build --target x86_64-apple-ios-macabi --release |
32 |
| -``` |
| 14 | +# Usage with normal nightly Rust |
33 | 15 |
|
34 |
| -## Caveats |
35 | 16 |
|
36 |
| -- Building for other archs (such as the host arch) also requires setting the target. |
37 |
| -- Non-Release builds fail on some targets |
38 |
| -- Lipo can be used to make a fat binary (see below) |
| 17 | +## Requirements |
39 | 18 |
|
40 |
| -## Fat Binary |
| 19 | +1. Rustup / Rust Nightly |
| 20 | +2. Xcode 12 |
41 | 21 |
|
42 |
| -This requires that the correct targets are installed for non-macabi: |
| 22 | +## Full Local Installation |
43 | 23 |
|
44 | 24 | ``` bash
|
| 25 | +rustup toolchain install nightly |
| 26 | +rustup toolchain default nightly |
45 | 27 | rustup target add aarch64-apple-ios
|
46 |
| -``` |
| 28 | +rustup target add x86_64-apple-ios |
47 | 29 |
|
48 |
| -Then the following will generate a fat binary. You can also just call the `make_fat.sh` included in the example project. |
| 30 | +# Or use a directory override |
| 31 | +rustup override set nightly |
49 | 32 |
|
50 |
| -``` bash |
51 |
| -# lipo together the different architectures into a universal 'fat' file |
52 |
| -xargo build --target x86_64-apple-ios-macabi --release |
53 |
| -xargo build --target aarch64-apple-ios --release |
54 |
| -lipo -create -output target/libtest1.a target/{x86_64-apple-darwin,aarch64-apple-ios,x86_64-apple-darwin}/release/libtest1.a |
| 33 | +./make_fat.sh |
55 | 34 | ```
|
56 | 35 |
|
57 |
| -Note that we're not including `x86_64-apple-darwin` because a fat binary cannot contain darwing x86_64 and iOS x86_64 together. |
| 36 | +It will automatically generate the binaries and write them to `XcodeIntegration/Rust` |
58 | 37 |
|
59 |
| -# How it works |
| 38 | +Open Xcode, hit compile. |
60 | 39 |
|
61 |
| -Tx86_64-he `x86_64-apple-ios-macabi.json` file contains the information that `Xargo` needs to build a custom sysroot to compile your project with. |
62 |
| -A sysroot is the `libstd`, `libcore` and so on. |
| 40 | +### build-std |
63 | 41 |
|
64 |
| -## Cargo.toml |
| 42 | +Catalyst is not a tier 1 Rust platform, so there is no pre-build standard library. This repository uses `cargo build-std` to automatically build the standard library for the given platform. This takes a bit longer but works reliably (at least for me). |
65 | 43 |
|
66 |
| -The panic line: |
| 44 | +## Custom Rust Toolchain |
67 | 45 |
|
68 |
| -``` toml |
69 |
| -[profile.release] |
70 |
| -panic = "abort" |
| 46 | +``` |
| 47 | +git clone https://github.com/rust-lang/rust.git |
| 48 | +cd rust |
| 49 | +cp config.toml.sample config.toml |
71 | 50 | ```
|
72 | 51 |
|
73 |
| -Seems to be required as `panic_unwind` leads to a failing build. |
74 |
| - |
75 |
| -## Xargo.toml |
| 52 | +Edit config.toml and change `build-stage = 1` to `build-stage = 2` (line 149) |
| 53 | + |
| 54 | +``` |
| 55 | +./x.py build |
| 56 | +``` |
| 57 | + |
| 58 | +Install with |
| 59 | +``` |
| 60 | +rustup toolchain link myrust ~/rust/build/x86_64-apple-darwin/stage2/ |
| 61 | +``` |
76 | 62 |
|
77 |
| -Just copy the contents verbatim. Otherwise it fails to build `libstd`. |
| 63 | +Go back to this directory |
| 64 | +``` |
| 65 | +rustup default myrust |
| 66 | +``` |
78 | 67 |
|
79 |
| -``` toml |
80 |
| -std = {features = ["jemalloc"]} |
| 68 | +Now you can continue with (Full Local Installation) above. |
81 | 69 |
|
82 |
| -[dependencies] |
83 |
| -std = {} |
84 |
| -``` |
0 commit comments