Skip to content

Commit

Permalink
Automatically create tar archives on release using the system-install…
Browse files Browse the repository at this point in the history
…ed tar command.
  • Loading branch information
IntegratedQuantum committed May 17, 2024
1 parent 50807f5 commit 4051777
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ pub inline fn makeCubyzLibs(b: *std.Build, name: []const u8, target: std.Build.R
return c_lib;
}

fn runChild(step: *std.Build.Step, argv: []const []const u8) !void {
const allocator = step.owner.allocator;
const result = try std.ChildProcess.run(.{.allocator = allocator, .argv = argv});
try std.io.getStdOut().writeAll(result.stdout);
try std.io.getStdErr().writeAll(result.stderr);
allocator.free(result.stdout);
allocator.free(result.stderr);
}

fn packageFunction(step: *std.Build.Step, _: *std.Progress.Node) anyerror!void {
const base: []const []const u8 = &.{"tar", "-czf"};
try runChild(step, base ++ .{"zig-out/cubyz_deps_x86_64-windows-gnu.tar.gz", "zig-out/lib/cubyz_deps_x86_64-windows-gnu.lib"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_aarch64-windows-gnu.tar.gz", "zig-out/lib/cubyz_deps_aarch64-windows-gnu.lib"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_x86_64-linux-musl.tar.gz", "zig-out/lib/libcubyz_deps_x86_64-linux-musl.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_aarch64-linux-musl.tar.gz", "zig-out/lib/libcubyz_deps_aarch64-linux-musl.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_x86_64-macos-none.tar.gz", "zig-out/lib/libcubyz_deps_x86_64-macos-none.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_aarch64-macos-none.tar.gz", "zig-out/lib/libcubyz_deps_aarch64-macos-none.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_headers.tar.gz", "zig-out/include"});
}

pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
Expand All @@ -220,6 +239,8 @@ pub fn build(b: *std.Build) !void {

const releaseStep = b.step("release", "Build and package all targets for distribution");
const nativeStep = b.step("native", "Build only native target for debugging or local builds");
const buildStep = b.step("build_all", "Build all targets for distribution");
releaseStep.dependOn(buildStep);

for (targets) |target| {
const t = b.resolveTargetQuery(target);
Expand All @@ -230,7 +251,7 @@ pub fn build(b: *std.Build) !void {
const install = b.addInstallArtifact(c_lib, .{});

subStep.dependOn(&install.step);
releaseStep.dependOn(subStep);
buildStep.dependOn(subStep);
}

{
Expand All @@ -241,6 +262,18 @@ pub fn build(b: *std.Build) !void {
nativeStep.dependOn(&install.step);
}

{
const step = try b.allocator.create(std.Build.Step);
step.* = std.Build.Step.init(.{
.name = "package",
.makeFn = &packageFunction,
.owner = b,
.id = .custom,
});
step.dependOn(buildStep);
releaseStep.dependOn(step);
}

// Alias the default `zig build` to only build native target.
// Run `zig build release` to build all targets.
b.getInstallStep().dependOn(nativeStep);
Expand Down

0 comments on commit 4051777

Please sign in to comment.