Skip to navigation
Cross compiling Rust from Linux to macOS
05.09.22
Cross compiling Rust from Linux to macOS February 17, 2019 I’ve recently been working on a Rust project at work which requires compiling for Linux (GNU), Linux (musl - for Alpine Linux) and macOS. I use Linux Mint nearly all the time, so building for macOS targets has required asking very nicely to borrow a spare Macbook Air. This is naturally a bit crap, so I set out to find a Linux-only solution to cross compile for macOS using osxcross. A weekend of pain later, and I have the following post. Hopefully it spares you a weekend of your own pain. Environment This process should work in any modern-ish Debian-based environment. This is the setup I used: Linux Mint 19.1, Dell XPS1 15, Intel i9 x64 Rust 1.32.0 (with Rustup) Clang 6.0.0 I’ve also tested this process in CircleCI and it seems to be working fine. The only device I have to test on at time of writing is a Macbook Air with macOS Mojave on it. This process should work for other macOS versions, but is untested. Requirements There are a few system dependencies required to work with osxcross. I don’t think the version requirements are too strict for the packages listed below. You may want to check the osxcross requirements as well if you’re having problems. # Install build dependencies apt install \ clang \ gcc \ g++ \ zlib1g-dev \ libmpc-dev \ libmpfr-dev \ libgmp-dev # Add macOS Rust target rustup target add x86_64-apple-darwin Building osxcross The following process is based on this tutorial on Reddit and some trial and error. I’m using the macOS 10.10 SDK as I had the least problems getting up and running with it. Add the following to a script called osxcross_setup.sh and make it executable. git clone https://github.com/tpoechtrager/osxcross cd osxcross wget -nc https://s3.dockerproject.org/darwin/v2/MacOSX10.10.sdk.tar.xz mv MacOSX10.10.sdk.tar.xz tarballs/ UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh Not a lot to it, thanks to the hard work put in by the osxcross developers. Running ./osxcross_setup.sh should create a folder named osxcross with everything you need in it to cross compile to macOS with Clang. This doesn’t modify $PATH or install any system files, so is useful for CI as well. Append ./build_gcc.sh to osxcross_setup.sh if you want to use GCC to cross compile. Configuring Cargo Cargo needs to be told to use the correct linker for the x86_64-apple-darwin target, so add the following to your project’s .cargo/config file: [target.x86_64-apple-darwin] linker = "x86_64-apple-darwin14-clang" ar = "x86_64-apple-darwin14-ar" If you’ve used a different macOS SDK version, you might need to replace darwin14 with darwin15. To check what binary to use, look in osxcross/target/bin. Building the project Because I chose not to install osxcross at the system level, the $PATH variable must be modified for Cargo to pick up the linker binaries specified previously. The build command changes to: # Add --release to build in release mode PATH="$(pwd)/osxcross/target/bin:$PATH" \ cargo build --target x86_64-apple-darwin This adds [pwd]/osxcross/target/bin to $PATH, which means the linker binaries should get picked up. The path must be absolute to work properly, hence $(pwd). Now you should have a binary in target/x86_64-apple-darwin/[debug|release] which works on macOS!
https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html
Reply
Anonymous
Information Epoch 1732476410
Worse is better.
Home
Notebook
Contact us