authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-03 00:57:25-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-03 00:57:25-05:00
log8bd734d60cd55d65ea52a051ccdc35939edeb99c
tree673eae57056242a83d1971af16b926b942528b6c
parentc1f404ad537ded5a584525f7f89dcf5705a2c405
parent7798af74614af595390130b364817626603be21e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13699 from mikdusan/bsd

give BSDs some attention

16 files changed, 164 insertions(+), 72 deletions(-)

CMakeLists.txt+5-11
...@@ -708,21 +708,15 @@ target_link_libraries(zigcpp LINK_PUBLIC...@@ -708,21 +708,15 @@ target_link_libraries(zigcpp LINK_PUBLIC
708 ${CMAKE_THREAD_LIBS_INIT}708 ${CMAKE_THREAD_LIBS_INIT}
709)709)
710710
711if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")711string(TOLOWER "${CMAKE_HOST_SYSTEM_PROCESSOR}" HOST_TARGET_ARCH)
712if(HOST_TARGET_ARCH STREQUAL "amd64")
712 set(HOST_TARGET_ARCH "x86_64")713 set(HOST_TARGET_ARCH "x86_64")
713elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "amd64")714elseif(HOST_TARGET_ARCH STREQUAL "arm64")
714 set(HOST_TARGET_ARCH "x86_64")
715elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
716 set(HOST_TARGET_ARCH "aarch64")
717elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "ARM64")
718 set(HOST_TARGET_ARCH "aarch64")715 set(HOST_TARGET_ARCH "aarch64")
719else()
720 string(TOLOWER "${CMAKE_HOST_SYSTEM_PROCESSOR}" HOST_TARGET_ARCH)
721endif()716endif()
722if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")717string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" HOST_TARGET_OS)
718if(HOST_TARGET_OS STREQUAL "darwin")
723 set(HOST_TARGET_OS "macos")719 set(HOST_TARGET_OS "macos")
724else()
725 string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" HOST_TARGET_OS)
726endif()720endif()
727set(HOST_TARGET_TRIPLE "${HOST_TARGET_ARCH}-${HOST_TARGET_OS}")721set(HOST_TARGET_TRIPLE "${HOST_TARGET_ARCH}-${HOST_TARGET_OS}")
728722
build.zig+32-18
...@@ -560,24 +560,38 @@ fn addCmakeCfgOptionsToExe(...@@ -560,24 +560,38 @@ fn addCmakeCfgOptionsToExe(
560560
561 // System -lc++ must be used because in this code path we are attempting to link561 // System -lc++ must be used because in this code path we are attempting to link
562 // against system-provided LLVM, Clang, LLD.562 // against system-provided LLVM, Clang, LLD.
563 if (exe.target.getOsTag() == .linux) {563 switch (exe.target.getOsTag()) {
564 // First we try to link against gcc libstdc++. If that doesn't work, we fall564 .linux => {
565 // back to -lc++ and cross our fingers.565 // First we try to link against gcc libstdc++. If that doesn't work, we fall
566 addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), "", need_cpp_includes) catch |err| switch (err) {566 // back to -lc++ and cross our fingers.
567 error.RequiredLibraryNotFound => {567 addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), "", need_cpp_includes) catch |err| switch (err) {
568 exe.linkSystemLibrary("c++");568 error.RequiredLibraryNotFound => {
569 },569 exe.linkSystemLibrary("c++");
570 else => |e| return e,570 },
571 };571 else => |e| return e,
572 exe.linkSystemLibrary("unwind");572 };
573 } else if (exe.target.isFreeBSD()) {573 exe.linkSystemLibrary("unwind");
574 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++.{s}", .{lib_suffix}), null, need_cpp_includes);574 },
575 exe.linkSystemLibrary("pthread");575 .ios, .macos, .watchos, .tvos => {
576 } else if (exe.target.getOsTag() == .openbsd) {576 exe.linkSystemLibrary("c++");
577 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++.{s}", .{lib_suffix}), null, need_cpp_includes);577 },
578 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++abi.{s}", .{lib_suffix}), null, need_cpp_includes);578 .freebsd => {
579 } else if (exe.target.isDarwin()) {579 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++.{s}", .{lib_suffix}), null, need_cpp_includes);
580 exe.linkSystemLibrary("c++");580 try addCxxKnownPath(b, cfg, exe, b.fmt("libgcc_eh.{s}", .{lib_suffix}), null, need_cpp_includes);
581 },
582 .openbsd => {
583 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++.{s}", .{lib_suffix}), null, need_cpp_includes);
584 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++abi.{s}", .{lib_suffix}), null, need_cpp_includes);
585 },
586 .netbsd => {
587 try addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), null, need_cpp_includes);
588 try addCxxKnownPath(b, cfg, exe, b.fmt("libgcc_eh.{s}", .{lib_suffix}), null, need_cpp_includes);
589 },
590 .dragonfly => {
591 try addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), null, need_cpp_includes);
592 try addCxxKnownPath(b, cfg, exe, b.fmt("libgcc_eh.{s}", .{lib_suffix}), null, need_cpp_includes);
593 },
594 else => {},
581 }595 }
582 }596 }
583597
lib/libcxx/include/__config+6-5
...@@ -148,7 +148,7 @@...@@ -148,7 +148,7 @@
148# endif148# endif
149// Feature macros for disabling pre ABI v1 features. All of these options149// Feature macros for disabling pre ABI v1 features. All of these options
150// are deprecated.150// are deprecated.
151# if defined(__FreeBSD__)151# if defined(__FreeBSD__) || defined(__DragonFly__)
152# define _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR152# define _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR
153# endif153# endif
154# endif154# endif
...@@ -726,11 +726,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD...@@ -726,11 +726,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
726# endif // _LIBCPP_CXX03_LANG726# endif // _LIBCPP_CXX03_LANG
727727
728# if defined(__APPLE__) || defined(__FreeBSD__) || defined(_LIBCPP_MSVCRT_LIKE) || defined(__sun__) || \728# if defined(__APPLE__) || defined(__FreeBSD__) || defined(_LIBCPP_MSVCRT_LIKE) || defined(__sun__) || \
729 defined(__NetBSD__)729 defined(__NetBSD__) || defined(__DragonFly__)
730# define _LIBCPP_LOCALE__L_EXTENSIONS 1730# define _LIBCPP_LOCALE__L_EXTENSIONS 1
731# endif731# endif
732732
733# ifdef __FreeBSD__733# if defined(__FreeBSD__) || defined(__DragonFly__)
734# define _DECLARE_C99_LDBL_MATH 1734# define _DECLARE_C99_LDBL_MATH 1
735# endif735# endif
736736
...@@ -750,11 +750,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD...@@ -750,11 +750,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
750# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION750# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
751# endif751# endif
752752
753# if defined(__APPLE__) || defined(__FreeBSD__)753# if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
754# define _LIBCPP_HAS_DEFAULTRUNELOCALE754# define _LIBCPP_HAS_DEFAULTRUNELOCALE
755# endif755# endif
756756
757# if defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun__)757# if defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun__) || defined(__DragonFly__)
758# define _LIBCPP_WCTYPE_IS_MASK758# define _LIBCPP_WCTYPE_IS_MASK
759# endif759# endif
760760
...@@ -901,6 +901,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD...@@ -901,6 +901,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
901901
902# if defined(__FreeBSD__) || \902# if defined(__FreeBSD__) || \
903 defined(__wasi__) || \903 defined(__wasi__) || \
904 defined(__DragonFly__) || \
904 defined(__NetBSD__) || \905 defined(__NetBSD__) || \
905 defined(__OpenBSD__) || \906 defined(__OpenBSD__) || \
906 defined(__NuttX__) || \907 defined(__NuttX__) || \
lib/libcxx/include/__locale+3-3
...@@ -33,7 +33,7 @@...@@ -33,7 +33,7 @@
33# include <__support/newlib/xlocale.h>33# include <__support/newlib/xlocale.h>
34#elif defined(__OpenBSD__)34#elif defined(__OpenBSD__)
35# include <__support/openbsd/xlocale.h>35# include <__support/openbsd/xlocale.h>
36#elif (defined(__APPLE__) || defined(__FreeBSD__))36#elif (defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__))
37# include <xlocale.h>37# include <xlocale.h>
38#elif defined(__Fuchsia__)38#elif defined(__Fuchsia__)
39# include <__support/fuchsia/xlocale.h>39# include <__support/fuchsia/xlocale.h>
...@@ -453,10 +453,10 @@ public:...@@ -453,10 +453,10 @@ public:
453 static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used453 static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
454# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT454# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
455# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA455# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
456#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)456#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) || defined(__DragonFly__)
457# ifdef __APPLE__457# ifdef __APPLE__
458 typedef __uint32_t mask;458 typedef __uint32_t mask;
459# elif defined(__FreeBSD__)459# elif defined(__FreeBSD__) || defined(__DragonFly__)
460 typedef unsigned long mask;460 typedef unsigned long mask;
461# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)461# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
462 typedef unsigned short mask;462 typedef unsigned short mask;
lib/libcxx/include/locale+1-1
...@@ -239,7 +239,7 @@ _LIBCPP_PUSH_MACROS...@@ -239,7 +239,7 @@ _LIBCPP_PUSH_MACROS
239239
240_LIBCPP_BEGIN_NAMESPACE_STD240_LIBCPP_BEGIN_NAMESPACE_STD
241241
242#if defined(__APPLE__) || defined(__FreeBSD__)242#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
243# define _LIBCPP_GET_C_LOCALE 0243# define _LIBCPP_GET_C_LOCALE 0
244#elif defined(__NetBSD__)244#elif defined(__NetBSD__)
245# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE245# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
lib/libcxx/src/locale.cpp+1-1
...@@ -1190,7 +1190,7 @@ ctype<char>::classic_table() noexcept...@@ -1190,7 +1190,7 @@ ctype<char>::classic_table() noexcept
1190const ctype<char>::mask*1190const ctype<char>::mask*
1191ctype<char>::classic_table() noexcept1191ctype<char>::classic_table() noexcept
1192{1192{
1193#if defined(__APPLE__) || defined(__FreeBSD__)1193#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
1194 return _DefaultRuneLocale.__runetype;1194 return _DefaultRuneLocale.__runetype;
1195#elif defined(__NetBSD__)1195#elif defined(__NetBSD__)
1196 return _C_ctype_tab_ + 1;1196 return _C_ctype_tab_ + 1;
lib/std/c/dragonfly.zig+2
...@@ -12,6 +12,7 @@ pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;...@@ -12,6 +12,7 @@ pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
12pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;12pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
13pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;13pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
14pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;14pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
15pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
1516
16pub const dl_iterate_phdr_callback = *const fn (info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.C) c_int;17pub const dl_iterate_phdr_callback = *const fn (info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.C) c_int;
17pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*anyopaque) c_int;18pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*anyopaque) c_int;
...@@ -419,6 +420,7 @@ pub const F = struct {...@@ -419,6 +420,7 @@ pub const F = struct {
419 pub const DUP2FD = 10;420 pub const DUP2FD = 10;
420 pub const DUPFD_CLOEXEC = 17;421 pub const DUPFD_CLOEXEC = 17;
421 pub const DUP2FD_CLOEXEC = 18;422 pub const DUP2FD_CLOEXEC = 18;
423 pub const GETPATH = 19;
422};424};
423425
424pub const FD_CLOEXEC = 1;426pub const FD_CLOEXEC = 1;
lib/std/c/freebsd.zig+3
...@@ -15,12 +15,15 @@ pub extern "c" fn pthread_getthreadid_np() c_int;...@@ -15,12 +15,15 @@ pub extern "c" fn pthread_getthreadid_np() c_int;
15pub extern "c" fn pthread_set_name_np(thread: std.c.pthread_t, name: [*:0]const u8) void;15pub extern "c" fn pthread_set_name_np(thread: std.c.pthread_t, name: [*:0]const u8) void;
16pub extern "c" fn pthread_get_name_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) void;16pub extern "c" fn pthread_get_name_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) void;
17pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;17pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
18pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
1819
19pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;20pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
20pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;21pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;
2122
22pub extern "c" fn getpid() pid_t;23pub extern "c" fn getpid() pid_t;
2324
25pub extern "c" fn kinfo_getfile(pid: pid_t, cntp: *c_int) ?[*]kinfo_file;
26
24pub const sf_hdtr = extern struct {27pub const sf_hdtr = extern struct {
25 headers: [*]const iovec_const,28 headers: [*]const iovec_const,
26 hdr_cnt: c_int,29 hdr_cnt: c_int,
lib/std/c/netbsd.zig+7-2
...@@ -537,6 +537,7 @@ pub const KERN = struct {...@@ -537,6 +537,7 @@ pub const KERN = struct {
537};537};
538538
539pub const PATH_MAX = 1024;539pub const PATH_MAX = 1024;
540pub const NAME_MAX = 255;
540pub const IOV_MAX = KERN.IOV_MAX;541pub const IOV_MAX = KERN.IOV_MAX;
541542
542pub const STDIN_FILENO = 0;543pub const STDIN_FILENO = 0;
...@@ -689,13 +690,17 @@ pub const F = struct {...@@ -689,13 +690,17 @@ pub const F = struct {
689 pub const SETFD = 2;690 pub const SETFD = 2;
690 pub const GETFL = 3;691 pub const GETFL = 3;
691 pub const SETFL = 4;692 pub const SETFL = 4;
692
693 pub const GETOWN = 5;693 pub const GETOWN = 5;
694 pub const SETOWN = 6;694 pub const SETOWN = 6;
695
696 pub const GETLK = 7;695 pub const GETLK = 7;
697 pub const SETLK = 8;696 pub const SETLK = 8;
698 pub const SETLKW = 9;697 pub const SETLKW = 9;
698 pub const CLOSEM = 10;
699 pub const MAXFD = 11;
700 pub const DUPFD_CLOEXEC = 12;
701 pub const GETNOSIGPIPE = 13;
702 pub const SETNOSIGPIPE = 14;
703 pub const GETPATH = 15;
699704
700 pub const RDLCK = 1;705 pub const RDLCK = 1;
701 pub const WRLCK = 3;706 pub const WRLCK = 3;
lib/std/c/openbsd.zig+1
...@@ -417,6 +417,7 @@ pub const AI = struct {...@@ -417,6 +417,7 @@ pub const AI = struct {
417};417};
418418
419pub const PATH_MAX = 1024;419pub const PATH_MAX = 1024;
420pub const NAME_MAX = 255;
420pub const IOV_MAX = 1024;421pub const IOV_MAX = 1024;
421422
422pub const STDIN_FILENO = 0;423pub const STDIN_FILENO = 0;
lib/std/fs.zig+3-3
...@@ -34,7 +34,7 @@ pub const Watch = @import("fs/watch.zig").Watch;...@@ -34,7 +34,7 @@ pub const Watch = @import("fs/watch.zig").Watch;
34/// fit into a UTF-8 encoded array of this length.34/// fit into a UTF-8 encoded array of this length.
35/// The byte count includes room for a null sentinel byte.35/// The byte count includes room for a null sentinel byte.
36pub const MAX_PATH_BYTES = switch (builtin.os.tag) {36pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
37 .linux, .macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, .haiku, .solaris => os.PATH_MAX,37 .linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .haiku, .solaris => os.PATH_MAX,
38 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.38 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
39 // If it would require 4 UTF-8 bytes, then there would be a surrogate39 // If it would require 4 UTF-8 bytes, then there would be a surrogate
40 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.40 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
...@@ -54,10 +54,10 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {...@@ -54,10 +54,10 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
54/// (depending on the platform) this assumption may not hold for every configuration.54/// (depending on the platform) this assumption may not hold for every configuration.
55/// The byte count does not include a null sentinel byte.55/// The byte count does not include a null sentinel byte.
56pub const MAX_NAME_BYTES = switch (builtin.os.tag) {56pub const MAX_NAME_BYTES = switch (builtin.os.tag) {
57 .linux, .macos, .ios, .freebsd, .dragonfly => os.NAME_MAX,57 .linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly => os.NAME_MAX,
58 // Haiku's NAME_MAX includes the null terminator, so subtract one.58 // Haiku's NAME_MAX includes the null terminator, so subtract one.
59 .haiku => os.NAME_MAX - 1,59 .haiku => os.NAME_MAX - 1,
60 .netbsd, .openbsd, .solaris => os.MAXNAMLEN,60 .solaris => os.system.MAXNAMLEN,
61 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.61 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
62 // If it would require 4 UTF-8 bytes, then there would be a surrogate62 // If it would require 4 UTF-8 bytes, then there would be a surrogate
63 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.63 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
lib/std/fs/test.zig+2-2
...@@ -446,8 +446,8 @@ test "file operations on directories" {...@@ -446,8 +446,8 @@ test "file operations on directories" {
446 try testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));446 try testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));
447 try testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));447 try testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));
448 switch (builtin.os.tag) {448 switch (builtin.os.tag) {
449 // NetBSD does not error when reading a directory.449 // no error when reading a directory.
450 .netbsd => {},450 .dragonfly, .netbsd => {},
451 // Currently, WASI will return error.Unexpected (via ENOTCAPABLE) when attempting fd_read on a directory handle.451 // Currently, WASI will return error.Unexpected (via ENOTCAPABLE) when attempting fd_read on a directory handle.
452 // TODO: Re-enable on WASI once https://github.com/bytecodealliance/wasmtime/issues/1935 is resolved.452 // TODO: Re-enable on WASI once https://github.com/bytecodealliance/wasmtime/issues/1935 is resolved.
453 .wasi => {},453 .wasi => {},
lib/std/os.zig+80-9
...@@ -2696,6 +2696,8 @@ pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: u32) MakeDirErr...@@ -2696,6 +2696,8 @@ pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: u32) MakeDirErr
2696 .NOSPC => return error.NoSpaceLeft,2696 .NOSPC => return error.NoSpaceLeft,
2697 .NOTDIR => return error.NotDir,2697 .NOTDIR => return error.NotDir,
2698 .ROFS => return error.ReadOnlyFileSystem,2698 .ROFS => return error.ReadOnlyFileSystem,
2699 // dragonfly: when dir_fd is unlinked from filesystem
2700 .NOTCONN => return error.FileNotFound,
2699 else => |err| return unexpectedErrno(err),2701 else => |err| return unexpectedErrno(err),
2700 }2702 }
2701}2703}
...@@ -5098,6 +5100,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {...@@ -5098,6 +5100,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
5098 switch (errno(system.fcntl(fd, F.GETPATH, out_buffer))) {5100 switch (errno(system.fcntl(fd, F.GETPATH, out_buffer))) {
5099 .SUCCESS => {},5101 .SUCCESS => {},
5100 .BADF => return error.FileNotFound,5102 .BADF => return error.FileNotFound,
5103 .NOSPC => return error.NameTooLong,
5101 // TODO man pages for fcntl on macOS don't really tell you what5104 // TODO man pages for fcntl on macOS don't really tell you what
5102 // errno values to expect when command is F.GETPATH...5105 // errno values to expect when command is F.GETPATH...
5103 else => |err| return unexpectedErrno(err),5106 else => |err| return unexpectedErrno(err),
...@@ -5130,19 +5133,85 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {...@@ -5130,19 +5133,85 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
5130 return target;5133 return target;
5131 },5134 },
5132 .freebsd => {5135 .freebsd => {
5133 comptime if (builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .lt)5136 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .gt) {
5134 @compileError("querying for canonical path of a handle is unsupported on FreeBSD 12 and below");5137 var kfile: system.kinfo_file = undefined;
51355138 kfile.structsize = system.KINFO_FILE_SIZE;
5136 var kfile: system.kinfo_file = undefined;5139 switch (errno(system.fcntl(fd, system.F.KINFO, @ptrToInt(&kfile)))) {
5137 kfile.structsize = system.KINFO_FILE_SIZE;5140 .SUCCESS => {},
5138 switch (errno(system.fcntl(fd, system.F.KINFO, @ptrToInt(&kfile)))) {5141 .BADF => return error.FileNotFound,
5142 else => |err| return unexpectedErrno(err),
5143 }
5144 const len = mem.indexOfScalar(u8, &kfile.path, 0) orelse MAX_PATH_BYTES;
5145 if (len == 0) return error.NameTooLong;
5146 mem.copy(u8, out_buffer, kfile.path[0..len]);
5147 return out_buffer[0..len];
5148 } else {
5149 // This fallback implementation reimplements libutil's `kinfo_getfile()`.
5150 // The motivation is to avoid linking -lutil when building zig or general
5151 // user executables.
5152 var mib = [4]c_int{ CTL.KERN, KERN.PROC, KERN.PROC_FILEDESC, system.getpid() };
5153 var len: usize = undefined;
5154 sysctl(&mib, null, &len, null, 0) catch |err| switch (err) {
5155 error.PermissionDenied => unreachable,
5156 error.SystemResources => return error.SystemResources,
5157 error.NameTooLong => unreachable,
5158 error.UnknownName => unreachable,
5159 else => return error.Unexpected,
5160 };
5161 len = len * 4 / 3;
5162 const buf = std.heap.c_allocator.alloc(u8, len) catch return error.SystemResources;
5163 defer std.heap.c_allocator.free(buf);
5164 len = buf.len;
5165 sysctl(&mib, &buf[0], &len, null, 0) catch |err| switch (err) {
5166 error.PermissionDenied => unreachable,
5167 error.SystemResources => return error.SystemResources,
5168 error.NameTooLong => unreachable,
5169 error.UnknownName => unreachable,
5170 else => return error.Unexpected,
5171 };
5172 var i: usize = 0;
5173 while (i < len) {
5174 const kf: *align(1) system.kinfo_file = @ptrCast(*align(1) system.kinfo_file, &buf[i]);
5175 if (kf.fd == fd) {
5176 len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES;
5177 if (len == 0) return error.NameTooLong;
5178 mem.copy(u8, out_buffer, kf.path[0..len]);
5179 return out_buffer[0..len];
5180 }
5181 i += @intCast(usize, kf.structsize);
5182 }
5183 return error.InvalidHandle;
5184 }
5185 },
5186 .dragonfly => {
5187 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 6, .minor = 0 }) == .lt) {
5188 @compileError("querying for canonical path of a handle is unsupported on this host");
5189 }
5190 @memset(out_buffer, 0, MAX_PATH_BYTES);
5191 switch (errno(system.fcntl(fd, F.GETPATH, out_buffer))) {
5139 .SUCCESS => {},5192 .SUCCESS => {},
5140 .BADF => return error.FileNotFound,5193 .BADF => return error.FileNotFound,
5194 .RANGE => return error.NameTooLong,
5141 else => |err| return unexpectedErrno(err),5195 else => |err| return unexpectedErrno(err),
5142 }5196 }
51435197 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5144 const len = mem.indexOfScalar(u8, &kfile.path, 0) orelse MAX_PATH_BYTES;5198 return out_buffer[0..len];
5145 mem.copy(u8, out_buffer, kfile.path[0..len]);5199 },
5200 .netbsd => {
5201 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 10, .minor = 0 }) == .lt) {
5202 @compileError("querying for canonical path of a handle is unsupported on this host");
5203 }
5204 @memset(out_buffer, 0, MAX_PATH_BYTES);
5205 switch (errno(system.fcntl(fd, F.GETPATH, out_buffer))) {
5206 .SUCCESS => {},
5207 .ACCES => return error.AccessDenied,
5208 .BADF => return error.FileNotFound,
5209 .NOENT => return error.FileNotFound,
5210 .NOMEM => return error.SystemResources,
5211 .RANGE => return error.NameTooLong,
5212 else => |err| return unexpectedErrno(err),
5213 }
5214 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5146 return out_buffer[0..len];5215 return out_buffer[0..len];
5147 },5216 },
5148 else => @compileError("querying for canonical path of a handle is unsupported on this host"),5217 else => @compileError("querying for canonical path of a handle is unsupported on this host"),
...@@ -6528,6 +6597,8 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t {...@@ -6528,6 +6597,8 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t {
6528 }6597 }
6529 },6598 },
6530 .freebsd => {6599 .freebsd => {
6600 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .lt)
6601 @compileError("memfd_create is unavailable on FreeBSD < 13.0");
6531 const rc = system.memfd_create(name, flags);6602 const rc = system.memfd_create(name, flags);
6532 switch (errno(rc)) {6603 switch (errno(rc)) {
6533 .SUCCESS => return rc,6604 .SUCCESS => return rc,
lib/std/os/test.zig+8-1
...@@ -515,7 +515,14 @@ test "argsAlloc" {...@@ -515,7 +515,14 @@ test "argsAlloc" {
515515
516test "memfd_create" {516test "memfd_create" {
517 // memfd_create is only supported by linux and freebsd.517 // memfd_create is only supported by linux and freebsd.
518 if (native_os != .linux and native_os != .freebsd) return error.SkipZigTest;518 switch (native_os) {
519 .linux => {},
520 .freebsd => {
521 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0 }) == .lt)
522 return error.SkipZigTest;
523 },
524 else => return error.SkipZigTest,
525 }
519526
520 const fd = std.os.memfd_create("test", 0) catch |err| switch (err) {527 const fd = std.os.memfd_create("test", 0) catch |err| switch (err) {
521 // Related: https://github.com/ziglang/zig/issues/4019528 // Related: https://github.com/ziglang/zig/issues/4019
lib/std/target.zig+4-4
...@@ -273,7 +273,7 @@ pub const Target = struct {...@@ -273,7 +273,7 @@ pub const Target = struct {
273 .freebsd => return .{273 .freebsd => return .{
274 .semver = Version.Range{274 .semver = Version.Range{
275 .min = .{ .major = 12, .minor = 0 },275 .min = .{ .major = 12, .minor = 0 },
276 .max = .{ .major = 13, .minor = 0 },276 .max = .{ .major = 13, .minor = 1 },
277 },277 },
278 },278 },
279 .macos => return switch (arch) {279 .macos => return switch (arch) {
...@@ -312,19 +312,19 @@ pub const Target = struct {...@@ -312,19 +312,19 @@ pub const Target = struct {
312 .netbsd => return .{312 .netbsd => return .{
313 .semver = .{313 .semver = .{
314 .min = .{ .major = 8, .minor = 0 },314 .min = .{ .major = 8, .minor = 0 },
315 .max = .{ .major = 9, .minor = 1 },315 .max = .{ .major = 10, .minor = 0 },
316 },316 },
317 },317 },
318 .openbsd => return .{318 .openbsd => return .{
319 .semver = .{319 .semver = .{
320 .min = .{ .major = 6, .minor = 8 },320 .min = .{ .major = 6, .minor = 8 },
321 .max = .{ .major = 6, .minor = 9 },321 .max = .{ .major = 7, .minor = 2 },
322 },322 },
323 },323 },
324 .dragonfly => return .{324 .dragonfly => return .{
325 .semver = .{325 .semver = .{
326 .min = .{ .major = 5, .minor = 8 },326 .min = .{ .major = 5, .minor = 8 },
327 .max = .{ .major = 6, .minor = 0 },327 .max = .{ .major = 6, .minor = 4 },
328 },328 },
329 },329 },
330 .solaris => return .{330 .solaris => return .{
src/link/Elf.zig+6-12
...@@ -3058,27 +3058,22 @@ const CsuObjects = struct {...@@ -3058,27 +3058,22 @@ const CsuObjects = struct {
30583058
3059 var result: CsuObjects = .{};3059 var result: CsuObjects = .{};
30603060
3061 // TODO: https://github.com/ziglang/zig/issues/46293061 // Flatten crt cases.
3062 // - use inline enum type3062 const mode: enum {
3063 // - reduce to enum-literals for values
3064 const Mode = enum {
3065 dynamic_lib,3063 dynamic_lib,
3066 dynamic_exe,3064 dynamic_exe,
3067 dynamic_pie,3065 dynamic_pie,
3068 static_exe,3066 static_exe,
3069 static_pie,3067 static_pie,
3070 };3068 } = switch (link_options.output_mode) {
3071
3072 // Flatten crt case types.
3073 const mode: Mode = switch (link_options.output_mode) {
3074 .Obj => return CsuObjects{},3069 .Obj => return CsuObjects{},
3075 .Lib => switch (link_options.link_mode) {3070 .Lib => switch (link_options.link_mode) {
3076 .Dynamic => Mode.dynamic_lib,3071 .Dynamic => .dynamic_lib,
3077 .Static => return CsuObjects{},3072 .Static => return CsuObjects{},
3078 },3073 },
3079 .Exe => switch (link_options.link_mode) {3074 .Exe => switch (link_options.link_mode) {
3080 .Dynamic => if (link_options.pie) Mode.dynamic_pie else Mode.dynamic_exe,3075 .Dynamic => if (link_options.pie) .dynamic_pie else .dynamic_exe,
3081 .Static => if (link_options.pie) Mode.static_pie else Mode.static_exe,3076 .Static => if (link_options.pie) .static_pie else .static_exe,
3082 },3077 },
3083 };3078 };
30843079
...@@ -3108,7 +3103,6 @@ const CsuObjects = struct {...@@ -3108,7 +3103,6 @@ const CsuObjects = struct {
3108 // hosted-glibc provides crtbegin/end objects in platform/compiler-specific dirs3103 // hosted-glibc provides crtbegin/end objects in platform/compiler-specific dirs
3109 // and they are not known at comptime. For now null-out crtbegin/end objects;3104 // and they are not known at comptime. For now null-out crtbegin/end objects;
3110 // there is no feature loss, zig has never linked those objects in before.3105 // there is no feature loss, zig has never linked those objects in before.
3111 // TODO: probe for paths, ie. `cc -print-file-name`
3112 result.crtbegin = null;3106 result.crtbegin = null;
3113 result.crtend = null;3107 result.crtend = null;
3114 } else {3108 } else {