From 0fe1acbc89489e09c3f3f234ea28fbea787f7240 Mon Sep 17 00:00:00 2001 From: coco875 Date: Fri, 29 Aug 2025 01:55:20 +0200 Subject: [PATCH 1/2] update gameboy example --- .gitignore | 2 +- build.zig | 31 +++++++++++++++++++++++-------- utils.zig | 11 +++++++---- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 18517d4..81575ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ zig-out -zig-cache +.zig-cache gbdk \ No newline at end of file diff --git a/build.zig b/build.zig index cdd3ae1..ca3f112 100644 --- a/build.zig +++ b/build.zig @@ -1,18 +1,26 @@ const std = @import("std"); -const builtin = @import("builtin"); const utils = @import("utils.zig"); const emulator = "vbam"; -pub fn build(b: *std.build.Builder) !void { - b.build_root = try utils.root(b.build_root); +fn buildImpl(b: *std.Build) !void { + // Obtenir la racine du projet (cwd lors de l'invocation de zig build) + const root_path = try std.process.getCwdAlloc(b.allocator); + defer b.allocator.free(root_path); + + // Télécharge + extrait le toolchain GBDK si absent try utils.ensure_tar( b.allocator, - b.build_root, + root_path, "gbdk", - "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.0.6/gbdk-linux64.tar.gz", + "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.4.0/gbdk-linux64.tar.gz", ); - const folder = try std.fs.openDirAbsolute(b.build_root, .{}); + + const zig_out_folder_path = try std.fs.path.join(b.allocator, &.{ root_path, "zig-out" }); + + try utils.ensure_file(b.allocator, zig_out_folder_path, "zig.h", "https://raw.githubusercontent.com/ziglang/zig/refs/tags/0.14.1/lib/zig.h"); + + const folder = try std.fs.openDirAbsolute(root_path, .{}); folder.makeDir("zig-out") catch {}; const obj = b.addSystemCommand(&.{ @@ -24,18 +32,25 @@ pub fn build(b: *std.build.Builder) !void { "zig-gb.o", "../src/main.c", }); - obj.cwd = "zig-out"; + obj.cwd = b.path("zig-out"); const gb = b.addSystemCommand(&.{ "../gbdk/bin/lcc", "-Wa-l", "-DUSE_SFR_FOR_REG", "-o", "zig-gb.gb", "zig-gb.o", }); - gb.cwd = "zig-out"; + gb.cwd = b.path("zig-out"); b.default_step.dependOn(&gb.step); gb.step.dependOn(&obj.step); const run_step = b.step("run", "Run in Visual Boy Advance"); const vbam = b.addSystemCommand(&.{ emulator, "-F", "zig-out/zig-gb.gb" }); + vbam.cwd = b.path("."); run_step.dependOn(&gb.step); run_step.dependOn(&vbam.step); } + +pub fn build(b: *std.Build) void { + buildImpl(b) catch |err| { + std.log.err("build failed: {s}", .{@errorName(err)}); + }; +} diff --git a/utils.zig b/utils.zig index 40d918a..9fa24ce 100644 --- a/utils.zig +++ b/utils.zig @@ -27,11 +27,14 @@ test "root" { /// Run shell command pub fn exec(allocator: std.mem.Allocator, cwd: []const u8, argv: []const []const u8, opts: Options) !void { - var child = try std.ChildProcess.init(argv, allocator); + var child = std.process.Child.init(argv, allocator); child.cwd = cwd; - if (opts.enable_stdout) child.stdout = std.io.getStdOut() else child.stdout = null; - if (opts.enable_stderr) child.stderr = std.io.getStdErr() else child.stderr = null; - _ = try child.spawnAndWait(); + // Comportement des flux selon options + child.stdin_behavior = .Ignore; + child.stdout_behavior = if (opts.enable_stdout) .Inherit else .Ignore; + child.stderr_behavior = if (opts.enable_stderr) .Inherit else .Ignore; + try child.spawn(); + _ = try child.wait(); } /// Ensure repository exists, if not clone it with git. cwd is absolute path From ca37305b3b142636c40728f3040f8318da89e5dc Mon Sep 17 00:00:00 2001 From: coco875 Date: Fri, 29 Aug 2025 01:56:31 +0200 Subject: [PATCH 2/2] Update build.zig --- build.zig | 4 ---- 1 file changed, 4 deletions(-) diff --git a/build.zig b/build.zig index ca3f112..30665a5 100644 --- a/build.zig +++ b/build.zig @@ -16,10 +16,6 @@ fn buildImpl(b: *std.Build) !void { "https://github.com/gbdk-2020/gbdk-2020/releases/download/4.4.0/gbdk-linux64.tar.gz", ); - const zig_out_folder_path = try std.fs.path.join(b.allocator, &.{ root_path, "zig-out" }); - - try utils.ensure_file(b.allocator, zig_out_folder_path, "zig.h", "https://raw.githubusercontent.com/ziglang/zig/refs/tags/0.14.1/lib/zig.h"); - const folder = try std.fs.openDirAbsolute(root_path, .{}); folder.makeDir("zig-out") catch {};