Skip to content

Commit a0d1c95

Browse files
committed
kbuild: support LLVM=1 to switch the default tools to Clang/LLVM
As Documentation/kbuild/llvm.rst implies, building the kernel with a full set of LLVM tools gets very verbose and unwieldy. Provide a single switch LLVM=1 to use Clang and LLVM tools instead of GCC and Binutils. You can pass it from the command line or as an environment variable. Please note LLVM=1 does not turn on the integrated assembler. You need to pass LLVM_IAS=1 to use it. When the upstream kernel is ready for the integrated assembler, I think we can make it default. We discussed what we need, and we agreed to go with a simple boolean flag that switches both target and host tools: https://lkml.org/lkml/2020/3/28/494 https://lkml.org/lkml/2020/4/3/43 Some items discussed, but not adopted: - LLVM_DIR When multiple versions of LLVM are installed, I just thought supporting LLVM_DIR=/path/to/my/llvm/bin/ might be useful. CC = $(LLVM_DIR)clang LD = $(LLVM_DIR)ld.lld ... However, we can handle this by modifying PATH. So, we decided to not do this. - LLVM_SUFFIX Some distributions (e.g. Debian) package specific versions of LLVM with naming conventions that use the version as a suffix. CC = clang$(LLVM_SUFFIX) LD = ld.lld(LLVM_SUFFIX) ... will allow a user to pass LLVM_SUFFIX=-11 to use clang-11 etc., but the suffixed versions in /usr/bin/ are symlinks to binaries in /usr/lib/llvm-#/bin/, so this can also be handled by PATH. Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Tested-by: Nathan Chancellor <[email protected]> # build Tested-by: Nick Desaulniers <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]>
1 parent 7e20e47 commit a0d1c95

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

Documentation/kbuild/kbuild.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,8 @@ KBUILD_BUILD_USER, KBUILD_BUILD_HOST
262262
These two variables allow to override the user@host string displayed during
263263
boot and in /proc/version. The default value is the output of the commands
264264
whoami and host, respectively.
265+
266+
LLVM
267+
----
268+
If this variable is set to 1, Kbuild will use Clang and LLVM utilities instead
269+
of GCC and GNU binutils to build the kernel.

Documentation/kbuild/llvm.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ example:
4747
LLVM Utilities
4848
--------------
4949

50-
LLVM has substitutes for GNU binutils utilities. These can be invoked as
51-
additional parameters to `make`.
50+
LLVM has substitutes for GNU binutils utilities. Kbuild supports `LLVM=1`
51+
to enable them.
52+
53+
make LLVM=1
54+
55+
They can be enabled individually. The full list of the parameters:
5256

5357
make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \\
5458
OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump OBJSIZE=llvm-size \\

Makefile

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,13 @@ HOST_LFS_CFLAGS := $(shell getconf LFS_CFLAGS 2>/dev/null)
399399
HOST_LFS_LDFLAGS := $(shell getconf LFS_LDFLAGS 2>/dev/null)
400400
HOST_LFS_LIBS := $(shell getconf LFS_LIBS 2>/dev/null)
401401

402-
HOSTCC = gcc
403-
HOSTCXX = g++
402+
ifneq ($(LLVM),)
403+
HOSTCC = clang
404+
HOSTCXX = clang++
405+
else
406+
HOSTCC = gcc
407+
HOSTCXX = g++
408+
endif
404409
KBUILD_HOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 \
405410
-fomit-frame-pointer -std=gnu89 $(HOST_LFS_CFLAGS) \
406411
$(HOSTCFLAGS)
@@ -409,16 +414,28 @@ KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
409414
KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
410415

411416
# Make variables (CC, etc...)
412-
LD = $(CROSS_COMPILE)ld
413-
CC = $(CROSS_COMPILE)gcc
414417
CPP = $(CC) -E
418+
ifneq ($(LLVM),)
419+
CC = clang
420+
LD = ld.lld
421+
AR = llvm-ar
422+
NM = llvm-nm
423+
OBJCOPY = llvm-objcopy
424+
OBJDUMP = llvm-objdump
425+
READELF = llvm-readelf
426+
OBJSIZE = llvm-size
427+
STRIP = llvm-strip
428+
else
429+
CC = $(CROSS_COMPILE)gcc
430+
LD = $(CROSS_COMPILE)ld
415431
AR = $(CROSS_COMPILE)ar
416432
NM = $(CROSS_COMPILE)nm
417-
STRIP = $(CROSS_COMPILE)strip
418433
OBJCOPY = $(CROSS_COMPILE)objcopy
419434
OBJDUMP = $(CROSS_COMPILE)objdump
420-
OBJSIZE = $(CROSS_COMPILE)size
421435
READELF = $(CROSS_COMPILE)readelf
436+
OBJSIZE = $(CROSS_COMPILE)size
437+
STRIP = $(CROSS_COMPILE)strip
438+
endif
422439
PAHOLE = pahole
423440
LEX = flex
424441
YACC = bison

tools/objtool/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ include ../scripts/Makefile.include
33
include ../scripts/Makefile.arch
44

55
# always use the host compiler
6+
ifneq ($(LLVM),)
7+
HOSTAR ?= llvm-ar
8+
HOSTCC ?= clang
9+
HOSTLD ?= ld.lld
10+
else
611
HOSTAR ?= ar
712
HOSTCC ?= gcc
813
HOSTLD ?= ld
14+
endif
915
AR = $(HOSTAR)
1016
CC = $(HOSTCC)
1117
LD = $(HOSTLD)

0 commit comments

Comments
 (0)