Crystal & Alpine on the Raspberry Pi
Bringing together two interesting technologies:
- Alpine Linux – a small-size and security oriented Linux distribution
- Crystal – a compiled language with a nice syntax (similar to Ruby)
- on the Raspberry Pi (armhf / ARMv6, which would also support the Pi Zero W)
is not possible currently, as of 21.12.2018.
There is no (at least no easy) way to cross-compile Crystal applications for the Raspberry Pi and Alpine Linux, from, for instance, x86 platforms, as the necessary target arm-linux-musleabihf is not supported yet.
Misc background information
Unfortunately it’s not possible to compile crystal on Alpine/ARM/Raspberry using gcc or the like, as “to compile Crystal you need Crystal” – the compilation will fail:
pitest:/usr/local/crystal/crystal# make
Using /usr/bin/llvm-config [version=5.0.1]
g++ -c -o src/llvm/ext/llvm_ext.o src/llvm/ext/llvm_ext.cc -I/usr/lib/llvm5/include -Os -founused-parameter
-Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-fdata-sections -Os
-fomit-frame-pointer -Os -DNDEBUG -fno-exceptions -DLLVM_BUILD_GLOBAL_ISD__STDC_LIMIT_MACROS
cc -fPIC -c -o src/ext/sigfault.o src/ext/sigfault.c
ar -rcs src/ext/libcrystal.a src/ext/sigfault.o
CRYSTAL_CONFIG_PATH=”/usr/local/crystal/crystal/src” CRYSTAL_CONFIG_BUILD_COMMIT=”74c65ce6b” lib
You need to have a crystal executable in your path!
make: *** [Makefile:123: .build/crystal] Error 1
The idea is that you crosscompile crystal on another architecture, and then can use it to natively compile it.
Alpine Linux uses musl, an alternative libc.
To find out the name of the target (LLVM target triple), you can execute llvm-config on the target system, like this:
llvm-config –host-target
To be able to run this command on Alpine, you install two packages:
apk add llvm
apk add llvm-dev
When trying a target which Crystal does not support yet for crosscompilation, you will get this error message:
in /usr/share/crystal/src/callstack.cr:3: while requiring “c/dlfcn”: can’t find file ‘c/dlfcn’
These libraries are located here:
/usr/share/crystal/src/lib_c
Crystal is supported on Alpine, on the aarch64 (ARM 64bit) and x86_64 platforms. (and x86 via portalier.com)
In general, crosscompiling with Crystal works like this:
crystal build –release –cross-compile –target “aarch64-linux-musl” src/armtest.cr
it will give you a command line to execute on the target machine:
cc ‘armtest.o’ -o ‘armtest’ -rdynamic -lpcre -lgc -lpthread /usr/share/crystal/src/ext/libcrystal.a -levent -lrt -L/usr/lib -L/usr/local/lib
See these references:
- https://github.com/crystal-lang/crystal/issues/4391
- https://crystal-lang.org/docs/installation/from_source_repository.html
- https://irclog.whitequark.org/crystal-lang/2017-08-12
- https://github.com/TheKernelCorp/NuummiteOS (proof of concept OS kernel written in Crystal)
- https://crystal-lang.org/docs/installation/on_alpine_linux.html
- http://public.portalier.com/alpine/ – packages for x86 as well
- https://github.com/ysbaddaden/crystal-alpine
- https://wiki.musl-libc.org/supported-platforms.html
- https://github.com/ysbaddaden/crystal-alpine/issues/6
- https://github.com/crystal-lang/crystal/pull/5862 <- issue about arm-linux-musleabihf
- https://github.com/crystal-lang/crystal/issues/5467
- https://pkgs.alpinelinux.org/packages?name=crystal&branch=edge <- packages by Jakub Jirutka for Crystal, for Alpine. He tried to build it for armhf as well, but did not succeed
- https://elinux.org/images/e/eb/Transitioning_From_uclibc_to_musl_for_Embedded_Development.pdf <- interesting background information about musl