-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .unpack = false
and --no-unpack to build.zig.zon and zig fetch
#22042
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) !void { | ||
const test_step = b.step("test", "Test it"); | ||
b.default_step = test_step; | ||
|
||
{ | ||
const codegen_exe = b.addExecutable(.{ | ||
.name = "codegen", | ||
.target = b.host, | ||
.root_source_file = b.path("codegen.zig"), | ||
}); | ||
const run_codegen = b.addRunArtifact(codegen_exe); | ||
const example_dir = run_codegen.addOutputDirectoryArg("example"); | ||
|
||
const run = b.addSystemCommand(&.{ | ||
b.graph.zig_exe, | ||
"build", | ||
"install", | ||
"--build-file", | ||
}); | ||
run.addFileArg(example_dir.path(b, "build.zig")); | ||
run.addArg("--prefix"); | ||
const install_dir = run.addOutputDirectoryArg("install"); | ||
const check_file = b.addCheckFile(install_dir.path(b, "example_dep_file.txt"), .{ | ||
.expected_exact = "This is an example file.\n", | ||
}); | ||
test_step.dependOn(&check_file.step); | ||
} | ||
|
||
{ | ||
const run = b.addSystemCommand(&.{ | ||
b.graph.zig_exe, | ||
"build", | ||
"--build-file", | ||
}); | ||
run.addFileArg(b.path("unpacktrue/build.zig")); | ||
run.addCheck(.{ .expect_stderr_match = "error: unpack cannot be set to true, omit it instead" }); | ||
test_step.dependOn(&run.step); | ||
} | ||
|
||
{ | ||
const run = b.addSystemCommand(&.{ | ||
b.graph.zig_exe, | ||
"fetch", | ||
"--no-unpack", | ||
}); | ||
run.addFileArg(b.path("example/example_dep_file.txt")); | ||
run.expectStdOutEqual("12200f68aca70ebc76057200af436aab5720ec53a780713c5dc614825db42a39dbfb\n"); | ||
test_step.dependOn(&run.step); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
const arena = arena_instance.allocator(); | ||
const args = try std.process.argsAlloc(arena); | ||
std.debug.assert(args.len == 2); | ||
const out_dir = args[1]; | ||
|
||
if (!std.fs.path.isAbsolute(out_dir)) { | ||
std.log.err("directory '{s}' must be absolute", .{out_dir}); | ||
std.process.exit(0xff); | ||
} | ||
|
||
var dir = try std.fs.openDirAbsolute(out_dir, .{}); | ||
defer dir.close(); | ||
|
||
try writeFile(dir, "build.zig", @embedFile("example/build.zig")); | ||
try writeFile(dir, "example_dep_file.txt", @embedFile("example/example_dep_file.txt")); | ||
|
||
{ | ||
const template = @embedFile("example/build.zig.zon.template"); | ||
const package_path_absolute = try arena.dupe(u8, out_dir); | ||
for (package_path_absolute) |*c| { | ||
c.* = if (c.* == '\\') '/' else c.*; | ||
} | ||
const content = try std.mem.replaceOwned(u8, arena, template, "<PACKAGE_PATH_ABSOLUTE>", package_path_absolute); | ||
try writeFile(dir, "build.zig.zon", content); | ||
} | ||
} | ||
|
||
fn writeFile(dir: std.fs.Dir, name: []const u8, content: []const u8) !void { | ||
const file = try dir.createFile(name, .{}); | ||
defer file.close(); | ||
try file.writer().writeAll(content); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const dep = b.dependency("somedependency", .{}); | ||
b.getInstallStep().dependOn(&b.addInstallFile( | ||
dep.path("example_dep_file.txt"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if one is necessarily better than the other, but personally, if I have a dependency like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with the solution that I didn't have to add any code for as the existing code paths already supported |
||
"example_dep_file.txt", | ||
).step); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.{ | ||
.name = "fetch", | ||
.version = "0.0.0", | ||
.dependencies = .{ | ||
.somedependency = .{ | ||
.url = "file://<PACKAGE_PATH_ABSOLUTE>/example_dep_file.txt", | ||
.hash = "12200f68aca70ebc76057200af436aab5720ec53a780713c5dc614825db42a39dbfb", | ||
.unpack = false, | ||
}, | ||
}, | ||
.paths = .{ | ||
"build.zig", | ||
"build.zig.zon", | ||
"example_dep_file.txt", | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is an example file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(_: *std.Build) void {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.{ | ||
.name = "unpacktrue", | ||
.version = "0.0.0", | ||
.dependencies = .{ | ||
.somedependency = .{ | ||
.url = "file://localhost/bar", | ||
.hash = "1220f3b02ca452c26a96b48d2912b7fc907bef8d0b85c2e8f7e4a5c8bd95cdbfbae6", | ||
.unpack = true, | ||
}, | ||
}, | ||
.paths = .{ | ||
"build.zig", | ||
"build.zig.zon", | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.lazy = false
is allowed despite it being the default, so it seems inconsistent that.unpack = true
is an error. Either both should be forbidden or both should be allowed. I don't think there's any harm in allowing.unpack = true
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
unpack
field should also be documented in https://github.com/ziglang/zig/blob/master/doc/build.zig.zon.md and https://github.com/ziglang/zig/blob/master/lib/init/build.zig.zon.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think anytime you can decrease the "surface area" of an interface it's a win, failing to do so does more harm than people realize.