Skip to content

Commit

Permalink
rework ci
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed May 5, 2024
1 parent 23ec7ae commit 49ae4a7
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 146 deletions.
33 changes: 12 additions & 21 deletions .github/workflows/artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,19 @@ jobs:
fail-fast: false
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.12.0
- run: |
zig build test -Dci_target=${{matrix.os}}-${{matrix.arch}}
- run: |
zig build -Dci_target=ubuntu-latest-x86_64 -p zig-out-ubuntu-latest-x86_64
- run: |
zig build -Dci_target=ubuntu-latest-aarch64 -p zig-out-ubuntu-latest-aarch64
- run: |
zig build -Dci_target=macos-latest-x86_64 -p zig-out-macos-latest-x86_64
- run: |
zig build -Dci_target=macos-latest-aarch64 -p zig-out-macos-latest-aarch64
- run: |
zig build -Dci_target=windows-latest-x86_64 -p zig-out-windows-latest-x86_64
- uses: actions/upload-artifact@v2
with:
name: zigup ${{ matrix.os }}-${{ matrix.arch }}
path: zig-out/bin/*
- if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
uses: actions/upload-artifact@v2
zig build ci --summary all
- if: ${{ matrix.os == 'ubuntu-latest' }}
uses: actions/upload-artifact@v4
with:
name: zigup ${{ matrix.os }}-aarch64
path: zig-out-${{matrix.os}}-aarch64/bin/*
name: zigup-archives
path: |
zig-out/zigup-x86_64-linux.tar.gz
zig-out/zigup-x86_64-macos.tar.gz
zig-out/zigup-x86_64-windows.zip
zig-out/zigup-aarch64-linux.tar.gz
zig-out/zigup-aarch64-macos.tar.gz
256 changes: 163 additions & 93 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,63 +1,42 @@
const std = @import("std");
const builtin = @import("builtin");
const Pkg = std.build.Pkg;

fn unwrapOptionalBool(optionalBool: ?bool) bool {
if (optionalBool) |b| return b;
return false;
}

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

const maybe_ci_target = b.option([]const u8, "ci_target", "the CI target being built");
const target = if (maybe_ci_target) |ci_target|
b.resolveTargetQuery(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.process.exit(1);
} }))
else
b.standardTargetOptions(.{});

// Compile in ReleaseSafe on Windows for faster extraction
const optimize: std.builtin.OptimizeMode = if (
(maybe_ci_target != null) and (target.result.os.tag == .windows)
) .ReleaseSafe else b.standardOptimizeOption(.{});

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

const zigup_exe_native = blk: {
const exe = addZigupExe(b, target, optimize);
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);
}
break :blk null;
break :blk exe;
};

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);
const test_step = b.step("test", "test the executable");
{
const exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "test.zig" },
.target = target,
.optimize = optimize,
});
const run_cmd = b.addRunArtifact(exe);
run_cmd.addArtifactArg(zigup_exe_native);
run_cmd.addDirectoryArg(b.path("scratch/native"));
test_step.dependOn(&run_cmd.step);
}

addTest(b, exe, target, optimize);
const unzip_step = b.step(
"unzip",
"Build/install the unzip cmdline tool",
);

{
const unzip = b.addExecutable(.{
Expand All @@ -67,8 +46,13 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
const install = b.addInstallArtifact(unzip, .{});
b.step("unzip", "Build/install the unzip cmdline tool").dependOn(&install.step);
unzip_step.dependOn(&install.step);
}

const zip_step = b.step(
"zip",
"Build/install the zip cmdline tool",
);
{
const zip = b.addExecutable(.{
.name = "zip",
Expand All @@ -77,39 +61,43 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
const install = b.addInstallArtifact(zip, .{});
b.step("zip", "Build/install the zip cmdline tool").dependOn(&install.step);
zip_step.dependOn(&install.step);
}
}

fn addTest(
b: *std.Build,
exe: *std.Build.Step.Compile,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
) void {
const test_exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "test.zig" },
.target = target,
.optimize = optimize,
const host_zip_exe = b.addExecutable(.{
.name = "zip",
.root_source_file = b.path("zip.zig"),
.target = b.host,
});
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);
const ci_step = b.step("ci", "The build/test step to run on the CI");
ci_step.dependOn(b.getInstallStep());
ci_step.dependOn(test_step);
ci_step.dependOn(unzip_step);
ci_step.dependOn(zip_step);
try ci(b, ci_step, test_step, host_zip_exe);
}

fn addZigupExe(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
win32exelink_mod: ?*std.Build.Module,
) !*std.Build.Step.Compile {
) *std.Build.Step.Compile {
const win32exelink_mod: ?*std.Build.Module = blk: {
if (target.result.os.tag == .windows) {
const exe = b.addExecutable(.{
.name = "win32exelink",
.root_source_file = .{ .path = "win32exelink.zig" },
.target = target,
.optimize = optimize,
});
break :blk b.createModule(.{
.root_source_file = exe.getEmittedBin(),
});
}
break :blk null;
};

const exe = b.addExecutable(.{
.name = "zigup",
.root_source_file = .{ .path = "zigup.zig" },
Expand All @@ -120,31 +108,113 @@ fn addZigupExe(
if (target.result.os.tag == .windows) {
exe.root_module.addImport("win32exelink", win32exelink_mod.?);
}

return exe;
}

fn addGithubReleaseExe(
fn ci(
b: *std.Build,
github_release_step: *std.build.Step,
comptime target_triple: []const u8,
ci_step: *std.Build.Step,
test_step: *std.Build.Step,
host_zip_exe: *std.Build.Step.Compile,
) !void {
const small_release = true;
const ci_targets = [_][]const u8 {
"x86_64-linux",
"x86_64-macos",
"x86_64-windows",
"aarch64-linux",
"aarch64-macos",
};

const make_archive_step = b.step("archive", "Create CI archives");
ci_step.dependOn(make_archive_step);

var previous_test_step = test_step;

for (ci_targets) |ci_target_str| {
const target = b.resolveTargetQuery(try std.zig.CrossTarget.parse(
.{ .arch_os_abi = ci_target_str },
));
const optimize: std.builtin.OptimizeMode =
// Compile in ReleaseSafe on Windows for faster extraction
if (target.result.os.tag == .windows) .ReleaseSafe
else .Debug;
const zigup_exe = addZigupExe(b, target, optimize);
const zigup_exe_install = b.addInstallArtifact(zigup_exe, .{
.dest_dir = .{ .override = .{ .custom = ci_target_str } },
});
ci_step.dependOn(&zigup_exe_install.step);

const test_exe = b.addExecutable(.{
.name = b.fmt("test-{s}", .{ci_target_str}),
.root_source_file = .{ .path = "test.zig" },
.target = target,
.optimize = optimize,
});
const run_cmd = b.addRunArtifact(test_exe);
run_cmd.addArtifactArg(zigup_exe);
run_cmd.addDirectoryArg(b.path(b.fmt("scratch/{s}", .{ci_target_str})));

// This doesn't seem to be working, so I've added a pre-check below
run_cmd.failing_to_execute_foreign_is_an_error = false;
const os_compatible = (builtin.os.tag == target.result.os.tag);
const arch_compatible = (builtin.cpu.arch == target.result.cpu.arch);
if (os_compatible and arch_compatible) {
ci_step.dependOn(&run_cmd.step);

// prevent tests from running at the same time so their output
// doesn't mangle each other.
run_cmd.step.dependOn(previous_test_step);
previous_test_step = &run_cmd.step;
}

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);
if (small_release) {
exe.strip = true;
if (builtin.os.tag == .linux) {
make_archive_step.dependOn(makeCiArchiveStep(
b, ci_target_str, target.result, zigup_exe_install, host_zip_exe
));
}
}
exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple);
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" },
});
fn makeCiArchiveStep(
b: *std.Build,
ci_target_str: []const u8,
target: std.Target,
exe_install: *std.Build.Step.InstallArtifact,
host_zip_exe: *std.Build.Step.Compile,
) *std.Build.Step {
const install_path = b.getInstallPath(.prefix, ".");

if (target.os.tag == .windows) {
const out_zip_file = b.pathJoin(&.{
install_path,
b.fmt("zigup-{s}.zip", .{ci_target_str}),
});
const zip = b.addRunArtifact(host_zip_exe);
zip.addArg(out_zip_file);
zip.addArg("zigup.exe");
zip.addArg("zigup.pdb");
zip.cwd = .{ .path = b.getInstallPath(
exe_install.dest_dir.?,
".",
)};
zip.step.dependOn(&exe_install.step);
return &zip.step;
}

const targz = b.pathJoin(&.{
install_path,
b.fmt("zigup-{s}.tar.gz", .{ci_target_str}),
});
const tar = b.addSystemCommand(&.{
"tar",
"-czf",
targz,
"zigup",
});
tar.cwd = .{ .path = b.getInstallPath(
exe_install.dest_dir.?,
".",
)};
tar.step.dependOn(&exe_install.step);
return &tar.step;
}
Loading

0 comments on commit 49ae4a7

Please sign in to comment.