Merge pull request #23913 from alexrp/netbsd-libc

Support dynamically-linked NetBSD libc when cross-compiling
This commit is contained in:
Alex Rønne Petersen
2025-05-20 07:46:08 +02:00
committed by GitHub
2273 changed files with 340293 additions and 176 deletions

View File

@@ -1,13 +1,13 @@
//! To get started, run this tool with no args and read the help message.
//!
//! The build systems of glibc, musl, and FreeBSD require specifying a single target
//! The build systems of glibc, musl, FreeBSD, and NetBSD require specifying a single target
//! architecture. Meanwhile, Zig supports out-of-the-box cross compilation for
//! every target. So the process to create libc headers that Zig ships is to use
//! this tool.
//!
//! First, use the glibc, musl, and FreeBSD build systems to create installations of all the
//! targets in the `glibc_targets`, `musl_targets`, and `freebsd_targets` variables.
//! Next, run this tool to create a new directory which puts .h files into
//! First, use the glibc, musl, FreeBSD, and NetBSD build systems to create installations of all the
//! targets in the `glibc_targets`, `musl_targets`, `freebsd_targets`, and `netbsd_targets`
//! variables. Next, run this tool to create a new directory which puts .h files into
//! <arch> subdirectories, with `generic` being files that apply to all architectures.
//! You'll then have to manually update Zig source repo with these new files.
@@ -87,6 +87,21 @@ const freebsd_targets = [_]LibCTarget{
.{ .arch = .x86_64, .abi = .none },
};
const netbsd_targets = [_]LibCTarget{
.{ .arch = .arm, .abi = .eabi },
.{ .arch = .arm, .abi = .eabihf },
.{ .arch = .aarch64, .abi = .none },
.{ .arch = .m68k, .abi = .none },
.{ .arch = .mips, .abi = .eabi },
.{ .arch = .mips, .abi = .eabihf },
.{ .arch = .powerpc, .abi = .eabi },
.{ .arch = .powerpc, .abi = .eabihf },
.{ .arch = .sparc, .abi = .none },
.{ .arch = .sparc64, .abi = .none },
.{ .arch = .x86, .abi = .none },
.{ .arch = .x86_64, .abi = .none },
};
const DestTarget = struct {
arch: Arch,
os: OsTag,
@@ -130,6 +145,7 @@ const LibCVendor = enum {
musl,
glibc,
freebsd,
netbsd,
};
pub fn main() !void {
@@ -177,6 +193,7 @@ pub fn main() !void {
.glibc => &glibc_targets,
.musl => &musl_targets,
.freebsd => &freebsd_targets,
.netbsd => &netbsd_targets,
};
var path_table = PathTable.init(allocator);
@@ -192,25 +209,49 @@ pub fn main() !void {
.musl => std.zig.target.muslArchName(libc_target.arch, libc_target.abi),
.freebsd => switch (libc_target.arch) {
.arm => "armv7",
.aarch64 => "aarch64",
.powerpc => "powerpc",
.powerpc64 => "powerpc64",
.powerpc64le => "powerpc64le",
.riscv64 => "riscv64",
.x86 => "i386",
.x86_64 => "amd64",
.aarch64,
.powerpc,
.powerpc64,
.riscv64,
=> |a| @tagName(a),
else => unreachable,
},
.netbsd => switch (libc_target.arch) {
.arm => if (libc_target.abi == .eabihf) "evbarmv7hf" else "evbarmv7",
.aarch64 => "evbarm64",
.m68k => "mac68k",
.mips => if (libc_target.abi == .eabihf) "evbmips" else "evbmipssf",
.powerpc => if (libc_target.abi == .eabihf) "evbppc" else "evbppcsf",
.x86 => "i386",
.x86_64 => "amd64",
.sparc,
.sparc64,
=> |a| @tagName(a),
else => unreachable,
},
};
const dest_target = DestTarget{
.arch = libc_target.arch,
.os = if (vendor == .freebsd) .freebsd else .linux,
.os = switch (vendor) {
.musl, .glibc => .linux,
.freebsd => .freebsd,
.netbsd => .netbsd,
},
.abi = libc_target.abi,
};
search: for (search_paths.items) |search_path| {
const sub_path = switch (vendor) {
.glibc, .freebsd => &[_][]const u8{ search_path, libc_dir, "usr", "include" },
.glibc,
.freebsd,
.netbsd,
=> &[_][]const u8{ search_path, libc_dir, "usr", "include" },
.musl => &[_][]const u8{ search_path, libc_dir, "usr", "local", "musl", "include" },
};
const target_include_dir = try std.fs.path.join(allocator, sub_path);
@@ -339,6 +380,6 @@ fn usageAndExit(arg0: []const u8) noreturn {
std.debug.print("--search-path can be used any number of times.\n", .{});
std.debug.print(" subdirectories of search paths look like, e.g. x86_64-linux-gnu\n", .{});
std.debug.print("--out is a dir that will be created, and populated with the results\n", .{});
std.debug.print("--abi is either glibc, musl, or freebsd\n", .{});
std.debug.print("--abi is either glibc, musl, freebsd, or netbsd\n", .{});
std.process.exit(1);
}

View File

@@ -0,0 +1,65 @@
//! This script updates the .c, .h, .s, and .S files that make up the start
//! files such as crt1.o.
//!
//! Example usage:
//! `zig run tools/update_netbsd_libc.zig -- ~/Downloads/netbsd-src .`
const std = @import("std");
const exempt_files = [_][]const u8{
// This file is maintained by a separate project and does not come from NetBSD.
"abilists",
};
pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
const args = try std.process.argsAlloc(arena);
const netbsd_src_path = args[1];
const zig_src_path = args[2];
const dest_dir_path = try std.fmt.allocPrint(arena, "{s}/lib/libc/netbsd", .{zig_src_path});
var dest_dir = std.fs.cwd().openDir(dest_dir_path, .{ .iterate = true }) catch |err| {
std.log.err("unable to open destination directory '{s}': {s}", .{
dest_dir_path, @errorName(err),
});
std.process.exit(1);
};
defer dest_dir.close();
var netbsd_src_dir = try std.fs.cwd().openDir(netbsd_src_path, .{});
defer netbsd_src_dir.close();
// Copy updated files from upstream.
{
var walker = try dest_dir.walk(arena);
defer walker.deinit();
walk: while (try walker.next()) |entry| {
if (entry.kind != .file) continue;
if (std.mem.startsWith(u8, entry.basename, ".")) continue;
for (exempt_files) |p| {
if (std.mem.eql(u8, entry.path, p)) continue :walk;
}
std.log.info("updating '{s}/{s}' from '{s}/{s}'", .{
dest_dir_path, entry.path,
netbsd_src_path, entry.path,
});
netbsd_src_dir.copyFile(entry.path, dest_dir, entry.path, .{}) catch |err| {
std.log.warn("unable to copy '{s}/{s}' to '{s}/{s}': {s}", .{
netbsd_src_path, entry.path,
dest_dir_path, entry.path,
@errorName(err),
});
if (err == error.FileNotFound) {
try dest_dir.deleteFile(entry.path);
}
};
}
}
}