diff mbox series

[1/5] bitbake/base: Implement RECIPE_HOSTTOOLS

Message ID 20261129223407.2976672-1-richard.purdie@linuxfoundation.org
State New
Headers show
Series [1/5] bitbake/base: Implement RECIPE_HOSTTOOLS | expand

Commit Message

Richard Purdie Nov. 29, 2026, 10:34 p.m. UTC
Being able to have hosttools added within specific recipes would be useful rather than
always in the global hosttools directory. Add support for this, impementing it within
the TaskStarted event.

We need to use a lockfile to ensure the function doesn't race against other tasks
also trying to create the same thing.

This directory builds on top of HOSTTOOLS which is left as existing so that we don't have
to do any work for recipes which don't need specific additional tools.

Add a dependency on the HOSTTOOLS to do_configure, so when things change, things
do get correctly rebuilt. Previously it was impractical to do this.

Move the conditional tools define for testimage/testsdk into the classes as the
first user. This also avoids a problematic dependency on BB_INCLUDED which would
then have to be excluded.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 meta/classes-global/base.bbclass      | 12 +++++++++++-
 meta/classes-global/sstate.bbclass    |  6 +++---
 meta/classes-global/staging.bbclass   |  2 +-
 meta/classes-recipe/testimage.bbclass |  2 ++
 meta/classes-recipe/testsdk.bbclass   |  2 ++
 meta/conf/bitbake.conf                |  4 +---
 meta/conf/layer.conf                  |  2 +-
 7 files changed, 21 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-global/base.bbclass b/meta/classes-global/base.bbclass
index 9553f004325..5aaa63ce505 100644
--- a/meta/classes-global/base.bbclass
+++ b/meta/classes-global/base.bbclass
@@ -153,7 +153,7 @@  def setup_hosttools_dir(dest, toolsvar, d, fatal=True):
                 notfound.append(tool)
 
     if notfound and fatal:
-        bb.fatal("The following required tools (as specified by HOSTTOOLS) appear to be unavailable in PATH, please install them in order to proceed:\n  %s" % " ".join(notfound))
+        bb.fatal("The following required tools (as specified by %s) appear to be unavailable in PATH, please install them in order to proceed:\n  %s" % (toolsvar, " ".join(notfound)))
 
 # We can't use vardepvalue against do_fetch directly since that would overwrite
 # the other task dependencies so we use an indirect function.
@@ -289,6 +289,15 @@  def buildcfg_neededvars(d):
     if pesteruser:
         bb.fatal('The following variable(s) were not set: %s\nPlease set them directly, or choose a MACHINE or DISTRO that sets them.' % ', '.join(pesteruser))
 
+addhandler base_task_eventhandler
+base_task_eventhandler[eventmask] = "bb.build.TaskStarted"
+python base_task_eventhandler() {
+    if d.getVar('RECIPE_HOSTTOOLS'):
+        toolsdir = d.getVar('RECIPE_HOSTTOOLS_DIR')
+        with bb.utils.fileslocked([toolsdir + ".lock"]):
+            setup_hosttools_dir(toolsdir, 'RECIPE_HOSTTOOLS', d)
+}
+
 addhandler base_eventhandler
 base_eventhandler[eventmask] = "bb.event.ConfigParsed bb.event.MultiConfigParsed bb.event.BuildStarted bb.event.RecipePreFinalise bb.event.RecipeParsed bb.event.RecipePreDeferredInherits"
 python base_eventhandler() {
@@ -383,6 +392,7 @@  base_do_configure() {
 		echo ${BB_TASKHASH} > ${CONFIGURESTAMPFILE}
 	fi
 }
+do_configure[vardeps] += "RECIPE_HOSTTOOLS HOSTTOOLS"
 
 addtask compile after do_configure
 do_compile[dirs] = "${B}"
diff --git a/meta/classes-global/sstate.bbclass b/meta/classes-global/sstate.bbclass
index c6d94591823..4f8128d3d4c 100644
--- a/meta/classes-global/sstate.bbclass
+++ b/meta/classes-global/sstate.bbclass
@@ -4,7 +4,7 @@ 
 # SPDX-License-Identifier: MIT
 #
 
-SSTATE_VERSION = "14"
+SSTATE_VERSION = "15"
 
 SSTATE_ZSTD_CLEVEL ??= "8"
 
@@ -69,7 +69,7 @@  SSTATE_ALLOW_OVERLAP_FILES += "${DEPLOY_DIR_IMAGE}/microcode"
 
 SSTATE_SCAN_FILES ?= "*.la *-config *_config postinst-*"
 SSTATE_SCAN_CMD ??= 'find ${SSTATE_BUILDDIR} \( -name "${@"\" -o -name \"".join(d.getVar("SSTATE_SCAN_FILES").split())}" \) -type f'
-SSTATE_SCAN_CMD_NATIVE ??= 'grep -Irl -e ${RECIPE_SYSROOT} -e ${RECIPE_SYSROOT_NATIVE} -e ${HOSTTOOLS_DIR} ${SSTATE_BUILDDIR}'
+SSTATE_SCAN_CMD_NATIVE ??= 'grep -Irl -e ${RECIPE_SYSROOT} -e ${RECIPE_SYSROOT_NATIVE} -e ${RECIPE_HOSTTOOLS_DIR} -e ${HOSTTOOLS_DIR} ${SSTATE_BUILDDIR}'
 SSTATE_HASHEQUIV_FILEMAP ?= " \
     populate_sysroot:*/postinst-useradd-*:${TMPDIR} \
     populate_sysroot:*/postinst-useradd-*:${COREBASE} \
@@ -99,7 +99,7 @@  SSTATECREATEFUNCS[vardeps] = "SSTATE_SCAN_FILES"
 SSTATEPOSTCREATEFUNCS = ""
 SSTATEPREINSTFUNCS = ""
 SSTATEPOSTUNPACKFUNCS = "sstate_hardcode_path_unpack"
-EXTRA_STAGING_FIXMES ?= "HOSTTOOLS_DIR"
+EXTRA_STAGING_FIXMES ?= "RECIPE_HOSTTOOLS_DIR HOSTTOOLS_DIR"
 
 # Check whether sstate exists for tasks that support sstate and are in the
 # locked signatures file.
diff --git a/meta/classes-global/staging.bbclass b/meta/classes-global/staging.bbclass
index 20340a16e1a..2d3edbb05ef 100644
--- a/meta/classes-global/staging.bbclass
+++ b/meta/classes-global/staging.bbclass
@@ -180,7 +180,7 @@  def staging_processfixme(fixme, target, recipesysroot, recipesysrootnative, d):
     if not fixme:
         return
     cmd = "sed -e 's:^[^/]*/:%s/:g' %s | xargs sed -i -e 's:FIXMESTAGINGDIRTARGET:%s:g; s:FIXMESTAGINGDIRHOST:%s:g'" % (target, " ".join(fixme), recipesysroot, recipesysrootnative)
-    for fixmevar in ['PSEUDO_SYSROOT', 'HOSTTOOLS_DIR', 'PKGDATA_DIR', 'PSEUDO_LOCALSTATEDIR', 'LOGFIFO']:
+    for fixmevar in ['PSEUDO_SYSROOT', 'RECIPE_HOSTTOOLS_DIR', 'HOSTTOOLS_DIR', 'PKGDATA_DIR', 'PSEUDO_LOCALSTATEDIR', 'LOGFIFO']:
         fixme_path = d.getVar(fixmevar)
         cmd += " -e 's:FIXME_%s:%s:g'" % (fixmevar, fixme_path)
     bb.debug(2, cmd)
diff --git a/meta/classes-recipe/testimage.bbclass b/meta/classes-recipe/testimage.bbclass
index 0c1a55a4c98..35eadef1f6d 100644
--- a/meta/classes-recipe/testimage.bbclass
+++ b/meta/classes-recipe/testimage.bbclass
@@ -95,6 +95,8 @@  DEFAULT_TEST_SUITES:remove:qemumips64 = "${MIPSREMOVE}"
 
 TEST_SUITES ?= "${DEFAULT_TEST_SUITES}"
 
+RECIPE_HOSTTOOLS += "ip ping ps scp ssh stty"
+
 QEMU_USE_KVM ?= "1"
 TEST_QEMUBOOT_TIMEOUT ?= "1000"
 TEST_OVERALL_TIMEOUT ?= ""
diff --git a/meta/classes-recipe/testsdk.bbclass b/meta/classes-recipe/testsdk.bbclass
index b1c4fa67e65..3cecdf63dab 100644
--- a/meta/classes-recipe/testsdk.bbclass
+++ b/meta/classes-recipe/testsdk.bbclass
@@ -21,6 +21,8 @@  TESTSDK_CLASS_NAME ?= "oeqa.sdk.testsdk.TestSDK"
 TESTSDKEXT_CLASS_NAME ?= "oeqa.sdkext.testsdk.TestSDKExt"
 TESTSDK_CASE_DIRS ?= "sdk"
 
+RECIPE_HOSTTOOLS += "ip ping ps scp ssh stty"
+
 def import_and_run(name, d):
     import importlib
 
diff --git a/meta/conf/bitbake.conf b/meta/conf/bitbake.conf
index d353a7cd2c3..99e76b704dc 100644
--- a/meta/conf/bitbake.conf
+++ b/meta/conf/bitbake.conf
@@ -524,6 +524,7 @@  export PATH
 
 # Directory with symlinks to host tools used by build
 HOSTTOOLS_DIR = "${TMPDIR}/hosttools"
+RECIPE_HOSTTOOLS_DIR = "${WORKDIR}/recipe-hosttools"
 
 # Tools needed to run builds with OE-Core
 HOSTTOOLS += " \
@@ -538,9 +539,6 @@  HOSTTOOLS += " \
     uniq unzstd wc wget which xargs zstd xz \
 "
 
-# Tools needed to run testimage runtime image testing
-HOSTTOOLS += "${@'ip ping ps scp ssh stty' if (bb.utils.contains_any('IMAGE_CLASSES', 'testimage testsdk', True, False, d) or any(x in (d.getVar("BBINCLUDED") or "") for x in ["testimage.bbclass", "testsdk.bbclass"])) else ''}"
-
 # Link to these if present
 HOSTTOOLS_NONFATAL += "aws gcc-ar gpg gpg-agent ld.bfd ld.gold nc pigz sftp socat ssh sudo"
 
diff --git a/meta/conf/layer.conf b/meta/conf/layer.conf
index 67fef791545..04bceadddee 100644
--- a/meta/conf/layer.conf
+++ b/meta/conf/layer.conf
@@ -138,7 +138,7 @@  DUMMY_PACKAGE_ARCHS_TARGET += "sdk-provides-dummy-target"
 # We need to keep bitbake tools in PATH
 # Avoid empty path entries
 BITBAKEPATH := "${@os.path.dirname(bb.utils.which(d.getVar('PATH'),'bitbake'))}"
-PATH := "${@'${BITBAKEPATH}:' if '${BITBAKEPATH}' != '' else ''}${HOSTTOOLS_DIR}"
+PATH := "${@'${BITBAKEPATH}:' if '${BITBAKEPATH}' != '' else ''}${RECIPE_HOSTTOOLS_DIR}:${HOSTTOOLS_DIR}"
 
 # Only OE-Core should set/change this
 BB_GLOBAL_PYMODULES = "os sys time"