Specify CLI argument in build.zigg

Is it possible to specify CLI argument from inside build.zig?
I want to integrate “–summary all” in build.zig so I don’t need to specify it in the shell.
Eg: running
zig build
Would have the same result as running this command:
zig build --summary all

I tried with addArgs but it doesn’t work:

const run_step = b.addRunArtifact(exe);
run_step.addArgs(&.{"--summary", "all"});

Try depending on the default build step, like this:

const run_step = b.addRunArtifact(exe);
run_step.addArgs(&.{"--summary", "all"});

Or, better yet, create a separate step for running the exe:

const exe_step = b.step("exe", "Run executable");

const exe_run = b.addRunArtifact(exe);
exe_run.addArgs(&.{"--summary", "all"});


Then, run it with zig build exe.

Also, it’s strange how you pass arguments on the CLI, you usually have to provide them after --, like this: zig build -- --summary all.

I am trying to pass CLI arguments to zig build system, not to my application (which is crosscompiled to another CPU arch).
Sorry if I was unclear - my example from 1st post is not the best.

Currently I need to run build like this:

% zig build --summary all
   text    data     bss     dec     hex filename
 149976       4     628  150608   24c50 ./zig-out/bin/stm32.elf
Build Summary: 4/4 steps succeeded
install success
├─ install stm32.elf cached
│  └─ zig build-exe stm32.elf Debug thumb-freestanding-eabi cached 34ms
└─ run llvm-size16 success 26ms
   └─ zig build-exe stm32.elf Debug thumb-freestanding-eabi (reused)

But I want to have same output when I run “zig build” (without using shell alias).

Oh, my bad, I didn’t recognise that build arg right away. There’s no official support for smth like that, but you can do it in a hacky way by going to your Zig installation folder, opening lib/build_runner.zig and changing this line:

var summary: ?Summary = null;

to this:

var summary: ?Summary = .all;

It’ll make --summary all the default when running zig build, while still allowing to pass other summary values if needed.

1 Like