#How can I build build.zig manually in order to customize the build options of build.zig itself?

1 messages Β· Page 1 of 1 (latest)

sullen quiver
#

I am using zigs build system as the second compilation step in a programming language called Acton. The actonc compiler generates C code which is then compiled by zig (cc). All Acton projects have the same structure so we can use the same build.zig. zig build actually takes some time to build build.zig into build, often several seconds. When actonc is compiling a single .act file, there is no persistent zig-cache, so taking a 5 second hit isn't acceptable. I put together a solution now where I prebuild build.zig by just building it once, grabbing the output build file in zig-cache. I then directly invoke this program the same way zig build does, providing build dir, caches etc from actonc.

My issue is that zig build builds build.zig with optimizations for the native CPU features and that means it's not very portable. The way I prebuild it, and place it in the Acton distribution which can be installed on other computers, it will SIGILL when run on a computer with older CPU than it was built on.

I skimmed cpuBuild in main.zig and noticed I can't modify the cpu stuff. I wonder if I can manually invoke zig build-exe perhaps with some magic arguments or so to build build.zig? That way I could specify -Dcpu=x86_64 for example to use base feature set, right.

spiral coral
#

We did something similar at TigerBeetle, but for a different reason; We use Zig 0.9.1 and it runs into a bad assert if you build anything with the "os" portion of the target beingnative on macOS Ventura. Since zig build doesn't let you customize the flags for build.zig itself, this assert would always trip for it. This was fixed after 0.10.1, but there's no pinned version for that yet (0.11.0 is close to being released) so we havent upgraded.

To address this, we copied the build_runner.zig (this is the exe that calls fn build() in build.zig) from the repo and build it with the correct target flag. Then, we used that as a substitute to all zig build commands. See here: https://github.com/tigerbeetledb/tigerbeetle/blob/main/scripts/build.sh#L32-L37. This build_runner is from 0.9.1. so it you might need the latest one (with maybe different cli args used) instead.

sullen quiver
#

King! Neato, I will have a look at your repo, thanks! πŸ™‚

sullen quiver
#

So you run the whole thing there in one go with zig run. I will run the binary later. I presume zig run really just consists of a build-exe and then execing the output or so, which means I can just stick to doing zig build-exe. The real trick there is that I need to build build_runner.zig, which in turn imports my build.zig - I just didn't know how this worked under the hood (despite skimming cmdBuild - didn't pay enough attention I suppose).

You've copied build_runner.zig into your own repo and modified at least the initial import from:

const root = @import("@build");

to:

const root = @import("../build.zig");

Is that the only change you've done? (git history doesn't really tell and I haven't diffed against the zig 0.9.something build_runner.zig)... I mean, do you have to copy build_runner.zig, can't you just use it in place and specify it to zig run ?

Maybe the answer is no πŸ˜‰ I attempted as much and it is indeed failing, not being able to locate build.zig.

kll@Boxy:~/terastream/acton/builder$ ../dist/zig/zig build-exe ../dist/zig/lib/build_runner.zig
/home/kll/terastream/acton/dist/zig/lib/build_runner.zig:1:22: error: no package named '@build' available within package 'root'
const root = @import("@build");
                     ^~~~~~~~
referenced by:
    main: /home/kll/terastream/acton/dist/zig/lib/build_runner.zig:293:30
    callMain: /home/kll/terastream/acton/dist/zig/lib/std/start.zig:609:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I thought I could just fix that with some other args to zig, like --mod or so? I can't get it to work though, but I have also never fiddled with these things before, I have only been using zig as a C compiler, not as a zig compiler so I'm completely lost πŸ˜‰

#

I can at least get it working by copying build_runner.zig and modifying that import, so making progress here πŸ™‚

spiral coral
sullen quiver
#

BTW, I'm @plajjan on twitter, we interacted befofre hi

#

I'm not sure what to do with --deps but I'll try πŸ™‚