From dbc9d0fb6863026b16f4805709acc7e4977d7e66 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Sun, 30 Aug 2026 03:10:39 +0300 Subject: cmake: fix kernel module installation Install each module from its real build directory, propagate staged install roots, and fail when kbuild modules_install fails. Add a rootless CTest and CI regression for successful and failed installs. Best practices followed: Use the standard CMake DESTDIR and kbuild INSTALL_MOD_PATH staging interfaces: https://cmake.org/cmake/help/latest/envvar/DESTDIR.html https://docs.kernel.org/kbuild/modules.html#install-mod-path Check child command status with execute_process RESULT_VARIABLE: https://cmake.org/cmake/help/latest/command/execute_process.html Register repeatable tests with CTest add_test: https://cmake.org/cmake/help/latest/command/add_test.html --- cmake/install-kernel-module.cmake | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 cmake/install-kernel-module.cmake (limited to 'cmake') diff --git a/cmake/install-kernel-module.cmake b/cmake/install-kernel-module.cmake new file mode 100644 index 00000000..1350e7c9 --- /dev/null +++ b/cmake/install-kernel-module.cmake @@ -0,0 +1,35 @@ +if (NOT DEFINED KERNEL_BUILD_DIR OR KERNEL_BUILD_DIR STREQUAL "") + message(FATAL_ERROR "Kernel build directory is not set") +endif () + +if (NOT DEFINED KERNEL_MODULE_BUILD_DIR OR + KERNEL_MODULE_BUILD_DIR STREQUAL "") + message(FATAL_ERROR "Kernel module build directory is not set") +endif () + +set(_install_command + make -C "${KERNEL_BUILD_DIR}" + "M=${KERNEL_MODULE_BUILD_DIR}" + modules_install +) + +# DESTDIR is the conventional CMake staging root. The kernel build system +# uses INSTALL_MOD_PATH for the same purpose, so propagate it explicitly. +if (NOT "$ENV{DESTDIR}" STREQUAL "") + list(APPEND _install_command "INSTALL_MOD_PATH=$ENV{DESTDIR}") +endif () + +execute_process( + COMMAND ${_install_command} + RESULT_VARIABLE _install_result +) + +if (NOT "${_install_result}" STREQUAL "0") + message(FATAL_ERROR + "Failed to install kernel module from " + "${KERNEL_MODULE_BUILD_DIR} (exit status: ${_install_result})" + ) +endif () + +unset(_install_command) +unset(_install_result) -- cgit v1.2.3