How to set CPU cores limit without CLI flag

Cargo has a configuration file that allows this (Configuration - The Cargo Book) which helps me not write all the time the limit of cores, I’m wondering if Zig has an equivalent, preferably by an environment variable or configuration file.

I don’t know if there is a way to set it directly in your build file or with an environment variable.

But i found something hacky that might give you a Solution: (from Tigerbeetle build.zig)

fn build_ci_step(
    b: *std.Build,
    step_ci: *std.Build.Step,
    command: anytype,
    options: struct { max_rss: u64 = 0 },
) void {
    const argv = .{ b.graph.zig_exe, "build" } ++ command;
    const system_command = b.addSystemCommand(&argv);
    const name = std.mem.join(b.allocator, " ", &command) catch @panic("OOM");
    system_command.max_stdio_size = 128 * MiB; // Prevent error.StreamTooLong.
    system_command.setName(name);
    system_command.step.max_rss = options.max_rss;
    hide_stderr(system_command);
    step_ci.dependOn(&system_command.step);
}

If i understand correctly they are invoking zig build with customs flags again from within the build.zig.

I don’t know if this is a good way to do that tho :smiley:

Well, that’s hacky, it’s one way to achieve it I guess, but not really what I wanted (non-hacky), thanks either way.

I guess you could replace your zig executable with a shell function or wrapper command that automatically adds the -j flag if you invoke a subcommand that supports it.

For example you could create a zig executable wrapper that receives the commandline arguments does a minimal parse on them and then invokes the actual zig command with -j added.