Doing heap things in build.zig

I never really got into the possibilities of build.zig. I really should do that soon.
I know we can do lots of things in there.

So question…

I am thinking about creating a “tiny” version of my chess engine as a fun side project.
The executable size of an “official tiny engine” is 4096 bytes max.

My “big” chess engine has a lot of compile time computed constants (lots of arrays).
That is blowing up the executable size of course.

So the plan is to stuff all these constants in a struct on the heap.
Is it possible to fill this heap-struct inside build.zig, thus avoiding runtime code. And somehow let this code execute at compiletime?
(Runtime code of course also consumes executable space).

Any other crazy tips are also welcome of course.

you can add values, but no clue how far one can get.

for instance, we put a version tag from git to build as

    // add build options to runtime
    const options = b.addOptions();
    exe.root_module.addOptions("build", options);

    // build: version
    const args = &[_][]const u8{ "sh", "-c", "git describe --exact-match --tags HEAD 2>/dev/null || echo \"$(git rev-parse --abbrev-ref HEAD)-$(git rev-parse --short HEAD)\"" };
    const version = b.run(args);
    options.addOption([]const u8, "version", version);

and later use it in the app accessing as `@import(“build”).version`.

std itself is available in the build, don’t see a reason why std.heap is not

1 Like

I don’t think so? since the memory exists on the heap at runtime it must be set at runtime. it can either be set by copying bytes from rodata (i.e. potentially increasing the binary size), or by executing code that writes to that memory (i.e. potentially slowing down the execution of your program).