NOTE: Clang/LLVM is the system compiler on several platforms in FreeBSD 10.0 and later, and GCC is not installed by default. Some of the information on this page is out of date, and maintained for historical reference only.

Building FreeBSD with clang/llvm

FreeBSD world and kernel can be built with clang/llvm. Clang is a compiler built on the LLVM compiler infrastructure. Both clang and llvm are released under a BSD like license.

Status

Clang has been imported into head and stable/9, and is built by default, so there is no need to install the port anymore, unless you want to play with the other tools that llvm provides. Alternatively, you can set WITH_CLANG_EXTRAS in src.conf, to build most of the additional tools.

An unmodified FreeBSD head and stable/9 can completely be built by clang! (Note that you still need to setup /etc/make.conf or /etc/src.conf properly, see below for the details.)

Newer snapshots of clang/llvm are imported regularly into head, and will be merged to stable branches, as appropriate. FreeBSD 9.0 is the first release to contain clang by default.

All of userland (plus clang/llvm itself) and the kernel compiles and runs ok. The amd64, i386, powerpc64 and arm kernels boot multiuser.

A buildbot that runs on-commit builds and boot tests can be found here.

Build status

amd64

i386

arm OABI

arm EABI

powerpc

powerpc64

mips

mips64

Hello world

OK

OK

OK

OK

OK

OK

unknown

OK

FreeBSD World

OK

OK

OK

OK

OK, PIC missing

OK, asm parser missing

unknown

unknown

GENERIC kernel

OK

OK

OK

OK

OK (with patches)

OK

missing -mno-abicalls

unknown

LINT kernel

OK

OK

OK

OK

unknown

unknown

missing -mno-abicalls

unknown

Please note that cross compiling is not yet supported by clang (or at least, it is not easy to do out of the box).

Quickstart

Checkout head:

# svn co http://svn.freebsd.org/base/head src

Add the following lines to /etc/make.conf (if you want to use clang for everything, even ports), or /etc/src.conf (if you want to use clang just for world and kernel):

CC=clang
CXX=clang++
CPP=clang-cpp

(NOTE: Do *not* use "clang -E" for CPP, this will not work correctly. You will almost certainly get RPC-related build errors.)

As of r233419, head should build without -Werror bailing out the build, e.g. there should be no unexpected warnings. If you are attempting to build something earlier, or encounter -Werror bailouts anyway, add the following to /etc/make.conf or /etc/src.conf, as appropriate:

# This setting to build world without -Werror:
NO_WERROR=
# This setting to build kernel without -Werror:
WERROR=

If you are building jails, the following can make life easier by not setting system-immutable flag on various important system files. This may have serious security implications depending on your use, (schg/nouchg are explained in chflags(1), the jail(8) man page exlains how jails implement BSD securelevel):

# Does not set schg bit on various system files,
# useful for building Jails, has security implications.
NO_FSCHG=

Now you have all the necessary tools in llvm/Release+Asserts/bin. They need to be next to each other, as we will be using the full-path to scan-build later and it must find clang-cc, which it searches in it's own directory and $PATH. As $PATH will be cleansed during buildworld, only the first option remains.

Weekly scans of buildworld and buildkernel can be studied at http://scan.freebsd.your.org/freebsd-head. The rate of false positives is decreasing and LLVM is working actively on fixing them. You can help by grabbing a report and working out if it's valid or false positive. Then send a bug report to either LLVM or FreeBSD and add it to the list below.