For the complete documentation index, see llms.txt. This page is also available as Markdown.

40.7 Cross-Building FreeBSD on a Linux System

Cross-building FreeBSD on Linux (using Ubuntu 24.04 LTS as an example) requires at least 12 GB of memory and 16 GB of swap. This section covers the steps from environment preparation to complete building.

Device Environment

This section uses Ubuntu 24.04 LTS as an example; other Linux distributions can also be referenced. The build process has high system resource requirements; the recommended configuration is:

  • Memory: Recommended capacity of at least 12 GB.

  • Swap: Recommended capacity of 16 GB.

Insufficient memory may cause build failures.

Installing Packages

Building FreeBSD requires installing the following packages and development libraries. Execute with root privileges:

apt update
apt install git build-essential libbz2-dev libarchive-dev libssl-dev flex

Purpose of each package:

Package
Purpose

git

Version control tool for obtaining FreeBSD source code

build-essential

Basic build tool collection

libbz2-dev

bzip2 compression library development files

libarchive-dev

Archive library development files

libssl-dev

SSL library development files

flex

Lexical analyzer generator tool

Some users may need to change software sources based on their network environment.

Disabling Screen Blank

During long build processes, preventing the system from automatically turning off the screen can avoid build interruptions due to unexpected sleep.

For the GNOME desktop environment, go to "Settings" → "Power" → "Power Saving Options" and set "Screen Blank" to "Never". If using a pure command-line interface, you can use terminal multiplexers (such as tmux or screen) to keep sessions running.

Disabling systemd-oomd

Starting from Ubuntu 22.04 LTS (Desktop), the system enables the systemd-oomd service by default. This service forcefully terminates high-usage processes when the system reaches memory thresholds, which may cause build failures without any notification to the user.

Disable systemd-oomd auto-start and stop it immediately:

Check the running status of systemd-oomd:

Cloning FreeBSD Source Code with Git

Tip

The /home/ykla path shown in the examples in this section is an example path; please replace it with your actual user home directory.

Clone the FreeBSD source code repository:

--depth 1 specifies a shallow clone, fetching only the latest commit, which significantly reduces download size and storage space. If you need the complete history, omit this parameter.

After cloning, the source code is located at /home/ykla/freebsd-src (the freebsd-src directory is automatically created by Git).

To build a specific version (such as 15.0-RELEASE), you can switch to the corresponding branch after cloning:

Creating a Directory for Build Artifacts

Create the directory bsdobj to store build artifacts:

The -p option: creates parent directories if they do not exist.

The directory structure is as follows:

Building Toolchain and World (User Space)

The buildworld target is used to build the complete FreeBSD user space (world), including system libraries, command-line tools, etc. This is the first step of cross-building.

Switch to the FreeBSD source code root directory:

Specify the object directory and use make.py to build the arm64/aarch64 base system, while enabling AArch64 and ARM LLVM backends and disabling 32-bit library support:

Option descriptions:

Item
Description

MAKEOBJDIRPREFIX=/home/ykla/bsdobj

Specify the output directory for all build artifacts

tools/build/make.py

Official script for starting the build process on non-FreeBSD systems

--bootstrap-toolchain

Use the LLVM/Clang/LLD bootstrap toolchain from the source tree instead of the one installed via apt, making it closer to a native build

-j16

Enable 16 parallel compilation tasks. Typically matches the CPU thread count, e.g., use -j4 for 4 threads

TARGET=arm64

Set the target platform to arm64

TARGET_ARCH=aarch64

Set the target CPU architecture to aarch64

WITH_LLVM_TARGET_AARCH64=yes

Enable the LLVM AArch64 backend

WITH_LLVM_TARGET_ARM=yes

Enable the LLVM ARM backend

WITHOUT_LIB32=yes

Disable 32-bit compatibility libraries and related components; currently not passing tests

buildworld

Build the FreeBSD user space (world)

The buildworld build takes a long time and may require several hours depending on hardware configuration. After a successful build, you can verify by checking whether the corresponding architecture directory structure has been generated under the bsdobj directory.

To verify whether the build was successful, refer to the following figure:

Build result verification

Building the Kernel Toolchain

Ensure you are still in the FreeBSD source code root directory:

Build the kernel toolchain using the same parameters as buildworld:

Except for the kernel-toolchain at the end, all other option parameters remain the same.

To verify the kernel toolchain build result, refer to the following figure:

Kernel toolchain build result

Building the Kernel

Ensure you are still in the FreeBSD source code root directory:

Build the arm64/aarch64 kernel using the specified parameters:

Except for the buildkernel at the end, all other option parameters remain the same.

To verify the kernel build result, refer to the following figure:

Kernel build result

Appendix: RISC-V 64

Build the riscv64 architecture kernel toolchain using the bootstrap toolchain and ignoring GCC detection:

To verify the RISC-V kernel toolchain build result, refer to the following figure:

RISC-V kernel toolchain build result

Build the riscv64 kernel using the cross-compiler and ignoring GCC detection:

To verify the RISC-V kernel build result, refer to the following figure:

RISC-V kernel build result

Troubleshooting and Outstanding Issues

Building FreeBSD on Arch Linux

If compiling on Arch, you need to set a temporary environment variable:

This parameter explicitly specifies the strerror_r return type, avoiding compilation errors caused by glibc supporting both POSIX and GNU specifications. Otherwise, the build process will abort at the krb5 stage.

You must also ensure that the hostname command exists and can output normally, and install packages such as time.

32-bit Build Issues

FreeBSD 15.0 no longer supports i386, armv6, and 32-bit powerpc architectures, retaining only armv7 as the last supported 32-bit platform.

Ubuntu Native LLVM Toolchain

To be verified.

Building More Architectures (e.g., amd64)

To be investigated.

References

Last updated