Skip to content

Commit a342014

Browse files
authored
Merge pull request #220 from glessard/1.4.2
[1.4.x] Support for FreeBSD and OpenBSD
2 parents 3a1b4a8 + 12573bf commit a342014

12 files changed

+171
-36
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
/.swiftpm
33
/.build
44
/Packages
5-
swift-system.xcodeproj
5+
/*.xcodeproj
66
xcuserdata/
77
.*.sw?
8+
/.swiftpm
89
.docc-build

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cmake_minimum_required(VERSION 3.16.0)
1111
project(SwiftSystem
1212
LANGUAGES C Swift)
1313

14+
include(GNUInstallDirs)
15+
1416
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
1517

1618
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

Sources/CSystem/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ See https://swift.org/LICENSE.txt for license information
99

1010
add_library(CSystem INTERFACE)
1111
target_include_directories(CSystem INTERFACE
12-
include)
13-
12+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
13+
$<INSTALL_INTERFACE:include/CSystem>)
1414

1515
install(FILES
1616
include/CSystemLinux.h

Sources/System/Errno.swift

+40-6
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
10671067
public static var ENOSYS: Errno { noFunction }
10681068

10691069
// BSD
1070-
#if SYSTEM_PACKAGE_DARWIN
1070+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
10711071
/// Inappropriate file type or format.
10721072
///
10731073
/// The file was the wrong type for the operation,
@@ -1082,7 +1082,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
10821082
public static var EFTYPE: Errno { badFileTypeOrFormat }
10831083
#endif
10841084

1085-
#if SYSTEM_PACKAGE_DARWIN
1085+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
10861086
/// Authentication error.
10871087
///
10881088
/// The authentication ticket used to mount an NFS file system was invalid.
@@ -1253,7 +1253,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
12531253
@available(*, unavailable, renamed: "illegalByteSequence")
12541254
public static var EILSEQ: Errno { illegalByteSequence }
12551255

1256-
#if SYSTEM_PACKAGE_DARWIN
1256+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
12571257
/// Attribute not found.
12581258
///
12591259
/// The specified extended attribute doesn't exist.
@@ -1294,7 +1294,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
12941294
@available(*, unavailable, renamed: "multiHop")
12951295
public static var EMULTIHOP: Errno { multiHop }
12961296

1297-
#if !os(WASI)
1297+
#if !os(WASI) && !os(FreeBSD)
12981298
/// No message available.
12991299
///
13001300
/// No message was available to be received by the requested operation.
@@ -1320,7 +1320,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
13201320
@available(*, unavailable, renamed: "noLink")
13211321
public static var ENOLINK: Errno { noLink }
13221322

1323-
#if !os(WASI)
1323+
#if !os(WASI) && !os(FreeBSD)
13241324
/// Reserved.
13251325
///
13261326
/// This error is reserved for future use.
@@ -1361,7 +1361,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
13611361
@available(*, unavailable, renamed: "protocolError")
13621362
public static var EPROTO: Errno { protocolError }
13631363

1364-
#if !os(OpenBSD) && !os(WASI)
1364+
#if !os(OpenBSD) && !os(WASI) && !os(FreeBSD)
13651365
/// Reserved.
13661366
///
13671367
/// This error is reserved for future use.
@@ -1459,6 +1459,38 @@ extension Errno {
14591459
public static var EOWNERDEAD: Errno { previousOwnerDied }
14601460
#endif
14611461

1462+
#if os(FreeBSD)
1463+
/// Capabilities insufficient.
1464+
///
1465+
/// The corresponding C error is `ENOTCAPABLE`.
1466+
@_alwaysEmitIntoClient
1467+
public static var notCapable: Errno { .init(rawValue: _ENOTCAPABLE) }
1468+
1469+
@_alwaysEmitIntoClient
1470+
@available(*, unavailable, renamed: "notCapable")
1471+
public static var ENOTCAPABLE: Errno { notCapable }
1472+
1473+
/// Not permitted in capability mode.
1474+
///
1475+
/// The corresponding C error is `ECAPMODE`.
1476+
@_alwaysEmitIntoClient
1477+
public static var capabilityMode: Errno { .init(rawValue: _ECAPMODE) }
1478+
1479+
@_alwaysEmitIntoClient
1480+
@available(*, unavailable, renamed: "capabilityMode")
1481+
public static var ECAPMODE: Errno { capabilityMode }
1482+
1483+
/// Integrity check failed.
1484+
///
1485+
/// The corresponding C error is `EINTEGRITY`.
1486+
@_alwaysEmitIntoClient
1487+
public static var integrityCheckFailed: Errno { .init(rawValue: _EINTEGRITY) }
1488+
1489+
@_alwaysEmitIntoClient
1490+
@available(*, unavailable, renamed: "integrityCheckFailed")
1491+
public static var EINTEGRITY: Errno { integrityCheckFailed }
1492+
#endif
1493+
14621494
#if SYSTEM_PACKAGE_DARWIN
14631495
/// Interface output queue is full.
14641496
///
@@ -1469,7 +1501,9 @@ extension Errno {
14691501
@_alwaysEmitIntoClient
14701502
@available(*, unavailable, renamed: "outputQueueFull")
14711503
public static var EQFULL: Errno { outputQueueFull }
1504+
#endif
14721505

1506+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
14731507
/// The largest valid error.
14741508
///
14751509
/// This value is the largest valid value

Sources/System/FileDescriptor.swift

+31-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension FileDescriptor {
182182
@available(*, unavailable, renamed: "exclusiveCreate")
183183
public static var O_EXCL: OpenOptions { exclusiveCreate }
184184

185-
#if SYSTEM_PACKAGE_DARWIN
185+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
186186
/// Indicates that opening the file
187187
/// atomically obtains a shared lock on the file.
188188
///
@@ -250,6 +250,22 @@ extension FileDescriptor {
250250
public static var O_DIRECTORY: OpenOptions { directory }
251251
#endif
252252

253+
#if os(FreeBSD)
254+
/// Indicates that each write operation is synchronous.
255+
///
256+
/// If this option is specified,
257+
/// each time you write to the file,
258+
/// the new data is written immediately and synchronously to the disk.
259+
///
260+
/// The corresponding C constant is `O_SYNC`.
261+
@_alwaysEmitIntoClient
262+
public static var sync: OpenOptions { .init(rawValue: _O_SYNC) }
263+
264+
@_alwaysEmitIntoClient
265+
@available(*, unavailable, renamed: "sync")
266+
public static var O_SYNC: OpenOptions { sync }
267+
#endif
268+
253269
#if SYSTEM_PACKAGE_DARWIN
254270
/// Indicates that opening the file
255271
/// opens symbolic links instead of following them.
@@ -354,7 +370,7 @@ extension FileDescriptor {
354370

355371
// TODO: These are available on some versions of Linux with appropriate
356372
// macro defines.
357-
#if SYSTEM_PACKAGE_DARWIN
373+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
358374
/// Indicates that the offset should be set
359375
/// to the next hole after the specified number of bytes.
360376
///
@@ -456,6 +472,19 @@ extension FileDescriptor.OpenOptions
456472
(.truncate, ".truncate"),
457473
(.exclusiveCreate, ".exclusiveCreate"),
458474
]
475+
#elseif os(FreeBSD)
476+
let descriptions: [(Element, StaticString)] = [
477+
(.nonBlocking, ".nonBlocking"),
478+
(.append, ".append"),
479+
(.create, ".create"),
480+
(.truncate, ".truncate"),
481+
(.exclusiveCreate, ".exclusiveCreate"),
482+
(.sharedLock, ".sharedLock"),
483+
(.exclusiveLock, ".exclusiveLock"),
484+
(.sync, ".sync"),
485+
(.noFollow, ".noFollow"),
486+
(.closeOnExec, ".closeOnExec")
487+
]
459488
#else
460489
let descriptions: [(Element, StaticString)] = [
461490
(.nonBlocking, ".nonBlocking"),

Sources/System/Internals/Constants.swift

+30-7
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ internal var _ENOLCK: CInt { ENOLCK }
359359
@_alwaysEmitIntoClient
360360
internal var _ENOSYS: CInt { ENOSYS }
361361

362-
#if SYSTEM_PACKAGE_DARWIN
362+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
363363
@_alwaysEmitIntoClient
364364
internal var _EFTYPE: CInt { EFTYPE }
365365

@@ -368,7 +368,9 @@ internal var _EAUTH: CInt { EAUTH }
368368

369369
@_alwaysEmitIntoClient
370370
internal var _ENEEDAUTH: CInt { ENEEDAUTH }
371+
#endif
371372

373+
#if SYSTEM_PACKAGE_DARWIN
372374
@_alwaysEmitIntoClient
373375
internal var _EPWROFF: CInt { EPWROFF }
374376

@@ -409,7 +411,7 @@ internal var _ENOMSG: CInt { ENOMSG }
409411
@_alwaysEmitIntoClient
410412
internal var _EILSEQ: CInt { EILSEQ }
411413

412-
#if SYSTEM_PACKAGE_DARWIN
414+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
413415
@_alwaysEmitIntoClient
414416
internal var _ENOATTR: CInt { ENOATTR }
415417
#endif
@@ -422,15 +424,15 @@ internal var _EBADMSG: CInt { EBADMSG }
422424
@_alwaysEmitIntoClient
423425
internal var _EMULTIHOP: CInt { EMULTIHOP }
424426

425-
#if !os(WASI)
427+
#if !os(WASI) && !os(FreeBSD)
426428
@_alwaysEmitIntoClient
427429
internal var _ENODATA: CInt { ENODATA }
428430
#endif
429431

430432
@_alwaysEmitIntoClient
431433
internal var _ENOLINK: CInt { ENOLINK }
432434

433-
#if !os(WASI)
435+
#if !os(WASI) && !os(FreeBSD)
434436
@_alwaysEmitIntoClient
435437
internal var _ENOSR: CInt { ENOSR }
436438

@@ -442,7 +444,7 @@ internal var _ENOSTR: CInt { ENOSTR }
442444
@_alwaysEmitIntoClient
443445
internal var _EPROTO: CInt { EPROTO }
444446

445-
#if !os(OpenBSD) && !os(WASI)
447+
#if !os(OpenBSD) && !os(WASI) && !os(FreeBSD)
446448
@_alwaysEmitIntoClient
447449
internal var _ETIME: CInt { ETIME }
448450
#endif
@@ -471,10 +473,23 @@ internal var _ENOTRECOVERABLE: CInt { ENOTRECOVERABLE }
471473
internal var _EOWNERDEAD: CInt { EOWNERDEAD }
472474
#endif
473475

476+
#if os(FreeBSD)
477+
@_alwaysEmitIntoClient
478+
internal var _ENOTCAPABLE: CInt { ENOTCAPABLE }
479+
480+
@_alwaysEmitIntoClient
481+
internal var _ECAPMODE: CInt { ECAPMODE }
482+
483+
@_alwaysEmitIntoClient
484+
internal var _EINTEGRITY: CInt { EINTEGRITY }
485+
#endif
486+
474487
#if SYSTEM_PACKAGE_DARWIN
475488
@_alwaysEmitIntoClient
476489
internal var _EQFULL: CInt { EQFULL }
490+
#endif
477491

492+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
478493
@_alwaysEmitIntoClient
479494
internal var _ELAST: CInt { ELAST }
480495
#endif
@@ -524,7 +539,7 @@ internal var _O_APPEND: CInt {
524539
#endif
525540
}
526541

527-
#if SYSTEM_PACKAGE_DARWIN
542+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
528543
@_alwaysEmitIntoClient
529544
internal var _O_SHLOCK: CInt { O_SHLOCK }
530545

@@ -543,6 +558,14 @@ internal var _O_ASYNC: CInt { O_ASYNC }
543558
internal var _O_NOFOLLOW: CInt { O_NOFOLLOW }
544559
#endif
545560

561+
#if os(FreeBSD)
562+
@_alwaysEmitIntoClient
563+
internal var _O_FSYNC: CInt { O_FSYNC }
564+
565+
@_alwaysEmitIntoClient
566+
internal var _O_SYNC: CInt { O_SYNC }
567+
#endif
568+
546569
@_alwaysEmitIntoClient
547570
internal var _O_CREAT: CInt {
548571
#if os(WASI)
@@ -609,7 +632,7 @@ internal var _SEEK_CUR: CInt { SEEK_CUR }
609632
@_alwaysEmitIntoClient
610633
internal var _SEEK_END: CInt { SEEK_END }
611634

612-
#if SYSTEM_PACKAGE_DARWIN
635+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
613636
@_alwaysEmitIntoClient
614637
internal var _SEEK_HOLE: CInt { SEEK_HOLE }
615638

Sources/System/Internals/RawBuffer.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ extension _RawBuffer {
8080
internal static func create(minimumCapacity: Int) -> Storage {
8181
Storage.create(
8282
minimumCapacity: minimumCapacity,
83-
makingHeaderWith: { $0.capacity }
83+
makingHeaderWith: {
84+
#if os(OpenBSD)
85+
minimumCapacity
86+
#else
87+
$0.capacity
88+
#endif
89+
}
8490
) as! Storage
8591
}
8692
}

Sources/System/Internals/Syscalls.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ internal func system_confstr(
191191
internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR
192192
internal let SYSTEM_DT_DIR = DT_DIR
193193
internal typealias system_dirent = dirent
194-
#if os(Linux) || os(Android)
194+
#if os(Linux) || os(Android) || os(FreeBSD) || os(OpenBSD)
195195
internal typealias system_DIRPtr = OpaquePointer
196196
#else
197197
internal typealias system_DIRPtr = UnsafeMutablePointer<DIR>

Sources/System/Internals/WindowsSyscallAdapters.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ internal func pwrite(
187187
internal func pipe(
188188
_ fds: UnsafeMutablePointer<Int32>, bytesReserved: UInt32 = 4096
189189
) -> CInt {
190-
  return _pipe(fds, bytesReserved, _O_BINARY | _O_NOINHERIT);
190+
return _pipe(fds, bytesReserved, _O_BINARY | _O_NOINHERIT);
191191
}
192192

193193
@inline(__always)

Tests/SystemTests/ErrnoTest.swift

+16-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ final class ErrnoTest: XCTestCase {
131131
XCTAssert(ENOLCK == Errno.noLocks.rawValue)
132132
XCTAssert(ENOSYS == Errno.noFunction.rawValue)
133133

134-
#if SYSTEM_PACKAGE_DARWIN
134+
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
135135
XCTAssert(EFTYPE == Errno.badFileTypeOrFormat.rawValue)
136136
XCTAssert(EAUTH == Errno.authenticationError.rawValue)
137137
XCTAssert(ENEEDAUTH == Errno.needAuthenticator.rawValue)
138+
#endif
139+
140+
#if SYSTEM_PACKAGE_DARWIN
138141
XCTAssert(EPWROFF == Errno.devicePowerIsOff.rawValue)
139142
XCTAssert(EDEVERR == Errno.deviceError.rawValue)
140143
#endif
@@ -164,13 +167,17 @@ final class ErrnoTest: XCTestCase {
164167
#if !os(Windows)
165168
XCTAssert(EBADMSG == Errno.badMessage.rawValue)
166169
XCTAssert(EMULTIHOP == Errno.multiHop.rawValue)
167-
XCTAssert(ENODATA == Errno.noData.rawValue)
168170
XCTAssert(ENOLINK == Errno.noLink.rawValue)
171+
XCTAssert(EPROTO == Errno.protocolError.rawValue)
172+
#endif
173+
174+
#if !os(Windows) && !os(FreeBSD)
175+
XCTAssert(ENODATA == Errno.noData.rawValue)
169176
XCTAssert(ENOSR == Errno.noStreamResources.rawValue)
170177
XCTAssert(ENOSTR == Errno.notStream.rawValue)
171-
XCTAssert(EPROTO == Errno.protocolError.rawValue)
172178
XCTAssert(ETIME == Errno.timeout.rawValue)
173179
#endif
180+
174181
XCTAssert(EOPNOTSUPP == Errno.notSupportedOnSocket.rawValue)
175182

176183
// From headers but not man page
@@ -192,6 +199,12 @@ final class ErrnoTest: XCTestCase {
192199
XCTAssert(EOWNERDEAD == Errno.previousOwnerDied.rawValue)
193200
#endif
194201

202+
#if os(FreeBSD)
203+
XCTAssert(ENOTCAPABLE == Errno.notCapable.rawValue)
204+
XCTAssert(ECAPMODE == Errno.capabilityMode.rawValue)
205+
XCTAssert(EINTEGRITY == Errno.integrityCheckFailed.rawValue)
206+
#endif
207+
195208
#if SYSTEM_PACKAGE_DARWIN
196209
XCTAssert(EQFULL == Errno.outputQueueFull.rawValue)
197210
XCTAssert(ELAST == Errno.lastErrnoValue.rawValue)

0 commit comments

Comments
 (0)