diff mbox series

[meta-tensorflow] bazel.bbclass: Properly set interpreter of bazel

Message ID 20261130-bazel-set-interpreter-properly-v1-1-6b4a4ee1a3b8@gmail.com
State New
Headers show
Series [meta-tensorflow] bazel.bbclass: Properly set interpreter of bazel | expand

Commit Message

Mateusz Marciniec Nov. 30, 2026, 1:37 p.m. UTC
From: Mateusz Marciniec <mateusz.marciniec@nokia.com>

UNINATIVE_LOADER is unset in bazel-native because patchelf may break
self-extracting executable archive. In such a case
populate_sysroot_setscene task won't change the interpreter and
sstate-cache artifacts won't work in different workspaces.
For more details see following commits in meta-tensorflow:
  - dd7642b bazel-native: explicitly disable uninative
  - 16f5def bazel-native: Allow build from sstate-cache
  - f3c18c0 bazel-native: disable uninative

We know that patchelf may break self-extracting archive, but we still
can use it only on the executable part, following steps were added to
accomplish this task:
  - Use `zip --junk-sfx` to get only compressed data
  - Remove compressed data from the original file to get executable part
  - Set interpreter of executable part
  - Merge back executable and compressed data
  - Adjust self-extracting executable archive:
    | Zip entry offsets appear off by 5795984 bytes - correcting...

Additionally I fixed the parts shellcheck would complain about to unify
syntax in do_install_bazel function.

Signed-off-by: Mateusz Marciniec <mateusz.marciniec@nokia.com>
Signed-off-by: Michal Sieron <michal.sieron@nokia.com>
---
Signed-off-by: Mateusz Marciniec <mateuszmar2@gmail.com>
---
 classes/bazel.bbclass | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)


---
base-commit: 5a8e3e631d8147911de36ea264489a1950cef7b0
change-id: 20261130-bazel-set-interpreter-properly-34073463cc4d

Best regards,
--  
Mateusz Marciniec <mateuszmar2@gmail.com>
diff mbox series

Patch

diff --git a/classes/bazel.bbclass b/classes/bazel.bbclass
index 085eff1..0276f5b 100644
--- a/classes/bazel.bbclass
+++ b/classes/bazel.bbclass
@@ -18,10 +18,26 @@  BAZEL ?= "${BAZEL_DIR}/bazel"
 
 do_prepare_recipe_sysroot[postfuncs] += "do_install_bazel"
 do_install_bazel() {
-    mkdir -p ${BAZEL_DIR}
-    install -m 0755 ${STAGING_BINDIR_NATIVE}/bazel ${BAZEL_DIR}
-    create_cmdline_wrapper ${BAZEL} \$BAZEL_STARTUP_OPTIONS
-    zip -A ${BAZEL}.real
+    mkdir -p "${BAZEL_DIR}"
+    install -m 0755 "${STAGING_BINDIR_NATIVE}/bazel" "${BAZEL_DIR}"
+    create_cmdline_wrapper "${BAZEL}" \$BAZEL_STARTUP_OPTIONS
+
+    if which patchelf-uninative; then
+        # Uninative is disabled in bazel-native, bazel will fail if interpreter path has changed
+        # patchelf may break self-extracting executable archive, let's set interpreter only for executable part
+        cp "${BAZEL}.real" "${BAZEL}.real.non-sfx"
+        # Strip SFX stub from the archive
+        zip --junk-sfx "${BAZEL}.real.non-sfx"
+        # Save all but the last x bytes, which are compressed data, to get SFX stub
+        head --bytes=-"$(wc --bytes "${BAZEL}.real.non-sfx" | cut -d' ' -f1)" "${BAZEL}.real" > "${BAZEL}.real.sfx-stub"
+        # Change the dynamic loader of executable part only
+        patchelf-uninative --set-interpreter "${UNINATIVE_LOADER}" "${BAZEL}.real.sfx-stub"
+        # Merge back executable and compressed data
+        cat "${BAZEL}.real.sfx-stub" "${BAZEL}.real.non-sfx" > "${BAZEL}.real"
+        rm "${BAZEL}.real.sfx-stub" "${BAZEL}.real.non-sfx"
+    fi
+
+    zip -A "${BAZEL}.real"
 }
 
 def bazel_get_target_flags(d):