This repository was archived by the owner on Dec 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuild.zig
More file actions
169 lines (139 loc) · 6.12 KB
/
Copy pathbuild.zig
File metadata and controls
169 lines (139 loc) · 6.12 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const std = @import("std");
const SemanticVersion = std.SemanticVersion;
const build_zon = @import("build.zig.zon");
const release_targets: []const std.Target.Query = &.{
.{ .os_tag = .linux, .cpu_arch = .x86 },
.{ .os_tag = .linux, .cpu_arch = .x86_64 },
.{ .os_tag = .linux, .cpu_arch = .aarch64 },
.{ .os_tag = .windows, .cpu_arch = .x86 },
.{ .os_tag = .windows, .cpu_arch = .x86_64 },
.{ .os_tag = .windows, .cpu_arch = .aarch64 },
.{ .os_tag = .macos, .cpu_arch = .x86_64 },
.{ .os_tag = .macos, .cpu_arch = .aarch64 },
};
fn getVersion(b: *std.Build) SemanticVersion {
if (b.option([]const u8, "force-version", "Force the version to a specific semver string")) |forced_ver| {
return SemanticVersion.parse(forced_ver) catch @panic("Unable to parse forced version string");
}
var version = SemanticVersion.parse(build_zon.version) catch @panic("Version parsing failed");
var code: u8 = undefined;
const git_version_cmd = std.mem.trim(u8, b.runAllowFail(&[_][]const u8{
"git",
"describe",
"--tags",
"--abbrev=10",
}, &code, .Ignore) catch {
version.pre = b.fmt("dev.{d}", .{b.option(usize, "version_commit_num", "The commit index since last release") orelse 0});
if (b.option([]const u8, "version_commit_id", "The commit ID")) |commit_id| {
version.build = commit_id;
}
return version;
}, "\n\r");
switch (std.mem.count(u8, git_version_cmd, "-")) {
0 => return version, // Here version == git_version_cmd
2 => {
var splitted = std.mem.splitScalar(u8, git_version_cmd, '-');
_ = splitted.first(); // Git tag
const commit_num = splitted.next() orelse @panic("Wrong `git describe` output!");
const commit_id = splitted.next() orelse @panic("Wrong `git describe` output!");
// The commit_id always starts with 'g' (for indicate that git was used), so we can skip it
return SemanticVersion{
.major = version.major,
.minor = version.minor,
.patch = version.patch,
.pre = b.fmt("dev.{s}", .{commit_num}),
.build = commit_id[1..],
};
},
else => {
version.pre = "dev";
return version;
},
}
}
fn addExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, pie: ?bool, release: bool, build_options: *std.Build.Step.Options, hevi_mod: *std.Build.Module) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = if (release) b.fmt("hevi-{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag) }) else "hevi",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
exe.pie = pie;
exe.root_module.addImport("hevi", hevi_mod);
exe.root_module.addOptions("build_options", build_options);
exe.root_module.addImport("ziggy", b.dependency("ziggy", .{}).module("ziggy"));
exe.root_module.addImport("pennant", b.dependency("pennant", .{}).module("pennant"));
return exe;
}
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");
var build_options = b.addOptions();
build_options.addOption(SemanticVersion, "version", getVersion(b));
const mod = b.addModule("hevi", .{
.root_source_file = b.path("src/hevi.zig"),
});
const exe = addExe(b, target, optimize, pie, false, build_options, mod);
b.installArtifact(exe);
const docs_step = b.step("docs", "Build the documentation");
const docs_obj = b.addObject(.{
.name = "hevi",
.root_module = b.createModule(.{
.root_source_file = b.path("src/hevi.zig"),
.target = target,
.optimize = .Debug,
}),
});
docs_obj.root_module.addOptions("build_options", build_options);
const docs = docs_obj.getEmittedDocs();
docs_step.dependOn(&b.addInstallDirectory(.{
.source_dir = docs,
.install_dir = .prefix,
.install_subdir = "docs",
}).step);
const web_step = b.step("web", "Build the whole web page");
const web_wf = b.addWriteFiles();
_ = web_wf.addCopyDirectory(b.path("web"), "", .{});
_ = web_wf.addCopyDirectory(docs, "docs", .{});
web_step.dependOn(&b.addInstallDirectory(.{
.source_dir = web_wf.getDirectory(),
.install_dir = .prefix,
.install_subdir = "web",
}).step);
const release_step = b.step("release", "Create release builds for all targets");
for (release_targets) |rt| {
const rexe = addExe(b, b.resolveTargetQuery(rt), .ReleaseSmall, null, true, build_options, mod);
release_step.dependOn(&b.addInstallArtifact(rexe, .{ .dest_sub_path = try std.fs.path.join(b.allocator, &.{ "release", rexe.out_filename }) }).step);
}
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
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_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
exe_unit_tests.root_module.addImport("hevi", mod);
exe_unit_tests.root_module.addOptions("build_options", build_options);
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const mod_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/hevi.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_mod_unit_tests = b.addRunArtifact(mod_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
test_step.dependOn(&run_mod_unit_tests.step);
}