-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.zig
More file actions
192 lines (163 loc) · 8.74 KB
/
build.zig
File metadata and controls
192 lines (163 loc) · 8.74 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const std = @import("std");
const mem = std.mem;
const zar_version = std.SemanticVersion{ .major = 0, .minor = 0, .patch = 1 };
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build`
// to choose what target to build for.
// Here we do not override the defaults, which means any target is
// allowed, and the default is native.
// Other options for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build`
// to select between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const optimize = b.standardOptimizeOption(.{});
const test_filter = b.option([]const u8, "test-filter", "Filter tests by name");
const exe_mod = b.addModule("zar", .{
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "src/main.zig" } },
.target = target,
.optimize = optimize,
});
const test_mod = b.addModule("tests", .{
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "src/test.zig" } },
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{ .name = "zar", .root_module = exe_mod, .version = zar_version });
var tests = b.addTest(.{
.root_module = test_mod,
.filters = if (test_filter != null) &.{test_filter.?} else &.{},
});
const build_test_executable_only = b.option(bool, "build-tests", "Build tests but don't run them.") orelse false;
const exe_options = b.addOptions();
exe_mod.addOptions("build_options", exe_options);
test_mod.addOptions("build_options", exe_options);
{
const test_errors_handled = b.option(bool, "test-errors-handled", "Compile with this to confirm zar sends all io errors through the io error handler") orelse false;
exe_options.addOption(bool, "test_errors_handled", test_errors_handled);
}
{
// see docs/llvm_ar_wrong_behaviour_cross_compiled.md for more info on this problem
// helps workaround it for testing purposes for now...
const mimmick_broken_cross_compiled_llvm_ar_behaviour = b.option(bool, "mimmick-broken-cross-compiled-llvm-ar-behaviour", "Workaround for the fact that cross-compiling zig at the moment result in llvm ar behaving on macOS as if it is running on any other OS") orelse false;
exe_options.addOption(bool, "mimmick_broken_cross_compiled_llvm_ar_behaviour", mimmick_broken_cross_compiled_llvm_ar_behaviour);
}
exe_options.addOption([]const u8, "zig_exe_path", b.graph.zig_exe);
// Taken from https://github.com/ziglang/zig/blob/master/build.zig, extract
// the git commit hash to get an actual version.
const version = v: {
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("{d}.{d}.{d}", .{ zar_version.major, zar_version.minor, zar_version.patch });
var code: u8 = undefined;
const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{
"git", "-C", b.build_root.path.?, "describe", "--match", "*.*.*", "--tags",
}, &code, .Ignore) catch {
break :v version_string;
};
const git_describe = mem.trim(u8, git_describe_untrimmed, " \n\r");
switch (mem.count(u8, git_describe, "-")) {
0 => {
// Tagged release version (e.g. 0.9.0).
if (!mem.eql(u8, git_describe, version_string)) {
std.debug.print("Zig version '{s}' does not match Git tag '{s}'\n", .{ version_string, git_describe });
std.process.exit(1);
}
break :v version_string;
},
2 => {
// Untagged development build (e.g. 0.9.0-dev.2025+ecf0050a9).
var it = mem.splitScalar(u8, git_describe, '-');
const tagged_ancestor = it.first();
const commit_height = it.next().?;
const commit_id = it.next().?;
const ancestor_ver = try std.SemanticVersion.parse(tagged_ancestor);
if (zar_version.order(ancestor_ver) != .gt) {
std.debug.print("Zig version '{f}' must be greater than tagged ancestor '{f}'\n", .{ zar_version, ancestor_ver });
std.process.exit(1);
}
// Check that the commit hash is prefixed with a 'g' (a Git convention).
if (commit_id.len < 1 or commit_id[0] != 'g') {
std.debug.print("Unexpected `git describe` output: {s}\n", .{git_describe});
break :v version_string;
}
// The version is reformatted in accordance with the https://semver.org specification.
break :v b.fmt("{s}-dev.{s}+{s}", .{ version_string, commit_height, commit_id[1..] });
},
else => {
std.debug.print("Unexpected `git describe` output: {s}\n", .{git_describe});
break :v version_string;
},
}
};
exe_options.addOption([]const u8, "version", version);
{
const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source");
const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse false;
const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse false;
exe_options.addOption(bool, "enable_tracy", tracy != null);
exe_options.addOption(bool, "enable_tracy_callstack", tracy_callstack);
exe_options.addOption(bool, "enable_tracy_allocation", tracy_allocation);
// "-DTRACY_NO_EXIT=1"
if (tracy) |tracy_path| {
const client_cpp = std.fs.path.join(
b.allocator,
&[_][]const u8{ tracy_path, "TracyClient.cpp" },
) catch unreachable;
// On mingw, we need to opt into windows 7+ to get some features required by tracy.
const tracy_c_flags: []const []const u8 =
if (target.result.os.tag == .windows and target.result.abi.isGnu())
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
else
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
const tracy_lazy_path: std.Build.LazyPath = .{ .src_path = .{ .owner = b, .sub_path = tracy_path } };
const client_cpp_lazy: std.Build.LazyPath = .{ .src_path = .{ .owner = b, .sub_path = client_cpp } };
exe_mod.addIncludePath(tracy_lazy_path);
exe_mod.addCSourceFile(.{ .file = client_cpp_lazy, .flags = tracy_c_flags });
exe.linkLibC();
exe.linkLibCpp();
tests.addIncludePath(tracy_lazy_path);
tests.addCSourceFile(.{ .file = client_cpp_lazy, .flags = tracy_c_flags });
tests.linkLibC();
tests.linkLibCpp();
if (target.result.os.tag == .windows) {
exe.linkSystemLibrary("dbghelp");
exe.linkSystemLibrary("ws2_32");
tests.linkSystemLibrary("dbghelp");
tests.linkSystemLibrary("ws2_32");
} else if (target.result.os.tag.isDarwin()) {
exe.linkFramework("CoreFoundation");
tests.linkFramework("CoreFoundation");
}
}
}
b.installArtifact(exe);
// There must be a better way to feed through the exe install path through to the build system...
{
var exe_name = exe.name;
if (target.result.os.tag == .windows) {
exe_name = "zar.exe";
}
const exe_path = try std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "bin", exe_name });
defer b.allocator.free(exe_path);
exe_options.addOption([]const u8, "zar_exe_path", exe_path);
}
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);
if (build_test_executable_only) {
const test_step = b.step("test", "Run tests");
b.installArtifact(tests);
test_step.dependOn(b.getInstallStep());
} else {
const test_step = b.step("test", "Run tests");
const run_tests = b.addRunArtifact(tests);
test_step.dependOn(b.getInstallStep());
test_step.dependOn(&run_tests.step);
}
}