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

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

Commit Message

Koch, Stefan Dec. 20, 2022, 5:09 p.m. UTC
When using a cross build this patch does introduce optionally
host and target specific kernel kbuild packages that
ship the "scripts" and "tools" binaries.

Appending ISAR_BUILDS with "target" will enable the
build of a target specific kbuild package at cross builds.

Appending ISAR_BUILDS with "host" will enable the
build of a host specific kbuild package at cross builds.
In this case no kbuild-cross package will be generated.

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/files/debian/control.tmpl           | 16 ++++++-
 .../linux/files/debian/isar/build.tmpl        | 13 ++++-
 .../linux/files/debian/isar/common.tmpl       | 12 ++++-
 .../linux/files/debian/isar/configure.tmpl    | 13 +++--
 .../linux/files/debian/isar/install.tmpl      | 26 ++++++----
 .../linux/files/debian/rules.tmpl             |  2 +-
 meta/recipes-kernel/linux/linux-custom.inc    | 47 +++++++++++++++----
 7 files changed, 102 insertions(+), 27 deletions(-)

Patch

diff --git a/meta/recipes-kernel/linux/files/debian/control.tmpl b/meta/recipes-kernel/linux/files/debian/control.tmpl
index b19ca2c..c012048 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: <!targetbuild !hostbuild>
 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: <!targetbuild !hostbuild>
 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 !targetbuild !hostbuild>
 Section: devel
 Provides: linux-kernel-headers
 Architecture: any
@@ -30,6 +32,7 @@  Description: Linux support headers for userspace development
  are used by the installed headers for GNU glibc and other system libraries.
 
 Package: linux-image-${KERNEL_NAME_PROVIDED}-dbg
+Build-Profiles: <!targetbuild !hostbuild>
 Section: debug
 Architecture: any
 Description: Linux kernel debugging symbols for @KR@
@@ -37,8 +40,19 @@  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: <targetbuild> <hostbuild> <defaultkbuild>
 Architecture: any
 Depends: ${perl:Depends}, ${shlib:Depends}
 Description: ${KERNEL_NAME_PROVIDED} Linux kbuild scripts and tools for @KR@
  This package provides kernel kbuild scripts and tools for @KR@
  This is useful for people who need to build external modules
+
+Package: linux-kbuild-${KERNEL_NAME_PROVIDED}-cross
+Build-Profiles: <!targetbuild !hostbuild crosskbuild>
+Architecture: any
+Depends: ${KERNEL_HEADERS_DEBIAN_DEPENDS}, ${perl:Depends}, ${shlib:Depends}
+Description: ${KERNEL_NAME_PROVIDED} Linux kbuild scripts and tools for @KR@
+ This package provides kernel kbuild scripts and tools
+ as ${HOST_ARCH} cross binaries for @KR@
+ This is useful for those who need to cross build
+ external modules using ISAR's sbuild-chroot-host
diff --git a/meta/recipes-kernel/linux/files/debian/isar/build.tmpl b/meta/recipes-kernel/linux/files/debian/isar/build.tmpl
index 94cfbe0..7e095ca 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/build.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/build.tmpl
@@ -22,7 +22,18 @@  do_build() {
     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}"
+    if echo "${DEB_BUILD_PROFILES}" | grep -q -e "targetbuild" -e "hostbuild"; then # Build kernel scripts and tools
+        cat ${KERNEL_BUILD_DIR}/.config | grep RECORDM
+        ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" scripts
+        if [ -d "tools/objtool" ]; then
+            ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" tools/objtool
+        fi
+        if grep -q "CONFIG_MODULES=y" ${KERNEL_BUILD_DIR}/.config; then
+            ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}" modules_prepare
+        fi
+    else # Build the Linux kernel
+        ${MAKE} O=${KERNEL_BUILD_DIR} ${PARALLEL_MAKE} KCFLAGS="${KCFLAGS}"
+    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 f4c0519..65fa1fa 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/common.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/common.tmpl
@@ -5,12 +5,22 @@ 
 set -e
 
 # Isar settings
-ARCH=${KERNEL_ARCH}
 KERNEL_PKG_IMAGE=linux-image-${KERNEL_NAME_PROVIDED}
 KERNEL_PKG_KERN_HEADERS=linux-headers-${KERNEL_NAME_PROVIDED}
 KERNEL_PKG_LIBC_HEADERS=linux-libc-dev
 KERNEL_PKG_KERN_KBUILD=linux-kbuild-${KERNEL_NAME_PROVIDED}
 
+if echo "${DEB_BUILD_PROFILES}" | grep -q "hostbuild"; then
+	# Force creating kernel kbuild debian package with valid host arch
+	# Use a cross build to comply with arch specific kernel defconfigs
+	# scripts and tools are always created in host arch
+	ARCH=${KERNEL_ARCH}
+	eval $(dpkg-architecture -f -A ${DISTRO_ARCH})
+	CROSS_COMPILE=${DEB_TARGET_GNU_TYPE}-
+else
+	ARCH=${KERNEL_ARCH}
+fi
+
 # Constants
 KCONF=.config
 
diff --git a/meta/recipes-kernel/linux/files/debian/isar/configure.tmpl b/meta/recipes-kernel/linux/files/debian/isar/configure.tmpl
index 389c9a8..900d5cd 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/configure.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/configure.tmpl
@@ -11,9 +11,16 @@  do_configure() {
     set -x
 
     # Process kernel config target and fragments
-    ${MAKE} O=${KERNEL_BUILD_DIR} ${KERNEL_CONFIG_TARGET}
-    ./scripts/kconfig/merge_config.sh -O ${KERNEL_BUILD_DIR}/ \
-        ${KERNEL_BUILD_DIR}/.config ${KERNEL_CONFIG_FRAGMENTS}
+    if [ -e ${PP}/kernelconfig ]; then
+        mkdir -p ${KERNEL_BUILD_DIR}
+        cp ${PP}/kernelconfig ${KERNEL_BUILD_DIR}/.config
+        ${MAKE} O=${KERNEL_BUILD_DIR} olddefconfig
+    else
+        ${MAKE} O=${KERNEL_BUILD_DIR} ${KERNEL_CONFIG_TARGET}
+        ./scripts/kconfig/merge_config.sh -O ${KERNEL_BUILD_DIR}/ \
+            ${KERNEL_BUILD_DIR}/.config ${KERNEL_CONFIG_FRAGMENTS}
+        cp ${KERNEL_BUILD_DIR}/.config ${PP}/kernelconfig
+    fi
 
     # Stop tracing
     set +x
diff --git a/meta/recipes-kernel/linux/files/debian/isar/install.tmpl b/meta/recipes-kernel/linux/files/debian/isar/install.tmpl
index 236b67c..08b8a9a 100644
--- a/meta/recipes-kernel/linux/files/debian/isar/install.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/isar/install.tmpl
@@ -34,18 +34,24 @@  do_install() {
     set -x
 
     # Run the install steps
-    install_image
-    if [ "${ARCH}" != "um" ]; then
-        install_config
-        install_map
+    if ! echo "${DEB_BUILD_PROFILES}" | grep -q -e "targetbuild" -e "hostbuild"; then
+        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}
+    if ! echo "${DEB_BUILD_PROFILES}" | grep -q -e "targetbuild" -e "hostbuild" && echo "${DEB_BUILD_PROFILES}" | grep -q "crosskbuild"; then
+        # Install cross kernel scripts and tools
+        install_kbuild ${deb_kern_kbuild_dir}-cross
+    fi
+
+    # Install kernel scripts and tools
     install_kbuild ${deb_kern_kbuild_dir}
 
     # Stop tracing
diff --git a/meta/recipes-kernel/linux/files/debian/rules.tmpl b/meta/recipes-kernel/linux/files/debian/rules.tmpl
index 8063c49..d176528 100755
--- a/meta/recipes-kernel/linux/files/debian/rules.tmpl
+++ b/meta/recipes-kernel/linux/files/debian/rules.tmpl
@@ -36,4 +36,4 @@  override_dh_auto_test:
 	true
 
 override_dh_strip:
-	unset DEB_HOST_GNU_TYPE && dh_strip -Xvmlinu --no-automatic-dbgsym
+	dh_strip -Xvmlinu -Xlinux-kbuild-${KERNEL_NAME_PROVIDED}-cross --no-automatic-dbgsym
diff --git a/meta/recipes-kernel/linux/linux-custom.inc b/meta/recipes-kernel/linux/linux-custom.inc
index dbda755..db2d7a3 100644
--- a/meta/recipes-kernel/linux/linux-custom.inc
+++ b/meta/recipes-kernel/linux/linux-custom.inc
@@ -80,6 +80,7 @@  TEMPLATE_VARS += "                \
     KERNEL_NAME_PROVIDED          \
     KERNEL_CONFIG_FRAGMENTS       \
     KCFLAGS                       \
+    PP                            \
 "
 
 inherit dpkg
@@ -91,28 +92,54 @@  KCFLAGS ?= "-fdebug-prefix-map=${CURDIR}=."
 # Derive name of the kernel packages from the name of this recipe
 KERNEL_NAME_PROVIDED ?= "${@ d.getVar('PN', True).partition('linux-')[2]}"
 
-# Make bitbake know we will be producing linux-image and linux-headers packages
 python() {
     kernel_name = d.getVar("KERNEL_NAME_PROVIDED", True)
-    d.setVar('PROVIDES', 'linux-image-' + kernel_name + ' ' + \
-                         'linux-headers-' + kernel_name)
+    distro_arch = d.getVar("DISTRO_ARCH", True)
+    host_arch = d.getVar("HOST_ARCH", True)
     headers_depends = "linux-kbuild-" + kernel_name
 
+    # By appending ISAR_BUILDS with "target" and/or "host" it's possible
+    # to run additional target or host builds.
+    # There are no "target" and "host" builds enabled by default.
+    # When both build modes are enabled then for a cross build a kbuild package
+    # for the target and a kbuild package for the host will be created.
+    # When "host" build is not enabled instead of the kbuild
+    # a kbuild-cross package for the host is generated.
+    if not "host" in d.getVar("ISAR_BUILDS", True) and d.getVar("ISAR_CROSS_COMPILE", True) == "1" and d.getVar("HOST_ARCH", True) != d.getVar("DISTRO_ARCH", True):
+       d.appendVar('DEB_BUILD_PROFILES', ' crosskbuild')
+
+    # For different distro and host archs
+    # - Add dependency for sbuild-chroot-target
+    #   to allow building arch specific kbuild scripts and tools
+    # - Set correct kbuild package dependency
+    if distro_arch != host_arch and d.getVar("ISAR_CROSS_COMPILE", True) == "1":
+        d.appendVar("SCHROOT_DEP", " sbuild-chroot-target:do_build")
+        headers_depends = headers_depends + "-cross | " + headers_depends
+        build_depends = "crossbuild-essential-" + distro_arch + " [" + host_arch + "], "
+        d.prependVar("KBUILD_DEPENDS", build_depends)
+    else:
+        d.appendVar("DEB_BUILD_PROFILES", " defaultkbuild")
+
+    # Make bitbake know we will be producing
+    # linux-image and linux-headers packages
+    d.setVar("PROVIDES", "linux-image-" + kernel_name + " " + \
+                         "linux-headers-" + kernel_name)
+
     # Set dependency for kernel headers
     d.appendVar("KERNEL_HEADERS_DEBIAN_DEPENDS", headers_depends)
 }
 
-def get_kernel_arch(d):
-    distro_arch = d.getVar("DISTRO_ARCH")
-    if distro_arch in ["amd64", "i386"]:
+def get_kernel_arch(d, arch="DISTRO_ARCH"):
+    arch = d.getVar(arch)
+    if arch in ["amd64", "i386"]:
         kernel_arch = "x86"
-    elif distro_arch == "arm64":
+    elif arch == "arm64":
         kernel_arch = "arm64"
-    elif distro_arch == "armhf":
+    elif arch == "armhf":
         kernel_arch = "arm"
-    elif distro_arch == "mipsel":
+    elif arch == "mipsel":
         kernel_arch = "mips"
-    elif distro_arch == "riscv64":
+    elif arch == "riscv64":
         kernel_arch = "riscv"
     else:
         kernel_arch = ""