开发者问题收集

如何使用 rust 在 Mac OS 上交叉编译 openssL 库?

2021-05-21
4377

我在 MacOS 上编译 Linux 二进制文件时遇到了这个问题: 看起来 rust 库“openssl-sys v0.9.61”与 MacOS 的 C 库绑定在一起。无法链接到 linux。

cargo build --release --target=x86_64-unknown-linux-musl
   Compiling openssl-sys v0.9.61
error: failed to run custom build command for `openssl-sys v0.9.61`

Caused by:
  process didn't exit successfully: `~/test/../bin/release/build/openssl-sys-96148dcd52905249/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\n                Install a sysroot for the target platform and configure it via\n                PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\n                cross-compiling wrapper for pkg-config and set it via\n                PKG_CONFIG environment variable."

  --- stderr
  thread 'main' panicked at '

  Could not find directory of OpenSSL installation, and this `-sys` crate cannot
  proceed without this knowledge. If OpenSSL is installed and this crate had
  trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
  compilation process.

  Make sure you also have the development packages of openssl installed.
  For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

  If you're in a situation where you think the directory *should* be found
  automatically, please open a bug at https://github.com/sfackler/rust-openssl
  and include information about your system as well as this message.

  $HOST = x86_64-apple-darwin
  $TARGET = x86_64-unknown-linux-musl
  openssl-sys = 0.9.61

  ', ~/.cargo/registry/src/openssl-sys-0.9.61/build/find_normal.rs:174:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

我发现有些人也遇到了这个问题: https://www.andrew-thorburn.com/cross-compiling-a-simple-rust-web-app/

2个回答

第一步是确定哪个依赖项正在使用 openssl,您可以使用 cargo tree 来完成:

cargo tree --target=x86_64-unknown-linux-musl -i openssl-sys

就我而言,这将 rusoto_core 包标识为依赖于它的包。某些包可以使用功能标志进行编译以使用备用 TLS 库,这使得它们比 OpenSSL 更易于使用。

例如,Rusoto 在 Cargo.toml 中像这样启用 rustls

rusoto_core = {version = "0.42.0", default_features = false, features=["rustls"]}
rusoto_s3 = {version = "0.42.0", default_features = false, features=["rustls"]}
rusoto_sqs = {version = "0.42.0", default_features = false, features=["rustls"]}

在 Rusoto 案例中,有用于与 AWS Lambda 一起使用的交叉编译文档: https://rusoto.org/lambdas.html

或者,您可以向编译器提供交叉编译的 OpenSSL,但据我所知,这在目前更加困难且文档记录不全。

Jess Bowers
2021-10-16

确实,您需要在交叉编译链接阶段确定哪些 rust 依赖项使用 openssl。在我的项目中,reqwest 和 mysql 依赖项显然在交叉编译时使用它们。当我在我的 mac m1 上从我的 mac 交叉编译到 x86_64 或 aarch64 (arm) linux 目标时,我遇到了类似的问题。链接到 openssl 时链接器失败。诀窍是将 openssl crate 添加到您的项目中。这里有很好的信息: https://docs.rs/openssl/latest/openssl/

这里还有其他有用的链接的步骤:

1.rustup add target  (this is AMD64 linux)
rustup target add x86_64-unknown-linux-musl

for (arm7 linux)
rustup target add aarch64-unknown-linux-musl



1(a) add the xplatform linker
brew install FiloSottile/musl-cross/musl-cross

for x86 and arm7 support:
brew install FiloSottile/musl-cross/musl-cross --with-aarch64 --with-x86_64

1(b) add openssl crate
https://docs.rs/openssl/latest/openssl/

may need to install openssl (see directions)
brew install [email protected]

[dependencies]
openssl = { version = "0.10", features = ["vendored"] }

2. modify ~/.cargo/config.toml

add link targets
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"

[target.x86_64-unknown-linux-gnu]  - notneeded
linker = "x86_64-unknown-linux-gnu-gcc"

3. export TARGET_CC

x86_64:
export TARGET_CC=x86_64-linux-musl-gcc

for arm7:
export TARGET_CC=aarch64-linux-musl-gcc

4. compiling openssl
include the openssl crate:
openssl = { version = "0.10", features = ["vendored"] }

5. build for target
cargo build --target x86_64-unknown-linux-musl

cargo build --target aarch64-unknown-linux-musl
chris mollis
2022-11-19