-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathTarget.zig
3012 lines (2749 loc) · 93.5 KB
/
Target.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
//! All the details about the machine that will be executing code.
//! Unlike `Query` which might leave some things as "default" or "host", this
//! data is fully resolved into a concrete set of OS versions, CPU features,
//! etc.
cpu: Cpu,
os: Os,
abi: Abi,
ofmt: ObjectFormat,
dynamic_linker: DynamicLinker = DynamicLinker.none,
pub const Query = @import("Target/Query.zig");
pub const Os = struct {
tag: Tag,
version_range: VersionRange,
pub const Tag = enum {
freestanding,
other,
contiki,
elfiamcu,
fuchsia,
hermit,
aix,
haiku,
hurd,
linux,
plan9,
rtems,
serenity,
zos,
dragonfly,
freebsd,
netbsd,
openbsd,
bridgeos,
driverkit,
ios,
macos,
tvos,
visionos,
watchos,
illumos,
solaris,
windows,
uefi,
ps3,
ps4,
ps5,
emscripten,
wasi,
amdhsa,
amdpal,
cuda,
mesa3d,
nvcl,
opencl,
opengl,
vulkan,
// LLVM tags deliberately omitted:
// - darwin
// - kfreebsd
// - nacl
// - shadermodel
pub inline fn isDarwin(tag: Tag) bool {
return switch (tag) {
.bridgeos,
.driverkit,
.ios,
.macos,
.tvos,
.visionos,
.watchos,
=> true,
else => false,
};
}
pub inline fn isBSD(tag: Tag) bool {
return tag.isDarwin() or switch (tag) {
.freebsd, .openbsd, .netbsd, .dragonfly => true,
else => false,
};
}
pub inline fn isSolarish(tag: Tag) bool {
return tag == .solaris or tag == .illumos;
}
pub fn exeFileExt(tag: Tag, arch: Cpu.Arch) [:0]const u8 {
return switch (tag) {
.windows => ".exe",
.uefi => ".efi",
.plan9 => arch.plan9Ext(),
else => switch (arch) {
.wasm32, .wasm64 => ".wasm",
else => "",
},
};
}
pub fn staticLibSuffix(tag: Tag, abi: Abi) [:0]const u8 {
return switch (abi) {
.msvc, .itanium => ".lib",
else => switch (tag) {
.windows, .uefi => ".lib",
else => ".a",
},
};
}
pub fn dynamicLibSuffix(tag: Tag) [:0]const u8 {
return switch (tag) {
.windows, .uefi => ".dll",
.bridgeos,
.driverkit,
.ios,
.macos,
.tvos,
.visionos,
.watchos,
=> ".dylib",
else => ".so",
};
}
pub fn libPrefix(tag: Os.Tag, abi: Abi) [:0]const u8 {
return switch (abi) {
.msvc, .itanium => "",
else => switch (tag) {
.windows, .uefi => "",
else => "lib",
},
};
}
pub inline fn isGnuLibC(tag: Os.Tag, abi: Abi) bool {
return (tag == .hurd or tag == .linux) and abi.isGnu();
}
pub fn defaultVersionRange(tag: Tag, arch: Cpu.Arch) Os {
return .{
.tag = tag,
.version_range = VersionRange.default(tag, arch),
};
}
pub inline fn getVersionRangeTag(tag: Tag) @typeInfo(TaggedVersionRange).@"union".tag_type.? {
return switch (tag) {
.freestanding,
.fuchsia,
.ps3,
.zos,
.haiku,
.rtems,
.aix,
.cuda,
.nvcl,
.amdhsa,
.ps4,
.ps5,
.elfiamcu,
.mesa3d,
.contiki,
.amdpal,
.hermit,
.hurd,
.emscripten,
.uefi,
.opencl, // TODO: OpenCL versions
.opengl, // TODO: GLSL versions
.vulkan,
.plan9,
.illumos,
.serenity,
.other,
=> .none,
// This should use semver once we determine the version history.
.bridgeos => .none,
.driverkit,
.freebsd,
.macos,
.ios,
.tvos,
.watchos,
.visionos,
.netbsd,
.openbsd,
.dragonfly,
.solaris,
.wasi,
=> .semver,
.linux => .linux,
.windows => .windows,
};
}
pub fn archName(tag: Tag, arch: Cpu.Arch) [:0]const u8 {
return switch (tag) {
.linux => switch (arch) {
.arm, .armeb, .thumb, .thumbeb => "arm",
.aarch64, .aarch64_be => "aarch64",
.loongarch32, .loongarch64 => "loongarch",
.mips, .mipsel, .mips64, .mips64el => "mips",
.powerpc, .powerpcle, .powerpc64, .powerpc64le => "powerpc",
.riscv32, .riscv64 => "riscv",
.sparc, .sparc64 => "sparc",
.x86, .x86_64 => "x86",
else => @tagName(arch),
},
else => @tagName(arch),
};
}
};
/// Based on NTDDI version constants from
/// https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt
pub const WindowsVersion = enum(u32) {
nt4 = 0x04000000,
win2k = 0x05000000,
xp = 0x05010000,
ws2003 = 0x05020000,
vista = 0x06000000,
win7 = 0x06010000,
win8 = 0x06020000,
win8_1 = 0x06030000,
win10 = 0x0A000000, //aka win10_th1
win10_th2 = 0x0A000001,
win10_rs1 = 0x0A000002,
win10_rs2 = 0x0A000003,
win10_rs3 = 0x0A000004,
win10_rs4 = 0x0A000005,
win10_rs5 = 0x0A000006,
win10_19h1 = 0x0A000007,
win10_vb = 0x0A000008, //aka win10_19h2
win10_mn = 0x0A000009, //aka win10_20h1
win10_fe = 0x0A00000A, //aka win10_20h2
win10_co = 0x0A00000B, //aka win10_21h1
win10_ni = 0x0A00000C, //aka win10_21h2
win10_cu = 0x0A00000D, //aka win10_22h2
win11_zn = 0x0A00000E, //aka win11_21h2
win11_ga = 0x0A00000F, //aka win11_22h2
win11_ge = 0x0A000010, //aka win11_23h2
_,
/// Latest Windows version that the Zig Standard Library is aware of
pub const latest = WindowsVersion.win11_ge;
/// Compared against build numbers reported by the runtime to distinguish win10 versions,
/// where 0x0A000000 + index corresponds to the WindowsVersion u32 value.
pub const known_win10_build_numbers = [_]u32{
10240, //win10 aka win10_th1
10586, //win10_th2
14393, //win10_rs1
15063, //win10_rs2
16299, //win10_rs3
17134, //win10_rs4
17763, //win10_rs5
18362, //win10_19h1
18363, //win10_vb aka win10_19h2
19041, //win10_mn aka win10_20h1
19042, //win10_fe aka win10_20h2
19043, //win10_co aka win10_21h1
19044, //win10_ni aka win10_21h2
19045, //win10_cu aka win10_22h2
22000, //win11_zn aka win11_21h2
22621, //win11_ga aka win11_22h2
22631, //win11_ge aka win11_23h2
};
/// Returns whether the first version `ver` is newer (greater) than or equal to the second version `ver`.
pub inline fn isAtLeast(ver: WindowsVersion, min_ver: WindowsVersion) bool {
return @intFromEnum(ver) >= @intFromEnum(min_ver);
}
pub const Range = struct {
min: WindowsVersion,
max: WindowsVersion,
pub inline fn includesVersion(range: Range, ver: WindowsVersion) bool {
return @intFromEnum(ver) >= @intFromEnum(range.min) and
@intFromEnum(ver) <= @intFromEnum(range.max);
}
/// Checks if system is guaranteed to be at least `version` or older than `version`.
/// Returns `null` if a runtime check is required.
pub inline fn isAtLeast(range: Range, min_ver: WindowsVersion) ?bool {
if (@intFromEnum(range.min) >= @intFromEnum(min_ver)) return true;
if (@intFromEnum(range.max) < @intFromEnum(min_ver)) return false;
return null;
}
};
pub fn parse(str: []const u8) !WindowsVersion {
return std.meta.stringToEnum(WindowsVersion, str) orelse
@enumFromInt(std.fmt.parseInt(u32, str, 0) catch
return error.InvalidOperatingSystemVersion);
}
/// This function is defined to serialize a Zig source code representation of this
/// type, that, when parsed, will deserialize into the same data.
pub fn format(
ver: WindowsVersion,
comptime fmt_str: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) @TypeOf(writer).Error!void {
const maybe_name = std.enums.tagName(WindowsVersion, ver);
if (comptime std.mem.eql(u8, fmt_str, "s")) {
if (maybe_name) |name|
try writer.print(".{s}", .{name})
else
try writer.print(".{d}", .{@intFromEnum(ver)});
} else if (comptime std.mem.eql(u8, fmt_str, "c")) {
if (maybe_name) |name|
try writer.print(".{s}", .{name})
else
try writer.print("@enumFromInt(0x{X:0>8})", .{@intFromEnum(ver)});
} else if (fmt_str.len == 0) {
if (maybe_name) |name|
try writer.print("WindowsVersion.{s}", .{name})
else
try writer.print("WindowsVersion(0x{X:0>8})", .{@intFromEnum(ver)});
} else std.fmt.invalidFmtError(fmt_str, ver);
}
};
pub const LinuxVersionRange = struct {
range: std.SemanticVersion.Range,
glibc: std.SemanticVersion,
pub inline fn includesVersion(range: LinuxVersionRange, ver: std.SemanticVersion) bool {
return range.range.includesVersion(ver);
}
/// Checks if system is guaranteed to be at least `version` or older than `version`.
/// Returns `null` if a runtime check is required.
pub inline fn isAtLeast(range: LinuxVersionRange, ver: std.SemanticVersion) ?bool {
return range.range.isAtLeast(ver);
}
};
/// The version ranges here represent the minimum OS version to be supported
/// and the maximum OS version to be supported. The default values represent
/// the range that the Zig Standard Library bases its abstractions on.
///
/// The minimum version of the range is the main setting to tweak for a target.
/// Usually, the maximum target OS version will remain the default, which is
/// the latest released version of the OS.
///
/// To test at compile time if the target is guaranteed to support a given OS feature,
/// one should check that the minimum version of the range is greater than or equal to
/// the version the feature was introduced in.
///
/// To test at compile time if the target certainly will not support a given OS feature,
/// one should check that the maximum version of the range is less than the version the
/// feature was introduced in.
///
/// If neither of these cases apply, a runtime check should be used to determine if the
/// target supports a given OS feature.
///
/// Binaries built with a given maximum version will continue to function on newer
/// operating system versions. However, such a binary may not take full advantage of the
/// newer operating system APIs.
///
/// See `Os.isAtLeast`.
pub const VersionRange = union {
none: void,
semver: std.SemanticVersion.Range,
linux: LinuxVersionRange,
windows: WindowsVersion.Range,
/// The default `VersionRange` represents the range that the Zig Standard Library
/// bases its abstractions on.
pub fn default(tag: Tag, arch: Cpu.Arch) VersionRange {
return switch (tag) {
.freestanding,
.fuchsia,
.ps3,
.zos,
.haiku,
.rtems,
.aix,
.cuda,
.nvcl,
.amdhsa,
.ps4,
.ps5,
.elfiamcu,
.mesa3d,
.contiki,
.amdpal,
.hermit,
.hurd,
.emscripten,
.uefi,
.opencl, // TODO: OpenCL versions
.opengl, // TODO: GLSL versions
.vulkan,
.plan9,
.illumos,
.serenity,
.bridgeos,
.other,
=> .{ .none = {} },
.freebsd => .{
.semver = std.SemanticVersion.Range{
.min = .{ .major = 12, .minor = 0, .patch = 0 },
.max = .{ .major = 14, .minor = 0, .patch = 0 },
},
},
.driverkit => .{
.semver = .{
.min = .{ .major = 19, .minor = 0, .patch = 0 },
.max = .{ .major = 24, .minor = 0, .patch = 0 },
},
},
.macos => switch (arch) {
.aarch64 => VersionRange{
.semver = .{
.min = .{ .major = 11, .minor = 7, .patch = 1 },
.max = .{ .major = 14, .minor = 6, .patch = 1 },
},
},
.x86_64 => VersionRange{
.semver = .{
.min = .{ .major = 11, .minor = 7, .patch = 1 },
.max = .{ .major = 14, .minor = 6, .patch = 1 },
},
},
else => unreachable,
},
.ios => .{
.semver = .{
.min = .{ .major = 12, .minor = 0, .patch = 0 },
.max = .{ .major = 17, .minor = 6, .patch = 1 },
},
},
.watchos => .{
.semver = .{
.min = .{ .major = 6, .minor = 0, .patch = 0 },
.max = .{ .major = 10, .minor = 6, .patch = 0 },
},
},
.tvos => .{
.semver = .{
.min = .{ .major = 13, .minor = 0, .patch = 0 },
.max = .{ .major = 17, .minor = 6, .patch = 0 },
},
},
.visionos => .{
.semver = .{
.min = .{ .major = 1, .minor = 0, .patch = 0 },
.max = .{ .major = 1, .minor = 3, .patch = 0 },
},
},
.netbsd => .{
.semver = .{
.min = .{ .major = 8, .minor = 0, .patch = 0 },
.max = .{ .major = 10, .minor = 0, .patch = 0 },
},
},
.openbsd => .{
.semver = .{
.min = .{ .major = 7, .minor = 3, .patch = 0 },
.max = .{ .major = 7, .minor = 5, .patch = 0 },
},
},
.dragonfly => .{
.semver = .{
.min = .{ .major = 5, .minor = 8, .patch = 0 },
.max = .{ .major = 6, .minor = 4, .patch = 0 },
},
},
.solaris => .{
.semver = .{
.min = .{ .major = 11, .minor = 0, .patch = 0 },
.max = .{ .major = 11, .minor = 4, .patch = 0 },
},
},
.wasi => .{
.semver = .{
.min = .{ .major = 0, .minor = 1, .patch = 0 },
.max = .{ .major = 0, .minor = 1, .patch = 0 },
},
},
.linux => .{
.linux = .{
.range = .{
.min = .{ .major = 4, .minor = 19, .patch = 0 },
.max = .{ .major = 6, .minor = 10, .patch = 3 },
},
.glibc = blk: {
const default_min = .{ .major = 2, .minor = 28, .patch = 0 };
for (std.zig.target.available_libcs) |libc| {
// We don't know the ABI here. We can get away with not checking it
// for now, but that may not always remain true.
if (libc.os != tag or libc.arch != arch) continue;
if (libc.glibc_min) |min| {
if (min.order(default_min) == .gt) break :blk min;
}
}
break :blk default_min;
},
},
},
.windows => .{
.windows = .{
.min = .win10,
.max = WindowsVersion.latest,
},
},
};
}
};
pub const TaggedVersionRange = union(enum) {
none: void,
semver: std.SemanticVersion.Range,
linux: LinuxVersionRange,
windows: WindowsVersion.Range,
};
/// Provides a tagged union. `Target` does not store the tag because it is
/// redundant with the OS tag; this function abstracts that part away.
pub inline fn getVersionRange(os: Os) TaggedVersionRange {
return switch (os.tag.getVersionRangeTag()) {
.none => .{ .none = {} },
.semver => .{ .semver = os.version_range.semver },
.linux => .{ .linux = os.version_range.linux },
.windows => .{ .windows = os.version_range.windows },
};
}
/// Checks if system is guaranteed to be at least `version` or older than `version`.
/// Returns `null` if a runtime check is required.
pub inline fn isAtLeast(os: Os, comptime tag: Tag, ver: switch (tag.getVersionRangeTag()) {
.none => void,
.semver, .linux => std.SemanticVersion,
.windows => WindowsVersion,
}) ?bool {
return if (os.tag != tag) false else switch (tag.getVersionRangeTag()) {
.none => true,
inline .semver,
.linux,
.windows,
=> |field| @field(os.version_range, @tagName(field)).isAtLeast(ver),
};
}
/// On Darwin, we always link libSystem which contains libc.
/// Similarly on FreeBSD and NetBSD we always link system libc
/// since this is the stable syscall interface.
pub fn requiresLibC(os: Os) bool {
return switch (os.tag) {
.freebsd,
.aix,
.netbsd,
.bridgeos,
.driverkit,
.macos,
.ios,
.tvos,
.watchos,
.visionos,
.dragonfly,
.openbsd,
.haiku,
.solaris,
.illumos,
.serenity,
=> true,
.linux,
.windows,
.freestanding,
.fuchsia,
.ps3,
.zos,
.rtems,
.cuda,
.nvcl,
.amdhsa,
.ps4,
.ps5,
.elfiamcu,
.mesa3d,
.contiki,
.amdpal,
.hermit,
.hurd,
.wasi,
.emscripten,
.uefi,
.opencl,
.opengl,
.vulkan,
.plan9,
.other,
=> false,
};
}
};
pub const aarch64 = @import("Target/aarch64.zig");
pub const arc = @import("Target/arc.zig");
pub const amdgpu = @import("Target/amdgpu.zig");
pub const arm = @import("Target/arm.zig");
pub const avr = @import("Target/avr.zig");
pub const bpf = @import("Target/bpf.zig");
pub const csky = @import("Target/csky.zig");
pub const hexagon = @import("Target/hexagon.zig");
pub const lanai = @import("Target/lanai.zig");
pub const loongarch = @import("Target/loongarch.zig");
pub const m68k = @import("Target/m68k.zig");
pub const mips = @import("Target/mips.zig");
pub const msp430 = @import("Target/msp430.zig");
pub const nvptx = @import("Target/nvptx.zig");
pub const powerpc = @import("Target/powerpc.zig");
pub const riscv = @import("Target/riscv.zig");
pub const sparc = @import("Target/sparc.zig");
pub const spirv = @import("Target/spirv.zig");
pub const s390x = @import("Target/s390x.zig");
pub const ve = @import("Target/ve.zig");
pub const wasm = @import("Target/wasm.zig");
pub const x86 = @import("Target/x86.zig");
pub const xcore = @import("Target/xcore.zig");
pub const xtensa = @import("Target/xtensa.zig");
pub const propeller = @import("Target/propeller.zig");
pub const Abi = enum {
none,
gnu,
gnuabin32,
gnuabi64,
gnueabi,
gnueabihf,
gnuf32,
gnusf,
gnux32,
gnuilp32,
code16,
eabi,
eabihf,
ilp32,
android,
androideabi,
musl,
musleabi,
musleabihf,
muslx32,
msvc,
itanium,
cygnus,
simulator,
macabi,
ohos,
ohoseabi,
// LLVM tags deliberately omitted:
// - amplification
// - anyhit
// - callable
// - closesthit
// - compute
// - coreclr
// - domain
// - geometry
// - gnuf64
// - hull
// - intersection
// - library
// - mesh
// - miss
// - pixel
// - raygeneration
// - vertex
pub fn default(arch: Cpu.Arch, os: Os) Abi {
return if (arch.isWasm()) .musl else switch (os.tag) {
.freestanding,
.other,
=> switch (arch) {
// Soft float is usually a sane default for freestanding.
.arm,
.armeb,
.thumb,
.thumbeb,
.csky,
.mips,
.mipsel,
.powerpc,
.powerpcle,
=> .eabi,
else => .none,
},
.aix,
=> if (arch == .powerpc) .eabihf else .none,
.linux,
.wasi,
.emscripten,
=> .musl,
.rtems,
=> switch (arch) {
.arm,
.armeb,
.thumb,
.thumbeb,
.mips,
.mipsel,
=> .eabi,
.powerpc,
=> .eabihf,
else => .none,
},
.hurd,
.windows,
=> .gnu,
.freebsd,
=> switch (arch) {
.arm,
.armeb,
.thumb,
.thumbeb,
.powerpc,
=> .eabihf,
.mips,
.mipsel,
=> .eabi,
else => .none,
},
.netbsd,
=> switch (arch) {
.arm,
.armeb,
.thumb,
.thumbeb,
.powerpc,
=> .eabihf,
.mips,
.mipsel,
=> .eabi,
else => .none,
},
.openbsd,
=> switch (arch) {
.arm,
.thumb,
=> .eabi,
.powerpc,
=> .eabihf,
else => .none,
},
.ios,
=> if (arch == .x86_64) .macabi else .none,
.tvos,
.visionos,
=> if (arch == .x86_64) .simulator else .none,
.uefi,
=> .msvc,
.contiki,
.elfiamcu,
.fuchsia,
.hermit,
.haiku,
.plan9,
.serenity,
.zos,
.dragonfly,
.bridgeos,
.driverkit,
.macos,
.watchos,
.illumos,
.solaris,
.ps3,
.ps4,
.ps5,
.amdhsa,
.amdpal,
.cuda,
.mesa3d,
.nvcl,
.opencl,
.opengl,
.vulkan,
=> .none,
};
}
pub inline fn isGnu(abi: Abi) bool {
return switch (abi) {
.gnu,
.gnuabin32,
.gnuabi64,
.gnueabi,
.gnueabihf,
.gnuf32,
.gnusf,
.gnux32,
.gnuilp32,
=> true,
else => false,
};
}
pub inline fn isMusl(abi: Abi) bool {
return switch (abi) {
.musl,
.musleabi,
.musleabihf,
.muslx32,
=> true,
else => abi.isOpenHarmony(),
};
}
pub inline fn isOpenHarmony(abi: Abi) bool {
return switch (abi) {
.ohos, .ohoseabi => true,
else => false,
};
}
pub inline fn isAndroid(abi: Abi) bool {
return switch (abi) {
.android, .androideabi => true,
else => false,
};
}
pub inline fn floatAbi(abi: Abi) FloatAbi {
return switch (abi) {
.androideabi,
.eabi,
.gnueabi,
.musleabi,
.gnusf,
.ohoseabi,
=> .soft,
else => .hard,
};
}
};
pub const ObjectFormat = enum {
/// C source code.
c,
/// The Common Object File Format used by Windows and UEFI.
coff,
/// The Executable and Linkable Format used by many Unixes.
elf,
/// The Generalized Object File Format used by z/OS.
goff,
/// The Intel HEX format for storing binary code in ASCII text.
hex,
/// The Mach object format used by macOS and other Apple platforms.
macho,
/// Nvidia's PTX (Parallel Thread Execution) assembly language.
nvptx,
/// The a.out format used by Plan 9 from Bell Labs.
plan9,
/// Machine code with no metadata.
raw,
/// The Khronos Group's Standard Portable Intermediate Representation V.
spirv,
/// The WebAssembly binary format.
wasm,
/// The eXtended Common Object File Format used by AIX.
xcoff,
// LLVM tags deliberately omitted:
// - dxcontainer
pub fn fileExt(of: ObjectFormat, arch: Cpu.Arch) [:0]const u8 {
return switch (of) {
.c => ".c",
.coff => ".obj",
.elf, .goff, .macho, .wasm, .xcoff => ".o",
.hex => ".ihex",
.nvptx => ".ptx",
.plan9 => arch.plan9Ext(),
.raw => ".bin",
.spirv => ".spv",
};
}
pub fn default(os_tag: Os.Tag, arch: Cpu.Arch) ObjectFormat {
return switch (os_tag) {
.aix => .xcoff,
.bridgeos, .driverkit, .ios, .macos, .tvos, .visionos, .watchos => .macho,
.plan9 => .plan9,
.uefi, .windows => .coff,
.zos => .goff,
else => switch (arch) {
.nvptx, .nvptx64 => .nvptx,
.spirv, .spirv32, .spirv64 => .spirv,
.wasm32, .wasm64 => .wasm,
else => .elf,
},
};
}
};
pub fn toElfMachine(target: Target) std.elf.EM {
if (target.os.tag == .elfiamcu) return .IAMCU;
return switch (target.cpu.arch) {
.amdgcn => .AMDGPU,
.arc => .ARC_COMPACT,
.arm, .armeb, .thumb, .thumbeb => .ARM,
.aarch64, .aarch64_be => .AARCH64,
.avr => .AVR,
.bpfel, .bpfeb => .BPF,
.csky => .CSKY,
.hexagon => .QDSP6,
.kalimba => .CSR_KALIMBA,
.lanai => .LANAI,
.loongarch32, .loongarch64 => .LOONGARCH,
.m68k => .@"68K",
.mips, .mips64, .mipsel, .mips64el => .MIPS,
.msp430 => .MSP430,
.powerpc, .powerpcle => .PPC,
.powerpc64, .powerpc64le => .PPC64,
.riscv32, .riscv64 => .RISCV,
.s390x => .S390,
.sparc => if (Target.sparc.featureSetHas(target.cpu.features, .v9)) .SPARC32PLUS else .SPARC,
.sparc64 => .SPARCV9,
.spu_2 => .SPU_2,
.ve => .VE,
.x86 => .@"386",
.x86_64 => .X86_64,
.xcore => .XCORE,
.xtensa => .XTENSA,
.propeller1 => .PROPELLER,
.propeller2 => .PROPELLER2,
.nvptx,
.nvptx64,
.spirv,
.spirv32,
.spirv64,
.wasm32,
.wasm64,
=> .NONE,
};
}
pub fn toCoffMachine(target: Target) std.coff.MachineType {
return switch (target.cpu.arch) {
.arm => .ARM,
.thumb => .THUMB,
.aarch64 => .ARM64,
.loongarch32 => .LOONGARCH32,
.loongarch64 => .LOONGARCH64,
.riscv32 => .RISCV32,
.riscv64 => .RISCV64,
.x86 => .I386,
.x86_64 => .X64,
.amdgcn,
.arc,
.armeb,
.thumbeb,
.aarch64_be,
.avr,
.bpfel,
.bpfeb,
.csky,
.hexagon,
.kalimba,
.lanai,
.m68k,
.mips,
.mipsel,
.mips64,
.mips64el,