From da24a10867a7093f77c6c0a7e4a4ffda05456d6a Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 12 Jun 2026 16:46:43 +0200 Subject: [PATCH] Reworked the build system --- build.zig | 101 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/build.zig b/build.zig index a4a0a64..ffa5b3d 100644 --- a/build.zig +++ b/build.zig @@ -2,47 +2,98 @@ const std = @import("std"); pub fn build(b: *std.Build) void { - // Compilation options + //// Compilation options + + // Static/Dynamic linkage const opt_shared = b.option( bool, "shared", "Build as a dynamic (shared) library", ) orelse false; - const linkage: std.builtin.LinkMode = if (opt_shared) - .static - else - .dynamic; + // `zig build test` + const opt_test = b.step( + "test", + "Run library tests", + ); - // Configuration - const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); + //// Build Context + var ctx = BuilderContext{ + .b = b, + // Configuration + .target = b.standardTargetOptions(.{}), + .optimize = b.standardOptimizeOption(.{}), + .linkage = if (opt_shared) .static else .dynamic, + .test_step = opt_test + }; + + //// Library + // Create root module - const root_module = b.createModule(.{ - .root_source_file = b.path("src/root.zig"), - .target = target, - .optimize = optimize, - }); + const root_module = ctx.createModule("src/root.zig"); // Create library - const lib = b.addLibrary(.{ + const lib_obj = b.addLibrary(.{ .name = "reticulum-zero", - .linkage = linkage, + .linkage = ctx.linkage, .root_module = root_module, }); // Output library object - b.installArtifact(lib); + b.installArtifact(lib_obj); - // Set up unit testing - // const unit_tests = b.addTest(.{ - // .root_module = root_module, - // }); - // const run_unit_tests = b.addRunStep(unit_tests); - - // `zig build test` command - // const test_step = b.step("test", "Run library unit tests"); - // test_step.dependency(&run_unit_tests.step); + ////////// Testing + // + + ctx.addTestModule(root_module); + ctx.addTestFile("src/packet.zig"); } + + +////////////// BuildContext +// + +// Holds all the context values during build and provides related functions +const BuilderContext = struct { + + ////////// Properties + // + + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + linkage: std.builtin.LinkMode, + test_step: *std.Build.Step, + + ////////// Functions + // + + // Creates a zig build module + fn createModule(self: *BuilderContext, file: []const u8) *std.Build.Module { + + return self.b.createModule(.{ + .root_source_file = self.b.path(file), + .target = self.target, + .optimize = self.optimize, + }); + } + + // Adds a source file's own tests to the tests list + fn addTestFile(self: *BuilderContext, file: []const u8) void { + + const module = self.createModule(file); + self.addTestModule(module); + } + + // Adds a module's own tests to the tests list + fn addTestModule(self: *BuilderContext, module: *std.Build.Module) void { + + const unit_tests = self.b.addTest(.{ + .root_module = module, + }); + const run_tests = self.b.addRunArtifact(unit_tests); + self.test_step.dependOn(&run_tests.step); + } +};