Skip to content
Merged
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
26 changes: 26 additions & 0 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,32 @@ pub inline fn lazyImport(
comptime unreachable; // Bad @dependencies source
}

pub fn dependencyFromBuildZig(
b: *Build,
/// The build.zig struct of the dependency, normally obtained by `@import` of the dependency.
/// If called from the build.zig file itself, use `@This` to obtain a reference to the struct.
comptime build_zig: type,
args: anytype,
) *Dependency {
const build_runner = @import("root");
const deps = build_runner.dependencies;

find_dep: {
const pkg, const pkg_hash = inline for (@typeInfo(deps.packages).Struct.decls) |decl| {
const pkg_hash = decl.name;
const pkg = @field(deps.packages, pkg_hash);
if (@hasDecl(pkg, "build_zig") and pkg.build_zig == build_zig) break .{ pkg, pkg_hash };
} else break :find_dep;
const dep_name = for (b.available_deps) |dep| {
Copy link
Copy Markdown
Contributor Author

@castholm castholm Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should add that there's no need to check for @hasDecl(pkg, "available") because if we get this far, we must have @imported the dependency and know for sure that the package is already fetched even if it's a lazy dependency.

if (mem.eql(u8, dep[1], pkg_hash)) break dep[1];
} else break :find_dep;
return dependencyInner(b, dep_name, pkg.build_root, pkg.build_zig, pkg.deps, args);
}

const full_path = b.pathFromRoot("build.zig.zon");
debug.panic("'{}' is not a build.zig struct of a dependecy in '{s}'", .{ build_zig, full_path });
}

pub fn anonymousDependency(
b: *Build,
/// The path to the directory containing the dependency's build.zig file,
Expand Down