diff mbox series

rust: enable fully static linking with TCLIBC=glibc

Message ID 20261130-mfe-rust-static-glibc-v1-1-5c24d978d5ef@pengutronix.de
State New
Headers show
Series rust: enable fully static linking with TCLIBC=glibc | expand

Commit Message

Marco Felsch Nov. 30, 2026, 8:29 p.m. UTC
From: Marco Felsch <marco.felsch@gmail.com>

Rust binaries built with TCLIBC=glibc and -C target-feature=+crt-static
were still dynamically linked, because the generated target spec only set
crt-static-respected for musl, so rustc ignored +crt-static. Upstream rustc
sets it for all Linux targets (base/linux.rs), so do the same. No other
change is needed: with crt-static the libc and unwind crates statically
link libc, libm, libpthread, libdl, librt, libutil, libgcc_eh and libgcc,
which glibc and libgcc already provide in the target sysroot.

Rename rust-static-musl-test to rust-static-test and RustStaticMuslTest to
RustStaticTest with a musl and a new glibc test, as the same check applies
to both.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
Add the moment only musl supports static builds albeit everything is in
place to static link against glibc as well. Of course the static linked
binary is not as small as musl builds, but a BSP can avoid multiconfig
setups.

I've tested this with rsinit, a Rust based initrd application which
requires only a dm-verity params file in addition to the static linked
Rust binary bundled as CPIO.

Regards,
Marco
---
 .../rust/rust-static-musl-test/src/main.rs           |  3 ---
 .../Cargo.lock                                       |  2 +-
 .../Cargo.toml                                       |  2 +-
 .../rust/rust-static-test/src/main.rs                |  3 +++
 ...atic-musl-test_0.1.bb => rust-static-test_0.1.bb} |  6 ++----
 meta/classes-recipe/rust-target-config.bbclass       |  4 +++-
 meta/lib/oeqa/selftest/cases/rust.py                 | 20 ++++++++++++--------
 7 files changed, 22 insertions(+), 18 deletions(-)


---
base-commit: 39019629a4ad25557f120ec98305a4a2ad6a8558
change-id: 20261130-mfe-rust-static-glibc-0fca41f2de61

Best regards,
diff mbox series

Patch

diff --git a/meta-selftest/recipes-devtools/rust/rust-static-musl-test/src/main.rs b/meta-selftest/recipes-devtools/rust/rust-static-musl-test/src/main.rs
deleted file mode 100644
index 85dcaf9df57252777c507a923954a21d460237f4..0000000000000000000000000000000000000000
--- a/meta-selftest/recipes-devtools/rust/rust-static-musl-test/src/main.rs
+++ /dev/null
@@ -1,3 +0,0 @@ 
-fn main() {
-    println!("static-musl-ok");
-}
diff --git a/meta-selftest/recipes-devtools/rust/rust-static-musl-test/Cargo.lock b/meta-selftest/recipes-devtools/rust/rust-static-test/Cargo.lock
similarity index 58%
rename from meta-selftest/recipes-devtools/rust/rust-static-musl-test/Cargo.lock
rename to meta-selftest/recipes-devtools/rust/rust-static-test/Cargo.lock
index cb488024c31b58403487518daf031a456d5a9ff1..16d319cbad5ab351aeb066f4f91942a1443b15ec 100644
--- a/meta-selftest/recipes-devtools/rust/rust-static-musl-test/Cargo.lock
+++ b/meta-selftest/recipes-devtools/rust/rust-static-test/Cargo.lock
@@ -1,5 +1,5 @@ 
 version = 3
 
 [[package]]
-name = "rust-static-musl-test"
+name = "rust-static-test"
 version = "0.1.0"
diff --git a/meta-selftest/recipes-devtools/rust/rust-static-musl-test/Cargo.toml b/meta-selftest/recipes-devtools/rust/rust-static-test/Cargo.toml
similarity index 59%
rename from meta-selftest/recipes-devtools/rust/rust-static-musl-test/Cargo.toml
rename to meta-selftest/recipes-devtools/rust/rust-static-test/Cargo.toml
index d7071302bde25aaa3f9c669f3d39fc5c87de9817..0504567e322aa5eff43faf554bea27d10d1b2159 100644
--- a/meta-selftest/recipes-devtools/rust/rust-static-musl-test/Cargo.toml
+++ b/meta-selftest/recipes-devtools/rust/rust-static-test/Cargo.toml
@@ -1,4 +1,4 @@ 
 [package]
-name = "rust-static-musl-test"
+name = "rust-static-test"
 version = "0.1.0"
 edition = "2021"
diff --git a/meta-selftest/recipes-devtools/rust/rust-static-test/src/main.rs b/meta-selftest/recipes-devtools/rust/rust-static-test/src/main.rs
new file mode 100644
index 0000000000000000000000000000000000000000..8f7367522333874670c83cf02cbc59f752bde4cf
--- /dev/null
+++ b/meta-selftest/recipes-devtools/rust/rust-static-test/src/main.rs
@@ -0,0 +1,3 @@ 
+fn main() {
+    println!("static-ok");
+}
diff --git a/meta-selftest/recipes-devtools/rust/rust-static-musl-test_0.1.bb b/meta-selftest/recipes-devtools/rust/rust-static-test_0.1.bb
similarity index 66%
rename from meta-selftest/recipes-devtools/rust/rust-static-musl-test_0.1.bb
rename to meta-selftest/recipes-devtools/rust/rust-static-test_0.1.bb
index 442c24c1da9246e2f0c5f2f033e9e8299ff4c6d0..c7492cf4cb4f879e948cbe77c3d987a68e5d5d8f 100644
--- a/meta-selftest/recipes-devtools/rust/rust-static-musl-test_0.1.bb
+++ b/meta-selftest/recipes-devtools/rust/rust-static-test_0.1.bb
@@ -1,7 +1,7 @@ 
 # SPDX-License-Identifier: MIT
-# Minimal Rust binary to test static musl linking (bug 16076)
+# Minimal Rust binary to test static musl (bug 16076) and glibc linking
 
-SUMMARY = "Minimal Rust binary for static musl linking regression test"
+SUMMARY = "Minimal Rust binary for static linking regression test"
 LICENSE = "MIT"
 LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
 
@@ -15,6 +15,4 @@  CARGO_SRC_DIR = ""
 
 inherit cargo
 
-COMPATIBLE_HOST = ".*-musl.*"
-
 SSTATE_SKIP_CREATION = "1"
diff --git a/meta/classes-recipe/rust-target-config.bbclass b/meta/classes-recipe/rust-target-config.bbclass
index 89fbd19374696ef72f26568ec0b608ec785521ee..8a3ab41f8f41c450394b1821152d1303391a0992 100644
--- a/meta/classes-recipe/rust-target-config.bbclass
+++ b/meta/classes-recipe/rust-target-config.bbclass
@@ -438,7 +438,9 @@  def rust_gen_target(d, thing, wd, arch):
     tspec['has-thread-local'] = True
     tspec['position-independent-executables'] = True
     tspec['panic-strategy'] = d.getVar("RUST_PANIC_STRATEGY")
-    if "musl" in tspec['llvm-target']:
+    # Like upstream rustc's base Linux target, honour -C target-feature=+crt-static
+    # for both glibc and musl
+    if tspec['os'] == "linux":
         tspec['crt-static-respected'] = True
     # AArch64 AAPCS64 requires non-leaf frame pointers per
     # https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#the-frame-pointer
diff --git a/meta/lib/oeqa/selftest/cases/rust.py b/meta/lib/oeqa/selftest/cases/rust.py
index 03af98345eb9a69748e7184007396e5b56cd039b..f27ccaf81b83b2ad52579dc542f93e6de2fdf576 100644
--- a/meta/lib/oeqa/selftest/cases/rust.py
+++ b/meta/lib/oeqa/selftest/cases/rust.py
@@ -152,22 +152,20 @@  class RustSelfTestSystemEmulated(OESelftestTestCase, OEPTestResultTestCase):
             for test in test_results:
                 self.ptest_result(ptestsuite, test, test_results[test])
 
-# Regression test for bug 16076 - verify static linking with TCLIBC=musl
-class RustStaticMuslTest(OESelftestTestCase):
+# Regression test for bug 16076 - verify static linking with TCLIBC=musl and glibc
+class RustStaticTest(OESelftestTestCase):
 
-    RECIPE = "rust-static-musl-test"
+    RECIPE = "rust-static-test"
 
-    def setUp(self):
-        super().setUp()
+    def check_static_linking(self, tclibc):
         self.write_config(
-            'TCLIBC = "musl"\n'
-            'RUSTFLAGS:append:pn-%s = " -C target-feature=+crt-static"\n' % self.RECIPE
+            'TCLIBC = "%s"\n'
+            'RUSTFLAGS:append:pn-%s = " -C target-feature=+crt-static"\n' % (tclibc, self.RECIPE)
         )
         result = bitbake(self.RECIPE, ignore_status=True)
         self.assertEqual(result.status, 0,
             msg="bitbake %s failed:\n%s" % (self.RECIPE, result.output))
 
-    def test_static_musl_linking(self):
         workdir = get_bb_var("WORKDIR", self.RECIPE)
         cargo_target_subdir = get_bb_var("CARGO_TARGET_SUBDIR", self.RECIPE)
         pn = get_bb_var("PN", self.RECIPE)
@@ -180,3 +178,9 @@  class RustStaticMuslTest(OESelftestTestCase):
         self.assertIn("statically linked", result.output,
             msg="Binary is not statically linked. Regression of bug 16076.\n"
                 "file output: %s" % result.output)
+
+    def test_static_glibc_linking(self):
+        self.check_static_linking("glibc")
+
+    def test_static_musl_linking(self):
+        self.check_static_linking("musl")