-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.zig
More file actions
439 lines (366 loc) · 15 KB
/
Copy pathbuild.zig
File metadata and controls
439 lines (366 loc) · 15 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
const std = @import("std");
const fs = std.fs;
// MAKE SURE TO USE zig build -Dwasm=false
pub fn build(b: *std.Build) void {
// Add an option to choose between WASM and native compilation
const target_wasm = b.option(bool, "wasm", "Build for WebAssembly target") orelse true;
const target = if (target_wasm)
// This might cause issues with the wasm build
b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.cpu_features_add = std.Target.wasm.featureSet(&.{
// See WASM options for later: https://github.com/ziglang/zig/blob/master/lib/std/Target/wasm.zig
.atomics,
.bulk_memory,
// TODO: Enable multi-value
}),
})
else
b.standardTargetOptions(.{});
// b.resolveTargetQuery(.{
// .cpu_arch = .x86_64,
// .os_tag = .linux,
// .abi = .gnu, // TODO: Find a way to not link against glibc
// });
// _ = target; // TODO: Add this back when I need wasm support
// const target = b.standardTargetOptions(.{});
// b.standardTargetOptions(.{}); // Use host system target for non-WASM build
// Choose the appropriate root source file based on target
const root_source = if (target_wasm)
b.path("main.zig")
else
b.path("native_main.zig");
const optimize = .ReleaseFast;
const lib = b.addExecutable(.{
.name = if (target_wasm) "riscv_wasm" else "riscv_native",
.root_source_file = root_source,
.target = target,
.link_libc = !target_wasm, // Link libc for native builds
// .strip = true, // Remove debug symbols
});
// lib.link_gc_sections = true;
// lib.verbose_link = true;
// lib.linkLibC();
// addUnicorn(b, lib);
addEmulatorAndSoftfloat(b, lib, target_wasm);
// Enable networking only for native builds (non-WASM) and non-macOS targets
// const target_os = target.result.os.tag;
// const enable_networking = target_wasm and target_os != .macos;
// if (enable_networking) {
addLwip(b, lib, target_wasm);
// }
// Add Tracy
const tracy = b.dependency("tracy", .{
.target = target,
.optimize = optimize,
});
lib.root_module.addImport("tracy", tracy.module("tracy"));
const tracy_enabled = b.option(
bool,
"tracy",
"Build with Tracy support.",
) orelse false;
if (tracy_enabled) {
// The user asked to enable Tracy, use the real implementation
lib.root_module.addImport("tracy_impl", tracy.module("tracy_impl_enabled"));
} else {
// The user asked to disable Tracy, use the dummy implementation
lib.root_module.addImport("tracy_impl", tracy.module("tracy_impl_disabled"));
}
b.reference_trace = 100;
lib.error_limit = 500;
// If is debug
if (!target_wasm and optimize == .Debug) {
// lib.root_module.sanitize_thread = true;
// lib.root_module.valgrind = true;
// lib.root_module.sanitize_c = true;
// lib.root_module.sanitize_thread = true;
// lib.root_module.sanitize_c = true;
// lib.addSanitize(.undefined); // Enable UBSan
// lib.addSanitize(.address); // Enable ASan
}
// WASM-specific settings - only apply when targeting WASM
if (target_wasm) {
lib.rdynamic = true;
lib.import_memory = true;
lib.shared_memory = true; // Enable shared memory support
lib.initial_memory = 16384 * 64 * 512; // 512MB in bytes
lib.max_memory = 16384 * 64 * 1024; // 1GB in bytes
lib.stack_size = 8 * 1024 * 1024; // 8MB
lib.entry = .disabled;
}
b.installArtifact(lib);
// Add a run step for the native build
if (!target_wasm) {
const run_cmd = b.addRunArtifact(lib);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the native executable");
run_step.dependOn(&run_cmd.step);
}
// For tests (zig build test -Dwasm=false)
// const test_step = b.addTest(.{ .root_source_file = b.path("test_file.zig"), .target = target, .optimize = .Debug, .link_libc = !target_wasm });
// test_step.addIncludePath(b.path("deps/unicorn/include"));
// test_step.addLibraryPath(b.path("deps/unicorn/build"));
// test_step.linkSystemLibrary("unicorn");
// test_step.linkLibC();
// const test_run = b.step("test", "Run tests with Unicorn");
// test_run.dependOn(&test_step.step);
// Add networking test
// const network_test = b.addExecutable(.{
// .name = "network_test",
// .root_source_file = b.path("network_test.zig"),
// .target = target,
// .optimize = .Debug,
// .link_libc = true,
// });
// // Add dependencies to the network test
// network_test.linkLibC();
// addSlirp(b, network_test);
// addUnicorn(b, network_test);
// addEmulatorAndSoftfloat(b, network_test);
// // Install the network test artifact
// b.installArtifact(network_test);
// // Create a run step for the network test
// const network_test_run = b.addRunArtifact(network_test);
// network_test_run.step.dependOn(&network_test.step);
// // Add a step to build and run the network test
// const network_test_step = b.step("test-network", "Run the networking test");
// network_test_step.dependOn(&network_test_run.step);
// Add specific step for libslirp test
// const slirp_test = b.addExecutable(.{
// .name = "slirp_test",
// .root_source_file = b.path("deps/libslirp/test.zig"),
// .target = b.standardTargetOptions(.{}),
// .optimize = .Debug,
// .link_libc = true,
// });
// addSlirpToTest(b, slirp_test);
// b.installArtifact(slirp_test);
// const slirp_test_run = b.addRunArtifact(slirp_test);
// slirp_test_run.step.dependOn(&slirp_test.step);
// const slirp_test_step = b.step("test-slirp", "Run the libslirp test");
// slirp_test_step.dependOn(&slirp_test_run.step);
// // Add TUN/TAP test
// const tuntap_test = b.addExecutable(.{
// .name = "tuntap_test",
// .root_source_file = b.path("test_tuntap.zig"),
// .target = target,
// .optimize = .Debug,
// .link_libc = !target_wasm,
// });
// // Add dependencies to the TUN/TAP test
// if (!target_wasm) {
// tuntap_test.linkLibC();
// addSlirp(b, tuntap_test);
// addUnicorn(b, tuntap_test);
// addEmulatorAndSoftfloat(b, tuntap_test);
// // Install the TUN/TAP test artifact
// b.installArtifact(tuntap_test);
// // Create a run step for the TUN/TAP test
// const tuntap_test_run = b.addRunArtifact(tuntap_test);
// tuntap_test_run.step.dependOn(&tuntap_test.step);
// // Add a step to build and run the TUN/TAP test
// const tuntap_test_step = b.step("test-tuntap", "Run the TUN/TAP test");
// tuntap_test_step.dependOn(&tuntap_test_run.step);
// }
// // Add simple TUN/TAP test
// const simple_tuntap_test = b.addExecutable(.{
// .name = "simple_tuntap_test",
// .root_source_file = b.path("simple_tuntap_test.zig"),
// .target = target,
// .optimize = .Debug,
// .link_libc = !target_wasm,
// });
// // Add dependencies to the simple TUN/TAP test
// if (!target_wasm) {
// simple_tuntap_test.linkLibC();
// addSlirp(b, simple_tuntap_test);
// addUnicorn(b, simple_tuntap_test);
// addEmulatorAndSoftfloat(b, simple_tuntap_test);
// // Install the simple TUN/TAP test artifact
// b.installArtifact(simple_tuntap_test);
// // Create a run step for the simple TUN/TAP test
// const simple_tuntap_test_run = b.addRunArtifact(simple_tuntap_test);
// simple_tuntap_test_run.step.dependOn(&simple_tuntap_test.step);
// // Add a step to build and run the simple TUN/TAP test
// const simple_tuntap_test_step = b.step("test-simple-tuntap", "Run the simple TUN/TAP test");
// simple_tuntap_test_step.dependOn(&simple_tuntap_test_run.step);
// }
}
// zig build -Dwasm=false
fn addUnicorn(b: *std.Build, lib: *std.Build.Step.Compile) void {
// Path to the expected output static library
const unicorn_lib_path = "deps/unicorn/build/libunicorn.a";
// Custom step: only build Unicorn if libunicorn.a is missing
const build_unicorn = b.addSystemCommand(&.{
"/bin/sh", "-c",
b.fmt("if [ ! -f {s} ]; then " ++
"cmake -B deps/unicorn/build -S deps/unicorn -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DUNICORN_BUILD_TESTS=OFF -DUNICORN_INSTALL=OFF -DUNICORN_ARCH=RISCV && " ++
"cmake --build deps/unicorn/build --config Release; fi", .{unicorn_lib_path}),
});
// Link to the locally built Unicorn static library
lib.addIncludePath(b.path("deps/unicorn/include"));
lib.addLibraryPath(b.path("deps/unicorn/build"));
lib.linkSystemLibrary("unicorn");
// Ensure riscv-test depends on the unicorn build step
lib.step.dependOn(&build_unicorn.step);
}
// Not in use anymore
fn addSlirp(b: *std.Build, lib: *std.Build.Step.Compile) void {
// Make sure libslirp is built using meson
const build_slirp = b.addSystemCommand(&.{
"/bin/sh", "-c",
"cd deps/libslirp && " ++
"if [ ! -f build/libslirp.so ]; then " ++
"meson setup build && " ++
"ninja -C build; fi",
});
// Add include paths for libslirp
lib.addIncludePath(b.path("deps/libslirp/src"));
lib.addIncludePath(b.path("deps/libslirp/build"));
// Link to libslirp
lib.addLibraryPath(b.path("deps/libslirp/build"));
lib.linkSystemLibrary("slirp");
// Directly link the static glib-2.0 from deps/glibc
// lib.addIncludePath(b.path("deps/glibc/include/glib-2.0"));
// lib.addIncludePath(b.path("deps/glibc/lib/glib-2.0/include"));
// lib.addLibraryPath(b.path("deps/glibc/lib"));
lib.linkSystemLibrary("glib-2.0");
// Ensure dependency on the libslirp build step
lib.step.dependOn(&build_slirp.step);
}
// fn addSlirpToTest(b: *std.Build, exe: *std.Build.Step.Compile) void {
// // Make sure libslirp is built using meson
// const build_slirp = b.addSystemCommand(&.{
// "/bin/sh", "-c",
// "cd deps/libslirp && " ++
// "if [ ! -f build/libslirp.so ]; then " ++
// "meson setup build && " ++
// "ninja -C build; fi",
// });
// // Add include paths for libslirp
// exe.addIncludePath(b.path("deps/libslirp/src"));
// exe.addIncludePath(b.path("deps/libslirp/build"));
// // Link to libslirp
// exe.addLibraryPath(b.path("deps/libslirp/build"));
// exe.linkSystemLibrary("slirp");
// exe.linkSystemLibrary("glib-2.0");
// // Ensure dependency on the libslirp build step
// exe.step.dependOn(&build_slirp.step);
// }
fn addEmulatorAndSoftfloat(b: *std.Build, lib: *std.Build.Step.Compile, is_wasm: bool) void {
const emu_module = b.createModule(.{
.root_source_file = b.path("./emu/emu.zig"),
});
// Add the emu module
lib.root_module.addImport("emu", emu_module);
// Define C flags for both compilation and linking
const softfloat_include_path = b.path("./deps/softfloat");
// Make sure the module's C imports can find the include path
lib.root_module.addIncludePath(softfloat_include_path);
// Also add it for the emu module (this needs to be at the root module level)
emu_module.addIncludePath(softfloat_include_path);
_ = is_wasm;
// if (!is_wasm) {
const c_files = findCSourceFiles(b, "./deps/softfloat") catch @panic("Failed to find C source files");
lib.addCSourceFiles(.{
.files = c_files,
.flags = &.{
"-nostdlib",
"-ffreestanding",
},
});
// }
}
fn findCSourceFiles(b: *std.Build, dir_path: []const u8) ![]const []const u8 {
var sources = std.ArrayList([]const u8).init(b.allocator);
var dir = try fs.cwd().openDir(dir_path, .{ .iterate = true });
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind == .file) {
const ext = std.fs.path.extension(entry.name);
if (std.mem.eql(u8, ext, ".c")) {
const full_path = try std.fs.path.join(b.allocator, &.{ dir_path, entry.name });
try sources.append(full_path);
}
}
}
return sources.toOwnedSlice();
}
fn addLwip(b: *std.Build, lib: *std.Build.Step.Compile, target_wasm: bool) void {
// lwIP source files
const lwip_core_sources = [_][]const u8{
"deps/lwip/src/core/init.c",
"deps/lwip/src/core/def.c",
"deps/lwip/src/core/dns.c",
"deps/lwip/src/core/inet_chksum.c",
"deps/lwip/src/core/ip.c",
"deps/lwip/src/core/mem.c",
"deps/lwip/src/core/memp.c",
"deps/lwip/src/core/netif.c",
"deps/lwip/src/core/pbuf.c",
"deps/lwip/src/core/raw.c",
"deps/lwip/src/core/stats.c",
"deps/lwip/src/core/sys.c",
"deps/lwip/src/core/altcp.c",
"deps/lwip/src/core/altcp_alloc.c",
"deps/lwip/src/core/altcp_tcp.c",
"deps/lwip/src/core/tcp.c",
"deps/lwip/src/core/tcp_in.c",
"deps/lwip/src/core/tcp_out.c",
"deps/lwip/src/core/timeouts.c",
"deps/lwip/src/core/udp.c",
};
const lwip_ipv4_sources = [_][]const u8{
"deps/lwip/src/core/ipv4/autoip.c",
"deps/lwip/src/core/ipv4/dhcp.c",
"deps/lwip/src/core/ipv4/etharp.c",
"deps/lwip/src/core/ipv4/icmp.c",
"deps/lwip/src/core/ipv4/igmp.c",
"deps/lwip/src/core/ipv4/ip4_frag.c",
"deps/lwip/src/core/ipv4/ip4.c",
"deps/lwip/src/core/ipv4/ip4_addr.c",
};
const lwip_netif_sources = [_][]const u8{
"deps/lwip/src/netif/ethernet.c",
};
// Combine all lwIP source files
const all_lwip_sources = lwip_core_sources ++ lwip_ipv4_sources ++ lwip_netif_sources;
// Choose appropriate compilation flags based on target
const lwip_flags = if (target_wasm)
&[_][]const u8{
"-DLWIP_NOASSERT",
"-DNO_SYS=1",
"-D__wasm32__",
"-DLWIP_NO_STDINT_H=1",
"-DLWIP_NO_INTTYPES_H=1",
"-DLWIP_NO_LIMITS_H=1",
"-std=c99",
"-Wno-unused-parameter",
"-Wno-unused-variable",
"-Wno-typedef-redefinition",
"-Wno-macro-redefined",
"-nostdlib",
"-ffreestanding",
}
else
&[_][]const u8{
"-DLWIP_NOASSERT",
"-DNO_SYS=1",
"-std=c99",
"-Wno-unused-parameter",
"-Wno-unused-variable",
"-Wno-typedef-redefinition",
"-Wno-macro-redefined",
};
// Add lwIP C source files
lib.addCSourceFiles(.{
.files = &all_lwip_sources,
.flags = lwip_flags,
});
// Add include directories
lib.addIncludePath(b.path("deps/lwip/include"));
// lib.addIncludePath(b.path("include")); // For lwipopts.h and other config files
}