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 via email to