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