AerialX / cargo-build

Experiments with Rust and Emscripten
59 stars 4 forks source link

cargo-build

Builds your Cargo projects for LLVM IR, alternate platforms, and Emscripten.

Running

cargo-build runs just like cargo build, but provides a few additional options:

*NOTE: Using llvm35-ir or any emit modes that depend on it (Emscripten) will require a run of LLVM opt. Make sure you've built with LLVM_PREFIX as described in the section below. The generated `Remove.sofiles must reside in the same directory as thecargo-build` binary.**

Building

cargo build

In order to build cargo-build, you need to use the same rust version as Cargo. multirust makes this easy, the version to install is referenced in src/rustversion.txt. Alternatively, fix your local Cargo for the latest rust (or find a relevant pull request) and override it with .cargo/config

When building, provide a LLVM_PREFIX environment variable to the location of an LLVM 3.5 install prefix if you intent on building for targets like Emscripten.

Alternate Platforms

The --sysroot flag documented above can be used to provide an alternate std and related crates to an application. This is useful for embedded targets and others that may not actually be an LLVM target supported by rust, or to allow for LLVM IR output that will be transformed later.

A lightweight std is provided in rust-rt-minimal for use in these situations. It includes a modified standard library with threads and unwinding disabled for platforms that don't need or support them.

git clone https://github.com/AerialX/rust-rt-minimal.git
cd rust-rt-minimal/
TRIPLE=arch-target-triple
cargo build --release --target $TRIPLE
mkdir -p sysroot/lib/rustlib/$TRIPLE/lib
cp target/$TRIPLE/release/deps/lib*.rlib sysroot/lib/rustlib/$TRIPLE/lib/

Emscripten

Install the incoming branch of Emscripten.

To build a project for Emscripten, you must first compile std as described above. Use the i386-unknown-emscripten triple, which is provided as a flexible target JSON in the rust-rt-minimal repo. Release mode must be used due to metadata compatibility issues with LLVM 3.5.

Then build cargo-build as described above, making sure that you use the LLVM_PREFIX environment variable to include the optimization passes.

Once that is set up, compiling an emscripten project is simply:

cargo-build --sysroot path/to/sysroot --target i386-unknown-emscripten --emit em-html

See here for a sample.

NOTE: rustc currently miscompiles some struct field accesses in a way that may result in incorrect code execution with Emscripten.

See Also

rust-emscripten-passes are used to transform rustc's LLVM IR to 3.5 compatibility. It's automatically pulled in by the build script when building cargo-build.

cargo-emscripten an older approach that this program is loosely based off of.