Reworked the build system

This commit is contained in:
Gu://em_ 2026-06-12 16:46:43 +02:00
parent 7c5f8c9f30
commit da24a10867

101
build.zig
View file

@ -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);
}
};