Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify WAMR doc #23

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

- [Nginx](./chap02/apps/nginx.md)

- [Wamr](./chap02/apps/wamr.md)
- [WAMR](./chap02/apps/wamr.md)

- [Perl](./chap02/apps/perl.md)

Expand Down
18 changes: 12 additions & 6 deletions src/chap02/apps/wamr.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RuxOS 支持在 Qemu 上通过wasm运行时 [WAMR](https://github.com/bytecodeal

## WAMR简介

WAMR是一个轻量级的wasm运行时,支持在嵌入式设备上运行wasm应用。RuxOS提供了Hello World和2048小游戏的wasm应用作为示例,同时支持WASI-NN,具有运行神经网络模型的能力。
WAMR是一个轻量级的wasm运行时,支持在嵌入式设备上运行wasm应用,现在归属于字节码联盟,由社区维护。RuxOS提供了Hello World和2048小游戏的wasm应用作为示例,同时支持WASI-NN,具有运行神经网络模型的能力。

将[rux-wamr](https://github.com/syswonder/rux-wamr)克隆到RuxOS项目的apps/c/wamr目录下,有如下结构:

Expand All @@ -18,14 +18,16 @@ WAMR是一个轻量级的wasm运行时,支持在嵌入式设备上运行wasm
├── wamr.patch
```

`rootfs/`目录下的`main.wasm`和其他wasm文件是通过WASM编译器从`.c`文件编译而来的。`rootfs/`是一个最小的RuxOS根文件系统,使用9pfs供 RuxOS 使用。

## 编译WAMR并运行示例

WAMR的编译依赖于cmake,所以在编译WAMR之前需要安装cmake。

在RuxOS根目录运行下面的命令,会启动hello world的wasm应用。

```shell
make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y NET=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,/main.wasm" run
make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,/main.wasm" run
```

参数解释:
Expand All @@ -40,8 +42,6 @@ make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y NET=y V9P=y V9P_PATH=apps/

* `MUSL`: 该参数表示使用musl libc作为编译时的c库。

* `NET`: 该参数用于使能 qemu 的 virtio-net。

* `V9P`: 该参数用于使能 qemu 的 virtio-9p。

* `V9P_PATH`: `V9P_PATH` 指向 host 上的用于共享的目录,这里使用rux-wamr的rootfs目录,其中包含了wasm应用的wasm文件。
Expand Down Expand Up @@ -77,7 +77,7 @@ $WASI_SDK_DIR/bin/clang -O3 -o main.wasm main.c
如果需要在WAMR中使用NN(神经网络)支持,需要运行带`WASI_NN=1`参数的`make`命令:

```shell
make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y NET=y V9P=y V9P_PATH=apps/c/wamr/rootfs WASI_NN=1 ARGS="iwasm,/main.wasm" run
make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y V9P=y V9P_PATH=apps/c/wamr/rootfs WASI_NN=1 ARGS="iwasm,/main.wasm" run
```

例如,如果你想自己编译支持神经网络的测试用例,可以在`apps/c/wamr/wasm-micro-runtime-{version}/core/iwasm/libraries/wasi-nn/test/`目录中使用如下命令:
Expand All @@ -93,6 +93,8 @@ make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y NET=y V9P=y V9P_PATH=apps/
test_tensorflow.c utils.c
```

如果你想将`c++`文件编译成`wasm`文件,你需要在上述命令中加上`-lc++`和`-lc++abi`参数。

然后复制`test_tensorflow.wasm`到`apps/c/wamr/rootfs`目录下即可:

```bash
Expand All @@ -108,5 +110,9 @@ cp test_tensorflow.wasm ../../../../../../rootfs/
如果你想在Rust中构建支持wasi_nn的wasm,需要在make命令中添加`WAMR_BUILD_WASI_EPHEMERAL_NN=1`参数。因为Rust中wasi_nn的模块名是`wasi_ephemeral_nn`,而不是`wasi_nn`:

```bash
make A=apps/c/wamr ARCH=aarch64 LOG=info run MUSL=y NET=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,--env="TARGET=cpu",--dir=.,/built_from_rust.wasm" WASI_NN=1 WAMR_BUILD_WASI_EPHEMERAL_NN=1
make A=apps/c/wamr ARCH=aarch64 LOG=info run MUSL=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,--env="TARGET=cpu",--dir=.,/built_from_rust.wasm" WASI_NN=1 WAMR_BUILD_WASI_EPHEMERAL_NN=1
```

# 更多

你也可以使用这个应用在ruxos上运行其他wasm文件。只需要编译`.wasm`文件并将其放入`rootfs/`目录中。然后使用上面的命令运行它,只需更改`ARGS`参数,就可以在ruxos中享受wasm应用。
18 changes: 12 additions & 6 deletions translations/en/chap02/apps/wamr.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RuxOS supports running wasm applications on Qemu through wasm runtime [WAMR](htt

## 1. Introduction to WAMR

WAMR is a lightweight wasm runtime that supports running wasm applications on embedded devices. RuxOS provides wasm applications of Hello World and 2048 game as examples. And it also supports WASI-NN, which has the ability to run neural network models.
WAMR is a lightweight wasm runtime that supports running wasm applications on embedded devices, now owned by the Bytecode Alliance and maintained by the community. RuxOS provides wasm applications of Hello World and 2048 game as examples. And it also supports WASI-NN, which has the ability to run neural network models.

Clone [rux-wamr](https://github.com/syswonder/rux-wamr) to the apps/c/wamr directory of the RuxOS project, with the following structure:

Expand All @@ -18,14 +18,16 @@ Clone [rux-wamr](https://github.com/syswonder/rux-wamr) to the apps/c/wamr direc
├── wamr.patch
```

The `main.wasm` and other wasm files under the `rootfs/` directory are compiled from `.c` files by the WASM compiler. `rootfs/` is a minimal RuxOS root file system, using 9pfs for RuxOS.

## 2. Compile WAMR and run the example

The compilation of `WAMR` depends on `cmake`.

Run the following command in the RuxOS root directory, which will start the hello world wasm application.

```shell
make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y NET=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,/main.wasm" run
make A=apps/c/wamr ARCH=aarch64 LOG=info SMP=4 MUSL=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,/main.wasm" run
```

Parameter explanation:
Expand All @@ -40,8 +42,6 @@ Parameter explanation:

* `MUSL`: This parameter indicates that musl libc is used as the c library at compile time.

* `NET`: This parameter is used to enable qemu's virtio-net.

* `V9P`: This parameter is used to enable qemu's virtio-9p.

* `V9P_PATH`: `V9P_PATH` points to the directory on the host used for sharing. Here, the rootfs directory of rux-wamr is used, which contains the wasm file of the wasm application.
Expand Down Expand Up @@ -77,7 +77,7 @@ Or you can put the `*.wasm` file from somewhere else into the rootfs.
If you want to run WAMR with NN (Neural Network) support, you need to run `make` command with `WASI_NN=1`:

```bash
make A=apps/c/wamr ARCH=aarch64 LOG=info run MUSL=y NET=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,--env="TARGET=cpu",--dir=.,/test_tensorflow.wasm" WASI_NN=1
make A=apps/c/wamr ARCH=aarch64 LOG=info run MUSL=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,--env="TARGET=cpu",--dir=.,/test_tensorflow.wasm" WASI_NN=1
```

For example, if you want to compile the demo with NN support by yourself, you can run the following command in `apps/c/wamr/wasm-micro-runtime-{version}/core/iwasm/libraries/wasi-nn/test/` directory:
Expand All @@ -93,6 +93,8 @@ For example, if you want to compile the demo with NN support by yourself, you ca
test_tensorflow.c utils.c
```

And if you want to compile **c++** file to wasm, you may need to link c++ library by adding `-lc++` and `-lc++abi` in the command above.

And copy the `test_tensorflow.wasm` to the `apps/c/wamr/rootfs/` directory:

```bash
Expand All @@ -108,5 +110,9 @@ Then run the `make` command above to enjoy the NN support in ruxos.
If you want to run WASM built from rust with wasi_nn support, you will need to add `WAMR_BUILD_WASI_EPHEMERAL_NN=1` argument in the make command. Because the module name of wasi_nn in rust is `wasi_ephemeral_nn`, instead of `wasi_nn`:

```bash
make A=apps/c/wamr ARCH=aarch64 LOG=info run MUSL=y NET=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,--env="TARGET=cpu",--dir=.,/built_from_rust.wasm" WASI_NN=1 WAMR_BUILD_WASI_EPHEMERAL_NN=1
make A=apps/c/wamr ARCH=aarch64 LOG=info run MUSL=y V9P=y V9P_PATH=apps/c/wamr/rootfs ARGS="iwasm,--env="TARGET=cpu",--dir=.,/built_from_rust.wasm" WASI_NN=1 WAMR_BUILD_WASI_EPHEMERAL_NN=1
```

# Further

You can also run other wasm files in ruxos using this application. Just compile the `.wasm` file and put it into the `rootfs/` directory. Then run it using the command above, only change the `ARGS` parameter, and you can enjoy the wasm application in ruxos.