【Rust/WebAssembly】Rustおじさん「HellowWorldまでした10手順(for Mac)」
2017/6/13日現在の記事です。
これはRust初心者の自分がやった手順(殴り書き記事)です。
以下参照記事
https://kripken.github.io/emscripten-site/docs/getting_started/index.html
WebAssemblyとは
https://medium.com/javascript-scene/what-is-webassembly-the-dawn-of-a-new-era-61256ec5a8f6
今日のゴール
Rustで書いたコードをコンパイル。ブラウザでHelloWorldを出力するまで
環境
Mac 10.12.4 (Sierra)
パスが/usr/local/bin/zshになっている(環境に依ります)
1
curl https://sh.rustup.rs -sSf | sh
((Rust 1.14.0 以降)スクリプトをダウンロードしインストールを始めます。
2
brew install rust
3
rustc -V
バージョン確認。バージョンとコミットハッシュ、日時が返ってくる
rustc 1.14.0 (e8a012324 2016-12-16)
cargo -V
バージョン確認。2のinstallで安定版のcargoがインストールされているはず
cargoとは??
Cargoは3つのものを管理します。コードのビルド、コードが依存するライブラリのダウンロード(cargo build時)、そしてそれらのライブラリのビルド。
参照
プロジェクトを生成する場合
(以下はcargoを理解するためのコマンドですので4へ。)
$ cargo new hello_world --bin
--binを渡すことでコードをビルド。
なければライブラリをビルド。
先ほどのプロジェクトに移動してどのようなディレクトリ構成になっているか調べる
$ cd hello_world $ tree . . ├── Cargo.toml (package.jsonやpom.xmlみたいなもの) └── src └── main.rs
4
rustup target add wasm32-unknown-emscripten
(RustのコードからWebAssemblyを生成するために必要)
emscriptenとは
https://github.com/kripken/emscripten
LLVMとは
https://stackoverflow.com/questions/2354725/what-exactly-is-llvm
SDKダウンロード
5
https://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz
6
brew install CMake
7
./emsdk install sdk-incoming-64bit
(Macの人は./emsdkになります。winは./なし)
8
./emsdk activate sdk-incoming-64bit
./emsdk install git-1.8.3 clang-incoming-64bit node-0.10.17-64bit python-2.7.5.3-64bit java-7.45-64bit emscripten-incoming
9
source ./emsdk_env.sh
更新
cd /emsdk-portable
ここの直下でhello.rs内にRustのコードを書く。
fn main(){ println!("Hello, WebAssembly!!"); }
↑適当なコード
10
コンパイル
rustc --target=wasm32-unknown-emscripten hello.rs -o hello.js
とか
rustc --target=wasm32-unknown-emscripten hello.rs -o hello.html
今回のGitHub
https://github.com/kenmori/RustPlayground
無視ファイル多いでいいですよね??
上記作業中下のエラーが出たら。
・【Rust/WebAssembly】これが出たら。Could not run CMake, perhaps it has not been installed?