# Zig build required option for a particular step

**URL:** <https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472>\
**Category:** Help\
**Tags:** build-system\
**Created:** [October 22, 2024, 5:22am UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472 "2024-10-22T05:22:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocompute](https://ziggit.dev/user_avatar/ziggit.dev/mocompute/32/3226_2.png) [@mocompute](https://ziggit.dev/u/mocompute)\
**Post date:** [October 22, 2024, 5:22am UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/1 "2024-10-22T05:22:00Z")

</div>

What is the right way to return an error to the user if they attempt to run a build step that requires a certain option to be non-null?

For example, I want:

`zig build dist-fat -Dassets=/path/to/assets`

to run that step with the provided option. If the option isn’t provided, the step should generate an error.

---

<div class="post-metadata">

**Author:** ![pierrelgol](https://ziggit.dev/user_avatar/ziggit.dev/pierrelgol/32/10233_2.png) [@pierrelgol](https://ziggit.dev/u/pierrelgol)\
**Post date:** [October 22, 2024, 6:50am UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/2 "2024-10-22T06:50:39Z")

</div>

I’m sure there are better ways to achieve this but here you go 🙂

```zig
    const mandatory_path = b.option([]const u8, "mandatory_path", "mandatory path required by <step>") orelse {
        std.log.err("the path is required to build the <step>. Please provide one.", .{});
        return;
    };

    const option = b.addOptions();
    option.addOption([]const u8, "mandatory_path", mandatory_path);

    const exe = b.addExecutable(.{
        .name = "temp",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.root_module.addOptions("option", option);
    b.installArtifact(exe);

```

---

<div class="post-metadata">

**Author:** ![mocompute](https://ziggit.dev/user_avatar/ziggit.dev/mocompute/32/3226_2.png) [@mocompute](https://ziggit.dev/u/mocompute)\
**Post date:** [October 23, 2024, 1:08am UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/3 "2024-10-23T01:08:01Z")

</div>

Thank you - I might not have explained it well, but I don’t think that solution works. The option is not required for the “install” step. It’s only required for the “dist-fat” step.

Currently my approach is to only define the dist-fat step if the option is supplied by the user. But this means `zig build --help` doesn’t show the dist-fat step.

What I would prefer is to define the dist-fat step but have it fail if the option is not supplied. You example will always fail if the option is not provided. An alternative I tried is to declare the step but have it do nothing if the option is null, but the problem is then we don’t get an error, so the user might thing the step succeeded.

I think the struggle is with the declarative nature of the build definitions. What I think I need is a way to declare that a step should return an error if an option is not provided. But other steps which don’t care about that option should be unaffected.

---

<div class="post-metadata">

**Author:** ![pierrelgol](https://ziggit.dev/user_avatar/ziggit.dev/pierrelgol/32/10233_2.png) [@pierrelgol](https://ziggit.dev/u/pierrelgol)\
**Post date:** [October 23, 2024, 7:27am UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/4 "2024-10-23T07:27:53Z")

</div>

if you replace the `std.log.err` by `std.log.info` it just prints the message without failing the compilation if it’s not provided, so you could put a message explaining to the user, that if he wants the “dist-fat” step to work he needs to provide this path ? but that doesn’t solve your problem of dependency

---

<div class="post-metadata">

**Author:** ![pierrelgol](https://ziggit.dev/user_avatar/ziggit.dev/pierrelgol/32/10233_2.png) [@pierrelgol](https://ziggit.dev/u/pierrelgol)\
**Post date:** [October 23, 2024, 7:33am UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/5 "2024-10-23T07:33:26Z")

</div>

Mayvbe you can use the orelse to provide a path yourself (a fake one) and do a little strcmp on it and if it match your default string, you just print the message, indicating the information and don’t fail the step ? or maybe you can add two time the same step one dummy one that always succeed but do nothing just to provide the message, and one that actually do the thing ?

```zig
    const mandatory_path = b.option([]const u8, "mandatory_path", "mandatory path required by <step>") orelse blk: {
        std.log.info("the path is required to build the <step>. Please provide one.", .{});
        break :blk "Private/path/that/doesn't/exist";
    };

    _ = b.step("dummy_step_for_message", "if you want to use my step provide the option -Dmandatory_path=<your/path/to/the/thing>");

    const option = b.addOptions();
    option.addOption([]const u8, "mandatory_path", mandatory_path);

    const exe = b.addExecutable(.{
        .name = "temp",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.root_module.addOptions("option", option);
    b.installArtifact(exe);

```

so running it does something like that 🙂

```zig
❯ zb -h
info: the path is required to build the <step>. Please provide one.
Usage: /home/pollivie/zig/0.14.0-dev.1952+9f84f7f92/files/zig build [steps] [options]

Steps:
  install (default) Copy build artifacts to prefix path
  uninstall Remove build artifacts from prefix path
  dummy_step_for_message if you want to use my step provide the option -Dmandatory_path=<your/path/to/the/thing>
  run Run the app
  test Run unit tests

```

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [October 23, 2024, 12:55pm UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/6 "2024-10-23T12:55:20Z")

</div>

There’s actually an example of exactly what you’re looking for in Zig’s `build.zig`:

> <https://github.com/ziglang/zig/blob/2d888a8e639856e8cb6e4c6f9e6a27647b464952/build.zig#L561-L576>

(and another [here](https://github.com/ziglang/zig/blob/2d888a8e639856e8cb6e4c6f9e6a27647b464952/test/tests.zig#L1509-L1513))

---

<div class="post-metadata">

**Author:** ![mocompute](https://ziggit.dev/user_avatar/ziggit.dev/mocompute/32/3226_2.png) [@mocompute](https://ziggit.dev/u/mocompute)\
**Post date:** [October 23, 2024, 8:45pm UTC](https://ziggit.dev/t/zig-build-required-option-for-a-particular-step/6472/7 "2024-10-23T20:45:41Z")

</div>

Excellent, addFail seems to be the thing. Thank you both.
