123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- const std = @import("std");
- const Build = std.Build;
- // TODO: get version from `build.zig.zon`
- const VERSION = std.SemanticVersion.parse("0.0.2") catch unreachable;
- const BuildCtx = struct {
- const Self = @This();
- const NamedExe = struct {
- name: []const u8,
- exe: *Build.Step.Compile,
- };
- build: *Build,
- modules: []*Build.Module,
- target: Build.ResolvedTarget,
- optimize: std.builtin.Mode,
- bin_path: []const u8,
- lib_path: []const u8,
- fn init(b: *Build) !Self {
- const lib_path = b.pathJoin(&.{ "src", "lib.zig" });
- return .{
- .build = b,
- .modules = blk: {
- var modules = std.ArrayList(*Build.Module).init(b.allocator);
- const clap =
- b.dependency("clap", .{}).module("clap");
- const lib = b.createModule(.{
- .root_source_file = b.path(lib_path),
- .imports = &.{.{ .name = "clap", .module = clap }},
- });
- try modules.appendSlice(&.{
- lib,
- clap,
- });
- break :blk try modules.toOwnedSlice();
- },
- .target = b.standardTargetOptions(.{}),
- .optimize = b.standardOptimizeOption(.{
- .preferred_optimize_mode = .ReleaseFast,
- }),
- .bin_path = b.pathJoin(&.{ "src", "bin" }),
- .lib_path = lib_path,
- };
- }
- fn add_run_step(self: Self, exe: Self.NamedExe) !void {
- var buf: [std.fs.MAX_NAME_BYTES * 4]u8 = undefined;
- var fba = std.heap.FixedBufferAllocator.init(&buf);
- const allocator = fba.allocator();
- const step = try std.mem.join(allocator, ":", &.{ "run", exe.name });
- const desc = try std.fmt.allocPrint(allocator, "Run command: `{s}`", .{exe.name});
- const b = self.build;
- const run_cmd = b.addRunArtifact(exe.exe);
- run_cmd.step.dependOn(b.getInstallStep());
- if (b.args) |args| {
- run_cmd.addArgs(args);
- }
- const run_step = b.step(step, desc);
- run_step.dependOn(&run_cmd.step);
- }
- fn do_exe(self: Self) !void {
- const b = self.build;
- const dir = try b.build_root.handle.openDir(self.bin_path, .{ .iterate = true });
- var it = dir.iterate();
- const exe_opts = b.addOptions();
- exe_opts.addOption(std.SemanticVersion, "version", VERSION);
- while (try it.next()) |bin| {
- const name = bin.name;
- if (std.mem.eql(u8, ".zig", std.fs.path.extension(name))) {
- const exe_name = std.fs.path.stem(name);
- const exe = b.addExecutable(.{
- .name = exe_name,
- .root_source_file = b.path(b.pathJoin(&.{ self.bin_path, name })),
- .target = self.target,
- .optimize = self.optimize,
- });
- exe.root_module.addOptions("build_options", exe_opts);
- for (self.modules) |mod| {
- const mod_name = std.fs.path.stem(mod.root_source_file.?.getDisplayName());
- exe.root_module.addImport(mod_name, mod);
- }
- b.installArtifact(exe);
- try self.add_run_step(.{ .name = exe_name, .exe = exe });
- }
- }
- }
- fn do_test(self: Self) !void {
- const b = self.build;
- const unit_tests = b.addTest(.{
- .root_source_file = b.path(self.lib_path),
- .target = self.target,
- .optimize = self.optimize,
- });
- const run_unit_tests = b.addRunArtifact(unit_tests);
- const test_step = b.step("test", "Run unit tests");
- test_step.dependOn(&run_unit_tests.step);
- }
- };
- pub fn build(b: *Build) !void {
- const ctx = try BuildCtx.init(b);
- try ctx.do_exe();
- try ctx.do_test();
- }
|