| ... | ... | @@ -1,11 +1,12 @@ |
| 1 | 1 | //! To get started, run this tool with no args and read the help message. |
| 2 | 2 | //! |
| 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 |
| 4 | 4 | //! architecture. Meanwhile, Zig supports out-of-the-box cross compilation for |
| 5 | 5 | //! every target. So the process to create libc headers that Zig ships is to use |
| 6 | 6 | //! 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. |
| 9 | 10 | //! Next, run this tool to create a new directory which puts .h files into |
| 10 | 11 | //! <arch> subdirectories, with `generic` being files that apply to all architectures. |
| 11 | 12 | //! You'll then have to manually update Zig source repo with these new files. |
| ... | ... | @@ -76,6 +77,16 @@ const musl_targets = [_]LibCTarget{ |
| 76 | 77 | .{ .arch = .x86_64, .abi = .muslx32 }, |
| 77 | 78 | }; |
| 78 | 79 | |
| 80 | const 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 | |
| 79 | 90 | const DestTarget = struct { |
| 80 | 91 | arch: Arch, |
| 81 | 92 | os: OsTag, |
| ... | ... | @@ -118,6 +129,7 @@ const PathTable = std.StringHashMap(*TargetToHash); |
| 118 | 129 | const LibCVendor = enum { |
| 119 | 130 | musl, |
| 120 | 131 | glibc, |
| 132 | freebsd, |
| 121 | 133 | }; |
| 122 | 134 | |
| 123 | 135 | pub fn main() !void { |
| ... | ... | @@ -164,6 +176,7 @@ pub fn main() !void { |
| 164 | 176 | const libc_targets = switch (vendor) { |
| 165 | 177 | .glibc => &glibc_targets, |
| 166 | 178 | .musl => &musl_targets, |
| 179 | .freebsd => &freebsd_targets, |
| 167 | 180 | }; |
| 168 | 181 | |
| 169 | 182 | var path_table = PathTable.init(allocator); |
| ... | ... | @@ -177,16 +190,27 @@ pub fn main() !void { |
| 177 | 190 | const libc_dir = switch (vendor) { |
| 178 | 191 | .glibc => try std.zig.target.glibcRuntimeTriple(allocator, libc_target.arch, .linux, libc_target.abi), |
| 179 | 192 | .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 | }, |
| 180 | 204 | }; |
| 181 | 205 | const dest_target = DestTarget{ |
| 182 | 206 | .arch = libc_target.arch, |
| 183 | | .os = .linux, |
| 207 | .os = if (vendor == .freebsd) .freebsd else .linux, |
| 184 | 208 | .abi = libc_target.abi, |
| 185 | 209 | }; |
| 186 | 210 | |
| 187 | 211 | search: for (search_paths.items) |search_path| { |
| 188 | 212 | 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" }, |
| 190 | 214 | .musl => &[_][]const u8{ search_path, libc_dir, "usr", "local", "musl", "include" }, |
| 191 | 215 | }; |
| 192 | 216 | const target_include_dir = try std.fs.path.join(allocator, sub_path); |
| ... | ... | @@ -207,7 +231,7 @@ pub fn main() !void { |
| 207 | 231 | const full_path = try std.fs.path.join(allocator, &[_][]const u8{ full_dir_name, entry.name }); |
| 208 | 232 | switch (entry.kind) { |
| 209 | 233 | .directory => try dir_stack.append(full_path), |
| 210 | | .file => { |
| 234 | .file, .sym_link => { |
| 211 | 235 | const rel_path = try std.fs.path.relative(allocator, target_include_dir, full_path); |
| 212 | 236 | const max_size = 2 * 1024 * 1024 * 1024; |
| 213 | 237 | const raw_bytes = try std.fs.cwd().readFileAlloc(allocator, full_path, max_size); |
| ... | ... | @@ -315,6 +339,6 @@ fn usageAndExit(arg0: []const u8) noreturn { |
| 315 | 339 | std.debug.print("--search-path can be used any number of times.\n", .{}); |
| 316 | 340 | std.debug.print(" subdirectories of search paths look like, e.g. x86_64-linux-gnu\n", .{}); |
| 317 | 341 | 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", .{}); |
| 319 | 343 | std.process.exit(1); |
| 320 | 344 | } |