Difference between pages "Talk:Funtoo Linux Installation" and "Clang"

(Difference between pages)
(make.conf video card addition)
 
m (Install LLVM and its Frontends)
 
Line 1: Line 1:
 +
==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.
  
== document rules ==
+
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.
The grub-install command does not have the --no-floppy option.
+
--ferdy
+
  
 +
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.
  
The crazy chroot command, while necessary I guess, is getting out of control. Is there a way we can trim it down and still keep things sane?
+
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.
  
-Daniel
+
==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.
  
AFAK this is a SysRescueCD specific issue, alternative:
+
The goal of the Clang project is to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler.
  
* '''chroot /funtoo/mount/path /bin/bash''' as usual
+
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.
* once chrooted just unset ''path'': '''unset path'''
+
  
-Adrien
+
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.
  
Here are the rules for this doc -
+
== 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>
 +
###i## emerge llvm clang dragonegg
 +
</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.
  
# Keep things simple, and reasonably short.
+
To use dragonegg, run gcc as usual, with an extra command line argument <tt>-fplugin=/usr/lib/llvm/dragonegg.so</tt>
# Explain all steps just enough to provide context, but not enough to interrupt the flow of the document.
+
If you change the active gcc profile, or update gcc to a new version, you will have to remerge dragonegg to update the plugin.
# Integrate reasonable defaults into the doc that users can and will be expected to use successfully for their own installs. Don't include millions of "if/then" statements. Make reasonable assumptions.
+
# Recommend preferred approaches for install rather than exhaustively cover all possibilities.
+
# Don't use this doc to make your own personal recommendations.
+
# Don't cater to a specific sub-community - keep things general.
+
# Following this guide should result in a successful installation of Funtoo Linux.
+
# Optional topics should not clutter the main flow of the document - break them out to their own pages.
+
# Installing Funtoo can be educational, but the primary goal of this document is to help users get Funtoo Linux up and running successfully. Changes to the doc should be made in the hopes of increasing the % of successful installs. Educate, but do not overwhelm.
+
  
OK, I think I have repeated myself enough :)
+
After the installation, check which CPUs are supported by using the command
 +
<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:
  
<span class="shell">Hello, world.
+
in <code>/etc/portage/make.conf</code>:
<span style="color: #f00">This is line 2.</span>
+
{{File
</span>
+
|/etc/portage/make.conf|<pre>
 +
# LLVM
 +
#CC="/usr/bin/clang"
 +
#CXX="/usr/bin/clang++"
  
<div class="shell">foo / # emerge -p metalog<br/><br/><span class="shell_green">These are the packages that would be merged, in order:</span>
+
# llvm-gcc for C++ code and fortran
<br/>
+
# llvm-gcc is deprecated and only used with LLVM 2.9
Calculating dependencies... done!<br/>
+
#CC="/usr/bin/llvm-gcc"
[ebuild  R    ] app-admin/metalog-2<br/><br/>
+
#CXX="/usr/bin/llvm-g++"
&nbsp;* IMPORTANT: 1 news items need reading for repository 'gentoo'.<br/>
+
#CPP="/usr/bin/llvm-cpp"
&nbsp;* Use eselect news to read news items.
+
#F77="/usr/bin/llvm-gfortran"
</div>
+
  
== stage3 filename changed ==
+
# Flags for clang: Insert your arch here instead of k8 and have a look at the manpage of clang for flag descriptions.
 +
# Some gcc flags like -pipe and -pthread also work, though they might be ignored by clang.
 +
#CFLAGS="-march=k8 -O2"
  
stage3-current.tar.xz -> stage3-latest.tar.xz
+
# Flags for dragonegg; just use all the gcc flags you like and append -fplugin=/path/to/dragonegg.so
:so change to a more universal command stage3*.tar.xz [[User:666threesixes666|666threesixes666]] ([[User talk:666threesixes666|talk]]) 02:22, 20 March 2014 (UTC)
+
#CFLAGS="-march=k8 -O2 -fplugin=/usr/lib64/llvm/dragonegg.so"
 +
</pre>}}
  
== broken SysRescueCD links ==
+
{{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.
The SysRescueCD links is broken since they link to specific files no longer mirrored.
+
  
[[User:Brantgurga|Brantgurga]] ([[User talk:Brantgurga|talk]])
+
== Using clang with portage ==
== reasons for filesystem recommendations ==
+
I'd like to see more reasoning for the filesystem recommendations, at least a link to another page.
+
  
[[User:Brantgurga|Brantgurga]] ([[User talk:Brantgurga|talk]])
+
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.
  
== make.conf video card addition ==
+
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 ''<code>/etc/portage/env/clang</code>'':
 +
<console>
 +
###i## nano /etc/portage/env/clang
 +
CC=clang
 +
CXX=clang++
 +
</console>
 +
 
 +
Then you can enable use of clang for packages using ''[[:/etc/portage/env|/etc/portage/package.env]]'' file:
 +
in ''<code>/etc/portage/package.env</code>'':
 +
<console>
 +
###i## nano /etc/portage/package.env
 +
app-foo/bar clang
 +
app-bar/baz clang
 +
</console>
 +
 
 +
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:
 +
 
 +
You need to add ''<code>/etc/portage/env/gcc</code>'':
 +
<console>
 +
###i## nano /etc/portage/env/gcc
 +
CC=gcc
 +
CXX=g++
 +
</console>
 +
 
 +
in addition, it is recommend to add compiler flags there:
 +
<console>
 +
###i## nano /etc/portage/env/gcc
 +
CFLAGS="-O2 -march=native -mtune=native -pipe"
 +
CXXFLAGS="-O2 -march=native -mtune=native -pipe"
 +
LDFLAGS="-Wl,--as-needed"
 +
#You can disable gold link here
 +
#EXTRA_ECONF="--enable-gold=default"
 +
</console>
 +
 
 +
And in ''<code>/etc/portage/package.env</code>'':
 +
<console>
 +
###i## nano /etc/portage/package.env
 +
#---------------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
 +
</console>
 +
 
 +
If you have {{Package|app-portage/flaggie}} installed, you can modify ''<code>/etc/portage/package.env</code>'' by running the following:
 +
<console>
 +
###i## flaggie app-foo/bar app-bar/baz +clang
 +
</console>
 +
 
 +
== 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 ''<code>/usr/local/bin/clang-ar</code>'':
 +
<console>
 +
###i## nano /usr/local/bin/clang-ar
 +
#!/bin/sh
 +
firstarg=${1}
 +
shift
 +
 
 +
exec /usr/bin/ar "${firstarg}" --plugin /usr/lib/llvm/LLVMgold.so "${@}"
 +
</console>
 +
 
 +
If that's done, you can create a new environment override profile for LTO-enabled clang:
 +
 
 +
in ''<code>/etc/portage/env/clang-lt</code>'':
 +
<console>
 +
CC='clang'
 +
CXX='clang++'
 +
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'
 +
</console>
  
might be a good idea to insert a edit make.conf and add video card = nouveau or intel or intel i915 section? [[Special:Contributions/75.135.156.3|75.135.156.3]] 22:13, 19 March 2014 (UTC) aka 666 ARRR!!!!
+
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.
:annnndddd INPUT_DEVICES too....  make.conf should probably be wrapped in precollapsed <div tags since its starting to become so populated. [[User:666threesixes666|666threesixes666]] ([[User talk:666threesixes666|talk]]) 05:47, 20 March 2014 (UTC)
+
  
== force toc ==
+
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.
<nowiki>{{#ask: [[Category:First Steps]] | format=category }}</nowiki>
+
ask show as category at the very bottom of the article blows away the toc.  so <nowiki>__FORCETOC__</nowiki> to force it.  i also looked into using iframes, but they are a security hazard to load the category.  ajax could do it but im pretty sure an admin would have to patch that in. id still rather have huge TOC over no toc....[[User:666threesixes666|666threesixes666]] ([[User talk:666threesixes666|talk]]) 02:22, 20 March 2014 (UTC)
+
  
== resolv.conf assumptions ==
+
== Using clang with distcc ==
copying the resolv.conf assumes a sane resolv.conf and default location are observed by host media.
+
  
move down to already chrooted in to funtoo
+
In order to use clang on distcc client, additional symlinks have to be created in ''<code>/usr/lib*/distcc/bin</code>'':
 
<console>
 
<console>
###b## echo "nameserver 8.8.8.8" > /etc/resolv.conf
+
###i## ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang
 +
###i## ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang++
 
</console>
 
</console>
  
leave no room for error, make google do the heavy lifting for the install.
+
{{GLW|src=http://wiki.gentoo.org/wiki/Clang}}
  
<pre>
+
[[Category:HOWTO]]
xubuntu@xubuntu:/mnt/funtoo$ cat /etc/resolv.conf
+
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
+
#    DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
+
nameserver 127.0.0.1</pre>
+

Revision as of 14:24, 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:

# nano /etc/portage/env/clang
CC=clang
CXX=clang++

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

# nano /etc/portage/package.env
app-foo/bar clang
app-bar/baz clang

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:

You need to add /etc/portage/env/gcc:

# nano /etc/portage/env/gcc
CC=gcc
CXX=g++

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

# nano /etc/portage/env/gcc
CFLAGS="-O2 -march=native -mtune=native -pipe"
CXXFLAGS="-O2 -march=native -mtune=native -pipe"
LDFLAGS="-Wl,--as-needed"
#You can disable gold link here
#EXTRA_ECONF="--enable-gold=default"

And in /etc/portage/package.env:

# nano /etc/portage/package.env
#---------------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

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:

# nano /usr/local/bin/clang-ar
#!/bin/sh
firstarg=${1}
shift

exec /usr/bin/ar "${firstarg}" --plugin /usr/lib/llvm/LLVMgold.so "${@}"

If that's done, you can create a new environment override profile for LTO-enabled clang:

in /etc/portage/env/clang-lt:

CC='clang'
CXX='clang++'
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'

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)