alexcrichton / ssh2-rs

Rust bindings for libssh2
https://docs.rs/ssh2
Apache License 2.0
491 stars 148 forks source link

Build fails on macOS 10.14.6 Mojave #176

Closed idubrov closed 4 years ago

idubrov commented 4 years ago

Simplest program depending on libssh2-sys = "0.2.16" and doing libssh2_sys::init(); fails on macOS 10.14.6 Mojave.

Here is the project: https://github.com/idubrov/openssl-macos-issue

For convenience, this is all it is:

Cargo.toml

[package]
name = "openssl-macos-issue"
version = "0.1.0"
edition = "2018"

[dependencies]
libssh2-sys = "0.2.16"

src/main.rs:

fn main() {
    libssh2_sys::init();
}

Steps to reproduce: run cargo run on macOS

Results:

   Compiling openssl-macos-issue v0.1.0 (/Users/idubrov/Projects/openssl-macos-issue)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5.14oph2ryrcic2ns5.rcgu.o" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5.1dts6u7a2usqigcg.rcgu.o" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5.3ozgcq2xcp4pi75n.rcgu.o" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5.3x3q5atvk5pzpzxq.rcgu.o" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5.77err7e5ernzm9f.rcgu.o" "-o" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/openssl_macos_issue-2efbd7ed99d011e5.2nymup00na9yzsl0.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps" "-L" "/usr/lib" "-L" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/build/libssh2-sys-e12fc67748fd4726/out/build" "-L" "/usr/local/opt/openssl@1.1/lib" "-L" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/liblibssh2_sys-11046a02422a3a81.rlib" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/libopenssl_sys-5e28547054fc718d.rlib" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/liblibz_sys-f8ff374315e8a85c.rlib" "/Users/idubrov/Projects/openssl-macos-issue/target/debug/deps/liblibc-17765c000cbeb2f7.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-b7d0d4c6440e3596.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-2fa12511d8ee5824.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-29d15f8ede79c49d.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-4f15d040f73e4150.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace-4fcf576fa458096e.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-2406d6e5b831dd2b.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-067abb57e8a16c4c.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-1e20b38d5a3d0391.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-f2707e3d7ac3a5d7.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-ffa668c5204aaff0.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-8baa8dc315a5da10.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-aa91dfbfa1d19d0e.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-6d8e604ca9ec56d7.rlib" "/Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-5eb0926740e75669.rlib" "-lz" "-lssl" "-lcrypto" "-lz" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: Undefined symbols for architecture x86_64:
            "_OPENSSL_init_ssl", referenced from:
                openssl_sys::init::_$u7b$$u7b$closure$u7d$$u7d$::h50edd338b29a354d in libopenssl_sys-5e28547054fc718d.rlib(openssl_sys-5e28547054fc718d.openssl_sys.f1funnwq-cgu.8.rcgu.o)
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error

error: could not compile `openssl-macos-issue`.

To learn more, run the command again with --verbose.

Investigating the cause revealed that wrong openssl library is being linked.

Note that it passes "-L" "/usr/local/opt/openssl@1.1/lib", which is the version I want to link to. However, in front of it, there is "-L" "/usr/lib", which makes linker to look in system directory and link to the version of OpenSSL shipped with macOS (which is 0.9).

Running cargo -vv run reveals the following piece of configuration emitted by libssh2-sys build:

[libssh2-sys 0.2.16] cargo:rustc-link-search=native=/usr/lib

Which, I think, is the cause for the error above.

To confirm, I added -Xlinker -v argument to the "cc" command above and got the following listing:

BUILD 18:57:17 Dec 13 2019
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
Library search paths:
    /Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib
    /Users/idubrov/Projects/openssl-macos-issue/target/debug/deps
    /usr/lib
    /Users/idubrov/Projects/openssl-macos-issue/target/debug/build/libssh2-sys-e12fc67748fd4726/out/build
    /usr/local/opt/openssl@1.1/lib
    /Users/idubrov/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib
    /usr/lib
    /usr/local/lib

Note how /usr/lib is included twice -- once before /usr/local/opt/openssl@1.1/lib and once after. I think, the latter is added by default. However, the former one, I think, is caused by libssh2-sys build.

To summarize:

  1. libssh2-sys build adds /usr/lib as a search path for libraries.
  2. openssl-sys build adds /usr/local/opt/openssl@1.1/lib as a search path for libraries. It, however, goes after /usr/lib.
  3. openssl-sys uses includes from /usr/local/opt/openssl@1.1/include (correct), but when final binary is linked, it is linked against openssl in /usr/lib (incorrect).

I think this is an issue with libssh2-sys build (but could be caused by downstream libraries like, pkg_config).

(also, reported to openssl crate for visibility https://github.com/sfackler/rust-openssl/issues/1252)

RazerM commented 4 years ago

This also happens on macOS 10.12 Sierra

wez commented 4 years ago

This may be the same root cause as https://github.com/alexcrichton/ssh2-rs/issues/174 I've just published 0.2.17 to address that other issue; could you try with that and let me know if this is still a problem?

wez commented 4 years ago

Assuming that this is resolved.

idubrov commented 4 years ago

Oops, sorry, missed previous notification. Yes, indeed it works now! Thanks a lot!