Cross Files & Native Files
Cross-compiling is where Meson shines compared with older tools: it was designed from the start around three machines (see Cross-Compilation):
| Machine | Meaning | In meson.build |
|---|---|---|
| build | the machine running the compiler | build_machine |
| host | the machine the program will run on | host_machine |
| target | only for compilers: the machine their output runs on | target_machine |
Everything about the host machine goes into a cross file, an INI-style file passed with --cross-file. Its counterpart, the native file (--native-file), configures tools for the build machine. Neither changes meson.build.
A cross file
# aarch64.ini
[binaries]
c = 'clang'
ar = 'ar'
strip = 'strip'
# exe_wrapper = 'qemu-aarch64' # lets Meson run tests and cc.run() checks
[built-in options]
c_args = ['--target=aarch64-linux-gnu']
c_link_args = ['--target=aarch64-linux-gnu', '-fuse-ld=lld']
[properties]
# sys_root = '/opt/sysroots/aarch64' # where the target's headers and libraries live
# pkg_config_libdir = '/opt/sysroots/aarch64/usr/lib/pkgconfig'
[host_machine]
system = 'linux'
cpu_family = 'aarch64'
cpu = 'armv8-a'
endian = 'little'| Section | Holds |
|---|---|
[binaries] |
compilers and tools: c, cpp, ar, strip, pkg-config, exe_wrapper… |
[built-in options] |
any built-in option for the host machine (c_args, c_link_args, default_library, buildtype…) |
[properties] |
sys_root, pkg_config_libdir, and values your build reads with meson.get_external_property() |
[host_machine] |
system, cpu_family, cpu, endian: what host_machine.* returns |
[constants] |
variables usable in the rest of the file (toolchain = '/opt/arm', then c = toolchain / 'bin/gcc') |
Clang is a cross compiler out of the box: --target=aarch64-linux-gnu makes it emit ARM64 code, no separate toolchain needed. What clang does not come with is the target's C library: the startup files (crt1.o), libc.so and the headers. Those come from a sysroot. The course image has none for ARM64, which this session shows honestly: objects compile, the static library builds, and linking a program fails exactly where the sysroot would be needed.
mkdir -p x && cd x
cat > meson.build <<'M'
project('x', 'c')
message('build machine: ' + build_machine.cpu_family() + ', host machine: ' + host_machine.cpu_family())
lib = static_library('mathx', 'mathx.c')
executable('app', 'main.c', link_with: lib)
M
echo 'int twice(int x) { return 2 * x; }' > mathx.c
echo 'int twice(int); int main(void) { return twice(0); }' > main.c
cat > aarch64.ini <<'M'
[binaries]
c = 'clang'
ar = 'ar'
strip = 'strip'
[built-in options]
c_args = ['--target=aarch64-linux-gnu']
c_link_args = ['--target=aarch64-linux-gnu', '-fuse-ld=lld']
[host_machine]
system = 'linux'
cpu_family = 'aarch64'
cpu = 'armv8-a'
endian = 'little'
M
meson setup build-arm --cross-file aarch64.ini | grep -E 'Build type|machine|Message'
echo "--- the library only needs the compiler"
meson compile -C build-arm -j 1 mathx | grep '^\['
file build-arm/libmathx.a.p/mathx.c.o | cut -d, -f1-2
echo "--- the program needs the target's libc: no sysroot, no link"
meson compile -C build-arm -j 1 app 2>&1 | grep -E 'cannot open|unable to find' | sort -uBuild type: cross build
C compiler for the host machine: clang (clang 18.1.3 "Ubuntu clang version 18.1.3 (1ubuntu1)")
C linker for the host machine: clang ld.lld 18.1.3
C compiler for the build machine: /usr/local/bin/ccache cc (gcc 13.3.0 "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0")
C linker for the build machine: cc ld.bfd 2.42
Build machine cpu family: x86_64
Build machine cpu: x86_64
Host machine cpu family: aarch64
Host machine cpu: armv8-a
Target machine cpu family: aarch64
Target machine cpu: armv8-a
Message: build machine: x86_64, host machine: aarch64
--- the library only needs the compiler
[1/2] Compiling C object libmathx.a.p/mathx.c.o
[2/2] Linking static target libmathx.a
build-arm/libmathx.a.p/mathx.c.o: ELF 64-bit LSB relocatable, ARM aarch64
--- the program needs the target's libc: no sysroot, no link
ld.lld: error: cannot open Scrt1.o: No such file or directory
ld.lld: error: cannot open crtbeginS.o: No such file or directory
ld.lld: error: cannot open crtendS.o: No such file or directory
ld.lld: error: cannot open crti.o: No such file or directory
ld.lld: error: cannot open crtn.o: No such file or directory
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
Points to notice:
- Meson set up two C compilers: clang for the host (ARM64) and the normal
ccfor the build machine, which it would use for anynative: truetarget, such as a code generator that must run during the build. - The configure step passed: Meson's sanity check for a cross compiler only compiles a test program (it can't run it), so a missing sysroot shows up later, at link time.
- With a sysroot you would add
'--sysroot=/path'to both argument lists (orsys_rootin[properties]), and apkg-configbinary orpkg_config_libdirthat looks inside the sysroot. Debian'scrossbuild-essential-arm64package or a vendor SDK provide all of that.
Ready-made cross files for many platforms are published by projects and distributions, and Meson looks for cross files by name in ~/.local/share/meson/cross/ and /usr/share/meson/cross/, so --cross-file aarch64 finds a file installed there.
Native files: configure the build machine
A native file has the same format, but describes the build machine. It replaces long CC=… CXX=… PKG_CONFIG_PATH=… command lines with a file you can commit, and several can be stacked (later ones override earlier ones):
mkdir -p n && cd n
cat > meson.build <<'M'
project('n', 'c')
message('C compiler: ' + meson.get_compiler('c').get_id())
message('c_args: ' + ' '.join(get_option('c_args')))
message('buildtype: ' + get_option('buildtype'))
M
cat > clang.ini <<'M'
[binaries]
c = 'clang'
M
cat > hardened.ini <<'M'
[built-in options]
c_args = ['-D_FORTIFY_SOURCE=3', '-fstack-protector-strong']
buildtype = 'debugoptimized'
M
meson setup build --native-file clang.ini --native-file hardened.ini | grep MessageMessage: C compiler: clang
Message: c_args: -D_FORTIFY_SOURCE=3 -fstack-protector-strong
Message: buildtype: debugoptimized
Cross builds and tests
When the host machine can't run the build machine's programs, meson test can't run tests either. Meson then marks them as skipped, unless the cross file names an exe_wrapper (QEMU user mode, Wine for Windows targets) that Meson prefixes to every test command and cc.run() check. meson.can_run_host_binaries() tells meson.build which case it is in.
Test yourself
In a Meson cross build from an x86-64 PC to an ARM64 board, what is the host machine?
- The x86-64 PC
- The ARM64 board
- Both
- The CI server
The session's cross configuration succeeded and the static library built, but linking app failed. Why?
- Meson doesn't support clang for cross builds
- There is no ARM64 sysroot: no target C library or startup files to link against
arcan't make ARM64 archives- The cross file has a syntax error
What is a native file for?
- Cross-compiling to another CPU
- Describing tools and options for the build machine (compilers, pkg-config, default options) in a reusable file
- Listing native libraries to link
- Disabling cross compilation