const std = @import("std"); pub const ModLib = struct { b: *std.Build, lib: *std.Build.Step.Compile, mod: *std.Build.Module, pub fn install(self: @This()) void { self.b.installArtifact(self.lib); } }; 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}", .{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, }; } pub const Options = struct { target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, static_build: bool, tracy: bool, }; // standard set of options that all backlog modules and dependenices shall use. pub fn declareOptions(b: *std.Build) Options { return .{ .target = b.standardTargetOptions(.{}), .optimize = b.standardOptimizeOption(.{}), .static_build = b.option(bool, "static_build", "builds backlog dependencies for static linking") orelse false, .tracy = b.option(bool, "tracy", "builds with tracy support") orelse false, }; } 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); }