Skip to content

Commit

Permalink
Add makefile to wasm recipe in the book
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanrn committed Dec 1, 2023
1 parent 309e702 commit b01f51b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
- [Retrofitting sandboxing in a simple application](./chapters/noop-sandbox.md)
- [Enforcing isolation with the `wasm2c` sandbox](./chapters/wasm-sandbox.md)
- [Installing and running tutorial examples](./chapters/tutorial-install.md)
- [Additional material](chapters/appendix.md)
- [Additional resources](./chapters/additional.md)
- [Modifying a project that uses Make to compile to Wasm](./chapters/makefile-wasm.md)
- [More tutorials](chapters/appendix.md)
4 changes: 4 additions & 0 deletions src/additional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Additional content

This section contains additional resources such as useful recipes, tutorials
which may have additional content (but may be slightly out of date) etc.
26 changes: 26 additions & 0 deletions src/chapters/makefile-wasm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Modifying a Makefile based project to compile to Wasm

This tutorial assumes you have the
[rlbox_wasm2c_sandbox](https://github.com/PLSysSec/rlbox_wasm2c_sandbox/) git
repo is in the path $(RLBOX_WASM2C_PATH), and you have installed
[wasi-sdk](https://github.com/WebAssembly/wasi-sdk/releases) on your computer in
the path $(WASI_SDK_PATH)

Build the sources of your library along with the file
`$(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.c`. Pass the flags `-Wl,--export-all -Wl,--stack-first -Wl,-z,stack-size=262144 -Wl,--no-entry -Wl,--growable-table -Wl,--import-memory -Wl,--import-table`
to the linker using the wasi-clang compiler. This will produce a wasm module.

To edit an existing Make based build system, you can run the commmand.

```bash
$(WASI_SDK_PATH)/bin/clang --sysroot $(WASI_SDK_PATH)/share/wasi-sysroot $(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.c -c -o $(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.o

AR=$(WASI_SDK_PATH)/bin/ar \
CC=$(WASI_SDK_PATH)/bin/clang \
CXX=$(WASI_SDK_PATH)/bin/clang++ \
CFLAGS="--sysroot $(WASI_SDK_PATH)/share/wasi-sysroot" \
LD=$(WASI_SDK_PATH)/bin/wasm-ld \
LDLIBS=$(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.o \
LDFLAGS="-Wl,--export-all -Wl,--stack-first -Wl,-z,stack-size=262144 -Wl,--no-entry -Wl,--growable-table -Wl,--import-memory -Wl,--import-table" \
make
```

0 comments on commit b01f51b

Please sign in to comment.