diff options
| author | Alex Rønne Petersen <alex@alexrp.com> | 2025-05-13 01:49:45 +0200 |
|---|---|---|
| committer | Alex Rønne Petersen <alex@alexrp.com> | 2025-05-17 20:12:56 +0200 |
| commit | a97e417ab1e555d14e6d76df722de92cca85ba2f (patch) | |
| tree | 240a553938a0fdebf5984b6f4cdcc498464abcef /src/Compilation.zig | |
| parent | c5e669ff768857df3cfab774318938ddfc254b90 (diff) | |
| download | zig-a97e417ab1e555d14e6d76df722de92cca85ba2f.tar.gz zig-a97e417ab1e555d14e6d76df722de92cca85ba2f.zip | |
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.
Diffstat (limited to 'src/Compilation.zig')
| -rw-r--r-- | src/Compilation.zig | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index 5c7707ec16..7c516229be 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -24,6 +24,7 @@ const LibCInstallation = std.zig.LibCInstallation; const glibc = @import("libs/glibc.zig"); const musl = @import("libs/musl.zig"); const freebsd = @import("libs/freebsd.zig"); +const netbsd = @import("libs/netbsd.zig"); const mingw = @import("libs/mingw.zig"); const libunwind = @import("libs/libunwind.zig"); const libcxx = @import("libs/libcxx.zig"); @@ -250,6 +251,7 @@ fuzzer_lib: ?CrtFile = null, glibc_so_files: ?glibc.BuiltSharedObjects = null, freebsd_so_files: ?freebsd.BuiltSharedObjects = null, +netbsd_so_files: ?netbsd.BuiltSharedObjects = null, wasi_emulated_libs: []const wasi_libc.CrtFile, /// For example `Scrt1.o` and `libc_nonshared.a`. These are populated after building libc from source, @@ -297,6 +299,7 @@ const QueuedJobs = struct { musl_crt_file: [@typeInfo(musl.CrtFile).@"enum".fields.len]bool = @splat(false), glibc_crt_file: [@typeInfo(glibc.CrtFile).@"enum".fields.len]bool = @splat(false), freebsd_crt_file: [@typeInfo(freebsd.CrtFile).@"enum".fields.len]bool = @splat(false), + netbsd_crt_file: [@typeInfo(netbsd.CrtFile).@"enum".fields.len]bool = @splat(false), /// one of WASI libc static objects wasi_libc_crt_file: [@typeInfo(wasi_libc.CrtFile).@"enum".fields.len]bool = @splat(false), /// one of the mingw-w64 static objects @@ -304,6 +307,7 @@ const QueuedJobs = struct { /// all of the glibc shared objects glibc_shared_objects: bool = false, freebsd_shared_objects: bool = false, + netbsd_shared_objects: bool = false, /// libunwind.a, usually needed when linking libc libunwind: bool = false, libcxx: bool = false, @@ -795,6 +799,8 @@ pub const MiscTask = enum { musl_crt_file, freebsd_crt_file, freebsd_shared_objects, + netbsd_crt_file, + netbsd_shared_objects, mingw_crt_file, windows_import_lib, libunwind, @@ -832,6 +838,9 @@ pub const MiscTask = enum { @"freebsd libc Scrt1.o", @"freebsd libc shared object", + @"netbsd libc Scrt0.o", + @"netbsd libc shared object", + @"mingw-w64 crt2.o", @"mingw-w64 dllcrt2.o", @"mingw-w64 libmingw32.lib", @@ -1893,6 +1902,16 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil comp.queued_jobs.freebsd_shared_objects = true; comp.remaining_prelink_tasks += freebsd.sharedObjectsCount(); + } else if (target.isNetBSDLibC()) { + if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable; + + if (netbsd.needsCrt0(comp.config.output_mode)) |f| { + comp.queued_jobs.netbsd_crt_file[@intFromEnum(f)] = true; + comp.remaining_prelink_tasks += 1; + } + + comp.queued_jobs.netbsd_shared_objects = true; + comp.remaining_prelink_tasks += netbsd.sharedObjectsCount(); } else if (target.isWasiLibC()) { if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable; @@ -2051,6 +2070,10 @@ pub fn destroy(comp: *Compilation) void { freebsd_file.deinit(gpa); } + if (comp.netbsd_so_files) |*netbsd_file| { + netbsd_file.deinit(gpa); + } + for (comp.c_object_table.keys()) |key| { key.destroy(gpa); } @@ -3866,6 +3889,10 @@ fn performAllTheWorkInner( comp.link_task_wait_group.spawnManager(buildFreeBSDSharedObjects, .{ comp, main_progress_node }); } + if (comp.queued_jobs.netbsd_shared_objects) { + comp.link_task_wait_group.spawnManager(buildNetBSDSharedObjects, .{ comp, main_progress_node }); + } + if (comp.queued_jobs.libunwind) { comp.link_task_wait_group.spawnManager(buildLibUnwind, .{ comp, main_progress_node }); } @@ -3907,6 +3934,13 @@ fn performAllTheWorkInner( } } + for (0..@typeInfo(netbsd.CrtFile).@"enum".fields.len) |i| { + if (comp.queued_jobs.netbsd_crt_file[i]) { + const tag: netbsd.CrtFile = @enumFromInt(i); + comp.link_task_wait_group.spawnManager(buildNetBSDCrtFile, .{ comp, tag, main_progress_node }); + } + } + for (0..@typeInfo(wasi_libc.CrtFile).@"enum".fields.len) |i| { if (comp.queued_jobs.wasi_libc_crt_file[i]) { const tag: wasi_libc.CrtFile = @enumFromInt(i); @@ -4933,6 +4967,29 @@ fn buildFreeBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) v } } +fn buildNetBSDCrtFile(comp: *Compilation, crt_file: netbsd.CrtFile, prog_node: std.Progress.Node) void { + if (netbsd.buildCrtFile(comp, crt_file, prog_node)) |_| { + comp.queued_jobs.netbsd_crt_file[@intFromEnum(crt_file)] = false; + } else |err| switch (err) { + error.SubCompilationFailed => return, // error reported already + else => comp.lockAndSetMiscFailure(.netbsd_crt_file, "unable to build NetBSD {s}: {s}", .{ + @tagName(crt_file), @errorName(err), + }), + } +} + +fn buildNetBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) void { + if (netbsd.buildSharedObjects(comp, prog_node)) |_| { + // The job should no longer be queued up since it succeeded. + comp.queued_jobs.netbsd_shared_objects = false; + } else |err| switch (err) { + error.SubCompilationFailed => return, // error reported already + else => comp.lockAndSetMiscFailure(.netbsd_shared_objects, "unable to build NetBSD libc shared objects: {s}", .{ + @errorName(err), + }), + } +} + fn buildMingwCrtFile(comp: *Compilation, crt_file: mingw.CrtFile, prog_node: std.Progress.Node) void { if (mingw.buildCrtFile(comp, crt_file, prog_node)) |_| { comp.queued_jobs.mingw_crt_file[@intFromEnum(crt_file)] = false; |
