master
  1const std = @import("std");
  2const Step = std.Build.Step;
  3const InstallDir = std.Build.InstallDir;
  4const InstallArtifact = @This();
  5const fs = std.fs;
  6const LazyPath = std.Build.LazyPath;
  7
  8step: Step,
  9
 10dest_dir: ?InstallDir,
 11dest_sub_path: []const u8,
 12emitted_bin: ?LazyPath,
 13
 14implib_dir: ?InstallDir,
 15emitted_implib: ?LazyPath,
 16
 17pdb_dir: ?InstallDir,
 18emitted_pdb: ?LazyPath,
 19
 20h_dir: ?InstallDir,
 21emitted_h: ?LazyPath,
 22
 23dylib_symlinks: ?DylibSymlinkInfo,
 24
 25artifact: *Step.Compile,
 26
 27const DylibSymlinkInfo = struct {
 28    major_only_filename: []const u8,
 29    name_only_filename: []const u8,
 30};
 31
 32pub const base_id: Step.Id = .install_artifact;
 33
 34pub const Options = struct {
 35    /// Which installation directory to put the main output file into.
 36    dest_dir: Dir = .default,
 37    pdb_dir: Dir = .default,
 38    h_dir: Dir = .default,
 39    implib_dir: Dir = .default,
 40
 41    /// Whether to install symlinks along with dynamic libraries.
 42    dylib_symlinks: ?bool = null,
 43    /// If non-null, adds additional path components relative to bin dir, and
 44    /// overrides the basename of the Compile step for installation purposes.
 45    dest_sub_path: ?[]const u8 = null,
 46
 47    pub const Dir = union(enum) {
 48        disabled,
 49        default,
 50        override: InstallDir,
 51    };
 52};
 53
 54pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *InstallArtifact {
 55    const install_artifact = owner.allocator.create(InstallArtifact) catch @panic("OOM");
 56    const dest_dir: ?InstallDir = switch (options.dest_dir) {
 57        .disabled => null,
 58        .default => switch (artifact.kind) {
 59            .obj, .test_obj => @panic("object files have no standard installation procedure"),
 60            .exe, .@"test" => .bin,
 61            .lib => if (artifact.isDll()) .bin else .lib,
 62        },
 63        .override => |o| o,
 64    };
 65    install_artifact.* = .{
 66        .step = Step.init(.{
 67            .id = base_id,
 68            .name = owner.fmt("install {s}", .{artifact.name}),
 69            .owner = owner,
 70            .makeFn = make,
 71        }),
 72        .dest_dir = dest_dir,
 73        .pdb_dir = switch (options.pdb_dir) {
 74            .disabled => null,
 75            .default => if (artifact.producesPdbFile()) dest_dir else null,
 76            .override => |o| o,
 77        },
 78        .h_dir = switch (options.h_dir) {
 79            .disabled => null,
 80            .default => if (artifact.kind == .lib) .header else null,
 81            .override => |o| o,
 82        },
 83        .implib_dir = switch (options.implib_dir) {
 84            .disabled => null,
 85            .default => if (artifact.producesImplib()) .lib else null,
 86            .override => |o| o,
 87        },
 88
 89        .dylib_symlinks = if (options.dylib_symlinks orelse (dest_dir != null and
 90            artifact.isDynamicLibrary() and
 91            artifact.version != null and
 92            std.Build.wantSharedLibSymLinks(artifact.rootModuleTarget()))) .{
 93            .major_only_filename = artifact.major_only_filename.?,
 94            .name_only_filename = artifact.name_only_filename.?,
 95        } else null,
 96
 97        .dest_sub_path = options.dest_sub_path orelse artifact.out_filename,
 98
 99        .emitted_bin = null,
100        .emitted_pdb = null,
101        .emitted_h = null,
102        .emitted_implib = null,
103
104        .artifact = artifact,
105    };
106
107    install_artifact.step.dependOn(&artifact.step);
108
109    if (install_artifact.dest_dir != null) install_artifact.emitted_bin = artifact.getEmittedBin();
110    if (install_artifact.pdb_dir != null) install_artifact.emitted_pdb = artifact.getEmittedPdb();
111    // https://github.com/ziglang/zig/issues/9698
112    //if (install_artifact.h_dir != null) install_artifact.emitted_h = artifact.getEmittedH();
113    if (install_artifact.implib_dir != null) install_artifact.emitted_implib = artifact.getEmittedImplib();
114
115    return install_artifact;
116}
117
118fn make(step: *Step, options: Step.MakeOptions) !void {
119    _ = options;
120    const install_artifact: *InstallArtifact = @fieldParentPtr("step", step);
121    const b = step.owner;
122
123    var all_cached = true;
124
125    if (install_artifact.dest_dir) |dest_dir| {
126        const full_dest_path = b.getInstallPath(dest_dir, install_artifact.dest_sub_path);
127        const p = try step.installFile(install_artifact.emitted_bin.?, full_dest_path);
128        all_cached = all_cached and p == .fresh;
129
130        if (install_artifact.dylib_symlinks) |dls| {
131            try Step.Compile.doAtomicSymLinks(step, full_dest_path, dls.major_only_filename, dls.name_only_filename);
132        }
133
134        install_artifact.artifact.installed_path = full_dest_path;
135    }
136
137    if (install_artifact.implib_dir) |implib_dir| {
138        const full_implib_path = b.getInstallPath(implib_dir, install_artifact.emitted_implib.?.basename(b, step));
139        const p = try step.installFile(install_artifact.emitted_implib.?, full_implib_path);
140        all_cached = all_cached and p == .fresh;
141    }
142
143    if (install_artifact.pdb_dir) |pdb_dir| {
144        const full_pdb_path = b.getInstallPath(pdb_dir, install_artifact.emitted_pdb.?.basename(b, step));
145        const p = try step.installFile(install_artifact.emitted_pdb.?, full_pdb_path);
146        all_cached = all_cached and p == .fresh;
147    }
148
149    if (install_artifact.h_dir) |h_dir| {
150        if (install_artifact.emitted_h) |emitted_h| {
151            const full_h_path = b.getInstallPath(h_dir, emitted_h.basename(b, step));
152            const p = try step.installFile(emitted_h, full_h_path);
153            all_cached = all_cached and p == .fresh;
154        }
155
156        for (install_artifact.artifact.installed_headers.items) |installation| switch (installation) {
157            .file => |file| {
158                const full_h_path = b.getInstallPath(h_dir, file.dest_rel_path);
159                const p = try step.installFile(file.source, full_h_path);
160                all_cached = all_cached and p == .fresh;
161            },
162            .directory => |dir| {
163                const src_dir_path = dir.source.getPath3(b, step);
164                const full_h_prefix = b.getInstallPath(h_dir, dir.dest_rel_path);
165
166                var src_dir = src_dir_path.root_dir.handle.openDir(src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
167                    return step.fail("unable to open source directory '{f}': {s}", .{
168                        src_dir_path, @errorName(err),
169                    });
170                };
171                defer src_dir.close();
172
173                var it = try src_dir.walk(b.allocator);
174                next_entry: while (try it.next()) |entry| {
175                    for (dir.options.exclude_extensions) |ext| {
176                        if (std.mem.endsWith(u8, entry.path, ext)) continue :next_entry;
177                    }
178                    if (dir.options.include_extensions) |incs| {
179                        for (incs) |inc| {
180                            if (std.mem.endsWith(u8, entry.path, inc)) break;
181                        } else {
182                            continue :next_entry;
183                        }
184                    }
185
186                    const full_dest_path = b.pathJoin(&.{ full_h_prefix, entry.path });
187                    switch (entry.kind) {
188                        .directory => {
189                            try Step.handleVerbose(b, null, &.{ "install", "-d", full_dest_path });
190                            const p = try step.installDir(full_dest_path);
191                            all_cached = all_cached and p == .existed;
192                        },
193                        .file => {
194                            const p = try step.installFile(try dir.source.join(b.allocator, entry.path), full_dest_path);
195                            all_cached = all_cached and p == .fresh;
196                        },
197                        else => continue,
198                    }
199                }
200            },
201        };
202    }
203
204    step.result_cached = all_cached;
205}