-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.zig
263 lines (212 loc) · 9.54 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
fn addZigDeps(allocator: Allocator, step: anytype) !void {
// Handle reading zig-deps.nix output
// Open the file
const file = try std.fs.openFileAbsolute(std.os.getenv("ZIG_DEPS").?, .{ .mode = .read_only });
defer file.close();
// Read the contents
const max_buffer_size = 1_000_000;
const file_buffer = try file.readToEndAlloc(allocator, max_buffer_size);
defer allocator.free(file_buffer);
var parser = std.json.Parser.init(allocator, false);
defer parser.deinit();
var tree = parser.parse(file_buffer) catch @panic("failed to parse JSON");
defer tree.deinit();
var dep_iterator = tree.root.Object.iterator();
while (dep_iterator.next()) |dep| {
const dep_name = dep.key_ptr;
const dep_location = dep.value_ptr.String;
const dep_pkg = std.build.Pkg{
.name = dep_name.*,
.source = .{ .path = dep_location },
};
// std.debug.print("Adding pkg {s} {s}\n", .{ dep_name.*, dep_location });
step.addPackage(dep_pkg);
}
}
fn includeLibSystemFromNix(allocator: Allocator, l: anytype) anyerror!void {
var vars = try std.process.getEnvMap(allocator);
l.addIncludePath(vars.get("LIBSYSTEM_INCLUDE").?);
}
fn includeLibSystemFromNix2(allocator: Allocator, l: *std.build.TranslateCStep) anyerror!void {
var vars = try std.process.getEnvMap(allocator);
l.addIncludeDir(vars.get("LIBSYSTEM_INCLUDE").?);
}
fn includeProtobuf(allocator: Allocator, l: anytype) anyerror!void {
var vars = try std.process.getEnvMap(allocator);
l.addIncludePath(vars.get("PB_INCLUDE").?);
l.addIncludePath("./pb");
}
fn linkOpenssl(allocator: std.mem.Allocator, l: *std.build.LibExeObjStep) anyerror!void {
var vars = try std.process.getEnvMap(allocator);
const openssl_path = try std.fs.path.join(allocator, &.{ vars.get("LIB_OPENSSL").?, "/lib" });
const openssl_inc_path = try std.fs.path.join(allocator, &.{ vars.get("LIB_OPENSSL").?, "/include" });
l.addLibraryPath(openssl_path);
l.addIncludePath(openssl_inc_path);
l.linkSystemLibraryName("ssl");
l.linkSystemLibraryName("crypto");
}
fn maybePatchElf(allocator: Allocator, b: *std.build.Builder, os: std.Target.Os.Tag, step: *std.build.Step, filename: []const u8) !*std.build.Step {
const elf_interpreter = std.os.getenv("ELF_INTERPRETER") orelse "";
if (os == .linux and (elf_interpreter).len > 0) {
const path = try std.fmt.allocPrint(allocator, "./zig-out/bin/{s}", .{filename});
defer allocator.free(path);
const patchElf = b.addSystemCommand(&[_][]const u8{
"patchelf",
"--set-interpreter",
elf_interpreter,
path,
});
patchElf.step.dependOn(step);
return &patchElf.step;
} else {
return step;
}
}
fn addCryptoTestStep(allocator: std.mem.Allocator, b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, test_filter: []const u8) !void {
const tests = b.addTestExe("crypto-tests", "src/crypto.zig");
tests.setBuildMode(mode);
// Handle reading zig-deps.nix output
try addZigDeps(allocator, tests);
tests.filter = test_filter;
try linkOpenssl(allocator, tests);
try includeLibSystemFromNix(allocator, tests);
const os = target.os_tag orelse builtin.os.tag;
if (os == .linux) {
tests.linkLibC();
}
var install_test = b.addInstallArtifact(tests);
const tests_step = b.step("crypto-tests", "Run libp2p crypto tests");
tests_step.dependOn(try maybePatchElf(allocator, b, os, &install_test.step, tests.out_filename));
}
pub fn buildTests(b: *std.build.Builder, allocator: Allocator, mode: std.builtin.Mode, target: std.zig.CrossTarget, test_filter: []const u8) anyerror!void {
const msquic_builder = @import("./zig-msquic/build.zig");
const libp2p_test = b.addTestExe("libp2p-tests", "src/libp2p.zig");
libp2p_test.setTarget(target);
libp2p_test.setBuildMode(mode);
libp2p_test.filter = test_filter;
// Add packages and link
inline for (.{libp2p_test}) |step| {
try msquic_builder.linkMsquic(allocator, target, step, true);
try includeLibSystemFromNix(allocator, step);
step.addPackage(std.build.Pkg{
.name = "msquic",
.source = .{
.path = "zig-msquic/src/msquic.zig",
},
});
step.addPackage(std.build.Pkg{ .name = "libp2p-msquic", .source = .{
.path = "src/msquic.zig",
}, .dependencies = &[_]std.build.Pkg{.{
.name = "msquic",
.source = .{
.path = "zig-msquic/src/msquic.zig",
},
}} });
step.setBuildMode(mode);
}
const os = target.os_tag orelse builtin.os.tag;
const build_libp2p_test_step = b.step("libp2p-tests", "Build libp2p tests");
build_libp2p_test_step.dependOn(try maybePatchElf(allocator, b, os, &b.addInstallArtifact(libp2p_test).step, libp2p_test.out_filename));
const run_test_interop_step = b.step("run-libp2p-tests", "Run libp2ptests");
run_test_interop_step.dependOn(&libp2p_test.run().step);
}
pub fn addZigLibp2pPackages(allocator: Allocator, step: *std.build.LibExeObjStep, mode: std.builtin.Mode, target: std.zig.CrossTarget) anyerror!void {
const msquic_builder = @import("./zig-msquic/build.zig");
try msquic_builder.linkMsquic(allocator, target, step, true);
try includeLibSystemFromNix(allocator, step);
step.addPackage(std.build.Pkg{
.name = "msquic",
.source = .{
.path = "zig-msquic/src/msquic.zig",
},
});
step.addPackage(std.build.Pkg{
.name = "okredis",
.source = .{
.path = "interop/okredis/src/okredis.zig",
},
});
step.addPackage(std.build.Pkg{ .name = "libp2p-msquic", .source = .{
.path = "src/msquic.zig",
}, .dependencies = &[_]std.build.Pkg{.{
.name = "msquic",
.source = .{
.path = "zig-msquic/src/msquic.zig",
},
}} });
step.addPackage(std.build.Pkg{
.name = "libp2p",
.source = .{
.path = "src/libp2p.zig",
},
.dependencies = &[_]std.build.Pkg{ .{
.name = "libp2p-msquic",
.source = .{
.path = "src/msquic.zig",
},
.dependencies = &[_]std.build.Pkg{.{
.name = "msquic",
.source = .{
.path = "zig-msquic/src/msquic.zig",
},
}},
}, .{
.name = "msquic",
.source = .{
.path = "zig-msquic/src/msquic.zig",
},
} },
});
step.setBuildMode(mode);
}
pub fn buildPingExample(b: *std.build.Builder, allocator: Allocator, mode: std.builtin.Mode, target: std.zig.CrossTarget) anyerror!void {
const ping = b.addExecutable("ping", "examples/ping/main.zig");
ping.setTarget(target);
ping.setBuildMode(mode);
// Add packages and link
try addZigLibp2pPackages(allocator, ping, mode, target);
const os = target.os_tag orelse builtin.os.tag;
const ping_step = b.step("ping", "Build ping binary");
ping_step.dependOn(try maybePatchElf(allocator, b, os, &b.addInstallArtifact(ping).step, ping.out_filename));
// const run_interop_step = b.step("run-interop", "Run interop");
// run_interop_step.dependOn(&interop.run().step);
}
pub fn buildInterop(b: *std.build.Builder, allocator: Allocator, mode: std.builtin.Mode, target: std.zig.CrossTarget, test_filter: []const u8) anyerror!void {
const interop = b.addExecutable("interop", "interop/main.zig");
const interop_test = b.addTestExe("interop-test", "interop/main.zig");
interop_test.filter = test_filter;
interop.setTarget(target);
interop.setBuildMode(mode);
interop_test.setTarget(target);
interop_test.setBuildMode(mode);
// Add packages and link
inline for (.{ interop, interop_test }) |step| {
try addZigLibp2pPackages(allocator, step, mode, target);
}
const os = target.os_tag orelse builtin.os.tag;
const build_test_interop_step = b.step("interop-test", "Build interop self test");
build_test_interop_step.dependOn(try maybePatchElf(allocator, b, os, &b.addInstallArtifact(interop_test).step, interop_test.out_filename));
const run_test_interop_step = b.step("run-interop-test", "Run interop self test");
run_test_interop_step.dependOn(&interop_test.run().step);
const interop_step = b.step("interop", "Build interop binary");
interop_step.dependOn(try maybePatchElf(allocator, b, os, &b.addInstallArtifact(interop).step, interop.out_filename));
const run_interop_step = b.step("run-interop", "Run interop");
run_interop_step.dependOn(&interop.run().step);
}
pub fn build(b: *std.build.Builder) anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter") orelse "";
try addCryptoTestStep(allocator, b, mode, target, test_filter);
try buildInterop(b, allocator, mode, target, test_filter);
try buildPingExample(b, allocator, mode, target);
try buildTests(b, allocator, mode, target, test_filter);
}