From 4d735a663154321e9f2fb609146fae979ab86ee9 Mon Sep 17 00:00:00 2001 From: Michael Chiu Date: Sun, 26 Jan 2025 21:29:13 -0800 Subject: [PATCH 1/2] Add FreeBSD errnos and open options --- Sources/System/Errno.swift | 40 ++++++++++++++++++++++-- Sources/System/FileDescriptor.swift | 33 +++++++++++++++++-- Sources/System/Internals/Constants.swift | 31 +++++++++++++++--- Tests/SystemTests/ErrnoTest.swift | 11 ++++++- Tests/SystemTests/FileTypesTest.swift | 11 +++++-- 5 files changed, 114 insertions(+), 12 deletions(-) diff --git a/Sources/System/Errno.swift b/Sources/System/Errno.swift index d9fe1c8..e88b96d 100644 --- a/Sources/System/Errno.swift +++ b/Sources/System/Errno.swift @@ -1067,7 +1067,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { public static var ENOSYS: Errno { noFunction } // BSD -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) /// Inappropriate file type or format. /// /// The file was the wrong type for the operation, @@ -1082,7 +1082,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { public static var EFTYPE: Errno { badFileTypeOrFormat } #endif -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) /// Authentication error. /// /// The authentication ticket used to mount an NFS file system was invalid. @@ -1253,7 +1253,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "illegalByteSequence") public static var EILSEQ: Errno { illegalByteSequence } -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) /// Attribute not found. /// /// The specified extended attribute doesn't exist. @@ -1459,6 +1459,38 @@ extension Errno { public static var EOWNERDEAD: Errno { previousOwnerDied } #endif +#if os(FreeBSD) + /// Capabilities insufficient. + /// + /// The corresponding C error is `ENOTCAPABLE`. + @_alwaysEmitIntoClient + public static var notCapable: Errno { .init(rawValue: _ENOTCAPABLE) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "notCapable") + public static var ENOTCAPABLE: Errno { notCapable } + + /// Not permitted in capability mode. + /// + /// The corresponding C error is `ECAPMODE`. + @_alwaysEmitIntoClient + public static var capabilityMode: Errno { .init(rawValue: _ECAPMODE) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "capabilityMode") + public static var ECAPMODE: Errno { capabilityMode } + + /// Integrity check failed. + /// + /// The corresponding C error is `EINTEGRITY`. + @_alwaysEmitIntoClient + public static var integrityCheckFailed: Errno { .init(rawValue: _EINTEGRITY) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "integrityCheckFailed") + public static var EINTEGRITY: Errno { integrityCheckFailed } +#endif + #if SYSTEM_PACKAGE_DARWIN /// Interface output queue is full. /// @@ -1469,7 +1501,9 @@ extension Errno { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "outputQueueFull") public static var EQFULL: Errno { outputQueueFull } +#endif +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) /// The largest valid error. /// /// This value is the largest valid value diff --git a/Sources/System/FileDescriptor.swift b/Sources/System/FileDescriptor.swift index d7b931e..c9953e8 100644 --- a/Sources/System/FileDescriptor.swift +++ b/Sources/System/FileDescriptor.swift @@ -182,7 +182,7 @@ extension FileDescriptor { @available(*, unavailable, renamed: "exclusiveCreate") public static var O_EXCL: OpenOptions { exclusiveCreate } -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) /// Indicates that opening the file /// atomically obtains a shared lock on the file. /// @@ -250,6 +250,22 @@ extension FileDescriptor { public static var O_DIRECTORY: OpenOptions { directory } #endif +#if os(FreeBSD) + /// Indicates that each write operation is synchronous. + /// + /// If this option is specified, + /// each time you write to the file, + /// the new data is written immediately and synchronously to the disk. + /// + /// The corresponding C constant is `O_SYNC`. + @_alwaysEmitIntoClient + public static var sync: OpenOptions { .init(rawValue: _O_SYNC) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "sync") + public static var O_SYNC: OpenOptions { sync } +#endif + #if SYSTEM_PACKAGE_DARWIN /// Indicates that opening the file /// opens symbolic links instead of following them. @@ -354,7 +370,7 @@ extension FileDescriptor { // TODO: These are available on some versions of Linux with appropriate // macro defines. -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) /// Indicates that the offset should be set /// to the next hole after the specified number of bytes. /// @@ -456,6 +472,19 @@ extension FileDescriptor.OpenOptions (.truncate, ".truncate"), (.exclusiveCreate, ".exclusiveCreate"), ] +#elseif os(FreeBSD) + let descriptions: [(Element, StaticString)] = [ + (.nonBlocking, ".nonBlocking"), + (.append, ".append"), + (.create, ".create"), + (.truncate, ".truncate"), + (.exclusiveCreate, ".exclusiveCreate"), + (.sharedLock, ".sharedLock"), + (.exclusiveLock, ".exclusiveLock"), + (.sync, ".sync"), + (.noFollow, ".noFollow"), + (.closeOnExec, ".closeOnExec") + ] #else let descriptions: [(Element, StaticString)] = [ (.nonBlocking, ".nonBlocking"), diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 171d01d..d8cbdcb 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -359,7 +359,7 @@ internal var _ENOLCK: CInt { ENOLCK } @_alwaysEmitIntoClient internal var _ENOSYS: CInt { ENOSYS } -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) @_alwaysEmitIntoClient internal var _EFTYPE: CInt { EFTYPE } @@ -368,7 +368,9 @@ internal var _EAUTH: CInt { EAUTH } @_alwaysEmitIntoClient internal var _ENEEDAUTH: CInt { ENEEDAUTH } +#endif +#if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient internal var _EPWROFF: CInt { EPWROFF } @@ -409,7 +411,7 @@ internal var _ENOMSG: CInt { ENOMSG } @_alwaysEmitIntoClient internal var _EILSEQ: CInt { EILSEQ } -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) @_alwaysEmitIntoClient internal var _ENOATTR: CInt { ENOATTR } #endif @@ -471,10 +473,23 @@ internal var _ENOTRECOVERABLE: CInt { ENOTRECOVERABLE } internal var _EOWNERDEAD: CInt { EOWNERDEAD } #endif +#if os(FreeBSD) +@_alwaysEmitIntoClient +internal var _ENOTCAPABLE: CInt { ENOTCAPABLE } + +@_alwaysEmitIntoClient +internal var _ECAPMODE: CInt { ECAPMODE } + +@_alwaysEmitIntoClient +internal var _EINTEGRITY: CInt { EINTEGRITY } +#endif + #if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient internal var _EQFULL: CInt { EQFULL } +#endif +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) @_alwaysEmitIntoClient internal var _ELAST: CInt { ELAST } #endif @@ -524,7 +539,7 @@ internal var _O_APPEND: CInt { #endif } -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) @_alwaysEmitIntoClient internal var _O_SHLOCK: CInt { O_SHLOCK } @@ -543,6 +558,14 @@ internal var _O_ASYNC: CInt { O_ASYNC } internal var _O_NOFOLLOW: CInt { O_NOFOLLOW } #endif +#if os(FreeBSD) +@_alwaysEmitIntoClient +internal var _O_FSYNC: CInt { O_FSYNC } + +@_alwaysEmitIntoClient +internal var _O_SYNC: CInt { O_SYNC } +#endif + @_alwaysEmitIntoClient internal var _O_CREAT: CInt { #if os(WASI) @@ -609,7 +632,7 @@ internal var _SEEK_CUR: CInt { SEEK_CUR } @_alwaysEmitIntoClient internal var _SEEK_END: CInt { SEEK_END } -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) @_alwaysEmitIntoClient internal var _SEEK_HOLE: CInt { SEEK_HOLE } diff --git a/Tests/SystemTests/ErrnoTest.swift b/Tests/SystemTests/ErrnoTest.swift index 3724e14..da09657 100644 --- a/Tests/SystemTests/ErrnoTest.swift +++ b/Tests/SystemTests/ErrnoTest.swift @@ -131,10 +131,13 @@ final class ErrnoTest: XCTestCase { XCTAssert(ENOLCK == Errno.noLocks.rawValue) XCTAssert(ENOSYS == Errno.noFunction.rawValue) -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) XCTAssert(EFTYPE == Errno.badFileTypeOrFormat.rawValue) XCTAssert(EAUTH == Errno.authenticationError.rawValue) XCTAssert(ENEEDAUTH == Errno.needAuthenticator.rawValue) +#endif + +#if SYSTEM_PACKAGE_DARWIN XCTAssert(EPWROFF == Errno.devicePowerIsOff.rawValue) XCTAssert(EDEVERR == Errno.deviceError.rawValue) #endif @@ -196,6 +199,12 @@ final class ErrnoTest: XCTestCase { XCTAssert(EOWNERDEAD == Errno.previousOwnerDied.rawValue) #endif +#if os(FreeBSD) + XCTAssert(ENOTCAPABLE == Errno.notCapable.rawValue) + XCTAssert(ECAPMODE == Errno.capabilityMode.rawValue) + XCTAssert(EINTEGRITY == Errno.integrityCheckFailed.rawValue) +#endif + #if SYSTEM_PACKAGE_DARWIN XCTAssert(EQFULL == Errno.outputQueueFull.rawValue) XCTAssert(ELAST == Errno.lastErrnoValue.rawValue) diff --git a/Tests/SystemTests/FileTypesTest.swift b/Tests/SystemTests/FileTypesTest.swift index 620cfd7..58ceac1 100644 --- a/Tests/SystemTests/FileTypesTest.swift +++ b/Tests/SystemTests/FileTypesTest.swift @@ -42,18 +42,25 @@ final class FileDescriptorTest: XCTestCase { #endif // BSD only -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) XCTAssertEqual(O_SHLOCK, FileDescriptor.OpenOptions.sharedLock.rawValue) XCTAssertEqual(O_EXLOCK, FileDescriptor.OpenOptions.exclusiveLock.rawValue) +#endif + +#if SYSTEM_PACKAGE_DARWIN XCTAssertEqual(O_SYMLINK, FileDescriptor.OpenOptions.symlink.rawValue) XCTAssertEqual(O_EVTONLY, FileDescriptor.OpenOptions.eventOnly.rawValue) #endif +#if os(FreeBSD) + XCTAssertEqual(O_SYNC, FileDescriptor.OpenOptions.sync.rawValue) +#endif + XCTAssertEqual(SEEK_SET, FileDescriptor.SeekOrigin.start.rawValue) XCTAssertEqual(SEEK_CUR, FileDescriptor.SeekOrigin.current.rawValue) XCTAssertEqual(SEEK_END, FileDescriptor.SeekOrigin.end.rawValue) -#if SYSTEM_PACKAGE_DARWIN +#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) XCTAssertEqual(SEEK_HOLE, FileDescriptor.SeekOrigin.nextHole.rawValue) XCTAssertEqual(SEEK_DATA, FileDescriptor.SeekOrigin.nextData.rawValue) #endif From df7341ee2b553b09dee6f82bb3b0c84e9b6f0441 Mon Sep 17 00:00:00 2001 From: Michael Chiu Date: Sun, 26 Jan 2025 21:29:19 -0800 Subject: [PATCH 2/2] fix cmake x86_64 build --- cmake/modules/SwiftSupport.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake index 8f1e2c4..19cf022 100644 --- a/cmake/modules/SwiftSupport.cmake +++ b/cmake/modules/SwiftSupport.cmake @@ -36,7 +36,7 @@ function(get_swift_host_arch result_var_name) elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a") set("${result_var_name}" "armv7" PARENT_SCOPE) elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64") - set("${result_var_name}" "amd64" PARENT_SCOPE) + set("${result_var_name}" "x86_64" PARENT_SCOPE) elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64") set("${result_var_name}" "x86_64" PARENT_SCOPE) elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")