Cross-compiling for Linux
radlang build --target linux-x86_64 builds an x86_64 Linux (glibc)
executable from any host, for example an Apple Silicon Mac. radlang compiles
the program and its namespace libraries to x86_64 ELF objects itself, using
the LLVM built into the compiler. Linking needs two things from outside
radlang: the target’s C headers and libraries (a sysroot) and an ELF
linker.
radlang build --target linux-x86_64 --sysroot ~/sysroots/debian-bookworm-x86_64
# -> output/release/linux-x86_64/<project.name> Command line and environment
| Flag / variable | Meaning |
|---|---|
--target linux-x86_64 |
Cross-compile for x86_64-unknown-linux-gnu. The default output is {build}/release/linux-x86_64/<name>, where <name> is project.name or the source file’s stem. -o/--output overrides it. |
--sysroot <dir> / RADLANG_LINUX_SYSROOT / build.linux-x86_64.sysroot |
The Linux sysroot to compile the runtime against and link with, in that precedence order. In config.yaml ~ expands and relative paths resolve from the project root. |
RADLANG_LINUX_LINKER / build.linux-x86_64.linker |
The linker to use: a path to (or name on PATH of) ld.lld or zig; the variable overrides the config. Without either radlang uses ld.lld (also ld.lld-20, ld.lld-19 and Homebrew’s lld/lld@20) when a sysroot is given, otherwise zig from PATH. |
--emit-object |
Stop after compiling and write the program’s ELF object (default {build}/release/linux-x86_64/<name>.o). Needs no sysroot or linker. |
RADLANG_CC |
The clang that compiles the runtime against the sysroot (default clang-20). |
RADLANG_LINUX_ZIG |
The zig binary build-runtime-archive.sh uses to compile the runtime (zig cc, zig ar) when no sysroot is given. |
RADLANG_CLANG |
The clang the compiler uses to compile IR and link (default: clang-20, then clang-19, then clang). |
RADLANG_AR |
The archiver for the runtime archive. It must index ELF objects: llvm-ar (found next to clang or on PATH by default), not macOS ar. |
A project can keep both in config.yaml so the build needs no flags or variables:
build:
linux-x86_64:
sysroot: "~/sysroots/debian-bookworm-x86_64"
linker: "ld.lld"radlang checks the sysroot and linker before it compiles anything. When they
are missing it stops and lists the ways to continue: give a sysroot with
ld.lld, install zig, or pass --emit-object. An incomplete sysroot error names
the files that are missing and the Debian packages that provide them.
What the sysroot must contain
A sysroot is the root filesystem of an x86_64 Debian or Ubuntu system (any glibc distribution with the same layout works) with these packages installed:
| Package | Provides |
|---|---|
libc6-dev |
glibc headers, Scrt1.o/crti.o/crtn.o, libc.so, libm.so |
libgcc-12-dev (or the libgcc-N-dev matching the release) |
crtbeginS.o/crtendS.o, libgcc.a, libgcc_s.so |
libcurl4-openssl-dev |
curl/curl.h (the runtime’s HTTP client includes it; libcurl itself is loaded at run time) |
radlang looks for:
usr/include/stdio.hScrt1.oandcrti.oinusr/lib/x86_64-linux-gnu(orusr/lib64,lib/x86_64-linux-gnu,usr/lib)usr/lib/gcc/<triple>/<version>/crtbeginS.o, where the triple isx86_64-linux-gnu,x86_64-pc-linux-gnu,x86_64-redhat-linuxorx86_64-suse-linux. The newest version is used.usr/include/curl/curl.horusr/include/x86_64-linux-gnu/curl/curl.h
Keep the sysroot’s symlinks as they are: glibc’s libc.so is a linker script
that names /lib/x86_64-linux-gnu/libc.so.6, and ld.lld --sysroot finds it
through the sysroot’s lib symlink. Do not put the sysroot at a path that
contains spaces.
The executable needs the sysroot’s glibc version or newer on the target machine. An older release (Debian 12 has glibc 2.36, Ubuntu 22.04 has 2.35) runs on more systems.
Making a sysroot on an x86_64 Debian or Ubuntu machine
sudo apt-get install libc6-dev libgcc-12-dev libcurl4-openssl-dev # Ubuntu 24.04: libgcc-13-dev
cd /
tar -czf ~/debian-x86_64-sysroot.tar.gz \
lib lib64 usr/include usr/lib/x86_64-linux-gnu usr/lib/gcc/x86_64-linux-gnu usr/lib64Copy the archive to the build machine and unpack it into an empty directory:
mkdir -p ~/sysroots/debian-x86_64
tar -xzf debian-x86_64-sysroot.tar.gz -C ~/sysroots/debian-x86_64On merged-/usr systems, lib and lib64 are relative symlinks into usr,
and tar keeps them that way. usr/lib64 holds the dynamic loader link that
libc.so names. Leave it out only on a system that does not have it.
Making a sysroot from a container
With Docker (or a compatible tool) on any machine, including an Apple Silicon Mac running x86_64 images under emulation:
docker run --platform linux/amd64 --name rad-sysroot debian:bookworm sh -c \
'apt-get update && apt-get install -y --no-install-recommends libc6-dev libgcc-12-dev libcurl4-openssl-dev'
mkdir -p ~/sysroots/debian-bookworm-x86_64
docker export rad-sysroot | tar -x -C ~/sysroots/debian-bookworm-x86_64 \
--exclude 'dev/*' --exclude 'proc/*' --exclude 'sys/*'
docker rm rad-sysrootThe exported root filesystem is a few hundred MB. Only the directories listed in the previous section are needed, so you can delete the rest.
The linker
ld.lld is LLVM’s ELF linker: brew install lld on macOS, or the lld
(lld-20) package on Linux. radlang runs it directly against the sysroot.
It links a position-independent executable that uses glibc’s dynamic loader
/lib64/ld-linux-x86-64.so.2, in the same way as radlang’s native Linux link.
zig can replace both the sysroot and ld.lld, because it ships glibc headers
and libraries for every target. With zig and no sysroot, radlang compiles the
runtime with zig cc -target x86_64-linux-gnu and links with zig cc. zig has
no libcurl headers. On macOS the runtime build uses the SDK’s curl/curl.h;
on other hosts, set RADLANG_CURL_INCLUDE to a directory that contains
curl/curl.h.
The runtime archive
The C runtime is compiled for the target the first time you link, into
compiler/runtime/linux-x86_64/runtime.a. It is rebuilt when the runtime
sources, the build script or the sysroot change. You can also build it
directly:
RADLANG_LINUX_SYSROOT=~/sysroots/debian-bookworm-x86_64 RADLANG_CC=clang-20 \
tools/release/build-runtime-archive.sh --platform linux-x86_64An installed radlang that has no build script uses a linux-x86_64/runtime.a
from its runtime directory as it is.
Libraries, packages and limits
- Namespace libraries built from source are compiled again for the target, into
lib/linux-x86_64/<name>.rlc, next to the host library that the compiler reads metadata from.radlang cleanremoves both. A prebuilt.rlcor.rlppackage that has no sources cannot be retargeted. To use one, add its project underpaths.include. sys.platform()returns"linux"andsys.arch()returns"x86_64"in cross-compiled code.- GUI windows (
gui,syn.webview,syn.window) need the WebKitGTK host, and GPU compute (gpu.*) needs Metal, so cross builds reject both.gpu.available()is allowed and returnsfalseon Linux. Build GUI apps on the Linux machine itself. --mlir,--signand--notarizeare not available with this target.- At run time the executable needs glibc and
libgcc_s.so.1, which every glibc system has. The HTTP client loadslibcurl.so.4the first time it is used. SQLite and BearSSL are built into the runtime.
Checking the result
file output/release/linux-x86_64/app
# ELF 64-bit LSB pie executable, x86-64, ... interpreter /lib64/ld-linux-x86-64.so.2tests/runners/linux-x86_64-target.py checks the object, library and link-line
behaviour without a sysroot. With RADLANG_LINUX_SYSROOT set, it also links a
real executable, and runs it when the host is x86_64 Linux:
RADLANG_LINUX_SYSROOT=~/sysroots/debian-bookworm-x86_64 \
python3 tests/runners/linux-x86_64-target.py target/release/radlang