Require specific zig version in build script

Is it possible to require a specific version of zig in my build script?

Here is the concrete use case:

In industrial controls, a project is finished and shipped to the customer, not touched for a long time, and then small changes are required, and the cycle repeats. It would perhaps be beneficial to enforce the version of the compiler used as part of the build system so I would not have to remember to read it from the README. The best documentation is code right :wink:

Aha itā€™s in builtin

From Build System Tricks :

3 Likes

Exact solution:

comptime {
    const required_zig_version = "0.13.0";
    const zig_version = @import("builtin").zig_version_string;
    if (!std.mem.eql(u8, required_zig_version, zig_version)) @compileError("This project requires zig version: " ++ required_zig_version ++ ", your zig version is: " ++ zig_version);
}
3 Likes

I want to note that itā€™s also possible to provide build-specific steps based on the Zig version, and even gate logic within the package based on it, since builtin is available there as well.

Iā€™ve found that pretty useful for minor differences between 0.13 and the master branch, although if the Type enum tag is involved, Iā€™ve just given up and picked a lane. It wouldnā€™t be impossible even there, but the amount of comptime magic involved gets pretty gnarly.

With 0.14 around the corner, you might find that useful or even essential for supporting the longer deploy cycle in embedded contexts, while handling the fact that Zig tends to move quickly, and most users are going to update to the next release version accordingly.

4 Likes

Slightly off-topic, but Iā€™ll just mention that shimming @typeInfo and std.builtin.Type isnā€™t that crazy difficult. @InKryption suggested the following to me and it has let me painlessly support both 0.13.0 and 0.14.0-dev just by replacing uses of @typeInfo with shims.typeInfo:

(Itā€™s missing @Type because my project didnā€™t need it, but would more or less just be the inverse of the switch in the @typeInfo shim.)

3 Likes