[1/2] per-kernel.bbclass: add class

Message ID 20250407160148.443385-2-chris.larson@siemens.com
State New
Headers show
Series Add support for per-kernel recipe variants | expand

Commit Message

chris.larson April 7, 2025, 4:01 p.m. UTC
From: Christopher Larson <chris.larson@siemens.com>

Add support for per-kernel recipe variants. This aids in the ability for a
MACHINE to support multiple kernels, by allowing us to generate per-kernel
variants in recipes like external kernel modules.

A new variable KERNEL_NAMES will list the kernels for which variants will be
generated. It defaults to KERNEL_NAME. While this variable lists all supported
kernels for the current machine, a variant will not be generated for
KERNEL_NAME, assuming that's the recipe's baseline. Each variant listed in
KERNEL_NAMES will add kernel-<kernel_name> to the OVERRIDES variable, and
per-kernel:<kernel_name> to the BBCLASSEXTEND variable. In addition,
KERNEL_NAME will be set to the kernel name for the current variant.

Signed-off-by: Christopher Larson <chris.larson@siemens.com>
---
 meta/classes/per-kernel.bbclass | 35 +++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 meta/classes/per-kernel.bbclass

Patch

diff --git a/meta/classes/per-kernel.bbclass b/meta/classes/per-kernel.bbclass
new file mode 100644
index 00000000..8abe117f
--- /dev/null
+++ b/meta/classes/per-kernel.bbclass
@@ -0,0 +1,35 @@ 
+# Generate per-kernel recipe variants
+#
+# Recipes which are specific to a specific kernel currently append KERNEL_NAME to the PN,
+# and depend on and target that specific kernel. For a machine which supports and builds
+# multiple kernel images, there is a need to generate a variant of the recipe for each
+# kernel image.
+# 
+# Each variant listed in KERNEL_NAMES will add `kernel-<kernel_name>` to the OVERRIDES variable, and
+# `per-kernel:<kernel_name>` to the BBCLASSEXTEND variable. In addition, KERNEL_NAME will be
+# set to the kernel name for the current variant.
+#
+# Copyright (c) Siemens AG, 2025
+# SPDX-License-Identifier: MIT
+
+OVERRIDES .= ":kernel-${KERNEL_NAME}"
+
+KERNEL_NAMES ?= "${KERNEL_NAME}"
+BBCLASSEXTEND += "${@' '.join(f'per-kernel:{kernel}' for kernel in d.getVar('KERNEL_NAMES').split() if kernel != d.getVar('KERNEL_NAME'))}"
+
+python per_kernel_virtclass_handler() {
+    orig_pn = d.getVar('PN')
+
+    d = e.data
+    extend = d.getVar('BBEXTENDCURR') or ''
+    variant = d.getVar('BBEXTENDVARIANT') or ''
+    if extend != 'per-kernel':
+        return
+    elif variant == '':
+        d.appendVar('PROVIDES', f' {orig_pn}')
+        return
+
+    d.setVar('KERNEL_NAME', variant)
+}
+addhandler per_kernel_virtclass_handler
+per_kernel_virtclass_handler[eventmask] = "bb.event.RecipePreFinalise"