From Gentoo Wiki
< Clang
Jump to:navigation Jump to:search

Advanced users may optionally choose to bootstrap Clang by building it using the full LLVM toolchain.

This is required only if looking to move a system to be entirely GCC-free (which is currently not possible on glibc systems). This is not yet supported and is dangerous.

Bootstrapping the Clang toolchain

Mixing Clang and GCC should be fine, unless using default-libcxx or default-compiler-rt. Clang and GCC should otherwise always produce output with the same ABI.

For purity, however, some may wish to build Clang and the rest of the LLVM toolchain with itself, to prove it works and to fully dogfood using Clang.

Preparing the environment

Prepare the environment for the Clang toolchain:

FILE /etc/portage/env/compiler-clang
COMMON_FLAGS="-march=native -O2 -pipe"


LDFLAGS="-fuse-ld=lld -rtlib=compiler-rt -unwindlib=libunwind -Wl,--as-needed"

This example replaces not only the compiler but also the GNU linker ld.bfd with the LLVM linker lld. It is a drop-in replacement, but significantly faster than the bfd linker.

Set USE flags default-compiler-rt default-lld llvm-libunwind for Clang via /etc/portage/package.use:

FILE /etc/portage/package.use/clang
sys-devel/clang-common default-compiler-rt default-lld llvm-libunwind

Then install Clang, LLVM, compiler-rt, llvm-libunwind, and lld with the default GCC environment:

root #emerge sys-devel/clang sys-devel/llvm sys-libs/compiler-rt sys-libs/llvm-libunwind sys-devel/lld

It is also possible to add the default-libcxx USE flag to use LLVM's C++ STL with clang, however this is heavily discouraged because libstdc++ and libc++ are not ABI compatible. i.e. A program built against libstdc++ will likely break when using a library built against libc++, and vice versa.

Note that sys-libs/llvm-libunwind deals with linking issues that sys-libs/libunwind has, so it is preferred to use and replace the non-llvm libunwind package if installed (it builds with -lgcc_s to resolve issues with __register_frame / __deregister_frame undefined symbols).


Enable the Clang environment for these packages now:

FILE /etc/portage/package.env
sys-devel/llvm compiler-clang
sys-libs/libcxx compiler-clang
sys-libs/libcxxabi compiler-clang
sys-libs/compiler-rt compiler-clang
sys-libs/compiler-rt-sanitizers compiler-clang
sys-libs/llvm-libunwind compiler-clang
sys-devel/lld compiler-clang
sys-devel/clang compiler-clang

Repeat the emerge step with the new environment - the toolchain will now be rebuilt with itself instead of GCC:

root #emerge sys-devel/clang sys-devel/llvm sys-libs/libcxx sys-libs/libcxxabi sys-libs/compiler-rt sys-libs/llvm-libunwind sys-devel/lld

Clang may now be used with other packages!