Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fs = std.fs;
const InstallDirectoryOptions = std.Build.InstallDirectoryOptions;
const assert = std.debug.assert;

const zig_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 };
const zig_version = std.SemanticVersion{ .major = 0, .minor = 11, .patch = 0 };
const stack_size = 32 * 1024 * 1024;

pub fn build(b: *std.Build) !void {
Expand Down Expand Up @@ -242,7 +242,7 @@ pub fn build(b: *std.Build) !void {
const commit_height = it.next().?;
const commit_id = it.next().?;

const ancestor_ver = try std.builtin.Version.parse(tagged_ancestor);
const ancestor_ver = try std.SemanticVersion.parse(tagged_ancestor);
if (zig_version.order(ancestor_ver) != .gt) {
std.debug.print("Zig version '{}' must be greater than tagged ancestor '{}'\n", .{ zig_version, ancestor_ver });
std.process.exit(1);
Expand Down
8 changes: 4 additions & 4 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ pub fn addOptions(self: *Build) *Step.Options {
pub const ExecutableOptions = struct {
name: []const u8,
root_source_file: ?FileSource = null,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
target: CrossTarget = .{},
optimize: std.builtin.Mode = .Debug,
linkage: ?Step.Compile.Linkage = null,
Expand Down Expand Up @@ -530,7 +530,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
pub const SharedLibraryOptions = struct {
name: []const u8,
root_source_file: ?FileSource = null,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
target: CrossTarget,
optimize: std.builtin.Mode,
max_rss: usize = 0,
Expand Down Expand Up @@ -562,7 +562,7 @@ pub const StaticLibraryOptions = struct {
root_source_file: ?FileSource = null,
target: CrossTarget,
optimize: std.builtin.Mode,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
max_rss: usize = 0,
link_libc: ?bool = null,
single_threaded: ?bool = null,
Expand Down Expand Up @@ -592,7 +592,7 @@ pub const TestOptions = struct {
root_source_file: FileSource,
target: CrossTarget = .{},
optimize: std.builtin.Mode = .Debug,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
max_rss: usize = 0,
filter: ?[]const u8 = null,
test_runner: ?[]const u8 = null,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub const HashHelper = struct {
/// Convert the input value into bytes and record it as a dependency of the process being cached.
pub fn add(hh: *HashHelper, x: anytype) void {
switch (@TypeOf(x)) {
std.builtin.Version => {
std.SemanticVersion => {
hh.add(x.major);
hh.add(x.minor);
hh.add(x.patch);
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ linker_script: ?FileSource = null,
version_script: ?[]const u8 = null,
out_filename: []const u8,
linkage: ?Linkage = null,
version: ?std.builtin.Version,
version: ?std.SemanticVersion,
kind: Kind,
major_only_filename: ?[]const u8,
name_only_filename: ?[]const u8,
Expand Down Expand Up @@ -278,7 +278,7 @@ pub const Options = struct {
optimize: std.builtin.Mode,
kind: Kind,
linkage: ?Linkage = null,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
max_rss: usize = 0,
filter: ?[]const u8 = null,
test_runner: ?[]const u8 = null,
Expand Down
23 changes: 0 additions & 23 deletions lib/std/Build/Step/Options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ fn addOptionFallible(self: *Options, comptime T: type, name: []const u8, value:
}
return;
},
std.builtin.Version => {
try out.print(
\\pub const {}: @import("std").builtin.Version = .{{
\\ .major = {d},
\\ .minor = {d},
\\ .patch = {d},
\\}};
\\
, .{
std.zig.fmtId(name),

value.major,
value.minor,
value.patch,
});
return;
},
std.SemanticVersion => {
try out.print(
\\pub const {}: @import("std").SemanticVersion = .{{
Expand Down Expand Up @@ -367,7 +350,6 @@ test Options {
options.addOption([2][2]u16, "nested_array", nested_array);
options.addOption([]const []const u16, "nested_slice", nested_slice);
//options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1");
options.addOption(std.builtin.Version, "version", try std.builtin.Version.parse("0.1.2"));
options.addOption(std.SemanticVersion, "semantic_version", try std.SemanticVersion.parse("0.1.2-foo+bar"));

try std.testing.expectEqualStrings(
Expand Down Expand Up @@ -401,11 +383,6 @@ test Options {
//\\ @"0.8.1",
//\\};
//\\pub const keyword_enum: KeywordEnum = KeywordEnum.@"0.8.1";
\\pub const version: @import("std").builtin.Version = .{
\\ .major = 0,
\\ .minor = 1,
\\ .patch = 2,
\\};
\\pub const semantic_version: @import("std").SemanticVersion = .{
\\ .major = 0,
\\ .minor = 1,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/TranslateC.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {

pub const AddExecutableOptions = struct {
name: ?[]const u8 = null,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
target: ?CrossTarget = null,
optimize: ?std.builtin.Mode = null,
linkage: ?Step.Compile.Linkage = null,
Expand Down
24 changes: 22 additions & 2 deletions lib/std/SemanticVersion.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! A software version formatted according to the Semantic Version 2 specification.
//! A software version formatted according to the Semantic Versioning 2.0.0 specification.
//!
//! See: https://semver.org

Expand Down Expand Up @@ -167,7 +167,7 @@ const expect = std.testing.expect;
const expectError = std.testing.expectError;

test "SemanticVersion format" {
// Test vectors are from https://github.com/semver/semver.org/issues/59#issuecomment-390854010.
// Many of these test strings are from https://github.com/semver/semver.org/issues/59#issuecomment-390854010.

// Valid version strings should be accepted.
for ([_][]const u8{
Expand Down Expand Up @@ -200,6 +200,8 @@ test "SemanticVersion format" {
"1.2.3----R-S.12.9.1--.12+meta",
"1.2.3----RC-SNAPSHOT.12.9.1--.12",
"1.0.0+0.build.1-rc.10000aaa-kk-0.1",
"5.4.0-1018-raspi",
"5.7.123",
}) |valid| try std.testing.expectFmt(valid, "{}", .{try parse(valid)});

// Invalid version strings should be rejected.
Expand Down Expand Up @@ -244,6 +246,24 @@ test "SemanticVersion format" {
"+justmeta",
"9.8.7+meta+meta",
"9.8.7-whatever+meta+meta",
"2.6.32.11-svn21605",
"2.11.2(0.329/5/3)",
"2.13-DEVELOPMENT",
"2.3-35",
"1a.4",
"3.b1.0",
"1.4beta",
"2.7.pre",
"0..3",
"8.008.",
"01...",
"55",
"foobar",
"",
"-1",
"+4",
".",
"....3",
}) |invalid| try expectError(error.InvalidVersion, parse(invalid));

// Valid version string that may overflow.
Expand Down
133 changes: 0 additions & 133 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -486,139 +486,6 @@ pub const WasiExecModel = enum {
reactor,
};

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Version = struct {
major: u32,
minor: u32,
patch: u32 = 0,

pub const Range = struct {
min: Version,
max: Version,

pub fn includesVersion(self: Range, ver: Version) bool {
if (self.min.order(ver) == .gt) return false;
if (self.max.order(ver) == .lt) return false;
return true;
}

/// Checks if system is guaranteed to be at least `version` or older than `version`.
/// Returns `null` if a runtime check is required.
pub fn isAtLeast(self: Range, ver: Version) ?bool {
if (self.min.order(ver) != .lt) return true;
if (self.max.order(ver) == .lt) return false;
return null;
}
};

pub fn order(lhs: Version, rhs: Version) std.math.Order {
if (lhs.major < rhs.major) return .lt;
if (lhs.major > rhs.major) return .gt;
if (lhs.minor < rhs.minor) return .lt;
if (lhs.minor > rhs.minor) return .gt;
if (lhs.patch < rhs.patch) return .lt;
if (lhs.patch > rhs.patch) return .gt;
return .eq;
}

pub fn parse(text: []const u8) !Version {
var end: usize = 0;
while (end < text.len) : (end += 1) {
const c = text[end];
if (!std.ascii.isDigit(c) and c != '.') break;
}
// found no digits or '.' before unexpected character
if (end == 0) return error.InvalidVersion;

var it = std.mem.splitScalar(u8, text[0..end], '.');
// substring is not empty, first call will succeed
const major = it.first();
if (major.len == 0) return error.InvalidVersion;
const minor = it.next() orelse "0";
// ignore 'patch' if 'minor' is invalid
const patch = if (minor.len == 0) "0" else (it.next() orelse "0");

return Version{
.major = try std.fmt.parseUnsigned(u32, major, 10),
.minor = try std.fmt.parseUnsigned(u32, if (minor.len == 0) "0" else minor, 10),
.patch = try std.fmt.parseUnsigned(u32, if (patch.len == 0) "0" else patch, 10),
};
}

pub fn format(
self: Version,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
out_stream: anytype,
) !void {
_ = options;
if (fmt.len == 0) {
if (self.patch == 0) {
if (self.minor == 0) {
return std.fmt.format(out_stream, "{d}", .{self.major});
} else {
return std.fmt.format(out_stream, "{d}.{d}", .{ self.major, self.minor });
}
} else {
return std.fmt.format(out_stream, "{d}.{d}.{d}", .{ self.major, self.minor, self.patch });
}
} else {
std.fmt.invalidFmtError(fmt, self);
}
}
};

test "Version.parse" {
@setEvalBranchQuota(3000);
try testVersionParse();
comptime (try testVersionParse());
}

fn testVersionParse() !void {
const f = struct {
fn eql(text: []const u8, v1: u32, v2: u32, v3: u32) !void {
const v = try Version.parse(text);
try std.testing.expect(v.major == v1 and v.minor == v2 and v.patch == v3);
}

fn err(text: []const u8, expected_err: anyerror) !void {
_ = Version.parse(text) catch |actual_err| {
if (actual_err == expected_err) return;
return actual_err;
};
return error.Unreachable;
}
};

try f.eql("2.6.32.11-svn21605", 2, 6, 32); // Debian PPC
try f.eql("2.11.2(0.329/5/3)", 2, 11, 2); // MinGW
try f.eql("5.4.0-1018-raspi", 5, 4, 0); // Ubuntu
try f.eql("5.7.12_3", 5, 7, 12); // Void
try f.eql("2.13-DEVELOPMENT", 2, 13, 0); // DragonFly
try f.eql("2.3-35", 2, 3, 0);
try f.eql("1a.4", 1, 0, 0);
try f.eql("3.b1.0", 3, 0, 0);
try f.eql("1.4beta", 1, 4, 0);
try f.eql("2.7.pre", 2, 7, 0);
try f.eql("0..3", 0, 0, 0);
try f.eql("8.008.", 8, 8, 0);
try f.eql("01...", 1, 0, 0);
try f.eql("55", 55, 0, 0);
try f.eql("4294967295.0.1", 4294967295, 0, 1);
try f.eql("429496729_6", 429496729, 0, 0);

try f.err("foobar", error.InvalidVersion);
try f.err("", error.InvalidVersion);
try f.err("-1", error.InvalidVersion);
try f.err("+4", error.InvalidVersion);
try f.err(".", error.InvalidVersion);
try f.err("....3", error.InvalidVersion);
try f.err("4294967296", error.Overflow);
try f.err("5000877755", error.Overflow);
// error.InvalidCharacter is not possible anymore
}

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const CallModifier = enum {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const Tokenizer = tokenizer.Tokenizer;
/// If linking gnu libc (glibc), the `ok` value will be true if the target
/// version is greater than or equal to `glibc_version`.
/// If linking a libc other than these, returns `false`.
pub fn versionCheck(comptime glibc_version: std.builtin.Version) type {
pub fn versionCheck(comptime glibc_version: std.SemanticVersion) type {
return struct {
pub const ok = blk: {
if (!builtin.link_libc) break :blk false;
Expand Down
1 change: 1 addition & 0 deletions lib/std/crypto/tlcsprng.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const want_fork_safety = os_has_fork and !os_has_arc4random and
const maybe_have_wipe_on_fork = builtin.os.isAtLeast(.linux, .{
.major = 4,
.minor = 14,
.patch = 0,
}) orelse true;
const is_haiku = builtin.os.tag == .haiku;

Expand Down
Loading