DEV Community

Carl Hörberg
Carl Hörberg

Posted on • Updated on

Cross compile Crystal to aarch64

Here's an example of how to cross compile a crystal app on a host machine and then transfering it to the target machine (in this case an Ubuntu aarch64/arm64) and compile the last bits there:


bash
#!/bin/bash -eu

srcfile=$1
target=$2

export CFLAGS="-fPIC"
buildcmd=$(crystal build --cross-compile --target=aarch64-unknown-linux-gnu --release $srcfile)

# Upload the object file to the aarch64 machine
scp "$(basename $srcfile .cr).o" "$target":.
rm "$(basename $srcfile .cr).o"

# SSH to the target machine
ssh "$target" << EOF
# install dependencies
sudo apt-get install -y clang libssl-dev libpcre3-dev libgc-dev libevent-dev zlib1g-dev

# download and compile crystal's sigfault library
wget https://raw.githubusercontent.com/crystal-lang/crystal/master/src/ext/sigfault.c
cc -c -o sigfault.o sigfault.c
ar -rcs libcrystal.a sigfault.o
sudo mkdir -p /usr/share/crystal/src/ext
sudo cp libcrystal.a /usr/share/crystal/src/ext/

# compile the object file with the command crystal build --cross-compile gave us
$buildcmd
EOF
Enter fullscreen mode Exit fullscreen mode

Top comments (0)