Skip to content

Commit 9b2dde9

Browse files
committed
Merge commit '246b3474fd4cb91d13b5ecb8fb5175aa6b50aa1e' into github/main
1 parent b81f510 commit 9b2dde9

28 files changed

+113
-14
lines changed

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
#include <sys/stat.h>
7070
#include <sys/syscall.h>
7171
#include <termios.h>
72+
#include <linux/fcntl.h>
73+
#ifdef __swift__
74+
// The linux/stat header is private in the Android modulemap.
75+
#pragma clang module import posix_filesystem.linux_stat
76+
#else
77+
#include <linux/stat.h>
78+
#endif
7279
#elif TARGET_OS_WASI
7380
#include <fcntl.h>
7481
#include <sys/stat.h>

Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ typedef char * Class;
109109
#include <pthread.h>
110110
#endif
111111

112+
#if TARGET_OS_ANDROID
113+
#define HAVE_STRLCPY 1
114+
#define HAVE_STRLCAT 1
115+
#endif
116+
112117
#if TARGET_OS_WIN32
113118
#define BOOL WINDOWS_BOOL
114119

Sources/Foundation/CGFloat.swift

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Android)
11+
import Android
12+
#endif
13+
1014
@frozen
1115
public struct CGFloat: Sendable {
1216
#if arch(i386) || arch(arm) || arch(wasm32)

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, @unchecked Sendable {
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

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
//
88
#if !os(Windows)
99

10+
#if canImport(Android)
11+
import Android
12+
#endif
13+
1014
#if os(Android) && (arch(i386) || arch(arm)) // struct stat.st_mode is UInt32
1115
internal func &(left: UInt32, right: mode_t) -> mode_t {
1216
return mode_t(left) & right
@@ -351,7 +355,14 @@ extension FileManager {
351355
defer { ps.deallocate() }
352356
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
353357
ps.advanced(by: 1).initialize(to: nil)
354-
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
358+
return ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) { rebound_ps in
359+
#if canImport(Android)
360+
let arg = rebound_ps
361+
#else
362+
let arg = ps
363+
#endif
364+
return fts_open(arg, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
365+
}
355366
}
356367
if _stream == nil {
357368
throw _NSErrorWithErrno(errno, reading: true, url: url)
@@ -398,13 +409,13 @@ extension FileManager {
398409

399410
_current = fts_read(stream)
400411
while let current = _current {
401-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
412+
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
402413

403414
switch Int32(current.pointee.fts_info) {
404415
case FTS_D:
405416
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
406417
if skipDescendants {
407-
fts_set(_stream, _current, FTS_SKIP)
418+
fts_set(stream, _current!, FTS_SKIP)
408419
}
409420
if showFile {
410421
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -578,7 +589,7 @@ extension FileManager {
578589
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
579590
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
580591
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
581-
if rename(newItemFS, originalFS) == 0 {
592+
if rename(newItemFS!, originalFS!) == 0 {
582593
return nil
583594
} else {
584595
return errno

Sources/Foundation/FileManager.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import WinSDK
2121

2222
#if os(WASI)
2323
import WASILibc
24+
#elseif canImport(Android)
25+
import Android
2426
#endif
2527

2628
#if os(Windows)

Sources/Foundation/Host.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
import WinSDK
1313
#endif
1414

15-
#if os(Android)
16-
// Android Glibc differs a little with respect to the Linux Glibc.
15+
#if canImport(Android)
16+
import Android
17+
// Android Bionic differs a little with respect to the Linux Glibc.
1718

1819
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
1920
// convert to UInt32.
@@ -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

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#if !os(WASI)
1212
import Dispatch
1313
#endif
14+
#if canImport(Android)
15+
import Android
16+
#endif
1417

1518
extension NSData {
1619
public typealias ReadingOptions = Data.ReadingOptions
@@ -469,6 +472,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
469472
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)
470473
#elseif canImport(WASILibc)
471474
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)
475+
#elseif canImport(Android)
476+
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)
472477
#endif
473478
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
474479
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

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@_implementationOnly import CoreFoundation
1111
#if os(Windows)
1212
import WinSDK
13+
#elseif canImport(Android)
14+
import Android
1315
#elseif os(WASI)
1416
import WASILibc
1517
#endif

Sources/Foundation/NSPlatform.swift

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#if os(macOS) || os(iOS)
1111
fileprivate let _NSPageSize = Int(vm_page_size)
1212
#elseif os(Linux) || os(Android) || os(OpenBSD)
13+
#if canImport(Android)
14+
import Android
15+
#endif
1316
fileprivate let _NSPageSize = Int(getpagesize())
1417
#elseif os(Windows)
1518
import WinSDK

Sources/Foundation/NSSwiftRuntime.swift

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ internal import Synchronization
1919
@_exported import Glibc
2020
#elseif canImport(Musl)
2121
@_exported import Musl
22+
#elseif canImport(Bionic)
23+
@_exported import Bionic
2224
#elseif os(WASI)
2325
@_exported import WASILibc
2426
#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

+8-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
107107
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
108108
#endif
109109

110-
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
110+
#if canImport(Glibc) && !os(OpenBSD)
111111
import Glibc
112112
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
113113
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
@@ -119,14 +119,19 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
119119
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
120120
#endif
121121

122-
#if canImport(Glibc) && os(Android) || os(OpenBSD)
122+
#if canImport(Glibc) || os(OpenBSD)
123123
import Glibc
124124
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
125125
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
126126
fileprivate let INADDR_ANY: in_addr_t = 0
127-
#if os(OpenBSD)
128127
fileprivate let INADDR_LOOPBACK = 0x7f000001
129128
#endif
129+
130+
#if canImport(Android)
131+
import Android
132+
fileprivate let FOUNDATION_SOCK_STREAM = Int32(Android.SOCK_STREAM)
133+
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
134+
fileprivate let INADDR_ANY: in_addr_t = 0
130135
#endif
131136

132137

Sources/Foundation/Process.swift

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import struct WinSDK.HANDLE
1818

1919
#if canImport(Darwin)
2020
import Darwin
21+
#elseif canImport(Android)
22+
import Android
2123
#endif
2224

2325
internal import Synchronization
@@ -940,6 +942,13 @@ open class Process: NSObject, @unchecked Sendable {
940942
var spawnAttrs: posix_spawnattr_t? = nil
941943
#else
942944
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()
945+
#endif
946+
#if os(Android)
947+
guard var spawnAttrs else {
948+
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: [
949+
NSURLErrorKey:self.executableURL!
950+
])
951+
}
943952
#endif
944953
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
945954
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/FoundationNetworking/HTTPCookie.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Foundation
1515

1616
#if os(Windows)
1717
import WinSDK
18+
#elseif canImport(Android)
19+
import Android
1820
#endif
1921

2022
public struct HTTPCookiePropertyKey : RawRepresentable, Equatable, Hashable, Sendable {

Sources/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

Sources/xdgTestHelper/main.swift

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import FoundationNetworking
1919
#endif
2020
#if os(Windows)
2121
import WinSDK
22+
#elseif os(Android)
23+
import Android
2224
#endif
2325

2426
enum HelperCheckStatus : Int32 {

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
final class ServerSemaphore : Sendable {

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/TestFileHandle.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import Dispatch
2020
#if os(Windows)
2121
import WinSDK
22+
#elseif canImport(Android)
23+
import Android
2224
#endif
2325

2426
class TestFileHandle : XCTestCase {
@@ -111,7 +113,7 @@ class TestFileHandle : XCTestCase {
111113
#else
112114
var fds: [Int32] = [-1, -1]
113115
fds.withUnsafeMutableBufferPointer { (pointer) -> Void in
114-
pipe(pointer.baseAddress)
116+
pipe(pointer.baseAddress!)
115117
}
116118

117119
close(fds[1])

Tests/Foundation/TestNSData.swift

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import XCTest
1111
@testable import Foundation
1212

13+
#if canImport(Android)
14+
import Android
15+
#endif
16+
1317
class TestNSData: XCTestCase {
1418

1519
class AllOnesImmutableData : NSData {
@@ -213,6 +217,8 @@ class TestNSData: XCTestCase {
213217
let permission = try fileManager.attributesOfItem(atPath: url.path)[.posixPermissions] as? Int
214218
#if canImport(Darwin)
215219
let expected = Int(S_IRUSR) | Int(S_IWUSR) | Int(S_IRGRP) | Int(S_IWGRP) | Int(S_IROTH) | Int(S_IWOTH)
220+
#elseif canImport(Android)
221+
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)
216222
#else
217223
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)
218224
#endif
@@ -236,6 +242,8 @@ class TestNSData: XCTestCase {
236242
let permission = try fileManager.attributesOfItem(atPath: url.path)[.posixPermissions] as? Int
237243
#if canImport(Darwin)
238244
let expected = Int(S_IRUSR) | Int(S_IWUSR) | Int(S_IRGRP) | Int(S_IWGRP) | Int(S_IROTH) | Int(S_IWOTH)
245+
#elseif canImport(Android)
246+
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)
239247
#else
240248
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)
241249
#endif

Tests/Foundation/TestProcess.swift

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
//
99

1010
import Synchronization
11+
#if canImport(Android)
12+
import Android
13+
#endif
1114

1215
class TestProcess : XCTestCase {
1316

Tests/Foundation/TestSocketPort.swift

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//
99
#if os(Windows)
1010
import WinSDK
11+
#elseif canImport(Android)
12+
import Android
1113
#endif
1214

1315
class TestPortDelegateWithBlock: NSObject, PortDelegate {

Tests/Foundation/TestTimeZone.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class TestTimeZone: XCTestCase {
164164
var lt = tm()
165165
localtime_r(&t, &lt)
166166
let zoneName = NSTimeZone.system.abbreviation() ?? "Invalid Abbreviation"
167-
let expectedName = String(cString: lt.tm_zone, encoding: .ascii) ?? "Invalid Zone"
167+
let expectedName = String(cString: lt.tm_zone!, encoding: .ascii) ?? "Invalid Zone"
168168
XCTAssertEqual(zoneName, expectedName, "expected name \"\(expectedName)\" is not equal to \"\(zoneName)\"")
169169
}
170170
#endif

0 commit comments

Comments
 (0)