Skip to content

Commit

Permalink
refactor(build): Clean up
Browse files Browse the repository at this point in the history
Signed-off-by: prajwalch <prajwal.chapagain58@gmail.com>
  • Loading branch information
prajwalch committed Sep 11, 2024
1 parent b22ac37 commit 2a10dc1
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const yazap_mod = b.addModule("yazap", .{ .root_source_file = b.path("src/lib.zig") });

const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const test_step = b.step("test", "Run library tests");
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
test_step.dependOn(&run_unit_tests.step);

const docs_test = b.addTest(.{ .root_source_file = b.path("src/lib.zig") });
const install_docs = b.addInstallDirectory(.{
.source_dir = docs_test.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const docs_step = b.step("docs", "Generate docs");
docs_step.dependOn(&install_docs.step);

const examples_step = b.step("examples", "Build all the example");

inline for (.{ "git", "touch", "ls" }) |example_name| {
const example = b.addExecutable(.{
const yazap = b.addModule("yazap", .{ .root_source_file = b.path("src/lib.zig") });

testStep(b);
examplesStep(b, yazap);
}

fn testStep(b: *std.Build) void {
// Test file information.
const tests = b.addTest(.{ .root_source_file = b.path("src/test.zig") });
// This runs the unit tests.
const runner = b.addRunArtifact(tests);

const step = b.step("test", "Run unit tests");
step.dependOn(&runner.step);
}

fn examplesStep(b: *std.Build, yazap: *std.Build.Module) void {
const step = b.step("examples", "Build all the examples");

var dir = std.fs.cwd().openDir("./examples/", .{ .iterate = true }) catch @panic(
"failed to open examples dir",
);
defer dir.close();

var examples = dir.iterate();
while (examples.next() catch @panic("failed to get example file")) |example_file| {
std.debug.assert(example_file.kind == .file);

// Example file path.
const example_file_path = b.path(b.fmt("examples/{s}", .{example_file.name}));
// Example file name without extension.
const example_name = std.fs.path.stem(example_file_path.getDisplayName());

// Binary information of an example.
const executable = b.addExecutable(.{
.name = example_name,
.root_source_file = b.path(b.fmt("examples/{s}.zig", .{example_name})),
.target = target,
.optimize = optimize,
.root_source_file = example_file_path,
.target = b.graph.host,
});
const install_example = b.addInstallArtifact(example, .{});
example.root_module.addImport("yazap", yazap_mod);
examples_step.dependOn(&example.step);
examples_step.dependOn(&install_example.step);
// Add yazap as a dependency.
executable.root_module.addImport("yazap", yazap);

// This copies the compiled binary to the `.zig-out/bin`.
const installer = b.addInstallArtifact(executable, .{});
step.dependOn(&installer.step);
}
}

0 comments on commit 2a10dc1

Please sign in to comment.