From 3d552b93e0974e8fdb38a89a3347d36a99da953a Mon Sep 17 00:00:00 2001 From: liyunfei Date: Wed, 13 Sep 2023 10:14:00 +0800 Subject: [PATCH] Add clang compile support Backport commit 6ddb09145930b03c88e63f5930e718a33501024d Support CC=clang and LD=ld.lld & add clang llvm lld requires --- Support-CC-clang-and-LD-ld.lld.patch | 59 ++++++++++++++++++++++++++++ dkms.spec | 7 +++- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Support-CC-clang-and-LD-ld.lld.patch diff --git a/Support-CC-clang-and-LD-ld.lld.patch b/Support-CC-clang-and-LD-ld.lld.patch new file mode 100644 index 0000000..093a922 --- /dev/null +++ b/Support-CC-clang-and-LD-ld.lld.patch @@ -0,0 +1,59 @@ +From cfe6363124a0d0495727c6f4e8a1809b295a8cbf Mon Sep 17 00:00:00 2001 +From: liyunfei +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 +Signed-off-by: Nick Desaulniers +--- + 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 + diff --git a/dkms.spec b/dkms.spec index 8411095..9d68265 100644 --- a/dkms.spec +++ b/dkms.spec @@ -1,19 +1,21 @@ Summary: Dynamic Kernel Module Support Framework Name: dkms Version: 2.6.1 -Release: 7 +Release: 8 License: GPLv2+ BuildArch: noarch URL: https://github.com/dell/dkms Source0: https://github.com/dell/dkms/archive/v%{version}.tar.gz#/dkms-%{version}.tar.gz # because Mandriva calls this package dkms-minimal Patch0: change-deprecated-egrep-for-grep-E.patch +Patch1: Support-CC-clang-and-LD-ld.lld.patch Provides: dkms-minimal = %{version} Requires: coreutils Requires: cpio Requires: findutils Requires: gawk Requires: gcc +Requires: clang llvm lld Requires: grep Requires: gzip Requires: kernel-devel @@ -128,6 +130,9 @@ fi %{_sysconfdir}/bash_completion.d/%{name} %changelog +* Tue Sep 19 2023 liyunfei - 2.6.1-8 +- Add clang compile support + * Mon Mar 27 2023 wangkai - 2.6.1-7 - Change deprecated egrep for grep -E