Skip to content

Commit e69e002

Browse files
committed
compiler: Support building NetBSD crt1.o/Scrt1.o and stub shared libraries.
Only works for NetBSD 10.1+. Note that we still default to targeting NetBSD 9. Contributes to #2877.
1 parent c5e669f commit e69e002

File tree

4 files changed

+836
-0
lines changed

4 files changed

+836
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ set(ZIG_STAGE2_SOURCES
588588
src/dev.zig
589589
src/libs/freebsd.zig
590590
src/libs/glibc.zig
591+
src/libs/netbsd.zig
591592
src/introspect.zig
592593
src/libs/libcxx.zig
593594
src/libs/libtsan.zig

src/Compilation.zig

+57
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const LibCInstallation = std.zig.LibCInstallation;
2424
const glibc = @import("libs/glibc.zig");
2525
const musl = @import("libs/musl.zig");
2626
const freebsd = @import("libs/freebsd.zig");
27+
const netbsd = @import("libs/netbsd.zig");
2728
const mingw = @import("libs/mingw.zig");
2829
const libunwind = @import("libs/libunwind.zig");
2930
const libcxx = @import("libs/libcxx.zig");
@@ -250,6 +251,7 @@ fuzzer_lib: ?CrtFile = null,
250251

251252
glibc_so_files: ?glibc.BuiltSharedObjects = null,
252253
freebsd_so_files: ?freebsd.BuiltSharedObjects = null,
254+
netbsd_so_files: ?netbsd.BuiltSharedObjects = null,
253255
wasi_emulated_libs: []const wasi_libc.CrtFile,
254256

255257
/// For example `Scrt1.o` and `libc_nonshared.a`. These are populated after building libc from source,
@@ -297,13 +299,15 @@ const QueuedJobs = struct {
297299
musl_crt_file: [@typeInfo(musl.CrtFile).@"enum".fields.len]bool = @splat(false),
298300
glibc_crt_file: [@typeInfo(glibc.CrtFile).@"enum".fields.len]bool = @splat(false),
299301
freebsd_crt_file: [@typeInfo(freebsd.CrtFile).@"enum".fields.len]bool = @splat(false),
302+
netbsd_crt_file: [@typeInfo(netbsd.CrtFile).@"enum".fields.len]bool = @splat(false),
300303
/// one of WASI libc static objects
301304
wasi_libc_crt_file: [@typeInfo(wasi_libc.CrtFile).@"enum".fields.len]bool = @splat(false),
302305
/// one of the mingw-w64 static objects
303306
mingw_crt_file: [@typeInfo(mingw.CrtFile).@"enum".fields.len]bool = @splat(false),
304307
/// all of the glibc shared objects
305308
glibc_shared_objects: bool = false,
306309
freebsd_shared_objects: bool = false,
310+
netbsd_shared_objects: bool = false,
307311
/// libunwind.a, usually needed when linking libc
308312
libunwind: bool = false,
309313
libcxx: bool = false,
@@ -795,6 +799,8 @@ pub const MiscTask = enum {
795799
musl_crt_file,
796800
freebsd_crt_file,
797801
freebsd_shared_objects,
802+
netbsd_crt_file,
803+
netbsd_shared_objects,
798804
mingw_crt_file,
799805
windows_import_lib,
800806
libunwind,
@@ -832,6 +838,9 @@ pub const MiscTask = enum {
832838
@"freebsd libc Scrt1.o",
833839
@"freebsd libc shared object",
834840

841+
@"netbsd libc Scrt0.o",
842+
@"netbsd libc shared object",
843+
835844
@"mingw-w64 crt2.o",
836845
@"mingw-w64 dllcrt2.o",
837846
@"mingw-w64 libmingw32.lib",
@@ -1893,6 +1902,16 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
18931902

18941903
comp.queued_jobs.freebsd_shared_objects = true;
18951904
comp.remaining_prelink_tasks += freebsd.sharedObjectsCount();
1905+
} else if (target.isNetBSDLibC()) {
1906+
if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
1907+
1908+
if (netbsd.needsCrt0(comp.config.output_mode)) |f| {
1909+
comp.queued_jobs.netbsd_crt_file[@intFromEnum(f)] = true;
1910+
comp.remaining_prelink_tasks += 1;
1911+
}
1912+
1913+
comp.queued_jobs.netbsd_shared_objects = true;
1914+
comp.remaining_prelink_tasks += netbsd.sharedObjectsCount();
18961915
} else if (target.isWasiLibC()) {
18971916
if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
18981917

@@ -2051,6 +2070,10 @@ pub fn destroy(comp: *Compilation) void {
20512070
freebsd_file.deinit(gpa);
20522071
}
20532072

2073+
if (comp.netbsd_so_files) |*netbsd_file| {
2074+
netbsd_file.deinit(gpa);
2075+
}
2076+
20542077
for (comp.c_object_table.keys()) |key| {
20552078
key.destroy(gpa);
20562079
}
@@ -3866,6 +3889,10 @@ fn performAllTheWorkInner(
38663889
comp.link_task_wait_group.spawnManager(buildFreeBSDSharedObjects, .{ comp, main_progress_node });
38673890
}
38683891

3892+
if (comp.queued_jobs.netbsd_shared_objects) {
3893+
comp.link_task_wait_group.spawnManager(buildNetBSDSharedObjects, .{ comp, main_progress_node });
3894+
}
3895+
38693896
if (comp.queued_jobs.libunwind) {
38703897
comp.link_task_wait_group.spawnManager(buildLibUnwind, .{ comp, main_progress_node });
38713898
}
@@ -3907,6 +3934,13 @@ fn performAllTheWorkInner(
39073934
}
39083935
}
39093936

3937+
for (0..@typeInfo(netbsd.CrtFile).@"enum".fields.len) |i| {
3938+
if (comp.queued_jobs.netbsd_crt_file[i]) {
3939+
const tag: netbsd.CrtFile = @enumFromInt(i);
3940+
comp.link_task_wait_group.spawnManager(buildNetBSDCrtFile, .{ comp, tag, main_progress_node });
3941+
}
3942+
}
3943+
39103944
for (0..@typeInfo(wasi_libc.CrtFile).@"enum".fields.len) |i| {
39113945
if (comp.queued_jobs.wasi_libc_crt_file[i]) {
39123946
const tag: wasi_libc.CrtFile = @enumFromInt(i);
@@ -4933,6 +4967,29 @@ fn buildFreeBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) v
49334967
}
49344968
}
49354969

4970+
fn buildNetBSDCrtFile(comp: *Compilation, crt_file: netbsd.CrtFile, prog_node: std.Progress.Node) void {
4971+
if (netbsd.buildCrtFile(comp, crt_file, prog_node)) |_| {
4972+
comp.queued_jobs.netbsd_crt_file[@intFromEnum(crt_file)] = false;
4973+
} else |err| switch (err) {
4974+
error.SubCompilationFailed => return, // error reported already
4975+
else => comp.lockAndSetMiscFailure(.netbsd_crt_file, "unable to build NetBSD {s}: {s}", .{
4976+
@tagName(crt_file), @errorName(err),
4977+
}),
4978+
}
4979+
}
4980+
4981+
fn buildNetBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) void {
4982+
if (netbsd.buildSharedObjects(comp, prog_node)) |_| {
4983+
// The job should no longer be queued up since it succeeded.
4984+
comp.queued_jobs.netbsd_shared_objects = false;
4985+
} else |err| switch (err) {
4986+
error.SubCompilationFailed => return, // error reported already
4987+
else => comp.lockAndSetMiscFailure(.netbsd_shared_objects, "unable to build NetBSD libc shared objects: {s}", .{
4988+
@errorName(err),
4989+
}),
4990+
}
4991+
}
4992+
49364993
fn buildMingwCrtFile(comp: *Compilation, crt_file: mingw.CrtFile, prog_node: std.Progress.Node) void {
49374994
if (mingw.buildCrtFile(comp, crt_file, prog_node)) |_| {
49384995
comp.queued_jobs.mingw_crt_file[@intFromEnum(crt_file)] = false;

0 commit comments

Comments
 (0)