-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
wasmserve.zig
1278 lines (1135 loc) · 47.4 KB
/
wasmserve.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const builtin = @import("builtin");
const mime = @import("mime.zig");
const net = std.net;
const mem = std.mem;
const fs = std.fs;
const build = std.build;
const www_dir_path = sdkPath("/www");
const buffer_size = 2048;
const esc = struct {
pub const reset = "\x1b[0m";
pub const bold = "\x1b[1m";
pub const underline = "\x1b[4m";
pub const red = "\x1b[31m";
pub const yellow = "\x1b[33m";
pub const cyan = "\x1b[36m";
pub const gray = "\x1b[90m";
};
pub const Options = struct {
install_dir: ?build.InstallDir = null,
watch_paths: ?[]const []const u8 = null,
listen_address: ?net.Address = null,
};
pub const Error = error{CannotOpenDirectory} || mem.Allocator.Error;
pub fn serve(step: *build.LibExeObjStep, options: Options) Error!*Wasmserve {
const self = try step.builder.allocator.create(Wasmserve);
const install_dir = options.install_dir orelse build.InstallDir{ .lib = {} };
const install_dir_iter = fs.cwd().makeOpenPathIterable(step.builder.getInstallPath(install_dir, ""), .{}) catch
return error.CannotOpenDirectory;
self.* = Wasmserve{
.step = build.Step.init(.run, "wasmserve", step.builder.allocator, Wasmserve.make),
.b = step.builder,
.exe_step = step,
.install_dir = install_dir,
.install_dir_iter = install_dir_iter,
.address = options.listen_address orelse net.Address.initIp4([4]u8{ 127, 0, 0, 1 }, 8080),
.subscriber = null,
.watch_paths = options.watch_paths orelse &.{step.root_src.?.path},
.mtimes = std.AutoHashMap(fs.File.INode, i128).init(step.builder.allocator),
.notify_msg = null,
};
return self;
}
const Wasmserve = struct {
step: build.Step,
b: *build.Builder,
exe_step: *build.LibExeObjStep,
install_dir: build.InstallDir,
install_dir_iter: fs.IterableDir,
address: net.Address,
subscriber: ?*net.StreamServer.Connection,
watch_paths: []const []const u8,
mtimes: std.AutoHashMap(fs.File.INode, i128),
notify_msg: ?NotifyMessage,
const NotifyMessage = struct {
const Event = enum {
built,
build_error,
stopped,
};
event: Event,
data: []const u8,
};
pub fn make(step: *build.Step) !void {
const self = @fieldParentPtr(Wasmserve, "step", step);
self.compile();
std.debug.assert(mem.eql(u8, fs.path.extension(self.exe_step.out_filename), ".wasm"));
var www_dir = try fs.cwd().openIterableDir(www_dir_path, .{});
defer www_dir.close();
var www_dir_iter = www_dir.iterate();
while (try www_dir_iter.next()) |file| {
const path = try fs.path.join(self.b.allocator, &.{ www_dir_path, file.name });
defer self.b.allocator.free(path);
const install_www = self.b.addInstallFileWithDir(
.{ .path = path },
self.install_dir,
file.name,
);
try install_www.step.make();
}
const watch_thread = try std.Thread.spawn(.{}, watch, .{self});
defer watch_thread.detach();
try self.runServer();
}
fn runServer(self: *Wasmserve) !void {
var server = net.StreamServer.init(.{ .reuse_address = true });
defer server.deinit();
try server.listen(self.address);
var addr_buf = @as([45]u8, undefined);
var fbs = std.io.fixedBufferStream(&addr_buf);
if (self.address.format("", .{}, fbs.writer())) {
std.log.info("Started listening at " ++ esc.cyan ++ esc.underline ++ "http://{s}" ++ esc.reset ++ "...", .{fbs.getWritten()});
} else |err| logErr(err, @src());
while (server.accept()) |conn| {
self.respond(conn) catch |err| {
logErr(err, @src());
continue;
};
} else |err| logErr(err, @src());
}
fn respond(self: *Wasmserve, conn: net.StreamServer.Connection) !void {
errdefer respondError(conn.stream, 500, "Internal Server Error") catch |err| logErr(err, @src());
var recv_buf: [buffer_size]u8 = undefined;
const first_line = conn.stream.reader().readUntilDelimiter(&recv_buf, '\n') catch |err| {
switch (err) {
error.StreamTooLong => try respondError(conn.stream, 414, "Too Long Request"),
else => try respondError(conn.stream, 400, "Bad Request"),
}
return;
};
var first_line_iter = mem.split(u8, first_line, " ");
_ = first_line_iter.next(); // skip method
if (first_line_iter.next()) |uri| {
if (uri[0] != '/') {
try respondError(conn.stream, 400, "Bad Request");
return;
}
const url = dropFragment(uri)[1..];
if (mem.eql(u8, url, "notify")) {
_ = try conn.stream.write("HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/event-stream\r\nCache-Control: No-Cache\r\n\r\n");
self.subscriber = try self.b.allocator.create(net.StreamServer.Connection);
self.subscriber.?.* = conn;
if (self.notify_msg) |msg|
if (msg.event != .built)
self.notify();
return;
}
if (self.researchPath(url)) |file_path| {
try self.respondFile(conn.stream, file_path);
return;
} else |_| {}
try respondError(conn.stream, 404, "Not Found");
} else {
try respondError(conn.stream, 400, "Bad Request");
}
}
fn respondFile(self: Wasmserve, stream: net.Stream, path: []const u8) !void {
const ext = fs.path.extension(path);
var file_mime: []const u8 = "text/plain";
inline for (mime.mime_list) |entry| {
for (entry.ext) |ext_entry| {
if (std.mem.eql(u8, ext, ext_entry))
file_mime = entry.mime;
}
}
const file = try self.install_dir_iter.dir.openFile(path, .{});
defer file.close();
const file_size = try file.getEndPos();
try stream.writer().print(
"HTTP/1.1 200 OK\r\n" ++
"Connection: close\r\n" ++
"Content-Length: {d}\r\n" ++
"Content-Type: {s}\r\n" ++
"\r\n",
.{ file_size, file_mime },
);
try fs.File.writeFileAll(.{ .handle = stream.handle }, file, .{});
}
fn respondError(stream: net.Stream, code: u32, desc: []const u8) !void {
try stream.writer().print(
"HTTP/1.1 {d} {s}\r\n" ++
"Connection: close\r\n" ++
"Content-Length: {d}\r\n" ++
"Content-Type: text/html\r\n" ++
"\r\n<!DOCTYPE html><html><body><h1>{s}</h1></body></html>",
.{ code, desc, desc.len + 50, desc },
);
}
fn researchPath(self: Wasmserve, path: []const u8) ![]const u8 {
var walker = try self.install_dir_iter.walk(self.b.allocator);
defer walker.deinit();
while (try walker.next()) |walk_entry| {
if (walk_entry.kind != .File) continue;
if (mem.eql(u8, walk_entry.path, path) or (path.len == 0 and mem.eql(u8, walk_entry.path, "index.html")))
return try self.b.allocator.dupe(u8, walk_entry.path);
}
return error.FileNotFound;
}
fn watch(self: *Wasmserve) void {
timer_loop: while (true) : (std.time.sleep(500 * std.time.ns_per_ms)) {
for (self.watch_paths) |path| {
var dir = fs.cwd().openIterableDir(path, .{}) catch continue;
defer dir.close();
var walker = dir.walk(self.b.allocator) catch |err| {
logErr(err, @src());
continue;
};
defer walker.deinit();
while (walker.next() catch |err| {
logErr(err, @src());
continue;
}) |walk_entry| {
if (walk_entry.kind != .File) continue;
if (self.checkForUpdate(dir.dir, walk_entry.path)) |is_updated| {
if (is_updated)
continue :timer_loop;
} else |err| {
logErr(err, @src());
continue;
}
}
}
}
}
fn checkForUpdate(self: *Wasmserve, p_dir: fs.Dir, path: []const u8) !bool {
const stat = try p_dir.statFile(path);
const entry = try self.mtimes.getOrPut(stat.inode);
if (entry.found_existing and stat.mtime > entry.value_ptr.*) {
std.log.info(esc.yellow ++ esc.underline ++ "{s}" ++ esc.reset ++ " updated", .{path});
self.compile();
entry.value_ptr.* = stat.mtime;
return true;
}
entry.value_ptr.* = stat.mtime;
return false;
}
fn notify(self: *Wasmserve) void {
if (self.subscriber) |s| {
if (self.notify_msg) |msg| {
s.stream.writer().print("event: {s}\n", .{@tagName(msg.event)}) catch |err| logErr(err, @src());
var lines = std.mem.split(u8, msg.data, "\n");
while (lines.next()) |line|
s.stream.writer().print("data: {s}\n", .{line}) catch |err| logErr(err, @src());
_ = s.stream.write("\n") catch |err| logErr(err, @src());
}
}
}
fn compile(self: *Wasmserve) void {
std.log.info("Building...", .{});
const argv = getExecArgs(self.exe_step) catch |err| {
logErr(err, @src());
return;
};
defer self.b.allocator.free(argv);
var res = std.ChildProcess.exec(.{ .argv = argv, .allocator = self.b.allocator }) catch |err| {
logErr(err, @src());
return;
};
defer self.b.allocator.free(res.stdout);
if (self.notify_msg) |msg|
if (msg.event == .build_error)
self.b.allocator.free(msg.data);
switch (res.term) {
.Exited => |code| {
if (code == 0) {
std.log.info("Built", .{});
self.notify_msg = .{
.event = .built,
.data = "",
};
} else {
std.log.err("Compile error", .{});
self.notify_msg = .{
.event = .build_error,
.data = res.stderr,
};
}
},
.Signal, .Stopped, .Unknown => {
std.log.err("The build process has stopped unexpectedly", .{});
self.notify_msg = .{
.event = .stopped,
.data = "",
};
},
}
std.io.getStdErr().writeAll(res.stderr) catch |err| logErr(err, @src());
self.notify();
}
};
fn dropFragment(input: []const u8) []const u8 {
for (input) |c, i|
if (c == '?' or c == '#')
return input[0..i];
return input;
}
fn logErr(err: anyerror, src: std.builtin.SourceLocation) void {
if (@errorReturnTrace()) |bt| {
std.log.err(esc.red ++ esc.bold ++ "{s}" ++ esc.reset ++ " >>>\n{s}", .{ @errorName(err), bt });
} else {
var file_name_buf: [1024]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&file_name_buf);
const allocator = fba.allocator();
const file_path = fs.path.relative(allocator, ".", src.file) catch @as([]const u8, src.file);
std.log.err(esc.red ++ esc.bold ++ "{s}" ++ esc.reset ++
" at " ++ esc.underline ++ "{s}:{d}:{d}" ++ esc.reset ++
esc.gray ++ " fn {s}()" ++ esc.reset, .{
@errorName(err),
file_path,
src.line,
src.column,
src.fn_name,
});
}
}
fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
// copied from LibExeObjStep.make()
// TODO: this is very tricky
fn getExecArgs(self: *build.LibExeObjStep) ![]const []const u8 {
const builder = self.builder;
if (self.root_src == null and self.link_objects.items.len == 0) {
std.log.err("{s}: linker needs 1 or more objects to link", .{self.step.name});
return error.NeedAnObject;
}
var zig_args = std.ArrayList([]const u8).init(builder.allocator);
defer zig_args.deinit();
zig_args.append(builder.zig_exe) catch unreachable;
const cmd = switch (self.kind) {
.lib => "build-lib",
.exe => "build-exe",
.obj => "build-obj",
.@"test" => "test",
.test_exe => "test",
};
zig_args.append(cmd) catch unreachable;
if (builder.color != .auto) {
try zig_args.append("--color");
try zig_args.append(@tagName(builder.color));
}
if (builder.reference_trace) |some| {
try zig_args.append(try std.fmt.allocPrint(builder.allocator, "-freference-trace={d}", .{some}));
}
if (self.use_llvm) |use_llvm| {
if (use_llvm) {
try zig_args.append("-fLLVM");
} else {
try zig_args.append("-fno-LLVM");
}
}
if (self.use_lld) |use_lld| {
if (use_lld) {
try zig_args.append("-fLLD");
} else {
try zig_args.append("-fno-LLD");
}
}
if (self.target.ofmt) |ofmt| {
try zig_args.append(try std.fmt.allocPrint(builder.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
}
if (self.entry_symbol_name) |entry| {
try zig_args.append("--entry");
try zig_args.append(entry);
}
if (self.stack_size) |stack_size| {
try zig_args.append("--stack");
try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));
}
if (self.root_src) |root_src| try zig_args.append(root_src.getPath(builder));
var prev_has_extra_flags = false;
// Resolve transitive dependencies
{
var transitive_dependencies = std.ArrayList(std.build.LibExeObjStep.LinkObject).init(builder.allocator);
defer transitive_dependencies.deinit();
for (self.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |other| {
// Inherit dependency on system libraries
for (other.link_objects.items) |other_link_object| {
switch (other_link_object) {
.system_lib => try transitive_dependencies.append(other_link_object),
else => continue,
}
}
// Inherit dependencies on darwin frameworks
if (!other.isDynamicLibrary()) {
var it = other.frameworks.iterator();
while (it.next()) |framework| {
self.frameworks.put(framework.key_ptr.*, framework.value_ptr.*) catch unreachable;
}
}
},
else => continue,
}
}
try self.link_objects.appendSlice(transitive_dependencies.items);
}
for (self.link_objects.items) |link_object| {
switch (link_object) {
.static_path => |static_path| try zig_args.append(static_path.getPath(builder)),
.other_step => |other| switch (other.kind) {
.exe => @panic("Cannot link with an executable build artifact"),
.test_exe => @panic("Cannot link with an executable build artifact"),
.@"test" => @panic("Cannot link with a test"),
.obj => {
try zig_args.append(other.getOutputSource().getPath(builder));
},
.lib => {
const full_path_lib = other.getOutputLibSource().getPath(builder);
try zig_args.append(full_path_lib);
if (other.linkage != null and other.linkage.? == .dynamic and !self.target.isWindows()) {
if (fs.path.dirname(full_path_lib)) |dirname| {
try zig_args.append("-rpath");
try zig_args.append(dirname);
}
}
},
},
.system_lib => |system_lib| {
const prefix: []const u8 = prefix: {
if (system_lib.needed) break :prefix "-needed-l";
if (system_lib.weak) {
if (self.target.isDarwin()) break :prefix "-weak-l";
std.log.warn("Weak library import used for a non-darwin target, this will be converted to normally library import `-lname`", .{});
}
break :prefix "-l";
};
switch (system_lib.use_pkg_config) {
.no => try zig_args.append(builder.fmt("{s}{s}", .{ prefix, system_lib.name })),
.yes, .force => {
if (self.runPkgConfig(system_lib.name)) |args| {
try zig_args.appendSlice(args);
} else |err| switch (err) {
error.PkgConfigInvalidOutput,
error.PkgConfigCrashed,
error.PkgConfigFailed,
error.PkgConfigNotInstalled,
error.PackageNotFound,
=> switch (system_lib.use_pkg_config) {
.yes => {
// pkg-config failed, so fall back to linking the library
// by name directly.
try zig_args.append(builder.fmt("{s}{s}", .{
prefix,
system_lib.name,
}));
},
.force => {
std.debug.panic("pkg-config failed for library {s}", .{system_lib.name});
},
.no => unreachable,
},
else => |e| return e,
}
},
}
},
.assembly_file => |asm_file| {
if (prev_has_extra_flags) {
try zig_args.append("-extra-cflags");
try zig_args.append("--");
prev_has_extra_flags = false;
}
try zig_args.append(asm_file.getPath(builder));
},
.c_source_file => |c_source_file| {
if (c_source_file.args.len == 0) {
if (prev_has_extra_flags) {
try zig_args.append("-cflags");
try zig_args.append("--");
prev_has_extra_flags = false;
}
} else {
try zig_args.append("-cflags");
for (c_source_file.args) |arg| {
try zig_args.append(arg);
}
try zig_args.append("--");
}
try zig_args.append(c_source_file.source.getPath(builder));
},
.c_source_files => |c_source_files| {
if (c_source_files.flags.len == 0) {
if (prev_has_extra_flags) {
try zig_args.append("-cflags");
try zig_args.append("--");
prev_has_extra_flags = false;
}
} else {
try zig_args.append("-cflags");
for (c_source_files.flags) |flag| {
try zig_args.append(flag);
}
try zig_args.append("--");
}
for (c_source_files.files) |file| {
try zig_args.append(builder.pathFromRoot(file));
}
},
}
}
if (self.image_base) |image_base| {
try zig_args.append("--image-base");
try zig_args.append(builder.fmt("0x{x}", .{image_base}));
}
if (self.filter) |filter| {
try zig_args.append("--test-filter");
try zig_args.append(filter);
}
if (self.test_evented_io) {
try zig_args.append("--test-evented-io");
}
if (self.name_prefix.len != 0) {
try zig_args.append("--test-name-prefix");
try zig_args.append(self.name_prefix);
}
if (self.test_runner) |test_runner| {
try zig_args.append("--test-runner");
try zig_args.append(builder.pathFromRoot(test_runner));
}
for (builder.debug_log_scopes) |log_scope| {
try zig_args.append("--debug-log");
try zig_args.append(log_scope);
}
if (builder.debug_compile_errors) {
try zig_args.append("--debug-compile-errors");
}
if (builder.verbose_cimport) zig_args.append("--verbose-cimport") catch unreachable;
if (builder.verbose_air) zig_args.append("--verbose-air") catch unreachable;
if (builder.verbose_llvm_ir) zig_args.append("--verbose-llvm-ir") catch unreachable;
if (builder.verbose_link or self.verbose_link) zig_args.append("--verbose-link") catch unreachable;
if (builder.verbose_cc or self.verbose_cc) zig_args.append("--verbose-cc") catch unreachable;
if (builder.verbose_llvm_cpu_features) zig_args.append("--verbose-llvm-cpu-features") catch unreachable;
if (self.emit_h) try zig_args.append("-femit-h");
if (self.strip) |strip| {
if (strip) {
try zig_args.append("-fstrip");
} else {
try zig_args.append("-fno-strip");
}
}
if (self.unwind_tables) |unwind_tables| {
if (unwind_tables) {
try zig_args.append("-funwind-tables");
} else {
try zig_args.append("-fno-unwind-tables");
}
}
switch (self.compress_debug_sections) {
.none => {},
.zlib => try zig_args.append("--compress-debug-sections=zlib"),
}
if (self.link_eh_frame_hdr) {
try zig_args.append("--eh-frame-hdr");
}
if (self.link_emit_relocs) {
try zig_args.append("--emit-relocs");
}
if (self.link_function_sections) {
try zig_args.append("-ffunction-sections");
}
if (self.link_gc_sections) |x| {
try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
}
if (self.linker_allow_shlib_undefined) |x| {
try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined");
}
if (self.link_z_notext) {
try zig_args.append("-z");
try zig_args.append("notext");
}
if (!self.link_z_relro) {
try zig_args.append("-z");
try zig_args.append("norelro");
}
if (self.link_z_lazy) {
try zig_args.append("-z");
try zig_args.append("lazy");
}
if (self.link_z_common_page_size) |size| {
try zig_args.append("-z");
try zig_args.append(builder.fmt("common-page-size={d}", .{size}));
}
if (self.link_z_max_page_size) |size| {
try zig_args.append("-z");
try zig_args.append(builder.fmt("max-page-size={d}", .{size}));
}
if (self.libc_file) |libc_file| {
try zig_args.append("--libc");
try zig_args.append(libc_file.getPath(self.builder));
} else if (builder.libc_file) |libc_file| {
try zig_args.append("--libc");
try zig_args.append(libc_file);
}
switch (self.build_mode) {
.Debug => {}, // Skip since it's the default.
else => zig_args.append(builder.fmt("-O{s}", .{@tagName(self.build_mode)})) catch unreachable,
}
try zig_args.append("--cache-dir");
try zig_args.append(builder.pathFromRoot(builder.cache_root));
try zig_args.append("--global-cache-dir");
try zig_args.append(builder.pathFromRoot(builder.global_cache_root));
zig_args.append("--name") catch unreachable;
zig_args.append(self.name) catch unreachable;
if (self.linkage) |some| switch (some) {
.dynamic => try zig_args.append("-dynamic"),
.static => try zig_args.append("-static"),
};
if (self.kind == .lib and self.linkage != null and self.linkage.? == .dynamic) {
if (self.version) |version| {
zig_args.append("--version") catch unreachable;
zig_args.append(builder.fmt("{}", .{version})) catch unreachable;
}
if (self.target.isDarwin()) {
const install_name = self.install_name orelse builder.fmt("@rpath/{s}{s}{s}", .{
self.target.libPrefix(),
self.name,
self.target.dynamicLibSuffix(),
});
try zig_args.append("-install_name");
try zig_args.append(install_name);
}
}
if (self.entitlements) |entitlements| {
try zig_args.appendSlice(&[_][]const u8{ "--entitlements", entitlements });
}
if (self.pagezero_size) |pagezero_size| {
const size = try std.fmt.allocPrint(builder.allocator, "{x}", .{pagezero_size});
try zig_args.appendSlice(&[_][]const u8{ "-pagezero_size", size });
}
if (self.search_strategy) |strat| switch (strat) {
.paths_first => try zig_args.append("-search_paths_first"),
.dylibs_first => try zig_args.append("-search_dylibs_first"),
};
if (self.headerpad_size) |headerpad_size| {
const size = try std.fmt.allocPrint(builder.allocator, "{x}", .{headerpad_size});
try zig_args.appendSlice(&[_][]const u8{ "-headerpad", size });
}
if (self.headerpad_max_install_names) {
try zig_args.append("-headerpad_max_install_names");
}
if (self.dead_strip_dylibs) {
try zig_args.append("-dead_strip_dylibs");
}
if (self.bundle_compiler_rt) |x| {
if (x) {
try zig_args.append("-fcompiler-rt");
} else {
try zig_args.append("-fno-compiler-rt");
}
}
if (self.single_threaded) |single_threaded| {
if (single_threaded) {
try zig_args.append("-fsingle-threaded");
} else {
try zig_args.append("-fno-single-threaded");
}
}
if (self.disable_stack_probing) {
try zig_args.append("-fno-stack-check");
}
if (self.stack_protector) |stack_protector| {
if (stack_protector) {
try zig_args.append("-fstack-protector");
} else {
try zig_args.append("-fno-stack-protector");
}
}
if (self.red_zone) |red_zone| {
if (red_zone) {
try zig_args.append("-mred-zone");
} else {
try zig_args.append("-mno-red-zone");
}
}
if (self.omit_frame_pointer) |omit_frame_pointer| {
if (omit_frame_pointer) {
try zig_args.append("-fomit-frame-pointer");
} else {
try zig_args.append("-fno-omit-frame-pointer");
}
}
if (self.dll_export_fns) |dll_export_fns| {
if (dll_export_fns) {
try zig_args.append("-fdll-export-fns");
} else {
try zig_args.append("-fno-dll-export-fns");
}
}
if (self.disable_sanitize_c) {
try zig_args.append("-fno-sanitize-c");
}
if (self.sanitize_thread) {
try zig_args.append("-fsanitize-thread");
}
if (self.rdynamic) {
try zig_args.append("-rdynamic");
}
if (self.import_memory) {
try zig_args.append("--import-memory");
}
if (self.import_symbols) {
try zig_args.append("--import-symbols");
}
if (self.import_table) {
try zig_args.append("--import-table");
}
if (self.export_table) {
try zig_args.append("--export-table");
}
if (self.initial_memory) |initial_memory| {
try zig_args.append(builder.fmt("--initial-memory={d}", .{initial_memory}));
}
if (self.max_memory) |max_memory| {
try zig_args.append(builder.fmt("--max-memory={d}", .{max_memory}));
}
if (self.shared_memory) {
try zig_args.append("--shared-memory");
}
if (self.global_base) |global_base| {
try zig_args.append(builder.fmt("--global-base={d}", .{global_base}));
}
if (self.code_model != .default) {
try zig_args.append("-mcmodel");
try zig_args.append(@tagName(self.code_model));
}
if (self.wasi_exec_model) |model| {
try zig_args.append(builder.fmt("-mexec-model={s}", .{@tagName(model)}));
}
for (self.export_symbol_names) |symbol_name| {
try zig_args.append(builder.fmt("--export={s}", .{symbol_name}));
}
if (!self.target.isNative()) {
try zig_args.append("-target");
try zig_args.append(try self.target.zigTriple(builder.allocator));
// TODO this logic can disappear if cpu model + features becomes part of the target triple
const cross = self.target.toTarget();
const all_features = cross.cpu.arch.allFeaturesList();
var populated_cpu_features = cross.cpu.model.features;
populated_cpu_features.populateDependencies(all_features);
if (populated_cpu_features.eql(cross.cpu.features)) {
// The CPU name alone is sufficient.
try zig_args.append("-mcpu");
try zig_args.append(cross.cpu.model.name);
} else {
var mcpu_buffer = std.ArrayList(u8).init(builder.allocator);
try mcpu_buffer.writer().print("-mcpu={s}", .{cross.cpu.model.name});
for (all_features) |feature, i_usize| {
const i = @intCast(std.Target.Cpu.Feature.Set.Index, i_usize);
const in_cpu_set = populated_cpu_features.isEnabled(i);
const in_actual_set = cross.cpu.features.isEnabled(i);
if (in_cpu_set and !in_actual_set) {
try mcpu_buffer.writer().print("-{s}", .{feature.name});
} else if (!in_cpu_set and in_actual_set) {
try mcpu_buffer.writer().print("+{s}", .{feature.name});
}
}
try zig_args.append(try mcpu_buffer.toOwnedSlice());
}
if (self.target.dynamic_linker.get()) |dynamic_linker| {
try zig_args.append("--dynamic-linker");
try zig_args.append(dynamic_linker);
}
}
if (self.linker_script) |linker_script| {
try zig_args.append("--script");
try zig_args.append(linker_script.getPath(builder));
}
if (self.version_script) |version_script| {
try zig_args.append("--version-script");
try zig_args.append(builder.pathFromRoot(version_script));
}
if (self.kind == .@"test") {
if (self.exec_cmd_args) |exec_cmd_args| {
for (exec_cmd_args) |cmd_arg| {
if (cmd_arg) |arg| {
try zig_args.append("--test-cmd");
try zig_args.append(arg);
} else {
try zig_args.append("--test-cmd-bin");
}
}
} else {
const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;
switch (self.builder.host.getExternalExecutor(self.target_info, .{
.qemu_fixes_dl = need_cross_glibc and builder.glibc_runtimes_dir != null,
.link_libc = self.is_linking_libc,
})) {
.native => {},
.bad_dl, .bad_os_or_cpu => {
try zig_args.append("--test-no-exec");
},
.rosetta => if (builder.enable_rosetta) {
try zig_args.append("--test-cmd-bin");
} else {
try zig_args.append("--test-no-exec");
},
.qemu => |bin_name| ok: {
if (builder.enable_qemu) qemu: {
const glibc_dir_arg = if (need_cross_glibc)
builder.glibc_runtimes_dir orelse break :qemu
else
null;
try zig_args.append("--test-cmd");
try zig_args.append(bin_name);
if (glibc_dir_arg) |dir| {
// TODO look into making this a call to `linuxTriple`. This
// needs the directory to be called "i686" rather than
// "x86" which is why we do it manually here.
const fmt_str = "{s}" ++ fs.path.sep_str ++ "{s}-{s}-{s}";
const cpu_arch = self.target.getCpuArch();
const os_tag = self.target.getOsTag();
const abi = self.target.getAbi();
const cpu_arch_name: []const u8 = if (cpu_arch == .x86)
"i686"
else
@tagName(cpu_arch);
const full_dir = try std.fmt.allocPrint(builder.allocator, fmt_str, .{
dir, cpu_arch_name, @tagName(os_tag), @tagName(abi),
});
try zig_args.append("--test-cmd");
try zig_args.append("-L");
try zig_args.append("--test-cmd");
try zig_args.append(full_dir);
}
try zig_args.append("--test-cmd-bin");
break :ok;
}
try zig_args.append("--test-no-exec");
},
.wine => |bin_name| if (builder.enable_wine) {
try zig_args.append("--test-cmd");
try zig_args.append(bin_name);
try zig_args.append("--test-cmd-bin");
} else {
try zig_args.append("--test-no-exec");
},
.wasmtime => |bin_name| if (builder.enable_wasmtime) {
try zig_args.append("--test-cmd");
try zig_args.append(bin_name);
try zig_args.append("--test-cmd");
try zig_args.append("--dir=.");
try zig_args.append("--test-cmd-bin");
} else {
try zig_args.append("--test-no-exec");
},
.darling => |bin_name| if (builder.enable_darling) {
try zig_args.append("--test-cmd");
try zig_args.append(bin_name);
try zig_args.append("--test-cmd-bin");
} else {
try zig_args.append("--test-no-exec");
},
}
}
} else if (self.kind == .test_exe) {
try zig_args.append("--test-no-exec");
}
for (self.packages.items) |pkg| {
try makePackageCmd(self, pkg, &zig_args);
}
for (self.include_dirs.items) |include_dir| {
switch (include_dir) {
.raw_path => |include_path| {
try zig_args.append("-I");
try zig_args.append(self.builder.pathFromRoot(include_path));
},
.raw_path_system => |include_path| {
if (builder.sysroot != null) {
try zig_args.append("-iwithsysroot");
} else {
try zig_args.append("-isystem");
}
const resolved_include_path = self.builder.pathFromRoot(include_path);
const common_include_path = if (builtin.os.tag == .windows and builder.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: {
// We need to check for disk designator and strip it out from dir path so
// that zig/clang can concat resolved_include_path with sysroot.
const disk_designator = fs.path.diskDesignatorWindows(resolved_include_path);
if (mem.indexOf(u8, resolved_include_path, disk_designator)) |where| {
break :blk resolved_include_path[where + disk_designator.len ..];
}
break :blk resolved_include_path;
} else resolved_include_path;
try zig_args.append(common_include_path);
},
.other_step => |other| if (other.emit_h) {
const h_path = other.getOutputHSource().getPath(self.builder);
try zig_args.append("-isystem");
try zig_args.append(fs.path.dirname(h_path).?);
},
.config_header_step => |config_header| {
try zig_args.append("-I");
try zig_args.append(config_header.output_dir);
},
}
}
for (self.lib_paths.items) |lib_path| {
try zig_args.append("-L");
try zig_args.append(lib_path);
}
for (self.rpaths.items) |rpath| {
try zig_args.append("-rpath");
try zig_args.append(rpath);
}
for (self.c_macros.items) |c_macro| {
try zig_args.append("-D");
try zig_args.append(c_macro);
}
if (self.target.isDarwin()) {
for (self.framework_dirs.items) |dir| {