Skip to content

Compiling Rust 1.49 (and up)

Niels Sascha Reedijk edited this page Jun 18, 2021 · 5 revisions

Cross-compiling Rust for Haiku

The best (and maybe only) way to compile Rust for Haiku is to cross-compile. This page documents the way to prepare a cross-compilation environment, so that this can be repeated by other users.

Note: the changes with the previous build environment is:

  • There is a new config.toml that adds support for wasm.

Setting up the cross-compilation environment

  • The basis for cross-compiling Rust is running Haiku R1 Beta 2.
  • These instructions are based on jessicah's work on dockerizing Haiku builds. See this post

Get the source

Start in a work dir, which we will call WORKDIR here (for me it is /mnt/d/Haiku/)

git clone https://review.haiku-os.org/haiku.git
cd haiku
git checkout r1beta2
cd ..
git clone https://review.haiku-os.org/buildtools.git
git checkout btrev43155 # I use this revision

cd .. #back in the work dir

Build the cross-compiler and haiku for x86

mkdir build-r1beta2-x86 && cd build-r1beta2-x86
../haiku/configure --build-cross-tools x86_gcc2 ../buildtools --build-cross-tools x86
jam -q haiku.hpkg haiku_devel.hpkg '<build>package' haiku_x86.hpkg haiku_x86_devel.hpkg
cd .. #back in the work dir

Build the cross-compiler and haiku for x86_64

mkdir build-r1beta2-x86_64 && cd build-r1beta2-x86_64
../haiku/configure --build-cross-tools x86_64 ../buildtools
jam -q haiku.hpkg haiku_devel.hpkg '<build>package'
cd .. #back in the work dir

Extract the package tool and install it locally

We also extract the package tool and place it in the $HOME/bin directory (so it can be found in path) including libraries that depend on it. On my system the search path does not include the place of the executable, so I do need to explicitly call LD_LIBARY_PATH when running package below.

cp build-r1beta2-x86/objects/linux/x86_64/release/tools/package/package ~/bin/
cp build-r1beta2-x86/objects/linux/lib/* ~/bin

To be honest, I had trouble running the r1beta2 package tool on my system. The package tool complained that Error: Failed to create attribute "BEOS:TYPE" of file "bin": Unknown error -1 on my WSL-based Ubuntu installation. I had to revert back to the R1 beta 1 package tool.

Set up the x86 sysroot

The compiler finds the Haiku files in the sysroot. The sysroot is hardcoded to be the sysroot directory in the place where the compiled cross-tools can be found. I chose to put the packages in a different location, and link these into the sysroot. This is a stylistic choice: the packages could be unpacked directly under build-r1beta2-x86/cross-tools-x86/sysroot/boot.

We also create links to the gcc x86 (not x86_gcc2!) toolset in ~/bin

# create dirs
mkdir build-r1beta2-x86/cross-tools-x86/sysroot
mkdir build-r1beta2-x86/cross-tools-x86/sysroot/boot
mkdir system-r1beta2-x86
ln -s `pwd`/system-r1beta2-x86 build-r1beta2-x86/cross-tools-x86/sysroot/boot/system

# create links to the compiler
find `pwd`/build-r1beta2-x86/cross-tools-x86/bin/ -type f -exec ln -s {} ~/bin/ \;

# get additional required packages
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_gcc2/current/packages/openssl_x86-1.1.1e-1-x86_gcc2.hpkg -P build-r1beta2-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_gcc2/current/packages/openssl_x86_devel-1.1.1e-1-x86_gcc2.hpkg -P build-r1beta2-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_gcc2/current/packages/curl_x86-7.69.1-2-x86_gcc2.hpkg -P build-r1beta2-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_gcc2/current/packages/curl_x86_devel-7.69.1-2-x86_gcc2.hpkg -P build-r1beta2-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_gcc2/current/packages/nghttp2_x86-1.40.0-1-x86_gcc2.hpkg -P build-r1beta2-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_gcc2/current/packages/nghttp2_x86_devel-1.40.0-1-x86_gcc2.hpkg -P build-r1beta2-x86/download

# extract packages
LD_LIBRARY_PATH=~/bin package extract -C system-r1beta2-x86/ build-r1beta2-x86/objects/haiku/x86_gcc2/packaging/packages/haiku.hpkg
LD_LIBRARY_PATH=~/bin package extract -C system-r1beta2-x86/ build-r1beta2-x86/objects/haiku/x86_gcc2/packaging/packages/haiku_devel.hpkg
LD_LIBRARY_PATH=~/bin package extract -C system-r1beta2-x86/ build-r1beta2-x86/objects/haiku/x86_gcc2/packaging/packages/haiku_x86.hpkg
LD_LIBRARY_PATH=~/bin package extract -C system-r1beta2-x86/ build-r1beta2-x86/objects/haiku/x86_gcc2/packaging/packages/haiku_x86_devel.hpkg
LD_LIBRARY_PATH=~/bin find build-r1beta2-x86/download/ -name "*.hpkg" -exec package extract -C system-r1beta2-x86/ {} \;

# Fix libgcc_s and other libraries so we can link to it
cd system-r1beta2-x86/develop/lib/x86
ln -s ../../../lib/x86/libgcc_s.so libgcc_s.so
ln -s ../../../lib/x86/libgcc_s.so.1 libgcc_s.so.1
rm libroot.so && ln -s ../../../lib/x86/libroot.so  libroot.so
rm libbsd.so && ln -s ../../../lib/x86/libbsd.so libbsd.so
rm libnetwork.so && ln -s ../../../lib/x86/libnetwork.so libnetwork.so

# return to the start dir
cd ../../../..

Set up the x86_64 sysroot

Like the previous sysroot

# create dirs
mkdir build-r1beta2-x86_64/cross-tools-x86_64/sysroot
mkdir build-r1beta2-x86_64/cross-tools-x86_64/sysroot/boot
mkdir system-r1beta2-x86_64
ln -s `pwd`/system-r1beta2-x86_64 build-r1beta2-x86_64/cross-tools-x86_64/sysroot/boot/system

# create links to the compiler
find `pwd`/build-r1beta2-x86_64/cross-tools-x86_64/bin/ -type f -exec ln -s {} ~/bin/ \;

# get additional required packages
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/openssl-1.1.1e-1-x86_64.hpkg -P build-r1beta2-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/openssl_devel-1.1.1e-1-x86_64.hpkg -P build-r1beta2-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/curl-7.69.1-2-x86_64.hpkg -P build-r1beta2-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/curl_devel-7.69.1-2-x86_64.hpkg -P build-r1beta2-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/nghttp2-1.40.0-1-x86_64.hpkg -P build-r1beta2-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/nghttp2_devel-1.40.0-1-x86_64.hpkg -P build-r1beta2-x86_64/download/

# extract packages
LD_LIBRARY_PATH=~/bin package extract -C system-r1beta2-x86_64/ build-r1beta2-x86_64/objects/haiku/x86_64/packaging/packages/haiku.hpkg
LD_LIBRARY_PATH=~/bin package extract -C system-r1beta2-x86_64/ build-r1beta2-x86_64/objects/haiku/x86_64/packaging/packages/haiku_devel.hpkg
LD_LIBRARY_PATH=~/bin find build-r1beta2-x86_64/download/ -name "*.hpkg" -exec package extract -C system-r1beta2-x86_64/ {} \;

# Fix libgcc_s so we can link to it
cd system-r1beta2-x86_64/develop/lib/
ln -s ../../lib/libgcc_s.so libgcc_s.so
rm libroot.so && ln -s ../../lib/libroot.so libroot.so
rm libbsd.so && ln -s ../../lib/libbsd.so libbsd.so
rm libnetwork.so && ln -s ../../lib/libnetwork.so libnetwork.so

# return to the start dir
cd ../../..

Getting the Rust source

Get the modified Rust repository from github.

git clone https://github.com/nielx/rust rust-haiku
cd rust-haiku
git checkout rust-haiku-1.45.0 # see other options below

Available tags and branches for this configuration:

  • rust-haiku-1.49.0
  • rust-haiku-1.50.0
  • rust-haiku-1.51.0
  • rust-haiku-1.52.0
  • rust-haiku-1.53.0

Configuration

Place the following configuration as config.toml in the root of the Rust source.

changelog-seen = 2

[llvm]
targets = "WebAssembly;X86"

[build]
host = ["i686-unknown-haiku", "x86_64-unknown-haiku"]
target = ["i686-unknown-haiku", "x86_64-unknown-haiku", "wasm32-unknown-unknown"]
extended = true
verbose = 2

[install]
prefix = "/boot/system"

[rust]
channel = "stable"
rpath = false
deny-warnings = false

[target.i686-unknown-haiku]
cc = "i586-pc-haiku-gcc"
cxx = "i586-pc-haiku-g++"
ar = "i586-pc-haiku-ar"
linker = "i586-pc-haiku-gcc"

[target.x86_64-unknown-haiku]
cc = "x86_64-unknown-haiku-gcc"
cxx = "x86_64-unknown-haiku-g++"
ar = "x86_64-unknown-haiku-ar"
linker = "x86_64-unknown-haiku-gcc"

[dist]
missing-tools = true

Building Rust

After all the preparations it is time to build rust. For a full overview of Rust build instructions, see the Readme's and the source. This page will show the specifics needed for cross-compilation.

The customized environment variables needed to build are:

  • *_OPENSSL_LIB_DIR needs to point to the location of the openssl libraries within our Haiku build system for each platform.
  • *_OPENSSL_INCLUDE_DIR needs to point to the location of the openssl headers within our Haiku build system for each platform.

That makes my invocation for the x.py script look as follows:

I686_UNKNOWN_HAIKU_OPENSSL_LIB_DIR=/mnt/d/Haiku/system-r1beta2-x86/develop/lib/x86 \
I686_UNKNOWN_HAIKU_OPENSSL_INCLUDE_DIR=/mnt/d/Haiku/system-r1beta2-x86/develop/headers/ \
X86_64_UNKNOWN_HAIKU_OPENSSL_LIB_DIR=/mnt/d/Haiku/system-r1beta2-x86_64/develop/lib/ \
X86_64_UNKNOWN_HAIKU_OPENSSL_INCLUDE_DIR=/mnt/d/Haiku/system-r1beta2-x86_64/develop/headers/ \
./x.py dist