LoginSignup
4
0

More than 3 years have passed since last update.

Apple Silicon を使った Mac に wasm-pack をインストールするには

Last updated at Posted at 2020-11-22

注意:

TL;DR

  • arch コマンドで x86_64 を指定して、インストールスクリプトを実行します
  • インストール後は、Intel Mac と同様に開発を行えます

インストールスクリプトが動きません

Web サイト には、次のようにインストールスクリプトを実行してインストールするように案内されています。

$ curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

そのまま実行すると、arm64 向けのバイナリは存在しないというエラーメッセージが出力され、インストールが進みません。

wasm-pack-init: no precompiled binaries available for CPU architecture: arm64

インストールスクリプトの 46 行目付近に次の記述があります。ここではインストールスクリプトの実行されたハードウェアのタイプから、インストールするバイナリを選んでいます。

    46      local _tardir="wasm-pack-v0.9.1-${_arch}"
    47      local _url="$UPDATE_ROOT/${_tardir}.tar.gz"

$UPDATE_ROOT には https://github.com/rustwasm/wasm-pack/releases/download/v0.9.1 が設定されていること、また 56 行目や 172 行目付近の内容から、このスクリプトは wasm-pack のリリースページからバイナリをダウンロードすることがわかります。

Rosetta2 のおかげで Apple Silicon では x86_64 のバイナリが実行できます。wasm-pack も x86_64 版をダウンロードしてインストールすればよさそうです。念のため、GitHub のリリースページ にアクセスし、v0.9.1 に x86_64 向けのバイナリがあることを確認します。

インストール方法

前節で、x86_64 のバイナリをインストールすればよさそうであることがわかりました。

インストールスクリプトはシェルスクリプトで記述されています。arch コマンドを使い、 x86_64 上で動いていることとして、このスクリプトを起動します。

$ curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | arch --x86_64 sh

しばらく待てばインストールが終わります。

動作確認

hello_wasm プロジェクトを作り、ビルドして、Wasm ファイルができていることを確認します。

$ wasm-pack new hello_wasm
[INFO]: ⬇️  Installing cargo-generate...
🐑  Generating a new rustwasm project with name 'hello_wasm'...
⚠️   Renaming project called `hello_wasm` to `hello-wasm`...
🔧   Creating project called `hello-wasm`...
✨   Done! New project created /Users/username/hello-wasm
[INFO]: 🐑 Generated new project at /hello_wasm
$ cd hello_wasm
$ wasm-pack build
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
# 中略
[INFO]: ⬇️  Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: ✨   Done in 9.54s
[INFO]: 📦   Your wasm pkg is ready to publish at /Users/username/hello-wasm/pkg.

以上でビルドが終わり、pkg フォルダに Wasm ファイルが作られていれば、動作確認終了です。

$ ls pkg
README.md       hello_wasm_bg.js    package.json
hello_wasm.d.ts     hello_wasm_bg.wasm
hello_wasm.js       hello_wasm_bg.wasm.d.ts
4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0