Zig Build Translate-C Step Output Path?

I am trying to do two things, get the output filename, and if possible have a build step thats just points to a zig function.

    // Add Translate-C Process
    var idf_sys = b.addTranslateC(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = .{
            .cwd_relative = "station_example_main.c",
        },
    });

    idf_sys.out_basename = "idf-out.zig";

It ends out as a different name in under zig-cache:
./.zig-cache/o/8e6cf1a8dbfca50b9546ca416e05382f/station_example_main.zig

I’ve added to my build lib step:
lib.step.dependOn(&idf_sys.step);

However even then I am not sure how to get the generate output and work on it from there. If I try to grab the output name i.e
std.debug.print("{s}\n", .{idf_sys.getOutput().generated.file.getPath()});

I’ll end up with the error:

thread 165328 panic: getPath() was called on a GeneratedFile that wasn't built yet. Is there a missing Step dependency on step 'translate-c'?
/home/joseph/Apps/zig-linux-x86_64-0.14.0-dev.3046+08d661fcf/lib/std/Build.zig:2444:75: 0x1413a6a in getPath (build)
        return gen.step.owner.pathFromRoot(gen.path orelse std.debug.panic(
                                                                          ^

So I tried to just do a move file command:

    const cli = &[_][]const u8{
        "mv",
        ".zig-cache/**/*/station_example_main.zig",
        "out.zig",
        // "sed -i '/pub const wifi_sta_config_t = opaque {};/d' out.zig",
    };
    var idf_sys_patch = b.addSystemCommand(cli);

    idf_sys_patch.step.dependOn(&idf_sys.step);

    lib.step.dependOn(&idf_sys_patch.step);

But it seems like the glob pattern isn’t working? It works in the terminal. I’d also just rather get the resulting path anyways than a hacky solution. It would be great if its possible to just pipe the translate-c to a Zig function as a step and I work inside that function instead.

In general, Zig build steps “return” LazyPaths. A LazyPath is an indeterminate location somewhere in the zig cache directory.

To get things out of zig cache and into zig-out, you need to “install” them, like this:

    b.addInstallFile(
        b.addTranslateC().getOutput(),
        "output.zig"
    );
1 Like

I think you could do something similar to this: Zig Build System ⚡ Zig Programming Language

Write a Zig program that accepts an input and output file, loads the input file patches it and writes it to the output file.
Then use addRunArtifact:

const tool_step = b.addRunArtifact(tool);
tool_step.addArg("--input-file");
tool_step.addFileArg(b.addTranslateC(.{...}).getOutput());
tool_step.addArg("--output-file");
const output = tool_step.addOutputFileArg("patched.zig");

exe.root_module.addAnonymousImport("patched", .{
    .root_source_file = output,
});

This solution didn’t work because globs are a part of bash or whatever shell you are using. Zig in this case is just executing the mv command with the literal string .zig-cache/**/*/station_example_main.zig as an argument, which is not what you want.

You could get this to work by doing "bash", "-c", "mv .zig-cache/**/*/station_example_main.zig out.zig", but as you’ve said, this isn’t really the solution you want.