[v5,3/4] linux-custom: Provide target and host specific kernel kbuild packages

Message ID 20240131160001.322247-4-stefan-koch@siemens.com
State Superseded, archived
Headers show
Series linux-custom: Split up binaries from kernel headers to kbuild package | expand

Commit Message

Koch, Stefan Jan. 31, 2024, 4 p.m. UTC
When using a cross build this patch does introduce
target and host specific kernel kbuild packages that
ship the "scripts" and "tools" binaries.

The "-kbuildtarget" and "-native" multiarch bitbake targets are useable to run
additional target or host specific builds for kbuild scripts and tools.

Using the "-kbuildtarget" bitbake target enables the build of
a target specific kbuild package at cross builds.
So using "linux-kbuild" provides the package for the target platform.

Using the "-native" bitbake target enables the build of
a host specific kbuild package at cross builds.
When cross building using "linux-kbuild-native"
provides the package for the host platform.

Only the "host" specific package is built automatically at cross builds.

This solves this from doc/custom_kernel.inc:
- The kernel headers package has not supported both native
  and cross compilation of kernel modules when itself was cross built
- Future roadmap: Generate kernel headers package for both host
  and target when using a cross build

Signed-off-by: Stefan Koch <stefan-koch@siemens.com>
---
 .../linux/classes/kbuildtarget.bbclass        |  8 +++
 .../linux/files/debian/control.tmpl           |  6 ++-
 .../linux/files/debian/isar/build.tmpl        | 13 ++++-
 .../linux/files/debian/isar/common.tmpl       |  9 ++++
 .../linux/files/debian/isar/install.tmpl      | 34 +++++++-----
 meta/recipes-kernel/linux/linux-custom.inc    | 52 +++++++++++++++++--
 6 files changed, 101 insertions(+), 21 deletions(-)
 create mode 100644 meta/recipes-kernel/linux/classes/kbuildtarget.bbclass

Patch

diff --git a/meta/recipes-kernel/linux/classes/kbuildtarget.bbclass b/meta/recipes-kernel/linux/classes/kbuildtarget.bbclass
new file mode 100644
index 00000000..26369861
--- /dev/null
+++ b/meta/recipes-kernel/linux/classes/kbuildtarget.bbclass
@@ -0,0 +1,8 @@ 
+python kbuildtarget_virtclass_handler() {
+    pn = e.data.getVar('PN')
+    if pn.endswith('-kbuildtarget'):
+        e.data.setVar('BPN', pn[:-len('-kbuildtarget')])
+        e.data.appendVar('OVERRIDES', ':class-kbuildtarget')
+}
+addhandler kbuildtarget_virtclass_handler
+kbuildtarget_virtclass_handler[eventmask] = "bb.event.RecipePreFinalise"
diff --git a/meta/recipes-kernel/linux/files/debian/control.tmpl b/meta/recipes-kernel/linux/files/debian/control.tmpl
index 7f271367..6f8f8afe 100644
--- a/meta/recipes-kernel/linux/files/debian/control.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/control.tmpl
@@ -6,6 +6,7 @@  Build-Depends: bc, kmod, cpio, ${KBUILD_DEPENDS}
 Homepage: http://www.kernel.org/
 
 Package: linux-image-${KERNEL_NAME_PROVIDED}
+Build-Profiles: <kernel>
 Architecture: any
 Depends: ${KERNEL_DEBIAN_DEPENDS}
 Description: ${KERNEL_NAME_PROVIDED} Linux kernel, version @KR@
@@ -13,6 +14,7 @@  Description: ${KERNEL_NAME_PROVIDED} Linux kernel, version @KR@
  files, version: @KR@.
 
 Package: linux-headers-${KERNEL_NAME_PROVIDED}
+Build-Profiles: <kernel>
 Architecture: any
 Depends: ${KERNEL_HEADERS_DEBIAN_DEPENDS}, ${perl:Depends}, ${shlib:Depends}
 Description: ${KERNEL_NAME_PROVIDED} Linux kernel headers for @KR@
@@ -21,7 +23,7 @@  Description: ${KERNEL_NAME_PROVIDED} Linux kernel headers for @KR@
  This is useful for people who need to build external modules
 
 Package: linux-libc-dev
-Build-Profiles: <!nolibcdev>
+Build-Profiles: <!nolibcdev kernel>
 Section: devel
 Provides: linux-kernel-headers
 Architecture: any
@@ -41,6 +43,7 @@  Description: Linux Kernel Headers for development (for cross-compiling)
  your kernel. Use linux-headers-* packages for that.
 
 Package: linux-image-${KERNEL_NAME_PROVIDED}-dbg
+Build-Profiles: <kernel>
 Section: debug
 Architecture: any
 Description: Linux kernel debugging symbols for @KR@
@@ -48,6 +51,7 @@  Description: Linux kernel debugging symbols for @KR@
  all the necessary debug symbols for the kernel and its modules.
 
 Package: linux-kbuild-${KERNEL_NAME_PROVIDED}
+Build-Profiles: <kbuild>
 Architecture: any
 Depends: ${perl:Depends}, ${shlib:Depends}
 Description: ${KERNEL_NAME_PROVIDED} Linux kbuild scripts and tools for @KR@
diff --git a/meta/recipes-kernel/linux/files/debian/isar/build.tmpl b/meta/recipes-kernel/linux/files/debian/isar/build.tmpl
index 906dc580..81a6ba8a 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/build.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/build.tmpl
@@ -21,8 +21,17 @@  do_build() {
     KR=$(${MAKE} O=${KERNEL_BUILD_DIR} -s --no-print-directory kernelrelease)
     sed -i "s/@KR@/${KR}/g" ${S}/debian/control ${S}/debian/linux-image-${KERNEL_NAME_PROVIDED}.*
 
-    # Build the Linux kernel
-    ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" KAFLAGS="${KAFLAGS}"
+    if echo "${DEB_BUILD_PROFILES}" | grep -q "kernel"; then # Build kernel scripts and tools
+        ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" KAFLAGS="${KAFLAGS}"
+    elif echo "${DEB_BUILD_PROFILES}" | grep -q "kbuild"; then # Build kernel scripts and tools
+        ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" KAFLAGS="${KAFLAGS}" scripts
+        if grep -q -E "CONFIG_STACK_VALIDATION=y|CONFIG_HAVE_OBJTOOL=y" ${KERNEL_BUILD_DIR}/.config && [ -d "tools/objtool" ]; then
+            ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" KAFLAGS="${KAFLAGS}" tools/objtool || true
+        fi
+        if grep -q "CONFIG_MODULES=y" ${KERNEL_BUILD_DIR}/.config; then
+            ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" KAFLAGS="${KAFLAGS}" modules_prepare
+        fi
+    fi
 
     # Stop tracing
     set +x
diff --git a/meta/recipes-kernel/linux/files/debian/isar/common.tmpl b/meta/recipes-kernel/linux/files/debian/isar/common.tmpl
index 0944e943..e3a1d8a0 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/common.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/common.tmpl
@@ -12,6 +12,15 @@  KERNEL_PKG_LIBC_HEADERS=linux-libc-dev
 KERNEL_PKG_LIBC_HEADERS_CROSS=linux-libc-dev-${DISTRO_ARCH}-cross
 KERNEL_PKG_KERN_KBUILD=linux-kbuild-${KERNEL_NAME_PROVIDED}
 
+# Force creating debian package with valid host arch for -native build
+# Use a cross build to comply with arch specific kernel defconfigs
+# The scripts and tools are always created for host arch
+if echo "${DEB_BUILD_PROFILES}" | grep -q -e "cross" -e "kbuild"
+then
+    eval $(dpkg-architecture -f -A ${DISTRO_ARCH})
+    CROSS_COMPILE=${DEB_TARGET_GNU_TYPE}-
+fi
+
 # Constants
 KCONF=.config
 
diff --git a/meta/recipes-kernel/linux/files/debian/isar/install.tmpl b/meta/recipes-kernel/linux/files/debian/isar/install.tmpl
index 97780dcc..77856aee 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/install.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/install.tmpl
@@ -33,20 +33,28 @@  do_install() {
     # Trace what we do here
     set -x
 
-    # Run the install steps
-    install_image
-    if [ "${ARCH}" != "um" ]; then
-        install_config
-        install_map
+    if echo "${DEB_BUILD_PROFILES}" | grep -q "kbuild"; then
+        # Install kernel scripts and tools
+        install_kbuild ${deb_kern_kbuild_dir}
+    fi
+
+    if echo "${DEB_BUILD_PROFILES}" | grep -q "kernel"; then
+        if echo "${DEB_BUILD_PROFILES}" | grep -q "cross"; then
+            # Install cross kernel scripts and tools
+            install_kbuild ${deb_kern_kbuild_dir}-${HOST_ARCH}-cross
+        fi
+
+        # Run the install steps
+        install_image
+        if [ "${ARCH}" != "um" ]; then
+            install_config
+            install_map
+        fi
+        install_hooks
+        install_dtbs
+        install_kmods
+        install_headers
     fi
-    install_hooks
-    install_dtbs
-    install_kmods
-    install_headers
-
-    # Cleanup and install kernel scripts and tools
-    rm -rf ${deb_kern_kbuild_dir}
-    install_kbuild ${deb_kern_kbuild_dir}
 
     # Stop tracing
     set +x
diff --git a/meta/recipes-kernel/linux/linux-custom.inc b/meta/recipes-kernel/linux/linux-custom.inc
index 48d86c2a..3f71fd7e 100644
--- a/meta/recipes-kernel/linux/linux-custom.inc
+++ b/meta/recipes-kernel/linux/linux-custom.inc
@@ -84,30 +84,72 @@  TEMPLATE_VARS += "                \
     DISTRIBUTOR                   \
 "
 
-inherit dpkg
-inherit template
-
 # Add custom cflags to the kernel build
 KCFLAGS ?= "-fdebug-prefix-map=${CURDIR}=."
 KAFLAGS ?= "-fdebug-prefix-map=${CURDIR}=."
 
 # Derive name of the kernel packages from the name of this recipe
-KERNEL_NAME_PROVIDED ?= "${@ d.getVar('PN').partition('linux-')[2]}"
+KERNEL_NAME_PROVIDED ?= "${@ d.getVar('BPN').partition('linux-')[2]}"
+
+python() {
+    if d.getVar("DISTRO_ARCH") != d.getVar("HOST_ARCH") and d.getVar("ISAR_CROSS_COMPILE", True) == "1" and "class-native" not in d.getVar("OVERRIDES", True):
+        d.appendVar("OVERRIDES", ":cross-profile")
+}
+
+# default profiles and provides
+BUILD_PROFILES = "kernel kbuild"
+
+# we only offer the -kbuildtarget variant when actually cross compiling
+BBCLASSEXTEND:append:cross-profile = " kbuildtarget"
+
+# when cross-profile is active
+# build only kernel with the default variant of the recipe
+BUILD_PROFILES:cross-profile = "kernel"
+
+# select correct kbuild package for isar cross-build
+HEADERS_DEPENDS:cross-profile = ", linux-kbuild-${KERNEL_NAME_PROVIDED}:${HOST_ARCH} | linux-kbuild-${KERNEL_NAME_PROVIDED}"
+
+# -native: kbuild package for host
+BUILD_PROFILES:class-native = "kbuild"
+RECIPE_PROVIDES:class-native = "linux-kbuild-${KERNEL_NAME_PROVIDED}-native"
+
+# -kbuildtarget: kbuild package for target, enforcing non-cross-build
+BUILD_PROFILES:class-kbuildtarget = "kbuild"
+RECIPE_PROVIDES:class-kbuildtarget = "linux-kbuild-${KERNEL_NAME_PROVIDED}"
+ISAR_CROSS_COMPILE:class-kbuildtarget = "0"
 
 # Make bitbake know we will be producing linux-image and linux-headers packages
 # Also make it know about other packages from control
-PROVIDES += " \
+RECIPE_PROVIDES = " \
     linux-image-${KERNEL_NAME_PROVIDED} \
     linux-headers-${KERNEL_NAME_PROVIDED} \
     linux-libc-dev \
     linux-libc-dev-${DISTRO_ARCH}-cross \
     linux-image-${KERNEL_NAME_PROVIDED}-dbg \
+    linux-kbuild-${KERNEL_NAME_PROVIDED} \
 "
+# when cross-profile is active
+# kbuild package is provided by -native or -kbuildtarget variant
+# otherwise it's provided by the default variant
+RECIPE_PROVIDES:remove:cross-profile = "linux-kbuild-${KERNEL_NAME_PROVIDED}"
 
 # append headers depends
 HEADERS_DEPENDS = ", linux-kbuild-${KERNEL_NAME_PROVIDED}"
 KERNEL_HEADERS_DEBIAN_DEPENDS:append = "${HEADERS_DEPENDS}"
 
+# append provides
+PROVIDES += "${RECIPE_PROVIDES}"
+
+# append build profiles
+DEB_BUILD_PROFILES += "${BUILD_PROFILES}"
+
+# add dependency to build -kbuildtarget and -native automatically
+RDEPENDS:append:cross-profile = " ${BPN}-native"
+
+inherit dpkg
+inherit template
+inherit kbuildtarget
+
 def get_kernel_arch(d):
     distro_arch = d.getVar("DISTRO_ARCH")
     if distro_arch in ["amd64", "i386"]: