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 template #671

Open
Ruulul opened this issue Nov 16, 2023 · 5 comments
Open

update zig template #671

Ruulul opened this issue Nov 16, 2023 · 5 comments

Comments

@Ruulul
Copy link

Ruulul commented Nov 16, 2023

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
Copy link
Author

Ruulul commented Nov 16, 2023

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
Copy link
Contributor

peterhellberg commented Nov 16, 2023

@Ruulul No need for an entrypoint:

exe.entry = .disabled;
(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:

Maybe I should also create something like that for WASM-4 :)

Note

I have used the new build.zig in a game jam submission ✨ 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
Copy link
Author

Ruulul commented Nov 18, 2023

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
Copy link
Contributor

Inve1951 commented Dec 4, 2023

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

@peterhellberg
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants