Porting Rust to DragonFly

During the last week I spent numerous hours on porting my favorite programming language Rust to my favorite operating system DragonFly BSD. While my first attempt months earlier failed, this time I was successful!

In this article I go through all the problems I had to deal with and how you can build Rust for DragonFly yourself. This might also be of interest to people who want to port Rust to further platforms like NetBSD or OpenBSD.

You find the scripts needed to compile Rust on DragonFly here and my DragonFly branch of Rust here (hopefully to be merged soon).

How to port Rust to a new platform (as of 2014)

In short, what needs to be done to port Rust to a new platform, is the following:

Once this is done we are ready to cross-compile Rust to DragonFly.

Cross-compiling Rust to DragonFly

After trying to cross-compile Rust by specifying --target x86_64-pc-dragonfly-elf to Rust’s own configure script and spending numerous hours just to note that the build fails, I gave up on this idea. Instead I took a different approach as shown below. Note that this all requires my dragonfly branch of rust. All stages depend sequentially on another. Below we are using the scripts from my rust-cross-dragonfly project.

Stage 1 (Linux, DragonFly)

Use scripts stage1-linux.sh and stage1-dragonfly.sh respectively.

Actually I tried to cross-compile everything on Linux but it failed for LLVM. So I decided to build everything that involved compiling C or C++ on the target (DragonFly) itself.

Stage 2 (Linux)

We have to copy the outcome of stage1-dragonfly (which is stage1-dragonfly.tgz) to the Linux machine and extract it to create stage1-dragonfly/. This includes the C libraries required for Rust as well as some system libraries.

Stage2 (sh stage2-linux.sh) cross-compiles all of Rust’s libraries (e.g. libstd, libsyntax, librustc) and generates the object file for the rustc binary (driver.o). I don’t generate the executable here itself, as static initializations were omitted and the generated rustc was unable to find statically registered command line options of LLVM. I spent hours to figure this out! The solution was to just generate the object file for rustc and together with the Rust libraries pass it on to Stage3, which links the rustc binary on DragonFly.

Copy the outcome of Stage2 (stage2-linux.tgz) back to the DragonFly system.

Stage 3 (DragonFly)

Finally back again on DragonFly. Extract stage2-linux.tgz to become stage2-linux. In this stage we will construct a working rustc binary (the Rust compiler) and test to compile a simple Hello World application hw.rs natively on DragonFly.

All you have to do is to execute sh stage3-dragonfly.sh.

Stage 4 (DragonFly)

Stage4 is the last stage and it builds my dragonfly branch with the compiler from Stage3. It uses Rust’s own build infrastructure and not my “hacked” build infrastructure.

All you have to do is to execute sh stage4-dragonfly.sh and wait. It does not automatically install Rust for you so you have to sudo gmake install inside directory stage4-dragonfly/rust yourself.

Problems faced

Conclusion

Rust works on DragonFly, yippey! I work on uploading a snapshot and maybe setting up a regular build and add it to dports. Hopefully this document also helps others in porting Rust to further platforms, mainly OpenBSD and NetBSD.