alsa-sys icon indicating copy to clipboard operation
alsa-sys copied to clipboard

Feature request: allow static linking to libasound

Open ssssam opened this issue 2 years ago • 7 comments

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.

ssssam avatar Nov 02 '21 15:11 ssssam

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

diwic avatar Nov 02 '21 16:11 diwic

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.

ssssam avatar Nov 02 '21 16:11 ssssam

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?

diwic avatar Nov 03 '21 01:11 diwic

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.

ssssam avatar Nov 04 '21 10:11 ssssam

@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)

cybersoulK avatar Sep 06 '23 07:09 cybersoulK