Skip to content

Commit

Permalink
Support for cross-compiling kernel modules
Browse files Browse the repository at this point in the history
In order to correctly cross-compile, one has to pass ARCH and
CROSS_COMPILE make flags to kernel module build calls. Facilitate this
in the same way as for custom CC flag by recognizing KERNEL_-prefixed
configure environment variables of same name.

Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Phil Sutter <[email protected]>
Closes #16924
  • Loading branch information
n0-1 authored Jan 6, 2025
1 parent 3a445f2 commit 18c67d2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config/kernel.m4
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,16 @@ AC_DEFUN([ZFS_LINUX_COMPILE], [
building kernel modules])
AC_ARG_VAR([KERNEL_LLVM], [Binary option to
build kernel modules with LLVM/CLANG toolchain])
AC_ARG_VAR([KERNEL_CROSS_COMPILE], [Cross compile prefix
for kernel module builds])
AC_ARG_VAR([KERNEL_ARCH], [Architecture to build kernel modules for])
AC_TRY_COMMAND([
KBUILD_MODPOST_NOFINAL="$5" KBUILD_MODPOST_WARN="$6"
make modules -k -j$TEST_JOBS ${KERNEL_CC:+CC=$KERNEL_CC}
${KERNEL_LD:+LD=$KERNEL_LD} ${KERNEL_LLVM:+LLVM=$KERNEL_LLVM}
CONFIG_MODULES=y CFLAGS_MODULE=-DCONFIG_MODULES
${KERNEL_CROSS_COMPILE:+CROSS_COMPILE=$KERNEL_CROSS_COMPILE}
${KERNEL_ARCH:+ARCH=$KERNEL_ARCH}
-C $LINUX_OBJ $ARCH_UM M=$PWD/$1 >$1/build.log 2>&1])
AS_IF([AC_TRY_COMMAND([$2])], [$3], [$4])
])
Expand Down
2 changes: 2 additions & 0 deletions config/zfs-build.m4
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ AC_DEFUN([ZFS_AC_RPM], [
RPM_DEFINE_KMOD=${RPM_DEFINE_KMOD}' --define "kernel_cc KERNEL_CC=$(KERNEL_CC)"'
RPM_DEFINE_KMOD=${RPM_DEFINE_KMOD}' --define "kernel_ld KERNEL_LD=$(KERNEL_LD)"'
RPM_DEFINE_KMOD=${RPM_DEFINE_KMOD}' --define "kernel_llvm KERNEL_LLVM=$(KERNEL_LLVM)"'
RPM_DEFINE_KMOD=${RPM_DEFINE_KMOD}' --define "kernel_cross_compile KERNEL_CROSS_COMPILE=$(KERNEL_CROSS_COMPILE)"'
RPM_DEFINE_KMOD=${RPM_DEFINE_KMOD}' --define "kernel_arch KERNEL_ARCH=$(KERNEL_ARCH)"'
])
RPM_DEFINE_DKMS=''
Expand Down
2 changes: 2 additions & 0 deletions module/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ modules-Linux:
mkdir -p $(sort $(dir $(zfs-objs) $(zfs-)))
$(MAKE) -C @LINUX_OBJ@ $(if @KERNEL_CC@,CC=@KERNEL_CC@) \
$(if @KERNEL_LD@,LD=@KERNEL_LD@) $(if @KERNEL_LLVM@,LLVM=@KERNEL_LLVM@) \
$(if @KERNEL_CROSS_COMPILE@,CROSS_COMPILE=@KERNEL_CROSS_COMPILE@) \
$(if @KERNEL_ARCH@,ARCH=@KERNEL_ARCH@) \
M="$$PWD" @KERNEL_MAKE@ CONFIG_ZFS=m modules

modules-FreeBSD:
Expand Down
4 changes: 3 additions & 1 deletion rpm/generic/zfs-kmod.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ for kernel_version in %{?kernel_versions}; do
%{debuginfo} \
%{?kernel_cc} \
%{?kernel_ld} \
%{?kernel_llvm}
%{?kernel_llvm} \
%{?kernel_cross_compile} \
%{?kernel_arch}

# Pre-6.10 kernel builds didn't need to copy over the source files to the
# build directory. However we do need to do it though post-6.10 due to
Expand Down
4 changes: 3 additions & 1 deletion rpm/redhat/zfs-kmod.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ fi
%{debuginfo} \
%{?kernel_cc} \
%{?kernel_ld} \
%{?kernel_llvm}
%{?kernel_llvm} \
%{?kernel_cross_compile} \
%{?kernel_arch}
make %{?_smp_mflags}

# Module signing (modsign)
Expand Down

0 comments on commit 18c67d2

Please sign in to comment.