Skip to content

Commit

Permalink
Updated build.zig to build for multiple systems
Browse files Browse the repository at this point in the history
  • Loading branch information
BrookJeynes committed May 4, 2024
1 parent 993c607 commit 9f2751c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
84 changes: 53 additions & 31 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const builtin = @import("builtin");
const min_zig_string = "0.13.0-dev.44+9d64332a5";
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 };

const targets: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
};

const Build = blk: {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
Expand All @@ -13,8 +21,7 @@ const Build = blk: {
break :blk std.Build;
};

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const options = b.addOptions();

Expand All @@ -23,34 +30,49 @@ pub fn build(b: *std.Build) void {
options.addOption(std.SemanticVersion, "zigup_version", version);
const exe_options_module = options.createModule();

const clap_dep = b.dependency("clap", .{ .target = target });
const clap_mod = clap_dep.module("clap");

const exe = b.addExecutable(.{
.name = "zigup",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("clap", clap_mod);
exe.root_module.addImport("options", exe_options_module);
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
for (targets) |t| {
const target = b.resolveTargetQuery(t);

const clap_dep = b.dependency("clap", .{ .target = target });
const clap_mod = clap_dep.module("clap");

const exe = b.addExecutable(.{
.name = "zigup",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("clap", clap_mod);
exe.root_module.addImport("options", exe_options_module);
b.installArtifact(exe);

const target_output = b.addInstallArtifact(exe, .{
.dest_dir = .{
.override = .{
.custom = try t.zigTriple(b.allocator),
},
},
});

b.getInstallStep().dependOn(&target_output.step);

// TODO: Figure out how to do this.
// const run_cmd = b.addRunArtifact(exe);
// run_cmd.step.dependOn(b.getInstallStep());
// if (b.args) |args| {
// run_cmd.addArgs(args);
// }
// const run_step = b.step("run", "Run the app");
// run_step.dependOn(&run_cmd.step);
//
// const exe_unit_tests = b.addTest(.{
// .root_source_file = .{ .path = "src/main.zig" },
// .target = target,
// .optimize = optimize,
// });
//
// const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
// const test_step = b.step("test", "Run unit tests");
// test_step.dependOn(&run_exe_unit_tests.step);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const exe_unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
1 change: 1 addition & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn main() !void {
.diagnostic = &diag,
.allocator = allocator,
}) catch |err| {
// TODO: Pretty print this.
diag.report(std.io.getStdErr().writer(), err) catch {};
return err;
};
Expand Down

0 comments on commit 9f2751c

Please sign in to comment.