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..30665a5 100644 --- a/build.zig +++ b/build.zig @@ -1,18 +1,22 @@ 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 folder = try std.fs.openDirAbsolute(root_path, .{}); folder.makeDir("zig-out") catch {}; const obj = b.addSystemCommand(&.{ @@ -24,18 +28,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