2026-06-09 19:29:01 +02:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
|
|
pub fn build(b: *std.Build) void {
|
2026-06-09 21:38:13 +02:00
|
|
|
|
2026-06-12 16:46:43 +02:00
|
|
|
//// Compilation options
|
|
|
|
|
|
|
|
|
|
// Static/Dynamic linkage
|
2026-06-09 21:38:13 +02:00
|
|
|
const opt_shared = b.option(
|
|
|
|
|
bool,
|
|
|
|
|
"shared",
|
|
|
|
|
"Build as a dynamic (shared) library",
|
|
|
|
|
) orelse false;
|
|
|
|
|
|
2026-06-12 16:46:43 +02:00
|
|
|
// `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
|
|
|
|
|
};
|
2026-06-09 21:38:13 +02:00
|
|
|
|
2026-06-09 19:29:01 +02:00
|
|
|
|
2026-06-12 16:46:43 +02:00
|
|
|
//// Library
|
|
|
|
|
|
2026-06-09 21:27:02 +02:00
|
|
|
// Create root module
|
2026-06-12 16:46:43 +02:00
|
|
|
const root_module = ctx.createModule("src/root.zig");
|
2026-06-09 21:27:02 +02:00
|
|
|
|
2026-06-09 19:29:01 +02:00
|
|
|
// Create library
|
2026-06-12 16:46:43 +02:00
|
|
|
const lib_obj = b.addLibrary(.{
|
2026-06-09 19:29:01 +02:00
|
|
|
.name = "reticulum-zero",
|
2026-06-12 16:46:43 +02:00
|
|
|
.linkage = ctx.linkage,
|
2026-06-09 21:27:02 +02:00
|
|
|
.root_module = root_module,
|
2026-06-09 19:29:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Output library object
|
2026-06-12 16:46:43 +02:00
|
|
|
b.installArtifact(lib_obj);
|
2026-06-09 19:29:01 +02:00
|
|
|
|
|
|
|
|
|
2026-06-12 16:46:43 +02:00
|
|
|
////////// Testing
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
ctx.addTestModule(root_module);
|
|
|
|
|
ctx.addTestFile("src/packet.zig");
|
2026-06-09 19:29:01 +02:00
|
|
|
}
|
2026-06-12 16:46:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////// 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);
|
|
|
|
|
}
|
|
|
|
|
};
|