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

zig update #37

Merged
merged 1 commit into from
Jan 1, 2022
Merged
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 .github/workflows/artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.9.0-dev.1524+d2f9646d9
version: 0.10.0-dev.104+e7ac05e88
- run: |
zig build test -Dfetch
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn buildOrFail(b: *Builder) anyerror {
const ziget_repo = GitRepoStep.create(b, .{
.url = "https://github.com/marler8997/ziget",
.branch = null,
.sha = "0b43c12a395b67326f5f60dd593a2eea745178c2",
.sha = @embedFile("zigetsha"),
});
const build2 = addBuild(b, .{ .path = "build2.zig" }, .{});
build2.addArgs(try getBuildArgs(b));
Expand Down
8 changes: 4 additions & 4 deletions build2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn build(b: *Builder) !void {
const ziget_repo = GitRepoStep.create(b, .{
.url = "https://github.com/marler8997/ziget",
.branch = null,
.sha = "0b43c12a395b67326f5f60dd593a2eea745178c2",
.sha = @embedFile("zigetsha"),
});

// TODO: implement this if/when we get @tryImport
Expand Down Expand Up @@ -82,9 +82,9 @@ fn addZigupExe(

if (targetIsWindows(target)) {
const zarc_repo = GitRepoStep.create(b, .{
.url = "https://github.com/SuperAuguste/zarc",
.branch = null,
.sha = "2a8fd27baa781b9de821b1b4e0b89283413054b8",
.url = "https://github.com/marler8997/zarc",
.branch = "protected",
.sha = "f66a05a50c71e17ee74fa13047f84002fb5cd6a0",
});
exe.step.dependOn(&zarc_repo.step);
const zarc_repo_path = zarc_repo.getPath(&exe.step);
Expand Down
6 changes: 3 additions & 3 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn main() !void {

var allocator_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer allocator_store.deinit();
const allocator = &allocator_store.allocator;
const allocator = allocator_store.allocator();

{
const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"-h"});
Expand Down Expand Up @@ -160,11 +160,11 @@ fn dumpExecResult(result: std.ChildProcess.ExecResult) void {
fn runNoCapture(cwd: []const u8, argv: []const []const u8) !void {
var arena_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_store.deinit();
const result = try runCaptureOuts(&arena_store.allocator, cwd, argv);
const result = try runCaptureOuts(arena_store.allocator(), cwd, argv);
dumpExecResult(result);
try passOrThrow(result.term);
}
fn runCaptureOuts(allocator: *std.mem.Allocator, cwd: []const u8, argv: []const []const u8) !std.ChildProcess.ExecResult {
fn runCaptureOuts(allocator: std.mem.Allocator, cwd: []const u8, argv: []const []const u8) !std.ChildProcess.ExecResult {
{
const cmd = try std.mem.join(allocator, " ", argv);
defer allocator.free(cmd);
Expand Down
1 change: 1 addition & 0 deletions zigetsha
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
484da18c6a7a7db188a36a3ce8254285517203c9
52 changes: 26 additions & 26 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const archive_ext = if (builtin.os.tag == .windows) "zip" else "tar.xz";
var global_optional_install_dir: ?[]const u8 = null;
var global_optional_path_link: ?[]const u8 = null;

//fn find_zigs(allocator: *Allocator) !?[][]u8 {
//fn find_zigs(allocator: Allocator) !?[][]u8 {
// // don't worry about free for now, this is a short lived program
//
// if (builtin.os.tag == .windows) {
Expand All @@ -54,7 +54,7 @@ var global_optional_path_link: ?[]const u8 = null;
// @panic("not impl");
//}

fn download(allocator: *Allocator, url: []const u8, writer: anytype) !void {
fn download(allocator: Allocator, url: []const u8, writer: anytype) !void {
var download_options = ziget.request.DownloadOptions{
.flags = 0,
.allocator = allocator,
Expand All @@ -73,13 +73,13 @@ fn download(allocator: *Allocator, url: []const u8, writer: anytype) !void {
);
}

fn downloadToFileAbsolute(allocator: *Allocator, url: []const u8, file_absolute: []const u8) !void {
fn downloadToFileAbsolute(allocator: Allocator, url: []const u8, file_absolute: []const u8) !void {
const file = try std.fs.createFileAbsolute(file_absolute, .{});
defer file.close();
try download(allocator, url, file.writer());
}

fn downloadToString(allocator: *Allocator, url: []const u8) ![]u8 {
fn downloadToString(allocator: Allocator, url: []const u8) ![]u8 {
var response_array_list = try ArrayList(u8).initCapacity(allocator, 20 * 1024); // 20 KB (modify if response is expected to be bigger)
errdefer response_array_list.deinit();
try download(allocator, url, response_array_list.writer());
Expand All @@ -99,7 +99,7 @@ fn getHomeDir() ![]const u8 {
};
}

fn allocInstallDirString(allocator: *Allocator) ![]const u8 {
fn allocInstallDirString(allocator: Allocator) ![]const u8 {
// TODO: maybe support ZIG_INSTALL_DIR environment variable?
// TODO: maybe support a file on the filesystem to configure install dir?
const home = try getHomeDir();
Expand All @@ -112,7 +112,7 @@ fn allocInstallDirString(allocator: *Allocator) ![]const u8 {
const GetInstallDirOptions = struct {
create: bool,
};
fn getInstallDir(allocator: *Allocator, options: GetInstallDirOptions) ![]const u8 {
fn getInstallDir(allocator: Allocator, options: GetInstallDirOptions) ![]const u8 {
var optional_dir_to_free_on_error: ?[]const u8 = null;
errdefer if (optional_dir_to_free_on_error) |dir| allocator.free(dir);

Expand All @@ -132,7 +132,7 @@ fn getInstallDir(allocator: *Allocator, options: GetInstallDirOptions) ![]const
return install_dir;
}

fn makeZigPathLinkString(allocator: *Allocator) ![]const u8 {
fn makeZigPathLinkString(allocator: Allocator) ![]const u8 {
if (global_optional_path_link) |path| return path;

// for now we're just going to hardcode the path to $HOME/bin/zig
Expand All @@ -144,7 +144,7 @@ fn makeZigPathLinkString(allocator: *Allocator) ![]const u8 {
}

// TODO: this should be in standard lib
fn toAbsolute(allocator: *Allocator, path: []const u8) ![]u8 {
fn toAbsolute(allocator: Allocator, path: []const u8) ![]u8 {
std.debug.assert(!std.fs.path.isAbsolute(path));
const cwd = try std.process.getCwdAlloc(allocator);
defer allocator.free(cwd);
Expand Down Expand Up @@ -198,7 +198,7 @@ pub fn main2() !u8 {
}

var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = &arena.allocator;
const allocator = arena.allocator();

const args_array = try std.process.argsAlloc(allocator);
// no need to free, os will do it
Expand Down Expand Up @@ -316,7 +316,7 @@ pub fn main2() !u8 {

const SetDefault = enum { set_default, leave_default };

fn fetchCompiler(allocator: *Allocator, version_arg: []const u8, set_default: SetDefault) !void {
fn fetchCompiler(allocator: Allocator, version_arg: []const u8, set_default: SetDefault) !void {
const install_dir = try getInstallDir(allocator, .{ .create = true });
defer allocator.free(install_dir);

Expand Down Expand Up @@ -365,13 +365,13 @@ const download_index_url = "https://ziglang.org/download/index.json";
const DownloadIndex = struct {
text: []u8,
json: std.json.ValueTree,
pub fn deinit(self: *DownloadIndex, allocator: *Allocator) void {
pub fn deinit(self: *DownloadIndex, allocator: Allocator) void {
self.json.deinit();
allocator.free(self.text);
}
};

fn fetchDownloadIndex(allocator: *Allocator) !DownloadIndex {
fn fetchDownloadIndex(allocator: Allocator) !DownloadIndex {
const text = downloadToString(allocator, download_index_url) catch |e| switch (e) {
else => {
std.debug.print("failed to download '{s}': {}\n", .{ download_index_url, e });
Expand Down Expand Up @@ -455,7 +455,7 @@ fn existsAbsolute(absolutePath: []const u8) !bool {
return true;
}

fn listCompilers(allocator: *Allocator) !void {
fn listCompilers(allocator: Allocator) !void {
const install_dir_string = try getInstallDir(allocator, .{ .create = false });
defer allocator.free(install_dir_string);

Expand All @@ -478,7 +478,7 @@ fn listCompilers(allocator: *Allocator) !void {
}
}

fn keepCompiler(allocator: *Allocator, compiler_version: []const u8) !void {
fn keepCompiler(allocator: Allocator, compiler_version: []const u8) !void {
const install_dir_string = try getInstallDir(allocator, .{ .create = true });
defer allocator.free(install_dir_string);

Expand All @@ -497,7 +497,7 @@ fn keepCompiler(allocator: *Allocator, compiler_version: []const u8) !void {
std.debug.print("created '{s}{c}{s}{c}{s}'\n", .{ install_dir_string, std.fs.path.sep, compiler_version, std.fs.path.sep, "keep" });
}

fn cleanCompilers(allocator: *Allocator, compiler_name_opt: ?[]const u8) !void {
fn cleanCompilers(allocator: Allocator, compiler_name_opt: ?[]const u8) !void {
const install_dir_string = try getInstallDir(allocator, .{ .create = true });
defer allocator.free(install_dir_string);
// getting the current compiler
Expand Down Expand Up @@ -542,7 +542,7 @@ fn cleanCompilers(allocator: *Allocator, compiler_name_opt: ?[]const u8) !void {
}
}
}
fn readDefaultCompiler(allocator: *Allocator, buffer: *[std.fs.MAX_PATH_BYTES]u8) !?[]const u8 {
fn readDefaultCompiler(allocator: Allocator, buffer: *[std.fs.MAX_PATH_BYTES]u8) !?[]const u8 {
const path_link = try makeZigPathLinkString(allocator);
defer allocator.free(path_link);

Expand All @@ -567,15 +567,15 @@ fn readDefaultCompiler(allocator: *Allocator, buffer: *[std.fs.MAX_PATH_BYTES]u8
return error.AlreadyReported;
}
const target_exe = content[1..content.len - 3];
return try std.mem.dupe(allocator, u8, targetPathToVersion(target_exe));
return try allocator.dupe(u8, targetPathToVersion(target_exe));
}

const target_path = std.fs.readLinkAbsolute(path_link, buffer) catch |e| switch (e) {
error.FileNotFound => return null,
else => return e,
};
defer allocator.free(target_path);
return try std.mem.dupe(allocator, u8, targetPathToVersion(target_path));
return try allocator.dupe(u8, targetPathToVersion(target_path));
}
fn targetPathToVersion(target_path: []const u8) []const u8 {
return std.fs.path.basename(std.fs.path.dirname(std.fs.path.dirname(target_path).?).?);
Expand All @@ -596,23 +596,23 @@ fn readMasterDir(buffer: *[std.fs.MAX_PATH_BYTES]u8, install_dir: *std.fs.Dir) !
};
}

fn getDefaultCompiler(allocator: *Allocator) !?[]const u8 {
fn getDefaultCompiler(allocator: Allocator) !?[]const u8 {
var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const slice_path = (try readDefaultCompiler(allocator, &buffer)) orelse return null;
var path_to_return = try allocator.alloc(u8, slice_path.len);
std.mem.copy(u8, path_to_return, slice_path);
return path_to_return;
}

fn getMasterDir(allocator: *Allocator, install_dir: *std.fs.Dir) !?[]const u8 {
fn getMasterDir(allocator: Allocator, install_dir: *std.fs.Dir) !?[]const u8 {
var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const slice_path = (try readMasterDir(&buffer, install_dir)) orelse return null;
var path_to_return = try allocator.alloc(u8, slice_path.len);
std.mem.copy(u8, path_to_return, slice_path);
return path_to_return;
}

fn printDefaultCompiler(allocator: *Allocator) !void {
fn printDefaultCompiler(allocator: Allocator) !void {
const default_compiler_opt = try getDefaultCompiler(allocator);
defer if (default_compiler_opt) |default_compiler| allocator.free(default_compiler);
const stdout = std.io.getStdOut().writer();
Expand All @@ -625,7 +625,7 @@ fn printDefaultCompiler(allocator: *Allocator) !void {

const ExistVerify = enum { existence_verified, verify_existence };

fn setDefaultCompiler(allocator: *Allocator, compiler_dir: []const u8, exist_verify: ExistVerify) !void {
fn setDefaultCompiler(allocator: Allocator, compiler_dir: []const u8, exist_verify: ExistVerify) !void {
switch (exist_verify) {
.existence_verified => {},
.verify_existence => {
Expand All @@ -652,11 +652,11 @@ fn setDefaultCompiler(allocator: *Allocator, compiler_dir: []const u8, exist_ver
}
}

fn getDefaultUrl(allocator: *Allocator, compiler_version: []const u8) ![]const u8 {
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 });
}

fn installCompiler(allocator: *Allocator, compiler_dir: []const u8, url: []const u8) !void {
fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const u8) !void {
if (try existsAbsolute(compiler_dir)) {
std.debug.print("compiler '{s}' already installed\n", .{compiler_dir});
return;
Expand Down Expand Up @@ -733,14 +733,14 @@ fn installCompiler(allocator: *Allocator, compiler_dir: []const u8, url: []const
try loggyRenameAbsolute(installing_dir, compiler_dir);
}

pub fn run(allocator: *std.mem.Allocator, argv: []const []const u8) !std.ChildProcess.Term {
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();
return proc.spawnAndWait();
}

fn logRun(allocator: *std.mem.Allocator, argv: []const []const u8) !void {
fn logRun(allocator: Allocator, argv: []const []const u8) !void {
var buffer = try allocator.alloc(u8, getCommandStringLength(argv));
defer allocator.free(buffer);

Expand Down