From 6620f13807b466c5e4af08b2d5d33f5a433b1e3f Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Sat, 28 Mar 2020 15:54:47 +0900 Subject: [PATCH 1/2] kbuild: support 'LLVM' 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' to use Clang and LLVM tools instead of GCC and Binutils. You can pass LLVM=1 from the command line or as an environment variable. Then, Kbuild will use LLVM toolchains in your PATH environment. This may not be convenient if you have multiple versions of LLVM. CROSS_COMPILE is used to specify not only the tool prefix, but also the directory path to the tools. For example, $ make ARCH=arm64 CROSS_COMPILE=/path/to/my/gcc/bin/aarch64-linux-gnu- To support a similar flow, this commit adds another variable, LLVM_DIR, to point to the specific installation of LLVM: $ make LLVM=1 LLVM_DIR=/path/to/my/llvm/bin/ It might be tedious to set two variables. So, the following is the shorthand: $ make LLVM=/path/to/my/llvm/bin/ Please note LLVM=1 does not turn on the LLVM integrated assembler. You need to pass AS=clang to use it. When the upstream kernel is ready for the integrated assembler, we can make it default. We will get rid of --no-integrated-as, then CROSS_COMPILE will be no longer needed. The --target option will be specified by other means. Signed-off-by: Masahiro Yamada --- Documentation/kbuild/kbuild.rst | 6 ++++++ Documentation/kbuild/llvm.rst | 5 +++++ Makefile | 29 +++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst index f1e5dce86af7..39bb3636e4f4 100644 --- a/Documentation/kbuild/kbuild.rst +++ b/Documentation/kbuild/kbuild.rst @@ -262,3 +262,9 @@ KBUILD_BUILD_USER, KBUILD_BUILD_HOST These two variables allow to override the user@host string displayed during boot and in /proc/version. The default value is the output of the commands whoami and host, respectively. + +LLVM +---- +If this variable is set to 1, Kbuild will use Clang and LLVM utilities instead +of GCC and GNU binutils to build the kernel. +If set to a value other than 1, it points to directory path to LLVM to be used. diff --git a/Documentation/kbuild/llvm.rst b/Documentation/kbuild/llvm.rst index d6c79eb4e23e..4602369f6a4f 100644 --- a/Documentation/kbuild/llvm.rst +++ b/Documentation/kbuild/llvm.rst @@ -55,6 +55,11 @@ additional parameters to `make`. READELF=llvm-readelf HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar \\ HOSTLD=ld.lld +You can use a single switch `LLVM=1` to use LLVM utilities by default (except +for building host programs). + + make LLVM=1 HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld + Getting Help ------------ diff --git a/Makefile b/Makefile index a3bc8bc562ee..f192f9bd8343 100644 --- a/Makefile +++ b/Makefile @@ -408,17 +408,38 @@ KBUILD_HOSTCXXFLAGS := -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS) KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS) KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS) +# LLVM=1 tells Kbuild to use Clang and LLVM utilities by default. +# You can still override CC, LD, etc. individually if desired. +# +# If LLVM is set to a value other than 1, it is set to LLVM_DIR, +# which is useful to select a specific LLVM installation. +ifneq ($(filter-out 1,$(LLVM)),) +LLVM_DIR := $(LLVM) +endif + # Make variables (CC, etc...) -LD = $(CROSS_COMPILE)ld -CC = $(CROSS_COMPILE)gcc CPP = $(CC) -E +ifneq ($(LLVM),) +CC = $(LLVM_DIR)clang +LD = $(LLVM_DIR)ld.lld +AR = $(LLVM_DIR)llvm-ar +NM = $(LLVM_DIR)llvm-nm +OBJCOPY = $(LLVM_DIR)llvm-objcopy +OBJDUMP = $(LLVM_DIR)llvm-objdump +READELF = $(LLVM_DIR)llvm-readelf +OBJSIZE = $(LLVM_DIR)llvm-size +STRIP = $(LLVM_DIR)llvm-strip +else +CC = $(CROSS_COMPILE)gcc +LD = $(CROSS_COMPILE)ld AR = $(CROSS_COMPILE)ar NM = $(CROSS_COMPILE)nm -STRIP = $(CROSS_COMPILE)strip OBJCOPY = $(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE)objdump -OBJSIZE = $(CROSS_COMPILE)size READELF = $(CROSS_COMPILE)readelf +OBJSIZE = $(CROSS_COMPILE)size +STRIP = $(CROSS_COMPILE)strip +endif PAHOLE = pahole LEX = flex YACC = bison -- 2.17.1