diwic / alsa-sys

Rust raw FFI bindings for ALSA
MIT License
18 stars 12 forks source link

Feature request: allow static linking to libasound #10

Open ssssam opened 2 years ago

ssssam commented 2 years ago

I am trying to build an embedded audio project using cpal, alsa-rs and alsa-sys. The target OS has an old GLibc and it might be tricky to upgrade. Building a static linked binary would be ideal.

At present alsa-sys hardwires statik(false) in build.rs so there's no way to static link.

I experimented with changing this to static(true) and building with armv7-unknown-linux-musleabihf toolchain. Something goes wrong with the link at this point, and various link errors appear like these:

  = note: /home/sam/src/organelle-ports-musl/output/host/lib/gcc/arm-buildroot-linux-musleabihf/10.3.0/../../../../arm-buildroot-linux-musleabihf/bin/ld: /home/sam/src/alsa-rs/synth-example/target/armv7-unknown-linux-musleabihf/debug/deps/libalsa_sys-aae2be964235592b.rlib(conf.o): in function `config_file_load':
          conf.c:(.text+0x4fa0): undefined reference to `__stat_time64'
          /home/sam/src/organelle-ports-musl/output/host/lib/gcc/arm-buildroot-linux-musleabihf/10.3.0/../../../../arm-buildroot-linux-musleabihf/bin/ld: /home/sam/src/alsa-rs/synth-example/target/armv7-unknown-linux-musleabihf/debug/deps/libalsa_sys-aae2be964235592b.rlib(conf.o): in function `snd_config_update_r':
          conf.c:(.text+0xc130): undefined reference to `__stat_time64'
          /home/sam/src/organelle-ports-musl/output/host/lib/gcc/arm-buildroot-linux-musleabihf/10.3.0/../../../../arm-buildroot-linux-musleabihf/bin/ld: /home/sam/src/alsa-rs/synth-example/target/armv7-unknown-linux-musleabihf/debug/deps/libalsa_sys-aae2be964235592b.rlib(pcm_dmix.o): in function `snd_pcm_dmix_status':
          pcm_dmix.c:(.text+0xe0c): undefined reference to `__clock_gettime64'

I experimented with adding -lc or removing -nodefaultlibs from the generated linkline and got other errors suggesting double linking against libc.

This is the limit of my expertise, just reporting the issue in case someone else has been looking into the same thing.

diwic commented 2 years ago

A quick googling yields this result, might be helpful? https://stackoverflow.com/questions/61934997/undefined-reference-to-stat-time64-when-cross-compiling-rust-project-on-mu

ssssam commented 2 years ago

Thanks for the pointer! This suggests that changing .statik(true) should just work, and something about my toolchain / sysroot is broken.

I am building with Buildroot 2021.08, which has Musl 1.22 and Rust 1.54. I have just spotted that Buildroot doesn't build the rust-std libraries itself, it downloads them from https://static.rust-lang.org/dist/rust-std-1.54.0-armv7-unknown-linux-musleabihf.tar.xz and looking at the contained liblibc-e1ae72f87d6a487b.rlib file, this might indeed contain a statically linked copy of Musl 1.1.

In which case, while alsa-sys could add support for static linking now, it might not be useful until https://github.com/rust-lang/rust/issues/72274 is completed.

diwic commented 2 years ago

Feel free to add a PR that adds support for static linking, I suppose this would be implemented as a feature not enabled by default?

ssssam commented 2 years ago

Right, I wasn't sure how to do this, but a feature flag makes sense. When I get some time I will do a PR.

cybersoulK commented 1 year ago

@ssssam @diwic does this mean that i cannot manually compile alsa lib on an ubuntu machine and link it with cargo-zigbuild on my mac?? I have been trying for days to make it work. and it compiles and links successfuly, just to segfault on my players...

(the target is x86_64-unknown-linux-gnu)

AnthonyMichaelTDM commented 3 months ago

any updates on this?

diwic commented 3 months ago

@AnthonyMichaelTDM Not to my knowledge. The other binding I maintain - https://github.com/diwic/dbus-rs - someone contributed a way to download the libdbus C code and cross compile it with the rest of the code. That might be an option here as well, if someone wants to do the work?