Skip to content
This repository was archived by the owner on Jan 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ macos-13, macos-14 ]
os: [ macos-13, macos-14, macos-15 ]

steps:
- uses: actions/checkout@v3
Expand Down
19 changes: 19 additions & 0 deletions src/Dylib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,25 @@ pub const Id = struct {

return out;
}

pub fn eql(id: Id, other: Id) bool {
return mem.eql(u8, id.name, other.name) and
id.timestamp == other.timestamp and
id.current_version == other.current_version and
id.compatibility_version == other.compatibility_version;
}

/// Hashes the Id.
/// TODO: we currently do differentiate between dylibs installed at the *same* path but having different
/// versions. This might be wrong and we should dedup them. Then again, surely this should be an error, right?
pub fn hash(id: Id) u64 {
var hasher = std.hash.Wyhash.init(0);
hasher.update(id.name);
hasher.update(mem.asBytes(&id.timestamp));
hasher.update(mem.asBytes(&id.current_version));
hasher.update(mem.asBytes(&id.compatibility_version));
return hasher.final();
}
};

const Export = struct {
Expand Down
53 changes: 53 additions & 0 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub fn link(self: *MachO) !void {
}

try self.parseInputFiles();
try self.dedupDylibs(resolved_objects.items);
try self.parseDependentDylibs(arena, lib_dirs.items, framework_dirs.items);

if (!self.options.relocatable) {
Expand Down Expand Up @@ -832,6 +833,58 @@ fn isHoisted(self: *MachO, install_name: []const u8) bool {
return false;
}

fn dedupDylibs(self: *MachO, resolved_objects: []const LinkObject) !void {
const tracy = trace(@src());
defer tracy.end();

var map = std.HashMap(Dylib.Id, void, struct {
pub fn hash(ctx: @This(), id: Dylib.Id) u64 {
_ = ctx;
return id.hash();
}

pub fn eql(ctx: @This(), id: Dylib.Id, other: Dylib.Id) bool {
_ = ctx;
return id.eql(other);
}
}, std.hash_map.default_max_load_percentage).init(self.allocator);
defer map.deinit();
try map.ensureTotalCapacity(@intCast(self.dylibs.items.len));

var marked_dylibs = std.ArrayList(bool).init(self.allocator);
defer marked_dylibs.deinit();
try marked_dylibs.ensureTotalCapacityPrecise(self.dylibs.items.len);
marked_dylibs.resize(self.dylibs.items.len) catch unreachable;
@memset(marked_dylibs.items, false);

for (self.dylibs.items, marked_dylibs.items) |index, *marker| {
const dylib = self.getFile(index).dylib;
const cmd_object = resolved_objects[@intFromEnum(index)];

const gop = map.getOrPutAssumeCapacity(dylib.id.?);

if (!gop.found_existing) continue;

if (cmd_object.tag == .lib) {
self.warn("ignoring duplicate libraries: {}", .{cmd_object});
}

marker.* = true;
}

var i: usize = 0;
while (i < self.dylibs.items.len) {
const index = self.dylibs.items[i];
const marker = marked_dylibs.items[i];
if (marker) {
_ = self.dylibs.orderedRemove(i);
_ = marked_dylibs.orderedRemove(i);
self.files.items(.data)[@intFromEnum(index)].dylib.deinit(self.allocator);
self.files.set(@intFromEnum(index), .none);
} else i += 1;
}
}

fn parseDependentDylibs(
self: *MachO,
arena: Allocator,
Expand Down
56 changes: 55 additions & 1 deletion test/macho.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub fn addTests(step: *Step, opts: Options) void {
step.dependOn(testBuildVersionIOS(b, opts));
step.dependOn(testDeadStrip(b, opts));
step.dependOn(testDeadStripDylibs(b, opts));
step.dependOn(testDedupDylibs(b, opts));
step.dependOn(testDylib(b, opts));
step.dependOn(testDylibReexport(b, opts));
step.dependOn(testDylibReexportDeep(b, opts));
Expand Down Expand Up @@ -384,6 +385,60 @@ fn testDeadStripDylibs(b: *Build, opts: Options) *Step {
return test_step;
}

fn testDedupDylibs(b: *Build, opts: Options) *Step {
const test_step = b.step("test-macho-dedup-dylibs", "");

const obj = cc(b, "a.o", opts);
obj.addArg("-c");
obj.addCSource(
\\char world[] = "world";
\\char* hello() {
\\ return "Hello";
\\}
);

const dylib = ld(b, "liba.dylib", opts);
dylib.addFileSource(obj.getFile());
dylib.addArgs(&.{
"-dynamic",
"-syslibroot",
opts.macos_sdk,
"-dylib",
"-install_name",
"@rpath/liba.dylib",
"-lSystem",
"-lSystem",
"-lc",
});

const check = dylib.check();
check.checkInHeaders();
// Check that we only have one copy of libSystem present
check.checkContains("libSystem");
check.checkNotPresent("libSystem");
test_step.dependOn(&check.step);

const exe = cc(b, "main", opts);
exe.addCSource(
\\#include<stdio.h>
\\char* hello();
\\extern char world[];
\\int main() {
\\ printf("%s %s", hello(), world);
\\ return 0;
\\}
);
exe.addArg("-la");
exe.addPrefixedDirectorySource("-L", dylib.getDir());
exe.addPrefixedDirectorySource("-Wl,-rpath,", dylib.getDir());

const run = exe.run();
run.expectStdOutEqual("Hello world");
test_step.dependOn(run.step());

return test_step;
}

fn testDylib(b: *Build, opts: Options) *Step {
const test_step = b.step("test-macho-dylib", "");

Expand Down Expand Up @@ -2801,7 +2856,6 @@ fn testSearchStrategy(b: *Build, opts: Options) *Step {
const obj = cc(b, "a.o", opts);
obj.addArg("-c");
obj.addCSource(
\\#include<stdio.h>
\\char world[] = "world";
\\char* hello() {
\\ return "Hello";
Expand Down