Skip to content
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 .github/workflows/artifact.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Artifacts
on: [pull_request]
on: [pull_request, workflow_dispatch]
jobs:
test:
strategy:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ cp zig-out/bin/zigup BIN_PATH

# Dependencies

zigup depends on https://github.com/marler8997/ziget which in turn depends on other projects depending on which SSL backend is selected. You can provide `-Dfetch` to `zig build` to automatically clone all repository dependencies, otherwise, the build will report a missing dependency error with an explanation of how to clone it.

The windows target depends on https://github.com/SuperAuguste/zarc to extract zip files. This repo might point to my fork if there are needed changes pending the acceptance of a PR: https://github.com/marler8997/zarc.

On linux and macos, zigup depends on `tar` to extract the compiler archive files (this may change in the future).
195 changes: 142 additions & 53 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,66 +1,155 @@
//! This build.zig file boostraps the real build in build2.zig

// NOTE: need to wait on https://github.com/ziglang/zig/pull/9989 before doing this
// to make build errors reasonable
const std = @import("std");
const Builder = std.build.Builder;
const builtin = @import("builtin");
const Build = std.Build;
const Pkg = std.build.Pkg;

// TODO: make this work with "GitRepoStep.zig", there is a
// problem with the -Dfetch option
const GitRepoStep = @import("GitRepoStep.zig");

pub fn build(b: *Builder) !void {
buildNoreturn(b);
fn unwrapOptionalBool(optionalBool: ?bool) bool {
if (optionalBool) |b| return b;
return false;
}
fn buildNoreturn(b: *Builder) noreturn {
const err = buildOrFail(b);
std.log.err("{s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);

pub fn build(b: *Build) !void {
// var github_release_step = b.step("github-release", "Build the github-release binaries");
// try addGithubReleaseExe(b, github_release_step, "x86_64-linux", null);

const target = if (b.option([]const u8, "ci_target", "the CI target being built")) |ci_target|
try std.zig.CrossTarget.parse(.{ .arch_os_abi = ci_target_map.get(ci_target) orelse {
std.log.err("unknown ci_target '{s}'", .{ci_target});
std.os.exit(1);
} })
else
b.standardTargetOptions(.{});

const optimize = b.standardOptimizeOption(.{});

// NOTE: weird spoof, fixes the -Dfetch problem
// I think `defaultFetchOption` is getting optimized out due to never being
// called or something like that
_ = GitRepoStep.defaultFetchOption(b);

const win32exelink_mod: ?*Build.Module = blk: {
if (target.getOs().tag == .windows) {
const exe = b.addExecutable(.{
.name = "win32exelink",
.root_source_file = .{ .path = "win32exelink.zig" },
.target = target,
.optimize = optimize,
});
break :blk b.createModule(.{
.source_file = exe.getEmittedBin(),
});
}
break :blk null;
};

// TODO: Maybe add more executables with different ssl backends
const exe = try addZigupExe(
b,
target,
optimize,
win32exelink_mod,
);
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
if (b.args) |args| {
run_cmd.addArgs(args);
}
std.os.exit(0xff);

addTest(b, exe, target, optimize);
}
fn buildOrFail(b: *Builder) anyerror {
const ziget_repo = GitRepoStep.create(b, .{
.url = "https://github.com/marler8997/ziget",
.branch = null,
.sha = @embedFile("zigetsha"),
.fetch_enabled = true,

fn addTest(
b: *Build,
exe: *Build.CompileStep,
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
) void {
const test_exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "test.zig" },
.target = target,
.optimize = optimize,
});
const build2 = addBuild(b, .{ .path = "build2.zig" }, .{});
build2.addArgs(try getBuildArgs(b));

var progress = std.Progress{};
{
var prog_node = progress.start("clone ziget", 1);
ziget_repo.step.make(prog_node) catch |e| return e;
prog_node.end();
}
{
var prog_node = progress.start("run build2.zig", 1);
build2.step.make(prog_node) catch |err| switch (err) {
error.MakeFailed => std.os.exit(0xff), // error already printed by subprocess, hopefully?
error.MakeSkipped => @panic("impossible?"),
};
prog_node.end();
const run_cmd = b.addRunArtifact(test_exe);

// TODO: make this work, add exe install path as argument to test
//run_cmd.addArg(exe.getInstallPath());
_ = exe;
run_cmd.step.dependOn(b.getInstallStep());

const test_step = b.step("test", "test the executable");
test_step.dependOn(&run_cmd.step);
}

fn addZigupExe(
b: *Build,
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
win32exelink_mod: ?*Build.Module,
) !*std.Build.CompileStep {
const exe = b.addExecutable(.{
.name = "zigup",
.root_source_file = .{ .path = "zigup.zig" },
.target = target,
.optimize = optimize,
});

if (targetIsWindows(target)) {
exe.addModule("win32exelink", win32exelink_mod.?);
const zarc_repo = GitRepoStep.create(b, .{
.url = "https://github.com/marler8997/zarc",
.branch = "protected",
.sha = "2e5256624d7871180badc9784b96dd66d927d604",
.fetch_enabled = true,
});
exe.step.dependOn(&zarc_repo.step);
const zarc_repo_path = zarc_repo.getPath(&exe.step);
const zarc_mod = b.addModule("zarc", .{
.source_file = .{ .path = b.pathJoin(&.{ zarc_repo_path, "src", "main.zig" }) },
});
exe.addModule("zarc", zarc_mod);
}
std.os.exit(0);

return exe;
}

// TODO: remove the following if https://github.com/ziglang/zig/pull/9987 is integrated
fn getBuildArgs(self: *Builder) ! []const [:0]const u8 {
const args = try std.process.argsAlloc(self.allocator);
return args[5..];
fn targetIsWindows(target: std.zig.CrossTarget) bool {
if (target.os_tag) |tag|
return tag == .windows;
return builtin.target.os.tag == .windows;
}
pub fn addBuild(self: *Builder, build_file: std.build.FileSource, _: struct { }) *std.build.RunStep {
const run_step = std.build.RunStep.create(
self,
self.fmt("zig build {s}", .{build_file.getDisplayName()}),
);
run_step.addArg(self.zig_exe);
run_step.addArg("build");
run_step.addArg("--build-file");
run_step.addFileSourceArg(build_file);
run_step.addArg("--cache-dir");
const cache_root_path = self.cache_root.path orelse @panic("todo");
run_step.addArg(self.pathFromRoot(cache_root_path));
return run_step;

fn addGithubReleaseExe(
b: *Build,
github_release_step: *Build.Step,
comptime target_triple: []const u8,
win32exelink_mod: ?*Build.Module,
) !void {
const small_release = true;

const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
const mode = if (small_release) .ReleaseSafe else .Debug;
const exe = try addZigupExe(b, target, mode, win32exelink_mod);
if (small_release) {
exe.strip = true;
}
exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str);
github_release_step.dependOn(&exe.step);
}

const ci_target_map = std.ComptimeStringMap([]const u8, .{
.{ "ubuntu-latest-x86_64", "x86_64-linux" },
.{ "macos-latest-x86_64", "x86_64-macos" },
.{ "windows-latest-x86_64", "x86_64-windows" },
.{ "ubuntu-latest-aarch64", "aarch64-linux" },
.{ "macos-latest-aarch64", "aarch64-macos" },
});
Loading