Skip to content

Compiling Rust 1.54 (and up)

Niels Sascha Reedijk edited this page Jul 2, 2022 · 8 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:

  • The cross-compilation environment upgraded to Haiku R1 Beta 3

Setting up the cross-compilation environment

  • The basis for cross-compiling Rust is running Haiku R1 Beta 3.
  • 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 r1beta3
cd ..
git clone https://review.haiku-os.org/buildtools.git
git checkout btrev43166 # This revision corresponds to the r1beta3 branch

cd .. #back in the work dir

Build the cross-compiler and haiku for x86

mkdir build-r1beta3-x86 && cd build-r1beta3-x86
../haiku/configure --build-cross-tools x86_gcc2 --build-cross-tools x86 --cross-tools-source ../buildtools
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-r1beta3-x86_64 && cd build-r1beta3-x86_64
../haiku/configure --build-cross-tools x86_64 --cross-tools-source ../buildtools
jam -q haiku.hpkg haiku_devel.hpkg
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-r1beta3-x86/objects/linux/x86_64/release/tools/package/package ~/bin/
cp build-r1beta3-x86/objects/linux/lib/* ~/bin

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-r1beta3-x86/cross-tools-x86/sysroot/boot.

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

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

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

# get additional required packages
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_gcc2/current/packages/openssl_x86-1.1.1k-1-x86_gcc2.hpkg -P build-r1beta3-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_gcc2/current/packages/openssl_x86_devel-1.1.1k-1-x86_gcc2.hpkg -P build-r1beta3-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_gcc2/current/packages/curl_x86-7.76.1-1-x86_gcc2.hpkg -P build-r1beta3-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_gcc2/current/packages/curl_x86_devel-7.76.1-1-x86_gcc2.hpkg -P build-r1beta3-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_gcc2/current/packages/nghttp2_x86-1.43.0-1-x86_gcc2.hpkg -P build-r1beta3-x86/download
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_gcc2/current/packages/nghttp2_x86_devel-1.43.0-1-x86_gcc2.hpkg -P build-r1beta3-x86/download

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

# Fix libgcc_s and other libraries so we can link to it
cd system-r1beta3-x86/develop/lib/x86
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-r1beta3-x86_64/cross-tools-x86_64/sysroot
mkdir build-r1beta3-x86_64/cross-tools-x86_64/sysroot/boot
mkdir system-r1beta3-x86_64
ln -s `pwd`/system-r1beta3-x86_64 build-r1beta3-x86_64/cross-tools-x86_64/sysroot/boot/system

# create links to the compiler
find `pwd`/build-r1beta3-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/r1beta3/x86_64/current/packages/openssl-1.1.1k-1-x86_64.hpkg -P build-r1beta3-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_64/current/packages/openssl_devel-1.1.1k-1-x86_64.hpkg -P build-r1beta3-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_64/current/packages/curl-7.76.1-1-x86_64.hpkg -P build-r1beta3-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_64/current/packages/curl_devel-7.76.1-1-x86_64.hpkg -P build-r1beta3-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_64/current/packages/nghttp2-1.43.0-1-x86_64.hpkg -P build-r1beta3-x86_64/download/
wget https://eu.hpkg.haiku-os.org/haikuports/r1beta3/x86_64/current/packages/nghttp2_devel-1.43.0-1-x86_64.hpkg -P build-r1beta3-x86_64/download/

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

# Fix libgcc_s so we can link to it
cd system-r1beta3-x86_64/develop/lib/
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 Rust repository from github. Up to Rust 1.55.0, you would always have to use the customized Rust repository. By default this checks out the haiku-stable branch, which should contain the latest Rust release with Haiku-specific patches. Since 1.56.0, you can use the vanilla source and check out the version you want to build.

git clone https://github.com/nielx/rust rust-haiku
cd rust-haiku

Available tags and branches for this configuration:

  • rust-haiku-1.54.0
  • rust-haiku-1.55.0
  • 1.56.0
  • 1.57.0
  • 1.58.0
  • 1.59.0
  • 1.60.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"
lld = true
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
compression-formats = ["xz"]

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-r1beta3-x86/develop/lib/x86 \
I686_UNKNOWN_HAIKU_OPENSSL_INCLUDE_DIR=/mnt/d/Haiku/system-r1beta3-x86/develop/headers/ \
X86_64_UNKNOWN_HAIKU_OPENSSL_LIB_DIR=/mnt/d/Haiku/system-r1beta3-x86_64/develop/lib/ \
X86_64_UNKNOWN_HAIKU_OPENSSL_INCLUDE_DIR=/mnt/d/Haiku/system-r1beta3-x86_64/develop/headers/ \
./x.py dist