aboutsummaryrefslogtreecommitdiff
path: root/src/Compilation.zig
diff options
context:
space:
mode:
authorAlex Rønne Petersen <alex@alexrp.com>2025-05-05 07:23:28 +0200
committerAlex Rønne Petersen <alex@alexrp.com>2025-05-10 20:58:15 +0200
commitd3a6236eef400ff0f99c3fe7d85cfda1c687bb82 (patch)
tree2274b70e9e07a613f8e10d48022f8dc2e05bcc25 /src/Compilation.zig
parent0e3609b8e05c12285786235636e9923deeea8c09 (diff)
downloadzig-d3a6236eef400ff0f99c3fe7d85cfda1c687bb82.tar.gz
zig-d3a6236eef400ff0f99c3fe7d85cfda1c687bb82.zip
compiler: Support building FreeBSD crt1.o/Scrt1.o and stub shared libraries.
Only works for FreeBSD 14+. Note that we still default to targeting FreeBSD 13. Contributes to #2876.
Diffstat (limited to 'src/Compilation.zig')
-rw-r--r--src/Compilation.zig57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig
index bd7d5221e7..f921a8cfd1 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -23,6 +23,7 @@ const build_options = @import("build_options");
const LibCInstallation = std.zig.LibCInstallation;
const glibc = @import("libs/glibc.zig");
const musl = @import("libs/musl.zig");
+const freebsd = @import("libs/freebsd.zig");
const mingw = @import("libs/mingw.zig");
const libunwind = @import("libs/libunwind.zig");
const libcxx = @import("libs/libcxx.zig");
@@ -248,6 +249,7 @@ compiler_rt_obj: ?CrtFile = null,
fuzzer_lib: ?CrtFile = null,
glibc_so_files: ?glibc.BuiltSharedObjects = null,
+freebsd_so_files: ?freebsd.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,
@@ -294,12 +296,14 @@ const QueuedJobs = struct {
update_builtin_zig: bool,
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),
/// 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
mingw_crt_file: [@typeInfo(mingw.CrtFile).@"enum".fields.len]bool = @splat(false),
/// all of the glibc shared objects
glibc_shared_objects: bool = false,
+ freebsd_shared_objects: bool = false,
/// libunwind.a, usually needed when linking libc
libunwind: bool = false,
libcxx: bool = false,
@@ -789,6 +793,8 @@ pub const MiscTask = enum {
glibc_crt_file,
glibc_shared_objects,
musl_crt_file,
+ freebsd_crt_file,
+ freebsd_shared_objects,
mingw_crt_file,
windows_import_lib,
libunwind,
@@ -823,6 +829,9 @@ pub const MiscTask = enum {
@"glibc libc_nonshared.a",
@"glibc shared object",
+ @"freebsd libc Scrt1.o",
+ @"freebsd libc shared object",
+
@"mingw-w64 crt2.o",
@"mingw-w64 dllcrt2.o",
@"mingw-w64 libmingw32.lib",
@@ -1874,6 +1883,16 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
comp.queued_jobs.glibc_crt_file[@intFromEnum(glibc.CrtFile.libc_nonshared_a)] = true;
comp.remaining_prelink_tasks += 1;
+ } else if (target.isFreeBSDLibC()) {
+ if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
+
+ if (freebsd.needsCrt0(comp.config.output_mode)) |f| {
+ comp.queued_jobs.freebsd_crt_file[@intFromEnum(f)] = true;
+ comp.remaining_prelink_tasks += 1;
+ }
+
+ comp.queued_jobs.freebsd_shared_objects = true;
+ comp.remaining_prelink_tasks += freebsd.sharedObjectsCount();
} else if (target.isWasiLibC()) {
if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
@@ -2028,6 +2047,10 @@ pub fn destroy(comp: *Compilation) void {
glibc_file.deinit(gpa);
}
+ if (comp.freebsd_so_files) |*freebsd_file| {
+ freebsd_file.deinit(gpa);
+ }
+
for (comp.c_object_table.keys()) |key| {
key.destroy(gpa);
}
@@ -3839,6 +3862,10 @@ fn performAllTheWorkInner(
comp.link_task_wait_group.spawnManager(buildGlibcSharedObjects, .{ comp, main_progress_node });
}
+ if (comp.queued_jobs.freebsd_shared_objects) {
+ comp.link_task_wait_group.spawnManager(buildFreeBSDSharedObjects, .{ comp, main_progress_node });
+ }
+
if (comp.queued_jobs.libunwind) {
comp.link_task_wait_group.spawnManager(buildLibUnwind, .{ comp, main_progress_node });
}
@@ -3873,6 +3900,13 @@ fn performAllTheWorkInner(
}
}
+ for (0..@typeInfo(freebsd.CrtFile).@"enum".fields.len) |i| {
+ if (comp.queued_jobs.freebsd_crt_file[i]) {
+ const tag: freebsd.CrtFile = @enumFromInt(i);
+ comp.link_task_wait_group.spawnManager(buildFreeBSDCrtFile, .{ 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);
@@ -4876,6 +4910,29 @@ fn buildGlibcSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) voi
}
}
+fn buildFreeBSDCrtFile(comp: *Compilation, crt_file: freebsd.CrtFile, prog_node: std.Progress.Node) void {
+ if (freebsd.buildCrtFile(comp, crt_file, prog_node)) |_| {
+ comp.queued_jobs.freebsd_crt_file[@intFromEnum(crt_file)] = false;
+ } else |err| switch (err) {
+ error.SubCompilationFailed => return, // error reported already
+ else => comp.lockAndSetMiscFailure(.freebsd_crt_file, "unable to build FreeBSD {s}: {s}", .{
+ @tagName(crt_file), @errorName(err),
+ }),
+ }
+}
+
+fn buildFreeBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) void {
+ if (freebsd.buildSharedObjects(comp, prog_node)) |_| {
+ // The job should no longer be queued up since it succeeded.
+ comp.queued_jobs.freebsd_shared_objects = false;
+ } else |err| switch (err) {
+ error.SubCompilationFailed => return, // error reported already
+ else => comp.lockAndSetMiscFailure(.freebsd_shared_objects, "unable to build FreeBSD 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;