-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathmain.zig
7532 lines (6968 loc) · 333 KB
/
main.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 assert = std.debug.assert;
const io = std.io;
const fs = std.fs;
const mem = std.mem;
const process = std.process;
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;
const Ast = std.zig.Ast;
const Color = std.zig.Color;
const warn = std.log.warn;
const ThreadPool = std.Thread.Pool;
const cleanExit = std.process.cleanExit;
const native_os = builtin.os.tag;
const tracy = @import("tracy.zig");
const Compilation = @import("Compilation.zig");
const link = @import("link.zig");
const Package = @import("Package.zig");
const build_options = @import("build_options");
const introspect = @import("introspect.zig");
const EnvVar = std.zig.EnvVar;
const LibCInstallation = std.zig.LibCInstallation;
const wasi_libc = @import("wasi_libc.zig");
const Cache = std.Build.Cache;
const target_util = @import("target.zig");
const crash_report = @import("crash_report.zig");
const Module = @import("Module.zig");
const AstGen = std.zig.AstGen;
const mingw = @import("mingw.zig");
const Server = std.zig.Server;
pub const std_options = .{
.wasiCwd = wasi_cwd,
.logFn = log,
.enable_segfault_handler = false,
.log_level = switch (builtin.mode) {
.Debug => .debug,
.ReleaseSafe, .ReleaseFast => .info,
.ReleaseSmall => .err,
},
};
// Crash report needs to override the panic handler
pub const panic = crash_report.panic;
var wasi_preopens: fs.wasi.Preopens = undefined;
pub fn wasi_cwd() std.os.wasi.fd_t {
// Expect the first preopen to be current working directory.
const cwd_fd: std.posix.fd_t = 3;
assert(mem.eql(u8, wasi_preopens.names[cwd_fd], "."));
return cwd_fd;
}
fn getWasiPreopen(name: []const u8) Compilation.Directory {
return .{
.path = name,
.handle = .{
.fd = wasi_preopens.find(name) orelse fatal("WASI preopen not found: '{s}'", .{name}),
},
};
}
pub fn fatal(comptime format: []const u8, args: anytype) noreturn {
std.log.err(format, args);
process.exit(1);
}
const normal_usage =
\\Usage: zig [command] [options]
\\
\\Commands:
\\
\\ build Build project from build.zig
\\ fetch Copy a package into global cache and print its hash
\\ init Initialize a Zig package in the current directory
\\
\\ build-exe Create executable from source or object files
\\ build-lib Create library from source or object files
\\ build-obj Create object from source or object files
\\ test Perform unit testing
\\ run Create executable and run immediately
\\
\\ ast-check Look for simple compile errors in any set of files
\\ fmt Reformat Zig source into canonical form
\\ reduce Minimize a bug report
\\ translate-c Convert C code to Zig code
\\
\\ ar Use Zig as a drop-in archiver
\\ cc Use Zig as a drop-in C compiler
\\ c++ Use Zig as a drop-in C++ compiler
\\ dlltool Use Zig as a drop-in dlltool.exe
\\ lib Use Zig as a drop-in lib.exe
\\ ranlib Use Zig as a drop-in ranlib
\\ objcopy Use Zig as a drop-in objcopy
\\ rc Use Zig as a drop-in rc.exe
\\
\\ env Print lib path, std path, cache directory, and version
\\ help Print this help and exit
\\ std View standard library documentation in a browser
\\ libc Display native libc paths file or validate one
\\ targets List available compilation targets
\\ version Print version number and exit
\\ zen Print Zen of Zig and exit
\\
\\General Options:
\\
\\ -h, --help Print command-specific usage
\\
;
const debug_usage = normal_usage ++
\\
\\Debug Commands:
\\
\\ changelist Compute mappings from old ZIR to new ZIR
\\ dump-zir Dump a file containing cached ZIR
\\ detect-cpu Compare Zig's CPU feature detection vs LLVM
\\ llvm-ints Dump a list of LLVMABIAlignmentOfType for all integers
\\
;
const usage = if (build_options.enable_debug_extensions) debug_usage else normal_usage;
var log_scopes: std.ArrayListUnmanaged([]const u8) = .{};
pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
// Hide debug messages unless:
// * logging enabled with `-Dlog`.
// * the --debug-log arg for the scope has been provided
if (@intFromEnum(level) > @intFromEnum(std.options.log_level) or
@intFromEnum(level) > @intFromEnum(std.log.Level.info))
{
if (!build_options.enable_logging) return;
const scope_name = @tagName(scope);
for (log_scopes.items) |log_scope| {
if (mem.eql(u8, log_scope, scope_name))
break;
} else return;
}
const prefix1 = comptime level.asText();
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
// Print the message to stderr, silently ignoring any errors
std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
}
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{
.stack_trace_frames = build_options.mem_leak_frames,
}){};
pub fn main() anyerror!void {
crash_report.initialize();
const use_gpa = (build_options.force_gpa or !builtin.link_libc) and native_os != .wasi;
const gpa = gpa: {
if (native_os == .wasi) {
break :gpa std.heap.wasm_allocator;
}
if (use_gpa) {
break :gpa general_purpose_allocator.allocator();
}
// We would prefer to use raw libc allocator here, but cannot
// use it if it won't support the alignment we need.
if (@alignOf(std.c.max_align_t) < @alignOf(i128)) {
break :gpa std.heap.c_allocator;
}
break :gpa std.heap.raw_c_allocator;
};
defer if (use_gpa) {
_ = general_purpose_allocator.deinit();
};
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
const args = try process.argsAlloc(arena);
if (tracy.enable_allocation) {
var gpa_tracy = tracy.tracyAllocator(gpa);
return mainArgs(gpa_tracy.allocator(), arena, args);
}
if (native_os == .wasi) {
wasi_preopens = try fs.wasi.preopensAlloc(arena);
}
// Short circuit some of the other logic for bootstrapping.
if (build_options.only_c) {
if (mem.eql(u8, args[1], "build-exe")) {
return buildOutputType(gpa, arena, args, .{ .build = .Exe });
} else if (mem.eql(u8, args[1], "build-obj")) {
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
} else {
@panic("only build-exe or build-obj is supported in a -Donly-c build");
}
}
return mainArgs(gpa, arena, args);
}
/// Check that LLVM and Clang have been linked properly so that they are using the same
/// libc++ and can safely share objects with pointers to static variables in libc++
fn verifyLibcxxCorrectlyLinked() void {
if (build_options.have_llvm and ZigClangIsLLVMUsingSeparateLibcxx()) {
fatal(
\\Zig was built/linked incorrectly: LLVM and Clang have separate copies of libc++
\\ If you are dynamically linking LLVM, make sure you dynamically link libc++ too
, .{});
}
}
fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
if (args.len <= 1) {
std.log.info("{s}", .{usage});
fatal("expected command argument", .{});
}
if (process.can_execv and std.posix.getenvZ("ZIG_IS_DETECTING_LIBC_PATHS") != null) {
// In this case we have accidentally invoked ourselves as "the system C compiler"
// to figure out where libc is installed. This is essentially infinite recursion
// via child process execution due to the CC environment variable pointing to Zig.
// Here we ignore the CC environment variable and exec `cc` as a child process.
// However it's possible Zig is installed as *that* C compiler as well, which is
// why we have this additional environment variable here to check.
var env_map = try process.getEnvMap(arena);
const inf_loop_env_key = "ZIG_IS_TRYING_TO_NOT_CALL_ITSELF";
if (env_map.get(inf_loop_env_key) != null) {
fatal("The compilation links against libc, but Zig is unable to provide a libc " ++
"for this operating system, and no --libc " ++
"parameter was provided, so Zig attempted to invoke the system C compiler " ++
"in order to determine where libc is installed. However the system C " ++
"compiler is `zig cc`, so no libc installation was found.", .{});
}
try env_map.put(inf_loop_env_key, "1");
// Some programs such as CMake will strip the `cc` and subsequent args from the
// CC environment variable. We detect and support this scenario here because of
// the ZIG_IS_DETECTING_LIBC_PATHS environment variable.
if (mem.eql(u8, args[1], "cc")) {
return process.execve(arena, args[1..], &env_map);
} else {
const modified_args = try arena.dupe([]const u8, args);
modified_args[0] = "cc";
return process.execve(arena, modified_args, &env_map);
}
}
const cmd = args[1];
const cmd_args = args[2..];
if (mem.eql(u8, cmd, "build-exe")) {
return buildOutputType(gpa, arena, args, .{ .build = .Exe });
} else if (mem.eql(u8, cmd, "build-lib")) {
return buildOutputType(gpa, arena, args, .{ .build = .Lib });
} else if (mem.eql(u8, cmd, "build-obj")) {
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
} else if (mem.eql(u8, cmd, "test")) {
return buildOutputType(gpa, arena, args, .zig_test);
} else if (mem.eql(u8, cmd, "run")) {
return buildOutputType(gpa, arena, args, .run);
} else if (mem.eql(u8, cmd, "dlltool") or
mem.eql(u8, cmd, "ranlib") or
mem.eql(u8, cmd, "lib") or
mem.eql(u8, cmd, "ar"))
{
return process.exit(try llvmArMain(arena, args));
} else if (mem.eql(u8, cmd, "build")) {
return cmdBuild(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "clang") or
mem.eql(u8, cmd, "-cc1") or mem.eql(u8, cmd, "-cc1as"))
{
return process.exit(try clangMain(arena, args));
} else if (mem.eql(u8, cmd, "ld.lld") or
mem.eql(u8, cmd, "lld-link") or
mem.eql(u8, cmd, "wasm-ld"))
{
return process.exit(try lldMain(arena, args, true));
} else if (build_options.only_core_functionality) {
@panic("only a few subcommands are supported in a zig2.c build");
} else if (mem.eql(u8, cmd, "cc")) {
return buildOutputType(gpa, arena, args, .cc);
} else if (mem.eql(u8, cmd, "c++")) {
return buildOutputType(gpa, arena, args, .cpp);
} else if (mem.eql(u8, cmd, "translate-c")) {
return buildOutputType(gpa, arena, args, .translate_c);
} else if (mem.eql(u8, cmd, "rc")) {
const use_server = cmd_args.len > 0 and std.mem.eql(u8, cmd_args[0], "--zig-integration");
return jitCmd(gpa, arena, cmd_args, .{
.cmd_name = "resinator",
.root_src_path = "resinator/main.zig",
.depend_on_aro = true,
.prepend_zig_lib_dir_path = true,
.server = use_server,
});
} else if (mem.eql(u8, cmd, "fmt")) {
return jitCmd(gpa, arena, cmd_args, .{
.cmd_name = "fmt",
.root_src_path = "fmt.zig",
});
} else if (mem.eql(u8, cmd, "objcopy")) {
return jitCmd(gpa, arena, cmd_args, .{
.cmd_name = "objcopy",
.root_src_path = "objcopy.zig",
});
} else if (mem.eql(u8, cmd, "fetch")) {
return cmdFetch(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "libc")) {
return jitCmd(gpa, arena, cmd_args, .{
.cmd_name = "libc",
.root_src_path = "libc.zig",
.prepend_zig_lib_dir_path = true,
});
} else if (mem.eql(u8, cmd, "std")) {
return jitCmd(gpa, arena, cmd_args, .{
.cmd_name = "std",
.root_src_path = "std-docs.zig",
.prepend_zig_lib_dir_path = true,
.prepend_zig_exe_path = true,
.prepend_global_cache_path = true,
});
} else if (mem.eql(u8, cmd, "init")) {
return cmdInit(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "targets")) {
const host = std.zig.resolveTargetQueryOrFatal(.{});
const stdout = io.getStdOut().writer();
return @import("print_targets.zig").cmdTargets(arena, cmd_args, stdout, host);
} else if (mem.eql(u8, cmd, "version")) {
try std.io.getStdOut().writeAll(build_options.version ++ "\n");
// Check libc++ linkage to make sure Zig was built correctly, but only
// for "env" and "version" to avoid affecting the startup time for
// build-critical commands (check takes about ~10 μs)
return verifyLibcxxCorrectlyLinked();
} else if (mem.eql(u8, cmd, "env")) {
verifyLibcxxCorrectlyLinked();
return @import("print_env.zig").cmdEnv(arena, cmd_args, io.getStdOut().writer());
} else if (mem.eql(u8, cmd, "reduce")) {
return jitCmd(gpa, arena, cmd_args, .{
.cmd_name = "reduce",
.root_src_path = "reduce.zig",
});
} else if (mem.eql(u8, cmd, "zen")) {
return io.getStdOut().writeAll(info_zen);
} else if (mem.eql(u8, cmd, "help") or mem.eql(u8, cmd, "-h") or mem.eql(u8, cmd, "--help")) {
return io.getStdOut().writeAll(usage);
} else if (mem.eql(u8, cmd, "ast-check")) {
return cmdAstCheck(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "detect-cpu")) {
return cmdDetectCpu(gpa, arena, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) {
return cmdChangelist(gpa, arena, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) {
return cmdDumpZir(gpa, arena, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "llvm-ints")) {
return cmdDumpLlvmInts(gpa, arena, cmd_args);
} else {
std.log.info("{s}", .{usage});
fatal("unknown command: {s}", .{args[1]});
}
}
const usage_build_generic =
\\Usage: zig build-exe [options] [files]
\\ zig build-lib [options] [files]
\\ zig build-obj [options] [files]
\\ zig test [options] [files]
\\ zig run [options] [files] [-- [args]]
\\ zig translate-c [options] [file]
\\
\\Supported file types:
\\ .zig Zig source code
\\ .o ELF object file
\\ .o Mach-O (macOS) object file
\\ .o WebAssembly object file
\\ .obj COFF (Windows) object file
\\ .lib COFF (Windows) static library
\\ .a ELF static library
\\ .a Mach-O (macOS) static library
\\ .a WebAssembly static library
\\ .so ELF shared object (dynamic link)
\\ .dll Windows Dynamic Link Library
\\ .dylib Mach-O (macOS) dynamic library
\\ .tbd (macOS) text-based dylib definition
\\ .s Target-specific assembly source code
\\ .S Assembly with C preprocessor (requires LLVM extensions)
\\ .c C source code (requires LLVM extensions)
\\ .cxx .cc .C .cpp .stub C++ source code (requires LLVM extensions)
\\ .m Objective-C source code (requires LLVM extensions)
\\ .mm Objective-C++ source code (requires LLVM extensions)
\\ .bc LLVM IR Module (requires LLVM extensions)
\\ .cu Cuda source code (requires LLVM extensions)
\\
\\General Options:
\\ -h, --help Print this help and exit
\\ --color [auto|off|on] Enable or disable colored error messages
\\ -femit-bin[=path] (default) Output machine code
\\ -fno-emit-bin Do not output machine code
\\ -femit-asm[=path] Output .s (assembly code)
\\ -fno-emit-asm (default) Do not output .s (assembly code)
\\ -femit-llvm-ir[=path] Produce a .ll file with optimized LLVM IR (requires LLVM extensions)
\\ -fno-emit-llvm-ir (default) Do not produce a .ll file with optimized LLVM IR
\\ -femit-llvm-bc[=path] Produce an optimized LLVM module as a .bc file (requires LLVM extensions)
\\ -fno-emit-llvm-bc (default) Do not produce an optimized LLVM module as a .bc file
\\ -femit-h[=path] Generate a C header file (.h)
\\ -fno-emit-h (default) Do not generate a C header file (.h)
\\ -femit-docs[=path] Create a docs/ dir with html documentation
\\ -fno-emit-docs (default) Do not produce docs/ dir with html documentation
\\ -femit-implib[=path] (default) Produce an import .lib when building a Windows DLL
\\ -fno-emit-implib Do not produce an import .lib when building a Windows DLL
\\ --show-builtin Output the source of @import("builtin") then exit
\\ --cache-dir [path] Override the local cache directory
\\ --global-cache-dir [path] Override the global cache directory
\\ --zig-lib-dir [path] Override path to Zig installation lib directory
\\
\\Global Compile Options:
\\ --name [name] Compilation unit name (not a file path)
\\ --libc [file] Provide a file which specifies libc paths
\\ -x language Treat subsequent input files as having type <language>
\\ --dep [[import=]name] Add an entry to the next module's import table
\\ -M[name][=src] Create a module based on the current per-module settings.
\\ The first module is the main module.
\\ "std" can be configured by omitting src
\\ After a -M argument, per-module settings are reset.
\\ --error-limit [num] Set the maximum amount of distinct error values
\\ -fllvm Force using LLVM as the codegen backend
\\ -fno-llvm Prevent using LLVM as the codegen backend
\\ -flibllvm Force using the LLVM API in the codegen backend
\\ -fno-libllvm Prevent using the LLVM API in the codegen backend
\\ -fclang Force using Clang as the C/C++ compilation backend
\\ -fno-clang Prevent using Clang as the C/C++ compilation backend
\\ -fPIE Force-enable Position Independent Executable
\\ -fno-PIE Force-disable Position Independent Executable
\\ -flto Force-enable Link Time Optimization (requires LLVM extensions)
\\ -fno-lto Force-disable Link Time Optimization
\\ -fdll-export-fns Mark exported functions as DLL exports (Windows)
\\ -fno-dll-export-fns Force-disable marking exported functions as DLL exports
\\ -freference-trace[=num] Show num lines of reference trace per compile error
\\ -fno-reference-trace Disable reference trace
\\ -fbuiltin Enable implicit builtin knowledge of functions
\\ -fno-builtin Disable implicit builtin knowledge of functions
\\ -ffunction-sections Places each function in a separate section
\\ -fno-function-sections All functions go into same section
\\ -fdata-sections Places each data in a separate section
\\ -fno-data-sections All data go into same section
\\ -fformatted-panics Enable formatted safety panics
\\ -fno-formatted-panics Disable formatted safety panics
\\ -fstructured-cfg (SPIR-V) force SPIR-V kernels to use structured control flow
\\ -fno-structured-cfg (SPIR-V) force SPIR-V kernels to not use structured control flow
\\ -mexec-model=[value] (WASI) Execution model
\\ -municode (Windows) Use wmain/wWinMain as entry point
\\
\\Per-Module Compile Options:
\\ -target [name] <arch><sub>-<os>-<abi> see the targets command
\\ -O [mode] Choose what to optimize for
\\ Debug (default) Optimizations off, safety on
\\ ReleaseFast Optimize for performance, safety off
\\ ReleaseSafe Optimize for performance, safety on
\\ ReleaseSmall Optimize for small binary, safety off
\\ -ofmt=[fmt] Override target object format
\\ elf Executable and Linking Format
\\ c C source code
\\ wasm WebAssembly
\\ coff Common Object File Format (Windows)
\\ macho macOS relocatables
\\ spirv Standard, Portable Intermediate Representation V (SPIR-V)
\\ plan9 Plan 9 from Bell Labs object format
\\ hex (planned feature) Intel IHEX
\\ raw (planned feature) Dump machine code directly
\\ -mcpu [cpu] Specify target CPU and feature set
\\ -mcmodel=[default|tiny| Limit range of code and data virtual addresses
\\ small|kernel|
\\ medium|large]
\\ -mred-zone Force-enable the "red-zone"
\\ -mno-red-zone Force-disable the "red-zone"
\\ -fomit-frame-pointer Omit the stack frame pointer
\\ -fno-omit-frame-pointer Store the stack frame pointer
\\ -fPIC Force-enable Position Independent Code
\\ -fno-PIC Force-disable Position Independent Code
\\ -fstack-check Enable stack probing in unsafe builds
\\ -fno-stack-check Disable stack probing in safe builds
\\ -fstack-protector Enable stack protection in unsafe builds
\\ -fno-stack-protector Disable stack protection in safe builds
\\ -fsanitize-c Enable C undefined behavior detection in unsafe builds
\\ -fno-sanitize-c Disable C undefined behavior detection in safe builds
\\ -fvalgrind Include valgrind client requests in release builds
\\ -fno-valgrind Omit valgrind client requests in debug builds
\\ -fsanitize-thread Enable Thread Sanitizer
\\ -fno-sanitize-thread Disable Thread Sanitizer
\\ -funwind-tables Always produce unwind table entries for all functions
\\ -fno-unwind-tables Never produce unwind table entries
\\ -ferror-tracing Enable error tracing in ReleaseFast mode
\\ -fno-error-tracing Disable error tracing in Debug and ReleaseSafe mode
\\ -fsingle-threaded Code assumes there is only one thread
\\ -fno-single-threaded Code may not assume there is only one thread
\\ -fstrip Omit debug symbols
\\ -fno-strip Keep debug symbols
\\ -idirafter [dir] Add directory to AFTER include search path
\\ -isystem [dir] Add directory to SYSTEM include search path
\\ -I[dir] Add directory to include search path
\\ -D[macro]=[value] Define C [macro] to [value] (1 if [value] omitted)
\\ -cflags [flags] -- Set extra flags for the next positional C source files
\\ -rcflags [flags] -- Set extra flags for the next positional .rc source files
\\ -rcincludes=[type] Set the type of includes to use when compiling .rc source files
\\ any (default) Use msvc if available, fall back to gnu
\\ msvc Use msvc include paths (must be present on the system)
\\ gnu Use mingw include paths (distributed with Zig)
\\ none Do not use any autodetected include paths
\\
\\Global Link Options:
\\ -T[script], --script [script] Use a custom linker script
\\ --version-script [path] Provide a version .map file
\\ --undefined-version Allow version scripts to refer to undefined symbols
\\ --no-undefined-version (default) Disallow version scripts from referring to undefined symbols
\\ --enable-new-dtags Use the new behavior for dynamic tags (RUNPATH)
\\ --disable-new-dtags Use the old behavior for dynamic tags (RPATH)
\\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
\\ --sysroot [path] Set the system root directory (usually /)
\\ --version [ver] Dynamic library semver
\\ -fentry Enable entry point with default symbol name
\\ -fentry=[name] Override the entry point symbol name
\\ -fno-entry Do not output any entry point
\\ --force_undefined [name] Specify the symbol must be defined for the link to succeed
\\ -fsoname[=name] Override the default SONAME value
\\ -fno-soname Disable emitting a SONAME
\\ -flld Force using LLD as the linker
\\ -fno-lld Prevent using LLD as the linker
\\ -fcompiler-rt Always include compiler-rt symbols in output
\\ -fno-compiler-rt Prevent including compiler-rt symbols in output
\\ -rdynamic Add all symbols to the dynamic symbol table
\\ -feach-lib-rpath Ensure adding rpath for each used dynamic library
\\ -fno-each-lib-rpath Prevent adding rpath for each used dynamic library
\\ -fallow-shlib-undefined Allows undefined symbols in shared libraries
\\ -fno-allow-shlib-undefined Disallows undefined symbols in shared libraries
\\ --build-id[=style] At a minor link-time expense, coordinates stripped binaries
\\ fast, uuid, sha1, md5 with debug symbols via a '.note.gnu.build-id' section
\\ 0x[hexstring] Maximum 32 bytes
\\ none (default) Disable build-id
\\ --eh-frame-hdr Enable C++ exception handling by passing --eh-frame-hdr to linker
\\ --emit-relocs Enable output of relocation sections for post build tools
\\ -z [arg] Set linker extension flags
\\ nodelete Indicate that the object cannot be deleted from a process
\\ notext Permit read-only relocations in read-only segments
\\ defs Force a fatal error if any undefined symbols remain
\\ undefs Reverse of -z defs
\\ origin Indicate that the object must have its origin processed
\\ nocopyreloc Disable the creation of copy relocations
\\ now (default) Force all relocations to be processed on load
\\ lazy Don't force all relocations to be processed on load
\\ relro (default) Force all relocations to be read-only after processing
\\ norelro Don't force all relocations to be read-only after processing
\\ common-page-size=[bytes] Set the common page size for ELF binaries
\\ max-page-size=[bytes] Set the max page size for ELF binaries
\\ -dynamic Force output to be dynamically linked
\\ -static Force output to be statically linked
\\ -Bsymbolic Bind global references locally
\\ --compress-debug-sections=[e] Debug section compression settings
\\ none No compression
\\ zlib Compression with deflate/inflate
\\ zstd Compression with zstandard
\\ --gc-sections Force removal of functions and data that are unreachable by the entry point or exported symbols
\\ --no-gc-sections Don't force removal of unreachable functions and data
\\ --sort-section=[value] Sort wildcard section patterns by 'name' or 'alignment'
\\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker
\\ --stack [size] Override default stack size
\\ --image-base [addr] Set base address for executable image
\\ -install_name=[value] (Darwin) add dylib's install name
\\ --entitlements [path] (Darwin) add path to entitlements file for embedding in code signature
\\ -pagezero_size [value] (Darwin) size of the __PAGEZERO segment in hexadecimal notation
\\ -headerpad [value] (Darwin) set minimum space for future expansion of the load commands in hexadecimal notation
\\ -headerpad_max_install_names (Darwin) set enough space as if all paths were MAXPATHLEN
\\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
\\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
\\ -ObjC (Darwin) force load all members of static archives that implement an Objective-C class or category
\\ --import-memory (WebAssembly) import memory from the environment
\\ --export-memory (WebAssembly) export memory to the host (Default unless --import-memory used)
\\ --import-symbols (WebAssembly) import missing symbols from the host environment
\\ --import-table (WebAssembly) import function table from the host environment
\\ --export-table (WebAssembly) export function table to the host environment
\\ --initial-memory=[bytes] (WebAssembly) initial size of the linear memory
\\ --max-memory=[bytes] (WebAssembly) maximum size of the linear memory
\\ --shared-memory (WebAssembly) use shared linear memory
\\ --global-base=[addr] (WebAssembly) where to start to place global data
\\
\\Per-Module Link Options:
\\ -l[lib], --library [lib] Link against system library (only if actually used)
\\ -needed-l[lib], Link against system library (even if unused)
\\ --needed-library [lib]
\\ -weak-l[lib] link against system library marking it and all
\\ -weak_library [lib] referenced symbols as weak
\\ -L[d], --library-directory [d] Add a directory to the library search path
\\ -search_paths_first For each library search path, check for dynamic
\\ lib then static lib before proceeding to next path.
\\ -search_paths_first_static For each library search path, check for static
\\ lib then dynamic lib before proceeding to next path.
\\ -search_dylibs_first Search for dynamic libs in all library search
\\ paths, then static libs.
\\ -search_static_first Search for static libs in all library search
\\ paths, then dynamic libs.
\\ -search_dylibs_only Only search for dynamic libs.
\\ -search_static_only Only search for static libs.
\\ -rpath [path] Add directory to the runtime library search path
\\ -framework [name] (Darwin) link against framework
\\ -needed_framework [name] (Darwin) link against framework (even if unused)
\\ -needed_library [lib] (Darwin) link against system library (even if unused)
\\ -weak_framework [name] (Darwin) link against framework and mark it and all referenced symbols as weak
\\ -F[dir] (Darwin) add search path for frameworks
\\ --export=[value] (WebAssembly) Force a symbol to be exported
\\
\\Test Options:
\\ --test-filter [text] Skip tests that do not match any filter
\\ --test-name-prefix [text] Add prefix to all tests
\\ --test-cmd [arg] Specify test execution command one arg at a time
\\ --test-cmd-bin Appends test binary path to test cmd args
\\ --test-evented-io Runs the test in evented I/O mode
\\ --test-no-exec Compiles test binary without running it
\\ --test-runner [path] Specify a custom test runner
\\
\\Debug Options (Zig Compiler Development):
\\ -fopt-bisect-limit=[limit] Only run [limit] first LLVM optimization passes
\\ -ftime-report Print timing diagnostics
\\ -fstack-report Print stack size diagnostics
\\ --verbose-link Display linker invocations
\\ --verbose-cc Display C compiler invocations
\\ --verbose-air Enable compiler debug output for Zig AIR
\\ --verbose-intern-pool Enable compiler debug output for InternPool
\\ --verbose-generic-instances Enable compiler debug output for generic instance generation
\\ --verbose-llvm-ir[=path] Enable compiler debug output for unoptimized LLVM IR
\\ --verbose-llvm-bc=[path] Enable compiler debug output for unoptimized LLVM BC
\\ --verbose-cimport Enable compiler debug output for C imports
\\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
\\ --debug-log [scope] Enable printing debug/info log messages for scope
\\ --debug-compile-errors Crash with helpful diagnostics at the first compile error
\\ --debug-link-snapshot Enable dumping of the linker's state in JSON format
\\ --debug-incremental Enable experimental feature: incremental compilation
\\
;
const SOName = union(enum) {
no,
yes_default_value,
yes: []const u8,
};
const EmitBin = union(enum) {
no,
yes_default_path,
yes: []const u8,
yes_a_out,
};
const Emit = union(enum) {
no,
yes_default_path,
yes: []const u8,
const Resolved = struct {
data: ?Compilation.EmitLoc,
dir: ?fs.Dir,
fn deinit(self: *Resolved) void {
if (self.dir) |*dir| {
dir.close();
}
}
};
fn resolve(emit: Emit, default_basename: []const u8, output_to_cache: bool) !Resolved {
var resolved: Resolved = .{ .data = null, .dir = null };
errdefer resolved.deinit();
switch (emit) {
.no => {},
.yes_default_path => {
resolved.data = Compilation.EmitLoc{
.directory = if (output_to_cache) null else .{
.path = null,
.handle = fs.cwd(),
},
.basename = default_basename,
};
},
.yes => |full_path| {
const basename = fs.path.basename(full_path);
if (fs.path.dirname(full_path)) |dirname| {
const handle = try fs.cwd().openDir(dirname, .{});
resolved = .{
.dir = handle,
.data = Compilation.EmitLoc{
.basename = basename,
.directory = .{
.path = dirname,
.handle = handle,
},
},
};
} else {
resolved.data = Compilation.EmitLoc{
.basename = basename,
.directory = .{ .path = null, .handle = fs.cwd() },
};
}
},
}
return resolved;
}
};
const ArgMode = union(enum) {
build: std.builtin.OutputMode,
cc,
cpp,
translate_c,
zig_test,
run,
};
/// Avoid dragging networking into zig2.c because it adds dependencies on some
/// linker symbols that are annoying to satisfy while bootstrapping.
const Ip4Address = if (build_options.only_core_functionality) void else std.net.Ip4Address;
const Listen = union(enum) {
none,
ip4: Ip4Address,
stdio,
};
const ArgsIterator = struct {
resp_file: ?ArgIteratorResponseFile = null,
args: []const []const u8,
i: usize = 0,
fn next(it: *@This()) ?[]const u8 {
if (it.i >= it.args.len) {
if (it.resp_file) |*resp| return resp.next();
return null;
}
defer it.i += 1;
return it.args[it.i];
}
fn nextOrFatal(it: *@This()) []const u8 {
if (it.i >= it.args.len) {
if (it.resp_file) |*resp| if (resp.next()) |ret| return ret;
fatal("expected parameter after {s}", .{it.args[it.i - 1]});
}
defer it.i += 1;
return it.args[it.i];
}
};
/// In contrast to `link.SystemLib`, this stores arguments that may need to be
/// resolved into static libraries so that we can pass only dynamic libraries
/// as system libs to `Compilation`.
const SystemLib = struct {
needed: bool,
weak: bool,
preferred_mode: std.builtin.LinkMode,
search_strategy: SearchStrategy,
const SearchStrategy = enum { paths_first, mode_first, no_fallback };
fn fallbackMode(this: SystemLib) std.builtin.LinkMode {
assert(this.search_strategy != .no_fallback);
return switch (this.preferred_mode) {
.dynamic => .static,
.static => .dynamic,
};
}
};
/// Similar to `link.Framework` except it doesn't store yet unresolved
/// path to the framework.
const Framework = struct {
needed: bool = false,
weak: bool = false,
};
const CliModule = struct {
paths: Package.Module.CreateOptions.Paths,
cc_argv: []const []const u8,
inherited: Package.Module.CreateOptions.Inherited,
target_arch_os_abi: ?[]const u8,
target_mcpu: ?[]const u8,
deps: []const Dep,
resolved: ?*Package.Module,
c_source_files_start: usize,
c_source_files_end: usize,
rc_source_files_start: usize,
rc_source_files_end: usize,
const Dep = struct {
key: []const u8,
value: []const u8,
};
};
fn buildOutputType(
gpa: Allocator,
arena: Allocator,
all_args: []const []const u8,
arg_mode: ArgMode,
) !void {
var provided_name: ?[]const u8 = null;
var root_src_file: ?[]const u8 = null;
var version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 };
var have_version = false;
var compatibility_version: ?std.SemanticVersion = null;
var formatted_panics: ?bool = null;
var function_sections = false;
var data_sections = false;
var no_builtin = false;
var listen: Listen = .none;
var debug_compile_errors = false;
var verbose_link = (native_os != .wasi or builtin.link_libc) and
EnvVar.ZIG_VERBOSE_LINK.isSet();
var verbose_cc = (native_os != .wasi or builtin.link_libc) and
EnvVar.ZIG_VERBOSE_CC.isSet();
var verbose_air = false;
var verbose_intern_pool = false;
var verbose_generic_instances = false;
var verbose_llvm_ir: ?[]const u8 = null;
var verbose_llvm_bc: ?[]const u8 = null;
var verbose_cimport = false;
var verbose_llvm_cpu_features = false;
var time_report = false;
var stack_report = false;
var show_builtin = false;
var emit_bin: EmitBin = .yes_default_path;
var emit_asm: Emit = .no;
var emit_llvm_ir: Emit = .no;
var emit_llvm_bc: Emit = .no;
var emit_docs: Emit = .no;
var emit_implib: Emit = .yes_default_path;
var emit_implib_arg_provided = false;
var target_arch_os_abi: ?[]const u8 = null;
var target_mcpu: ?[]const u8 = null;
var emit_h: Emit = .no;
var soname: SOName = undefined;
var want_compiler_rt: ?bool = null;
var linker_script: ?[]const u8 = null;
var version_script: ?[]const u8 = null;
var linker_allow_undefined_version: bool = false;
var linker_enable_new_dtags: ?bool = null;
var disable_c_depfile = false;
var linker_sort_section: ?link.File.Elf.SortSection = null;
var linker_gc_sections: ?bool = null;
var linker_compress_debug_sections: ?link.File.Elf.CompressDebugSections = null;
var linker_allow_shlib_undefined: ?bool = null;
var linker_bind_global_refs_locally: ?bool = null;
var linker_import_symbols: bool = false;
var linker_import_table: bool = false;
var linker_export_table: bool = false;
var linker_initial_memory: ?u64 = null;
var linker_max_memory: ?u64 = null;
var linker_global_base: ?u64 = null;
var linker_print_gc_sections: bool = false;
var linker_print_icf_sections: bool = false;
var linker_print_map: bool = false;
var llvm_opt_bisect_limit: c_int = -1;
var linker_z_nocopyreloc = false;
var linker_z_nodelete = false;
var linker_z_notext = false;
var linker_z_defs = false;
var linker_z_origin = false;
var linker_z_now = true;
var linker_z_relro = true;
var linker_z_common_page_size: ?u64 = null;
var linker_z_max_page_size: ?u64 = null;
var linker_tsaware = false;
var linker_nxcompat = false;
var linker_dynamicbase = true;
var linker_optimization: ?[]const u8 = null;
var linker_module_definition_file: ?[]const u8 = null;
var test_no_exec = false;
var entry: Compilation.CreateOptions.Entry = .default;
var force_undefined_symbols: std.StringArrayHashMapUnmanaged(void) = .{};
var stack_size: ?u64 = null;
var image_base: ?u64 = null;
var link_eh_frame_hdr = false;
var link_emit_relocs = false;
var build_id: ?std.zig.BuildId = null;
var runtime_args_start: ?usize = null;
var test_filters: std.ArrayListUnmanaged([]const u8) = .{};
var test_name_prefix: ?[]const u8 = null;
var test_runner_path: ?[]const u8 = null;
var override_local_cache_dir: ?[]const u8 = try EnvVar.ZIG_LOCAL_CACHE_DIR.get(arena);
var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
var override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena);
var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
var subsystem: ?std.Target.SubSystem = null;
var major_subsystem_version: ?u16 = null;
var minor_subsystem_version: ?u16 = null;
var mingw_unicode_entry_point: bool = false;
var enable_link_snapshots: bool = false;
var debug_incremental: bool = false;
var install_name: ?[]const u8 = null;
var hash_style: link.File.Elf.HashStyle = .both;
var entitlements: ?[]const u8 = null;
var pagezero_size: ?u64 = null;
var lib_search_strategy: SystemLib.SearchStrategy = .paths_first;
var lib_preferred_mode: std.builtin.LinkMode = .dynamic;
var headerpad_size: ?u32 = null;
var headerpad_max_install_names: bool = false;
var dead_strip_dylibs: bool = false;
var force_load_objc: bool = false;
var contains_res_file: bool = false;
var reference_trace: ?u32 = null;
var pdb_out_path: ?[]const u8 = null;
var error_limit: ?Module.ErrorInt = null;
// These are before resolving sysroot.
var extra_cflags: std.ArrayListUnmanaged([]const u8) = .{};
var extra_rcflags: std.ArrayListUnmanaged([]const u8) = .{};
var symbol_wrap_set: std.StringArrayHashMapUnmanaged(void) = .{};
var rc_includes: Compilation.RcIncludes = .any;
var manifest_file: ?[]const u8 = null;
var linker_export_symbol_names: std.ArrayListUnmanaged([]const u8) = .{};
// Tracks the position in c_source_files which have already their owner populated.
var c_source_files_owner_index: usize = 0;
// Tracks the position in rc_source_files which have already their owner populated.
var rc_source_files_owner_index: usize = 0;
// null means replace with the test executable binary
var test_exec_args: std.ArrayListUnmanaged(?[]const u8) = .{};
// These get set by CLI flags and then snapshotted when a `--mod` flag is
// encountered.
var mod_opts: Package.Module.CreateOptions.Inherited = .{};
// These get appended to by CLI flags and then slurped when a `--mod` flag
// is encountered.
var cssan: ClangSearchSanitizer = .{};
var cc_argv: std.ArrayListUnmanaged([]const u8) = .{};
var deps: std.ArrayListUnmanaged(CliModule.Dep) = .{};
// Contains every module specified via --mod. The dependencies are added
// after argument parsing is completed. We use a StringArrayHashMap to make
// error output consistent. "root" is special.
var create_module: CreateModule = .{
// Populated just before the call to `createModule`.
.global_cache_directory = undefined,
.object_format = null,
.dynamic_linker = null,
.modules = .{},
.opts = .{
.is_test = arg_mode == .zig_test,
// Populated while parsing CLI args.
.output_mode = undefined,
// Populated in the call to `createModule` for the root module.
.resolved_target = undefined,
.have_zcu = false,
// Populated just before the call to `createModule`.
.emit_llvm_ir = undefined,
// Populated just before the call to `createModule`.
.emit_llvm_bc = undefined,
// Populated just before the call to `createModule`.
.emit_bin = undefined,
// Populated just before the call to `createModule`.
.any_c_source_files = undefined,
},
// Populated in the call to `createModule` for the root module.
.resolved_options = undefined,
.system_libs = .{},
.resolved_system_libs = .{},
.wasi_emulated_libs = .{},
.c_source_files = .{},
.rc_source_files = .{},
.llvm_m_args = .{},
.sysroot = null,
.lib_dirs = .{}, // populated by createModule()
.lib_dir_args = .{}, // populated from CLI arg parsing
.libc_installation = null,
.want_native_include_dirs = false,
.frameworks = .{},
.framework_dirs = .{},
.rpath_list = .{},
.each_lib_rpath = null,
.libc_paths_file = try EnvVar.ZIG_LIBC.get(arena),
.link_objects = .{},
.native_system_include_paths = &.{},
};
// before arg parsing, check for the NO_COLOR environment variable
// if it exists, default the color setting to .off
// explicit --color arguments will still override this setting.
// Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet()) .off else .auto;