Install the latest version (0.11) of Zig (for example brew install zig
).
You also have to install everything as described in Common tooling.
Currently we consider Zig a Tier 3 guest language, which means we cannot use any WIT interface definitions for it and can only call the Zig program’s main
function exposed as a parameterless run
function for Golem.
The easiest way to get started once the tooling is installed is to use the golem new
command as described in the Quickstart.
If you prefer to do it from scratch, first write a Zig program that has a main
function src/main.zig
:
pub fn main() anyerror!void {
// ...
}
Create a Zig build file that targets WASM and WASI:
const std = @import("std");
const Builder = std.build.Builder;
const CrossTarget = std.zig.CrossTarget;
pub fn build(b: *Builder) void {
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseSmall,
});
const exe = b.addExecutable(.{
.name = "main",
.root_source_file = .{ .path = "src/main.zig" },
.target = .{
.cpu_arch = .wasm32,
.os_tag = .wasi,
},
.optimize = optimize
});
b.installArtifact(exe);
}
Then build the WASM module with:
zig build
The resulting WASM file in zig-out/bin/main.wasm
is just a WASM module, not ready to be used with Golem.
Then we have to convert the module to a WASM component, including mapping its WASI Preview1 imports into WASI Preview2 imports.
This is done with wasm-tools
:
wasm-tools component new zig-out/bin/main.wasm -o template.wasm --adapt tier3/wasi_snapshot_preview1.wasm
The wasi_snapshot_preview1.wasm
describes the mapping from WASI Preview1 to Preview2. You must use the version of this file packaged together with golem-cli.
The resulting template.wasm
is ready to be used with Golem.