# Using Text From a System Command as part of a Path During Build

**URL:** <https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594>\
**Category:** Help\
**Tags:** build-system\
**Created:** [June 5, 2024, 11:34pm UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594 "2024-06-05T23:34:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![haydenridd](https://ziggit.dev/user_avatar/ziggit.dev/haydenridd/32/2375_2.png) [@haydenridd](https://ziggit.dev/u/haydenridd)\
**Post date:** [June 5, 2024, 11:34pm UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/1 "2024-06-05T23:34:09Z")

</div>

Just encountered something I haven’t done yet with Zig’s build system, but have with others (Cmake/Meson/Makefiles). I’m trying to get the output of a system command, and then use said output to determine a path to include in my build. For example I know how to generate a _file_ at install time that contains this output:

```Zig
// Getting the output of some system command
const get_a_string_cmd = b.addSystemCommand(&.{ "echo", "some/partial/path" });
const get_a_string_stdout = get_a_string_cmd.captureStdOut();
const get_a_string_install_file = b.addInstallFile(get_a_string_stdout, "output.txt");
b.default_step.dependOn(&get_a_string_install_file.step);

```

This generates a file `zig-out/output.txt` as expected, the trick now is I want to _use_ that string value in an include path. For instance, I want to do something to the effect of:

```Zig
const my_command_output = "some/partial/path"; // This needs to come from system command instead!
exe.addLibraryPath(.{ .path = b.fmt("/home/whatever/something/{s}", .{my_command_output}) });

```

Once figured out I’ll add to the build system tricks and tips post!

As an aside, are there any issues open to make this kind of thing easier? It seems like there’s a LOT of hoops to jump through to get behavior that can be achieved with a single line in a Makefile:

```Makefile
VARIABLE = $(shell echo helloworld 2>&1)

```

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [June 6, 2024, 12:21am UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/2 "2024-06-06T00:21:42Z")

</div>

In other words, you need to run a system command _during the **configure phase**._

This is what [b.run](https://ziglang.org/documentation/0.12.0/std/#std.Build.run) is for.

Note that this is generally discouraged as it adds a dependency on the system to your build script, making it less portable, and more of a pain in the ass to set up a dev environment.

---

<div class="post-metadata">

**Author:** ![haydenridd](https://ziggit.dev/user_avatar/ziggit.dev/haydenridd/32/2375_2.png) [@haydenridd](https://ziggit.dev/u/haydenridd)\
**Post date:** [June 6, 2024, 1:54am UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/3 "2024-06-06T01:54:58Z")

</div>

Much appreciated, this is perfect!

> Note that this is generally discouraged as it adds a dependency on the system to of your build script, making it less portable, and more of a pain in the ass to set up a dev environment.

Completely agreed, the context for needing this ugliness for anyone interested is explained in this Makefile:

> <https://github.com/haydenridd/stm32-zig-porting-guide/blob/main/DropinZigccMakefile>

Basically I need to use the `arm-none-eabi-gcc` compiler to tell me where the processor specific pre-compiled libc libraries are based on the `-mcpu`, etc arguments so I can manually link them in to my executable. In this case, the dev environment _absolutely must_ have an installation of the `arm-none-eabi-gcc` compiler somewhere.

---

<div class="post-metadata">

**Author:** ![marler8997](https://ziggit.dev/user_avatar/ziggit.dev/marler8997/32/77_2.png) [@marler8997](https://ziggit.dev/u/marler8997)\
**Post date:** [June 7, 2024, 11:12pm UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/4 "2024-06-07T23:12:56Z")

</div>

I wonder if you could make your own wrapper around the `Compile` step that adds extra include paths at build time instead of configure time? The only thing I’m not sure of is if there are other parts of configuration that rely on knowing all the include paths, I have a feeling this isn’t the case so this should be possible.

You just create your own `Compile` struct with a `std.Build.Step.Compile` step inside of it, and inside your make function you add any extra include paths that are missing (might need some synchronization though since I don’t think `Compile` is meant to be thread-safe).

---

<div class="post-metadata">

**Author:** ![haydenridd](https://ziggit.dev/user_avatar/ziggit.dev/haydenridd/32/2375_2.png) [@haydenridd](https://ziggit.dev/u/haydenridd)\
**Post date:** [June 9, 2024, 12:40am UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/5 "2024-06-09T00:40:53Z")

</div>

Interesting! Any good examples or notes you could point me towards on differentiating what is executed at “configure” vs “build” time with Zigs build system? With Cmake its relatively easy to figure out through docs and because you actually have to run a “configure” and then “build” command separately.

I guess I’m confused on the distinction with Zigs build system and why you would potentially want this step (finding paths from a system command) in one versus the other.

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [June 9, 2024, 1:20am UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/6 "2024-06-09T01:20:06Z")

</div>

The “configure” phase is during the execution of your `pub fn build(b: *std.Build) void { ... }` function. You could, for example, put `defer std.debug.print("end of configure phase; start of make phase\n", .{});` at the top.

The “make” phase is what happens after that, and is implemented in `lib/compiler/build_runner.zig`.

It is planned for these phases to run in separate processes in the future. [Run build.zig logic in a WebAssembly sandbox · Issue #14286 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/14286)

---

<div class="post-metadata">

**Author:** ![haydenridd](https://ziggit.dev/user_avatar/ziggit.dev/haydenridd/32/2375_2.png) [@haydenridd](https://ziggit.dev/u/haydenridd)\
**Post date:** [June 9, 2024, 3:45am UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/7 "2024-06-09T03:45:17Z")

</div>

Ahh awesome thank you! Makes sense, you’re setting up the structure of what “will” run in the make phase during the configure phase in `build()`.

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [June 9, 2024, 8:59pm UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/8 "2024-06-09T20:59:28Z")

</div>

yeah, in retrospect, I should have called that function “configure” rather than “build”.

---

<div class="post-metadata">

**Author:** ![marler8997](https://ziggit.dev/user_avatar/ziggit.dev/marler8997/32/77_2.png) [@marler8997](https://ziggit.dev/u/marler8997)\
**Post date:** [June 9, 2024, 11:31pm UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/9 "2024-06-09T23:31:06Z")

</div>

Oooo I like that 🙂 Not too late to change. You could easily support both for a smooth transition.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [June 10, 2024, 12:22am UTC](https://ziggit.dev/t/using-text-from-a-system-command-as-part-of-a-path-during-build/4594/10 "2024-06-10T00:22:53Z")

</div>

It makes the signature read more like a sentence too:

```zig
pub fn configure(b: *std.Build) void {

```

So we _configure the build_.
