From 3ab19421562e8c7d7d4394d5655088de5c52edd7 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Fri, 6 Oct 2023 00:33:24 +0200 Subject: [PATCH] changed compat check to comptime --- build.zig | 35 +++++++++++++++++++++++---- src/compat.zig | 65 -------------------------------------------------- 2 files changed, 30 insertions(+), 70 deletions(-) delete mode 100644 src/compat.zig diff --git a/build.zig b/build.zig index 1026058..013408b 100644 --- a/build.zig +++ b/build.zig @@ -1,17 +1,43 @@ const std = @import("std"); const builtin = @import("builtin"); -const compat = @import("src/compat.zig"); const tests = @import("test/tests.zig"); -const Build = compat.Build; -const CompileStep = compat.build.CompileStep; -const Step = compat.build.Step; +const Build = std.Build; +const CompileStep = Build.CompileStep; +const Step = Build.Step; const Child = std.process.Child; const assert = std.debug.assert; const join = std.fs.path.join; const print = std.debug.print; +// When changing this version, be sure to also update README.md in two places: +// 1) Getting Started +// 2) Version Changes +comptime { + const required_zig = "0.11.0-dev.4246"; + const current_zig = builtin.zig_version; + const min_zig = std.SemanticVersion.parse(required_zig) catch unreachable; + if (current_zig.order(min_zig) == .lt) { + const error_message = + \\Sorry, it looks like your version of zig is too old. :-( + \\ + \\Ziglings requires development build + \\ + \\{} + \\ + \\or higher. + \\ + \\Please download a development ("master") build from + \\ + \\https://ziglang.org/download/ + \\ + \\ + ; + @compileError(std.fmt.comptimePrint(error_message, .{min_zig})); + } +} + const Kind = enum { /// Run the artifact as a normal executable. exe, @@ -93,7 +119,6 @@ pub const logo = ; pub fn build(b: *Build) !void { - if (!compat.is_compatible) compat.die(); if (!validate_exercises()) std.os.exit(2); use_color_escapes = false; diff --git a/src/compat.zig b/src/compat.zig deleted file mode 100644 index 6e7d92e..0000000 --- a/src/compat.zig +++ /dev/null @@ -1,65 +0,0 @@ -/// 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.4246"; - -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;