feat: build.zig: add step to build for all platforms
/ build-x86_64-linux-gnu (push) Successful in 1m10s Details

ci-dev
LeRoyce Pearson 2024-05-29 22:57:00 -06:00
parent 026d835803
commit 5c4a10bf26
2 changed files with 112 additions and 108 deletions

View File

@ -9,8 +9,9 @@ jobs:
version: 0.12.0 version: 0.12.0
cache: false cache: false
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- run: zig build install --summary all -Dtarget=x86_64-linux-gnu -Doptimize=ReleaseSmall - name: zig build dist
run: zig build dist --summary all -Doptimize=ReleaseSmall
- uses: https://code.forgejo.org/forgejo/upload-artifact@v3 - uses: https://code.forgejo.org/forgejo/upload-artifact@v3
with: with:
name: seizer-solitaire name: seizer-solitaire-dist
path: zig-out/bin/seizer-solitaire path: zig-out/

213
build.zig
View File

@ -7,43 +7,12 @@ const seizer_solitaire_version = std.SemanticVersion{ .major = 0, .minor = 1, .p
// declaratively construct a build graph that will be executed by an external // declaratively construct a build graph that will be executed by an external
// runner. // runner.
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const seizer = b.dependency("seizer", .{ const version = try getVersionString(b);
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{ const exe = try addSeizerSolitaireExe(b, b.getInstallStep(), .{ .target = target, .optimize = optimize, .version = version });
.name = "seizer-solitaire",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("seizer", seizer.module("seizer"));
b.installArtifact(exe);
// additionally generate an HTML file with the wasm module embedded when we use the wasi target
if (target.result.os.tag == .wasi) {
exe.wasi_exec_model = .reactor;
const bundle_webpage_exe = seizer.artifact("bundle-webpage");
const bundle_webpage = b.addRunArtifact(bundle_webpage_exe);
bundle_webpage.addArtifactArg(exe);
const install_html = b.addInstallFile(bundle_webpage.captureStdOut(), "www/seizer-solitaire.html");
b.getInstallStep().dependOn(&install_html.step);
}
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
@ -55,85 +24,119 @@ pub fn build(b: *std.Build) !void {
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const exe_unit_tests = b.addTest(.{ // build for all targets
.root_source_file = .{ .path = "src/main.zig" }, const dist_step = b.step("dist", "Build binaries for all targets");
.target = target, _ = try addSeizerSolitaireExe(b, dist_step, .{ .optimize = optimize, .version = version, .target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu }) });
.optimize = optimize, _ = try addSeizerSolitaireExe(b, dist_step, .{ .optimize = optimize, .version = version, .target = b.resolveTargetQuery(.{ .cpu_arch = .arm, .os_tag = .linux, .abi = .gnueabihf }) });
_ = try addSeizerSolitaireExe(b, dist_step, .{ .optimize = optimize, .version = version, .target = b.resolveTargetQuery(.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu }) });
_ = try addSeizerSolitaireExe(b, dist_step, .{ .optimize = optimize, .version = version, .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi, .abi = .none }) });
}
pub fn addSeizerSolitaireExe(b: *std.Build, install_step: *std.Build.Step, options: struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
version: [:0]const u8,
}) !*std.Build.Step.Compile {
const seizer = b.dependency("seizer", .{
.target = options.target,
.optimize = options.optimize,
}); });
exe_unit_tests.root_module.addImport("seizer", seizer.module("seizer"));
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); const exe = b.addExecutable(.{
.name = "seizer-solitaire",
.root_source_file = .{ .path = "src/main.zig" },
.target = options.target,
.optimize = options.optimize,
});
exe.root_module.addImport("seizer", seizer.module("seizer"));
// Similar to creating the run step earlier, this exposes a `test` step to const install_exe = b.addInstallArtifact(exe, .{ .dest_sub_path = b.fmt("{s}/{s}", .{ try options.target.result.linuxTriple(b.allocator), exe.name }) });
// the `zig build --help` menu, providing a way for the user to request install_step.dependOn(&install_exe.step);
// running the unit tests.
const test_step = b.step("test", "Run unit tests"); // additionally generate an HTML file with the wasm module embedded when we use the wasi target
test_step.dependOn(&run_exe_unit_tests.step); if (options.target.result.os.tag == .wasi) {
exe.wasi_exec_model = .reactor;
const bundle_webpage_exe = seizer.artifact("bundle-webpage");
const bundle_webpage = b.addRunArtifact(bundle_webpage_exe);
bundle_webpage.addArtifactArg(exe);
const install_html = b.addInstallFile(bundle_webpage.captureStdOut(), "www/seizer-solitaire.html");
install_step.dependOn(&install_html.step);
}
// code to add version to binary, stolen from the zig language's build.zig // code to add version to binary, stolen from the zig language's build.zig
const exe_options = b.addOptions(); const exe_options = b.addOptions();
exe.root_module.addOptions("build_options", exe_options); exe.root_module.addOptions("build_options", exe_options);
const opt_version_string = b.option([]const u8, "version-string", "Override version string. Default is to find out with git."); exe_options.addOption([:0]const u8, "version", options.version);
const version_slice = if (opt_version_string) |version| version else v: {
if (!std.process.can_spawn) {
std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{});
std.process.exit(1);
}
const version_string = b.fmt("{d}.{d}.{d}", .{ seizer_solitaire_version.major, seizer_solitaire_version.minor, seizer_solitaire_version.patch });
var code: u8 = undefined; return exe;
const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{ }
"git",
"-C", pub fn getVersionString(b: *std.Build) ![:0]const u8 {
b.build_root.path orelse ".", const opt_version_string = b.option([]const u8, "version-string", "Override version string. Default is to find out with git.");
"describe", if (opt_version_string) |version| {
"--match", return try b.allocator.dupeZ(u8, version);
"*.*.*", }
"--tags",
"--abbrev=9", if (!std.process.can_spawn) {
}, &code, .Ignore) catch { std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{});
break :v version_string; std.process.exit(1);
}; }
const git_describe = mem.trim(u8, git_describe_untrimmed, " \n\r"); const version_string = b.fmt("{d}.{d}.{d}", .{ seizer_solitaire_version.major, seizer_solitaire_version.minor, seizer_solitaire_version.patch });
switch (mem.count(u8, git_describe, "-")) { var code: u8 = undefined;
0 => { const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{
// Tagged release version (e.g. 0.10.0). "git",
if (!mem.eql(u8, git_describe, version_string)) { "-C",
std.debug.print("Zig version '{s}' does not match Git tag '{s}'\n", .{ version_string, git_describe }); b.build_root.path orelse ".",
std.process.exit(1); "describe",
} "--match",
break :v version_string; "*.*.*",
}, "--tags",
2 => { "--abbrev=9",
// Untagged development build (e.g. 0.10.0-dev.2025+ecf0050a9). }, &code, .Ignore) catch {
var it = mem.splitScalar(u8, git_describe, '-'); return try b.allocator.dupeZ(u8, version_string);
const tagged_ancestor = it.first(); };
const commit_height = it.next().?; const git_describe = mem.trim(u8, git_describe_untrimmed, " \n\r");
const commit_id = it.next().?;
switch (mem.count(u8, git_describe, "-")) {
const ancestor_ver = try std.SemanticVersion.parse(tagged_ancestor); 0 => {
if (seizer_solitaire_version.order(ancestor_ver) != .gt) { // Tagged release version (e.g. 0.10.0).
std.debug.print("version '{}' must be greater than tagged ancestor '{}'\n", .{ seizer_solitaire_version, ancestor_ver }); if (!mem.eql(u8, git_describe, version_string)) {
std.process.exit(1); std.debug.print("Zig version '{s}' does not match Git tag '{s}'\n", .{ version_string, git_describe });
} std.process.exit(1);
}
// Check that the commit hash is prefixed with a 'g' (a Git convention). return try b.allocator.dupeZ(u8, version_string);
if (commit_id.len < 1 or commit_id[0] != 'g') { },
std.debug.print("Unexpected `git describe` output: {s}\n", .{git_describe}); 2 => {
break :v version_string; // Untagged development build (e.g. 0.10.0-dev.2025+ecf0050a9).
} var it = mem.splitScalar(u8, git_describe, '-');
const tagged_ancestor = it.first();
// The version is reformatted in accordance with the https://semver.org specification. const commit_height = it.next().?;
break :v b.fmt("{s}-dev.{s}+{s}", .{ version_string, commit_height, commit_id[1..] }); const commit_id = it.next().?;
},
else => { const ancestor_ver = try std.SemanticVersion.parse(tagged_ancestor);
std.debug.print("Unexpected `git describe` output: {s}\n", .{git_describe}); if (seizer_solitaire_version.order(ancestor_ver) != .gt) {
break :v version_string; std.debug.print("version '{}' must be greater than tagged ancestor '{}'\n", .{ seizer_solitaire_version, ancestor_ver });
}, std.process.exit(1);
} }
};
const version = try b.allocator.dupeZ(u8, version_slice); // Check that the commit hash is prefixed with a 'g' (a Git convention).
exe_options.addOption([:0]const u8, "version", version); if (commit_id.len < 1 or commit_id[0] != 'g') {
std.debug.print("Unexpected `git describe` output: {s}\n", .{git_describe});
return try b.allocator.dupeZ(u8, version_string);
}
// The version is reformatted in accordance with the https://semver.org specification.
const formatted_version = b.fmt("{s}-dev.{s}+{s}", .{ version_string, commit_height, commit_id[1..] });
return try b.allocator.dupeZ(u8, formatted_version);
},
else => {
std.debug.print("Unexpected `git describe` output: {s}\n", .{git_describe});
return try b.allocator.dupeZ(u8, version_string);
},
}
} }