Difference between pages "Clang" and "Install/BootLoader"

From Funtoo
(Difference between pages)
Jump to navigation Jump to search
m
 
 
Line 1: Line 1:
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.
<noinclude>
{{InstallPart|boot loader configuration}}
</noinclude>
=== Installing a Bootloader ===


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.
These install instructions show you how to use GRUB to boot using BIOS (old-school) or UEFI (new-school).


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.
==== Old School (BIOS) ====


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.
If you're using the BIOS to boot, setting up GRUB, the bootloader, is pretty easy.


==LLVM Frontends==
To use this recommended boot method, first emerge <code>boot-update</code>. This will also cause <code>grub-2</code> to be merged, since it is a dependency of <code>boot-update</code>.
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.
<console>
<console>
###i## emerge llvm clang dragonegg
(chroot) # ##i##emerge boot-update
</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.


To use dragonegg, run gcc as usual, with an extra command line argument <tt>-fplugin=/usr/lib/llvm/dragonegg.so</tt>
Then, edit <code>/etc/boot.conf</code> and specify "<code>Funtoo Linux genkernel</code>" as the <code>default</code> setting at the top of the file, replacing <code>"Funtoo Linux"</code>.  
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
<code>/etc/boot.conf</code> should now look like this:
<console>
###i## llvm-as < /dev/null | llc -mcpu=help
</console>
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:
 
in <code>/etc/portage/make.conf</code>:


<pre>
<pre>
# LLVM
boot {
#CC="/usr/bin/clang"
generate grub
#CXX="/usr/bin/clang++"
default "Funtoo Linux genkernel"  
timeout 3
}


# llvm-gcc for C++ code and fortran
"Funtoo Linux" {
# llvm-gcc is deprecated and only used with LLVM 2.9
kernel bzImage[-v]
#CC="/usr/bin/llvm-gcc"
}
#CXX="/usr/bin/llvm-g++"
#CPP="/usr/bin/llvm-cpp"
#F77="/usr/bin/llvm-gfortran"


# Flags for clang: Insert your arch here instead of k8 and have a look at the manpage of clang for flag descriptions.
"Funtoo Linux genkernel" {
# Some gcc flags like -pipe and -pthread also work, though they might be ignored by clang.
kernel kernel[-v]
#CFLAGS="-march=k8 -O2"
initrd initramfs[-v]
params += real_root=auto
}


# Flags for dragonegg; just use all the gcc flags you like and append -fplugin=/path/to/dragonegg.so
"Funtoo Linux better-initramfs" {
#CFLAGS="-march=k8 -O2 -fplugin=/usr/lib64/llvm/dragonegg.so"
kernel vmlinuz[-v]
initrd /initramfs.cpio.gz
}
</pre>
</pre>


{{Fancynote| 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.}}
Please read <code>man boot.conf</code> for further details.


== Using clang with portage ==
===== Running grub-install and boot-update =====


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.
Finally, we will need to actually install the GRUB boot loader to your disk, and also run <code>boot-update</code> which will generate your boot loader configuration file:


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.
<console>
(chroot) # ##i##grub-install --no-floppy /dev/sda
(chroot) # ##i##boot-update
</console>


In order to do that, first create a new environment override to use in <code>/etc/portage/env/clang</code>:
Now you need to update your boot loader configuration file:
<console>
(chroot) # ##i##boot-update
</console>
You only need to run <code>grub-install</code> when you first install Funtoo Linux, but you need to re-run <code>boot-update</code> every time you modify your <code>/etc/boot.conf</code> file, so your changes are applied on next boot.
 
==== New School (UEFI) ====
 
If you're using UEFI to boot, setting up the boot loader is a bit more complicated for now, but this process will be improving soon. Perform the following steps.
 
===== Emerging GRUB =====


<pre>
You will still use GRUB as a boot loader, but before emerging grub, you will need to enable EFI booting. To do this,
CC=clang
add the following line to <code>/etc/make.conf</code>:
CXX=clang++
</pre>


Then you can enable use of clang for packages using the [[:/etc/portage/env|/etc/portage/package.env]] file:
For x86-64bit systems:


<pre>
<pre>
app-foo/bar clang
GRUB_PLATFORMS="efi-64"
app-bar/baz clang
</pre>
</pre>


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:
For x86-32bit systems:


<pre>
<pre>
CC=gcc
GRUB_PLATFORMS="efi-32"
CXX=g++
</pre>
</pre>


in addition, it is recommended to add compiler flags there (<tt>/etc/portage/env/gcc</tt>:
Then, <code>emerge boot-update</code>. You will notice <code>grub</code> and <code>efibootmgr</code> getting pulled in as dependencies. This is expected and good:


<pre>
<console>
CFLAGS="-O2 -march=native -mtune=native -pipe"
(chroot) # ##i##emerge boot-update
CXXFLAGS="-O2 -march=native -mtune=native -pipe"
</console>
LDFLAGS="-Wl,--as-needed"
#You can disable gold link here
#EXTRA_ECONF="--enable-gold=default"
</pre>


And in <code>/etc/portage/package.env</code>:
===== Installing GRUB =====


<pre>
Now, for the magic of getting everything in place for booting. You should copy your kernel and initramfs (if you have one -- you will if you are following the default install) to <tt>/boot</tt>. GRUB will boot those. But how do we get UEFI to boot GRUB? Well, we need to run the following command (for 32 bit simply set it as i386-efi):
#---------------CORE PACKAGES TO BUILD WITH GCC:
sys-apps/which gcc
sys-fs/reiserfsprogs gcc
sys-libs/ncurses gcc
sys-libs/zlib gcc
sys-apps/busybox gcc
sys-fs/e2fsprogs gcc
sys-devel/binutils gcc
sys-libs/glibc gcc
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>


If you have {{Package|app-portage/flaggie}} installed, you can modify <code>/etc/portage/package.env</code> by running the following:
<console>
<console>
###i## flaggie app-foo/bar app-bar/baz +clang
(chroot) # ##i##grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id="Funtoo Linux [GRUB]" --recheck /dev/sda
</console>
</console>
This command will simply install all the stuff to <tt>/boot/EFI</tt> and <tt>/boot/grub</tt> that your system needs to boot. In particular, the <tt>/boot/EFI/grub/grubx64.efi</tt> file will be created. This is the GRUB boot image that UEFI will load and start.


== Enabling link-time optimizations ==
A more detailed explanation of the flags used in the above command:
* <code>--target=x86_64-efi</code>: Tells GRUB that we want to install it in a way that allows it to boot in UEFI
* <code>--efi-directory=/boot</code>: All GRUB UEFI files will be installed in ''/boot''
* <code>--bootloader-id="Funtoo Linux [GRUB]"</code>: This flag is not necessary for GRUB to boot. However, it allows you to change the text of the boot option in the UEFI BIOS. The stuff in the quotes can be set to anything that you would like.
* <code>--recheck</code>: If a device map already exists on the disk or partition that GRUB is being installed on, it will be removed.
* <code>/dev/sda</code>:The device that we are installing GRUB on.


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.
===== Configuring GRUB =====


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.
OK, now UEFI has the GRUB image it needs to boot. But we still need to configure GRUB itself so it finds and boots your kernel and initramfs. This is done by performing the following steps. Since boot-update doesn't yet support UEFI, we will use boot-update, but then edit our <code>/boot/grub/grub.cfg</code> to support UEFI booting.  


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>:
First, you will need to edit <code>/etc/boot.conf</code>. Format this as you would if you were booting without UEFI. If you are not sure how this should look, below is an example of what it could look like if you are booting from an unencrypted ext4 partition:


<pre>
{{file|name=/etc/boot.conf|desc=|body=
#!/bin/sh
boot {
firstarg=${1}
        generate grub
shift
        default "Funtoo Linux"
        timeout 3
}


exec /usr/bin/ar "${firstarg}" --plugin /usr/lib/llvm/LLVMgold.so "${@}"
"Funtoo Linux" {
</pre>
        kernel vmlinuz[-v]
        params += rootfstype=ext4 root=/dev/sda2
}
}}


If that's done, you can create a new environment override profile for LTO-enabled clang in <code>/etc/portage/env/clang-lt</code>:
After you have edited your <code>/etc/boot.conf</code> file, run <code>boot-update</code>. You should now have a <code>/boot/grub/grub.cfg</code> file, which you can edit using the following command:


<pre>
<console>
CC='clang'
# ##i##nano /boot/grub/grub.cfg
CXX='clang++'
</console>
CFLAGS="${CFLAGS} -O4"
CXXFLAGS="${CXXFLAGS} -O4"
LDFLAGS="${LDFLAGS} -O4 -Wl,-plugin,/usr/lib/llvm/LLVMgold.so"
AR='/usr/local/bin/clang-ar'
RANLIB=':'
NM='nm --plugin /usr/lib64/llvm/LLVMgold.so'
</pre>


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.
To get your <code>/boot/grub/grub.cfg</code> to support booting with UEFI, make the following changes. Below the existing insmod lines, add the following lines. Both of these involve adding support for the UEFI framebuffer to GRUB.:


== Using clang with distcc ==
<pre>
  insmod efi_gop
  insmod efi_uga
</pre>


In order to use clang on distcc client, additional symlinks have to be created in <code>/usr/lib*/distcc/bin</code>:
Then, change the <code>set gfxpayload</code> line to read as follows. UEFI does not support text mode, so we will keep video initialized to the current resolution.:
<console>
###i## ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang
###i## ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang++
</console>


{{GLW|src=http://wiki.gentoo.org/wiki/Clang}}
<pre>
  set gfxpayload=keep
</pre>


[[Category:HOWTO]]
You can now save your changes by pressing <code>Control-X</code> and answering <code>y</code> when asked if you want to save the modified buffer. When prompted for a filename, hit Enter to use the existing filename.

Revision as of 19:48, January 1, 2015


   Note

This is a template that is used as part of the Installation instructions which covers: boot loader configuration. Templates are being used to allow multiple variant install guides that use most of the same re-usable parts.


Installing a Bootloader

These install instructions show you how to use GRUB to boot using BIOS (old-school) or UEFI (new-school).

Old School (BIOS)

If you're using the BIOS to boot, setting up GRUB, the bootloader, is pretty easy.

To use this recommended boot method, first emerge boot-update. This will also cause grub-2 to be merged, since it is a dependency of boot-update.

(chroot) # emerge boot-update

Then, edit /etc/boot.conf and specify "Funtoo Linux genkernel" as the default setting at the top of the file, replacing "Funtoo Linux".

/etc/boot.conf should now look like this:

boot {
	generate grub
	default "Funtoo Linux genkernel" 
	timeout 3 
}

"Funtoo Linux" {
	kernel bzImage[-v]
}

"Funtoo Linux genkernel" {
	kernel kernel[-v]
	initrd initramfs[-v]
	params += real_root=auto 
} 

"Funtoo Linux better-initramfs" {
	kernel vmlinuz[-v]
	initrd /initramfs.cpio.gz
}

Please read man boot.conf for further details.

Running grub-install and boot-update

Finally, we will need to actually install the GRUB boot loader to your disk, and also run boot-update which will generate your boot loader configuration file:

(chroot) # grub-install --no-floppy /dev/sda
(chroot) # boot-update

Now you need to update your boot loader configuration file:

(chroot) # boot-update

You only need to run grub-install when you first install Funtoo Linux, but you need to re-run boot-update every time you modify your /etc/boot.conf file, so your changes are applied on next boot.

New School (UEFI)

If you're using UEFI to boot, setting up the boot loader is a bit more complicated for now, but this process will be improving soon. Perform the following steps.

Emerging GRUB

You will still use GRUB as a boot loader, but before emerging grub, you will need to enable EFI booting. To do this, add the following line to /etc/make.conf:

For x86-64bit systems:

GRUB_PLATFORMS="efi-64"

For x86-32bit systems:

GRUB_PLATFORMS="efi-32"

Then, emerge boot-update. You will notice grub and efibootmgr getting pulled in as dependencies. This is expected and good:

(chroot) # emerge boot-update
Installing GRUB

Now, for the magic of getting everything in place for booting. You should copy your kernel and initramfs (if you have one -- you will if you are following the default install) to /boot. GRUB will boot those. But how do we get UEFI to boot GRUB? Well, we need to run the following command (for 32 bit simply set it as i386-efi):

(chroot) # grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id="Funtoo Linux [GRUB]" --recheck /dev/sda

This command will simply install all the stuff to /boot/EFI and /boot/grub that your system needs to boot. In particular, the /boot/EFI/grub/grubx64.efi file will be created. This is the GRUB boot image that UEFI will load and start.

A more detailed explanation of the flags used in the above command:

  • --target=x86_64-efi: Tells GRUB that we want to install it in a way that allows it to boot in UEFI
  • --efi-directory=/boot: All GRUB UEFI files will be installed in /boot
  • --bootloader-id="Funtoo Linux [GRUB]": This flag is not necessary for GRUB to boot. However, it allows you to change the text of the boot option in the UEFI BIOS. The stuff in the quotes can be set to anything that you would like.
  • --recheck: If a device map already exists on the disk or partition that GRUB is being installed on, it will be removed.
  • /dev/sda:The device that we are installing GRUB on.
Configuring GRUB

OK, now UEFI has the GRUB image it needs to boot. But we still need to configure GRUB itself so it finds and boots your kernel and initramfs. This is done by performing the following steps. Since boot-update doesn't yet support UEFI, we will use boot-update, but then edit our /boot/grub/grub.cfg to support UEFI booting.

First, you will need to edit /etc/boot.conf. Format this as you would if you were booting without UEFI. If you are not sure how this should look, below is an example of what it could look like if you are booting from an unencrypted ext4 partition:

   /etc/boot.conf
boot {
        generate grub
        default "Funtoo Linux"
        timeout 3
}

"Funtoo Linux" {
        kernel vmlinuz[-v]
        params += rootfstype=ext4 root=/dev/sda2
}

After you have edited your /etc/boot.conf file, run boot-update. You should now have a /boot/grub/grub.cfg file, which you can edit using the following command:

root # nano /boot/grub/grub.cfg


To get your /boot/grub/grub.cfg to support booting with UEFI, make the following changes. Below the existing insmod lines, add the following lines. Both of these involve adding support for the UEFI framebuffer to GRUB.:

  insmod efi_gop
  insmod efi_uga

Then, change the set gfxpayload line to read as follows. UEFI does not support text mode, so we will keep video initialized to the current resolution.:

  set gfxpayload=keep

You can now save your changes by pressing Control-X and answering y when asked if you want to save the modified buffer. When prompted for a filename, hit Enter to use the existing filename.