thp-zig/build.zig

115 lines
3.6 KiB
Zig
Raw Permalink Normal View History

2024-11-16 01:00:15 +00:00
const std = @import("std");
pub fn build(b: *std.Build) void {
// Standard target options
2024-11-16 01:00:15 +00:00
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Create options module for tracing
const executionTracing = b.option(bool, "tracing", "enable execution tracing") orelse false;
const options = b.addOptions();
options.addOption(bool, "tracing", executionTracing);
// Create the options module that will be shared
const options_module = options.createModule();
2024-12-06 02:02:09 +00:00
const exe = b.addExecutable(.{
2024-11-16 01:00:15 +00:00
.name = "thp",
2024-12-06 02:02:09 +00:00
.root_source_file = b.path("src/main.zig"),
2024-11-16 01:00:15 +00:00
.target = target,
.optimize = optimize,
});
// Add options to executable
exe.root_module.addImport("config", options_module);
2024-12-21 11:46:53 +00:00
2024-12-21 11:11:16 +00:00
//
// Error handling module
//
const error_module = b.addModule("errors", .{
.root_source_file = b.path("src/errors/root.zig"),
.target = target,
.optimize = optimize,
});
error_module.addImport("config", options_module);
2024-12-21 11:11:16 +00:00
exe.root_module.addImport("errors", error_module);
2024-12-06 02:02:09 +00:00
//
// Lexic module
//
const lexic_module = b.addModule("lexic", .{
.root_source_file = b.path("src/01_lexic/root.zig"),
.target = target,
.optimize = optimize,
});
lexic_module.addImport("config", options_module);
2024-12-21 11:11:16 +00:00
lexic_module.addImport("errors", error_module);
exe.root_module.addImport("lexic", lexic_module);
2024-11-16 01:00:15 +00:00
2024-12-06 02:02:09 +00:00
//
// Syntax module
//
const syntax_module = b.addModule("syntax", .{
.root_source_file = b.path("src/02_syntax/root.zig"),
2024-11-16 01:00:15 +00:00
.target = target,
.optimize = optimize,
});
syntax_module.addImport("config", options_module);
2024-12-21 11:11:16 +00:00
syntax_module.addImport("lexic", lexic_module);
syntax_module.addImport("errors", error_module);
exe.root_module.addImport("syntax", syntax_module);
2024-11-16 01:00:15 +00:00
// Install step
2024-11-16 01:00:15 +00:00
b.installArtifact(exe);
// Run command
2024-11-16 01:00:15 +00:00
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
// Pass arguments if any
2024-11-16 01:00:15 +00:00
if (b.args) |args| {
run_cmd.addArgs(args);
}
// Run step
2024-11-16 01:00:15 +00:00
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Unit tests
2024-11-16 01:00:15 +00:00
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe_unit_tests.root_module.addImport("config", options_module);
2024-12-06 02:02:09 +00:00
exe_unit_tests.root_module.addImport("lexic", lexic_module);
exe_unit_tests.root_module.addImport("syntax", syntax_module);
2024-12-21 11:11:16 +00:00
exe_unit_tests.root_module.addImport("errors", error_module);
2024-11-16 01:00:15 +00:00
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run ALL unit tests");
2024-11-16 01:00:15 +00:00
test_step.dependOn(&run_exe_unit_tests.step);
2024-12-06 02:02:09 +00:00
// Add dependencies for unit testing
const files = [_][]const u8{
"src/01_lexic/root.zig",
2024-12-06 02:02:09 +00:00
"src/02_syntax/root.zig",
2024-12-24 23:33:02 +00:00
"src/errors/root.zig",
};
for (files) |file| {
const file_unit_test = b.addTest(.{
.root_source_file = b.path(file),
.target = target,
.optimize = optimize,
});
file_unit_test.root_module.addImport("config", options_module);
2024-12-06 02:02:09 +00:00
file_unit_test.root_module.addImport("lexic", lexic_module);
file_unit_test.root_module.addImport("syntax", syntax_module);
2024-12-21 11:11:16 +00:00
file_unit_test.root_module.addImport("errors", error_module);
2024-12-06 02:02:09 +00:00
var test_artifact = b.addRunArtifact(file_unit_test);
test_step.dependOn(&test_artifact.step);
}
2024-11-16 01:00:15 +00:00
}