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

Add build.zig to qjs-wasi project #8

Open
wants to merge 4 commits into
base: master
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
23 changes: 23 additions & 0 deletions qjs-wasi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,31 @@ wasmtime --dir . build/qjs.wasm

## Building

### With wasienv

The following script will install [`wasienv`](https://github.com/wasienv/wasienv) and build the Wasm binary.

```bash
./build.sh
```

### With Zig

You will need nightly Zig which you can either build from source or download from the official website [here],
under "master" heading.

[here]: https://ziglang.org/download/

```bash
zig build
```

This will cross-compile the project to `wasm32-wasi` by default and build it in Debug mode. If you want to build the
project in ReleaseFast (ReleaseFast is the equivalent for Release in other build systems), add `-Drelease-fast` option
to the build invocation.

```bash
zig build -Drelease-fast
```

The runnable WASI module can be found in `zig-out/bin/qjs.wasm`.
43 changes: 43 additions & 0 deletions qjs-wasi/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) !void {
// We will target wasm32-wasi by default.
const target = b.standardTargetOptions(.{
.default_target = .{
.cpu_arch = .wasm32,
.os_tag = .wasi,
},
});
const mode = b.standardReleaseOptions();

const qjs = b.addExecutable("qjs", null);
qjs.setTarget(target);
qjs.setBuildMode(mode);
qjs.install();
qjs.linkLibC();

var flags = std.ArrayList([]const u8).init(b.allocator);
defer flags.deinit();
try flags.appendSlice(&.{
"-funsigned-char",
"-fno-sanitize=undefined", // Delete this line to enable UBSAN in non-ReleaseFast builds.
"-D_GNU_SOURCE",
"-DCONFIG_VERSION=\"2019-07-09\"",
});

if (target.getCpuArch() == .wasm32 and target.getOsTag() == .wasi) {
qjs.linkSystemLibrary("wasi-emulated-process-clocks");
qjs.linkSystemLibrary("wasi-emulated-signal");
try flags.append("-D_WASI_EMULATED_SIGNAL");
}

qjs.addCSourceFiles(&.{
"src/cutils.c",
"src/libregexp.c",
"src/libunicode.c",
"src/qjs.c",
"src/quickjs-libc.c",
"src/quickjs.c",
"src/repl.c",
}, flags.items);
}