diff mbox series

[RESEND,v4] linux: Install artifacts to staging for kmod build

Message ID 20241104-feature-linux-kmod-build-from-staging-sdk-v4-1-69dd47edc4ab@collins.com
State New
Headers show
Series [RESEND,v4] linux: Install artifacts to staging for kmod build | expand

Commit Message

Adam Michaelis Nov. 4, 2024, 2:03 p.m. UTC
This commit adds an option to install the necessary kernel headers,
scripts, and build artifacts to the staging directory so that a user
could use the exported Buildroot SDK (make sdk) to build out-of-tree
kernel modules compatible with this kernel.

Signed-off-by: Adam Michaelis <adam.michaelis@collins.com>
---
This patch will allow the user to install the Linux build artifacts and sources
into the BR staging folder that is built into a SDK that is delivered to
upstream (application software) development teams. With these new Linux
artifacts, those teams will be able to build kernel modules compatible with the
BSP's kernel (built with BR) without having to run or maintain a full buildroot
environment and Linux build.

Similar kernel build export logic used by distribution maintainers to create
kernel-devel sort of packages use much-longer procedures to possibly make the
output smaller. To balance complexity and space savings, I have filtered-out
most build artifacts except what seems to be necessary, and a arm64 build of
Linux 5.15 adds 91 MiB to my staging folder (and SDK).

Arch: https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/blob/main/PKGBUILD?ref_type=heads#L133
Fedora: https://src.fedoraproject.org/rpms/kernel/blob/rawhide/f/kernel.spec#_3507
Yocto: https://github.com/openembedded/openembedded-core/blob/master/meta/recipes-kernel/linux/kernel-devsrc.bb#L37

v4:
  - re-send v3 with b4 for proper formatting

v3:
  - Improve rsync excludes to reduce size of output

v2:
  - Add the certs folder for module signing
---
 linux/Config.in |  8 ++++++++
 linux/linux.mk  | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)


---
base-commit: 0ad7035fce67cbe91db9dea7d81e6a4bc0691ad1
change-id: 20241025-feature-linux-kmod-build-from-staging-sdk-72a494eca232

Best regards,
diff mbox series

Patch

diff --git a/linux/Config.in b/linux/Config.in
index e8c0722de0f90e6752409a899abdcc3ea211e9d6..372cd9ddd7aa4ef1b0796011b62be000cb562669 100644
--- a/linux/Config.in
+++ b/linux/Config.in
@@ -454,6 +454,14 @@  config BR2_LINUX_KERNEL_INSTALL_TARGET
 	  /boot if DTBs have been generated by the kernel build
 	  process.
 
+config BR2_LINUX_KERNEL_INSTALL_STAGING
+	bool "Install kmod build files to staging"
+	help
+	  Select this option to have the kernel files required for
+	  out-of-tree module building installed to staging. This will
+	  then enable module building from an exported SDK without
+	  the full Linux build and Buildroot.
+
 config BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL
 	bool "Needs host OpenSSL"
 	help
diff --git a/linux/linux.mk b/linux/linux.mk
index 469a80db9f55fc4f890b219dcd5aac23a5bda487..a26e9374b39debdc9457b4e772b6c1bdd43637c7 100644
--- a/linux/linux.mk
+++ b/linux/linux.mk
@@ -585,6 +585,25 @@  define LINUX_RUN_DEPMOD
 endef
 LINUX_TARGET_FINALIZE_HOOKS += LINUX_RUN_DEPMOD
 
+ifeq ($(BR2_LINUX_KERNEL_INSTALL_STAGING),y)
+LINUX_INSTALL_STAGING=YES
+endif
+
+define LINUX_INSTALL_STAGING_CMDS
+	# Install artifacts needed for module builds into staging for SDK use
+	if grep -q "CONFIG_MODULES=y" $(@D)/.config; then \
+		install -d "$(STAGING_DIR)/lib/modules/$(LINUX_VERSION_PROBED)/build/arch" && \
+		rsync --exclude "*.o" --exclude "*.a" --exclude "*.dtb" --exclude "*.dtbo" \
+			--exclude "Image*" --exclude "*.c" --exclude "*.S" \
+			-a "$(@D)/arch/$(KERNEL_ARCH)" \
+			"$(STAGING_DIR)/lib/modules/$(LINUX_VERSION_PROBED)/build/arch/" && \
+		rsync --exclude "*.o" --exclude "*.a" -a "$(@D)/include" \
+			"$(@D)/scripts" "$(@D)/certs" "$(@D)/Makefile" \
+			"$(@D)/Module.symvers" "$(@D)/.config" \
+			"$(STAGING_DIR)/lib/modules/$(LINUX_VERSION_PROBED)/build/" ; \
+	fi
+endef
+
 # Include all our extensions.
 #
 # Note: our package infrastructure uses the full-path of the last-scanned