authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-18 21:58:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:35-07:00
logbb1b59ee1feecd5500847d394fc90842efe138ed
treea472dc6fb7a4fce1a411075d7ed11d78a122a892
parentdb7ceada15b007747bfd433e6635324edcec918e

Maker: implement Step.InstallDir


7 files changed, 103 insertions(+), 59 deletions(-)

BRANCH_TODO+1
......@@ -41,6 +41,7 @@
4141## Already Filed Followup Issues
4242* build system fmt step with check=false does not acquire a write lock on source files #35204
4343* enhance CheckFile step output when there is not a match #35208
44* missing truncate functionality #35353
4445
4546## Release Notes
4647
lib/compiler/Maker.zig+41-4
......@@ -1879,13 +1879,50 @@ pub fn installGenerated(
18791879 return installPath(maker, arena, src_path, dest_path, asking_step_index);
18801880}
18811881
1882pub fn truncatePath(
1883 maker: *Maker,
1884 arena: Allocator,
1885 dest_path: Path,
1886 asking_step_index: Configuration.Step.Index,
1887) Step.ExtendedMakeError!void {
1888 const graph = maker.graph;
1889 const io = graph.io;
1890 if (graph.verbose) try graph.handleVerbose(.inherit, null, &.{
1891 "truncate", try dest_path.toString(arena),
1892 });
1893 // https://codeberg.org/ziglang/zig/issues/35353
1894 const err = e: {
1895 var file = f: {
1896 break :f dest_path.root_dir.handle.createFile(io, dest_path.sub_path, .{}) catch |err| switch (err) {
1897 error.FileNotFound => {
1898 const parent_path = dest_path.dirname() orelse break :e err;
1899 parent_path.root_dir.handle.createDirPath(io, parent_path.sub_path) catch |in| switch (in) {
1900 error.Canceled => |e| return e,
1901 else => |e| {
1902 const s = stepByIndex(maker, asking_step_index);
1903 return s.fail(maker, "failed creating directory {f}: {t}", .{ parent_path, e });
1904 },
1905 };
1906 break :f dest_path.root_dir.handle.createFile(io, dest_path.sub_path, .{}) catch |in| break :e in;
1907 },
1908 error.Canceled => |e| return e,
1909 else => |e| break :e e,
1910 };
1911 };
1912 file.close(io);
1913 return;
1914 };
1915 const s = stepByIndex(maker, asking_step_index);
1916 return s.fail(maker, "failed truncating file {f}: {t}", .{ dest_path, err });
1917}
1918
18821919pub fn installPath(
18831920 maker: *Maker,
18841921 arena: Allocator,
18851922 src_path: Path,
18861923 dest_path: Path,
18871924 asking_step_index: Configuration.Step.Index,
1888) !Dir.PrevStatus {
1925) Step.ExtendedMakeError!Dir.PrevStatus {
18891926 const graph = maker.graph;
18901927 const io = graph.io;
18911928 if (graph.verbose) try graph.handleVerbose(.inherit, null, &.{
......@@ -1900,7 +1937,7 @@ pub fn installPath(
19001937 .{},
19011938 ) catch |err| {
19021939 const s = stepByIndex(maker, asking_step_index);
1903 return s.fail(maker, "unable to update file from {f} to {f}: {t}", .{ src_path, dest_path, err });
1940 return s.fail(maker, "failed updating file from {f} to {f}: {t}", .{ src_path, dest_path, err });
19041941 };
19051942}
19061943
......@@ -1910,7 +1947,7 @@ pub fn installDir(
19101947 arena: Allocator,
19111948 dest_path: Path,
19121949 asking_step_index: Configuration.Step.Index,
1913) !Dir.CreatePathStatus {
1950) Step.ExtendedMakeError!Dir.CreatePathStatus {
19141951 const graph = maker.graph;
19151952 const io = graph.io;
19161953 if (graph.verbose) try graph.handleVerbose(.inherit, null, &.{
......@@ -1918,7 +1955,7 @@ pub fn installDir(
19181955 });
19191956 return dest_path.root_dir.handle.createDirPathStatus(io, dest_path.sub_path, .default_dir) catch |err| {
19201957 const s = stepByIndex(maker, asking_step_index);
1921 return s.fail(maker, "unable to create dir {f}: {t}", .{ dest_path, err });
1958 return s.fail(maker, "failed creating dir {f}: {t}", .{ dest_path, err });
19221959 };
19231960}
19241961
lib/compiler/Maker/Step.zig+2-3
......@@ -21,6 +21,7 @@ const Maker = @import("../Maker.zig");
2121pub const Compile = @import("Step/Compile.zig");
2222pub const Fmt = @import("Step/Fmt.zig");
2323pub const InstallArtifact = @import("Step/InstallArtifact.zig");
24pub const InstallDir = @import("Step/InstallDir.zig");
2425pub const InstallFile = @import("Step/InstallFile.zig");
2526pub const ObjCopy = @import("Step/ObjCopy.zig");
2627pub const Options = @import("Step/Options.zig");
......@@ -79,11 +80,10 @@ pub const Extended = union(enum) {
7980 find_program: Todo,
8081 fmt: Fmt,
8182 install_artifact: InstallArtifact,
82 install_dir: Todo,
83 install_dir: InstallDir,
8384 install_file: InstallFile,
8485 obj_copy: ObjCopy,
8586 options: Options,
86 remove_dir: Todo,
8787 run: Run,
8888 top_level: TopLevel,
8989 translate_c: Todo,
......@@ -103,7 +103,6 @@ pub const Extended = union(enum) {
103103 .install_file => .{ .install_file = .{} },
104104 .obj_copy => .{ .obj_copy = .{} },
105105 .options => .{ .options = .{} },
106 .remove_dir => .{ .remove_dir = .{} },
107106 .run => .{ .run = .{} },
108107 .top_level => .{ .top_level = .{} },
109108 .translate_c => .{ .translate_c = .{} },
lib/compiler/Maker/Step/InstallDir.zig+56-22
......@@ -1,7 +1,10 @@
11const InstallDir = @This();
22
33const std = @import("std");
4const Io = std.Io;
5const log = std.log;
46const Configuration = std.Build.Configuration;
7const endsWith = std.mem.endsWith;
58
69const Step = @import("../Step.zig");
710const Maker = @import("../../Maker.zig");
......@@ -12,51 +15,82 @@ pub fn make(
1215 maker: *Maker,
1316 progress_node: std.Progress.Node,
1417) Step.ExtendedMakeError!void {
18 _ = install_dir;
1519 const graph = maker.graph;
20 const gpa = maker.gpa;
1621 const arena = maker.graph.arena; // TODO don't leak into process arena
1722 const io = graph.io;
1823 const step = maker.stepByIndex(step_index);
24 const conf = &maker.scanned_config.configuration;
25 const conf_step = step_index.ptr(conf);
26 const conf_id = conf_step.extended.get(conf.extra).install_dir;
1927
20 step.clearWatchInputs();
21 const dest_prefix = b.getInstallPath(install_dir.options.install_dir, install_dir.options.install_subdir);
22 const src_dir_path = install_dir.options.source_dir.getPath3(b, step);
23 const need_derived_inputs = try step.addDirectoryWatchInput(install_dir.options.source_dir);
24 var src_dir = src_dir_path.root_dir.handle.openDir(io, src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
25 return step.fail("unable to open source directory '{f}': {t}", .{ src_dir_path, err });
26 };
28 step.clearWatchInputs(maker);
29
30 const dest_parent_path = try maker.resolveInstallDir(arena, conf_id.dest_dir);
31 const dest_prefix = if (conf_id.dest_sub_path.value) |s|
32 try dest_parent_path.join(arena, s.slice(conf))
33 else
34 dest_parent_path;
35 const src_dir_lazy_path = conf_id.source_dir.get(conf);
36 const src_dir_path = try maker.resolveLazyPath(arena, src_dir_lazy_path, step_index);
37 const need_derived_inputs = try step.addDirectoryWatchInput(maker, src_dir_lazy_path);
38
39 var src_dir = src_dir_path.root_dir.handle.openDir(
40 io,
41 src_dir_path.subPathOrDot(),
42 .{ .iterate = true },
43 ) catch |err| return step.fail(maker, "failed opening source directory {f}: {t}", .{ src_dir_path, err });
2744 defer src_dir.close(io);
28 var it = try src_dir.walk(arena);
45
46 const exclude_extensions = conf_id.exclude_extensions.slice;
47 const include_extensions: ?[]const Configuration.String = if (conf_id.flags.include_extensions_active)
48 conf_id.include_extensions.slice
49 else
50 null;
51 const blank_extensions = conf_id.blank_extensions.slice;
52
2953 var all_cached = true;
30 next_entry: while (try it.next(io)) |entry| {
31 for (install_dir.options.exclude_extensions) |ext| {
32 if (std.mem.endsWith(u8, entry.path, ext)) continue :next_entry;
54 var it = try src_dir.walk(gpa);
55 defer it.deinit();
56 next_entry: while (it.next(io) catch |err| switch (err) {
57 error.Canceled, error.OutOfMemory => |e| return e,
58 else => |e| return step.fail(maker, "failed iterating dir {f}: {t}", .{ src_dir_path, e }),
59 }) |entry| {
60 for (exclude_extensions) |ext| {
61 if (endsWith(u8, entry.path, ext.slice(conf))) continue :next_entry;
3362 }
34 if (install_dir.options.include_extensions) |incs| {
35 for (incs) |inc| {
36 if (std.mem.endsWith(u8, entry.path, inc)) break;
63 if (include_extensions) |includes| {
64 for (includes) |inc| {
65 if (endsWith(u8, entry.path, inc.slice(conf))) break;
3766 } else {
3867 continue :next_entry;
3968 }
4069 }
4170
42 const src_path = try install_dir.options.source_dir.join(arena, entry.path);
43 const dest_path = b.pathJoin(&.{ dest_prefix, entry.path });
71 const dest_path = try dest_prefix.join(arena, entry.path);
4472 switch (entry.kind) {
4573 .directory => {
46 if (need_derived_inputs) _ = try step.addDirectoryWatchInput(src_path);
47 const p = try step.installDir(dest_path);
74 if (need_derived_inputs) {
75 const entry_path = try src_dir_path.join(arena, entry.path);
76 try step.addDirectoryWatchInputFromPath(maker, entry_path);
77 }
78 const p = try maker.installDir(arena, dest_path, step_index);
4879 all_cached = all_cached and p == .existed;
4980 },
5081 .file => {
51 for (install_dir.options.blank_extensions) |ext| {
52 if (std.mem.endsWith(u8, entry.path, ext)) {
53 try b.truncateFile(dest_path);
82 for (blank_extensions) |ext| {
83 if (endsWith(u8, entry.path, ext.slice(conf))) {
84 // TODO check if the file was already there and length 0
85 try maker.truncatePath(arena, dest_path, step_index);
5486 continue :next_entry;
5587 }
5688 }
5789
58 const p = try step.installFile(src_path, dest_path);
90 const entry_path = try src_dir_path.join(arena, entry.path);
91 const p = try maker.installPath(arena, entry_path, dest_path, step_index);
5992 all_cached = all_cached and p == .fresh;
93 progress_node.completeOne();
6094 },
6195 else => continue,
6296 }
lib/compiler/configurer.zig+1-1
......@@ -931,6 +931,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
931931 .dest_sub_path = dest_sub_path != null,
932932 .exclude_extensions = sid.options.exclude_extensions.len != 0,
933933 .include_extensions = include_extensions.len != 0,
934 .include_extensions_active = sid.options.include_extensions != null,
934935 .blank_extensions = sid.options.blank_extensions.len != 0,
935936 },
936937 .source_dir = try s.addLazyPath(sid.options.source_dir),
......@@ -941,7 +942,6 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
941942 .blank_extensions = .{ .slice = try s.initStringList(sid.options.blank_extensions) },
942943 })));
943944 },
944 .remove_dir => @panic("TODO"),
945945 .fail => e: {
946946 const sf: *Step.Fail = @fieldParentPtr("step", step);
947947 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.Fail, .{
lib/std/Build.zig-17
......@@ -1584,23 +1584,6 @@ pub fn addCheckFile(
15841584 return Step.CheckFile.create(b, file_source, options);
15851585}
15861586
1587pub fn truncateFile(b: *Build, dest_path: []const u8) (Io.Dir.CreateDirError || Io.Dir.StatFileError)!void {
1588 const graph = b.graph;
1589 const io = graph.io;
1590 if (graph.verbose) log.info("truncate {s}", .{dest_path});
1591 const cwd = Io.Dir.cwd();
1592 var src_file = cwd.createFile(io, dest_path, .{}) catch |err| switch (err) {
1593 error.FileNotFound => blk: {
1594 if (fs.path.dirname(dest_path)) |dirname| {
1595 try cwd.createDirPath(io, dirname);
1596 }
1597 break :blk try cwd.createFile(io, dest_path, .{});
1598 },
1599 else => |e| return e,
1600 };
1601 src_file.close(io);
1602}
1603
16041587/// References a file or directory relative to the source root.
16051588pub fn path(b: *Build, sub_path: []const u8) LazyPath {
16061589 if (fs.path.isAbsolute(sub_path)) {
lib/std/Build/Configuration.zig+2-12
......@@ -466,7 +466,6 @@ pub const Step = extern struct {
466466 install_file: InstallFile,
467467 obj_copy: ObjCopy,
468468 options: Options,
469 remove_dir: RemoveDir,
470469 run: Run,
471470 top_level: TopLevel,
472471 translate_c: TranslateC,
......@@ -501,7 +500,6 @@ pub const Step = extern struct {
501500 install_file,
502501 obj_copy,
503502 options,
504 remove_dir,
505503 run,
506504 top_level,
507505 translate_c,
......@@ -1197,8 +1195,9 @@ pub const Step = extern struct {
11971195 dest_sub_path: bool,
11981196 exclude_extensions: bool,
11991197 include_extensions: bool,
1198 include_extensions_active: bool,
12001199 blank_extensions: bool,
1201 _: u23 = 0,
1200 _: u22 = 0,
12021201 };
12031202 };
12041203
......@@ -1320,15 +1319,6 @@ pub const Step = extern struct {
13201319 };
13211320 };
13221321
1323 pub const RemoveDir = struct {
1324 flags: @This().Flags,
1325
1326 pub const Flags = packed struct(u32) {
1327 tag: Tag = .remove_dir,
1328 _: u27 = 0,
1329 };
1330 };
1331
13321322 pub const TranslateC = struct {
13331323 flags: @This().Flags,
13341324 src_path: LazyPath.Index,