From 5902c5fad365225ed7ef4217badb7015f1964619 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Mon, 4 Jul 2022 11:02:26 -0600 Subject: [PATCH] zig update: accomodate language changes and fix non-release URL --- .github/workflows/artifact.yml | 2 +- GitRepoStep.zig | 52 ++++++++++++++++++---------------- build2.zig | 2 +- test.zig | 16 ++--------- win32exelink.zig | 5 ++-- zigetsha | 2 +- zigup.zig | 16 +++++++---- 7 files changed, 45 insertions(+), 50 deletions(-) diff --git a/.github/workflows/artifact.yml b/.github/workflows/artifact.yml index cd10654..506cd69 100644 --- a/.github/workflows/artifact.yml +++ b/.github/workflows/artifact.yml @@ -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 diff --git a/GitRepoStep.zig b/GitRepoStep.zig index f2a777d..0909d64 100644 --- a/GitRepoStep.zig +++ b/GitRepoStep.zig @@ -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"); @@ -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.?; @@ -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), }; @@ -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)) @@ -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); } @@ -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", @@ -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", }, @@ -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 }); } }, } @@ -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; diff --git a/build2.zig b/build2.zig index c118560..3f71e21 100644 --- a/build2.zig +++ b/build2.zig @@ -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" }) }, }); } diff --git a/test.zig b/test.zig index 8dd2302..61ac629 100644 --- a/test.zig +++ b/test.zig @@ -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; @@ -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; }; @@ -277,15 +277,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); -} diff --git a/win32exelink.zig b/win32exelink.zig index 30ebd78..512639c 100644 --- a/win32exelink.zig +++ b/win32exelink.zig @@ -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(); }; @@ -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()}); diff --git a/zigetsha b/zigetsha index e706353..f4b49b6 100644 --- a/zigetsha +++ b/zigetsha @@ -1 +1 @@ -5ff0dcb4b27b7351a86f4c5a4b164b4d01b740e7 \ No newline at end of file +116486f06e82aa7de9895e9145a22b384e029e5f \ No newline at end of file diff --git a/zigup.zig b/zigup.zig index 753a02a..63932e6 100644 --- a/zigup.zig +++ b/zigup.zig @@ -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, @@ -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 { @@ -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(); }