authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-02-13 00:02:52+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-07 15:54:30-07:00
log05126fc4c575b833e772239426f31af23eba0d20
tree815a173d0b8ad6c24fd7b36bf9886f1c8796deb7
parente204a6edb8866093a087ef2325a5d6ea1824487c

std.Build: add dependencyFromBuildZig


1 files changed, 26 insertions(+), 0 deletions(-)

lib/std/Build.zig+26
......@@ -1947,6 +1947,32 @@ pub inline fn lazyImport(
19471947 comptime unreachable; // Bad @dependencies source
19481948}
19491949
1950pub fn dependencyFromBuildZig(
1951 b: *Build,
1952 /// The build.zig struct of the dependency, normally obtained by `@import` of the dependency.
1953 /// If called from the build.zig file itself, use `@This` to obtain a reference to the struct.
1954 comptime build_zig: type,
1955 args: anytype,
1956) *Dependency {
1957 const build_runner = @import("root");
1958 const deps = build_runner.dependencies;
1959
1960 find_dep: {
1961 const pkg, const pkg_hash = inline for (@typeInfo(deps.packages).Struct.decls) |decl| {
1962 const pkg_hash = decl.name;
1963 const pkg = @field(deps.packages, pkg_hash);
1964 if (@hasDecl(pkg, "build_zig") and pkg.build_zig == build_zig) break .{ pkg, pkg_hash };
1965 } else break :find_dep;
1966 const dep_name = for (b.available_deps) |dep| {
1967 if (mem.eql(u8, dep[1], pkg_hash)) break dep[1];
1968 } else break :find_dep;
1969 return dependencyInner(b, dep_name, pkg.build_root, pkg.build_zig, pkg.deps, args);
1970 }
1971
1972 const full_path = b.pathFromRoot("build.zig.zon");
1973 debug.panic("'{}' is not a build.zig struct of a dependecy in '{s}'", .{ build_zig, full_path });
1974}
1975
19501976pub fn anonymousDependency(
19511977 b: *Build,
19521978 /// The path to the directory containing the dependency's build.zig file,