From 45713ec8ab21f81291bc14e2725730dac26d5137 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 3 Apr 2023 11:06:39 +0200 Subject: build: restore support for Zig 0.6.0 The version check for Zig 0.6.0 was incorrect since commit 971ab7f (Use a zig build script to run ziglings). Move compatibility support to a separate file, in order to simplify build.zig. In case of incompatible version, exit with code 3 instead of 0, in order to detect the case of failure in a test (to be implemented). Remove the use of comptime when checking compatibility at the start of the build function, since it is not necessary. Closes #210. --- src/compat.zig | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/compat.zig (limited to 'src/compat.zig') diff --git a/src/compat.zig b/src/compat.zig new file mode 100644 index 0000000..34dfc76 --- /dev/null +++ b/src/compat.zig @@ -0,0 +1,65 @@ +/// Compatibility support for very old versions of Zig and recent versions before +/// commit efa25e7d5 (Merge pull request #14498 from ziglang/zig-build-api). +/// +/// Versions of Zig from before 0.6.0 cannot do the version check and will just +/// fail to compile, but 0.5.0 was a long time ago, it is unlikely that anyone +/// who attempts these exercises is still using it. +const std = @import("std"); +const builtin = @import("builtin"); + +const debug = std.debug; + +// Very old versions of Zig used warn instead of print. +const print = if (@hasDecl(debug, "print")) debug.print else debug.warn; + +// When changing this version, be sure to also update README.md in two places: +// 1) Getting Started +// 2) Version Changes +const needed_version_str = "0.11.0-dev.2157"; + +fn isCompatible() bool { + if (!@hasDecl(builtin, "zig_version") or !@hasDecl(std, "SemanticVersion")) { + return false; + } + + const needed_version = std.SemanticVersion.parse(needed_version_str) catch unreachable; + const version = builtin.zig_version; + const order = version.order(needed_version); + + return order != .lt; +} + +pub fn die() noreturn { + const error_message = + \\ERROR: Sorry, it looks like your version of zig is too old. :-( + \\ + \\Ziglings requires development build + \\ + \\ {s} + \\ + \\or higher. Please download a development ("master") build from + \\ + \\ https://ziglang.org/download/ + \\ + \\ + ; + + print(error_message, .{needed_version_str}); + + // Use exit code 2, to differentiate from a normal Zig compiler error. + std.os.exit(2); +} + +// A separate function is required because very old versions of Zig doesn't +// support labeled block expressions. +pub const is_compatible: bool = isCompatible(); + +/// This is the type to be used only for the build function definition, since +/// the type must be compatible with the build runner. +/// +/// Don't use std.Build.Builder, since it is deprecated and may be removed in +/// future. +pub const Build = if (is_compatible) std.Build else std.build.Builder; + +/// This is the type to be used for accessing the build namespace. +pub const build = if (is_compatible) std.Build else std.build; -- cgit v1.2.3-70-g09d2 From ddc835762c1276c781295164a2bb3e42b35670df Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Fri, 7 Apr 2023 18:18:15 +0200 Subject: Insert current zig version for compatiblity! This is primarily to make users aware that there has been a change in the call for individual exercises. --- src/compat.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/compat.zig') diff --git a/src/compat.zig b/src/compat.zig index 34dfc76..1adf8c0 100644 --- a/src/compat.zig +++ b/src/compat.zig @@ -15,7 +15,7 @@ const print = if (@hasDecl(debug, "print")) debug.print else debug.warn; // When changing this version, be sure to also update README.md in two places: // 1) Getting Started // 2) Version Changes -const needed_version_str = "0.11.0-dev.2157"; +const needed_version_str = "0.11.0-dev.2401"; fn isCompatible() bool { if (!@hasDecl(builtin, "zig_version") or !@hasDecl(std, "SemanticVersion")) { -- cgit v1.2.3-70-g09d2