Difference between pages "Rootfs over encrypted lvm" and "Clang"

(Difference between pages)
m (Added some color to a <console>)
 
(Enabling link-time optimizations)
 
Line 1: Line 1:
This howto describes how to setup LVM and rootfs with cryptoLUKS-encrypted drive
+
==Introduction==
 +
LLVM can be used as an alternative to GNU's compiler, GCC. The main benefit of using LLVM compilers instead of GCC is their lower memory usage, faster compile time and better diagnostics. There are some Benchmarks on the [http://clang.llvm.org/features.html#performance Clang] and [http://www.phoronix.com/scan.php?page=article&item=llvm3_gcc_open64 Phoronix] homepages.
  
= Prepare the hard drive and partitions =
+
It may happen that some programs do not compile (like glibc) because they depend on GCC-specific language extensions [http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html] (this is why the whole BSD code can be compiled with LLVM but some GNU code cannot) or segfault after successful compilation with LLVM (like xorg-server) but after following this guide, the system will still be able to compile packages with gcc. So if something goes wrong, it can be switched back to gcc for the particular package by uncommenting lines in /etc/make.conf and the bug should be reported.
This is an example partition scheme, you may want to choose differently.
+
<code>/dev/sda1</code> used as <code>/boot</code>. <code>/dev/sda2</code> will be encrypted drive with LVM.
+
  
* <code>/dev/sda1</code> -- <code>/boot</code> partition.  
+
LLVM's C/C++ frontends clang and clang++ version 3.0 are stable enough to be self-hosting [http://blog.llvm.org/2010/02/clang-successfully-self-hosts.html] and compile Boost [http://blog.llvm.org/2010/05/clang-builds-boost.html], Qt [http://labs.qt.nokia.com/2010/10/29/compiling-qt-with-clang/], LibreOffice [http://wiki.documentfoundation.org/Development/Building_LibreOffice_with_Clang], FreeBSD [http://wiki.freebsd.org/BuildingFreeBSDWithClang], some parts of the Linux kernel [http://lwn.net/Articles/411654/] and more.
* <code>/dev/sda2</code> -- BIOS boot partition (not needed for MBR - only needed if you are using GPT) This step required for GRUB2. For more info, see: [http://www.funtoo.org/Funtoo_Linux_Installation#Prepare_Hard_Disk] for more information on GPT and MBR.  
+
* <code>/dev/sda3</code> -- <code>/</code> partition, will be the drive with LUKS and LVM.
+
  
<console>
+
Further, using LLVM 3.0 and up, there is a third way to compile with LLVM: the dragonegg package creates a gcc-plugin, that uses LLVM's optimizers but parses the code and creates binaries with gcc, which means that everything that compiles and works with gcc should work with dragonegg also. This plugin can be enabled by using a single CFLAG. Since LLVM 3.0 the old llvm-gcc package is deprecated and replaced by dragonegg, so it will disappear from portage with llvm version 2.9.
##r### ##b##dd if=/dev/zero of=/dev/sda3 bs=100M
+
##r### ##b##dd if=/dev/urandom of=/dev/sda3 bs=100M
+
</console>
+
The <code>dd</code> part is optional, and the command only needs to be run for security reasons (i.e only if you had top secret files on your drive). The command overwrites the lingering data on the device with random data. It takes around 6 hours to complete for a 200GB drive.
+
  
Note that you will get a message about reaching the end of the device when the <code>dd</code> command has finished. This behavior is intended.
+
==LLVM Frontends==
 +
To be able to compile some sourcecode of a specific language, LLVM needs an appropriate frontend. There are clang, llvm-gcc and dragonegg in portage.
  
= Encrypting the drive =
+
The goal of the Clang project is to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler.
<console>
+
##r### ##b##cryptsetup --cipher aes-xts-plain64 luksFormat /dev/sda3
+
##r### ##b##cryptsetup luksOpen /dev/sda3 dmcrypt_root
+
</console>
+
  
There you'll be prompted to enter your password phrase for encrypted drive, type your paranoid password there.
+
llvm-gcc is a modified version of gcc that compiles C/ObjC programs into native objects, LLVM bitcode or LLVM assembly language, depending upon the options. As written in the previous section, dragonegg replaced llvm-gcc in version 3.0.
  
= Create logical volumes =
+
So after installing llvm, clang and dragonegg, you will be able to choose between gcc and llvm whenever you like or use them both at the same time.
<console>
+
##r### ##b##pvcreate /dev/mapper/dmcrypt_root
+
##r### ##b##vgcreate vg /dev/mapper/dmcrypt_root
+
##r### ##b##lvcreate -L10G --name root vg         
+
##r### ##b##lvcreate -L2G --name swap vg
+
##r### ##b##lvcreate -L5G --name portage vg
+
##r### ##b##lvcreate -l 100%FREE -nhome vg
+
</console>
+
Feel free to specify your desired size by altering the numbers after the -L flag. For example, to make your portage dataset 20GB's, use the flag -L20G instead of -L5G.
+
  
= Create a filesystem on volumes =
+
== Install LLVM and its Frontends ==
 +
Simply emerge the packages on ~arch systems. On arch systems you have to unmask some packages first. dragonegg requires gcc's ''lto'' USE-flag to be set and works with gcc 4.5 and gcc 4.6.
 
<console>
 
<console>
##r### ##b##mkfs.ext2 /dev/sda1
+
###i## emerge llvm clang dragonegg
##r### ##b##mkswap /dev/mapper/vg-swap
+
##r### ##b##mkfs.ext4 /dev/mapper/vg-root
+
##r### ##b##mkfs.ext4 /dev/mapper/vg-portage
+
##r### ##b##mkfs.ext4 /dev/mapper/vg-home
+
 
</console>
 
</console>
 +
Note, that for clang++ the C++ headers search path is hardcoded to the active gcc profile.
 +
If you change the active gcc profile, or update gcc to a new version, you will have to remerge clang to update the search path.
  
= Basic system setup =
+
To use dragonegg, run gcc as usual, with an extra command line argument <tt>-fplugin=/usr/lib/llvm/dragonegg.so</tt>
 +
If you change the active gcc profile, or update gcc to a new version, you will have to remerge dragonegg to update the plugin.
 +
 
 +
After the installation, check which CPUs are supported by using the command
 
<console>
 
<console>
##r### ##b##swapon /dev/mapper/vg-swap
+
###i## llvm-as < /dev/null | llc -mcpu=help
##r### ##b##mkdir /mnt/funtoo
+
##r### ##b##mount /dev/mapper/vg-root /mnt/funtoo
+
##r### ##b##mkdir -p /mnt/funtoo/{boot,usr/portage,home}
+
##r### ##b##mount /dev/sda1 /mnt/funtoo/boot
+
##r### ##b##mount /dev/mapper/vg-portage /mnt/funtoo/usr/portage
+
##r### ##b##mount /dev/mapper/vg-home /mnt/funtoo/home
+
 
</console>
 
</console>
Now perform all the steps required for basic system install, please follow [http://docs.funtoo.org/wiki/Funtoo_Linux_Installation]
+
and then add the following lines to <code>/etc/make.conf</code> (uncommenting the lines you need) to enable compilation via LLVM, adapting the march-option according to the previous command:
don't forget to emerge the following:
+
  
* cryptsetup
+
in <code>/etc/portage/make.conf</code>:
* lvm2
+
{{File
* grub
+
|/etc/portage/make.conf|<pre>
* kernel sources (gentoo-sources recommended)
+
# LLVM
 +
#CC="/usr/bin/clang"
 +
#CXX="/usr/bin/clang++"
  
= Kernel options =
+
# llvm-gcc for C++ code and fortran
{{Note}}Important, do not miss this part.
+
# llvm-gcc is deprecated and only used with LLVM 2.9
{{kernelop
+
#CC="/usr/bin/llvm-gcc"
|'''General setup --->'''
+
#CXX="/usr/bin/llvm-g++"
|'''[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support'''
+
#CPP="/usr/bin/llvm-cpp"
}}
+
#F77="/usr/bin/llvm-gfortran"
  
{{kernelop
+
# Flags for clang: Insert your arch here instead of k8 and have a look at the manpage of clang for flag descriptions.
|'''Device Drivers --->''' <br> '''Generic Driver Options --->'''
+
# Some gcc flags like -pipe and -pthread also work, though they might be ignored by clang.
|'''[*] Maintain a devtmpfs filesystem to mount at /dev''' <br>
+
#CFLAGS="-march=k8 -O2"
}}
+
  
{{kernelop
+
# Flags for dragonegg; just use all the gcc flags you like and append -fplugin=/path/to/dragonegg.so
|'''Device Drivers --->''' <br> '''[*] Multiple devices driver support --->'''
+
#CFLAGS="-march=k8 -O2 -fplugin=/usr/lib64/llvm/dragonegg.so"
|'''<*>Device Mapper Support''' <br> '''<*> Crypt target support'''
+
</pre>}}
}}
+
  
{{kernelop
+
{{Note}} Have a look at clang's manpages for additional information. If you get errors that your compiler cannot produce code, you should check your flags, e.g. don't use <tt>-O4 -flto -S</tt> or stuff like that; the examples above will work.
|'''Cryptographic API --->'''
+
|'''-*-AES cipher algorithms''' <br> '''<*> XTS support'''
+
}}
+
  
= Initramfs setup and configuration =
+
== Using clang with portage ==
Build your initramfs with [https://bitbucket.org/piotrkarbowski/better-initramfs better-initramfs] project.
+
  
{{note}}better-initramfs supports neither dynamic modules nor udev, so you should compile your kernel with built-in support for your block devices.
+
Although Gentoo package tree is not designed to be used with compiler other than GCC, clang can be enforced on most of the packages through ''CC'' and ''CXX'' variables.
  
<console>
+
Please note, however, that many of Gentoo packages still don't build with clang and a few don't work correctly after being built. That's why we suggest using <tt>/etc/portage/env</tt> file to enable the use of clang per-package.
##r### ##b##git clone git://github.com/slashbeast/better-initramfs.git
+
##r### ##b##cd better-initramfs
+
##r### ##b##less README.rst
+
##r### ##b##bootstrap/bootstrap-all
+
##r### ##b##make prepare
+
##r### ##b##make image
+
</console>
+
  
Copy resulting <code>initramfs.cpio.gz</code> to <code>/boot</code>.
+
In order to do that, first create a new environment override to use in <code>/etc/portage/env/clang</code>:
<pre># cp output/initramfs.cpio.gz /boot
+
{{File
</pre>
+
|/etc/portage/env/clang|<pre>
Alternatively pre-compiled binary initramfs available at https://bitbucket.org/piotrkarbowski/better-initramfs/downloads
+
CC=clang
<pre># wget https://bitbucket.org/piotrkarbowski/better-initramfs/downloads/release-x86_64-v0.7.2.tar.bz2
+
CXX=clang++
# tar xf release-x86_64-v0.5.tar.bz2
+
</pre>}}
# cd release*
+
# gzip initramfs.cpio
+
# cp initramfs.cpio.gz /boot</pre>
+
  
Remember, better-initramfs project is a work in progress, so you need to update from time to time. It can be done easily with <code>git</code>. Go to the better-initramfs source dir and follow:
+
Then you can enable use of clang for packages using the [[:/etc/portage/env|/etc/portage/package.env]] file:
<pre># git pull
+
{{File
# less ChangeLog
+
|/etc/portage/package.env|<pre>
</pre>
+
app-foo/bar clang
Please, read the ChangeLog carefuly and do necessary updates, to <code>/etc/boot.conf</code>, the example config below. Please, backup working <code>initramfs.cpio.gz</code> and <code>/etc/boot.conf</code> before updating initramfs.
+
app-bar/baz clang
 +
</pre>}}
  
= Genkernel approach =
+
If you want to use clang by default you can and need to specify some core packages. Here is small list of core packages that are currently failing on clang, but not that could be outdated:
Funtoo's genkernel capable to create initramfs for encrypted drive. Compile and install kernel and initramfs of your favorite kernel sources:
+
{{File
<pre>genkernel --kernel-config=/path/to/your/custom-kernel-config --no-mrproper --makeopts=-j5 --install --lvm --luks all</pre>
+
|/etc/portage/env/gcc|<pre>
Configure the bootloader as described above, with correct kernel and initramfs images names. An example for genkernel and grub2:
+
CC=gcc
 +
CXX=g++
 +
</pre>}}
  
{{code|/etc/boot.conf|<pre>
+
in addition, it is recommend to add compiler flags there:
boot {
+
{{File
  generate grub
+
|/etc/portage/env/gcc|<pre>
  default "Funtoo Linux"
+
CFLAGS="-O2 -march=native -mtune=native -pipe"
  timeout 3
+
CXXFLAGS="-O2 -march=native -mtune=native -pipe"
}
+
LDFLAGS="-Wl,--as-needed"
"Funtoo Linux" {
+
#You can disable gold link here
  kernel kernel-genkernel-x86_64-2.6.39
+
#EXTRA_ECONF="--enable-gold=default"
  initrd initramfs-genkernel-x86_64-2.6.39
+
</pre>}}
  params += crypt_root=/dev/sda2 dolvm real_root=/dev/mapper/vg-root  rootfstype=ext4 resume=swap:/dev/mapper/vg-swap quiet
+
}</pre>}}
+
  
= Grub2 configuration =
+
And in <code>/etc/portage/package.env</code>:
An example of <code>/etc/boot.conf</code> for better-initramfs
+
{{File
{{code|/etc/boot.conf|<pre>
+
|/etc/portage/package.env|<pre>
boot {
+
#---------------CORE PACKAGES TO BUILD WITH GCC:
  generate grub
+
sys-apps/which gcc
  default "Funtoo Linux"
+
sys-fs/reiserfsprogs gcc
  timeout 3
+
sys-libs/ncurses gcc
}
+
sys-libs/zlib gcc
"Funtoo Linux" {
+
sys-apps/busybox gcc
  kernel bzImage[-v]
+
sys-fs/e2fsprogs gcc
  initrd /initramfs.cpio.gz
+
sys-devel/binutils gcc
  params += enc_root=/dev/sda2 lvm luks root=/dev/mapper/vg-root  rootfstype=ext4 resume=swap:/dev/mapper/vg-swap quiet
+
sys-libs/glibc gcc
}</pre>}}
+
sys-devel/dragonegg gcc
 +
dev-libs/openssl gcc
 +
sys-boot/grub gcc
 +
#---------------USER PACKAGES TO BUILD WITH GCC:
 +
sys-apps/pacman gcc
 +
www-client/firefox gcc
 +
x11-libs/cairo gcc
 +
media-libs/mesa gcc
 +
</pre>}}
  
{{code|/etc/fstab|
+
If you have {{Package|app-portage/flaggie}} installed, you can modify <code>/etc/portage/package.env</code> by running the following:
<pre>
+
<console>
# <fs>                  <mountpoint>  <type>    <opts>                          <dump/pass>
+
###i## flaggie app-foo/bar app-bar/baz +clang
/dev/sda1              /boot        ext2      noauto,noatime                  1 2
+
</console>
/dev/mapper/vg-swap    none          swap      sw                              0 0
+
/dev/mapper/vg-root    /             ext4      noatime,nodiratime,defaults    0 1
+
/dev/sr0                /mnt/cdrom    auto      noauto,ro                      0 0
+
/dev/mapper/vg-portage  /usr/portage  reiserfs  noatime,nodiratime              0 0
+
/dev/mapper/vg-home    /home        xfs      noatime,nodiratime,osyncisdsync 0 0</pre>}}
+
  
= Lilo configuration =
+
== Enabling link-time optimizations ==
For oldschool geeks, an example for lilo bootloader. Emerge lilo with device-mapper support
+
<pre>
+
# echo 'sys-boot/lilo device-mapper' >> /etc/portage/package.use/lilo
+
# emerge lilo</pre>
+
  
{{code|/etc/lilo.conf|<pre>append="init=/linuxrc dolvm crypt_root=/dev/sda2 real_root=/dev/mapper/vg-root"
+
The ''link-time optimization'' feature defers optimizing the resulting executables to linking phase. This can result in better optimization of packages but is unsupported in Gentoo, and many packages simply fail to build.
boot=/dev/sda
+
 
compact
+
When using LTO, clang compiles units into LLVM byte-code rather than machine code. In order to support linking such object files, the [[gold]] linker must be installed and set as the default linker, as it does support plugins.
default=funtoo
+
 
lba32
+
Similarly, ''ar'' needs plugin support as well. Sadly, binutils ar doesn't support passing '--plugin'' option before the actual command. Thus, we need to create a wrapper for it in <code>/usr/local/bin/clang-ar</code>:
prompt
+
{{File
read-only
+
|/usr/local/bin/clang-ar|<pre>
timeout=50
+
#!/bin/sh
image=/boot/kernel-genkernel-x86_64-2.6.39
+
firstarg=${1}
initrd=/boot/initramfs-genkernel-x86_64-2.6.39
+
shift
label=funtoo
+
 
 +
exec /usr/bin/ar "${firstarg}" --plugin /usr/lib/llvm/LLVMgold.so "${@}"
 
</pre>}}
 
</pre>}}
= Syslinux bootloader setup =
+
 
Syslinux is another advanced bootloader which you can find on all live CD's.
+
If that's done, you can create a new environment override profile for LTO-enabled clang in <code>/etc/portage/env/clang-lt</code>:
<pre>
+
{{File
# emerge syslinux
+
|/etc/portage/env/clang-lt|<pre>
# mkdir /boot/extlinux
+
CC='clang'
# extlinux --install /boot/extlinux
+
CXX='clang++'
# dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/mbr.bin of=/dev/sda
+
CFLAGS="${CFLAGS} -O4"
- or -
+
CXXFLAGS="${CXXFLAGS} -O4"
# sgdisk /dev/sda --attributes=1:set:2
+
LDFLAGS="${LDFLAGS} -O4 -Wl,-plugin,/usr/lib/llvm/LLVMgold.so"
# dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of=/dev/sda, for GPT partition</pre>
+
AR='/usr/local/bin/clang-ar'
{{code|/boot/extlinux/extlinux.conf|<pre>LABEL kernel1_bzImage-3.2.1
+
RANLIB=':'
MENU LABEL Funtoo Linux bzImage-3.2.1
+
NM='nm --plugin /usr/lib64/llvm/LLVMgold.so'
LINUX /bzImage-3.2.1
+
INITRD /initramfs.cpio.gz
+
APPEND rootfstype=ext4 luks enc_root=/dev/sda2 lvm root=/dev/mapper/vg-root
+
 
</pre>}}
 
</pre>}}
  
= Final steps =
+
Note that the link-time optimizations were indirectly enabled here via ''-O4''. If you don't want to enable other optimizations enforced by ''-O3'', please use ''-flto'' instead. You need to also pass optimization flags when linking because that's where clang needs them.
Umount everything, close encrypted drive and reboot
+
<pre>umount /mnt/funtoo/proc (/dev, /home, /usr/portage, /boot)
+
vgchange -a n
+
cryptsetup luksClose /dev/sda2 dmcrypt_root</pre>
+
After reboot you will get the following:
+
<pre>>>> better-initramfs started. Kernel version 2.6.35-gentoo-r10
+
>>> Create all the symlinks to /bin/busybox.
+
>>> Initiating /dev/dir
+
>>> Getting LVM volumes up (if any)
+
Reding all physical volumes. This make take awhile...
+
No volume group found
+
No volume group found
+
>>> Opening encrypted partition and mapping to /dev/mapper/dmcrypt_root
+
Enter passphrase fore /dev/sda2:</pre>
+
Type your password
+
  
<pre>>>> Again, getting LVM volumes up (if any, after map dmcrypt).
+
You may also need to adjust the libdir path to plugin. Newer (live) versions of clang add `-plugin` when linking automatically, so `-Wl,-plugin`… is no longer necessary.
  Reading all physical volumes.  This may take a while...
+
 
  Found volume group "vg" using metadata type lvm2
+
== Using clang with distcc ==
  4 logical volume(s) in volume group "vg" now active
+
 
>>> Mounting rootfs to /newroot
+
In order to use clang on distcc client, additional symlinks have to be created in ''<code>/usr/lib*/distcc/bin</code>'':
>>> Umounting /sys and /proc.
+
<console>
>>> Switching root to /newroot and executing /sbin/init.
+
###i## ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang
INIT: version 2.88 booting
+
###i## ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang++
Loading /libexec/rc/console/keymap
+
</console>
  OpenRC 0.6.1 is starting up Funtoo Linux (x86_64)
+
...boot messages omitted for clarity
+
 
+
orion login: oleg
+
Password:
+
Last login: Thu Oct 14 20:49:21 EEST 2010 on tty1
+
oleg@orion ~ %</pre>
+
  
= Additional links =
+
{{GLW|src=http://wiki.gentoo.org/wiki/Clang}}
* [[gentoo-wiki:Root filesystem over LVM2, DM-Crypt and RAID|Root filesystem over LVM2, DM-Crypt, and RAID]]
+
* [http://wiki.archlinux.org/index.php/System_Encryption_with_LUKS_for_dm-crypt System Encryption with LUKS for dm-crypt]
+
  
 
[[Category:HOWTO]]
 
[[Category:HOWTO]]

Revision as of 14:29, January 28, 2014

Introduction

LLVM can be used as an alternative to GNU's compiler, GCC. The main benefit of using LLVM compilers instead of GCC is their lower memory usage, faster compile time and better diagnostics. There are some Benchmarks on the Clang and Phoronix homepages.

It may happen that some programs do not compile (like glibc) because they depend on GCC-specific language extensions [1] (this is why the whole BSD code can be compiled with LLVM but some GNU code cannot) or segfault after successful compilation with LLVM (like xorg-server) but after following this guide, the system will still be able to compile packages with gcc. So if something goes wrong, it can be switched back to gcc for the particular package by uncommenting lines in /etc/make.conf and the bug should be reported.

LLVM's C/C++ frontends clang and clang++ version 3.0 are stable enough to be self-hosting [2] and compile Boost [3], Qt [4], LibreOffice [5], FreeBSD [6], some parts of the Linux kernel [7] and more.

Further, using LLVM 3.0 and up, there is a third way to compile with LLVM: the dragonegg package creates a gcc-plugin, that uses LLVM's optimizers but parses the code and creates binaries with gcc, which means that everything that compiles and works with gcc should work with dragonegg also. This plugin can be enabled by using a single CFLAG. Since LLVM 3.0 the old llvm-gcc package is deprecated and replaced by dragonegg, so it will disappear from portage with llvm version 2.9.

LLVM Frontends

To be able to compile some sourcecode of a specific language, LLVM needs an appropriate frontend. There are clang, llvm-gcc and dragonegg in portage.

The goal of the Clang project is to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler.

llvm-gcc is a modified version of gcc that compiles C/ObjC programs into native objects, LLVM bitcode or LLVM assembly language, depending upon the options. As written in the previous section, dragonegg replaced llvm-gcc in version 3.0.

So after installing llvm, clang and dragonegg, you will be able to choose between gcc and llvm whenever you like or use them both at the same time.

Install LLVM and its Frontends

Simply emerge the packages on ~arch systems. On arch systems you have to unmask some packages first. dragonegg requires gcc's lto USE-flag to be set and works with gcc 4.5 and gcc 4.6.

# emerge llvm clang dragonegg

Note, that for clang++ the C++ headers search path is hardcoded to the active gcc profile. If you change the active gcc profile, or update gcc to a new version, you will have to remerge clang to update the search path.

To use dragonegg, run gcc as usual, with an extra command line argument -fplugin=/usr/lib/llvm/dragonegg.so If you change the active gcc profile, or update gcc to a new version, you will have to remerge dragonegg to update the plugin.

After the installation, check which CPUs are supported by using the command

# llvm-as < /dev/null | llc -mcpu=help

and then add the following lines to /etc/make.conf (uncommenting the lines you need) to enable compilation via LLVM, adapting the march-option according to the previous command:

in /etc/portage/make.conf:

{{{name}}}
{{{body}}}

Note

{{{1}}}

Have a look at clang's manpages for additional information. If you get errors that your compiler cannot produce code, you should check your flags, e.g. don't use -O4 -flto -S or stuff like that; the examples above will work.

Using clang with portage

Although Gentoo package tree is not designed to be used with compiler other than GCC, clang can be enforced on most of the packages through CC and CXX variables.

Please note, however, that many of Gentoo packages still don't build with clang and a few don't work correctly after being built. That's why we suggest using /etc/portage/env file to enable the use of clang per-package.

In order to do that, first create a new environment override to use in /etc/portage/env/clang:

{{{name}}}
{{{body}}}

Then you can enable use of clang for packages using the /etc/portage/package.env file:

{{{name}}}
{{{body}}}

If you want to use clang by default you can and need to specify some core packages. Here is small list of core packages that are currently failing on clang, but not that could be outdated:

{{{name}}}
{{{body}}}

in addition, it is recommend to add compiler flags there:

{{{name}}}
{{{body}}}

And in /etc/portage/package.env:

{{{name}}}
{{{body}}}

If you have app-portage/flaggie (package not on wiki - please add) installed, you can modify /etc/portage/package.env by running the following:

# flaggie app-foo/bar app-bar/baz +clang

Enabling link-time optimizations

The link-time optimization feature defers optimizing the resulting executables to linking phase. This can result in better optimization of packages but is unsupported in Gentoo, and many packages simply fail to build.

When using LTO, clang compiles units into LLVM byte-code rather than machine code. In order to support linking such object files, the gold linker must be installed and set as the default linker, as it does support plugins.

Similarly, ar needs plugin support as well. Sadly, binutils ar doesn't support passing '--plugin option before the actual command. Thus, we need to create a wrapper for it in /usr/local/bin/clang-ar:

{{{name}}}
{{{body}}}

If that's done, you can create a new environment override profile for LTO-enabled clang in /etc/portage/env/clang-lt:

{{{name}}}
{{{body}}}

Note that the link-time optimizations were indirectly enabled here via -O4. If you don't want to enable other optimizations enforced by -O3, please use -flto instead. You need to also pass optimization flags when linking because that's where clang needs them.

You may also need to adjust the libdir path to plugin. Newer (live) versions of clang add `-plugin` when linking automatically, so `-Wl,-plugin`… is no longer necessary.

Using clang with distcc

In order to use clang on distcc client, additional symlinks have to be created in /usr/lib*/distcc/bin:

# ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang
# ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang++


Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States

The content of this page is a derivative of a Gentoo Linux Wiki article. You can redistribute and/or modify it under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

(Original Article)