Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Boostrapping Clang: Updated guides?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
alecStewart1
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2022
Posts: 143

PostPosted: Tue Sep 24, 2024 4:16 pm    Post subject: Boostrapping Clang: Updated guides? Reply with quote

Hey gang!

So, I've been going through the process of trying to bootstrap Clang/LLVM and then use it as my system compiler for most things. Why? Because I thought it might be fun.

There's been a few guides thrown around, and I wanted to give what I've done so far, and hopefully we can come to have some updated guide. Gentoo recently introduced the clang-common package that does throw a minor wrench into some of the existing bootstrapping clang guides.

Here's what I did, although I've yet to figure out all the correct flags to get everything to compile nicely.

1. Install sys-devel/mold

I guess this step isn't necessary, but mold seems to have really improved the build time of packages over bfd or even lld. The project has some metrics that shows that this isn't just a bunch of foobar. I guess this is a bit of heresy if you're some clang/llvm purist, but I'm not so I don't care.

After installing mold, you can use

Code:

-fuse-ld=mold


In your LDFLAGS and watch the fun. Mold is compatible with pretty much all standard linker arguments, but it does offer some interesting options of it's own.

2. Set some USE flags in your make.conf

In your USE flags, add

Code:

-clang -llvm -llvm-libunwind


This will be temporary while we install

3. Create a env file for using clang in /etc/portage/env/

I called mine clang-mold to keep it simple. The contents are as follows:

Code:

# To clarify, this is going off of memory as I'm typing this during lunch on my work laptop, not my Gentoo machine.
CC="clang"
CXX="clang++"
CPP="clang-cpp" # trust me, you will want this set
LD="ld.lld" # with -fuse-ld=mold we'll end up using mold anyway
AR="llvm-ar"
NM="llvm-nm"
RANLIB="llvm-ranlib"
STRIP="llvm-strip"
OBJCOPY="llvm-objcopy"
OBJDUMP="llvm-objdump"

CFLAGS="-march=native -O2 -pipe -fPIC -flto=thin -fomit-frame-pointer -fstack-protector-strong -D_FORTIFY_SOURCE=3"
CXXFLAGS="${CFLAGS} -stdlib=libstdc++ "
LDFLAGS="-stdlib=libstdc++ -fuse-ld=mold -flto=thin -rtlib=compiler-rt -unwindlib=libunwind -Wl,-O2 -Wl,--pie -Wl,--sort-common -Wl,--as-needed -Wl,--icf=safe -Wl,-S"
LLVM=1
LLVM_ISA=1


You can obviously add more if you need them, I don't really know if I need more to pass -lgcc_s or -lm to the linker or not.

4. Install the clang/llvm toolchain using gcc, like normal

Do your old

Code:
emerge -av clang llvm lld clang-runtime clang-common


You will see that libunwind is installed and not the llvm-libunwind package, but don't worry.

5. Enable our USE flags.

Once everything compiles, the previous flags in our USE falgs in /etc/portage/make.conf we added earlier should now be changed to.

Code:

clang llvm llvm-libunwind


6. Add the env configuration to the relevant packages

In /etc/portage/package.env, we should have

Code:

sys-devel/llvm clang-mold
sys-libs/libcxx clang-mold # you may end up having this and the next package installed with some other package in the future
sys-libs/libcxxabi clang-mold
sys-libs/compiler-rt clang-mold
sys-libs/compiler-rt-sanitizers clang-mold
sys-libs/llvm-libunwind clang-mold
sys-devel/lld clang-mold
sys-devel/clang-runtime clang-mold
sys-devel/clang-common clang-mold # you probably don't need this but I wanted to be cautious
sys-devel/clang clang-mold


7. Reinstall the clang/llvm toolchain

Do

Code:
emerge -av clang llvm lld clang-runtime clang-common compiler-rt compiler-rt-sanitizers


You'll notice that libunwind will be uninstalled and llvm-libunwind will replace it now.

Go watch a movie or do some chore around the house or whatever while everything recompiles. Mold will help speed things up a little, but it still will take a minute.

I can't guarantee it'll all compile nicely, and you may need to adjust some flags here or there. I'm still figuring out what flags/envvars to adjust, but this is the general flow you want to follow.

8. (Optional) See if clang works a your system compiler.

You'll likely need another env configuration file to account for the packages that just won't compile with clang, but with llvm-libunwind and using the -unwindlib=libunwind flag many things should compile fine, but of course you'll have to experiment some.

Caveats

You might end up having 2 versions of clang installed if you're using the ~* keyword for your architecture. If so, you might end up having to do:

Code:

emerge -av clang:18 clang:19 llvm:18 llvm:19 # etc


All version need to be compiled by gcc first.

References

A few posts I looked at:

https://forums.gentoo.org/viewtopic-t-1102590-start-0.html

https://wiki.gentoo.org/wiki/Clang/Bootstrapping

https://xdaforums.com/t/using-gentoo-why-using-gcc-use-clang-compiler-instead-fast-and-less-ram-eater.4510915/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum