dkms/Support-CC-clang-and-LD-ld.lld.patch
liyunfei 3d552b93e0 Add clang compile support
Backport commit 6ddb09145930b03c88e63f5930e718a33501024d Support CC=clang and LD=ld.lld & add clang llvm lld requires
2023-09-19 20:04:04 +08:00

60 lines
2.2 KiB
Diff

From cfe6363124a0d0495727c6f4e8a1809b295a8cbf Mon Sep 17 00:00:00 2001
From: liyunfei <liyunfei33@huawei.com>
Date: Wed, 13 Sep 2023 10:05:33 +0800
Subject: [PATCH] Support CC=clang and LD=ld.lld When building the Linux
kernel, one may use `make LLVM=1` to set multiple flags, akin to `make
CC=clang LD=ld.lld NM=llvm-nm ...` (see the link below for kernel docs
explaining this further in detail).
When building kernel modules, to ensure we're using the same toolchain
as the underlying core kernel image, Kbuild will error if it's reinvoked
with a toolchain that differs. This causes DKMS to fail, since it's not
re-specifying the same compiler (or linker, etc).
Check the .comment section of the vmlinux file in the Linux kernel
source dir, and set CC= and LD= flags for make based on that.
If vmlinux does not exist, grep .config for CONFIG_CC_IS_CLANG=y and
CONFIG_LD_IS_LLD=y.
Fixes: #124
Fixes: ClangBuiltLinux/linux#1104
Link: https://docs.kernel.org/kbuild/llvm.html
Suggested-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Nick Desaulniers <nick.desaulniers@gmail.com>
---
dkms | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/dkms b/dkms
index c4b9a26..983aefc 100755
--- a/dkms
+++ b/dkms
@@ -699,6 +699,23 @@ read_conf()
[[ ${MODULES_CONF[$index]} ]] && modules_conf_array[$index]="${MODULES_CONF[$index]}"
done
+ # Check if clang was used to compile or lld was used to link the kernel.
+ if [[ -e $kernel_source_dir/vmlinux ]]; then
+ if readelf -p .comment $kernel_source_dir/vmlinux | grep -q clang; then
+ make_command="${make_command} CC=clang"
+ fi
+ if readelf -p .comment $kernel_source_dir/vmlinux | grep -q LLD; then
+ make_command="${make_command} LD=ld.lld"
+ fi
+ elif [[ -e $kernel_source_dir/.config ]]; then
+ if grep -q CONFIG_CC_IS_CLANG=y $kernel_source_dir/.config; then
+ make_command="${make_command} CC=clang"
+ fi
+ if grep -q CONFIG_LD_IS_LLD=y $kernel_source_dir/.config; then
+ make_command="${make_command} LD=ld.lld"
+ fi
+ fi
+
# Set patch_array (including kernel specific patches)
count=0
for ((index=0; index < ${#PATCH[@]}; index++)); do
--
2.27.0