Commit 5af4e91e00

Carl Åstholm <carl@astholm.se>
2024-03-04 01:38:29
Oops, forgot to dupe installations in `installLibraryHeaders`
Added test coverage for `installLibraryHeaders`
1 parent 7b1a6a9
Changed files (2)
lib
std
Build
test
standalone
install_headers
lib/std/Build/Step/Compile.zig
@@ -309,8 +309,8 @@ pub const HeaderInstallation = union(enum) {
 
     pub fn dupe(self: HeaderInstallation, b: *std.Build) HeaderInstallation {
         return switch (self) {
-            .file => |f| f.dupe(b),
-            .directory => |d| d.dupe(b),
+            .file => |f| .{ .file = f.dupe(b) },
+            .directory => |d| .{ .directory = d.dupe(b) },
         };
     }
 };
@@ -480,10 +480,12 @@ pub fn installConfigHeader(cs: *Compile, config_header: *Step.ConfigHeader) void
 
 pub fn installLibraryHeaders(cs: *Compile, lib: *Compile) void {
     assert(lib.kind == .lib);
+    const b = cs.step.owner;
     for (lib.installed_headers.items) |installation| {
-        cs.installed_headers.append(installation) catch @panic("OOM");
-        cs.addHeaderInstallationToIncludeTree(installation);
-        installation.getSource().addStepDependencies(&cs.step);
+        const installation_copy = installation.dupe(b);
+        cs.installed_headers.append(installation_copy) catch @panic("OOM");
+        cs.addHeaderInstallationToIncludeTree(installation_copy);
+        installation_copy.getSource().addStepDependencies(&cs.step);
     }
 }
 
test/standalone/install_headers/build.zig
@@ -4,12 +4,14 @@ pub fn build(b: *std.Build) void {
     const test_step = b.step("test", "Test");
     b.default_step = test_step;
 
+    const empty_c = b.addWriteFiles().add("empty.c", "");
+
     const libfoo = b.addStaticLibrary(.{
         .name = "foo",
         .target = b.resolveTargetQuery(.{}),
         .optimize = .Debug,
     });
-    libfoo.addCSourceFile(.{ .file = b.addWriteFiles().add("empty.c", "") });
+    libfoo.addCSourceFile(.{ .file = empty_c });
 
     const exe = b.addExecutable(.{
         .name = "exe",
@@ -23,8 +25,9 @@ pub fn build(b: *std.Build) void {
         \\#include <foo/sub_dir/b.h>
         \\#include <foo/d.h>
         \\#include <foo/config.h>
+        \\#include <bar.h>
         \\int main(void) {
-        \\    printf(FOO_A FOO_B FOO_D FOO_CONFIG_1 FOO_CONFIG_2);
+        \\    printf(FOO_A FOO_B FOO_D FOO_CONFIG_1 FOO_CONFIG_2 BAR_X);
         \\    return 0;
         \\}
     ) });
@@ -51,8 +54,20 @@ pub fn build(b: *std.Build) void {
         .FOO_CONFIG_2 = "2",
     }));
 
+    const libbar = b.addStaticLibrary(.{
+        .name = "bar",
+        .target = b.resolveTargetQuery(.{}),
+        .optimize = .Debug,
+    });
+    libbar.addCSourceFile(.{ .file = empty_c });
+    libbar.installHeader(b.addWriteFiles().add("bar.h",
+        \\#define BAR_X "X"
+        \\
+    ), "bar.h");
+    libfoo.installLibraryHeaders(libbar);
+
     const run_exe = b.addRunArtifact(exe);
-    run_exe.expectStdOutEqual("ABD12");
+    run_exe.expectStdOutEqual("ABD12X");
     test_step.dependOn(&run_exe.step);
 
     const install_exe = b.addInstallArtifact(libfoo, .{
@@ -75,6 +90,7 @@ pub fn build(b: *std.Build) void {
         "!custom/include/foo/sub_dir/c.ignore_me.h",
         "custom/include/foo/d.h",
         "custom/include/foo/config.h",
+        "custom/include/bar.h",
     });
     run_check_exists.setCwd(.{ .cwd_relative = b.getInstallPath(.prefix, "") });
     run_check_exists.expectExitCode(0);