103 lines
2.9 KiB
Zig
103 lines
2.9 KiB
Zig
const std = @import("std");
|
|
|
|
pub const ModLib = struct {
|
|
b: *std.Build,
|
|
lib: *std.Build.Step.Compile,
|
|
mod: *std.Build.Module,
|
|
|
|
target: std.Build.ResolvedTarget,
|
|
optimize: std.builtin.OptimizeMode,
|
|
static_build: bool,
|
|
|
|
pub fn install(self: @This()) void {
|
|
self.b.installArtifact(self.lib);
|
|
}
|
|
|
|
pub fn addIncludePath(self: @This(), path: []const u8) void {
|
|
self.lib.addIncludePath(self.b.path(path));
|
|
self.mod.addIncludePath(self.b.path(path));
|
|
}
|
|
|
|
pub fn linkModLibs(self: @This(), list: []const []const u8) void {
|
|
for (list) |depName| {
|
|
const dep = self.b.dependency(depName, .{
|
|
.target = self.target,
|
|
.optimize = self.optimize,
|
|
.static_build = self.static_build,
|
|
});
|
|
|
|
self.mod.addImport(depName, dep.module(depName));
|
|
self.lib.linkLibrary(dep.artifact(depName));
|
|
}
|
|
}
|
|
};
|
|
|
|
pub const ModLibOptions = struct {
|
|
name: []const u8,
|
|
target: std.Build.ResolvedTarget,
|
|
optimize: std.builtin.OptimizeMode,
|
|
static_build: bool,
|
|
allow_dynamic: bool = false,
|
|
link_libc: bool = false,
|
|
link_libcpp: bool = false,
|
|
root: ?std.Build.LazyPath = null,
|
|
stub: ?std.Build.LazyPath = null,
|
|
};
|
|
|
|
pub fn MakeModLib(b: *std.Build, o: ModLibOptions) ModLib {
|
|
const mod = b.addModule(o.name, .{
|
|
.target = o.target,
|
|
.optimize = o.optimize,
|
|
.root_source_file = if (o.root != null) o.root.? else b.path(b.fmt("src/{s}.zig", .{o.name})),
|
|
});
|
|
|
|
const empty_file = b.addWriteFile("stubs", "");
|
|
|
|
const lib = b.addLibrary(.{
|
|
.name = o.name,
|
|
.linkage = if (o.allow_dynamic and !o.static_build) .dynamic else .static,
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = empty_file.add("stubc.zig", ""),
|
|
.target = o.target,
|
|
.optimize = o.optimize,
|
|
}),
|
|
});
|
|
|
|
return .{
|
|
.b = b,
|
|
.lib = lib,
|
|
.mod = mod,
|
|
.target = o.target,
|
|
.optimize = o.optimize,
|
|
.static_build = o.static_build,
|
|
};
|
|
}
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
const static_build = b.option(bool, "static_build", "builds backlog dependencies for static linking") orelse false;
|
|
|
|
const r = MakeModLib(b, .{
|
|
.name = "bh",
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.static_build = static_build,
|
|
});
|
|
|
|
b.installArtifact(r.lib);
|
|
|
|
const test_step = b.step("test", "run unit tests for enet");
|
|
const tests = b.addTest(.{
|
|
.root_module = b.createModule(.{
|
|
.link_libc = true,
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.root_source_file = b.path("src/tests.zig"),
|
|
}),
|
|
});
|
|
|
|
const runArtifact = b.addRunArtifact(tests);
|
|
test_step.dependOn(&runArtifact.step);
|
|
}
|