Skip to content

Commit

Permalink
zig update: accomodate language changes and fix non-release URL
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Jul 4, 2022
1 parent 9cb83cb commit fc7f561
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/artifact.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Artifacts
on: [push, pull_request]
on: [pull_request]
jobs:
test:
strategy:
Expand All @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.10.0-dev.555+1b6a1e691
version: 0.10.0-dev.2836+2360f8c49
- run: |
zig build test -Dfetch -Dci_target=${{matrix.os}}
shell: bash
Expand Down
52 changes: 27 additions & 25 deletions GitRepoStep.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Publish Date: 2021_10_17
//! Publish Date: 2022_05_05
//! This file is hosted at github.com/marler8997/zig-build-repos and is meant to be copied
//! to projects that use it.
const std = @import("std");
Expand Down Expand Up @@ -33,7 +33,7 @@ fetch_enabled: bool,

var cached_default_fetch_option: ?bool = null;
pub fn defaultFetchOption(b: *std.build.Builder) bool {
if (cached_default_fetch_option) |_| { } else {
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.?;
Expand All @@ -49,20 +49,18 @@ pub fn create(b: *std.build.Builder, opt: struct {
}) *GitRepoStep {
var result = b.allocator.create(GitRepoStep) catch @panic("memory");
const name = std.fs.path.basename(opt.url);
result.* = GitRepoStep {
result.* = GitRepoStep{
.step = std.build.Step.init(.custom, "clone a git repository", b.allocator, make),
.builder = b,
.url = opt.url,
.name = name,
.branch = opt.branch,
.sha = opt.sha,
.path = if (opt.path) |p| (b.allocator.dupe(u8, p) catch @panic("memory")) else (
std.fs.path.resolve(b.allocator, &[_][]const u8{
b.build_root,
"dep",
name,
})
) catch @panic("memory"),
.path = if (opt.path) |p| (b.allocator.dupe(u8, p) catch @panic("memory")) else (std.fs.path.resolve(b.allocator, &[_][]const u8{
b.build_root,
"dep",
name,
})) catch @panic("memory"),
.sha_check = opt.sha_check,
.fetch_enabled = if (opt.fetch_enabled) |fe| fe else defaultFetchOption(b),
};
Expand All @@ -72,7 +70,7 @@ pub fn create(b: *std.build.Builder, opt: struct {
// 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
// TODO: should probably use step.loop_flag to prevent infinite recursion
// when a circular reference is encountered, or maybe keep track of
// the steps encounterd with a hash set
if (dep == dep_candidate or hasDependency(dep, dep_candidate))
Expand All @@ -85,12 +83,17 @@ fn make(step: *std.build.Step) !void {
const self = @fieldParentPtr(GitRepoStep, "step", step);

std.fs.accessAbsolute(self.path, .{}) catch {
const branch_args = if (self.branch) |b| &[2][]const u8 {" -b ", b} else &[2][]const u8 {"", ""};
const branch_args = if (self.branch) |b| &[2][]const u8{ " -b ", b } else &[2][]const u8{ "", "" };
if (!self.fetch_enabled) {
std.debug.print("Error: git repository '{s}' does not exist\n", .{self.path});
std.debug.print(" Use -Dfetch to download it automatically, or run the following to clone it:\n", .{});
std.debug.print(" git clone {s}{s}{s} {s} && git -C {3s} checkout {s} -b fordep\n",
.{self.url, branch_args[0], branch_args[1], self.path, self.sha});
std.debug.print(" git clone {s}{s}{s} {s} && git -C {3s} checkout {s} -b fordep\n", .{
self.url,
branch_args[0],
branch_args[1],
self.path,
self.sha,
});
std.os.exit(1);
}

Expand All @@ -109,9 +112,10 @@ fn make(step: *std.build.Step) !void {
}
try run(self.builder, args.items);
}
try run(self.builder, &[_][]const u8 {
try run(self.builder, &[_][]const u8{
"git",
"-C", self.path,
"-C",
self.path,
"checkout",
self.sha,
"-b",
Expand All @@ -129,9 +133,10 @@ fn checkSha(self: GitRepoStep) !void {
const result: union(enum) { failed: anyerror, output: []const u8 } = blk: {
const result = std.ChildProcess.exec(.{
.allocator = self.builder.allocator,
.argv = &[_][]const u8 {
.argv = &[_][]const u8{
"git",
"-C", self.path,
"-C",
self.path,
"rev-parse",
"HEAD",
},
Expand All @@ -151,11 +156,11 @@ fn checkSha(self: GitRepoStep) !void {
};
switch (result) {
.failed => |err| {
return self.sha_check.reportFail("failed to retreive sha for repository '{s}': {s}", .{self.name, @errorName(err)});
return self.sha_check.reportFail("failed to retreive sha for repository '{s}': {s}", .{ self.name, @errorName(err) });
},
.output => |output| {
if (!std.mem.eql(u8, std.mem.trimRight(u8, output, "\n\r"), self.sha)) {
return self.sha_check.reportFail("repository '{s}' sha does not match\nexpected: {s}\nactual : {s}\n", .{self.name, self.sha, output});
return self.sha_check.reportFail("repository '{s}' sha does not match\nexpected: {s}\nactual : {s}\n", .{ self.name, self.sha, output });
}
},
}
Expand All @@ -168,16 +173,13 @@ fn run(builder: *std.build.Builder, argv: []const []const u8) !void {
const writer = msg.writer();
var prefix: []const u8 = "";
for (argv) |arg| {
try writer.print("{s}\"{s}\"", .{prefix, arg});
try writer.print("{s}\"{s}\"", .{ prefix, arg });
prefix = " ";
}
std.log.info("[RUN] {s}", .{msg.items});
}


const child = try std.ChildProcess.init(argv, builder.allocator);
defer child.deinit();

var child = std.ChildProcess.init(argv, builder.allocator);
child.stdin_behavior = .Ignore;
child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit;
Expand Down
2 changes: 1 addition & 1 deletion build2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn addZigupExe(
const zarc_repo_path = zarc_repo.getPath(&exe.step);
exe.addPackage(Pkg {
.name = "zarc",
.path = .{ .path = try join(b, &[_][]const u8 { zarc_repo_path, "src", "main.zig" }) },
.source = .{ .path = try join(b, &[_][]const u8 { zarc_repo_path, "src", "main.zig" }) },
});
}

Expand Down
18 changes: 4 additions & 14 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path_env_sep = if (builtin.os.tag == .windows) ";" else ":";

const fixdeletetree = @import("fixdeletetree.zig");

var child_env_map: std.BufMap = undefined;
var child_env_map: std.process.EnvMap = undefined;
var path_env_ptr: *[]const u8 = undefined;
fn setPathEnv(new_path: []const u8) void {
path_env_ptr.* = new_path;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn main() !u8 {

// add our scratch/bin directory to PATH
child_env_map = try std.process.getEnvMap(allocator);
path_env_ptr = bufMapGetEnvPtr(child_env_map, "PATH") orelse {
path_env_ptr = child_env_map.getPtr("PATH") orelse {
std.log.err("the PATH environment variable does not exist?", .{});
return 1;
};
Expand Down Expand Up @@ -205,6 +205,8 @@ pub fn main() !u8 {
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, " is lower priority in PATH than "));
}
}
// verify a dev build
try runNoCapture(zigup_args ++ &[_][]const u8 { "0.10.0-dev.2836+2360f8c49" });

std.log.info("Success", .{});
return 0;
Expand Down Expand Up @@ -277,15 +279,3 @@ fn execResultPassed(term: std.ChildProcess.Term) bool {
else => return false,
}
}

fn bufMapGetEnvPtr(buf_map: std.BufMap, env_name: []const u8) ?*[]const u8 {
if (builtin.os.tag == .windows) {
var it = buf_map.iterator();
while (it.next()) |kv| {
if (std.ascii.eqlIgnoreCase(env_name, kv.key_ptr.*)) {
return kv.value_ptr;
}
}
}
return buf_map.getPtr(env_name);
}
5 changes: 2 additions & 3 deletions win32exelink.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export var zig_exe_string: [exe_marker_len + std.fs.MAX_PATH_BYTES + 1]u8 =
("!!!THIS MARKS THE zig_exe_string MEMORY!!#" ++ ([1]u8 {0} ** (std.fs.MAX_PATH_BYTES + 1))).*;

const global = struct {
var child: *std.ChildProcess = undefined;
var child: std.ChildProcess = undefined;
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = arena_instance.allocator();
};
Expand Down Expand Up @@ -42,8 +42,7 @@ pub fn main() !u8 {
args[0] = zig_exe;

// NOTE: create the ChildProcess before calling SetConsoleCtrlHandler because it uses it
global.child = try std.ChildProcess.init(args, global.arena);
defer global.child.deinit();
global.child = std.ChildProcess.init(args, global.arena);

if (0 == win32.SetConsoleCtrlHandler(consoleCtrlHandler, 1)) {
log.err("SetConsoleCtrlHandler failed, error={}", .{win32.GetLastError()});
Expand Down
2 changes: 1 addition & 1 deletion zigetsha
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5ff0dcb4b27b7351a86f4c5a4b164b4d01b740e7
116486f06e82aa7de9895e9145a22b384e029e5f
16 changes: 11 additions & 5 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,7 @@ pub fn runCompiler(allocator: Allocator, args: []const []const u8) !u8 {
try argv.appendSlice(args[1..]);

// TODO: use "execve" if on linux
const proc = try std.ChildProcess.init(argv.items, allocator);
defer proc.deinit();
var proc = std.ChildProcess.init(argv.items, allocator);
const ret_val = try proc.spawnAndWait();
switch (ret_val) {
.Exited => |code| return code,
Expand Down Expand Up @@ -875,8 +874,16 @@ fn createExeLink(link_target: []const u8, path_link: []const u8) !void {
try file.writer().writeAll(win32exelink.content[win32exelink.exe_offset + link_target.len ..]);
}

const VersionKind = enum { release, dev };
fn determineVersionKind(version: []const u8) VersionKind {
return if (std.mem.indexOfAny(u8, version, "-+")) |_| .dev else .release;
}

fn getDefaultUrl(allocator: Allocator, compiler_version: []const u8) ![]const u8 {
return try std.fmt.allocPrint(allocator, "https://ziglang.org/download/{s}/zig-" ++ url_platform ++ "-{0s}." ++ archive_ext, .{ compiler_version });
return switch (determineVersionKind(compiler_version)) {
.dev => try std.fmt.allocPrint(allocator, "https://ziglang.org/builds/zig-" ++ url_platform ++ "-{0s}." ++ archive_ext, .{ compiler_version }),
.release => try std.fmt.allocPrint(allocator, "https://ziglang.org/download/{s}/zig-" ++ url_platform ++ "-{0s}." ++ archive_ext, .{ compiler_version }),
};
}

fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const u8) !void {
Expand Down Expand Up @@ -958,8 +965,7 @@ fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const

pub fn run(allocator: Allocator, argv: []const []const u8) !std.ChildProcess.Term {
try logRun(allocator, argv);
var proc = try std.ChildProcess.init(argv, allocator);
defer proc.deinit();
var proc = std.ChildProcess.init(argv, allocator);
return proc.spawnAndWait();
}

Expand Down

0 comments on commit fc7f561

Please sign in to comment.