Skip to content

Commit

Permalink
Use wasm tools in docker image (envoyproxy#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Sep 17, 2022
1 parent 0cd77e5 commit 0df5526
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,25 @@ func Check() {
mg.SerialDeps(Lint, Test)
}

func CheckBuildTools() error {
if err := sh.Run("tinygo", "version"); err != nil {
return errors.New("missing build tool tinygo (https://tinygo.org/getting-started/install/)")
}

wabtTools := []string{"wasm2wat", "wasm-opt", "wasm2wat"}
for _, t := range wabtTools {
if err := sh.Run(t, "--version"); err != nil {
return fmt.Errorf("missing build tool %q, we recommend you to install wabt (https://github.com/WebAssembly/wabt)", t)
}
}
return nil
}

// Build builds the Coraza wasm plugin.
func Build() error {
mg.Deps(CheckBuildTools)
if err := os.MkdirAll("build", 0755); err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
if err := sh.RunV("docker", "run", "--rm", "-v", fmt.Sprintf("%s:/src", wd), "ghcr.io/anuraaga/coraza-wasm-filter/buildtools-tinygo:main", "bash", "-c",
"cd /src && tinygo build -opt 2 -o build/mainraw.wasm -scheduler=none -target=wasi ."); err != nil {
return err
}
if err := sh.RunV("wasm2wat", "build/mainraw.wasm", "-o", "build/mainraw.wat"); err != nil {
return err
}
// Removes unused code, which is important since compiled unused code may import unavailable host functions
if err := sh.RunV("wasm-opt", "-Os", "-c", "build/mainraw.wasm", "-o", "build/mainopt.wasm"); err != nil {
return err
}
// Unfortuantely the imports themselves are left due to potential use with call_indirect. Hack away missing functions
// until they are stubbed in Envoy because we know we don't need them.
if err := sh.RunV("wasm2wat", "build/mainopt.wasm", "-o", "build/mainopt.wat"); err != nil {

script := `
cd /src && \
tinygo build -opt 2 -o build/mainraw.wasm -scheduler=none -target=wasi . && \
wasm-opt -Os -c build/mainraw.wasm -o build/mainopt.wasm && \
wasm2wat --enable-all build/mainopt.wasm -o build/mainopt.wat
`

if err := sh.RunV("docker", "run", "--pull", "always", "--rm", "-v", fmt.Sprintf("%s:/src", wd), "ghcr.io/anuraaga/coraza-wasm-filter/buildtools-tinygo:main", "bash", "-c",
strings.TrimSpace(script)); err != nil {
return err
}

Expand All @@ -155,7 +136,8 @@ func Build() error {
if err != nil {
return err
}
return sh.RunV("wat2wasm", "build/main.wat", "-o", "build/main.wasm")
return sh.RunV("docker", "run", "--rm", "-v", fmt.Sprintf("%s:/build", filepath.Join(wd, "build")), "ghcr.io/anuraaga/coraza-wasm-filter/buildtools-tinygo:main", "bash", "-c",
"wat2wasm --enable-all build/main.wat -o build/main.wasm")
}

func UpdateLibs() error {
Expand Down

0 comments on commit 0df5526

Please sign in to comment.