Skip to content

Commit 246b347

Browse files
committed
Merge remote-tracking branch 'github/eng/android-package-merge' into main
2 parents 15ee5c3 + 30c1e40 commit 246b347

28 files changed

+114
-14
lines changed

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
#include <sys/stat.h>
6767
#include <sys/syscall.h>
6868
#include <termios.h>
69+
#include <linux/fcntl.h>
70+
#ifdef __swift__
71+
// The linux/stat header is private in the Android modulemap.
72+
#pragma clang module import posix_filesystem.linux_stat
73+
#else
74+
#include <linux/stat.h>
75+
#endif
6976
#elif TARGET_OS_WASI
7077
#include <fcntl.h>
7178
#include <sys/stat.h>

Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h

Lines changed: 5 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 15 additions & 4 deletions
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
@@ -345,7 +349,14 @@ extension FileManager {
345349
defer { ps.deallocate() }
346350
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
347351
ps.advanced(by: 1).initialize(to: nil)
348-
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
352+
return ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) { rebound_ps in
353+
#if canImport(Android)
354+
let arg = rebound_ps
355+
#else
356+
let arg = ps
357+
#endif
358+
return fts_open(arg, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
359+
}
349360
}
350361
if _stream == nil {
351362
throw _NSErrorWithErrno(errno, reading: true, url: url)
@@ -392,13 +403,13 @@ extension FileManager {
392403

393404
_current = fts_read(stream)
394405
while let current = _current {
395-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
406+
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
396407

397408
switch Int32(current.pointee.fts_info) {
398409
case FTS_D:
399410
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
400411
if skipDescendants {
401-
fts_set(_stream, _current, FTS_SKIP)
412+
fts_set(stream, _current!, FTS_SKIP)
402413
}
403414
if showFile {
404415
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -572,7 +583,7 @@ extension FileManager {
572583
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
573584
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
574585
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
575-
if rename(newItemFS, originalFS) == 0 {
586+
if rename(newItemFS!, originalFS!) == 0 {
576587
return nil
577588
} else {
578589
return errno

Sources/Foundation/FileManager.swift

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 5 additions & 0 deletions
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
@@ -463,6 +466,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
463466
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)
464467
#elseif canImport(WASILibc)
465468
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)
469+
#elseif canImport(Android)
470+
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)
466471
#endif
467472
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
468473
throw _NSErrorWithErrno(errno, reading: false, path: path)

Sources/Foundation/NSError.swift

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)

0 commit comments

Comments
 (0)