reticulum-zero/build.zig
2026-06-12 16:46:43 +02:00

99 lines
2.3 KiB
Zig

const std = @import("std");
pub fn build(b: *std.Build) void {
//// Compilation options
// Static/Dynamic linkage
const opt_shared = b.option(
bool,
"shared",
"Build as a dynamic (shared) library",
) orelse false;
// `zig build test`
const opt_test = b.step(
"test",
"Run library tests",
);
//// 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 = ctx.createModule("src/root.zig");
// Create library
const lib_obj = b.addLibrary(.{
.name = "reticulum-zero",
.linkage = ctx.linkage,
.root_module = root_module,
});
// Output library object
b.installArtifact(lib_obj);
////////// 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);
}
};