Cross Compiling FreeBSD for Fun and Profit




For the sake of illustration, the document assumes that:

Notation Used

Obtaining FreeBSD

The reader can obtain the sources as noted in the Handbook chapter titled Obtaining FreeBSD. The reader should place the sources in /home/bob/src.

Setting up Build Environment

For architectures that are supported by FreeBSD, cross-building is relatively easy. For architectures that FreeBSD doesn't support, more work is required.

The FreeBSD build system uses three environment variables to control which platform to build (TARGET), which target architecture to build (TARGET_ARCH), and where to put the results (MAKEOBJDIRPREFX). By default, FreeBSD builds the current architecture and places the results in /usr/obj, or in this example /home/bob/obj.

When cross-compiling however, FreeBSD places binaries in $MAKEOBJDIRPREFIX/$TARGET (for cases where $TARGET == $TARGET_ARCH, e.g. amd64) or $MAKEOBJDIRPREFIX/$TARGET.$TARGET_ARCH (for cases where $TARGET != $TARGET_ARCH). In this example, the directory used will be /home/bob/obj/arm.armeb.

Note that a non-default value of MAKEOBJDIRPREFIX must be passed to make as an environment variable. It cannot be set as an argument to make directly.

make.conf / src.conf

A standard make.conf and src.conf file can contain variables or make snippets which tune build behavior. /usr/share/examples/etc/make.conf has more details on some recommended make.conf tunable items, and src.conf(5) lists some of the variables that one could put in src.conf . The following notes some potential pitfalls in using these files for cross-compiling, make universe, make tinderbox, etc:

CPUTYPE= core2

CPUTYPE?= core2



Building and Installing World

The following subsections show the reader how they could build and install world for the big-endian ARM architecture from scratch.

Go to the Source Directory

% cd /home/bob/src

Build World

% env MAKEOBJDIRPREFIX=/home/bob/obj make buildworld TARGET=arm TARGET_ARCH=armeb

Install World

% mkdir /home/bob/my-armeb

% env MAKEOBJDIRPREFIX=/home/bob/obj make installworld DESTDIR=/home/bob/my-armeb TARGET=arm TARGET_ARCH=armeb

% env MAKEOBJDIRPREFIX=/home/bob/obj make distribution DESTDIR=/home/bob/my-armeb TARGET=arm TARGET_ARCH=armeb

What's Next

The above example demonstrated how one could build world and install world with armeb. This should provide you with the building blocks required to build the FreeBSD base system for any cross-compilation environment.

Cheat sheet

A_Brief_Guide_To_Cross_Compiling_FreeBSD (last edited 2015-04-06 20:50:05 by JohnBaldwin)