#*.zig transofmration Build step?

1 messages · Page 1 of 1 (latest)

trim shoal
#

I'd like to have a build.zig step which takes in a *.zig file, does a transoformation on it, and outputs the resulting *.zig file. I've thought of a few ways of doing this, I'm wondering if there's a better way that I'm missing.
(Background: What I'm trying to do is run a translate-c step on some *.h files, then run a transformation on the output because I know some of the input headers contained structs with bitfields on them. The transformation would replace these headers with zig implementations rather than opaque {}. After the transformation, I'll include the resulting *.zig as a module for the final product.)
Here's what I've thought of:

  1. addSystemCommand(). I could use awk or sed to replace the opaque definitions with zig structs. This is problematic for 2 reasons: one is that using text transformations is just fragile in general, and two is that I can't seem to get it to work. For example, I attempted to run the following sed command through addSystemCommand, and it would always complain about some syntax failure, even though the command works perfectly for my purpose when run from the terminal.
    Example command I'd try to run (to no avail):
const fixup_module_step = b.addSystemCommand(&.{
    "sed",
    "-e",
    "'pub const struct_contains_bitfields/{r c_import/header.zig' -e 'd}'",
});
fixup_module_step.addFileArg(translate_c_path);
const fixup_module_path = fixup_module_step.captureStdOut();

(continued in comment)

#
  1. A custom step. I could write a custom step which takes the translate_c_path LazyPath, does the transformation in zig, and outputs it. This could be done via text parsing, or perhaps utilizing some of the standard library's parsing capabilities. This could be problematic for 2 reasons: one is that I've read here that custom build steps might be going away, so I don't want to rely on a technique which won't be forward compatible, and two is that if I'm not taking advantage of the build system normally, I'm not sure if it could cause problems for things like caching.

My question is: Is there a "recommended" way to do this, or any canonical examples? Thank you!