Clang

Clang is Article description::a C/C++/Objective-C compiler using LLVM as a backend and optimizer. It aims to be GCC compatible yet stricter, offers fast compile times with low memory usage, and has useful error and warning messages for easier compile troubleshooting.

Prerequisites
One of the goals of the Clang project is to be compatible with GCC, in dialect and on the command-line (see ). Occasionally some packages will fail to build correctly with it and some may build successfully but segfault when executed. Some packages also have GCC specific code and will also fail during compiling. In these events, GCC will need to be used as a fallback.

USE flags
Some packages are aware of the USE flag. This is typically here as specific flags must be used by the package internally for it to compile or work correctly at runtime:

Packages with a USE flag usually don't need any environment variables set, as they will handle it instead.

Emerge
Emerge clang:

Configuration
There are many possible configurations.

Users may wish to default to Clang and selectively use GCC or vice-versa.

There are two ways to do this:


 * 1) System wide using  or,
 * 2) via environment variables like the one(s) created for the GCC fallback.

Basic setup
This example is for defaulting to Clang but using GCC per-package for those which fail to build with Clang.

Clang environment
When attempting to use Clang system wide the system absolutely must have a GCC fallback! This cannot be stressed enough as the system will not be able to compile everything using Clang at the moment, such as glibc or wine-vanilla.

Gentoo maintains a bug tracker for packages that fail to build with Clang. Configuring Gentoo to use Clang system wide is simple. Change the CC and CXX variables in to reference the Clang equivalents. No further configuration is necessary.

Alternatively, the same contents could be put in e.g. . This would allow using Clang on a per package basis by invoking the compiler-clang environment file if desired:

The setup of a clang + LTO environment is described later in the article.

GCC fallback environment
Create a configuration file with a set of environment variables using Portage's built in directory. This will override any defaults for any packages that fail to compile with clang.

The name used below is just an example, so feel free to choose whatever name is desired for the fallback environment. Be sure to substitute the chosen name with the examples used in this article.

The most basic example is:

In the event GCC should be used as the fallback environment, set the appropriate flags in the file:

Advanced examples
Adjust the following entries to suit the desired needs, such as enabling/disabling link-time optimizations, alternative AR, NM, RANLIB, and so on.

For enabling LTO:

NO-LTO GCC fallback option:

Basically, copy the current working GCC config from, in the event it needs to be used it as a fallback.

When choosing to use LLVM's implementation of AR, NM , and RANLIB as detailed later in the article, be sure to set them back to the GNU versions for the GCC fallback environments as shown in the above example.

When choosing to not LTO, ignore the AR, NM , and RANLIB variables. When desiring to continue to use link-time optimization it's a good idea to have two separate environments like the above examples.

In the event the GCC fallback environment is needed, set the appropriate flags in the file:

Usage
This covers more advanced usage than described above for configuration.

Bootstrapping the Clang toolchain
For a "pure" Clang toolchain, one can build the whole LLVM stack using itself.

This is detailed in a subpage: Clang/Bootstrapping.

Link-time optimizations with Clang
The link-time optimization feature defers optimizing the resulting executables to linking phase. This can result in better optimization of packages but isn't standard behavior in Gentoo yet. Clang uses the linker for LTO.

Environment
Clang supports two types of link time optimization:


 * Full LTO, which is the traditional approach also used by gcc where the whole link unit is analyzed at once. Using it is no longer recommended.
 * ThinLTO, where the link unit is scanned and split up into multiple parts. With ThinLTO, the final compilation units only contain the code that are relevant to the current scope, thus speeding up compilation, lowering footprint and allowing for more parallelism at (mostly) no cost. ThinLTO is the recommended LTO mode when using Clang.

For full LTO, replace  with   in the following examples. There should be no compatibility differences between full LTO and thin LTO. Additionally, if Clang was not built with the  USE flag, add the   value to the following LDFLAGS.

As an alternative, LLVM provides its own,  , and   values. Feel free to use them though mileage may vary over using the standard,  , and  , since they're intended to handle LLVM bitcode which Clang produces when using the   flag.

Now set overrides using Clang with LTO enabled:

Global configuration
Similar to what was covered earlier in the article, a system wide Clang with LTO enabled can be done by changing the file:

Again, it is possible to set the AR, NM , and RANLIB to the LLVM implementations. Since earlier in the article compiler environments were set up using Clang without LTO, GCC without LTO, and GCC with LTO, it is now possible to pick and choose which is best on a per package basis. Since the goal is to compile packages system wide with Clang using LTO and not every package will successfully compile using it, fall back to Clang with LTO disabled or GCC. The may look like the following:

distcc
In order to use Clang on a distcc client, additional symlinks must to be created in :

ccache
ccache support is automatic once Clang is emerged.

Kernel
The Linux kernel can be compiled via clang/llvm either with Genkernel or with the kernel environment variables:

Genkernel
When using genkernel, edit the by substituting the following "Low Level Compile Settings" and adding the additional MAKEOPTS:

After that use genkernel as usual:

Additionally, the same options will have to be provided for any kernel modules:

Further, once clang becomes the default compiler, it might be possible to use and make things DRY.

Troubleshooting
The main place for looking up known failures with Clang is the tracker. If hitting an issue not reported on Gentoo's Bugzilla already, please open a new bug report and make it block the linked tracker.

Compile errors when using Clang with -flto
If the packages being installed are failing, check the logs. Often, packages with errors like the following will need to disable LTO by invoking the compiler-clang environment.

The following error may be seen in every LTO failure case:

Simply add the failing package to. In this case, it's the package, so to apply the proper override.

Sometimes a package will fail to compile even when disabling LTO because it requires another package which was compiled using -flto and works incorrectly. Something like the following error may be seen:

In this case libatomic_ops is causing boehm-gc to fail compiling. Recompile the program causing the failure using the non-LTO environment and then recompile the new program. In this case, boehm-gc fails when using LTO, so add both of them to the file to build them without LTO:

Use of GNU extensions without proper -std=
Some packages tend to use GNU extensions in their code without specifying  appropriately. GCC allows that usage, yet Clang disables some of more specific GNU extensions by default.

If a particular package relies on such extensions being available, then append the correct  flag to it:


 * for C89/C90 with GNU extensions,
 * for C99 with GNU extensions,
 * for C++:1998 with GNU extensions.

A common symptom of this problem are multiple definitions of inline functions like this:

This is because Clang uses C99 inline rules by default which do not work with gnu89 code. To work around it, it is likely necessary to pass  or set one of the environmental overrides to use GCC to compile the failing package if passing the right   flag doesn't work.

Since both current (2020) GCC and Clang default to  with C99 inline rules, chances are the problems have already been spotted by a GCC user.

Grub2 on MBR systems
Clang appends a string to the end of boot.img and diskboot.img while compiling that makes it larger than 512 bytes and can't be installed on a MBR system because of this.

A workaround exists by doing:

As this just removes the appended string   the compiled file will still work on the system without issue.

sudo: clang: command not found
Clang is not added to /usr/bin and instead lives in a separate path that is added to the PATH variable. Sudo has a whitelisted PATH variable that is baked in at compile time. So when a new version of clang is installed,it will not be added to sudo's PATH until sudo is re-emerged.

Differences from GCC
Clang's optimizer is different from GCC's. As a result, the command-line semantics are different:


 * The  flags will work, but mean slightly different things.
 * Clang also vectorizes on  and , albeit more conservatively in terms of code size than.
 * Instead of being the same as, Clang's   is an alias of.
 * The compatibility of  flags are limited as they can be simply meaningless to Clang.
 * The  and related flags are supposed to work identically, but Clang may not know about certain options. There are also Clang-only options not known by GCC.
 * The PGO in clang is a bit different as it requires post-processing the sample with.

The differences in language are documented by the project itself.

External resources

 * Playing with LLVM, gold linker and CLANG Gentoo Forums post
 * Nearly full clang+lto'd system configuration, stable amd64 July 2022
 * https://blogs.gentoo.org/mgorny/2022/10/07/clang-in-gentoo-now-sets-default-runtimes-via-config-file/