diff mbox series

[v2] bindgen-cli: set CLANG_PATH in native and nativesdk wrappers to matching clang

Message ID 20260712054313.2099693-1-yogesh.tyagi@intel.com
State Under Review
Headers show
Series [v2] bindgen-cli: set CLANG_PATH in native and nativesdk wrappers to matching clang | expand

Commit Message

Tyagi, Yogesh July 12, 2026, 5:43 a.m. UTC
From: Yogesh Tyagi <yogesh.tyagi@intel.com>

bindgen uses the clang-sys crate to locate a 'clang' executable for
querying default include paths. clang-sys's search order is CLANG_PATH,
then libclang's directory, then 'llvm-config --bindir', then PATH.

When clang is used as the toolchain, the crossscripts llvm-config wrapper
honours YOCTO_ALTERNATE_EXE_PATH and reports the target sysroot bindir.
clang-sys's 'llvm-config --bindir' step then resolves to a target clang
that cannot execute on the build host, so bindgen fails with:

  could not run executable .../recipe-sysroot/usr/bin/clang-NN:
  No such file or directory (os error 2)

This is hit, for example, when building mesa with the rusticl/opencl
PACKAGECONFIG, whose do_compile runs the native bindgen against
rusticl_mesa_bindings.h, and when building rust-in-kernel via
kernel-yocto-rust.bbclass, which pulls in clang-native and
bindgen-cli-native.

Set CLANG_PATH to the clang that matches the wrapped libclang so
clang-sys uses the runnable binary first, short-circuiting the
llvm-config lookup. The wrapper already exports LIBCLANG_PATH the same
way.

The recipe is BBCLASSEXTEND = "native nativesdk", but only the native
variant had a wrapper. Add the equivalent nativesdk wrapper so an SDK
that runs bindgen (e.g. rust kernel-module development in the SDK) does
not hit the identical target-clang failure. The native wrapper uses
STAGING_*_NATIVE; the nativesdk wrapper uses the on-target ${libdir}/
${bindir} so create_wrapper's relocation rewrites the full path and the
shipped wrapper carries no build-time (TMPDIR) reference.

Signed-off-by: Yogesh Tyagi <yogesh.tyagi@intel.com>
---
v2:
 - Add the missing nativesdk bindgen wrapper (v1 only wrapped native),
   so an SDK that runs bindgen does not hit the same target-clang failure.
 - The nativesdk wrapper uses the on-target ${libdir}/${bindir} rather
   than STAGING_LIBDIR/STAGING_BINDIR: the latter include the recipe
   sysroot (TMPDIR) prefix, which create_wrapper's relocation does not
   strip, so the shipped wrapper failed do_package_qa with a buildpaths
   (reference to TMPDIR) error.

 .../bindgen-cli/bindgen-cli_0.72.1.bb         | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/meta/recipes-devtools/bindgen-cli/bindgen-cli_0.72.1.bb b/meta/recipes-devtools/bindgen-cli/bindgen-cli_0.72.1.bb
index 72dadfe52f..2ef797c156 100644
--- a/meta/recipes-devtools/bindgen-cli/bindgen-cli_0.72.1.bb
+++ b/meta/recipes-devtools/bindgen-cli/bindgen-cli_0.72.1.bb
@@ -11,8 +11,25 @@  SRCREV = "d874de8d646d9b8a3e7ba2db2bcd52f2fba8f1f5"
 
 require ${BPN}-crates.inc
 
+# bindgen uses the clang-sys crate to locate a 'clang' executable for querying
+# default include paths. clang-sys's search order is: CLANG_PATH, libclang's dir,
+# 'llvm-config --bindir', then PATH. When clang is the toolchain, the crossscripts
+# llvm-config wrapper honours YOCTO_ALTERNATE_EXE_PATH and reports the target
+# sysroot bindir, so clang-sys's 'llvm-config --bindir' step resolves to a target
+# clang that cannot run on the build host (native bindgen then fails with
+# "could not run executable .../clang-NN: No such file or directory", e.g. when
+# building mesa with the rusticl/opencl PACKAGECONFIG). Set CLANG_PATH to the
+# native clang so the runnable host binary is used first.
 do_install:append:class-native() {
-	create_wrapper ${D}/${bindir}/bindgen LIBCLANG_PATH="${STAGING_LIBDIR_NATIVE}"
+	create_wrapper ${D}/${bindir}/bindgen \
+		LIBCLANG_PATH="${STAGING_LIBDIR_NATIVE}" \
+		CLANG_PATH="${STAGING_BINDIR_NATIVE}/clang"
+}
+
+do_install:append:class-nativesdk() {
+	create_wrapper ${D}/${bindir}/bindgen \
+		LIBCLANG_PATH="${libdir}" \
+		CLANG_PATH="${bindir}/clang"
 }
 
 BBCLASSEXTEND = "native nativesdk"