an experimental irc client
1const std = @import("std");
2const zzdoc = @import("zzdoc");
3
4/// Must be kept in sync with git tags
5const comlink_version: std.SemanticVersion = .{ .major = 0, .minor = 1, .patch = 1 };
6
7pub fn build(b: *std.Build) void {
8 const target = b.standardTargetOptions(.{});
9 const optimize = b.standardOptimizeOption(.{});
10
11 const pie = b.option(bool, "pie", "Build a Position Independent Executable");
12
13 // manpages
14 {
15 var man_step = zzdoc.addManpageStep(b, .{
16 .root_doc_dir = b.path("docs/"),
17 });
18
19 const install_step = man_step.addInstallStep(.{});
20 b.default_step.dependOn(&install_step.step);
21 }
22
23 const ziglua_dep = b.dependency("lua_wrapper", .{
24 .target = target,
25 .optimize = optimize,
26 .lang = .lua54,
27 });
28
29 const tls_dep = b.dependency("tls", .{
30 .target = target,
31 .optimize = optimize,
32 });
33
34 const vaxis_dep = b.dependency("vaxis", .{
35 .target = target,
36 .optimize = optimize,
37 });
38
39 const zeit_dep = b.dependency("zeit", .{
40 .target = target,
41 .optimize = optimize,
42 });
43
44 const exe = b.addExecutable(.{
45 .name = "comlink",
46 .root_source_file = b.path("src/main.zig"),
47 .target = target,
48 .optimize = optimize,
49 });
50 exe.pie = pie;
51
52 const opts = b.addOptions();
53 const version_string = version(b) catch |err| {
54 std.debug.print("{}", .{err});
55 @compileError("couldn't get version");
56 };
57 opts.addOption([]const u8, "version", version_string);
58
59 exe.root_module.addOptions("build_options", opts);
60 exe.root_module.addImport("tls", tls_dep.module("tls"));
61 exe.root_module.addImport("ziglua", ziglua_dep.module("ziglua"));
62 exe.root_module.addImport("vaxis", vaxis_dep.module("vaxis"));
63 exe.root_module.addImport("zeit", zeit_dep.module("zeit"));
64
65 b.installArtifact(exe);
66 b.installFile("docs/comlink.lua", "share/comlink/lua/comlink.lua");
67 b.installFile("contrib/comlink.desktop", "share/applications/comlink.desktop");
68
69 const run_cmd = b.addRunArtifact(exe);
70 run_cmd.step.dependOn(b.getInstallStep());
71
72 // This allows the user to pass arguments to the application in the build
73 // command itself, like this: `zig build run -- arg1 arg2 etc`
74 if (b.args) |args| {
75 run_cmd.addArgs(args);
76 }
77 const run_step = b.step("run", "Run the app");
78 run_step.dependOn(&run_cmd.step);
79
80 const exe_unit_tests = b.addTest(.{
81 .root_source_file = b.path("src/main.zig"),
82 .target = target,
83 .optimize = optimize,
84 });
85 exe_unit_tests.root_module.addImport("vaxis", vaxis_dep.module("vaxis"));
86 exe_unit_tests.root_module.addImport("tls", tls_dep.module("tls"));
87 exe_unit_tests.root_module.addImport("zeit", zeit_dep.module("zeit"));
88 exe_unit_tests.root_module.addImport("ziglua", ziglua_dep.module("ziglua"));
89
90 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
91
92 const test_step = b.step("test", "Run unit tests");
93 test_step.dependOn(&run_exe_unit_tests.step);
94
95 {
96 // Add a check step for zls
97 const check_exe = b.addExecutable(.{
98 .name = "comlink",
99 .root_source_file = b.path("src/main.zig"),
100 .target = target,
101 .optimize = optimize,
102 .use_llvm = target.result.cpu.arch != .x86_64,
103 });
104 check_exe.root_module.addImport("vaxis", vaxis_dep.module("vaxis"));
105 check_exe.root_module.addImport("tls", tls_dep.module("tls"));
106 check_exe.root_module.addImport("zeit", zeit_dep.module("zeit"));
107 check_exe.root_module.addImport("ziglua", ziglua_dep.module("ziglua"));
108 check_exe.root_module.addOptions("build_options", opts);
109
110 const check_step = b.step("check", "Check if comlink compiles");
111 check_step.dependOn(&check_exe.step);
112 }
113}
114
115fn version(b: *std.Build) ![]const u8 {
116 if (!std.process.can_spawn) {
117 std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{});
118 std.process.exit(1);
119 }
120 const version_string = b.fmt("v{d}.{d}.{d}", .{ comlink_version.major, comlink_version.minor, comlink_version.patch });
121
122 var code: u8 = undefined;
123 const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{
124 "git",
125 "-C",
126 b.build_root.path orelse ".",
127 "describe",
128 "--tags",
129 "--abbrev=9",
130 }, &code, .Ignore) catch {
131 return version_string;
132 };
133 if (!std.mem.startsWith(u8, git_describe_untrimmed, version_string)) {
134 std.debug.print("error: tagged version does not match internal version\n", .{});
135 std.process.exit(1);
136 }
137 return std.mem.trim(u8, git_describe_untrimmed, " \n\r");
138}