compat.zig 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /// Compatibility support for very old versions of Zig and recent versions before
  2. /// commit efa25e7d5 (Merge pull request #14498 from ziglang/zig-build-api).
  3. ///
  4. /// Versions of Zig from before 0.6.0 cannot do the version check and will just
  5. /// fail to compile, but 0.5.0 was a long time ago, it is unlikely that anyone
  6. /// who attempts these exercises is still using it.
  7. const std = @import("std");
  8. const builtin = @import("builtin");
  9. const debug = std.debug;
  10. // Very old versions of Zig used warn instead of print.
  11. const print = if (@hasDecl(debug, "print")) debug.print else debug.warn;
  12. // When changing this version, be sure to also update README.md in two places:
  13. // 1) Getting Started
  14. // 2) Version Changes
  15. const needed_version_str = "0.11.0-dev.4246";
  16. fn isCompatible() bool {
  17. if (!@hasDecl(builtin, "zig_version") or !@hasDecl(std, "SemanticVersion")) {
  18. return false;
  19. }
  20. const needed_version = std.SemanticVersion.parse(needed_version_str) catch unreachable;
  21. const version = builtin.zig_version;
  22. const order = version.order(needed_version);
  23. return order != .lt;
  24. }
  25. pub fn die() noreturn {
  26. const error_message =
  27. \\ERROR: Sorry, it looks like your version of zig is too old. :-(
  28. \\
  29. \\Ziglings requires development build
  30. \\
  31. \\ {s}
  32. \\
  33. \\or higher. Please download a development ("master") build from
  34. \\
  35. \\ https://ziglang.org/download/
  36. \\
  37. \\
  38. ;
  39. print(error_message, .{needed_version_str});
  40. // Use exit code 2, to differentiate from a normal Zig compiler error.
  41. std.os.exit(2);
  42. }
  43. // A separate function is required because very old versions of Zig doesn't
  44. // support labeled block expressions.
  45. pub const is_compatible: bool = isCompatible();
  46. /// This is the type to be used only for the build function definition, since
  47. /// the type must be compatible with the build runner.
  48. ///
  49. /// Don't use std.Build.Builder, since it is deprecated and may be removed in
  50. /// future.
  51. pub const Build = if (is_compatible) std.Build else std.build.Builder;
  52. /// This is the type to be used for accessing the build namespace.
  53. pub const build = if (is_compatible) std.Build else std.build;