qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH for-10.2 0/2] meson: let Meson handle mixed-language linking of Rust and C objects
@ 2025-08-11 16:17 Paolo Bonzini
  2025-08-11 16:17 ` [PATCH 1/2] rust: do not link C libraries into Rust rlibs Paolo Bonzini
  2025-08-11 16:17 ` [PATCH 2/2] meson: let Meson handle mixed-language linking of Rust and C objects Paolo Bonzini
  0 siblings, 2 replies; 3+ messages in thread
From: Paolo Bonzini @ 2025-08-11 16:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-rust, manos.pitsidianakis

One of the pending issues with Rust's build system integration is
the bloated executables caused by the use of staticlibs.

For an example of what this means, see the following test:

hello.c:
    extern void hello(void);
    int main(void)
    {
        hello();
    }

hello.rs:
    #![no_main]

    #[no_mangle]
    extern "C" fn hello() {
        println!("hello world");
    }

staticlib + gcc:
    $ rustc -Copt-level=2 --crate-type=staticlib hello.rs --emit link=libhello.a
    $ gcc hello.c -lhello -O2
    $ size ./a.out
       text     data    bss     dec     hex filename
     917435    39593    352  957380   e9bc4 ./a.out

rustc + static libstd:
    $ gcc -c -o hello.o hello.c -O2
    $ rustc hello.rs -Clink-arg=hello.o
    $ size ./hello
       text     data    bss     dec     hex filename
     322684    14464    590  337738   5274a ./hello

rustc + dynamic libstd:
    $ gcc -c -o hello.o hello.c -O2
    $ rustc hello.rs -Clink-arg=-Wl,-R$(rustc --print target-libdir) -Clink-arg=hello.o -Cprefer-dynamic
    $ size ./hello
       text    data     bss     dec     hex filename
       1809     600     993    3402     d4a ./hello

The second and third methods will be supported by Meson 1.9.0
(https://mesonbuild.com/Release-notes-for-1-9-0.html).  Modify the final
link pass to use a Rust source file directly, instead of going through
a staticlib, when the target includes Rust crates; Meson will then do
the right thing automatically.

Paolo


Paolo Bonzini (2):
  rust: do not link C libraries into Rust rlibs
  meson: let Meson handle mixed-language linking of Rust and C objects

 meson.build                     | 19 ++++++++-----------
 rust/hw/char/pl011/meson.build  |  2 +-
 rust/hw/timer/hpet/meson.build  |  2 +-
 rust/meson.build                |  2 --
 rust/qemu-api/meson.build       | 15 +++++++++------
 scripts/rust/rust_root_crate.sh |  1 +
 6 files changed, 20 insertions(+), 21 deletions(-)

-- 
2.50.1



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-08-11 16:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-11 16:17 [PATCH for-10.2 0/2] meson: let Meson handle mixed-language linking of Rust and C objects Paolo Bonzini
2025-08-11 16:17 ` [PATCH 1/2] rust: do not link C libraries into Rust rlibs Paolo Bonzini
2025-08-11 16:17 ` [PATCH 2/2] meson: let Meson handle mixed-language linking of Rust and C objects Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).