Skip to content

Instantly share code, notes, and snippets.

@drujensen
Last active March 9, 2024 17:56
Show Gist options
  • Save drujensen/d756e7b6dfaefcbec12eaaed6ce909ba to your computer and use it in GitHub Desktop.
Save drujensen/d756e7b6dfaefcbec12eaaed6ce909ba to your computer and use it in GitHub Desktop.
Cross Compile Crystal

Cross compile Crystal

These are instructions to cross compile crystal compiler on Ubuntu Server 20-04 on an ARM processor. This was tested on a Raspberry Pi 4 - 4Gb RAM.

From an Ubuntu 20-04 instance, first compile crystal from source. You will need to install crystal to compile crystal.

You can find the instructions here:

https://crystal-lang.org/reference/installation/on_debian_and_ubuntu.html

https://crystal-lang.org/reference/installation/from_source_repository.html

Next, you will need to cross-compile for the armv6 processor:

32 bit OS:

./bin/crystal build -D without_openssl -D without_zlib -o crystal src/compiler/crystal.cr --release --cross-compile --target "armv6-linux-gnueabihf"

64 bit OS:

./bin/crystal build -D without_openssl -D without_zlib -o crystal src/compiler/crystal.cr --release --cross-compile --target "aarch64-linux-gnu"

This will generate a crystal.o file. Secure copy this file to the raspberry pi:

scp crystal.o pi@ubuntu.local:workspace/crystal/.

Now on the raspberry pi, you will need to compile and build crystal from source.

Follow these instructions on the raspberry pi:

https://crystal-lang.org/reference/installation/from_source_repository.html

This will fail when it tries to compile crystal because crystal doesn't exist, but the src/llvm/ext/llvm_ext.o and the src/ext/libcrystal.a files will be created.

cc 'crystal.o' -o 'bin/crystal' -rdynamic src/llvm/ext/llvm_ext.o `/usr/bin/llvm-config-8 --libs --system-libs --ldflags 2> /dev/null` -lstdc++ -lpcre -lm -lgc -lpthread -levent -lrt -ldl src/ext/libcrystal.a -L/usr/lib -L/usr/local/lib

This should generate a crystal binary. link this into /usr/bin directory with:

ln -sf /home/ubuntu/workspace/crystal/bin/crystal /usr/bin/crystal

Add CRYSTAL_PATH environment to your .bash_profile:

export CRYSTAL_PATH=/home/ubuntu/workspace/crystal/src

verify everything is working crystal --version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment