Skip to content

Commit 29153b1

Browse files
committed
Update to take advantage of wasm-zig pkg and gyro
There's a lot of rough edges to work out such as how to alter/augment structs defined in wasm-zig with runtime specific functionality. For now, wrap the wasm.* structs in their own containers in wasmtime-zig.
1 parent 9a9d7c1 commit 29153b1

22 files changed

+360
-841
lines changed

.github/workflows/ci.yml

+7-44
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,13 @@ jobs:
2424
git config --global core.autocrlf false
2525
- name: Checkout
2626
uses: actions/checkout@v1
27-
- name: Install zig (ubuntu)
27+
- name: Test linux
2828
if: matrix.os == 'ubuntu'
29-
run: |
30-
sudo snap install zig --classic --beta
31-
- name: Install zig (macOS)
32-
if: matrix.os == 'macos'
33-
run: |
34-
brew install zig
35-
- name: Install zig (windows)
36-
if: matrix.os == 'windows'
37-
run: |
38-
choco install zig
39-
- name: Install libwasmtime (ubuntu)
40-
if: matrix.os == 'ubuntu'
41-
run: |
42-
curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v0.24.0/wasmtime-v0.24.0-x86_64-linux-c-api.tar.xz -o wasmtime.tar.xz
43-
tar -C . --strip-components 2 --wildcards -xvf wasmtime.tar.xz */lib/libwasmtime.a
44-
- name: Install libwasmtime (macOS)
29+
run: ./ci/linux_ci
30+
- name: Test macos
4531
if: matrix.os == 'macos'
46-
run: |
47-
curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v0.24.0/wasmtime-v0.24.0-x86_64-macos-c-api.tar.xz -o wasmtime.tar.xz
48-
tar -C . --strip-components 2 -xvf wasmtime.tar.xz */lib/libwasmtime.a
49-
- name: Install libwasmtime (windows)
32+
run: ./ci/macos_ci
33+
- name: Test windows
34+
shell: bash
5035
if: matrix.os == 'windows'
51-
run: |
52-
curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v0.24.0/wasmtime-v0.24.0-x86_64-windows-c-api.zip -o wasmtime.zip
53-
7z e -i!"*/lib/wasmtime.dll.lib" wasmtime.zip
54-
7z e -i!"*/lib/wasmtime.dll" wasmtime.zip
55-
- name: zig build
56-
run: |
57-
zig build
58-
- name: zig build test
59-
run: |
60-
zig build test -Dlibrary-search-path="."
61-
- name: zig build run -Dexample=simple
62-
run: |
63-
zig build run -Dexample=simple -Dlibrary-search-path="."
64-
- name: zig build run -Dexample=gcd
65-
run: |
66-
zig build run -Dexample=gcd -Dlibrary-search-path="."
67-
- name: zig build run -Dexample=linking
68-
run: |
69-
zig build run -Dexample=linking -Dlibrary-search-path="."
70-
- name: zig build run -Dexample=memory
71-
run: |
72-
zig build run -Dexample=memory -Dlibrary-search-path="."
73-
36+
run: ./ci/win_ci

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
zig-cache
22
.DS_Store
33
*.swp
4+
.gyro
5+
gyro.lock

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ but expected, and things might just not work as expected yet.
1313

1414
## Building
1515

16+
To build this library, you will need Zig nightly 0.8.0, as well as [`gyro`] package manager.
17+
18+
[`gyro`]: https://github.com/mattnite/gyro
19+
1620
This library consumes the C API of the Wasmtime project which you can download with every release of
1721
Wasmtime. It relies on version `v0.24.0` of Wasmtime and you need it to build tests and examples.
1822
You can download the library from [here].
@@ -21,7 +25,7 @@ After you unpack it, if you installed the lib in path that is not your system se
2125
you can add the installed path to the build command using the following flag
2226

2327
```
24-
zig build -Dlibrary-search-path=<path-to-libwasmtime>
28+
gyro build --search-prefix=<path-to-libwasmtime>
2529
```
2630

2731
[here]: https://github.com/bytecodealliance/wasmtime/releases/tag/v0.24.0
@@ -33,18 +37,15 @@ zig build -Dlibrary-search-path=<path-to-libwasmtime>
3337
The `simple.zig` example is equivalent to [`hello.c`] example in Wasmtime. You can run it with
3438

3539
```
36-
zig build example-simple
40+
gyro build run -Dexample=simple
3741
```
3842

3943
Optionally, if you installed `libwasmtime` into some custom path, you can tell zig where to find it
4044
with
4145

4246
```
43-
zig build example-simple -Dlibrary-search-path=<path-to-libwasmtime>
47+
gyro build run -Dexample=simple --search-prefix=<path-to-libwasmtime>
4448
```
4549

4650
[`hello.c`]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/hello.c
4751

48-
**NOTE:** while on Linux and macOS, it is fine to link with `libwasmtime` statically, I've found that
49-
on Windows it is generally safer to use a dynamic library. Therefore, when installing `libwasmtime` make
50-
sure you point to the path where `wasmtime.dll.lib` is located.

build.zig

+21-28
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
1-
const builtin = @import("builtin");
21
const std = @import("std");
3-
const Builder = std.build.Builder;
4-
const LibExeObjStep = std.build.LibExeObjStep;
5-
6-
fn linkWasmtime(step: *LibExeObjStep, search_path: ?[]const u8) void {
7-
if (builtin.os.tag == .windows) {
8-
// On Windows, link dynamic library as otherwise lld will have a
9-
// hard time satisfying `libwasmtime` deps
10-
step.linkSystemLibrary("wasmtime.dll");
11-
} else {
12-
step.linkSystemLibrary("wasmtime");
13-
}
14-
if (search_path) |path| {
15-
step.addLibPath(path);
16-
}
17-
}
2+
const builtin = @import("builtin");
3+
const pkgs = @import("deps.zig").pkgs;
184

19-
pub fn build(b: *Builder) !void {
5+
pub fn build(b: *std.build.Builder) !void {
206
const mode = b.standardReleaseOptions();
217

22-
const lib_path = b.option([]const u8, "library-search-path", "Add additional system library search path.");
23-
248
const lib = b.addStaticLibrary("wasmtime-zig", "src/main.zig");
259
lib.setBuildMode(mode);
10+
lib.addPackage(pkgs.wasm);
2611
lib.install();
2712

2813
var main_tests = b.addTest("src/main.zig");
2914
main_tests.setBuildMode(mode);
30-
linkWasmtime(main_tests, lib_path);
15+
main_tests.addPackage(pkgs.wasm);
3116
main_tests.step.dependOn(b.getInstallStep());
3217

3318
const test_step = b.step("test", "Run library tests");
3419
test_step.dependOn(&main_tests.step);
3520

36-
const example = b.option([]const u8, "example", "The example to run from the /example folder");
37-
const example_path = blk: {
38-
const ex = example orelse "simple";
39-
40-
const path = try std.fs.path.join(b.allocator, &[_][]const u8{ "example", ex });
41-
break :blk try std.mem.concat(b.allocator, u8, &[_][]const u8{ path, ".zig" });
21+
const example = b.option([]const u8, "example", "The example to run from the examples/ folder");
22+
const example_path = example_path: {
23+
const basename = example orelse "simple";
24+
const with_ext = try std.fmt.allocPrint(b.allocator, "{s}.zig", .{basename});
25+
const full_path = try std.fs.path.join(b.allocator, &[_][]const u8{ "examples", with_ext });
26+
break :example_path full_path;
4227
};
4328

4429
const simple_exe = b.addExecutable(example orelse "simple", example_path);
4530
simple_exe.setBuildMode(mode);
46-
simple_exe.addPackagePath("wasmtime", "src/main.zig");
31+
simple_exe.addPackage(.{
32+
.name = "wasmtime",
33+
.path = "src/main.zig",
34+
.dependencies = &.{pkgs.wasm},
35+
});
36+
if (builtin.os.tag == .windows) {
37+
simple_exe.linkSystemLibrary("wasmtime.dll");
38+
} else {
39+
simple_exe.linkSystemLibrary("wasmtime");
40+
}
4741
simple_exe.linkLibC();
48-
linkWasmtime(simple_exe, lib_path);
4942
simple_exe.step.dependOn(b.getInstallStep());
5043

5144
const run_simple_cmd = simple_exe.run();

ci/linux_ci

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
set -x
4+
set -e
5+
6+
ZIG="zig-linux-x86_64-0.8.0-dev.2667+44de88498"
7+
WASMTIME_VERSION="v0.24.0"
8+
WASMTIME="wasmtime-$WASMTIME_VERSION-x86_64-linux-c-api"
9+
GYRO_VERSION="0.2.3"
10+
GYRO="gyro-$GYRO_VERSION-linux-x86_64"
11+
12+
wget -nv "https://ziglang.org/builds/$ZIG.tar.xz"
13+
tar xf "$ZIG.tar.xz"
14+
export PATH="$(pwd)/$ZIG:$PATH"
15+
16+
wget -nv "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.tar.xz"
17+
tar xf "$WASMTIME.tar.xz"
18+
19+
wget -nv "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.tar.gz"
20+
tar xf "$GYRO.tar.gz"
21+
export PATH="$(pwd)/$GYRO/bin:$PATH"
22+
23+
gyro build
24+
gyro build test
25+
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
26+
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
27+
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
28+
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
29+
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"
30+

ci/macos_ci

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
set -x
4+
set -e
5+
6+
ZIG="zig-macos-x86_64-0.8.0-dev.2667+44de88498"
7+
WASMTIME_VERSION="v0.24.0"
8+
WASMTIME="wasmtime-$WASMTIME_VERSION-x86_64-macos-c-api"
9+
GYRO_VERSION="0.2.3"
10+
GYRO="gyro-$GYRO_VERSION-macos-x86_64"
11+
12+
curl -L "https://ziglang.org/builds/$ZIG.tar.xz" -o "$ZIG.tar.xz"
13+
tar xf "$ZIG.tar.xz"
14+
export PATH="$(pwd)/$ZIG:$PATH"
15+
16+
curl -L "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.tar.xz" -o "$WASMTIME.tar.xz"
17+
tar xf "$WASMTIME.tar.xz"
18+
19+
curl -L "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.tar.gz" -o "$GYRO.tar.gz"
20+
tar xf "$GYRO.tar.gz"
21+
export PATH="$(pwd)/$GYRO/bin:$PATH"
22+
23+
gyro build
24+
gyro build test
25+
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
26+
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
27+
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
28+
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
29+
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"
30+

ci/win_ci

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
set -x
4+
set -e
5+
6+
ZIG="zig-windows-x86_64-0.8.0-dev.2667+44de88498"
7+
WASMTIME_VERSION="v0.24.0"
8+
WASMTIME="wasmtime-$WASMTIME_VERSION-x86_64-windows-c-api"
9+
GYRO_VERSION="0.2.3"
10+
GYRO="gyro-$GYRO_VERSION-windows-x86_64"
11+
12+
curl -L "https://ziglang.org/builds/$ZIG.zip" -o "$ZIG.zip"
13+
7z x "$ZIG.zip"
14+
export PATH="$(pwd)/$ZIG:$PATH"
15+
16+
curl -L "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.zip" -o "$WASMTIME.zip"
17+
7z x "$WASMTIME.zip"
18+
19+
curl -L "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.zip" -o "$GYRO.zip"
20+
7z x "$GYRO.zip"
21+
export PATH="$(pwd)/$GYRO/bin:$PATH"
22+
23+
gyro build
24+
gyro build test
25+
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
26+
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
27+
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
28+
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
29+
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"
30+

deps.zig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const std = @import("std");
2+
pub const pkgs = struct {
3+
pub const wasm = std.build.Pkg{
4+
.name = "wasm",
5+
.path = ".gyro/wasm-zig-kubkon-a8f98d100ae0ede37f42d5c084d1401805e1e843/pkg/src/main.zig",
6+
};
7+
8+
pub fn addAllTo(artifact: *std.build.LibExeObjStep) void {
9+
@setEvalBranchQuota(1_000_000);
10+
inline for (std.meta.declarations(pkgs)) |decl| {
11+
if (decl.is_pub and decl.data == .Var) {
12+
artifact.addPackage(@field(pkgs, decl.name));
13+
}
14+
}
15+
}
16+
};
17+
18+
pub const base_dirs = struct {
19+
pub const wasm = ".gyro/wasm-zig-kubkon-a8f98d100ae0ede37f42d5c084d1401805e1e843/pkg";
20+
};
File renamed without changes.

example/gcd.zig renamed to examples/gcd.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const ga = std.heap.c_allocator;
66
const Allocator = std.mem.Allocator;
77

88
pub fn main() !void {
9-
const wasm_path = if (builtin.os.tag == .windows) "example\\gcd.wat" else "example/gcd.wat";
9+
const wasm_path = if (builtin.os.tag == .windows) "examples\\gcd.wat" else "examples/gcd.wat";
1010
const wasm_file = try fs.cwd().openFile(wasm_path, .{});
1111
const wasm = try wasm_file.readToEndAlloc(ga, std.math.maxInt(u64));
1212
defer ga.free(wasm);
@@ -23,7 +23,7 @@ pub fn main() !void {
2323
defer module.deinit();
2424
std.debug.print("Wasm module compiled...\n", .{});
2525

26-
var instance = try wasmtime.Instance.init(store, module, &[_]*wasmtime.Func{});
26+
var instance = try wasmtime.Instance.init(store, module, &.{});
2727
defer instance.deinit();
2828
std.debug.print("Instance initialized...\n", .{});
2929

File renamed without changes.

example/interrupt.zig renamed to examples/interrupt.zig

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@ const fs = std.fs;
55
const ga = std.heap.c_allocator;
66
const Allocator = std.mem.Allocator;
77

8-
fn interrupt(handle: *wasmtime.c.InterruptHandle) void {
8+
fn interrupt(handle: *wasmtime.InterruptHandle) void {
99
// sleep for 2 seconds
1010
std.time.sleep(std.time.ns_per_s * 2);
1111
std.debug.print("Sending interrupt...\n", .{});
1212
handle.interrupt();
1313
}
1414

1515
pub fn main() !void {
16-
const wasm_path = if (builtin.os.tag == .windows) "example\\interrupt.wat" else "example/interrupt.wat";
16+
const wasm_path = if (builtin.os.tag == .windows) "examples\\interrupt.wat" else "examples/interrupt.wat";
1717
const wasm_file = try fs.cwd().openFile(wasm_path, .{});
1818
const wasm = try wasm_file.readToEndAlloc(ga, std.math.maxInt(u64));
1919
defer ga.free(wasm);
2020

2121
const config = try wasmtime.Config.init(.{ .interruptable = true });
22-
var engine = try wasmtime.Engine.withConfig(config);
22+
var engine = try wasmtime.Engine.withConfig(config.inner);
2323
defer engine.deinit();
2424
std.debug.print("Engine initialized...\n", .{});
2525

2626
var store = try wasmtime.Store.init(engine);
2727
std.debug.print("Store initialized...\n", .{});
2828

29-
var handle = try wasmtime.c.InterruptHandle.init(store);
29+
var handle = try wasmtime.InterruptHandle.init(store);
3030
defer handle.deinit();
3131
std.debug.print("Interrupt handle created...\n", .{});
3232

3333
var module = try wasmtime.Module.initFromWat(engine, wasm);
3434
defer module.deinit();
3535
std.debug.print("Wasm module compiled...\n", .{});
3636

37-
var instance = try wasmtime.Instance.init(store, module, &[_]*wasmtime.Func{});
37+
var instance = try wasmtime.Instance.init(store, module, &.{});
3838
defer instance.deinit();
3939
std.debug.print("Instance initialized...\n", .{});
4040

0 commit comments

Comments
 (0)