Skip to contentSkip to conversation

toolchain-scripts: drop unused OECORE_TARGET_SYSROOT environment-setup.d sourcing

PATCH New [email protected] 1 participant 4 messages Sep 30

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 toolchain-scripts: drop unused OECORE_TARGET_SYSROOT environment-setup.d sourcing

From: Harish Sadineni <[email protected]>

Fixes [YOCTO #15061]

The generated SDK/tree environment scripts source *.sh fragments from $OECORE_TARGET_SYSROOT/environment-setup.d. This target-sysroot handling was added in 2d9466734f0c ("toolchain-scripts: Add mechanism to add environment setup scripts") back in 2014, but nothing in oe-core (or the other core layers) has ever installed a fragment into the target sysroot's environment-setup.d. Every environment-setup.d producer (openssl, rpm, git, curl, file, babeltrace2, python3-requests, cmake, rust, cargo, rust-cross-canadian, ...) installs into the native sysroot (SDKPATHNATIVE / native base_prefix), i.e. everything is nativesdk based.

The target-sysroot loop is therefore dead code that only adds confusion about where environment fragments are expected to live. Remove it.

---
 meta/classes-recipe/toolchain-scripts.bbclass | 5 -----
 1 file changed, 5 deletions(-)
meta/classes-recipe/toolchain-scripts.bbclass +0 -5
index e971b9a270..8bfcde55b5 100644
@@ -164,11 +164,6 @@ toolchain_shared_env_script () {
     cat >> $script <<EOF
 
 # Append environment subscripts
-if [ -d "\$OECORE_TARGET_SYSROOT/environment-setup.d" ]; then
-    for envfile in \$OECORE_TARGET_SYSROOT/environment-setup.d/*.sh; do
-	    . \$envfile
-    done
-fi
 if [ -d "\$OECORE_NATIVE_SYSROOT/environment-setup.d" ]; then
     for envfile in \$OECORE_NATIVE_SYSROOT/environment-setup.d/*.sh; do
 	    . \$envfile
Signature · 1 line
2.51.0
#1 lore raw

PATCH v3 2/4 toolchain-scripts: source target-specific environment setup scripts

From: Harish Sadineni <[email protected]>

Fixes [YOCTO #15061]

Source any *.sh files from environment-setup.d/${TARGET_ARCH} in addition to the existing environment-setup.d directory in the generated SDK and tree environment setup scripts.

This complements the rust-cross-canadian changes, where Rust environment setup scripts are installed under a target-specific directory. By sourcing only the scripts that match the active target, the SDK avoids loading environment settings for other target variants, such as mixing 32-bit and 64-bit configurations in multilib SDKs.

---
 meta/classes-recipe/toolchain-scripts.bbclass | 5 +++++
 1 file changed, 5 insertions(+)
meta/classes-recipe/toolchain-scripts.bbclass +5 -0
index 8bfcde55b5..7b10b76609 100644
@@ -169,6 +169,11 @@ if [ -d "\$OECORE_NATIVE_SYSROOT/environment-setup.d" ]; then
 	    . \$envfile
     done
 fi
+if [ -d "\$OECORE_NATIVE_SYSROOT/environment-setup.d/\$OECORE_TARGET_ARCH" ]; then
+    for envfile in \$OECORE_NATIVE_SYSROOT/environment-setup.d/\$OECORE_TARGET_ARCH/*.sh; do
+            . \$envfile
+    done
+fi
 EOF
 }
 
Signature · 1 line
2.51.0
#2 lore raw

PATCH v3 3/4 rust-cross-canadian: install target-specific env scripts and fix rustlib lookup

In reply to [email protected]

From: Harish Sadineni <[email protected]>

Fixes [YOCTO #15061]

Fixes 32-bit Cargo build failures when using multilib-enabled SDKs.

Store the generated environment setup script in a target-specific subdirectory so that sourcing the SDK environment always loads the matching script for the selected target, avoiding accidental reuse of the 64-bit configuration when building for a 32-bit target.

Also update CARGO_TARGET_<TRIPLE>_RUSTFLAGS to add an explicit -L search path to the target rustlib directory inside the sysroot. This ensures Cargo can locate the correct target-specific Rust libraries during cross-compilation with the cross-canadian toolchain.

---
 meta/recipes-devtools/rust/rust-cross-canadian.inc | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
meta/recipes-devtools/rust/rust-cross-canadian.inc +5 -4
index f083bcda82..689ffa7efd 100644
@@ -51,14 +51,15 @@ do_install () {
     chmod +x "$outfile"
     create_sdk_wrapper "${SYS_BINDIR}/target-rust-ccld-wrapper" "CC"
 
-    ENV_SETUP_DIR=${D}${base_prefix}/environment-setup.d
-    mkdir "${ENV_SETUP_DIR}"
-    RUST_ENV_SETUP_SH="${ENV_SETUP_DIR}/${RUST_TARGET_SYS}_rust.sh"
+    ENV_SETUP_DIR=${D}${base_prefix}/environment-setup.d/${TARGET_ARCH}
+    mkdir -p "${ENV_SETUP_DIR}"
+    RUST_ENV_SETUP_SH="${ENV_SETUP_DIR}/rust.sh"
 
     RUST_TARGET_TRIPLE=`echo ${RUST_TARGET_SYS} | tr '[:lower:]' '[:upper:]' | sed 's/-/_/g'`
 
     cat <<- EOF > "${RUST_ENV_SETUP_SH}"
-	export CARGO_TARGET_${RUST_TARGET_TRIPLE}_RUSTFLAGS="--sysroot=\$OECORE_TARGET_SYSROOT/usr -C link-arg=--sysroot=\$OECORE_TARGET_SYSROOT"
+	LIB_PATH="\$OECORE_TARGET_SYSROOT/usr/${baselib}/rustlib/${RUST_TARGET_SYS}/lib"
+	export CARGO_TARGET_${RUST_TARGET_TRIPLE}_RUSTFLAGS="--sysroot=\$OECORE_TARGET_SYSROOT/usr -C link-arg=--sysroot=\$OECORE_TARGET_SYSROOT -L \$LIB_PATH"
 	export CARGO_BUILD_TARGET="${RUST_TARGET_SYS}"
 	export RUST_TARGET_PATH="\$OECORE_NATIVE_SYSROOT/usr/lib/${TARGET_SYS}/rustlib"
 	EOF
Signature · 1 line
2.51.0
#3 lore raw

PATCH v3 4/4 oeqa/sdk/cases/rust.py: Expand test to verify cargo build builds for target

In reply to [email protected]

From: Harish Sadineni <[email protected]>

Fixes [YOCTO #15061]

Ensure that cargo build successfully builds the binary for the target by default. This test validates whether the default build process produces the expected output for the specified target.

---
 meta/lib/oeqa/sdk/cases/rust.py | 37 ++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
meta/lib/oeqa/sdk/cases/rust.py +36 -1
index 4b115bebf5..b4a8a1b671 100644
@@ -7,6 +7,10 @@
 import os
 import shutil
 import unittest
+import json
+import subprocess
+import re
+
 
 from oeqa.sdk.case import OESDKTestCase
 
@@ -32,8 +36,39 @@ class RustCompileTest(OESDKTestCase):
 
     def test_cargo_build(self):
         self._run('cd %s/hello; cargo add zstd' % (self.tc.sdk_dir))
-        self._run('cd %s/hello; cargo build' % self.tc.sdk_dir)
+        result_env = self._run("echo $CARGO_BUILD_TARGET")
+        rust_target_sys = result_env.strip()
+        result = self._run(
+            "cd %s/hello; cargo build --message-format=json-render-diagnostics"
+            % self.tc.sdk_dir
+        )
+
+        executable_path = None
+        for line in result.splitlines():
+            try:
+                msg = json.loads(line)
+            except json.JSONDecodeError:
+                continue
+
+            # Cargo emits multiple JSON messages; we want the executable
+            if isinstance(msg, dict) and msg.get("executable"):
+                executable_path = msg["executable"]
+
+        parts = executable_path.split(os.sep)
+        target_index = parts.index("target")
+        path_component = parts[target_index + 1]
+        if path_component in ("debug", "release"):
+            file_output = subprocess.check_output(["file", executable_path]).decode().strip()
+            match = re.search(r"/sysroots/([^/]+)/lib/", file_output)
+            target_triple = match.group(1) if match else None
+        else:
+            target_triple = path_component
 
+        self.assertTrue(
+            rust_target_sys == target_triple,
+            f"Cargo built natively for the nativesdk host instead of '{rust_target_sys}.\n"
+            f"Cargo build needs to build for target by default not nativesdk_host"
+        )
 class RustHostCompileTest(OESDKTestCase):
     td_vars = ['MACHINE', 'SDK_SYS']
 
Signature · 1 line
2.51.0