aduros / wasm4

Build retro games using WebAssembly for a fantasy console.
https://wasm4.org
ISC License
1.16k stars 170 forks source link

update zig template #671

Open Ruulul opened 11 months ago

Ruulul commented 11 months ago

Using master build 0.12.0-dev.1625+6fd1c64f2.

Currently, when you try to build with the template, it has issues, related to open issues with compiling as lib on wasm. Current workaround: add a dummy export fn _start() void {} and compile as executable.

Ergonomics suggestion: it is possible to make zig itself call the w4, requiring a relatively small addition to the build and a new file:

// build.zig
const std = @import("std");

pub fn build(b: *std.Build) !void {
  ...
    const runner = b.addExecutable(.{
        .name = "invoke",
        .root_source_file = .{ .path = "src/invoke.zig" },
    });

    b.installArtifact(lib);
    b.installArtifact(runner);

    const run_cmd = b.addRunArtifact(runner);
    run_cmd.step.dependOn(b.getInstallStep());
    run_cmd.addArg(b.install_path);
    if (b.args) |args| run_cmd.addArgs(args);

    const run_step = b.step("run", "run the program");
    run_step.dependOn(&run_cmd.step);
}
// invoke.zig
const std = @import("std");

pub fn main() !void {
  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
  defer _ = gpa.deinit();
  const allocator = gpa.allocator();

  const args = try std.process.argsAlloc(allocator);
  defer std.process.argsFree(allocator, args);
  const exe_folder = args[1];
  const mode = if (args.len > 2) args[2] else null;
  const exe_path = try std.mem.concat(allocator, u8, &.{ exe_folder, "/bin/cart.wasm" });
  defer allocator.free(exe_path);

  var process = std.ChildProcess.init(&.{ "w4", mode orelse "run", exe_path }, allocator);
  try process.spawn();
  _ = try process.wait();
}

Opening as an issue first to know if I should just directly make a PR to the templates? It bothers me that one has to create the _start function, but I couldnt find a better workaround

Ruulul commented 11 months ago

hmm, poking the repository, I see the master branch already changed the template to use executable (and with a better way to do it!), it is just not on the release version yet

peterhellberg commented 11 months ago

@Ruulul No need for an entrypoint: https://github.com/aduros/wasm4/blob/091b829d548bd37c061b56300488154ad2df4272/cli/assets/templates/zig/build.zig#L14 (after the change to using addExecutable)

As a small sidenote, I have two command line tools that I use to start projects for TIC-80 and Sokol Zig:

[!Note] I have used the new build.zig in a game jam submission :sparkles: and it worked out quite well.

I guess we could have a zig build run command similar to what I have here: https://github.com/peterhellberg/tic-init/blob/f4a6aca706dc6734952635444067dc9251134d92/content/build.zig#L32-L48 🤔

Ruulul commented 11 months ago

in the process of poking your links and the build documentation, I learned so much more about the capacities of the zig build system

I replaced the invoke.zig file with a simple snippet https://github.com/Ruulul/wasm4evolution/blob/281176497e5ae7f23216ea80a9df6d1073828b02/build.zig#L23C1-L30C38

gonna soon publish this small simulator to wasm4, just need to adjust some things first

Inve1951 commented 10 months ago

I just published my zig-wasm4-starter-kit which might interest you.

peterhellberg commented 10 months ago

And I have a little tool myself over at https://github.com/peterhellberg/w4-init which I've so far used in a few small experiments ✨