std: replace builtin.Version with SemanticVersion

This commit is contained in:
r00ster91 2023-02-21 18:39:22 +01:00 committed by Andrew Kelley
parent 96acc204b2
commit 6e84f46990
34 changed files with 189 additions and 280 deletions

View file

@ -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 {
@ -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);

View file

@ -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,
@ -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,
@ -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,
@ -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,

View file

@ -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);

View file

@ -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,
@ -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,

View file

@ -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 = .{{
@ -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(
@ -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,

View file

@ -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,

View file

@ -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
@ -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{
@ -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.
@ -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.

View file

@ -483,139 +483,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 {

View file

@ -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;

View file

@ -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;

View file

@ -488,7 +488,7 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
if (builtin.os.tag == .linux or builtin.os.tag == .freebsd) {
var buf = buffer;
const use_c = builtin.os.tag != .linux or
std.c.versionCheck(std.builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
std.c.versionCheck(std.SemanticVersion{ .major = 2, .minor = 25, .patch = 0 }).ok;
while (buf.len != 0) {
const res = if (use_c) blk: {
@ -5272,7 +5272,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
return target;
},
.freebsd => {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .gt) {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .gt) {
var kfile: system.kinfo_file = undefined;
kfile.structsize = system.KINFO_FILE_SIZE;
switch (errno(system.fcntl(fd, system.F.KINFO, @ptrToInt(&kfile)))) {
@ -5325,7 +5325,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
}
},
.dragonfly => {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 6, .minor = 0 }) == .lt) {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 6, .minor = 0, .patch = 0 }) == .lt) {
@compileError("querying for canonical path of a handle is unsupported on this host");
}
@memset(out_buffer[0..MAX_PATH_BYTES], 0);
@ -5339,7 +5339,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
return out_buffer[0..len];
},
.netbsd => {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 10, .minor = 0 }) == .lt) {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 10, .minor = 0, .patch = 0 }) == .lt) {
@compileError("querying for canonical path of a handle is unsupported on this host");
}
@memset(out_buffer[0..MAX_PATH_BYTES], 0);
@ -6152,9 +6152,9 @@ pub fn sendfile(
.linux => sf: {
// sendfile() first appeared in Linux 2.2, glibc 2.1.
const call_sf = comptime if (builtin.link_libc)
std.c.versionCheck(.{ .major = 2, .minor = 1 }).ok
std.c.versionCheck(.{ .major = 2, .minor = 1, .patch = 0 }).ok
else
builtin.os.version_range.linux.range.max.order(.{ .major = 2, .minor = 2 }) != .lt;
builtin.os.version_range.linux.range.max.order(.{ .major = 2, .minor = 2, .patch = 0 }) != .lt;
if (!call_sf) break :sf;
if (headers.len != 0) {
@ -6453,8 +6453,8 @@ var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true);
///
/// Maximum offsets on Linux and FreeBSD are `math.maxInt(i64)`.
pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize {
if ((comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0 }) orelse false) or
((comptime builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse false and
if ((comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0, .patch = 0 }) orelse false) or
((comptime builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5, .patch = 0 }) orelse false and
std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok) and
has_copy_file_range_syscall.load(.Monotonic)))
{
@ -6787,7 +6787,7 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t {
}
},
.freebsd => {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .lt)
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .lt)
@compileError("memfd_create is unavailable on FreeBSD < 13.0");
const rc = system.memfd_create(name, flags);
switch (errno(rc)) {

View file

@ -541,7 +541,7 @@ test "memfd_create" {
switch (native_os) {
.linux => {},
.freebsd => {
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .lt)
if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .lt)
return error.SkipZigTest;
},
else => return error.SkipZigTest,

View file

@ -1,7 +1,7 @@
const std = @import("std.zig");
const builtin = @import("builtin");
const mem = std.mem;
const Version = std.builtin.Version;
const Version = std.SemanticVersion;
/// TODO Nearly all the functions in this namespace would be
/// better off if https://github.com/ziglang/zig/issues/425
@ -272,75 +272,75 @@ pub const Target = struct {
.freebsd => return .{
.semver = Version.Range{
.min = .{ .major = 12, .minor = 0 },
.max = .{ .major = 13, .minor = 1 },
.min = .{ .major = 12, .minor = 0, .patch = 0 },
.max = .{ .major = 13, .minor = 1, .patch = 0 },
},
},
.macos => return switch (arch) {
.aarch64 => VersionRange{
.semver = .{
.min = .{ .major = 11, .minor = 7, .patch = 1 },
.max = .{ .major = 13, .minor = 3 },
.max = .{ .major = 13, .minor = 3, .patch = 0 },
},
},
.x86_64 => VersionRange{
.semver = .{
.min = .{ .major = 11, .minor = 7, .patch = 1 },
.max = .{ .major = 13, .minor = 3 },
.max = .{ .major = 13, .minor = 3, .patch = 0 },
},
},
else => unreachable,
},
.ios => return .{
.semver = .{
.min = .{ .major = 12, .minor = 0 },
.min = .{ .major = 12, .minor = 0, .patch = 0 },
.max = .{ .major = 13, .minor = 4, .patch = 0 },
},
},
.watchos => return .{
.semver = .{
.min = .{ .major = 6, .minor = 0 },
.min = .{ .major = 6, .minor = 0, .patch = 0 },
.max = .{ .major = 6, .minor = 2, .patch = 0 },
},
},
.tvos => return .{
.semver = .{
.min = .{ .major = 13, .minor = 0 },
.min = .{ .major = 13, .minor = 0, .patch = 0 },
.max = .{ .major = 13, .minor = 4, .patch = 0 },
},
},
.netbsd => return .{
.semver = .{
.min = .{ .major = 8, .minor = 0 },
.max = .{ .major = 10, .minor = 0 },
.min = .{ .major = 8, .minor = 0, .patch = 0 },
.max = .{ .major = 10, .minor = 0, .patch = 0 },
},
},
.openbsd => return .{
.semver = .{
.min = .{ .major = 6, .minor = 8 },
.max = .{ .major = 7, .minor = 2 },
.min = .{ .major = 6, .minor = 8, .patch = 0 },
.max = .{ .major = 7, .minor = 2, .patch = 0 },
},
},
.dragonfly => return .{
.semver = .{
.min = .{ .major = 5, .minor = 8 },
.max = .{ .major = 6, .minor = 4 },
.min = .{ .major = 5, .minor = 8, .patch = 0 },
.max = .{ .major = 6, .minor = 4, .patch = 0 },
},
},
.solaris => return .{
.semver = .{
.min = .{ .major = 5, .minor = 11 },
.max = .{ .major = 5, .minor = 11 },
.min = .{ .major = 5, .minor = 11, .patch = 0 },
.max = .{ .major = 5, .minor = 11, .patch = 0 },
},
},
.linux => return .{
.linux = .{
.range = .{
.min = .{ .major = 3, .minor = 16 },
.min = .{ .major = 3, .minor = 16, .patch = 0 },
.max = .{ .major = 5, .minor = 10, .patch = 81 },
},
.glibc = .{ .major = 2, .minor = 19 },
.glibc = .{ .major = 2, .minor = 19, .patch = 0 },
},
},

View file

@ -108,7 +108,7 @@ pub const BinNameOptions = struct {
target: std.Target,
output_mode: std.builtin.OutputMode,
link_mode: ?std.builtin.LinkMode = null,
version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
};
/// Returns the standard file system basename of a binary generated by the Zig compiler.

View file

@ -33,7 +33,7 @@ os_version_max: ?OsVersion = null,
/// `null` means default when cross compiling, or native when os_tag is native.
/// If `isGnuLibC()` is `false`, this must be `null` and is ignored.
glibc_version: ?SemVer = null,
glibc_version: ?SemanticVersion = null,
/// `null` means the native C ABI, if `os_tag` is native, otherwise it means the default C ABI.
abi: ?Target.Abi = null,
@ -61,11 +61,11 @@ pub const CpuModel = union(enum) {
pub const OsVersion = union(enum) {
none: void,
semver: SemVer,
semver: SemanticVersion,
windows: Target.Os.WindowsVersion,
};
pub const SemVer = std.builtin.Version;
pub const SemanticVersion = std.SemanticVersion;
pub const DynamicLinker = Target.DynamicLinker;
@ -266,9 +266,8 @@ pub fn parse(args: ParseOptions) !CrossTarget {
const abi_ver_text = abi_it.rest();
if (abi_it.next() != null) {
if (result.isGnuLibC()) {
result.glibc_version = SemVer.parse(abi_ver_text) catch |err| switch (err) {
result.glibc_version = parseVersion(abi_ver_text) catch |err| switch (err) {
error.Overflow => return error.InvalidAbiVersion,
error.InvalidCharacter => return error.InvalidAbiVersion,
error.InvalidVersion => return error.InvalidAbiVersion,
};
} else {
@ -353,6 +352,31 @@ pub fn parseCpuArch(args: ParseOptions) ?Target.Cpu.Arch {
}
}
/// Parses a version with an omitted patch component, such as "1.0",
/// which SemanticVersion.parse is not capable of.
fn parseVersion(ver: []const u8) !SemanticVersion {
const parseVersionComponent = struct {
fn parseVersionComponent(component: []const u8) !usize {
return std.fmt.parseUnsigned(usize, component, 10) catch |err| {
switch (err) {
error.InvalidCharacter => return error.InvalidVersion,
error.Overflow => return error.Overflow,
}
};
}
}.parseVersionComponent;
var version_components = mem.split(u8, ver, ".");
const major = version_components.first();
const minor = version_components.next() orelse return error.InvalidVersion;
const patch = version_components.next() orelse "0";
if (version_components.next() != null) return error.InvalidVersion;
return .{
.major = try parseVersionComponent(major),
.minor = try parseVersionComponent(minor),
.patch = try parseVersionComponent(patch),
};
}
/// TODO deprecated, use `std.zig.system.NativeTargetInfo.detect`.
pub fn getCpu(self: CrossTarget) Target.Cpu {
switch (self.cpu_model) {
@ -534,6 +558,16 @@ pub fn isNative(self: CrossTarget) bool {
return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();
}
/// Formats a version with the patch component omitted if it is zero,
/// unlike SemanticVersion.format which formats all its version components regardless.
fn formatVersion(version: SemanticVersion, writer: anytype) !void {
if (version.patch == 0) {
try writer.print("{d}.{d}", .{ version.major, version.minor });
} else {
try writer.print("{d}.{d}.{d}", .{ version.major, version.minor, version.patch });
}
}
pub fn zigTriple(self: CrossTarget, allocator: mem.Allocator) error{OutOfMemory}![]u8 {
if (self.isNative()) {
return allocator.dupe(u8, "native");
@ -552,20 +586,27 @@ pub fn zigTriple(self: CrossTarget, allocator: mem.Allocator) error{OutOfMemory}
if (self.os_version_min != null or self.os_version_max != null) {
switch (self.getOsVersionMin()) {
.none => {},
.semver => |v| try result.writer().print(".{}", .{v}),
.semver => |v| {
try result.writer().writeAll(".");
try formatVersion(v, result.writer());
},
.windows => |v| try result.writer().print("{s}", .{v}),
}
}
if (self.os_version_max) |max| {
switch (max) {
.none => {},
.semver => |v| try result.writer().print("...{}", .{v}),
.semver => |v| {
try result.writer().writeAll("...");
try formatVersion(v, result.writer());
},
.windows => |v| try result.writer().print("..{s}", .{v}),
}
}
if (self.glibc_version) |v| {
try result.writer().print("-{s}.{}", .{ @tagName(self.getAbi()), v });
try result.writer().print("-{s}.", .{@tagName(self.getAbi())});
try formatVersion(v, result.writer());
} else if (self.abi) |abi| {
try result.writer().print("-{s}", .{@tagName(abi)});
}
@ -630,7 +671,7 @@ pub fn isGnuLibC(self: CrossTarget) bool {
pub fn setGnuLibCVersion(self: *CrossTarget, major: u32, minor: u32, patch: u32) void {
assert(self.isGnuLibC());
self.glibc_version = SemVer{ .major = major, .minor = minor, .patch = patch };
self.glibc_version = SemanticVersion{ .major = major, .minor = minor, .patch = patch };
}
pub fn getObjectFormat(self: CrossTarget) Target.ObjectFormat {
@ -709,17 +750,15 @@ fn parseOs(result: *CrossTarget, diags: *ParseOptions.Diagnostics, text: []const
var range_it = mem.splitSequence(u8, version_text, "...");
const min_text = range_it.next().?;
const min_ver = SemVer.parse(min_text) catch |err| switch (err) {
const min_ver = parseVersion(min_text) catch |err| switch (err) {
error.Overflow => return error.InvalidOperatingSystemVersion,
error.InvalidCharacter => return error.InvalidOperatingSystemVersion,
error.InvalidVersion => return error.InvalidOperatingSystemVersion,
};
result.os_version_min = .{ .semver = min_ver };
const max_text = range_it.next() orelse return;
const max_ver = SemVer.parse(max_text) catch |err| switch (err) {
const max_ver = parseVersion(max_text) catch |err| switch (err) {
error.Overflow => return error.InvalidOperatingSystemVersion,
error.InvalidCharacter => return error.InvalidOperatingSystemVersion,
error.InvalidVersion => return error.InvalidOperatingSystemVersion,
};
result.os_version_max = .{ .semver = max_ver };

View file

@ -43,24 +43,22 @@ pub fn detect(cross_target: CrossTarget) DetectError!NativeTargetInfo {
const release = mem.sliceTo(&uts.release, 0);
// The release field sometimes has a weird format,
// `Version.parse` will attempt to find some meaningful interpretation.
if (std.builtin.Version.parse(release)) |ver| {
if (std.SemanticVersion.parse(release)) |ver| {
os.version_range.linux.range.min = ver;
os.version_range.linux.range.max = ver;
} else |err| switch (err) {
error.Overflow => {},
error.InvalidCharacter => {},
error.InvalidVersion => {},
}
},
.solaris => {
const uts = std.os.uname();
const release = mem.sliceTo(&uts.release, 0);
if (std.builtin.Version.parse(release)) |ver| {
if (std.SemanticVersion.parse(release)) |ver| {
os.version_range.semver.min = ver;
os.version_range.semver.max = ver;
} else |err| switch (err) {
error.Overflow => {},
error.InvalidCharacter => {},
error.InvalidVersion => {},
}
},
@ -144,7 +142,7 @@ pub fn detect(cross_target: CrossTarget) DetectError!NativeTargetInfo {
error.Unexpected => return error.OSVersionDetectionFail,
};
if (std.builtin.Version.parse(buf[0 .. len - 1])) |ver| {
if (std.SemanticVersion.parse(buf[0 .. len - 1])) |ver| {
os.version_range.semver.min = ver;
os.version_range.semver.max = ver;
} else |_| {
@ -390,7 +388,7 @@ fn detectAbiAndDynamicLinker(
};
}
fn glibcVerFromRPath(rpath: []const u8) !std.builtin.Version {
fn glibcVerFromRPath(rpath: []const u8) !std.SemanticVersion {
var dir = fs.cwd().openDir(rpath, .{}) catch |err| switch (err) {
error.NameTooLong => unreachable,
error.InvalidUtf8 => unreachable,
@ -471,7 +469,7 @@ fn glibcVerFromRPath(rpath: []const u8) !std.builtin.Version {
};
}
fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
fn glibcVerFromSoFile(file: fs.File) !std.SemanticVersion {
var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 align(@alignOf(elf.Elf64_Ehdr)) = undefined;
_ = try preadMin(file, &hdr_buf, 0, hdr_buf.len);
const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);
@ -557,13 +555,12 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
const dynstr_bytes = buf[0..dynstr_size];
_ = try preadMin(file, dynstr_bytes, dynstr.offset, dynstr_bytes.len);
var it = mem.splitScalar(u8, dynstr_bytes, 0);
var max_ver: std.builtin.Version = .{ .major = 2, .minor = 2, .patch = 5 };
var max_ver: std.SemanticVersion = .{ .major = 2, .minor = 2, .patch = 5 };
while (it.next()) |s| {
if (mem.startsWith(u8, s, "GLIBC_2.")) {
const chopped = s["GLIBC_".len..];
const ver = std.builtin.Version.parse(chopped) catch |err| switch (err) {
const ver = std.SemanticVersion.parse(chopped) catch |err| switch (err) {
error.Overflow => return error.InvalidGnuLibCVersion,
error.InvalidCharacter => return error.InvalidGnuLibCVersion,
error.InvalidVersion => return error.InvalidGnuLibCVersion,
};
switch (ver.order(max_ver)) {
@ -575,7 +572,7 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
return max_ver;
}
fn glibcVerFromLinkName(link_name: []const u8, prefix: []const u8) !std.builtin.Version {
fn glibcVerFromLinkName(link_name: []const u8, prefix: []const u8) !std.SemanticVersion {
// example: "libc-2.3.4.so"
// example: "libc-2.27.so"
// example: "ld-2.33.so"
@ -585,9 +582,8 @@ fn glibcVerFromLinkName(link_name: []const u8, prefix: []const u8) !std.builtin.
}
// chop off "libc-" and ".so"
const link_name_chopped = link_name[prefix.len .. link_name.len - suffix.len];
return std.builtin.Version.parse(link_name_chopped) catch |err| switch (err) {
return std.SemanticVersion.parse(link_name_chopped) catch |err| switch (err) {
error.Overflow => return error.InvalidGnuLibCVersion,
error.InvalidCharacter => return error.InvalidGnuLibCVersion,
error.InvalidVersion => return error.InvalidGnuLibCVersion,
};
}

View file

@ -2,7 +2,7 @@ const std = @import("std");
const mem = std.mem;
const Allocator = mem.Allocator;
const Target = std.Target;
const Version = std.builtin.Version;
const Version = std.SemanticVersion;
pub const macos = @import("darwin/macos.zig");
@ -69,6 +69,7 @@ pub fn getDarwinSDK(allocator: Allocator, target: Target) ?DarwinSDK {
const version = Version.parse(raw_version) catch Version{
.major = 0,
.minor = 0,
.patch = 0,
};
break :version version;
};

View file

@ -74,20 +74,39 @@ pub fn detect(target_os: *Target.Os) !void {
return error.OSVersionDetectionFail;
}
fn parseSystemVersion(buf: []const u8) !std.builtin.Version {
fn parseSystemVersion(buf: []const u8) !std.SemanticVersion {
var svt = SystemVersionTokenizer{ .bytes = buf };
try svt.skipUntilTag(.start, "dict");
while (true) {
try svt.skipUntilTag(.start, "key");
const content = try svt.expectContent();
try svt.skipUntilTag(.end, "key");
if (std.mem.eql(u8, content, "ProductVersion")) break;
if (mem.eql(u8, content, "ProductVersion")) break;
}
try svt.skipUntilTag(.start, "string");
const ver = try svt.expectContent();
try svt.skipUntilTag(.end, "string");
return std.builtin.Version.parse(ver);
const parseVersionComponent = struct {
fn parseVersionComponent(component: []const u8) !usize {
return std.fmt.parseUnsigned(usize, component, 10) catch |err| {
switch (err) {
error.InvalidCharacter => return error.InvalidVersion,
error.Overflow => return error.Overflow,
}
};
}
}.parseVersionComponent;
var version_components = mem.split(u8, ver, ".");
const major = version_components.first();
const minor = version_components.next() orelse return error.InvalidVersion;
const patch = version_components.next() orelse "0";
if (version_components.next() != null) return error.InvalidVersion;
return .{
.major = try parseVersionComponent(major),
.minor = try parseVersionComponent(minor),
.patch = try parseVersionComponent(patch),
};
}
const SystemVersionTokenizer = struct {
@ -246,7 +265,7 @@ const SystemVersionTokenizer = struct {
while (try self.next()) |tok| {
switch (tok) {
.tag => |tag| {
if (tag.kind == kind and std.mem.eql(u8, tag.name, name)) return;
if (tag.kind == kind and mem.eql(u8, tag.name, name)) return;
},
else => {},
}
@ -297,7 +316,7 @@ test "detect" {
\\</dict>
\\</plist>
,
.{ .major = 10, .minor = 3 },
.{ .major = 10, .minor = 3, .patch = 0 },
},
.{
\\<?xml version="1.0" encoding="UTF-8"?>
@ -361,7 +380,7 @@ test "detect" {
\\</dict>
\\</plist>
,
.{ .major = 11, .minor = 0 },
.{ .major = 11, .minor = 0, .patch = 0 },
},
.{
\\<?xml version="1.0" encoding="UTF-8"?>
@ -383,27 +402,17 @@ test "detect" {
\\</dict>
\\</plist>
,
.{ .major = 11, .minor = 1 },
.{ .major = 11, .minor = 1, .patch = 0 },
},
};
inline for (cases) |case| {
const ver0 = try parseSystemVersion(case[0]);
const ver1: std.builtin.Version = case[1];
try testVersionEquality(ver1, ver0);
const ver1: std.SemanticVersion = case[1];
try testing.expectEqual(@as(std.math.Order, .eq), ver0.order(ver1));
}
}
fn testVersionEquality(expected: std.builtin.Version, got: std.builtin.Version) !void {
var b_expected: [64]u8 = undefined;
const s_expected: []const u8 = try std.fmt.bufPrint(b_expected[0..], "{}", .{expected});
var b_got: [64]u8 = undefined;
const s_got: []const u8 = try std.fmt.bufPrint(b_got[0..], "{}", .{got});
try testing.expectEqualStrings(s_expected, s_got);
}
pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
var cpu_family: std.c.CPUFAMILY = undefined;
var len: usize = @sizeOf(std.c.CPUFAMILY);

View file

@ -615,8 +615,8 @@ pub const InitOptions = struct {
stack_size_override: ?u64 = null,
image_base_override: ?u64 = null,
self_exe_path: ?[]const u8 = null,
version: ?std.builtin.Version = null,
compatibility_version: ?std.builtin.Version = null,
version: ?std.SemanticVersion = null,
compatibility_version: ?std.SemanticVersion = null,
libc_installation: ?*const LibCInstallation = null,
machine_code_model: std.builtin.CodeModel = .default,
clang_preprocessor_mode: ClangPreprocessorMode = .no,

View file

@ -1,6 +1,6 @@
//! This file is auto-generated by tools/gen_spirv_spec.zig.
const Version = @import("std").builtin.Version;
const Version = @import("std").SemanticVersion;
pub const Word = u32;
pub const IdResult = struct {

View file

@ -5,7 +5,7 @@ const log = std.log;
const fs = std.fs;
const path = fs.path;
const assert = std.debug.assert;
const Version = std.builtin.Version;
const Version = std.SemanticVersion;
const target_util = @import("target.zig");
const Compilation = @import("Compilation.zig");
@ -172,7 +172,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
const target = comp.getTarget();
const target_ver = target.os.version_range.linux.glibc;
const start_old_init_fini = target_ver.order(.{ .major = 2, .minor = 33 }) != .gt;
const start_old_init_fini = target_ver.order(.{ .major = 2, .minor = 33, .patch = 0 }) != .gt;
// In all cases in this function, we add the C compiler flags to
// cache_exempt_flags rather than extra_flags, because these arguments

View file

@ -197,8 +197,8 @@ pub const Options = struct {
/// __real_symbol.
symbol_wrap_set: std.StringArrayHashMapUnmanaged(void),
version: ?std.builtin.Version,
compatibility_version: ?std.builtin.Version,
version: ?std.SemanticVersion,
compatibility_version: ?std.SemanticVersion,
libc_installation: ?*const LibCInstallation,
dwarf_format: ?std.dwarf.Format,

View file

@ -3414,7 +3414,7 @@ const CsuObjects = struct {
if (result.crtn) |*obj| obj.* = try fs.path.join(arena, &[_][]const u8{ crt_dir_path, obj.* });
var gccv: []const u8 = undefined;
if (link_options.target.os.version_range.semver.isAtLeast(.{ .major = 5, .minor = 4 }) orelse true) {
if (link_options.target.os.version_range.semver.isAtLeast(.{ .major = 5, .minor = 4, .patch = 0 }) orelse true) {
gccv = "gcc80";
} else {
gccv = "gcc54";

View file

@ -204,12 +204,12 @@ pub fn writeDylibIdLC(gpa: Allocator, options: *const link.Options, lc_writer: a
const emit = options.emit.?;
const install_name = options.install_name orelse try emit.directory.join(gpa, &.{emit.sub_path});
defer if (options.install_name == null) gpa.free(install_name);
const curr = options.version orelse std.builtin.Version{
const curr = options.version orelse std.SemanticVersion{
.major = 1,
.minor = 0,
.patch = 0,
};
const compat = options.compatibility_version orelse std.builtin.Version{
const compat = options.compatibility_version orelse std.SemanticVersion{
.major = 1,
.minor = 0,
.patch = 0,
@ -217,8 +217,8 @@ pub fn writeDylibIdLC(gpa: Allocator, options: *const link.Options, lc_writer: a
try writeDylibLC(.{
.cmd = .ID_DYLIB,
.name = install_name,
.current_version = curr.major << 16 | curr.minor << 8 | curr.patch,
.compatibility_version = compat.major << 16 | compat.minor << 8 | compat.patch,
.current_version = @intCast(u32, curr.major << 16 | curr.minor << 8 | curr.patch),
.compatibility_version = @intCast(u32, compat.major << 16 | compat.minor << 8 | compat.patch),
}, lc_writer);
}
@ -275,12 +275,12 @@ pub fn writeBuildVersionLC(options: *const link.Options, lc_writer: anytype) !vo
const cmdsize = @sizeOf(macho.build_version_command) + @sizeOf(macho.build_tool_version);
const platform_version = blk: {
const ver = options.target.os.version_range.semver.min;
const platform_version = ver.major << 16 | ver.minor << 8;
const platform_version = @intCast(u32, ver.major << 16 | ver.minor << 8);
break :blk platform_version;
};
const sdk_version = if (options.native_darwin_sdk) |sdk| blk: {
const ver = sdk.version;
const sdk_version = ver.major << 16 | ver.minor << 8;
const sdk_version = @intCast(u32, ver.major << 16 | ver.minor << 8);
break :blk sdk_version;
} else platform_version;
const is_simulator_abi = options.target.abi == .simulator;

View file

@ -724,9 +724,9 @@ fn buildOutputType(
var dll_export_fns: ?bool = null;
var single_threaded: ?bool = null;
var root_src_file: ?[]const u8 = null;
var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
var version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 };
var have_version = false;
var compatibility_version: ?std.builtin.Version = null;
var compatibility_version: ?std.SemanticVersion = null;
var strip: ?bool = null;
var formatted_panics: ?bool = null;
var function_sections = false;
@ -1121,7 +1121,7 @@ fn buildOutputType(
try cssan.addIncludePath(.iframework, arg, args_iter.nextOrFatal(), false);
} else if (mem.eql(u8, arg, "--version")) {
const next_arg = args_iter.nextOrFatal();
version = std.builtin.Version.parse(next_arg) catch |err| {
version = std.SemanticVersion.parse(next_arg) catch |err| {
fatal("unable to parse --version '{s}': {s}", .{ next_arg, @errorName(err) });
};
have_version = true;
@ -2152,12 +2152,12 @@ fn buildOutputType(
try system_libs.put(linker_args_it.nextOrFatal(), .{ .weak = true });
} else if (mem.eql(u8, arg, "-compatibility_version")) {
const compat_version = linker_args_it.nextOrFatal();
compatibility_version = std.builtin.Version.parse(compat_version) catch |err| {
compatibility_version = std.SemanticVersion.parse(compat_version) catch |err| {
fatal("unable to parse -compatibility_version '{s}': {s}", .{ compat_version, @errorName(err) });
};
} else if (mem.eql(u8, arg, "-current_version")) {
const curr_version = linker_args_it.nextOrFatal();
version = std.builtin.Version.parse(curr_version) catch |err| {
version = std.SemanticVersion.parse(curr_version) catch |err| {
fatal("unable to parse -current_version '{s}': {s}", .{ curr_version, @errorName(err) });
};
have_version = true;
@ -2207,10 +2207,9 @@ fn buildOutputType(
} else if (mem.startsWith(u8, arg, "/version:")) {
var split_it = mem.splitBackwardsScalar(u8, arg, ':');
const version_arg = split_it.first();
version = std.builtin.Version.parse(version_arg) catch |err| {
version = std.SemanticVersion.parse(version_arg) catch |err| {
fatal("unable to parse /version '{s}': {s}", .{ arg, @errorName(err) });
};
have_version = true;
} else {
fatal("unsupported linker arg: {s}", .{arg});

View file

@ -6,7 +6,7 @@ pub const ArchOsAbi = struct {
arch: std.Target.Cpu.Arch,
os: std.Target.Os.Tag,
abi: std.Target.Abi,
os_ver: ?std.builtin.Version = null,
os_ver: ?std.SemanticVersion = null,
};
pub const available_libcs = [_]ArchOsAbi{
@ -16,9 +16,9 @@ pub const available_libcs = [_]ArchOsAbi{
.{ .arch = .aarch64, .os = .linux, .abi = .gnu },
.{ .arch = .aarch64, .os = .linux, .abi = .musl },
.{ .arch = .aarch64, .os = .windows, .abi = .gnu },
.{ .arch = .aarch64, .os = .macos, .abi = .none, .os_ver = .{ .major = 11, .minor = 0 } },
.{ .arch = .aarch64, .os = .macos, .abi = .none, .os_ver = .{ .major = 12, .minor = 0 } },
.{ .arch = .aarch64, .os = .macos, .abi = .none, .os_ver = .{ .major = 13, .minor = 0 } },
.{ .arch = .aarch64, .os = .macos, .abi = .none, .os_ver = .{ .major = 11, .minor = 0, .patch = 0 } },
.{ .arch = .aarch64, .os = .macos, .abi = .none, .os_ver = .{ .major = 12, .minor = 0, .patch = 0 } },
.{ .arch = .aarch64, .os = .macos, .abi = .none, .os_ver = .{ .major = 13, .minor = 0, .patch = 0 } },
.{ .arch = .armeb, .os = .linux, .abi = .gnueabi },
.{ .arch = .armeb, .os = .linux, .abi = .gnueabihf },
.{ .arch = .armeb, .os = .linux, .abi = .musleabi },
@ -71,9 +71,9 @@ pub const available_libcs = [_]ArchOsAbi{
.{ .arch = .x86_64, .os = .linux, .abi = .gnux32 },
.{ .arch = .x86_64, .os = .linux, .abi = .musl },
.{ .arch = .x86_64, .os = .windows, .abi = .gnu },
.{ .arch = .x86_64, .os = .macos, .abi = .none, .os_ver = .{ .major = 11, .minor = 0 } },
.{ .arch = .x86_64, .os = .macos, .abi = .none, .os_ver = .{ .major = 12, .minor = 0 } },
.{ .arch = .x86_64, .os = .macos, .abi = .none, .os_ver = .{ .major = 13, .minor = 0 } },
.{ .arch = .x86_64, .os = .macos, .abi = .none, .os_ver = .{ .major = 11, .minor = 0, .patch = 0 } },
.{ .arch = .x86_64, .os = .macos, .abi = .none, .os_ver = .{ .major = 12, .minor = 0, .patch = 0 } },
.{ .arch = .x86_64, .os = .macos, .abi = .none, .os_ver = .{ .major = 13, .minor = 0, .patch = 0 } },
};
pub fn libCGenericName(target: std.Target) [:0]const u8 {

View file

@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
const dylib = b.addSharedLibrary(.{
.name = "a",
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.optimize = optimize,
.target = target,
});

View file

@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
const dylib = b.addSharedLibrary(.{
.name = "a",
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.optimize = optimize,
.target = target,
});

View file

@ -61,7 +61,7 @@ fn createScenario(
const dylib = b.addSharedLibrary(.{
.name = name,
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.optimize = optimize,
.target = target,
});

View file

@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
const lib = b.addSharedLibrary(.{
.name = "a",
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.optimize = optimize,
.target = target,
});

View file

@ -62,7 +62,7 @@ fn simpleDylib(
) *std.Build.Step.Compile {
const dylib = b.addSharedLibrary(.{
.name = "test",
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.optimize = optimize,
.target = target,
});

View file

@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
const lib = b.addSharedLibrary(.{
.name = "add",
.root_source_file = .{ .path = "add.zig" },
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.optimize = optimize,
.target = target,
});

View file

@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const lib = b.addSharedLibrary(.{
.name = "mathtest",
.root_source_file = .{ .path = "mathtest.zig" },
.version = .{ .major = 1, .minor = 0 },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.target = target,
.optimize = optimize,
});

View file

@ -76,7 +76,7 @@ fn render(writer: anytype, allocator: Allocator, registry: g.CoreRegistry) !void
try writer.writeAll(
\\//! This file is auto-generated by tools/gen_spirv_spec.zig.
\\
\\const Version = @import("std").builtin.Version;
\\const Version = @import("std").SemanticVersion;
\\
\\pub const Word = u32;
\\pub const IdResult = struct{