Compiling for a specific cpu in build.zig?

in b.standardTargetOptions() I’ve got

.default_target = .{
    .cpu_arch = std.Target.Cpu.Arch.thumb,
    .os_tag = std.Target.Os.Tag.linux,
    .abi = std.Target.Abi.musleabihf,
},

replacing -Dtarget=thumb-linux-musleabihf for my zig build invocation.

How do I replace -Dcpu=cortex_a7?

There’s the cpu_model option, but I don’t see an obvious way to fill it in correctly.

I’m not sure what the exact options for Cortex-A7 are, but here’s how I explicitly target a RISC-V architecture.

const CrossTarget = @import("std").zig.CrossTarget;
const target = b.resolveTargetQuery(.{
        .cpu_arch = Target.Cpu.Arch.riscv32,
        .os_tag = Target.Os.Tag.freestanding,
        .abi = Target.Abi.none,
        .cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32},
        .cpu_features_sub = disabled_features,
        .cpu_features_add = enabled_features
    });

  const exe = b.addExecutable(.{
        .target = target,
        ...
});

Full example

3 Likes

For cortex_a7 add in .default_target:

.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_a7},
3 Likes

Ah, that did the trick. Thank you.

I tried filling out .explicit myself, didn’t know std.Target has a list of them.

I suppose this is where the output from zig targets comes from?