其他分享
首页 > 其他分享> > mac m1上使用rust编译openssl-sys失败的一次记录

mac m1上使用rust编译openssl-sys失败的一次记录

作者:互联网

mac m1 编译openssl-sys的时候报错:

遇到错误,报错信息如下

admin@admindeMacBook-Air b3 % cargo run
   Compiling openssl-sys v0.9.72
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/Users/admin/project/b3/target/debug/build/openssl-sys-7ab7e6047ef6ec59/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_DIR
  AARCH64_APPLE_DARWIN_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_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=OPENSSL_STATIC
  cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  run pkg_config fail: "Could not run `\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"`\nThe pkg-config command could not be found.\n\nMost likely, you need to install a pkg-config package for your OS.\nTry `brew install pkg-config` if you have Homebrew.\n\nIf you've already installed it, ensure the pkg-config command is one of the\ndirectories in the PATH environment variable.\n\nIf you did not expect this build to link to a pre-installed system library,\nthen check documentation of the openssl-sys crate for an option to\nbuild the library from source, or disable features or dependencies\nthat require pkg-config."

  --- 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 = aarch64-apple-darwin
  $TARGET = aarch64-apple-darwin
  openssl-sys = 0.9.72

  ', /Users/admin/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.72/build/find_normal.rs:180:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

同样的代码在ubuntu上正常,但是在mac m1上面无法编译,根据报错信息所以猜测是openssl-sys这个库需要调用本机上的openssl,但是无法找到。
找到了一篇文档,
https://reality0ne.com/macos-xia-shi-yong-rust-openssl/

执行文章中的命令:

$ cargo clean
$ export OPENSSL_LIB_DIR=$(brew --prefix openssl)/lib
$ export OPENSSL_INCLUDE_DIR=$(brew --prefix openssl)/include
$ cargo build
$ cargo test

然后到项目里面cargo run一下,仍然报错

admin@admindeMacBook-Air b3 % cargo run
   Compiling openssl-sys v0.9.72
   Compiling socket2 v0.4.3
   Compiling memmap2 v0.5.3
   Compiling rand v0.4.6
   Compiling memmap v0.7.0
   Compiling quote v1.0.15
   Compiling twoway v0.1.8
   Compiling buf_redux v0.8.4
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/Users/admin/project/b3/target/debug/build/openssl-sys-7ab7e6047ef6ec59/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR = /opt/homebrew/opt/openssl@3/lib
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR = /opt/homebrew/opt/openssl@3/include

  --- stderr
  thread 'main' panicked at 'OpenSSL library directory does not exist: /opt/homebrew/opt/openssl@3/lib', /Users/admin/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.72/build/main.rs:68:9
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

这个提示大概看懂了,意思是openssl的库在/opt/homebrew/opt/openssl@3/lib下不存在,cd到这个目录之后发现确实不存在

admin@admindeMacBook-Air opt % ls
pkg-config      pkgconfig

所以打算用homebrew重新安装一下,这里使用了这个作者的方法
https://zhuanlan.zhihu.com/p/111014448 (homebrew国内镜像安装)
然后执行brew install openssl

执行过程:

admin@admindeMacBook-Air opt % brew install openssl
Error: 
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.
To `brew update`, first run:
  git -C /opt/homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
  git -C /opt/homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
These commands may take a few minutes to run due to the large size of the repositories.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/ca-certificates-2022-03-29.all.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/openssl%403-3.0.2.arm64_big_sur.bottle.tar.gz
######################################################################## 100.0%
==> Installing dependencies for openssl@3: ca-certificates
==> Installing openssl@3 dependency: ca-certificates
==> Pouring ca-certificates-2022-03-29.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...

标签:opt,cargo,changed,openssl,sys,mac,env,homebrew
来源: https://www.cnblogs.com/ipdaxia/p/16191982.html