qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-rust@nongnu.org, manos.pitsidianakis@linaro.org
Subject: [PATCH for-10.2 0/2] meson: let Meson handle mixed-language linking of Rust and C objects
Date: Mon, 11 Aug 2025 18:17:35 +0200	[thread overview]
Message-ID: <20250811161737.220835-1-pbonzini@redhat.com> (raw)

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



             reply	other threads:[~2025-08-11 16:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-11 16:17 Paolo Bonzini [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250811161737.220835-1-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=manos.pitsidianakis@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-rust@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).