Cross development of Linux binaries on a FreeBSD system

Note that the text below is somewhat outdated - current way of setting up a Linux development environment is described at LinuxJails.

Previous to the use of FC4 as the default linux base port cross-developing Linux binaries on a FreeBSD system required the use of the linux_devtools port. This is not the case anymore. Cross development of Linux binaries requires to do a chroot in to the linux directory (default LINUXBASE: /compat/linux) to not pick up any FreeBSD native stuff (more or less compatible header files and/or libraries) by accident. Not doing a chroot may work in some cases but requires a little bit of luck if you don't really know what you are doing.

Doing a chroot with FC4 (or later) as the linux base port will not work as expected. The default linux base port tries to be as much userfriendly as possible. Because most of the users don't want to do cross-development but want to use the linux programs in the ports collection, the linux base port is optimized for this purpose. As much as possible is designed to use the FreeBSD native parts. Several important files and directories e.g., passwd dev and tmp, are not available in the default linux base so that the linux compatibility code in the kernel falls through to the corresponding native files and directories. The linux base part is a lean and mean system which is supposed to satisfy just the requirements to be able to at least run linux programs installed with the ports collection.

Providing a devtools port for the linux base would lead to a not so nice user experience when using linux programs for non-cross-development purposes, e.g., acroread would not be able to print, because it saves a temporary file to the linux tmp directory and the FreeBSD native print command (lpr/cups/...) would not find it in the FreeBSD tmp directory then (both referenced as "/tmp" by both applications).

For this reason anyone who wants to do cross-development should either use a linux dist port, e.g., linux_dist-gentoo-stage3, or build a native cross-compiler.

Cross development by chrooting into a linux_dist directory

The linux_dist ports are fully featured distributions with its own package management (in the example case "emerge") and development stuff. A linux dist port does not install into LINUXBASE, it installs into LOCALBASE (default: /usr/local). The gentoo-stage3 linux dist port installs by default into /usr/local/gentoo-stage3.

To do cross-development with a linux dist port you need to mount linprocfs, linsysfs (optional) and devfs (required) there. It may also be convenient to null-mount the home directories (corresponding entries in the passwd and shadow files are required then). When all this is in place, cross-development can start by chrooting into the linux dist root directory with e.g., "chroot /usr/local/gentoo-stage3 bash".

Cross development with a FreeBSD native cross-compiler

In the ports there are the cross-* ports. They can be used to compile a cross compiler for various architectures. All you have to do is to compile the ports and add includes and libs from the linux system your target in your development.


linux-xdev (last edited 2021-01-14T12:12:11+0000 by EdwardTomaszNapierala)