Skip to content

Commit c5026b4

Browse files
committed
Support non zig dependencies
Dependencies no longer require a build.zig file. Adds path function to Dependency struct which returns a LazyPath into a dependency.
1 parent fc6e575 commit c5026b4

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

lib/std/Build.zig

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,15 @@ pub const Dependency = struct {
15561556
panic("unable to find module '{s}'", .{name});
15571557
};
15581558
}
1559+
1560+
pub fn path(d: *Dependency, sub_path: []const u8) LazyPath {
1561+
return .{
1562+
.dependency = .{
1563+
.dependency = d,
1564+
.sub_path = sub_path,
1565+
},
1566+
};
1567+
}
15591568
};
15601569

15611570
pub fn dependency(b: *Build, name: []const u8, args: anytype) *Dependency {
@@ -1573,6 +1582,16 @@ pub fn dependency(b: *Build, name: []const u8, args: anytype) *Dependency {
15731582
}
15741583
}
15751584

1585+
inline for (@typeInfo(deps.build_root).Struct.decls) |decl| {
1586+
if (mem.startsWith(u8, decl.name, b.dep_prefix) and
1587+
mem.endsWith(u8, decl.name, name) and
1588+
decl.name.len == b.dep_prefix.len + name.len)
1589+
{
1590+
const build_root = @field(deps.build_root, decl.name);
1591+
return dependencyInner(b, name, build_root, null, args);
1592+
}
1593+
}
1594+
15761595
const full_path = b.pathFromRoot("build.zig.zon");
15771596
std.debug.print("no dependency named '{s}' in '{s}'. All packages used in build.zig must be declared in this file.\n", .{ name, full_path });
15781597
process.exit(1);
@@ -1601,7 +1620,7 @@ pub fn dependencyInner(
16011620
b: *Build,
16021621
name: []const u8,
16031622
build_root_string: []const u8,
1604-
comptime build_zig: type,
1623+
comptime maybe_build_zig: ?type,
16051624
args: anytype,
16061625
) *Dependency {
16071626
if (b.initialized_deps.get(build_root_string)) |dep| {
@@ -1619,10 +1638,12 @@ pub fn dependencyInner(
16191638
},
16201639
};
16211640
const sub_builder = b.createChild(name, build_root, args) catch @panic("unhandled error");
1622-
sub_builder.runBuild(build_zig) catch @panic("unhandled error");
1641+
if (maybe_build_zig) |build_zig| {
1642+
sub_builder.runBuild(build_zig) catch @panic("unhandled error");
16231643

1624-
if (sub_builder.validateUserInputDidItFail()) {
1625-
std.debug.dumpCurrentStackTrace(@returnAddress());
1644+
if (sub_builder.validateUserInputDidItFail()) {
1645+
std.debug.dumpCurrentStackTrace(@returnAddress());
1646+
}
16261647
}
16271648

16281649
const dep = b.allocator.create(Dependency) catch @panic("OOM");
@@ -1687,6 +1708,11 @@ pub const LazyPath = union(enum) {
16871708
/// Use of this tag indicates a dependency on the host system.
16881709
cwd_relative: []const u8,
16891710

1711+
dependency: struct {
1712+
dependency: *Dependency,
1713+
sub_path: []const u8,
1714+
},
1715+
16901716
/// Returns a new file source that will have a relative path to the build root guaranteed.
16911717
/// Asserts the parameter is not an absolute path.
16921718
pub fn relative(path: []const u8) LazyPath {
@@ -1700,13 +1726,14 @@ pub const LazyPath = union(enum) {
17001726
return switch (self) {
17011727
.path, .cwd_relative => self.path,
17021728
.generated => "generated",
1729+
.dependency => "dependency",
17031730
};
17041731
}
17051732

17061733
/// Adds dependencies this file source implies to the given step.
17071734
pub fn addStepDependencies(self: LazyPath, other_step: *Step) void {
17081735
switch (self) {
1709-
.path, .cwd_relative => {},
1736+
.path, .cwd_relative, .dependency => {},
17101737
.generated => |gen| other_step.dependOn(gen.step),
17111738
}
17121739
}
@@ -1732,6 +1759,12 @@ pub const LazyPath = union(enum) {
17321759
dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {};
17331760
@panic("misconfigured build script");
17341761
},
1762+
.dependency => |dep| {
1763+
return dep.dependency.builder.pathJoin(&[_][]const u8{
1764+
dep.dependency.builder.build_root.path.?,
1765+
dep.sub_path,
1766+
});
1767+
},
17351768
}
17361769
}
17371770

@@ -1741,6 +1774,7 @@ pub const LazyPath = union(enum) {
17411774
.path => |p| .{ .path = b.dupePath(p) },
17421775
.cwd_relative => |p| .{ .cwd_relative = b.dupePath(p) },
17431776
.generated => |gen| .{ .generated = gen },
1777+
.dependency => |dep| .{ .dependency = dep },
17441778
};
17451779
}
17461780
};

lib/std/Build/Step/Compile.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
18411841
continue;
18421842
}
18431843
},
1844-
.generated => {},
1844+
.generated, .dependency => {},
18451845
};
18461846

18471847
zig_args.appendAssumeCapacity(rpath.getPath2(b, step));

src/Compilation.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,11 @@ fn addPackageTableToCacheHash(
700700
const pkg_zig_file = try pkg.value.root_src_directory.join(allocator, &[_][]const u8{
701701
pkg.value.root_src_path,
702702
});
703-
_ = try man.addFile(pkg_zig_file, null);
703+
blk: {
704+
pkg.value.root_src_directory.handle.access(pkg.value.root_src_path, .{}) catch break :blk;
705+
706+
_ = try man.addFile(pkg_zig_file, null);
707+
}
704708
},
705709
}
706710
// Recurse to handle the package's dependencies

src/Package.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,12 @@ pub fn fetchAndAddDependencies(
330330
try deps_pkg.add(gpa, dep.hash.?, sub.mod);
331331
}
332332

333-
try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{
334-
std.zig.fmtId(fqn), std.zig.fmtEscapes(dep.hash.?),
335-
});
333+
blk: {
334+
sub.mod.root_src_directory.handle.access(build_zig_basename, .{}) catch break :blk;
335+
try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{
336+
std.zig.fmtId(fqn), std.zig.fmtEscapes(dep.hash.?),
337+
});
338+
}
336339
}
337340
}
338341

0 commit comments

Comments
 (0)