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

Update Zig examples to 0.13 #50

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
18 changes: 9 additions & 9 deletions examples/zig/zig-default-minimal/build.zig
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
const std = @import("std");
const Builder = std.build.Builder;
const Build = std.Build;
const CrossTarget = std.zig.CrossTarget;

pub fn build(b: *Builder) !void {
pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseSmall,
});

const bindgen = b.addSystemCommand(&.{ "wit-bindgen", "c", "--autodrop-borrows", "yes", "./wit", "--out-dir", "src/bindings" });

const wasm = b.addExecutable(.{ .name = "main", .root_source_file = .{ .path = "src/main.zig" }, .target = .{
const wasm = b.addExecutable(.{ .name = "main", .root_source_file = b.path("src/main.zig"), .target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .wasi,
}, .optimize = optimize });
}), .optimize = optimize });

const binding_root = b.pathFromRoot("src/bindings");
var binding_root_dir = try std.fs.cwd().openIterableDir(binding_root, .{});
const binding_root = "src/bindings";
var binding_root_dir = try std.fs.cwd().openDir(binding_root, .{ .iterate = true});
defer binding_root_dir.close();
var it = try binding_root_dir.walk(b.allocator);
while (try it.next()) |entry| {
switch (entry.kind) {
.file => {
const path = b.pathJoin(&.{ binding_root, entry.path });
if (std.mem.endsWith(u8, entry.basename, ".c")) {
wasm.addCSourceFile(.{ .file = .{ .path = path }, .flags = &.{} });
wasm.addCSourceFile(.{ .file = b.path(path), .flags = &.{} });
} else if (std.mem.endsWith(u8, entry.basename, ".o")) {
wasm.addObjectFile(.{ .path = path });
wasm.addObjectFile(b.path(path));
}
},
else => continue,
}
}

wasm.addIncludePath(.{ .path = binding_root });
wasm.addIncludePath(b.path(binding_root));
wasm.linkLibC();

wasm.step.dependOn(&bindgen.step);
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions examples/zig/zig-default-minimal/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const Command = union(CommandTag) { add: i32, get: void };

var state: u64 = 0;

export fn exports_golem_it_api_add(value: u64) void {
export fn exports_pack_name_api_add(value: u64) void {
const stdout = std.io.getStdOut().writer();
stdout.print("Adding {} to state\n", .{value}) catch unreachable;
state += value;
}

export fn exports_golem_it_api_get() u64 {
export fn exports_pack_name_api_get() u64 {
return state;
}

Expand Down
18 changes: 9 additions & 9 deletions examples/zig/zig-default/build.zig
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
const std = @import("std");
const Builder = std.build.Builder;
const Build = std.Build;
const CrossTarget = std.zig.CrossTarget;

pub fn build(b: *Builder) !void {
pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseSmall,
});

const bindgen = b.addSystemCommand(&.{ "wit-bindgen", "c", "--autodrop-borrows", "yes", "./wit", "--out-dir", "src/bindings" });

const wasm = b.addExecutable(.{ .name = "main", .root_source_file = .{ .path = "src/main.zig" }, .target = .{
const wasm = b.addExecutable(.{ .name = "main", .root_source_file = b.path("src/main.zig"), .target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .wasi,
}, .optimize = optimize });
}), .optimize = optimize });

const binding_root = b.pathFromRoot("src/bindings");
var binding_root_dir = try std.fs.cwd().openIterableDir(binding_root, .{});
const binding_root = "src/bindings";
var binding_root_dir = try std.fs.cwd().openDir(binding_root, .{ .iterate = true});
defer binding_root_dir.close();
var it = try binding_root_dir.walk(b.allocator);
while (try it.next()) |entry| {
switch (entry.kind) {
.file => {
const path = b.pathJoin(&.{ binding_root, entry.path });
if (std.mem.endsWith(u8, entry.basename, ".c")) {
wasm.addCSourceFile(.{ .file = .{ .path = path }, .flags = &.{} });
wasm.addCSourceFile(.{ .file = b.path(path), .flags = &.{} });
} else if (std.mem.endsWith(u8, entry.basename, ".o")) {
wasm.addObjectFile(.{ .path = path });
wasm.addObjectFile(b.path(path));
}
},
else => continue,
}
}

wasm.addIncludePath(.{ .path = binding_root });
wasm.addIncludePath(b.path(binding_root));
wasm.linkLibC();

wasm.step.dependOn(&bindgen.step);
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions examples/zig/zig-default/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const Command = union(CommandTag) { add: i32, get: void };

var state: u64 = 0;

export fn exports_golem_it_api_add(value: u64) void {
export fn exports_pack_name_api_add(value: u64) void {
const stdout = std.io.getStdOut().writer();
stdout.print("Adding {} to state\n", .{value}) catch unreachable;
state += value;
}

export fn exports_golem_it_api_get() u64 {
export fn exports_pack_name_api_get() u64 {
return state;
}

Expand Down
Loading