diff options
author | Ratakor <ratakor@disroot.org> | 2023-09-18 12:32:04 +0200 |
---|---|---|
committer | Ratakor <ratakor@disroot.org> | 2023-09-18 12:32:04 +0200 |
commit | 3d16629a0533b2a78f6e0692c51c5425153ea342 (patch) | |
tree | c997d378b1756834c5e33189e86e91eccdfe68fb | |
parent | 5edd4065a9bcb70e9cd557548f49cf3605303fe8 (diff) |
Add examples build step and remove bfc step
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | build.zig | 38 |
2 files changed, 32 insertions, 12 deletions
@@ -27,3 +27,9 @@ Options: -c Compile and assemble, but do not link. -s Strip the output file. ``` + +todo +---- +- add sighandler to remove temporary files +- use an IR to do more optimization +- support more targets @@ -1,6 +1,6 @@ const std = @import("std"); -pub fn build(b: *std.Build) void { +pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -28,21 +28,35 @@ pub fn build(b: *std.Build) void { bf.strip = true; b.installArtifact(bf); - const bfc_cmd = b.addRunArtifact(bfc); - bfc_cmd.step.dependOn(b.getInstallStep()); + const run_cmd = b.addRunArtifact(bf); + run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { - bfc_cmd.addArgs(args); + run_cmd.addArgs(args); } - const bfc_step = b.step("bfc", "Compile a brainfuck program with bfc"); - bfc_step.dependOn(&bfc_cmd.step); + const run_step = b.step("run", "Run a brainfuck program"); + run_step.dependOn(&run_cmd.step); - const bf_cmd = b.addRunArtifact(bf); - bf_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - bf_cmd.addArgs(args); + const examples_step = b.step("examples", "Compile all the examples"); + examples_step.dependOn(b.getInstallStep()); + const cwd = std.fs.cwd(); + try cwd.makePath("./zig-out/examples"); + var examples_dir = try cwd.openIterableDir("./examples", .{}); + defer examples_dir.close(); + var examples_iterator = examples_dir.iterate(); + while (try examples_iterator.next()) |file| { + const example_cmd = b.addRunArtifact(bfc); + const in = try std.fmt.allocPrint(b.allocator, "./examples/{s}", .{file.name}); + const out = try std.fmt.allocPrint( + b.allocator, + "./zig-out/examples/{s}", + // trim .o assuming there is only source files + .{file.name[0..file.name.len - 2]} + ); + example_cmd.addArgs(&[_][]const u8{ "-s", "-o", out, in }); + examples_step.dependOn(&example_cmd.step); + b.allocator.free(in); + b.allocator.free(out); } - const bf_step = b.step("bf", "Interprete a brainfuck program with bf"); - bf_step.dependOn(&bf_cmd.step); const clean_step = b.step("clean", "Delete all artifacts created by zig build"); clean_step.dependOn(&b.addRemoveDirTree("zig-cache").step); |