Skip to content

Instantly share code, notes, and snippets.

@eNV25
Last active August 3, 2023 13:03
Show Gist options
  • Save eNV25/a56a7169cdeeddef9b06257ec89360d9 to your computer and use it in GitHub Desktop.
Save eNV25/a56a7169cdeeddef9b06257ec89360d9 to your computer and use it in GitHub Desktop.
build.zig installArtifact(), install build shared object, executable or object to custom location
const std = @import("std");
pub fn build(b: *std.build.Builder) !void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const lib = b.addSharedLibrary("plugin", "src/plugin.zig", .unversioned);
lib.setTarget(target);
lib.setBuildMode(mode);
_ = installArtifact(b, lib, .prefix, "plugin.so");
}
fn installArtifact(b: *std.build.Builder, source: *std.build.LibExeObjStep, install_dir: std.build.InstallDir, dest_rel_path: []const u8) *ArtifactInstallStep {
const self = b.allocator.create(ArtifactInstallStep) catch unreachable;
self.* = .{
.step = std.build.Step.init(.custom, "install library, executable or object to custom location", b.allocator, ArtifactInstallStep.make),
.builder = b,
.source = source.getOutputSource(),
.install_dir = install_dir,
.dest_rel_path = b.allocator.dupe(u8, dest_rel_path) catch unreachable,
};
self.step.dependOn(&source.step);
b.getInstallStep().dependOn(&self.step);
return self;
}
const ArtifactInstallStep = struct {
step: std.build.Step,
builder: *std.build.Builder,
source: std.build.FileSource,
install_dir: std.build.InstallDir,
dest_rel_path: []const u8,
fn make(step: *std.build.Step) !void {
const self = @fieldParentPtr(ArtifactInstallStep, "step", step);
const b = self.builder;
const source_path = self.source.getPath(b);
const dest_path = b.getInstallPath(self.install_dir, self.dest_rel_path);
try b.updateFile(source_path, dest_path);
}
};
@eNV25
Copy link
Author

eNV25 commented Aug 3, 2023

Works as of zig 0.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment