Skip to content

Commit

Permalink
Resolve order-of-call dependencies in build.zig
Browse files Browse the repository at this point in the history
  • Loading branch information
moosichu authored and Vexu committed Aug 20, 2021
1 parent ddaca72 commit 0cecdca
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,6 @@ pub const LibExeObjStep = struct {
}

pub fn linkFramework(self: *LibExeObjStep, framework_name: []const u8) void {
assert(self.target.isDarwin());
// Note: No need to dupe because frameworks dupes internally.
self.frameworks.insert(framework_name) catch unreachable;
}
Expand Down Expand Up @@ -2247,28 +2246,6 @@ pub const LibExeObjStep = struct {
self.step.dependOn(&other.step);
self.link_objects.append(.{ .other_step = other }) catch unreachable;
self.include_dirs.append(.{ .other_step = other }) catch unreachable;

// BUG: The following code introduces a order-of-call dependency:
// var lib = addSharedLibrary(...);
// var exe = addExecutable(...);
// exe.linkLibrary(lib);
// lib.linkSystemLibrary("foobar"); // this will be ignored for exe!

// Inherit dependency on system libraries
for (other.link_objects.items) |link_object| {
switch (link_object) {
.system_lib => |name| self.linkSystemLibrary(name),
else => continue,
}
}

// Inherit dependencies on darwin frameworks
if (self.target.isDarwin() and !other.isDynamicLibrary()) {
var it = other.frameworks.iterator();
while (it.next()) |framework| {
self.frameworks.insert(framework.*) catch unreachable;
}
}
}

fn makePackageCmd(self: *LibExeObjStep, pkg: Pkg, zig_args: *ArrayList([]const u8)) error{OutOfMemory}!void {
Expand Down Expand Up @@ -2322,6 +2299,31 @@ pub const LibExeObjStep = struct {
if (self.root_src) |root_src| try zig_args.append(root_src.getPath(builder));

var prev_has_extra_flags = false;

// Resolve transitive dependencies
for (self.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |other| {
// Inherit dependency on system libraries
for (other.link_objects.items) |other_link_object| {
switch (other_link_object) {
.system_lib => |name| self.linkSystemLibrary(name),
else => continue,
}
}

// Inherit dependencies on darwin frameworks
if (!other.isDynamicLibrary()) {
var it = other.frameworks.iterator();
while (it.next()) |framework| {
self.frameworks.insert(framework.*) catch unreachable;
}
}
},
else => continue,
}
}

for (self.link_objects.items) |link_object| {
switch (link_object) {
.static_path => |static_path| try zig_args.append(static_path.getPath(builder)),
Expand Down Expand Up @@ -2719,6 +2721,14 @@ pub const LibExeObjStep = struct {
zig_args.append("-framework") catch unreachable;
zig_args.append(framework.*) catch unreachable;
}
} else {
if (self.framework_dirs.items.len > 0) {
warn("Framework directories have been added for a non-darwin target, this will have no affect on the build\n", .{});
}

if (self.frameworks.count() > 0) {
warn("Frameworks have been added for a non-darwin target, this will have no affect on the build\n", .{});
}
}

if (builder.sysroot) |sysroot| {
Expand Down

0 comments on commit 0cecdca

Please sign in to comment.