Skip to content

Commit

Permalink
rework ci
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed May 4, 2024
1 parent 6e645c4 commit a65141b
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 144 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ jobs:
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
zig build ci
- 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' }}
- if: ${{ matrix.os == 'linux-latest' }}
uses: actions/upload-artifact@v2
with:
name: zigup x86_64-linux
path: zig-out/x86_64-linux/bin/*
- if: ${{ matrix.os == 'macos-latest' }}
uses: actions/upload-artifact@v2
with:
name: zigup x86_64-macos
path: zig-out/x86_64-macos/bin/*
- if: ${{ matrix.os == 'windows-latest' }}
uses: actions/upload-artifact@v2
with:
name: zigup ${{ matrix.os }}-aarch64
path: zig-out-${{matrix.os}}-aarch64/bin/*
name: zigup x86_64-windows
path: zig-out/x86_64-windows/bin/*
195 changes: 97 additions & 98 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,39 +46,36 @@ 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);
}
}

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 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);
try ci(b, ci_step, test_step);
}

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 @@ -110,31 +86,54 @@ fn addZigupExe(
if (target.result.os.tag == .windows) {
exe.root_module.addImport("win32exelink", win32exelink_mod.?);
}

return exe;
}

fn addGithubReleaseExe(
b: *std.Build,
github_release_step: *std.build.Step,
comptime target_triple: []const u8,
) !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);
if (small_release) {
exe.strip = true;
fn ci(b: *std.Build, ci_step: *std.Build.Step, test_step: *std.Build.Step) !void {
const ci_targets = [_][]const u8 {
"x86_64-linux",
"x86_64-macos",
"x86_64-windows",
"aarch64-linux",
"aarch64-macos",
};

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);
ci_step.dependOn(&b.addInstallArtifact(zigup_exe, .{
.dest_dir = .{ .override = .{ .custom = ci_target_str } },
}).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;
}
}
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" },
});
Loading

0 comments on commit a65141b

Please sign in to comment.