-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
build.zig
174 lines (155 loc) · 7.02 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
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
const builtin = @import("builtin");
const std = @import("std");
const fs = std.fs;
const log = std.log;
const tests = @import("test/test.zig");
const Allocator = std.mem.Allocator;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardOptimizeOption(.{});
const enable_logging = b.option(bool, "log", "Whether to enable logging") orelse (mode == .Debug);
const enable_tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source");
const tracy_callstack_depth = b.option(usize, "tracy-callstack-depth", "Set Tracy callstack depth") orelse 20;
const strip = b.option(bool, "strip", "Omit debug information") orelse blk: {
if (enable_tracy != null) break :blk false;
break :blk null;
};
const use_llvm = b.option(bool, "use-llvm", "Whether to use LLVM") orelse true;
const use_lld = if (builtin.os.tag == .macos) false else use_llvm;
const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
const single_threaded = b.option(bool, "single-threaded", "Force single-threaded") orelse false;
const yaml = b.dependency("zig-yaml", .{
.target = target,
.optimize = mode,
});
const dis_x86_64 = b.dependency("zig-dis-x86_64", .{
.target = target,
.optimize = mode,
});
const exe = b.addExecutable(.{
.name = "emerald",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = mode,
.use_llvm = use_llvm,
.use_lld = use_lld,
.sanitize_thread = sanitize_thread,
.single_threaded = single_threaded,
});
exe.root_module.addImport("yaml", yaml.module("yaml"));
exe.root_module.addImport("dis_x86_64", dis_x86_64.module("dis_x86_64"));
exe.root_module.strip = strip;
exe.linkLibC();
const exe_opts = b.addOptions();
exe.root_module.addOptions("build_options", exe_opts);
exe_opts.addOption(bool, "enable_logging", enable_logging);
exe_opts.addOption(bool, "enable_tracy", enable_tracy != null);
exe_opts.addOption(usize, "tracy_callstack_depth", tracy_callstack_depth);
if (enable_tracy) |tracy_path| {
const client_cpp = 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 == .gnu)
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
else
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
exe.addIncludePath(.{ .cwd_relative = tracy_path });
exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });
if (target.result.os.tag == .windows) {
exe.linkSystemLibrary("dbghelp");
exe.linkSystemLibrary("ws2_32");
}
}
const install = b.addInstallArtifact(exe, .{});
const symlinks = addSymlinks(b, install, &[_][]const u8{
"ld",
"ld.emerald",
"ld64.emerald",
"emerald-link.exe",
"wasm-emerald",
});
symlinks.step.dependOn(&install.step);
const system_compiler = b.option(tests.SystemCompiler, "system-compiler", "System compiler we are utilizing for tests: gcc, clang");
const has_static = b.option(bool, "has-static", "Whether the system compiler supports '-static' flag") orelse false;
const has_zig = b.option(bool, "has-zig", "Whether the Zig compiler is in path") orelse false;
const is_musl = b.option(bool, "musl", "Whether the tests are linked against musl libc") orelse false;
const has_objc_msgsend_stubs = b.option(bool, "has-objc-msgsend-stubs", "Whether the system compiler supports '-fobjc-msgsend-selector-stubs' flag") orelse false;
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/Ld.zig"),
.target = target,
.optimize = mode,
.use_llvm = use_llvm,
.use_lld = use_lld,
.sanitize_thread = sanitize_thread,
.single_threaded = single_threaded,
});
const unit_tests_opts = b.addOptions();
unit_tests.root_module.addOptions("build_options", unit_tests_opts);
unit_tests_opts.addOption(bool, "enable_logging", enable_logging);
unit_tests_opts.addOption(bool, "enable_tracy", enable_tracy != null);
unit_tests.root_module.addImport("yaml", yaml.module("yaml"));
unit_tests.root_module.addImport("dis_x86_64", dis_x86_64.module("dis_x86_64"));
unit_tests.linkLibC();
const test_step = b.step("test", "Run tests");
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
test_step.dependOn(tests.addTests(b, exe, .{
.system_compiler = system_compiler,
.has_static = has_static,
.has_zig = has_zig,
.is_musl = is_musl,
.has_objc_msgsend_stubs = has_objc_msgsend_stubs,
}));
}
fn addSymlinks(
builder: *std.Build,
install: *std.Build.Step.InstallArtifact,
names: []const []const u8,
) *CreateSymlinksStep {
const step = CreateSymlinksStep.create(builder, install, names);
builder.getInstallStep().dependOn(&step.step);
return step;
}
const CreateSymlinksStep = struct {
pub const base_id = .custom;
step: std.Build.Step,
builder: *std.Build,
install: *std.Build.Step.InstallArtifact,
targets: []const []const u8,
pub fn create(
builder: *std.Build,
install: *std.Build.Step.InstallArtifact,
targets: []const []const u8,
) *CreateSymlinksStep {
const self = builder.allocator.create(CreateSymlinksStep) catch unreachable;
self.* = CreateSymlinksStep{
.builder = builder,
.step = std.Build.Step.init(.{
.id = .custom,
.name = builder.fmt("symlinks to {s}", .{install.artifact.name}),
.owner = builder,
.makeFn = make,
}),
.install = install,
.targets = builder.dupeStrings(targets),
};
return self;
}
fn make(step: *std.Build.Step, prog_node: std.Progress.Node) anyerror!void {
const self: *CreateSymlinksStep = @fieldParentPtr("step", step);
const install_path = self.install.artifact.getEmittedBin().getPath(self.builder);
const rel_source = fs.path.basename(install_path);
var node = prog_node.start("creating symlinks", self.targets.len);
defer node.end();
for (self.targets, 0..) |target, i| {
const target_path = self.builder.getInstallPath(.bin, target);
fs.atomicSymLink(self.builder.allocator, rel_source, target_path) catch |err| {
log.err("Unable to symlink {s} -> {s}", .{ rel_source, target_path });
return err;
};
node.setCompletedItems(i + 1);
}
}
};