Skip to content

Commit

Permalink
Merge pull request #50 from golemcloud/zig-fix
Browse files Browse the repository at this point in the history
Update Zig examples to 0.13
  • Loading branch information
vigoo authored Aug 16, 2024
2 parents 14239d9 + df87b8c commit 3ba1017
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
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

0 comments on commit 3ba1017

Please sign in to comment.