Idiomatic way to compile for several targets simultaneously in build.zig?

Oh yeah, forgot about that. Just the name change will do:

const std = @import("std");

pub fn build(b: *std.Build) !void {
    const optimize = b.standardOptimizeOption(.{});
    const root_source_file = std.Build.FileSource.relative("src/main.zig");

    const exe_step = b.step("exe", "Install executable for all targets");

    for (TARGETS) |TARGET| {
        const name = try std.fmt.allocPrint(b.allocator, "{s}-{s}", .{
            @tagName(TARGET.cpu_arch.?),
            @tagName(TARGET.os_tag.?),
        });

        const exe = b.addExecutable(.{
            .name = name,
            .target = TARGET,
            .optimize = optimize,
            .root_source_file = root_source_file,
        });

        const exe_install = b.addInstallArtifact(exe, .{});
        exe_step.dependOn(&exe_install.step);
    }

    b.default_step.dependOn(exe_step);
}

const TARGETS = [_]std.zig.CrossTarget{
    .{ .cpu_arch = .aarch64, .os_tag = .linux },
    .{ .cpu_arch = .aarch64, .os_tag = .macos },
    .{ .cpu_arch = .aarch64, .os_tag = .windows },
};
5 Likes