Skip to content

Commit

Permalink
make link a self-injecting custom build step
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Sep 17, 2023
1 parent 4a89c19 commit 2c120d0
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,42 @@ pub fn build(b: *Build) !void {
.target = target,
.optimize = optimize,
});
try link(b, example, options);
link(b, example, options);
b.installArtifact(example);

const example_step = b.step("example", "Run library tests");
example_step.dependOn(&b.addRunArtifact(example).step);
}

pub const LinkStep = struct {
target: *std.build.Step.Compile,
options: Options,
step: std.build.Step,
b: *std.build.Builder,

pub fn init(b: *std.build.Builder, target: *std.build.Step.Compile, options: Options) *LinkStep {
const link_step = b.allocator.create(LinkStep) catch unreachable;
link_step.* = .{
.target = target,
.options = options,
.step = std.build.Step.init(.{
.id = .custom,
.name = "link",
.owner = b,
.makeFn = &make,
}),
.b = b,
};
return link_step;
}

fn make(step: *std.build.Step, prog_node: *std.Progress.Node) anyerror!void {
_ = prog_node;
const link_step = @fieldParentPtr(LinkStep, "step", step);
try doLink(link_step.b, link_step.target, link_step.options);
}
};

pub const Options = struct {
/// Defaults to true on Windows
d3d12: ?bool = null,
Expand Down Expand Up @@ -80,7 +112,12 @@ pub const Options = struct {
}
};

pub fn link(b: *Build, step: *std.build.CompileStep, options: Options) !void {
pub fn link(b: *Build, step: *std.build.CompileStep, options: Options) void {
var link_step = LinkStep.init(b, step, options);
step.step.dependOn(&link_step.step);
}

fn doLink(b: *Build, step: *std.build.CompileStep, options: Options) !void {
const opt = options.detectDefaults(step.target_info.target);

if (step.target_info.target.os.tag == .windows) @import("direct3d_headers").addLibraryPath(step);
Expand Down

0 comments on commit 2c120d0

Please sign in to comment.