aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRatakor <ratakor@disroot.org>2023-09-18 12:32:04 +0200
committerRatakor <ratakor@disroot.org>2023-09-18 12:32:04 +0200
commit3d16629a0533b2a78f6e0692c51c5425153ea342 (patch)
treec997d378b1756834c5e33189e86e91eccdfe68fb
parent5edd4065a9bcb70e9cd557548f49cf3605303fe8 (diff)
Add examples build step and remove bfc step
-rw-r--r--README.md6
-rw-r--r--build.zig38
2 files changed, 32 insertions, 12 deletions
diff --git a/README.md b/README.md
index a92a0f4..6e0b1af 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/build.zig b/build.zig
index 91d33f8..8b45eb1 100644
--- a/build.zig
+++ b/build.zig
@@ -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);