1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
//! To get started, run this tool with no args and read the help message.
//!
//! The build systems of musl-libc and glibc 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 musl/glibc build systems to create installations of all the
//! targets in the `glibc_targets`/`musl_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.
const std = @import("std");
const Arch = std.Target.Cpu.Arch;
const Abi = std.Target.Abi;
const OsTag = std.Target.Os.Tag;
const assert = std.debug.assert;
const Blake3 = std.crypto.hash.Blake3;
const LibCTarget = struct {
name: []const u8,
arch: MultiArch,
abi: MultiAbi,
};
const MultiArch = union(enum) {
aarch64,
arm,
mips,
mips64,
powerpc64,
specific: Arch,
fn eql(a: MultiArch, b: MultiArch) bool {
if (@enumToInt(a) != @enumToInt(b))
return false;
if (a != .specific)
return true;
return a.specific == b.specific;
}
};
const MultiAbi = union(enum) {
musl,
specific: Abi,
fn eql(a: MultiAbi, b: MultiAbi) bool {
if (@enumToInt(a) != @enumToInt(b))
return false;
if (@TagType(MultiAbi)(a) != .specific)
return true;
return a.specific == b.specific;
}
};
const glibc_targets = [_]LibCTarget{
LibCTarget{
.name = "aarch64_be-linux-gnu",
.arch = MultiArch{ .specific = Arch.aarch64_be },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "aarch64-linux-gnu",
.arch = MultiArch{ .specific = Arch.aarch64 },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "armeb-linux-gnueabi",
.arch = MultiArch{ .specific = Arch.armeb },
.abi = MultiAbi{ .specific = Abi.gnueabi },
},
LibCTarget{
.name = "armeb-linux-gnueabihf",
.arch = MultiArch{ .specific = Arch.armeb },
.abi = MultiAbi{ .specific = Abi.gnueabihf },
},
LibCTarget{
.name = "arm-linux-gnueabi",
.arch = MultiArch{ .specific = Arch.arm },
.abi = MultiAbi{ .specific = Abi.gnueabi },
},
LibCTarget{
.name = "arm-linux-gnueabihf",
.arch = MultiArch{ .specific = Arch.arm },
.abi = MultiAbi{ .specific = Abi.gnueabihf },
},
LibCTarget{
.name = "i686-linux-gnu",
.arch = MultiArch{ .specific = Arch.i386 },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "mips64el-linux-gnu-n32",
.arch = MultiArch{ .specific = Arch.mips64el },
.abi = MultiAbi{ .specific = Abi.gnuabin32 },
},
LibCTarget{
.name = "mips64el-linux-gnu-n64",
.arch = MultiArch{ .specific = Arch.mips64el },
.abi = MultiAbi{ .specific = Abi.gnuabi64 },
},
LibCTarget{
.name = "mips64-linux-gnu-n32",
.arch = MultiArch{ .specific = Arch.mips64 },
.abi = MultiAbi{ .specific = Abi.gnuabin32 },
},
LibCTarget{
.name = "mips64-linux-gnu-n64",
.arch = MultiArch{ .specific = Arch.mips64 },
.abi = MultiAbi{ .specific = Abi.gnuabi64 },
},
LibCTarget{
.name = "mipsel-linux-gnu",
.arch = MultiArch{ .specific = Arch.mipsel },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "mips-linux-gnu",
.arch = MultiArch{ .specific = Arch.mips },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "powerpc64le-linux-gnu",
.arch = MultiArch{ .specific = Arch.powerpc64le },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "powerpc64-linux-gnu",
.arch = MultiArch{ .specific = Arch.powerpc64 },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "powerpc-linux-gnu",
.arch = MultiArch{ .specific = Arch.powerpc },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "riscv64-linux-gnu-rv64imac-lp64",
.arch = MultiArch{ .specific = Arch.riscv64 },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "s390x-linux-gnu",
.arch = MultiArch{ .specific = Arch.s390x },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "sparc64-linux-gnu",
.arch = MultiArch{ .specific = Arch.sparc },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "sparcv9-linux-gnu",
.arch = MultiArch{ .specific = Arch.sparcv9 },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "x86_64-linux-gnu",
.arch = MultiArch{ .specific = Arch.x86_64 },
.abi = MultiAbi{ .specific = Abi.gnu },
},
LibCTarget{
.name = "x86_64-linux-gnu-x32",
.arch = MultiArch{ .specific = Arch.x86_64 },
.abi = MultiAbi{ .specific = Abi.gnux32 },
},
};
const musl_targets = [_]LibCTarget{
LibCTarget{
.name = "aarch64",
.arch = MultiArch.aarch64,
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "arm",
.arch = MultiArch.arm,
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "i386",
.arch = MultiArch{ .specific = .i386 },
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "mips",
.arch = MultiArch.mips,
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "mips64",
.arch = MultiArch.mips64,
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "powerpc",
.arch = MultiArch{ .specific = .powerpc },
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "powerpc64",
.arch = MultiArch.powerpc64,
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "riscv64",
.arch = MultiArch{ .specific = .riscv64 },
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "s390x",
.arch = MultiArch{ .specific = .s390x },
.abi = MultiAbi.musl,
},
LibCTarget{
.name = "x86_64",
.arch = MultiArch{ .specific = .x86_64 },
.abi = MultiAbi.musl,
},
};
const DestTarget = struct {
arch: MultiArch,
os: OsTag,
abi: Abi,
fn hash(a: DestTarget) u32 {
return @enumToInt(a.arch) +%
(@enumToInt(a.os) *% @as(u32, 4202347608)) +%
(@enumToInt(a.abi) *% @as(u32, 4082223418));
}
fn eql(a: DestTarget, b: DestTarget) bool {
return a.arch.eql(b.arch) and
a.os == b.os and
a.abi == b.abi;
}
};
const Contents = struct {
bytes: []const u8,
hit_count: usize,
hash: []const u8,
is_generic: bool,
fn hitCountLessThan(context: void, lhs: *const Contents, rhs: *const Contents) bool {
return lhs.hit_count < rhs.hit_count;
}
};
const HashToContents = std.StringHashMap(Contents);
const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql, true);
const PathTable = std.StringHashMap(*TargetToHash);
const LibCVendor = enum {
musl,
glibc,
};
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = &arena.allocator;
const args = try std.process.argsAlloc(allocator);
var search_paths = std.ArrayList([]const u8).init(allocator);
var opt_out_dir: ?[]const u8 = null;
var opt_abi: ?[]const u8 = null;
var arg_i: usize = 1;
while (arg_i < args.len) : (arg_i += 1) {
if (std.mem.eql(u8, args[arg_i], "--help"))
usageAndExit(args[0]);
if (arg_i + 1 >= args.len) {
std.debug.warn("expected argument after '{}'\n", .{args[arg_i]});
usageAndExit(args[0]);
}
if (std.mem.eql(u8, args[arg_i], "--search-path")) {
try search_paths.append(args[arg_i + 1]);
} else if (std.mem.eql(u8, args[arg_i], "--out")) {
assert(opt_out_dir == null);
opt_out_dir = args[arg_i + 1];
} else if (std.mem.eql(u8, args[arg_i], "--abi")) {
assert(opt_abi == null);
opt_abi = args[arg_i + 1];
} else {
std.debug.warn("unrecognized argument: {}\n", .{args[arg_i]});
usageAndExit(args[0]);
}
arg_i += 1;
}
const out_dir = opt_out_dir orelse usageAndExit(args[0]);
const abi_name = opt_abi orelse usageAndExit(args[0]);
const vendor = if (std.mem.eql(u8, abi_name, "musl"))
LibCVendor.musl
else if (std.mem.eql(u8, abi_name, "glibc"))
LibCVendor.glibc
else {
std.debug.warn("unrecognized C ABI: {}\n", .{abi_name});
usageAndExit(args[0]);
};
const generic_name = try std.fmt.allocPrint(allocator, "generic-{}", .{abi_name});
// TODO compiler crashed when I wrote this the canonical way
var libc_targets: []const LibCTarget = undefined;
switch (vendor) {
.musl => libc_targets = &musl_targets,
.glibc => libc_targets = &glibc_targets,
}
var path_table = PathTable.init(allocator);
var hash_to_contents = HashToContents.init(allocator);
var max_bytes_saved: usize = 0;
var total_bytes: usize = 0;
var hasher = Blake3.init(.{});
for (libc_targets) |libc_target| {
const dest_target = DestTarget{
.arch = libc_target.arch,
.abi = switch (vendor) {
.musl => .musl,
.glibc => libc_target.abi.specific,
},
.os = .linux,
};
search: for (search_paths.span()) |search_path| {
var sub_path: []const []const u8 = undefined;
switch (vendor) {
.musl => {
sub_path = &[_][]const u8{ search_path, libc_target.name, "usr", "local", "musl", "include" };
},
.glibc => {
sub_path = &[_][]const u8{ search_path, libc_target.name, "usr", "include" };
},
}
const target_include_dir = try std.fs.path.join(allocator, sub_path);
var dir_stack = std.ArrayList([]const u8).init(allocator);
try dir_stack.append(target_include_dir);
while (dir_stack.popOrNull()) |full_dir_name| {
var dir = std.fs.cwd().openDir(full_dir_name, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => continue :search,
error.AccessDenied => continue :search,
else => return err,
};
defer dir.close();
var dir_it = dir.iterate();
while (try dir_it.next()) |entry| {
const full_path = try std.fs.path.join(allocator, &[_][]const u8{ full_dir_name, entry.name });
switch (entry.kind) {
.Directory => try dir_stack.append(full_path),
.File => {
const rel_path = try std.fs.path.relative(allocator, target_include_dir, full_path);
const max_size = 2 * 1024 * 1024 * 1024;
const raw_bytes = try std.fs.cwd().readFileAlloc(allocator, full_path, max_size);
const trimmed = std.mem.trim(u8, raw_bytes, " \r\n\t");
total_bytes += raw_bytes.len;
const hash = try allocator.alloc(u8, 32);
hasher = Blake3.init(.{});
hasher.update(rel_path);
hasher.update(trimmed);
hasher.final(hash);
const gop = try hash_to_contents.getOrPut(hash);
if (gop.found_existing) {
max_bytes_saved += raw_bytes.len;
gop.entry.value.hit_count += 1;
std.debug.warn("duplicate: {} {} ({Bi:2})\n", .{
libc_target.name,
rel_path,
raw_bytes.len,
});
} else {
gop.entry.value = Contents{
.bytes = trimmed,
.hit_count = 1,
.hash = hash,
.is_generic = false,
};
}
const path_gop = try path_table.getOrPut(rel_path);
const target_to_hash = if (path_gop.found_existing) path_gop.entry.value else blk: {
const ptr = try allocator.create(TargetToHash);
ptr.* = TargetToHash.init(allocator);
path_gop.entry.value = ptr;
break :blk ptr;
};
try target_to_hash.putNoClobber(dest_target, hash);
},
else => std.debug.warn("warning: weird file: {}\n", .{full_path}),
}
}
}
break;
} else {
std.debug.warn("warning: libc target not found: {}\n", .{libc_target.name});
}
}
std.debug.warn("summary: {Bi:2} could be reduced to {Bi:2}\n", .{ total_bytes, total_bytes - max_bytes_saved });
try std.fs.cwd().makePath(out_dir);
var missed_opportunity_bytes: usize = 0;
// iterate path_table. for each path, put all the hashes into a list. sort by hit_count.
// the hash with the highest hit_count gets to be the "generic" one. everybody else
// gets their header in a separate arch directory.
var path_it = path_table.iterator();
while (path_it.next()) |path_kv| {
var contents_list = std.ArrayList(*Contents).init(allocator);
{
var hash_it = path_kv.value.iterator();
while (hash_it.next()) |hash_kv| {
const contents = &hash_to_contents.getEntry(hash_kv.value).?.value;
try contents_list.append(contents);
}
}
std.sort.sort(*Contents, contents_list.span(), {}, Contents.hitCountLessThan);
const best_contents = contents_list.popOrNull().?;
if (best_contents.hit_count > 1) {
// worth it to make it generic
const full_path = try std.fs.path.join(allocator, &[_][]const u8{ out_dir, generic_name, path_kv.key });
try std.fs.cwd().makePath(std.fs.path.dirname(full_path).?);
try std.fs.cwd().writeFile(full_path, best_contents.bytes);
best_contents.is_generic = true;
while (contents_list.popOrNull()) |contender| {
if (contender.hit_count > 1) {
const this_missed_bytes = contender.hit_count * contender.bytes.len;
missed_opportunity_bytes += this_missed_bytes;
std.debug.warn("Missed opportunity ({Bi:2}): {}\n", .{ this_missed_bytes, path_kv.key });
} else break;
}
}
var hash_it = path_kv.value.iterator();
while (hash_it.next()) |hash_kv| {
const contents = &hash_to_contents.getEntry(hash_kv.value).?.value;
if (contents.is_generic) continue;
const dest_target = hash_kv.key;
const arch_name = switch (dest_target.arch) {
.specific => |a| @tagName(a),
else => @tagName(dest_target.arch),
};
const out_subpath = try std.fmt.allocPrint(allocator, "{}-{}-{}", .{
arch_name,
@tagName(dest_target.os),
@tagName(dest_target.abi),
});
const full_path = try std.fs.path.join(allocator, &[_][]const u8{ out_dir, out_subpath, path_kv.key });
try std.fs.cwd().makePath(std.fs.path.dirname(full_path).?);
try std.fs.cwd().writeFile(full_path, contents.bytes);
}
}
}
fn usageAndExit(arg0: []const u8) noreturn {
std.debug.warn("Usage: {} [--search-path <dir>] --out <dir> --abi <name>\n", .{arg0});
std.debug.warn("--search-path can be used any number of times.\n", .{});
std.debug.warn(" subdirectories of search paths look like, e.g. x86_64-linux-gnu\n", .{});
std.debug.warn("--out is a dir that will be created, and populated with the results\n", .{});
std.debug.warn("--abi is either musl or glibc\n", .{});
std.process.exit(1);
}
|