Skip to content

Commit

Permalink
Move compilation support image to wasm-corpus (#236)
Browse files Browse the repository at this point in the history
* Move compilation support image to wasm-corpus

* fix tests

* Make scripts runnable on host or docker

* add readme

* reformat
  • Loading branch information
bhelx authored Feb 9, 2024
1 parent 323d1d4 commit 0e52356
Show file tree
Hide file tree
Showing 91 changed files with 199 additions and 359 deletions.
29 changes: 0 additions & 29 deletions compilation-support-image/Dockerfile

This file was deleted.

12 changes: 0 additions & 12 deletions compilation-support-image/compile-c.sh

This file was deleted.

10 changes: 0 additions & 10 deletions compilation-support-image/compile-js.sh

This file was deleted.

10 changes: 0 additions & 10 deletions compilation-support-image/compile-rust-wasi.sh

This file was deleted.

10 changes: 0 additions & 10 deletions compilation-support-image/compile-rust.sh

This file was deleted.

10 changes: 0 additions & 10 deletions compilation-support-image/compile-wat.sh

This file was deleted.

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<module>wasm</module>
<module>runtime</module>
<module>wasi</module>
<module>wasm-corpus</module>
</modules>

<scm>
Expand Down
6 changes: 6 additions & 0 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
</dependencies>

<build>
<testResources>
<testResource>
<directory>${project.basedir}/../wasm-corpus/src/test/resources</directory>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>com.dylibso.chicory</groupId>
Expand Down
Binary file not shown.
7 changes: 7 additions & 0 deletions wasi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
</dependency>
</dependencies>

<build>
<testResources>
<testResource>
<directory>${project.basedir}/../wasm-corpus/src/test/resources</directory>
</testResource>
</testResources>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.dylibso.chicory.runtime.HostImports;
import com.dylibso.chicory.runtime.Module;
import java.io.ByteArrayInputStream;
import java.io.File;
import org.junit.jupiter.api.Test;

public class WasiPreview1Test {
Expand All @@ -20,8 +19,7 @@ public void shouldRunWasiModule() {
var wasi =
new WasiPreview1(this.logger, WasiOptions.builder().withStdout(fakeStdout).build());
var imports = new HostImports(wasi.toHostFunctions());
var module =
Module.builder(new File("src/test/resources/compiled/hello-wasi.wat.wasm")).build();
var module = Module.builder("compiled/hello-wasi.wat.wasm").build();
module.instantiate(imports);
assertEquals(fakeStdout.output().strip(), "hello world");
}
Expand All @@ -33,8 +31,7 @@ public void shouldRunWasiRustModule() {
var stdout = new MockPrintStream();
var wasi = new WasiPreview1(this.logger, WasiOptions.builder().withStdout(stdout).build());
var imports = new HostImports(wasi.toHostFunctions());
var module =
Module.builder(new File("src/test/resources/compiled/hello-wasi.rs.wasm")).build();
var module = Module.builder("compiled/hello-wasi.rs.wasm").build();
module.instantiate(imports); // run _start and prints Hello, World!
assertEquals(expected, stdout.output().strip());
}
Expand All @@ -46,8 +43,7 @@ public void shouldRunWasiGreetRustModule() {
var wasiOpts = WasiOptions.builder().withStdout(System.out).withStdin(fakeStdin).build();
var wasi = new WasiPreview1(this.logger, wasiOpts);
var imports = new HostImports(wasi.toHostFunctions());
var module =
Module.builder(new File("src/test/resources/compiled/greet-wasi.rs.wasm")).build();
var module = Module.builder("compiled/greet-wasi.rs.wasm").build();
module.instantiate(imports);
}

Expand All @@ -60,7 +56,7 @@ public void shouldRunWasiDemoJavyModule() {
var wasiOpts = WasiOptions.builder().withStdout(fakeStdout).withStdin(fakeStdin).build();
var wasi = new WasiPreview1(this.logger, wasiOpts);
var imports = new HostImports(wasi.toHostFunctions());
var module = Module.builder("compiled/javy-demo.js.wasm").build();
var module = Module.builder("compiled/javy-demo.js.javy.wasm").build();
module.instantiate(imports);

assertEquals(fakeStdout.output(), "{\"foo\":3,\"newBar\":\"baz!\"}");
Expand Down
Binary file removed wasi/src/test/resources/compiled/greet-wasi.rs.wasm
Binary file not shown.
Binary file removed wasi/src/test/resources/compiled/hello-wasi.rs.wasm
Binary file not shown.
45 changes: 45 additions & 0 deletions wasm-corpus/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM ubuntu:jammy

# Set the environment variable to avoid prompts
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
apt-get install -y curl build-essential gcc make pkg-config libssl-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustc --version && cargo --version
RUN rustup target add wasm32-unknown-unknown
RUN rustup target add wasm32-wasi

WORKDIR /tmp

# Install wasi-sdk
RUN curl -L -O https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz && \
mkdir -p /opt/wasi-sdk && \
tar xvf wasi-sdk-20.0-linux.tar.gz && \
cp -r wasi-sdk-20.0/* /opt/wasi-sdk

ENV WASI_SDK_PATH=/opt/wasi-sdk

# Install wabt tools
RUN curl -L -O https://github.com/WebAssembly/wabt/releases/download/1.0.34/wabt-1.0.34-ubuntu.tar.gz && \
mkdir -p /opt/wabt && \
tar xvf wabt-1.0.34-ubuntu.tar.gz && \
cp -r wabt-1.0.34/* /opt/wabt

# Install Javy
RUN curl -L -O https://github.com/bytecodealliance/javy/releases/download/v1.4.0/javy-x86_64-linux-v1.4.0.gz && \
mkdir -p /opt/javy && \
gunzip javy-x86_64-linux-v1.4.0.gz && \
mv javy-x86_64-linux-v1.4.0 /usr/bin/javy && \
chmod a+x /usr/bin/javy

ENV PATH="/opt/wabt/bin:${PATH}"

WORKDIR /usr/code

ENTRYPOINT ["./compile.sh"]
34 changes: 34 additions & 0 deletions wasm-corpus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Wasm Corpus


This is a central place for all of our guest source code. It can be included in other maven projects as a test
resources bundle.

If you want to add or edit guest modules for tests, you'll need to compile here. Add the program to the subfolder
for the appropriate source language. Example: src/test/resources/rust.

You can use docker to compile everything. The run.sh script can orchestrate this for you:

```bash
cd wasm-corpus

# rebuild (or build image for first time)
./run.sh rebuild

# compile all the submodules
./run.sh

# compile just one folder
./run.sh rust
```


If you want to run on your host machine you can run the `compile.sh` subscript yourself.
It has the same arguments (assumes you have WASI_SDK_PATH set for compiling c:

```bash
cd wasm-corpus/src/test/resources

# example
./compile.sh rust
```
12 changes: 12 additions & 0 deletions wasm-corpus/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.dylibso.chicory</groupId>
<artifactId>chicory</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>wasm-corpus</artifactId>
<name>Chicory - WASM Corpus</name>
<description>A Corpus of guest modules for testing</description>
</project>
12 changes: 12 additions & 0 deletions wasm-corpus/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# run with `rebuild` to build the image
# run with no args to compile everything

if [[ "$1" = "rebuild" ]]; then
docker build . --platform linux/amd64 -t chicory/wasm-corpus
else
# Optionally takes the args `lang` (ex: wat, rust) and `file` (ex: br.wat)
# both default to all
docker run --platform linux/amd64 -v ./src/test/resources:/usr/code --rm chicory/wasm-corpus $1 $2
fi
File renamed without changes.
File renamed without changes.
70 changes: 70 additions & 0 deletions wasm-corpus/src/test/resources/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

set -e

compileWat() {
filename=$(basename "$1")
(set -x; wat2wasm $1 -o "./compiled/$filename.wasm")
}

compileRust() {
filename=$(basename "$1")
# we can use the wasi targets for any file that end in -wasi.rs
if [[ "$filename" =~ wasi.rs$ ]]; then
target="wasm32-wasi"
crate_type="bin"
else
target="wasm32-unknown-unknown"
crate_type="cdylib"
fi
(set -x; rustc $1 --target=$target --crate-type=$crate_type -o "./compiled/$filename.wasm")
}

ENV_WASI_SDK_PATH="${WASK_SDK_PATH:-/opt/wasi-sdk}"
compileC() {
filename=$(basename "$1")
(set -x; ${ENV_WASI_SDK_PATH}/bin/clang -g -o "./compiled/$filename.wasm" $1 -nostartfiles -Wl,--no-entry -Wl,--export=run)
}

compileJavy() {
filename=$(basename "$1")
(set -x; javy compile $1 -o "./compiled/$filename.javy.wasm")
}

compile() {
lang=$1
case "$lang" in
wat)
compileWat $2
;;
rust)
compileRust $2
;;
c)
compileC $2
;;
javy)
compileJavy $2
;;
*)
echo "Don't know how to compile language $lang"
exit 1
;;
esac
}

lang="${1:-all}"
path="${2:-all}"

if [[ "$lang" == "all" ]]; then
langs=("wat" "rust" "c" "javy")
else
langs=("$lang")
fi

for lang in "${langs[@]}"; do
echo "Compiling all modules in ./$lang/*"
for file in ./$lang/*; do
compile $lang $file
done
done
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ function writeOutput(output) {
// Stdout file descriptor
const fd = 1;
Javy.IO.writeSync(fd, buffer);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions wasm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
</dependencies>

<build>
<testResources>
<testResource>
<directory>${project.basedir}/../wasm-corpus/src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>com.dylibso.chicory</groupId>
Expand Down
Loading

0 comments on commit 0e52356

Please sign in to comment.