Reworked the build system
This commit is contained in:
parent
7c5f8c9f30
commit
da24a10867
1 changed files with 76 additions and 25 deletions
97
build.zig
97
build.zig
|
|
@ -2,47 +2,98 @@ const std = @import("std");
|
||||||
|
|
||||||
pub fn build(b: *std.Build) void {
|
pub fn build(b: *std.Build) void {
|
||||||
|
|
||||||
// Compilation options
|
//// Compilation options
|
||||||
|
|
||||||
|
// Static/Dynamic linkage
|
||||||
const opt_shared = b.option(
|
const opt_shared = b.option(
|
||||||
bool,
|
bool,
|
||||||
"shared",
|
"shared",
|
||||||
"Build as a dynamic (shared) library",
|
"Build as a dynamic (shared) library",
|
||||||
) orelse false;
|
) orelse false;
|
||||||
|
|
||||||
const linkage: std.builtin.LinkMode = if (opt_shared)
|
// `zig build test`
|
||||||
.static
|
const opt_test = b.step(
|
||||||
else
|
"test",
|
||||||
.dynamic;
|
"Run library tests",
|
||||||
|
);
|
||||||
|
|
||||||
|
//// Build Context
|
||||||
|
var ctx = BuilderContext{
|
||||||
|
.b = b,
|
||||||
// Configuration
|
// Configuration
|
||||||
const target = b.standardTargetOptions(.{});
|
.target = b.standardTargetOptions(.{}),
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
.optimize = b.standardOptimizeOption(.{}),
|
||||||
|
.linkage = if (opt_shared) .static else .dynamic,
|
||||||
|
.test_step = opt_test
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//// Library
|
||||||
|
|
||||||
// Create root module
|
// Create root module
|
||||||
const root_module = b.createModule(.{
|
const root_module = ctx.createModule("src/root.zig");
|
||||||
.root_source_file = b.path("src/root.zig"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create library
|
// Create library
|
||||||
const lib = b.addLibrary(.{
|
const lib_obj = b.addLibrary(.{
|
||||||
.name = "reticulum-zero",
|
.name = "reticulum-zero",
|
||||||
.linkage = linkage,
|
.linkage = ctx.linkage,
|
||||||
.root_module = root_module,
|
.root_module = root_module,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Output library object
|
// 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);
|
////////// Testing
|
||||||
|
//
|
||||||
|
|
||||||
// `zig build test` command
|
ctx.addTestModule(root_module);
|
||||||
// const test_step = b.step("test", "Run library unit tests");
|
ctx.addTestFile("src/packet.zig");
|
||||||
// test_step.dependency(&run_unit_tests.step);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////// 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue