authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-04-21 06:53:48+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-10 12:19:26+02:00
logdf719f249d10ddefd46ccca028bc482922933f8e
treee00400ba2a79da964556c054ab39a8f92a3e0f10
parent162b11b250dcd7c31c67438a0bb643c718a2ae90
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

process_headers: Add FreeBSD libc support.


1 files changed, 31 insertions(+), 7 deletions(-)

tools/process_headers.zig+31-7
......@@ -1,11 +1,12 @@
11//! To get started, run this tool with no args and read the help message.
22//!
3//! The build systems of musl-libc and glibc require specifying a single target
3//! The build systems of glibc, musl, and FreeBSD require specifying a single target
44//! architecture. Meanwhile, Zig supports out-of-the-box cross compilation for
55//! every target. So the process to create libc headers that Zig ships is to use
66//! this tool.
7//! First, use the musl/glibc build systems to create installations of all the
8//! targets in the `glibc_targets`/`musl_targets` variables.
7//!
8//! First, use the glibc, musl, and FreeBSD build systems to create installations of all the
9//! targets in the `glibc_targets`, `musl_targets`, and `freebsd_targets` variables.
910//! Next, run this tool to create a new directory which puts .h files into
1011//! <arch> subdirectories, with `generic` being files that apply to all architectures.
1112//! You'll then have to manually update Zig source repo with these new files.
......@@ -76,6 +77,16 @@ const musl_targets = [_]LibCTarget{
7677 .{ .arch = .x86_64, .abi = .muslx32 },
7778};
7879
80const freebsd_targets = [_]LibCTarget{
81 .{ .arch = .arm, .abi = .eabihf },
82 .{ .arch = .aarch64, .abi = .none },
83 .{ .arch = .powerpc, .abi = .eabihf },
84 .{ .arch = .powerpc64, .abi = .none },
85 .{ .arch = .riscv64, .abi = .none },
86 .{ .arch = .x86, .abi = .none },
87 .{ .arch = .x86_64, .abi = .none },
88};
89
7990const DestTarget = struct {
8091 arch: Arch,
8192 os: OsTag,
......@@ -118,6 +129,7 @@ const PathTable = std.StringHashMap(*TargetToHash);
118129const LibCVendor = enum {
119130 musl,
120131 glibc,
132 freebsd,
121133};
122134
123135pub fn main() !void {
......@@ -164,6 +176,7 @@ pub fn main() !void {
164176 const libc_targets = switch (vendor) {
165177 .glibc => &glibc_targets,
166178 .musl => &musl_targets,
179 .freebsd => &freebsd_targets,
167180 };
168181
169182 var path_table = PathTable.init(allocator);
......@@ -177,16 +190,27 @@ pub fn main() !void {
177190 const libc_dir = switch (vendor) {
178191 .glibc => try std.zig.target.glibcRuntimeTriple(allocator, libc_target.arch, .linux, libc_target.abi),
179192 .musl => std.zig.target.muslArchName(libc_target.arch, libc_target.abi),
193 .freebsd => switch (libc_target.arch) {
194 .arm => "armv7",
195 .aarch64 => "aarch64",
196 .powerpc => "powerpc",
197 .powerpc64 => "powerpc64",
198 .powerpc64le => "powerpc64le",
199 .riscv64 => "riscv64",
200 .x86 => "i386",
201 .x86_64 => "amd64",
202 else => unreachable,
203 },
180204 };
181205 const dest_target = DestTarget{
182206 .arch = libc_target.arch,
183 .os = .linux,
207 .os = if (vendor == .freebsd) .freebsd else .linux,
184208 .abi = libc_target.abi,
185209 };
186210
187211 search: for (search_paths.items) |search_path| {
188212 const sub_path = switch (vendor) {
189 .glibc => &[_][]const u8{ search_path, libc_dir, "usr", "include" },
213 .glibc, .freebsd => &[_][]const u8{ search_path, libc_dir, "usr", "include" },
190214 .musl => &[_][]const u8{ search_path, libc_dir, "usr", "local", "musl", "include" },
191215 };
192216 const target_include_dir = try std.fs.path.join(allocator, sub_path);
......@@ -207,7 +231,7 @@ pub fn main() !void {
207231 const full_path = try std.fs.path.join(allocator, &[_][]const u8{ full_dir_name, entry.name });
208232 switch (entry.kind) {
209233 .directory => try dir_stack.append(full_path),
210 .file => {
234 .file, .sym_link => {
211235 const rel_path = try std.fs.path.relative(allocator, target_include_dir, full_path);
212236 const max_size = 2 * 1024 * 1024 * 1024;
213237 const raw_bytes = try std.fs.cwd().readFileAlloc(allocator, full_path, max_size);
......@@ -315,6 +339,6 @@ fn usageAndExit(arg0: []const u8) noreturn {
315339 std.debug.print("--search-path can be used any number of times.\n", .{});
316340 std.debug.print(" subdirectories of search paths look like, e.g. x86_64-linux-gnu\n", .{});
317341 std.debug.print("--out is a dir that will be created, and populated with the results\n", .{});
318 std.debug.print("--abi is either musl or glibc\n", .{});
342 std.debug.print("--abi is either glibc, musl, or freebsd\n", .{});
319343 std.process.exit(1);
320344}