From e7345e0dc9113c37a24b2a9d3710e70d1ee7e2c0 Mon Sep 17 00:00:00 2001 From: Jakub Konka <kubkon@jakubkonka.com> Date: Mon, 7 Jun 2021 08:51:22 +0200 Subject: [PATCH 1/4] Add build.zig to qjs-wasi project This change integrates the project with Zig as a replacement build system to CMake. With this change, you can use Zig to easily cross-compile qjs to WASI: ``` zig build -Dtarget=wasm32-wasi -Drelease-fast ``` Note that you need latest nightly Zig so that you have access to emulated WASI libc subcomponents such as process clocks and signals. --- qjs-wasi/build.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 qjs-wasi/build.zig diff --git a/qjs-wasi/build.zig b/qjs-wasi/build.zig new file mode 100644 index 0000000..6a94a2f --- /dev/null +++ b/qjs-wasi/build.zig @@ -0,0 +1,36 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) !void { + const target = b.standardTargetOptions(.{}); + 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", + "-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); +} From ab12a2e3c3ee2b0e45cac4692939e0053dc7a966 Mon Sep 17 00:00:00 2001 From: Jakub Konka <kubkon@jakubkonka.com> Date: Tue, 8 Jun 2021 06:27:15 +0200 Subject: [PATCH 2/4] Target wasm32-wasi by default unless overriden by user --- qjs-wasi/build.zig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/qjs-wasi/build.zig b/qjs-wasi/build.zig index 6a94a2f..29a66a2 100644 --- a/qjs-wasi/build.zig +++ b/qjs-wasi/build.zig @@ -1,7 +1,13 @@ const std = @import("std"); pub fn build(b: *std.build.Builder) !void { - const target = b.standardTargetOptions(.{}); + // 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); From 5941cfd09c9f7a978cb50a2a87ddea77c391626f Mon Sep 17 00:00:00 2001 From: Jakub Konka <kubkon@jakubkonka.com> Date: Tue, 8 Jun 2021 07:37:18 +0200 Subject: [PATCH 3/4] Turn off UBSAN to replicate default project behavior --- qjs-wasi/build.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/qjs-wasi/build.zig b/qjs-wasi/build.zig index 29a66a2..6b88010 100644 --- a/qjs-wasi/build.zig +++ b/qjs-wasi/build.zig @@ -20,6 +20,7 @@ pub fn build(b: *std.build.Builder) !void { 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\"", }); From 78eca7fc2335920d2b5c72e383cc85260b07ec3f Mon Sep 17 00:00:00 2001 From: Jakub Konka <kubkon@jakubkonka.com> Date: Tue, 8 Jun 2021 07:47:44 +0200 Subject: [PATCH 4/4] Update README with build instructions for Zig --- qjs-wasi/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/qjs-wasi/README.md b/qjs-wasi/README.md index 542be23..693086f 100644 --- a/qjs-wasi/README.md +++ b/qjs-wasi/README.md @@ -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`.