authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-13 01:33:18+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-17 04:41:27+02:00
log90911b39d54cd947ac7c49a257554917bdd7fb38
treecd0d980cb698f399f5d0e67ee4fd87fb2193824a
parent1c342ca7c38d396bc38235af25058543c86c5bed
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

update_netbsd_libc: Add tool for updating NetBSD libc startup code.


2 files changed, 66 insertions(+), 0 deletions(-)

test/standalone/build.zig+1
......@@ -44,6 +44,7 @@ pub fn build(b: *std.Build) void {
4444 "../../tools/update_cpu_features.zig",
4545 "../../tools/update_freebsd_libc.zig",
4646 "../../tools/update_glibc.zig",
47 "../../tools/update_netbsd_libc.zig",
4748 }) |tool_src_path| {
4849 const tool = b.addTest(.{
4950 .name = std.fs.path.stem(tool_src_path),
tools/update_netbsd_libc.zig created+65
......@@ -0,0 +1,65 @@
1//! This script updates the .c, .h, .s, and .S files that make up the start
2//! files such as crt1.o.
3//!
4//! Example usage:
5//! `zig run tools/update_netbsd_libc.zig -- ~/Downloads/netbsd-src .`
6
7const std = @import("std");
8
9const exempt_files = [_][]const u8{
10 // This file is maintained by a separate project and does not come from NetBSD.
11 "abilists",
12};
13
14pub fn main() !void {
15 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
16 defer arena_instance.deinit();
17 const arena = arena_instance.allocator();
18
19 const args = try std.process.argsAlloc(arena);
20 const netbsd_src_path = args[1];
21 const zig_src_path = args[2];
22
23 const dest_dir_path = try std.fmt.allocPrint(arena, "{s}/lib/libc/netbsd", .{zig_src_path});
24
25 var dest_dir = std.fs.cwd().openDir(dest_dir_path, .{ .iterate = true }) catch |err| {
26 std.log.err("unable to open destination directory '{s}': {s}", .{
27 dest_dir_path, @errorName(err),
28 });
29 std.process.exit(1);
30 };
31 defer dest_dir.close();
32
33 var netbsd_src_dir = try std.fs.cwd().openDir(netbsd_src_path, .{});
34 defer netbsd_src_dir.close();
35
36 // Copy updated files from upstream.
37 {
38 var walker = try dest_dir.walk(arena);
39 defer walker.deinit();
40
41 walk: while (try walker.next()) |entry| {
42 if (entry.kind != .file) continue;
43 if (std.mem.startsWith(u8, entry.basename, ".")) continue;
44 for (exempt_files) |p| {
45 if (std.mem.eql(u8, entry.path, p)) continue :walk;
46 }
47
48 std.log.info("updating '{s}/{s}' from '{s}/{s}'", .{
49 dest_dir_path, entry.path,
50 netbsd_src_path, entry.path,
51 });
52
53 netbsd_src_dir.copyFile(entry.path, dest_dir, entry.path, .{}) catch |err| {
54 std.log.warn("unable to copy '{s}/{s}' to '{s}/{s}': {s}", .{
55 netbsd_src_path, entry.path,
56 dest_dir_path, entry.path,
57 @errorName(err),
58 });
59 if (err == error.FileNotFound) {
60 try dest_dir.deleteFile(entry.path);
61 }
62 };
63 }
64 }
65}