std.Build: add dependencyFromBuildZig#18908
Merged
andrewrk merged 1 commit intoziglang:masterfrom Apr 7, 2024
Merged
Conversation
castholm
commented
Feb 12, 2024
| 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| { |
Contributor
Author
There was a problem hiding this comment.
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.
492f551 to
47c46e2
Compare
dependencyFromBuildZigdependencyFromBuildZig
dependencyFromBuildZigdependencyFromBuildZig
47c46e2 to
c887963
Compare
Contributor
Author
|
Quick repro for testing: // build.zig
const std = @import("std");
const foo = @import("foo");
pub fn build(b: *std.Build) void {
b.getInstallStep().dependOn(&b.addInstallFile(foo.importantFile(b), "file.txt").step);
}// build.zig.zon
.{
.name = "repro",
.version = "0.0.0",
.dependencies = .{
.foo = .{
.path = "foo",
},
},
.paths = .{""},
}// foo/build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
_ = b;
}
pub fn importantFile(b: *std.Build) std.Build.LazyPath {
const this_dep = b.dependencyFromBuildZig(@This(), .{});
return this_dep.path("file.txt");
}// foo/file.txt
123 |
c887963 to
7f6a972
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Given a struct that corresponds to the build.zig of a dependency,
b.dependencyFromBuildZigreturns that same dependency. In other words, if you have already a@imported a depdency's build.zig, you can use this function to obtain the correspondingDependency:This function is also important for packages that expose functions from their build.zig files that need to use their corresponding
Dependency(for example, for package-relative paths, or for running system commands and returning the output as lazy paths). This would be accomplished through:Currently, such use cases need to either
1. take a
Dependencyas argumentwhich is prone to user error,
2. call
b.dependencywith a hard-coded namewhich only works if the consumer has added that package as a dependency under that exact name (i.e. it must be
.zinein the build.zig.zon, not.zine_ssgor some other name the consumer may have chosen), or3. implement the same logic as
dependencyFromBuildZig, which requires the user to know how the undocumented@import("root").dependenciesworks or that it's even a thing.There's currently no way to test this in the compiler without setting up a build.zig.zon and some local packages, but I have tested this function with a few packages of my own locally and can report that it works exactly as advertised.