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