Skip to content

Commit d0011ab

Browse files
authored
Merge pull request #3003 from 3405691582/OpenBSDOmnibus
Omnibus to get a clean OpenBSD build.
2 parents 62c50ad + 1f0a55f commit d0011ab

File tree

15 files changed

+162
-17
lines changed

15 files changed

+162
-17
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

+62
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ static inline void _CFSetProgramNameFromPath(const char *path) {
119119
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
120120
}
121121

122+
#if TARGET_OS_BSD && defined(__OpenBSD__)
123+
#include <sys/types.h>
124+
#include <sys/sysctl.h>
125+
#include <sys/exec.h>
126+
#endif
127+
122128
const char *_CFProcessPath(void) {
123129
if (__CFProcessPath) return __CFProcessPath;
124130

@@ -175,6 +181,53 @@ const char *_CFProcessPath(void) {
175181
}
176182
return __CFProcessPath;
177183
#else // TARGET_OS_BSD
184+
char *argv0 = NULL;
185+
186+
// Get argv[0].
187+
#if defined(__OpenBSD__)
188+
int mib[2] = {CTL_VM, VM_PSSTRINGS};
189+
struct _ps_strings _ps;
190+
size_t len = sizeof(_ps);
191+
192+
if (sysctl(mib, 2, &_ps, &len, NULL, 0) != -1) {
193+
struct ps_strings *ps = _ps.val;
194+
char *res = realpath(ps->ps_argvstr[0], NULL);
195+
argv0 = res? res: strdup(ps->ps_argvstr[0]);
196+
}
197+
#endif
198+
199+
if (!__CFProcessIsRestricted() && argv0 && argv0[0] == '/') {
200+
_CFSetProgramNameFromPath(argv0);
201+
free(argv0);
202+
return __CFProcessPath;
203+
}
204+
205+
// Search PATH.
206+
if (argv0) {
207+
char *paths = getenv("PATH");
208+
char *p = NULL;
209+
while ((p = strsep(&paths, ":")) != NULL) {
210+
char pp[PATH_MAX];
211+
int l = snprintf(pp, PATH_MAX, "%s/%s", p, argv0);
212+
if (l >= PATH_MAX) {
213+
continue;
214+
}
215+
char *res = realpath(pp, NULL);
216+
if (!res) {
217+
continue;
218+
}
219+
if (!__CFProcessIsRestricted() && access(res, X_OK) == 0) {
220+
_CFSetProgramNameFromPath(res);
221+
free(argv0);
222+
free(res);
223+
return __CFProcessPath;
224+
}
225+
free(res);
226+
}
227+
free(argv0);
228+
}
229+
230+
// See if the shell will help.
178231
if (!__CFProcessIsRestricted()) {
179232
char *path = getenv("_");
180233
if (path != NULL) {
@@ -1574,6 +1627,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
15741627
return 0;
15751628
#elif TARGET_OS_LINUX
15761629
return pthread_setname_np(thread, name);
1630+
#elif TARGET_OS_BSD
1631+
pthread_set_name_np(thread, name);
1632+
return 0;
15771633
#endif
15781634
}
15791635

@@ -1593,6 +1649,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
15931649
return 0;
15941650
#elif TARGET_OS_LINUX
15951651
return pthread_getname_np(pthread_self(), buf, length);
1652+
#elif TARGET_OS_BSD
1653+
pthread_get_name_np(pthread_self(), buf, length);
1654+
return 0;
15961655
#elif TARGET_OS_WIN32
15971656
*buf = '\0';
15981657

@@ -1630,6 +1689,9 @@ CF_EXPORT char **_CFEnviron(void) {
16301689
#elif TARGET_OS_WIN32
16311690
return _environ;
16321691
#else
1692+
#if TARGET_OS_BSD
1693+
extern char **environ;
1694+
#endif
16331695
return environ;
16341696
#endif
16351697
}

CoreFoundation/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ add_compile_options($<$<COMPILE_LANGUAGE:C>:-fblocks>)
5151
if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
5252
add_compile_options($<$<COMPILE_LANGUAGE:C>:/EHsc>)
5353
else()
54-
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fexceptions>)
54+
if(NOT CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
55+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fexceptions>)
56+
endif()
5557
endif()
5658

5759
if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")

CoreFoundation/PlugIn.subproj/CFBundle_Binary.c

+4
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,11 @@ CF_PRIVATE Boolean _CFBundleDlfcnCheckLoaded(CFBundleRef bundle) {
470470
char buff[CFMaxPathSize];
471471

472472
if (executableURL && CFURLGetFileSystemRepresentation(executableURL, true, (uint8_t *)buff, CFMaxPathSize)) {
473+
#if !defined(__OpenBSD__)
473474
int mode = RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD | RTLD_FIRST;
475+
#else
476+
int mode = RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST;
477+
#endif
474478
void *handle = dlopen(buff, mode);
475479
if (handle) {
476480
if (!bundle->_handleCookie) {

CoreFoundation/PlugIn.subproj/CFBundle_Main.c

+4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ static CFBundleRef _CFBundleGetMainBundleAlreadyLocked(void) {
107107
// get cookie for already-loaded main bundle
108108
#if defined(BINARY_SUPPORT_DLFCN)
109109
if (!_mainBundle->_handleCookie) {
110+
#if !defined(__OpenBSD__)
110111
_mainBundle->_handleCookie = dlopen(NULL, RTLD_NOLOAD | RTLD_FIRST);
112+
#else
113+
_mainBundle->_handleCookie = dlopen(NULL, RTLD_FIRST);
114+
#endif
111115
#if LOG_BUNDLE_LOAD
112116
printf("main bundle %p getting handle %p\n", _mainBundle, _mainBundle->_handleCookie);
113117
#endif /* LOG_BUNDLE_LOAD */

CoreFoundation/Stream.subproj/CFStream.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -1675,11 +1675,7 @@ static void _perform(void* info)
16751675

16761676
static void* _legacyStreamRunLoop_workThread(void* arg)
16771677
{
1678-
#if TARGET_OS_LINUX
1679-
pthread_setname_np(pthread_self(), "com.apple.CFStream.LegacyThread");
1680-
#else
1681-
pthread_setname_np("com.apple.CFStream.LegacyThread");
1682-
#endif
1678+
_CFThreadSetName(pthread_self(), "com.apple.CFStream.LegacyThread");
16831679
sLegacyRL = CFRunLoopGetCurrent();
16841680

16851681
#if defined(LOG_STREAM)

Sources/Foundation/FileManager+POSIX.swift

+26
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ extension FileManager {
7070
}
7171
urls = mountPoints(statBuf, Int(fsCount))
7272
}
73+
#elseif os(OpenBSD)
74+
func mountPoints(_ statBufs: UnsafePointer<statfs>, _ fsCount: Int) -> [URL] {
75+
var urls: [URL] = []
76+
77+
for fsIndex in 0..<fsCount {
78+
var fs = statBufs.advanced(by: fsIndex).pointee
79+
80+
let mountPoint = withUnsafePointer(to: &fs.f_mntonname.0) { (ptr: UnsafePointer<Int8>) -> String in
81+
return string(withFileSystemRepresentation: ptr, length: strlen(ptr))
82+
}
83+
urls.append(URL(fileURLWithPath: mountPoint, isDirectory: true))
84+
}
85+
return urls
86+
}
87+
88+
var fsCount = getfsstat(nil, 0, MNT_WAIT)
89+
guard fsCount > 0 else {
90+
return nil
91+
}
92+
let statBuf = UnsafeMutablePointer<statfs>.allocate(capacity: Int(fsCount))
93+
defer { statBuf.deallocate() }
94+
fsCount = getfsstat(statBuf, Int(fsCount) * MemoryLayout<statfs>.stride, MNT_WAIT)
95+
guard fsCount > 0 else {
96+
return nil
97+
}
98+
urls = mountPoints(statBuf, Int(fsCount))
7399
#else
74100
#error("Requires a platform-specific implementation")
75101
#endif

Sources/Foundation/Host.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ open class Host: NSObject {
183183
let family = ifa_addr.pointee.sa_family
184184
if family == sa_family_t(AF_INET) || family == sa_family_t(AF_INET6) {
185185
let sa_len: socklen_t = socklen_t((family == sa_family_t(AF_INET6)) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
186-
if getnameinfo(ifa_addr, sa_len, address, socklen_t(NI_MAXHOST), nil, 0, NI_NUMERICHOST) == 0 {
186+
#if os(OpenBSD)
187+
let hostlen = size_t(NI_MAXHOST)
188+
#else
189+
let hostlen = socklen_t(NI_MAXHOST)
190+
#endif
191+
if getnameinfo(ifa_addr, sa_len, address, hostlen, nil, 0, NI_NUMERICHOST) == 0 {
187192
_addresses.append(String(cString: address))
188193
}
189194
}
@@ -303,7 +308,12 @@ open class Host: NSObject {
303308
}
304309
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
305310
let lookupInfo = { (content: inout [String], flags: Int32) in
306-
if getnameinfo(info.ai_addr, sa_len, host, socklen_t(NI_MAXHOST), nil, 0, flags) == 0 {
311+
#if os(OpenBSD)
312+
let hostlen = size_t(NI_MAXHOST)
313+
#else
314+
let hostlen = socklen_t(NI_MAXHOST)
315+
#endif
316+
if getnameinfo(info.ai_addr, sa_len, host, hostlen, nil, 0, flags) == 0 {
307317
content.append(String(cString: host))
308318
}
309319
}

Sources/Foundation/NSError.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ extension POSIXError {
13441344
/// Bad message.
13451345
public static var EBADMSG: POSIXError.Code { return .EBADMSG }
13461346

1347+
#if !os(OpenBSD)
13471348
/// Reserved.
13481349
public static var EMULTIHOP: POSIXError.Code { return .EMULTIHOP }
13491350

@@ -1358,13 +1359,16 @@ extension POSIXError {
13581359

13591360
/// Not a STREAM.
13601361
public static var ENOSTR: POSIXError.Code { return .ENOSTR }
1362+
#endif
13611363

13621364
/// Protocol error.
13631365
public static var EPROTO: POSIXError.Code { return .EPROTO }
13641366

1367+
#if !os(OpenBSD)
13651368
/// STREAM ioctl timeout.
13661369
public static var ETIME: POSIXError.Code { return .ETIME }
13671370
#endif
1371+
#endif
13681372

13691373
#if canImport(Darwin)
13701374
/// No such policy registered.

Sources/Foundation/NSLock.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ open class NSRecursiveLock: NSObject, NSLocking {
249249
#endif
250250
withUnsafeMutablePointer(to: &attrib) { attrs in
251251
pthread_mutexattr_init(attrs)
252-
pthread_mutexattr_settype(attrs, Int32(PTHREAD_MUTEX_RECURSIVE))
252+
#if os(OpenBSD)
253+
let type = Int32(PTHREAD_MUTEX_RECURSIVE.rawValue)
254+
#else
255+
let type = Int32(PTHREAD_MUTEX_RECURSIVE)
256+
#endif
257+
pthread_mutexattr_settype(attrs, type)
253258
pthread_mutex_init(mutex, attrs)
254259
}
255260
#if os(macOS) || os(iOS)

Sources/Foundation/Port.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,22 @@ public protocol PortDelegate: AnyObject {
8383
func handle(_ message: PortMessage)
8484
}
8585

86-
#if canImport(Glibc) && !os(Android)
86+
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
8787
import Glibc
8888
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue)
8989
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM.rawValue)
9090
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
9191
#endif
9292

93-
#if canImport(Glibc) && os(Android)
93+
#if canImport(Glibc) && os(Android) || os(OpenBSD)
9494
import Glibc
9595
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM)
9696
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM)
9797
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
9898
fileprivate let INADDR_ANY: in_addr_t = 0
99+
#if os(OpenBSD)
100+
fileprivate let INADDR_LOOPBACK = 0x7f000001
101+
#endif
99102
#endif
100103

101104

Sources/Foundation/Thread.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ open class Thread : NSObject {
135135
var ti = end_at - start_at
136136
let end_ut = start_ut + ti
137137
while (0.0 < ti) {
138-
var __ts__ = timespec(tv_sec: Int.max, tv_nsec: 0)
138+
var __ts__ = timespec(tv_sec: time_t.max, tv_nsec: 0)
139139
if ti < Double(Int.max) {
140140
var integ = 0.0
141141
let frac: Double = withUnsafeMutablePointer(to: &integ) { integp in
142142
return modf(ti, integp)
143143
}
144-
__ts__.tv_sec = Int(integ)
144+
__ts__.tv_sec = time_t(integ)
145145
__ts__.tv_nsec = Int(frac * 1000000000.0)
146146
}
147147
let _ = withUnsafePointer(to: &__ts__) { ts in
@@ -170,13 +170,13 @@ open class Thread : NSObject {
170170
let start_ut = CFGetSystemUptime()
171171
let end_ut = start_ut + ti
172172
while 0.0 < ti {
173-
var __ts__ = timespec(tv_sec: Int.max, tv_nsec: 0)
173+
var __ts__ = timespec(tv_sec: time_t.max, tv_nsec: 0)
174174
if ti < Double(Int.max) {
175175
var integ = 0.0
176176
let frac: Double = withUnsafeMutablePointer(to: &integ) { integp in
177177
return modf(ti, integp)
178178
}
179-
__ts__.tv_sec = Int(integ)
179+
__ts__.tv_sec = time_t(integ)
180180
__ts__.tv_nsec = Int(frac * 1000000000.0)
181181
}
182182
let _ = withUnsafePointer(to: &__ts__) { ts in

Tests/Foundation/HTTPServer.swift

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import Dispatch
2727
typealias SOCKET = Int32
2828
#endif
2929

30+
#if os(OpenBSD)
31+
let INADDR_LOOPBACK = 0x7f000001
32+
#endif
33+
3034
private let serverDebug = (ProcessInfo.processInfo.environment["SCLF_HTTP_SERVER_DEBUG"] == "YES")
3135

3236
private func debugLog(_ msg: String) {

Tests/Foundation/Tests/TestThread.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class TestThread : XCTestCase {
5656
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
5757
XCTAssertEqual(Thread.current.name, "TestFoundation")
5858
testInternalThreadName("TestFoundation")
59+
#elseif os(OpenBSD) // OpenBSD sets the initial thread name to this.
60+
XCTAssertEqual(Thread.current.name, "Original thread")
61+
testInternalThreadName("Original thread")
5962
#else
6063
// No name is set initially
6164
XCTAssertEqual(Thread.current.name, "")
@@ -160,8 +163,16 @@ class TestThread : XCTestCase {
160163
("test_currentThread", test_currentThread),
161164
("test_threadStart", test_threadStart),
162165
("test_mainThread", test_mainThread),
163-
("test_callStackSymbols", testExpectedToFailOnAndroid(test_callStackSymbols, "Android doesn't support backtraces at the moment.")),
164-
("test_callStackReturnAddresses", testExpectedToFailOnAndroid(test_callStackReturnAddresses, "Android doesn't support backtraces at the moment.")),
166+
("test_callStackSymbols", testExpectedToFailOnOpenBSD(
167+
testExpectedToFailOnAndroid(
168+
test_callStackSymbols,
169+
"Android doesn't support backtraces at the moment."),
170+
"And not currently on OpenBSD.")),
171+
("test_callStackReturnAddresses", testExpectedToFailOnOpenBSD(
172+
testExpectedToFailOnAndroid(
173+
test_callStackReturnAddresses,
174+
"Android doesn't support backtraces at the moment."),
175+
"And not currently on OpenBSD.")),
165176
("test_sleepForTimeInterval", test_sleepForTimeInterval),
166177
("test_sleepUntilDate", test_sleepUntilDate),
167178
("test_threadName", test_threadName),

Tests/Foundation/Utilities.swift

+12
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,14 @@ func shouldAttemptAndroidXFailTests(_ reason: String) -> Bool {
557557
#endif
558558
}
559559

560+
func shouldAttemptOpenBSDXFailTests(_ reason: String) -> Bool {
561+
#if os(OpenBSD)
562+
return shouldAttemptXFailTests(reason)
563+
#else
564+
return true
565+
#endif
566+
}
567+
560568
#if !DARWIN_COMPATIBILITY_TESTS
561569
func testCaseExpectedToFail<T: XCTestCase>(_ allTests: [(String, (T) -> () throws -> Void)], _ reason: String) -> XCTestCaseEntry {
562570
return testCase(allTests.map { ($0.0, testExpectedToFail($0.1, "This test suite is disabled: \(reason)")) })
@@ -581,6 +589,10 @@ func testExpectedToFailOnAndroid<T>(_ test: @escaping (T) -> () throws -> Void,
581589
testExpectedToFailWithCheck(check: shouldAttemptAndroidXFailTests(_:), test, reason)
582590
}
583591

592+
func testExpectedToFailOnOpenBSD<T>(_ test: @escaping (T) -> () throws -> Void, _ reason: String) -> (T) -> () throws -> Void {
593+
testExpectedToFailWithCheck(check: shouldAttemptOpenBSDXFailTests(_:), test, reason)
594+
}
595+
584596
func testExpectedToFailWithCheck<T>(check: (String) -> Bool, _ test: @escaping (T) -> () throws -> Void, _ reason: String) -> (T) -> () throws -> Void {
585597
if check(reason) {
586598
return test

cmake/modules/SwiftSupport.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function(get_swift_host_arch result_var_name)
2323
set("${result_var_name}" "armv7" PARENT_SCOPE)
2424
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
2525
set("${result_var_name}" "armv7" PARENT_SCOPE)
26+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64")
27+
set("${result_var_name}" "amd64" PARENT_SCOPE)
2628
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
2729
set("${result_var_name}" "x86_64" PARENT_SCOPE)
2830
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")

0 commit comments

Comments
 (0)