Raspberry Pi(reTerminal)でRustのggezのcargo buildエラーになった

Raspberry PiでRustのゲームエンジンggezを使ってビルドしようとしたところエラーが出て、解決したので記録。

:::note warn
(2022/02/19追記) Raspberry Piで使われている OpenGL ES 2 はggezサポートされていないようでしたので、この手順をやっても実行できません。
以下にissueがあります。
[OpenGL ES 2 support · Issue #808 · ggez/ggez](https://github.com/ggez/ggez/issues/808)
:::

# 環境

タイトルにRaspberry Piと書いたが正確にはreTerminal。購入時に入っていたバージョンなので、通常のRaspberry Pi OSとは異なるかもしれない。

```terminal
$ uname -a
Linux reterminal 5.10.52-v7l+ #1441 SMP Tue Aug 3 18:11:56 BST 2021 armv7l GNU/Linux
```

# ビルドエラー1: alsa関連

```terminal
$ cargo build
error: failed to run custom build command for `alsa-sys v0.3.1`

Caused by:
  process didn't exit successfully: `/home/pi/ggez_test/target/debug/build/alsa-sys-3fe4e438fc56a3bf/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=ALSA_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_armv7_unknown_linux_gnueabihf
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=ALSA_STATIC
  cargo:rerun-if-env-changed=ALSA_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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_armv7_unknown_linux_gnueabihf
  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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_armv7_unknown_linux_gnueabihf
  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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_armv7_unknown_linux_gnueabihf
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR

  --- stderr
  thread 'main' panicked at '`"pkg-config" "--libs" "--cflags" "alsa"` did not exit successfully: exit status: 1
  error: could not find system library 'alsa' required by the 'alsa-sys' crate

  --- stderr
  Package alsa was not found in the pkg-config search path.
  Perhaps you should add the directory containing `alsa.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'alsa' found
  ', /home/pi/.cargo/registry/src/github.com-1285ae84e5963aae/alsa-sys-0.3.1/build.rs:13:18
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
```

以下のissueで解決策が書いてあった。
[error: failed to run custom build command for libudev-sys v0.1.4 #119](https://github.com/ggez/ggez/issues/100)

libasound2-devをインストール

```terminal
$ sudo apt install libasound2-dev
```

上記を実行したところ処理は進んだように見えたが、また別のエラーが出た。

# ビルドエラー2: libudev関連


```terminal
$ cargo build
   Compiling alsa-sys v0.3.1
   Compiling libudev-sys v0.1.4
   Compiling unicase v2.6.0
   Compiling error-chain v0.12.4
error: failed to run custom build command for `libudev-sys v0.1.4`

Caused by:
  process didn't exit successfully: `/home/pi/ggez_test/target/debug/build/libudev-sys-561f2bcc47a91ca7/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=LIBUDEV_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_armv7_unknown_linux_gnueabihf
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=LIBUDEV_STATIC
  cargo:rerun-if-env-changed=LIBUDEV_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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_armv7_unknown_linux_gnueabihf
  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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_armv7_unknown_linux_gnueabihf
  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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_armv7_unknown_linux_gnueabihf
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR

  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "`\"pkg-config\" \"--libs\" \"--cflags\" \"libudev\"` did not exit successfully: exit status: 1\nerror: could not find system library 'libudev' required by the 'libudev-sys' crate\n\n--- stderr\nPackage libudev was not found in the pkg-config search path.\nPerhaps you should add the directory containing `libudev.pc'\nto the PKG_CONFIG_PATH environment variable\nNo package 'libudev' found\n"', /home/pi/.cargo/registry/src/github.com-1285ae84e5963aae/libudev-sys-0.1.4/build.rs:38:41
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
```

調べたら下記を発見。
[ggez crate won't build; need help with dependency errors](https://www.reddit.com/r/rust_gamedev/comments/h99i4w/ggez_crate_wont_build_need_help_with_dependency/)

libudev-devをインストール

```terminal
$ sudo apt install libudev-dev
```

これで私は解決できました。

```terminal
(略)
    Finished dev [unoptimized + debuginfo] target(s) in 8m 13s
```

# 公式ドキュメントに書いてあった

公式ドキュメントはよく読みましょう。

[ggez/docs/BuildingForEveryPlatform.md](https://github.com/ggez/ggez/blob/master/docs/BuildingForEveryPlatform.md)
より

> ## Debian
> Very easy, just install the required dev packages:
>
> ```sh
> apt install libasound2-dev libudev-dev pkg-config
> ```
> 
> Then you should be able to build with `cargo run`