Windows11でRust使ってたら「error failed to run custom build command for `openssl sys v0.9.93`」がでたとき

にあえん

October 2, 2023

新しく使いはじめたWindows11でcargo buildしたらこんなエラーが出ました。

error: failed to run custom build command for `openssl-sys v0.9.93`

Caused by:
  process didn't exit successfully: `C:\Users\Nanao\Project\tools\sync-zaim\target\debug\build\openssl-sys-18a1f426c8f5f296\build-script-main` (exit code: 101)
  --- stdout
  cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR
  X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR
  X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR
  X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  note: vcpkg did not find openssl: Could not look up details of packages in vcpkg tree could not read status file updates dir: 指定されたパスが見つかりません。 (os error 3)

  --- 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-pc-windows-msvc
  $TARGET = x86_64-pc-windows-msvc
  openssl-sys = 0.9.93


  It looks like you're compiling for MSVC but we couldn't detect an OpenSSL
  installation. If there isn't one installed then you can try the rust-openssl
  README for more information about how to download precompiled binaries of
  OpenSSL:

  https://github.com/sfackler/rust-openssl#windows

  ', C:\Users\Nanao\.cargo\registry\src\index.crates.io-6f17d22bba15001f\openssl-sys-0.9.93\build\find_normal.rs:190:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

OpenSSLが存在しないから出てるエラーっぽい?

ということで、wingetでインストールしていきます。

まずはopenssl関連のパッケージを調べます。

> winget search openssl
名前          ID                         バージョン 一致         ソース
-----------------------------------------------------------------------
OpenSSL       ShiningLight.OpenSSL       3.1.3                   winget
OpenSSL Light ShiningLight.OpenSSL.Light 3.1.3      Tag: openssl winget

二つありますね。

公式っぽそうなShiningLight.OpenSSLをインストールしておきましょう。

winget install ShiningLight.OpenSSL

ただこのパッケージのインストール中404エラーで落ちてしまいました。。

仕方ないので軽量版をインストールしていきます。

winget install ShiningLight.OpenSSL.Light

そのあとに、OPENSSL_DIR環境変数を設定しておきましょう。

エラーメッセージにも書いてありました。

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.

「環境変数の編集」から編集していきます。

しかし、追加してもエラーが出てしまいました。

どうやら軽量版をインストールしたのがダメだったみたいで、includeディレクトリが存在しないといわれてしまいました。

error: failed to run custom build command for `openssl-sys v0.9.93`

Caused by:
  process didn't exit successfully: `C:\Users\Nanao\Project\tools\sync-zaim\target\debug\build\openssl-sys-18a1f426c8f5f296\build-script-main` (exit code: 101)
  --- stdout
  cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR
  X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR
  X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR
  X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR = C:\Program Files\OpenSSL-Win64

  --- stderr
  thread 'main' panicked at 'OpenSSL include directory does not exist: C:\Program Files\OpenSSL-Win64\include', C:\Users\Nanao\.cargo\registry\src\index.crates.io-6f17d22bba15001f\openssl-sys-0.9.93\build/main.rs:79:9
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

困ったなぁと思ってもう一回wingetで軽量版じゃないほうをインストールしてみたら、なんか成功しました。

> winget install OpenSSL
既存のパッケージが既にインストールされています。インストールされているパッケージ...をアップグレードしようとしています
見つかりました OpenSSL [ShiningLight.OpenSSL] バージョン 3.1.3
このアプリケーションは所有者からライセンス供与されます。
Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
ダウンロード中 https://slproweb.com/download/Win64OpenSSL-3_1_3.msi
  ██████████████████████████████   151 MB /  151 MB
インストーラーハッシュが正常に検証されました
パッケージのインストールを開始しています...
インストールが完了しました

元あるパッケージをアップグレードするという体ならいいってことなんですかね。

ちゃんとincludeディレクトリも用意されています!

この状態で再度cargo buildしたら通りました。

めでたしめでたし。

参考