authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-03-03 16:22:11+01:00
committergravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-04-07 15:34:46+02:00
log2c7be4f8dd55653be6cbf5d06f8f9c47e8d9276e
tree0a032fe065a1e973952b400a44ad7654593852b4
parentce71eb31dadd255b0560e4c7a837e255e0617a91

Create an include tree of installed headers for dependent modules


3 files changed, 107 insertions(+), 90 deletions(-)

lib/std/Build/Module.zig+5-12
......@@ -265,8 +265,7 @@ fn addShallowDependencies(m: *Module, dependee: *Module) void {
265265 for (dependee.link_objects.items) |link_object| switch (link_object) {
266266 .other_step => |compile| {
267267 addStepDependencies(m, dependee, &compile.step);
268 for (compile.installed_headers.items) |header|
269 addLazyPathDependenciesOnly(m, header.source.path());
268 addLazyPathDependenciesOnly(m, compile.getEmittedIncludeTree());
270269 },
271270
272271 .static_path,
......@@ -693,14 +692,9 @@ pub fn appendZigProcessFlags(
693692 if (other.generated_h) |header| {
694693 try zig_args.appendSlice(&.{ "-isystem", std.fs.path.dirname(header.getPath()).? });
695694 }
696 for (other.installed_headers.items) |header| switch (header.source) {
697 .file => |lp| {
698 try zig_args.appendSlice(&.{ "-I", std.fs.path.dirname(lp.getPath2(b, asking_step)).? });
699 },
700 .directory => |dir| {
701 try zig_args.appendSlice(&.{ "-I", dir.path.getPath2(b, asking_step) });
702 },
703 };
695 if (other.installed_headers_include_tree) |include_tree| {
696 try zig_args.appendSlice(&.{ "-I", include_tree.generated_directory.getPath() });
697 }
704698 },
705699 .config_header_step => |config_header| {
706700 try zig_args.appendSlice(&.{ "-I", std.fs.path.dirname(config_header.output_file.getPath()).? });
......@@ -751,8 +745,7 @@ fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void {
751745 m.link_objects.append(allocator, .{ .other_step = other }) catch @panic("OOM");
752746 m.include_dirs.append(allocator, .{ .other_step = other }) catch @panic("OOM");
753747
754 for (other.installed_headers.items) |header|
755 addLazyPathDependenciesOnly(m, header.source.path());
748 addLazyPathDependenciesOnly(m, other.getEmittedIncludeTree());
756749}
757750
758751fn requireKnownTarget(m: *Module) std.Target {
lib/std/Build/Step/Compile.zig+96-72
......@@ -59,7 +59,13 @@ test_runner: ?[]const u8,
5959test_server_mode: bool,
6060wasi_exec_model: ?std.builtin.WasiExecModel = null,
6161
62installed_headers: ArrayList(InstalledHeader),
62installed_headers: ArrayList(HeaderInstallation),
63
64/// This step is used to create an include tree that dependent modules can add to their include
65/// search paths. Installed headers are copied to this step.
66/// This step is created the first time a module links with this artifact and is not
67/// created otherwise.
68installed_headers_include_tree: ?*Step.WriteFile = null,
6369
6470// keep in sync with src/Compilation.zig:RcIncludes
6571/// Behavior of automatic detection of include directories when compiling .rc files.
......@@ -249,66 +255,62 @@ pub const Kind = enum {
249255 @"test",
250256};
251257
252pub const InstalledHeader = struct {
253 source: Source,
254 dest_rel_path: []const u8,
258pub const HeaderInstallation = union(enum) {
259 file: File,
260 directory: Directory,
255261
256 pub const Source = union(enum) {
257 file: LazyPath,
258 directory: Directory,
259
260 pub const Directory = struct {
261 path: LazyPath,
262 options: Directory.Options,
263
264 pub const Options = struct {
265 /// File paths which end in any of these suffixes will be excluded
266 /// from installation.
267 exclude_extensions: []const []const u8 = &.{},
268 /// Only file paths which end in any of these suffixes will be included
269 /// in installation.
270 /// `null` means all suffixes will be included.
271 /// `exclude_extensions` takes precedence over `include_extensions`
272 include_extensions: ?[]const []const u8 = &.{".h"},
273
274 pub fn dupe(self: Directory.Options, b: *std.Build) Directory.Options {
275 return .{
276 .exclude_extensions = b.dupeStrings(self.exclude_extensions),
277 .include_extensions = if (self.include_extensions) |incs|
278 b.dupeStrings(incs)
279 else
280 null,
281 };
282 }
262 pub const File = struct {
263 source: LazyPath,
264 dest_rel_path: []const u8,
265
266 pub fn dupe(self: File, b: *std.Build) File {
267 return .{
268 .source = self.source.dupe(b),
269 .dest_rel_path = b.dupePath(self.dest_rel_path),
283270 };
271 }
272 };
273
274 pub const Directory = struct {
275 source: LazyPath,
276 dest_rel_path: []const u8,
277 options: Directory.Options,
278
279 pub const Options = struct {
280 /// File paths that end in any of these suffixes will be excluded from installation.
281 exclude_extensions: []const []const u8 = &.{},
282 /// Only file paths that end in any of these suffixes will be included in installation.
283 /// `null` means that all suffixes will be included.
284 /// `exclude_extensions` takes precedence over `include_extensions`.
285 include_extensions: ?[]const []const u8 = &.{".h"},
284286
285 pub fn dupe(self: Directory, b: *std.Build) Directory {
287 pub fn dupe(self: Directory.Options, b: *std.Build) Directory.Options {
286288 return .{
287 .path = self.path.dupe(b),
288 .options = self.options.dupe(b),
289 .exclude_extensions = b.dupeStrings(self.exclude_extensions),
290 .include_extensions = if (self.include_extensions) |incs| b.dupeStrings(incs) else null,
289291 };
290292 }
291293 };
292294
293 pub fn path(self: Source) LazyPath {
294 return switch (self) {
295 .file => |lp| lp,
296 .directory => |dir| dir.path,
297 };
298 }
299
300 pub fn dupe(self: Source, b: *std.Build) Source {
301 return switch (self) {
302 .file => |lp| .{ .file = lp.dupe(b) },
303 .directory => |dir| .{ .directory = dir.dupe(b) },
295 pub fn dupe(self: Directory, b: *std.Build) Directory {
296 return .{
297 .source = self.source.dupe(b),
298 .dest_rel_path = b.dupePath(self.dest_rel_path),
299 .options = self.options.dupe(b),
304300 };
305301 }
306302 };
307303
308 pub fn dupe(self: InstalledHeader, b: *std.Build) InstalledHeader {
309 return .{
310 .source = self.source.dupe(b),
311 .dest_rel_path = b.dupePath(self.dest_rel_path),
304 pub fn getSource(self: HeaderInstallation) LazyPath {
305 return switch (self) {
306 inline .file, .directory => |x| x.source,
307 };
308 }
309
310 pub fn dupe(self: HeaderInstallation, b: *std.Build) HeaderInstallation {
311 return switch (self) {
312 .file => |f| f.dupe(b),
313 .directory => |d| d.dupe(b),
312314 };
313315 }
314316};
......@@ -372,7 +374,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
372374 .out_lib_filename = undefined,
373375 .major_only_filename = null,
374376 .name_only_filename = null,
375 .installed_headers = ArrayList(InstalledHeader).init(owner.allocator),
377 .installed_headers = ArrayList(HeaderInstallation).init(owner.allocator),
376378 .zig_lib_dir = null,
377379 .exec_cmd_args = null,
378380 .filters = options.filters,
......@@ -444,49 +446,71 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
444446 return self;
445447}
446448
447pub fn installHeader(
448 cs: *Compile,
449 source: LazyPath,
450 dest_rel_path: []const u8,
451) void {
449pub fn installHeader(cs: *Compile, source: LazyPath, dest_rel_path: []const u8) void {
452450 const b = cs.step.owner;
453 cs.installed_headers.append(.{
454 .source = .{ .file = source.dupe(b) },
451 const installation: HeaderInstallation = .{ .file = .{
452 .source = source.dupe(b),
455453 .dest_rel_path = b.dupePath(dest_rel_path),
456 }) catch @panic("OOM");
457 source.addStepDependencies(&cs.step);
454 } };
455 cs.installed_headers.append(installation) catch @panic("OOM");
456 cs.addHeaderInstallationToIncludeTree(installation);
457 installation.getSource().addStepDependencies(&cs.step);
458458}
459459
460460pub fn installHeaders(
461461 cs: *Compile,
462462 source: LazyPath,
463463 dest_rel_path: []const u8,
464 options: InstalledHeader.Source.Directory.Options,
464 options: HeaderInstallation.Directory.Options,
465465) void {
466466 const b = cs.step.owner;
467 cs.installed_headers.append(.{
468 .source = .{ .directory = .{
469 .path = source.dupe(b),
470 .options = options.dupe(b),
471 } },
467 const installation: HeaderInstallation = .{ .directory = .{
468 .source = source.dupe(b),
472469 .dest_rel_path = b.dupePath(dest_rel_path),
473 }) catch @panic("OOM");
474 source.addStepDependencies(&cs.step);
470 .options = options.dupe(b),
471 } };
472 cs.installed_headers.append(installation) catch @panic("OOM");
473 cs.addHeaderInstallationToIncludeTree(installation);
474 installation.getSource().addStepDependencies(&cs.step);
475475}
476476
477477pub fn installConfigHeader(cs: *Compile, config_header: *Step.ConfigHeader) void {
478 cs.installHeader(.{ .generated = &config_header.output_file }, config_header.include_path);
478 cs.installHeader(config_header.getOutput(), config_header.include_path);
479479}
480480
481481pub fn installLibraryHeaders(cs: *Compile, lib: *Compile) void {
482482 assert(lib.kind == .lib);
483 const b = cs.step.owner;
484 for (lib.installed_headers.items) |header| {
485 cs.installed_headers.append(header.dupe(b)) catch @panic("OOM");
486 header.source.path().addStepDependencies(&cs.step);
483 for (lib.installed_headers.items) |installation| {
484 cs.installed_headers.append(installation) catch @panic("OOM");
485 cs.addHeaderInstallationToIncludeTree(installation);
486 installation.getSource().addStepDependencies(&cs.step);
487487 }
488488}
489489
490fn addHeaderInstallationToIncludeTree(cs: *Compile, installation: HeaderInstallation) void {
491 if (cs.installed_headers_include_tree) |wf| switch (installation) {
492 .file => |file| {
493 _ = wf.addCopyFile(file.source, file.dest_rel_path);
494 },
495 .directory => |dir| {
496 _ = dir; // TODO
497 },
498 };
499}
500
501pub fn getEmittedIncludeTree(cs: *Compile) LazyPath {
502 if (cs.installed_headers_include_tree) |wf| return wf.getDirectory();
503 const b = cs.step.owner;
504 const wf = b.addWriteFiles();
505 cs.installed_headers_include_tree = wf;
506 for (cs.installed_headers.items) |installation| {
507 cs.addHeaderInstallationToIncludeTree(installation);
508 }
509 // The compile step itself does not need to depend on the write files step,
510 // only dependent modules do.
511 return wf.getDirectory();
512}
513
490514pub fn addObjCopy(cs: *Compile, options: Step.ObjCopy.Options) *Step.ObjCopy {
491515 const b = cs.step.owner;
492516 var copy = options;
lib/std/Build/Step/InstallArtifact.zig+6-6
......@@ -177,10 +177,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
177177 all_cached = all_cached and p == .fresh;
178178 }
179179
180 for (self.artifact.installed_headers.items) |header| switch (header.source) {
181 .file => |lp| {
182 const full_src_path = lp.getPath2(b, step);
183 const full_h_path = b.getInstallPath(h_dir, header.dest_rel_path);
180 for (self.artifact.installed_headers.items) |installation| switch (installation) {
181 .file => |file| {
182 const full_src_path = file.source.getPath2(b, step);
183 const full_h_path = b.getInstallPath(h_dir, file.dest_rel_path);
184184 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_h_path, .{}) catch |err| {
185185 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
186186 full_src_path, full_h_path, @errorName(err),
......@@ -189,8 +189,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
189189 all_cached = all_cached and p == .fresh;
190190 },
191191 .directory => |dir| {
192 const full_src_dir_path = dir.path.getPath2(b, step);
193 const full_h_prefix = b.getInstallPath(h_dir, header.dest_rel_path);
192 const full_src_dir_path = dir.source.getPath2(b, step);
193 const full_h_prefix = b.getInstallPath(h_dir, dir.dest_rel_path);
194194
195195 var src_dir = b.build_root.handle.openDir(full_src_dir_path, .{ .iterate = true }) catch |err| {
196196 return step.fail("unable to open source directory '{s}': {s}", .{