Skip to content

Commit 2e0ab92

Browse files
committed
Merge remote-tracking branch 'compnerd/bionic'
2 parents ca3669e + 69cfa27 commit 2e0ab92

22 files changed

+114
-31
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ if(HAS_LIBDISPATCH_API)
7676
find_package(Threads REQUIRED)
7777
endif()
7878

79+
# CMake's Threads adds '-pthread' flag to the interface link
80+
# libraries, which isn't supported by Swift. This is not enabled
81+
# when building with MSVC, but it trips up the Android build, so
82+
# we need to clear out the threads INTERFACE_LINK_LIBRARIES.
83+
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
84+
set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "")
85+
endif()
86+
7987
set(SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
8088
set(BUILD_SHARED_LIBS NO)
8189
add_subdirectory(CoreFoundation EXCLUDE_FROM_ALL)

Sources/Foundation/Data.swift

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
@usableFromInline let memset = WASILibc.memset
4343
@usableFromInline let memcpy = WASILibc.memcpy
4444
@usableFromInline let memcmp = WASILibc.memcmp
45+
#elseif canImport(Android)
46+
@usableFromInline let calloc = Android.calloc
47+
@usableFromInline let malloc = Android.malloc
48+
@usableFromInline let free = Android.free
49+
@usableFromInline let memset = Android.memset
50+
@usableFromInline let memcpy = Android.memcpy
51+
@usableFromInline let memcmp = Android.memcmp
4552
#endif
4653

4754
#if !canImport(Darwin)

Sources/Foundation/FileHandle.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import WASILibc
3434
fileprivate let _read = WASILibc.read(_:_:_:)
3535
fileprivate let _write = WASILibc.write(_:_:_:)
3636
fileprivate let _close = WASILibc.close(_:)
37+
#elseif canImport(Android)
38+
import Android
39+
fileprivate let _read = Android.read(_:_:_:)
40+
fileprivate let _write = Android.write(_:_:_:)
41+
fileprivate let _close = Android.close(_:)
3742
#endif
3843

3944
#if canImport(WinSDK)
@@ -324,7 +329,7 @@ open class FileHandle : NSObject {
324329
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
325330
// Swift does not currently expose MAP_FAILURE
326331
if data != UnsafeMutableRawPointer(bitPattern: -1) {
327-
return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
332+
return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
328333
munmap(buffer, length)
329334
}
330335
}

Sources/Foundation/FileManager+POSIX.swift

+20-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
internal func &(left: UInt32, right: mode_t) -> mode_t {
1212
return mode_t(left) & right
1313
}
14+
#elseif os(Android)
15+
internal func &(left: mode_t, right: Int32) -> mode_t {
16+
return left & mode_t(right)
17+
}
1418
#endif
1519

1620
#if os(WASI)
@@ -409,7 +413,7 @@ extension FileManager {
409413
if !parent.isEmpty && !fileExists(atPath: parent, isDirectory: &isDir) {
410414
try createDirectory(atPath: parent, withIntermediateDirectories: true, attributes: attributes)
411415
}
412-
if mkdir(pathFsRep, S_IRWXU | S_IRWXG | S_IRWXO) != 0 {
416+
if mkdir(pathFsRep, mode_t(S_IRWXU) | mode_t(S_IRWXG) | mode_t(S_IRWXO)) != 0 {
413417
let posixError = errno
414418
if posixError == EEXIST && fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue {
415419
// Continue; if there is an existing file and it is a directory, that is still a success.
@@ -428,7 +432,7 @@ extension FileManager {
428432
throw _NSErrorWithErrno(EEXIST, reading: false, path: path)
429433
}
430434
} else {
431-
if mkdir(pathFsRep, S_IRWXU | S_IRWXG | S_IRWXO) != 0 {
435+
if mkdir(pathFsRep, mode_t(S_IRWXU) | mode_t(S_IRWXG) | mode_t(S_IRWXO)) != 0 {
432436
throw _NSErrorWithErrno(errno, reading: false, path: path)
433437
} else if let attr = attributes {
434438
try self.setAttributes(attr, ofItemAtPath: path)
@@ -803,29 +807,31 @@ extension FileManager {
803807
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
804808
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
805809
ps.advanced(by: 1).initialize(to: nil)
806-
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
810+
let stream = ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) {
811+
fts_open($0, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
812+
}
807813
ps.deinitialize(count: 2)
808814
ps.deallocate()
809815

810-
if stream != nil {
816+
if let stream {
811817
defer {
812818
fts_close(stream)
813819
}
814820

815-
while let current = fts_read(stream)?.pointee {
816-
let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
821+
while let current = fts_read(stream)?.pointee, let fts_path = current.fts_path {
822+
let itemPath = string(withFileSystemRepresentation: fts_path, length: Int(current.fts_pathlen))
817823
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
818824
continue
819825
}
820826

821827
do {
822828
switch Int32(current.fts_info) {
823829
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
824-
if unlink(current.fts_path) == -1 {
830+
if unlink(fts_path) == -1 {
825831
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
826832
}
827833
case FTS_DP:
828-
if rmdir(current.fts_path) == -1 {
834+
if rmdir(fts_path) == -1 {
829835
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
830836
}
831837
case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1171,7 +1177,9 @@ extension FileManager {
11711177
defer { ps.deallocate() }
11721178
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
11731179
ps.advanced(by: 1).initialize(to: nil)
1174-
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
1180+
return ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) {
1181+
fts_open($0, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
1182+
}
11751183
}
11761184
if _stream == nil {
11771185
throw _NSErrorWithErrno(errno, reading: true, url: url)
@@ -1218,13 +1226,13 @@ extension FileManager {
12181226

12191227
_current = fts_read(stream)
12201228
while let current = _current {
1221-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
1229+
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
12221230

12231231
switch Int32(current.pointee.fts_info) {
12241232
case FTS_D:
12251233
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
12261234
if skipDescendants {
1227-
fts_set(_stream, _current, FTS_SKIP)
1235+
fts_set(stream, _current!, FTS_SKIP)
12281236
}
12291237
if showFile {
12301238
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -1398,7 +1406,7 @@ extension FileManager {
13981406
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
13991407
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
14001408
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
1401-
if rename(newItemFS, originalFS) == 0 {
1409+
if rename(newItemFS!, originalFS!) == 0 {
14021410
return nil
14031411
} else {
14041412
return errno

Sources/Foundation/FileManager.swift

+14-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import WinSDK
2525
import WASILibc
2626
#endif
2727

28+
#if os(Android)
29+
import Android
30+
#endif
31+
2832
#if os(Windows)
2933
internal typealias NativeFSRCharType = WCHAR
3034
internal let NativeFSREncoding = String.Encoding.utf16LittleEndian.rawValue
@@ -579,13 +583,13 @@ open class FileManager : NSObject {
579583
#elseif os(WASI)
580584
let type = FileAttributeType(statMode: mode_t(s.st_mode))
581585
#else
582-
if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
583-
let name = String(cString: pwd.pointee.pw_name)
586+
if let pwd = getpwuid(s.st_uid), let pw_name = pwd.pointee.pw_name {
587+
let name = String(cString: pw_name)
584588
result[.ownerAccountName] = name
585589
}
586590

587-
if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
588-
let name = String(cString: grd.pointee.gr_name)
591+
if let grd = getgrgid(s.st_gid), let gr_name = grd.pointee.gr_name {
592+
let name = String(cString: gr_name)
589593
result[.groupOwnerAccountName] = name
590594
}
591595

@@ -1342,12 +1346,12 @@ public struct FileAttributeType : RawRepresentable, Equatable, Hashable {
13421346
#else
13431347
internal init(statMode: mode_t) {
13441348
switch statMode & S_IFMT {
1345-
case S_IFCHR: self = .typeCharacterSpecial
1346-
case S_IFDIR: self = .typeDirectory
1347-
case S_IFBLK: self = .typeBlockSpecial
1348-
case S_IFREG: self = .typeRegular
1349-
case S_IFLNK: self = .typeSymbolicLink
1350-
case S_IFSOCK: self = .typeSocket
1349+
case mode_t(S_IFCHR): self = .typeCharacterSpecial
1350+
case mode_t(S_IFDIR): self = .typeDirectory
1351+
case mode_t(S_IFBLK): self = .typeBlockSpecial
1352+
case mode_t(S_IFREG): self = .typeRegular
1353+
case mode_t(S_IFLNK): self = .typeSymbolicLink
1354+
case mode_t(S_IFSOCK): self = .typeSocket
13511355
default: self = .typeUnknown
13521356
}
13531357
}

Sources/Foundation/Host.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import WinSDK
1313
#endif
1414

1515
#if os(Android)
16+
import Android
1617
// Android Glibc differs a little with respect to the Linux Glibc.
1718

1819
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
@@ -24,8 +25,8 @@ import WinSDK
2425
}
2526

2627
// getnameinfo uses size_t for its 4th and 6th arguments.
27-
private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
28-
return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
28+
private func getnameinfo(_ addr: UnsafePointer<sockaddr>, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
29+
return Android.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
2930
}
3031

3132
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.

Sources/Foundation/NSData.swift

+2
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
500500
let createMode = Int(Musl.S_IRUSR) | Int(Musl.S_IWUSR) | Int(Musl.S_IRGRP) | Int(Musl.S_IWGRP) | Int(Musl.S_IROTH) | Int(Musl.S_IWOTH)
501501
#elseif canImport(WASILibc)
502502
let createMode = Int(WASILibc.S_IRUSR) | Int(WASILibc.S_IWUSR) | Int(WASILibc.S_IRGRP) | Int(WASILibc.S_IWGRP) | Int(WASILibc.S_IROTH) | Int(WASILibc.S_IWOTH)
503+
#elseif canImport(Android)
504+
let createMode = Int(Android.S_IRUSR) | Int(Android.S_IWUSR) | Int(Android.S_IRGRP) | Int(Android.S_IWGRP) | Int(Android.S_IROTH) | Int(Android.S_IWOTH)
503505
#endif
504506
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
505507
throw _NSErrorWithErrno(errno, reading: false, path: path)

Sources/Foundation/NSError.swift

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import Darwin
1616
import Glibc
1717
#elseif canImport(CRT)
1818
import CRT
19+
#elseif canImport(Android)
20+
import Android
1921
#endif
2022

2123
@_implementationOnly import CoreFoundation

Sources/Foundation/NSLock.swift

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#if canImport(Glibc)
1313
import Glibc
14+
#elseif canImport(Android)
15+
import Android
1416
#endif
1517

1618
#if os(Windows)

Sources/Foundation/NSPathUtilities.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
803803
}
804804

805805
// Set the file mode to match macOS
806-
guard fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) != -1 else {
806+
guard fchmod(fd, mode_t(S_IRUSR) | mode_t(S_IWUSR) | mode_t(S_IRGRP) | mode_t(S_IWGRP) | mode_t(S_IROTH) | mode_t(S_IWOTH)) != -1 else {
807807
let _errno = errno
808808
close(fd)
809809
throw _NSErrorWithErrno(_errno, reading: false, path: pathResult)

Sources/Foundation/NSSwiftRuntime.swift

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
@_exported import Glibc
1919
#elseif canImport(Musl)
2020
@_exported import Musl
21+
#elseif canImport(Android)
22+
@_exported import Android
2123
#elseif os(WASI)
2224
@_exported import WASILibc
2325
#elseif os(Windows)

Sources/Foundation/NSURL.swift

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import Darwin
2222
import Glibc
2323
#elseif canImport(Musl)
2424
import Musl
25+
#elseif canImport(Android)
26+
import Android
2527
#endif
2628

2729
// NOTE: this represents PLATFORM_PATH_STYLE

Sources/Foundation/Port.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,28 @@ open class SocketPort: Port {}
9090

9191
#else
9292

93-
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
93+
#if canImport(Glibc) && !os(OpenBSD)
9494
import Glibc
9595
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue)
9696
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM.rawValue)
9797
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
9898
#endif
9999

100-
#if canImport(Glibc) && os(Android) || os(OpenBSD)
100+
#if canImport(Glibc) && os(OpenBSD)
101101
import Glibc
102102
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM)
103103
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM)
104104
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
105105
fileprivate let INADDR_ANY: in_addr_t = 0
106-
#if os(OpenBSD)
107106
fileprivate let INADDR_LOOPBACK = 0x7f000001
108107
#endif
108+
109+
#if canImport(Android)
110+
import Android
111+
fileprivate let SOCK_STREAM = Int32(Android.SOCK_STREAM)
112+
fileprivate let SOCK_DGRAM = Int32(Android.SOCK_DGRAM)
113+
fileprivate let IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
114+
fileprivate let INADDR_ANY: in_addr_t = 0
109115
#endif
110116

111117

Sources/Foundation/Process.swift

+7
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,13 @@ open class Process: NSObject {
928928
var spawnAttrs: posix_spawnattr_t? = nil
929929
#else
930930
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()
931+
#endif
932+
#if os(Android)
933+
guard var spawnAttrs else {
934+
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: [
935+
NSURLErrorKey:self.executableURL!
936+
])
937+
}
931938
#endif
932939
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
933940
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_SETPGROUP)))

Sources/Foundation/Thread.swift

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import WinSDK
1717
import Glibc
1818
#elseif canImport(Musl)
1919
import Musl
20+
#elseif canImport(Android)
21+
import Android
2022
#endif
2123

2224
// WORKAROUND_SR9811

Sources/Tools/plutil/main.swift

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import Glibc
1515
#elseif canImport(Musl)
1616
import Foundation
1717
import Musl
18+
#elseif canImport(Android)
19+
import Foundation
20+
import Android
1821
#elseif canImport(CRT)
1922
import Foundation
2023
import CRT

Tests/Foundation/FTPServer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Dispatch
1515
import Glibc
1616
#elseif canImport(Darwin)
1717
import Darwin
18+
#elseif canImport(Android)
19+
import Android
1820
#endif
1921

2022
public class ServerSemaphore {

Tests/Foundation/HTTPServer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Dispatch
2121
import Darwin
2222
#elseif canImport(Glibc)
2323
import Glibc
24+
#elseif canImport(Android)
25+
import Android
2426
#endif
2527

2628
#if !os(Windows)

Tests/Foundation/Tests/TestFileHandle.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ class TestFileHandle : XCTestCase {
111111
#else
112112
var fds: [Int32] = [-1, -1]
113113
fds.withUnsafeMutableBufferPointer { (pointer) -> Void in
114-
pipe(pointer.baseAddress)
114+
let baseAddress = pointer.baseAddress
115+
#if canImport(Android)
116+
// pipe takes in a non-nullable pointer in the Android NDK only.
117+
guard let baseAddress else {
118+
return
119+
}
120+
#endif
121+
pipe(baseAddress)
115122
}
116123

117124
close(fds[1])

Tests/Foundation/Tests/TestNSData.swift

+4
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ class TestNSData: LoopbackServerTest {
589589
let permission = try fileManager._permissionsOfItem(atPath: url.path)
590590
#if canImport(Darwin)
591591
let expected = Int(S_IRUSR) | Int(S_IWUSR) | Int(S_IRGRP) | Int(S_IWGRP) | Int(S_IROTH) | Int(S_IWOTH)
592+
#elseif canImport(Android)
593+
let expected = Int(Android.S_IRUSR) | Int(Android.S_IWUSR) | Int(Android.S_IRGRP) | Int(Android.S_IWGRP) | Int(Android.S_IROTH) | Int(Android.S_IWOTH)
592594
#else
593595
let expected = Int(Glibc.S_IRUSR) | Int(Glibc.S_IWUSR) | Int(Glibc.S_IRGRP) | Int(Glibc.S_IWGRP) | Int(Glibc.S_IROTH) | Int(Glibc.S_IWOTH)
594596
#endif
@@ -612,6 +614,8 @@ class TestNSData: LoopbackServerTest {
612614
let permission = try fileManager._permissionsOfItem(atPath: url.path)
613615
#if canImport(Darwin)
614616
let expected = Int(S_IRUSR) | Int(S_IWUSR) | Int(S_IRGRP) | Int(S_IWGRP) | Int(S_IROTH) | Int(S_IWOTH)
617+
#elseif canImport(Android)
618+
let expected = Int(Android.S_IRUSR) | Int(Android.S_IWUSR) | Int(Android.S_IRGRP) | Int(Android.S_IWGRP) | Int(Android.S_IROTH) | Int(Android.S_IWOTH)
615619
#else
616620
let expected = Int(Glibc.S_IRUSR) | Int(Glibc.S_IWUSR) | Int(Glibc.S_IRGRP) | Int(Glibc.S_IWGRP) | Int(Glibc.S_IROTH) | Int(Glibc.S_IWOTH)
617621
#endif

0 commit comments

Comments
 (0)