Last active
August 3, 2023 13:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works as of zig 0.10.1