Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to newest build API #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
zig-cache/
.zig-cache/
zig-out/
/dep/
40 changes: 20 additions & 20 deletions GitRepoStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub const ShaCheck = enum {
.warn => std.log.warn(fmt, args),
.err => {
std.log.err(fmt, args);
std.os.exit(0xff);
std.process.exit(0xff);
},
}
}
};

step: std.build.Step,
step: std.Build.Step,
url: []const u8,
name: []const u8,
branch: ?[]const u8 = null,
Expand All @@ -31,14 +31,14 @@ sha_check: ShaCheck = .warn,
fetch_enabled: bool,

var cached_default_fetch_option: ?bool = null;
pub fn defaultFetchOption(b: *std.build.Builder) bool {
pub fn defaultFetchOption(b: *std.Build) bool {
if (cached_default_fetch_option) |_| {} else {
cached_default_fetch_option = if (b.option(bool, "fetch", "automatically fetch network resources")) |o| o else false;
}
return cached_default_fetch_option.?;
}

pub fn create(b: *std.build.Builder, opt: struct {
pub fn create(b: *std.Build, opt: struct {
url: []const u8,
branch: ?[]const u8 = null,
sha: []const u8,
Expand All @@ -47,10 +47,10 @@ pub fn create(b: *std.build.Builder, opt: struct {
fetch_enabled: ?bool = null,
first_ret_addr: ?usize = null,
}) *GitRepoStep {
var result = b.allocator.create(GitRepoStep) catch @panic("memory");
const result = b.allocator.create(GitRepoStep) catch @panic("memory");
const name = std.fs.path.basename(opt.url);
result.* = GitRepoStep{
.step = std.build.Step.init(.{
.step = std.Build.Step.init(.{
.id = .custom,
.name = b.fmt("clone git repository '{s}'", .{name}),
.owner = b,
Expand All @@ -69,8 +69,8 @@ pub fn create(b: *std.build.Builder, opt: struct {
return result;
}

// TODO: this should be included in std.build, it helps find bugs in build files
fn hasDependency(step: *const std.build.Step, dep_candidate: *const std.build.Step) bool {
// TODO: this should be included in std.Build, it helps find bugs in build files
fn hasDependency(step: *const std.Build.Step, dep_candidate: *const std.Build.Step) bool {
for (step.dependencies.items) |dep| {
// TODO: should probably use step.loop_flag to prevent infinite recursion
// when a circular reference is encountered, or maybe keep track of
Expand All @@ -81,9 +81,9 @@ fn hasDependency(step: *const std.build.Step, dep_candidate: *const std.build.St
return false;
}

fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const self = @fieldParentPtr(GitRepoStep, "step", step);
fn make(step: *std.Build.Step, opts: std.Build.Step.MakeOptions) !void {
_ = opts;
const self: *GitRepoStep = @fieldParentPtr("step", step);

std.fs.accessAbsolute(self.path, .{}) catch {
const branch_args = if (self.branch) |b| &[2][]const u8{ " -b ", b } else &[2][]const u8{ "", "" };
Expand All @@ -97,7 +97,7 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
self.path,
self.sha,
});
std.os.exit(1);
std.process.exit(1);
}

{
Expand Down Expand Up @@ -134,7 +134,7 @@ fn checkSha(self: GitRepoStep) !void {
return;

const result: union(enum) { failed: anyerror, output: []const u8 } = blk: {
const result = std.ChildProcess.exec(.{
const result = std.process.Child.run(.{
.allocator = self.step.owner.allocator,
.argv = &[_][]const u8{
"git",
Expand All @@ -144,7 +144,7 @@ fn checkSha(self: GitRepoStep) !void {
"HEAD",
},
.cwd = self.step.owner.build_root.path,
.env_map = self.step.owner.env_map,
.env_map = &self.step.owner.graph.env_map,
}) catch |e| break :blk .{ .failed = e };
try std.io.getStdErr().writer().writeAll(result.stderr);
switch (result.term) {
Expand All @@ -169,7 +169,7 @@ fn checkSha(self: GitRepoStep) !void {
}
}

fn run(builder: *std.build.Builder, argv: []const []const u8) !void {
fn run(builder: *std.Build, argv: []const []const u8) !void {
{
var msg = std.ArrayList(u8).init(builder.allocator);
defer msg.deinit();
Expand All @@ -182,30 +182,30 @@ fn run(builder: *std.build.Builder, argv: []const []const u8) !void {
std.log.info("[RUN] {s}", .{msg.items});
}

var child = std.ChildProcess.init(argv, builder.allocator);
var child = std.process.Child.init(argv, builder.allocator);
child.stdin_behavior = .Ignore;
child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit;
child.cwd = builder.build_root.path;
child.env_map = builder.env_map;
child.env_map = &builder.graph.env_map;

try child.spawn();
const result = try child.wait();
switch (result) {
.Exited => |code| if (code != 0) {
std.log.err("git clone failed with exit code {}", .{code});
std.os.exit(0xff);
std.process.exit(0xff);
},
else => {
std.log.err("git clone failed with: {}", .{result});
std.os.exit(0xff);
std.process.exit(0xff);
},
}
}

// Get's the repository path and also verifies that the step requesting the path
// is dependent on this step.
pub fn getPath(self: *const GitRepoStep, who_wants_to_know: *const std.build.Step) []const u8 {
pub fn getPath(self: *const GitRepoStep, who_wants_to_know: *const std.Build.Step) []const u8 {
if (!hasDependency(who_wants_to_know, &self.step))
@panic("a step called GitRepoStep.getPath but has not added it as a dependency");
return self.path;
Expand Down
18 changes: 9 additions & 9 deletions ProcessFileStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const std = @import("std");
const ProcessFileStep = @This();
const filecheck = @import("filecheck.zig");

step: std.build.Step,
//builder: *std.build.Builder,
step: std.Build.Step,
//builder: *std.Build.Builder,
in_filename: []const u8,
out_filename: []const u8,
subs: []const Sub,
Expand All @@ -13,15 +13,15 @@ pub const Sub = struct {
new: []const u8,
};

pub fn create(b: *std.build.Builder, opt: struct {
pub fn create(b: *std.Build, opt: struct {
in_filename: []const u8,
out_filename: []const u8,
subs: []const Sub = &[_]Sub{},
}) *ProcessFileStep {
var result = b.allocator.create(ProcessFileStep) catch unreachable;
const result = b.allocator.create(ProcessFileStep) catch unreachable;
const name = std.fmt.allocPrint(b.allocator, "process file '{s}'", .{std.fs.path.basename(opt.in_filename)}) catch unreachable;
result.* = ProcessFileStep{
.step = std.build.Step.init(.{
.step = std.Build.Step.init(.{
.id = .custom,
.name = name,
.owner = b,
Expand All @@ -34,9 +34,9 @@ pub fn create(b: *std.build.Builder, opt: struct {
return result;
}

fn make(step: *std.build.Step, progress: *std.Progress.Node) !void {
_ = progress;
const self = @fieldParentPtr(ProcessFileStep, "step", step);
fn make(step: *std.Build.Step, opts: std.Build.Step.MakeOptions) !void {
_ = opts;
const self: *ProcessFileStep = @fieldParentPtr("step", step);

if (try filecheck.leftFileIsNewer(self.out_filename, self.in_filename)) {
return;
Expand All @@ -46,7 +46,7 @@ fn make(step: *std.build.Step, progress: *std.Progress.Node) !void {
defer arena.deinit();
const content = std.fs.cwd().readFileAlloc(arena.allocator(), self.in_filename, std.math.maxInt(usize)) catch |err| {
std.log.err("failed to read file '{s}' to process ({s})", .{ self.in_filename, @errorName(err) });
std.os.exit(0xff);
std.process.exit(0xff);
};
const tmp_filename = try std.fmt.allocPrint(arena.allocator(), "{s}.processing", .{self.out_filename});
{
Expand Down
23 changes: 12 additions & 11 deletions awkbuild.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const std = @import("std");
const GitRepoStep = @import("GitRepoStep.zig");

pub fn addAwk(
b: *std.build.Builder,
b: *std.Build,
target: anytype,
optimize: anytype,
libc_only_std_static: *std.build.LibExeObjStep,
zig_start: *std.build.LibExeObjStep,
zig_posix: *std.build.LibExeObjStep,
) *std.build.LibExeObjStep {
libc_only_std_static: *std.Build.Step.Compile,
zig_start: *std.Build.Step.Compile,
zig_posix: *std.Build.Step.Compile,
) *std.Build.Step.Compile {
const repo = GitRepoStep.create(b, .{
.url = "https://github.com/onetrueawk/awk",
.sha = "9e248c317b88470fc86aa7c988919dc49452c88c",
Expand Down Expand Up @@ -41,18 +41,19 @@ pub fn addAwk(
files.append(b.pathJoin(&.{ repo_path, "src", src })) catch unreachable;
}

exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{
"-std=c11",
exe.addCSourceFiles(.{
.files = files.toOwnedSlice() catch unreachable,
.flags = &[_][]const u8{"-std=c11"},
});

exe.addIncludePath(.{ .path = "inc/libc" });
exe.addIncludePath(.{ .path = "inc/posix" });
exe.addIncludePath(.{ .path = "inc/gnu" });
exe.addIncludePath(b.path("inc/libc"));
exe.addIncludePath(b.path("inc/posix"));
exe.addIncludePath(b.path("inc/gnu"));
exe.linkLibrary(libc_only_std_static);
exe.linkLibrary(zig_start);
exe.linkLibrary(zig_posix);
// TODO: should libc_only_std_static and zig_start be able to add library dependencies?
if (target.getOs().tag == .windows) {
if (target.result.os.tag == .windows) {
exe.linkSystemLibrary("ntdll");
exe.linkSystemLibrary("kernel32");
}
Expand Down
Loading