jguhlin / minimap2-rs

Rust bindings to minimap2 library
Other
51 stars 11 forks source link

Linking issues for static build #38

Closed zeeev closed 4 months ago

zeeev commented 1 year ago

Hi @jguhlin,

Thanks for building this wrapper to minimap2 it's been extremely useful. We (@jrharting) are trying to get a static build working for our project, but hitting linking errors in linux. We traced the problem back to minimap2-rs. Do you have any suggestions?

cargo --version
cargo 1.68.2 (6feb7c9cf 2023-03-26)
export RUSTFLAGS="cargo build --release --target x86_64-unknown-linux-gnu"
cargo build --release --target x86_64-unknown-linux-gnu

I reproduced the error just in the minimap2-rs repo by doing the following:

export RUSTFLAGS="cargo build --release --target x86_64-unknown-linux-gnu"
cargo test --release --target x86_64-unknown-linux-gnu

  = note: /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: /mnt/software/r/rust/1.68.2/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-bc6b80525d6b1f3b.rlib(std-bc6b80525d6b1f3b.std.3431a86c-cgu.0.rcgu.o): in function `std::sys::unix::os::home_dir::fallback':
          /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys/unix/os.rs:626: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: /mnt/software/r/rust/1.68.2/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-bc6b80525d6b1f3b.rlib(std-bc6b80525d6b1f3b.std.3431a86c-cgu.0.rcgu.o): in function `<std::sys_common::net::LookupHost as core::convert::TryFrom<(&str,u16)>>::try_from::{{closure}}':
          /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/net.rs:206: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: attempted static link of dynamic object `/mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/../x86_64-libc_2.17-linux-gnu/sysroot/usr/lib/../lib64/libm.so'
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: attempted static link of dynamic object `/mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/../x86_64-libc_2.17-linux-gnu/sysroot/lib64/libpthread.so.0'
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: attempted static link of dynamic object `/mnt/software/z/zlib/1.2.11/lib/libz.so'
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: attempted static link of dynamic object `/mnt/software/z/zlib/1.2.11/lib/libz.so'
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: /mnt/software/r/rust/1.68.2/lib/rustlib/x86_64-unknown-linux-gnu/lib/libtest-dfbb7690b1f6faf1.rlib(test-dfbb7690b1f6faf1.test.ee13aa5d-cgu.0.rcgu.o): undefined reference to symbol '__tls_get_addr@@GLIBC_2.3'
          /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/x86_64-libc_2.17-linux-gnu-ld.bfd: /mnt/software/g/gcc/gcctoolchain/gcctoolchain-release_10.2.0.125178/gcc/gcc_10.3.0.pbi01/build-libc_2.17/target-libc_2.17/binnowrap/../x86_64-libc_2.17-linux-gnu/sysroot/lib64/ld-linux-x86-64.so.2: error adding symbols: DSO missing from command line
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `minimap2` (lib test) generated 6 warnings (3 duplicates)
error: could not compile `minimap2` due to previous error; 6 warnings emitted
jguhlin commented 1 year ago

Hey there. I believe Rust statically links everything by default except for glibc, which is where the error is occurring. I don't think you can get around that, but you could compile on an older glibc system (like the manylinux container the Python library uses) to assure backwards compatibility. It lets me release a python module that is precompiled yet works on many different systems. You can see the manylinux compilation here: https://github.com/jguhlin/minimap2-rs/blob/main/.github/workflows/build-test-rust-minimappers2.yaml

If you truly want to statically link, you need to drop glibc and use musl (minimap2-rs has a test to compile with musl as well, so compatibility is assured). I haven't done any benchmarking though.

rustup target add x86_64-unknown-linux-musl
set -x RUSTFLAGS '-C target-feature=+crt-static'
cargo test --release --target x86_64-unknown-linux-musl

Let me know if none of those work.

zeeev commented 1 year ago

Hi @jguhlin,

Thanks for quick reply with good suggestions. Unfortunately, we are tied to using a system wide install of rust (i.e. rustup cannot be used) and therefore I cannot switch to musl. Looking into other options.

jrharting commented 1 year ago

sorry to bug, but we were wondering if there is any update on a release version?

jguhlin commented 1 year ago

@jrharting Oops, missed this. There's a release coming out soon, but it may not fix the above problem

jrharting commented 1 year ago

no worries! thanks for the update. We will see what we can do re the glibc issue and if we find anything will let you know.

zeeev commented 1 year ago

@jguhlin we've been able to build tools for static release that have htslib as a dependancy. Wondering what deps are making this challenging?

jguhlin commented 1 year ago

@jrharting @zeeev New update is out, see if that fixes it? Or if you can get a new error report posted here?

Otherwise I think it's zlib, we can change it by using zlib-ng, but that could have problems on other platforms. I'll work on making it into a feature.

Update: there's a static flag for libz-sys (with your mention of htslib I went digging in there code and found it). I think that might work too, so if the newest version doesn't work try out the github repo?

zeeev commented 1 year ago

Hi @jguhlin, sorry for the delay. I've uploaded the error message for 0.1.14

err.txt

Trying the GitHub version now. Thanks again!

zeeev commented 1 year ago

Here is the GitHub version error.

err-gh.txt

jguhlin commented 1 year ago

Thanks, this is great. I'll enable a static flag for zlib and a zlibng flag as well. It may be later today or next week, unfortunately as I'm at a conference this week. But I do think it is that.

On Wed, 19 Jul 2023, 1:28 am Zev Kronenberg, @.***> wrote:

Here is the GitHub version error.

err-gh.txt https://github.com/jguhlin/minimap2-rs/files/12082948/err-gh.txt

— Reply to this email directly, view it on GitHub https://github.com/jguhlin/minimap2-rs/issues/38#issuecomment-1640448332, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOA2CBAIE4TUKH2ZYCNLBTXQ2TQFANCNFSM6AAAAAAYG622AQ . You are receiving this because you were mentioned.Message ID: @.***>

jguhlin commented 11 months ago

@zeeev Looks like the errors have changed, so it could be a libcurl problem. Do you have any deps needing curl-sys (cargo tree should give this). There are static features for curl-sys. But in case it is still this crate, I've created a test branch:

Please try this in the Cargo.toml and give it a go

minimap2 = { git = "https://github.com/jguhlin/minimap2-rs", branch = "static" }

Let me know what your command to build the static binary is as well, I'd like to get it into the CI system here too. :)

zeeev commented 11 months ago

Hi @jguhlin,

Here's the command that works for other tools on our build server.

module load cmake gcc zlib rust/1.68.2 htslib
export RUSTFLAGS="-C target-feature=+crt-static -C relocation-model=static"
cargo build --release --target x86_64-unknown-linux-gnu

I tried the new branch and attached the error build.err.txt

Thanks for checking it out.

jguhlin commented 11 months ago

@zeeev Thanks, that helps a ton. It compiles and runs tests with: "target-feature=+crt-static" but the relocation model is causing some errors with -fPIE, so different from the errors you are getting. I suspect you have other crates causing problems now. But can't be certain without checking the code. But this is compiling and testing with +crt-static, and giving another error entirely at the linking step for the relocation model. Going to dig into this, and will let you know when I've got something!

jguhlin commented 11 months ago

@zeeev Do you need the relocation model to be static? It does compile and run all tests without it, and ldd on fakeminimap2 in the static branch shows it as static, and I can bring it over to another machine and it runs?

(base) joseph@josephguhlin:~/development/minimap2-rs/fakeminimap2$ ldd target/release/fakeminimap2
        statically linked

Also change the dep line to:

minimap2 = { git = "https://github.com/jguhlin/minimap2-rs", branch = "static", features = ["static"]}

(although zlib-ng should work as a feature too)