Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 786405

Summary: sys-kernel/genkernel - LLVM + Clang + LTO support
Product: Gentoo Hosted Projects Reporter: Michiel Hazelhof <michiel>
Component: genkernelAssignee: Gentoo Genkernel Maintainers <genkernel>
Status: RESOLVED FIXED    
Severity: enhancement CC: Adrian.Bassett, bertrand, hjckr, matthew, michiel
Priority: Normal Keywords: InVCS
Version: unspecified   
Hardware: All   
OS: Linux   
Whiteboard:
Package list:
Runtime testing required: ---

Description Michiel Hazelhof 2021-04-28 11:13:15 UTC
With the new LTO patches in the kernel it would be nice to be able to use them in conjunction with genkernel.
As far as I can tell the following commandlines do the trick:

make CC=clang LD=ld.lld NM=llvm-nm AR=llvm-ar LLVM_IAS=1 menuconfig (choose LTO)
make CC=clang LD=ld.lld NM=llvm-nm AR=llvm-ar LLVM_IAS=1

Without any of these options LTO disappears from the .config
Apart from the CC and LD options I found no solution for the other flags.

Reproducible: Always
Comment 1 Thomas Deutschmann (RETIRED) gentoo-dev 2021-04-28 12:13:09 UTC
I don't think we will add explicit support for this.

You can set compiler to clang/llvm already, please have a look at /etc/genkernel.conf or read man page (--kernel-cc and friends). You would just need to pass LLVM_IAS=1 to your genkernel call in addition if you want to enable LLVM assembler.
Comment 2 Michiel Hazelhof 2021-04-28 13:06:21 UTC
Not looking for explecit support, but I cannot figure out how to properly pass the NM=llvm-nm AR=llvm-ar LLVM_IAS=1 parameters to the make command.
Comment 3 Thomas Deutschmann (RETIRED) gentoo-dev 2021-04-28 13:25:15 UTC
Please read man page (`man genkernel`). You can either pass "--kernel-cc=clang ..." or set values in /etc/genkernel.conf.
Comment 4 Michiel Hazelhof 2021-04-28 13:29:22 UTC
That doesn't work, both:
genkernel all --install --no-splash --oldconfig --kernel-cc="clang LD=ld.lld NM=llvm-nm AR=llvm-ar LLVM_IAS=1"
and
genkernel all --install --no-splash --oldconfig "--kernel-cc=clang LD=ld.lld NM=llvm-nm AR=llvm-ar LLVM_IAS=1"

immediately give "Restart config..." while a manual "make CC=clang LD=ld.lld NM=llvm-nm AR=llvm-ar LLVM_IAS=1" works fine.
Comment 5 Thomas Deutschmann (RETIRED) gentoo-dev 2021-04-28 13:35:11 UTC
Please read man page. --kernel-cc is just for CC. You have to use similar commands for NM, AR...
Comment 6 Michiel Hazelhof 2021-04-28 13:38:08 UTC
(In reply to Thomas Deutschmann from comment #5)
> Please read man page. --kernel-cc is just for CC. You have to use similar
> commands for NM, AR...

That simply doesn't work, the man page does not give an option to add these parameters!

See (this doesn't even include the "freeform" LLVM_IAS=:
genkernel all --install --no-splash --oldconfig --kernel-cc=clang --kernel-ld=ld.lld --kernel-nm=llvm-nm --kernel-ar=llvm-ar
ERROR: Unknown option '--kernel-nm=llvm-nm'!
Comment 7 Thomas Deutschmann (RETIRED) gentoo-dev 2021-04-28 13:51:32 UTC
OK, we are not exposing all stuff, you can overwrite everything listed in https://gitweb.gentoo.org/proj/genkernel.git/tree/defaults/config.sh?h=v4.2.1#n40. Please don't edit /usr/share/genkernel/defaults/config.sh directly, set KERNEL_NM in /etc/genkernel.conf for example.
Comment 8 Michiel Hazelhof 2021-04-28 15:21:28 UTC
After overriding the AR and NM it still drops to "Restart config...", while the config is valid when the make command is called manually.
What could be wrong?
Comment 9 Nikolay Kichukov 2021-05-14 08:39:10 UTC
same here, any workarounds apart from manual build?
Comment 10 Larry the Git Cow gentoo-dev 2021-07-06 21:27:38 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=a30b7139cfad7261c84345b136c671744e64ec29

commit a30b7139cfad7261c84345b136c671744e64ec29
Author:     Thomas Deutschmann <whissi@gentoo.org>
AuthorDate: 2021-07-06 20:22:12 +0000
Commit:     Thomas Deutschmann <whissi@gentoo.org>
CommitDate: 2021-07-06 20:59:30 +0000

    gen_cmdline.sh: parse_cmdline(): Allow to override AS, AR, NM & Co via CLI
    
    Bug: https://bugs.gentoo.org/786405
    Signed-off-by: Thomas Deutschmann <whissi@gentoo.org>

 doc/genkernel.8.txt | 44 +++++++++++++++++++++++++++++++-
 gen_cmdline.sh      | 72 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 genkernel.conf      | 38 +++++++++++++++++++---------
 3 files changed, 135 insertions(+), 19 deletions(-)
Comment 11 Thomas Deutschmann (RETIRED) gentoo-dev 2021-07-06 21:35:34 UTC
With this change I was able to compile kernel using CLANG, even with LTO enabled.
Comment 12 Thomas Deutschmann (RETIRED) gentoo-dev 2021-07-06 23:11:03 UTC
Fixed in >=sys-kernel/genkernel-4.2.2!
Comment 13 Michiel Hazelhof 2021-07-15 10:16:27 UTC
Seems to work fine, many many thanks for the patches!

One tiny question, is it ok to do this: KERNEL_MAKE="make LLVM=1 LLVM_IAS=1 ?

For anyone finding this bug:

genkernel.conf:
KERNEL_AS="llvm-as"
KERNEL_AR="llvm-ar"
KERNEL_CC="clang"
KERNEL_LD="ld.lld"
KERNEL_NM="llvm-nm"
UTILS_AS="llvm-as"
UTILS_AR="llvm-ar"
UTILS_CC="clang"
UTILS_CXX="clang++"
UTILS_LD="ld.lld"
UTILS_NM="llvm-nm"

invoking menuconfig:
make CC=clang LD=ld.lld NM=llvm-nm AR=llvm-ar LLVM_IAS=1 menuconfig

invoking the build:
genkernel all --install --no-splash --oldconfig --utils-objcopy=llvm-objcopy --utils-objdump=llvm-objdump --utils-readelf=llvm-readelf --utils-strip=llvm-strip --utils-ranlib=llvm-ranlib --kernel-objcopy=llvm-objcopy --kernel-objdump=llvm-objdump --kernel-readelf=llvm-readelf --kernel-strip=llvm-strip --kernel-ranlib=llvm-ranlib
Comment 14 Thomas Deutschmann (RETIRED) gentoo-dev 2021-07-15 11:04:28 UTC
No, it's not. Most build systems won't expected spaces in values so you will run into errors due to quoting/word splitting. What you can do: Set this variables before running genkernel, i.e.

LLVM=1 LLVM_IAS=1 genkernel ...
Comment 15 Morteza 2021-11-06 10:49:01 UTC
(In reply to Thomas Deutschmann from comment #14)
> No, it's not. Most build systems won't expected spaces in values so you will
> run into errors due to quoting/word splitting. What you can do: Set this
> variables before running genkernel, i.e.
> 
> LLVM=1 LLVM_IAS=1 genkernel ...

Just emerged gentoo-sources with clang-13 appending LLVM=1 just doesn't make kernel compiled with clang I had to change KERNEL_* in config file

I'm using genkernel as a hook for gentoo-sources like this
post_pkg_postinst() {
	LLVM=1 genkernel --kernel-config="/etc/portage/savedconfig/sys-kernel/gentoo-sources/.config" all || die
	eclean-kernel
	grub-mkconfig -o /boot/grub/grub.cfg
}