- I package my prototype as a Python script with a bunch of dependencies into a simple command line tool.
- I send the package over to a collaborator.
- Collaborator is missing roughly half of the dependencies.
- I debate teaching collaborator about virtual environments or anaconda. Meanwhile, collaborator is busy typing things like
sudo pip install randompackageinto their terminal.
- Collaborator says, "it's still not working". Turns out collaborator only has Python 2.7 but the script is meant for Python 3.
- The process repeats until we get the script running.
And it turns out that it's not nearly as hard as I expected! Most of the key ideas have already been fully explained by Brian Pearce in this post. Brian cross compiles from a Vagrant Ubuntu build environment running on an OS X host to an OS X binary. But Brian's post isn't quite enough for the devoted Linux user -- it still assumes you're running a copy of OS X somewhere (namely on the Vagrant host).
Below, I give a complete account of how to cross compile a Rust library from Ubuntu 14.04 for an OS X target. I emphasize again that most of the ideas are not mine but come from Brian's post as well as from the work of the osxcross project. I've just added a few details to complete the build without having to run OS X anywhere.
PrerequisitesMake sure you've got the following dependencies installed directly from apt-get:
sudo apt-get update sudo apt-get install clang autotools-dev automake cmake libfuse-devNote, a difference from Brian's post is that we require
libfuse. This allows osxcross to work directly with the necessary Xcode disk image so that we need only have the Xcode image and not a full OS X system to build the OS X SDK. Next, install rustup:
curl https://sh.rustup.rs -sSf | shYou'll then want to run (if this is the first time using rustup):
source .cargo/envNext clone the osxcross project:
git clone https://github.com/tpoechtrager/osxcrossFinally, download version 7.3 of Xcode. For this you'll need to signin in with any valid Apple ID. You want to obtain a
Bootstrapping the OS X cross compile toolchainFirst, we'll use osxcross to build an OS X SDK package for cross compilation. Change into the osxcross root directory and run:
./tools/gen_sdk_package_p7zip.sh Xcode_7.3.1.dmgproviding the full path to your Xcode
.dmgfile. If all goes well, you osxcross will have built a packaged OS X SDK into the root of the osxcross project. Move this package into the tarballs directory:
mv MacOSX10.11.sdk.tar.xz tarballs/Now we build osxcross:
OSX_VERSION_MIN=10.7 ./build.shThis creates the full OS X cross compiler tool chain, allowing us to compile Rust targets just like we would on a native OS X system. To make the toolchain available, you need to add it to your path. For this, you might consider edit your
# this line was automatically added to .profile by rustup: export PATH="$HOME/.cargo/bin:$PATH"to:
Cross compilingNow we are almost ready to cross compile. Change into your Rust project directory and use Rustup to configure an OS X target:
rustup target add x86_64-apple-darwinFinally, we need to tell
rustcwhere to find the linker. Add or edit a
.cargo/configfile to the root of your Rust project to contain the following line:
[target.x86_64-apple-darwin] linker = "x86_64-apple-darwin15-clang"We can then cross compile our Rust code with:
cargo build --release --target=x86_64-apple-darwinYour shiny new binary can be found under
Hooray and happy sharing!