Skip to content

Commit a769223

Browse files
authored
FoundationEssentials: initial pass to add Android support (#704)
This adds the necessary guards and includes for the Android modules. While the module does not compile currently due to nullability differences (and in some cases missing declarations), this at least brings the module to a point where we can start working on the errors and differences to create a maintainable codebase for Android.
1 parent c39fe2d commit a769223

24 files changed

+81
-15
lines changed

Sources/FoundationEssentials/Calendar/Calendar.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
internal import os
15+
#elseif os(Android)
16+
import Android
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif canImport(CRT)

Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(os)
1414
internal import os
15+
#elseif os(Android)
16+
import Android
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif canImport(CRT)

Sources/FoundationEssentials/CodableUtilities.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Bionic
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#endif

Sources/FoundationEssentials/Data/Data+Reading.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal import _FoundationCShims
1818

1919
#if canImport(Darwin)
2020
import Darwin
21+
#elseif os(Android)
22+
import Android
2123
#elseif canImport(Glibc)
2224
import Glibc
2325
#elseif os(Windows)

Sources/FoundationEssentials/Data/Data+Writing.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ internal import _FoundationCShims
1919

2020
#if canImport(Darwin)
2121
import Darwin
22+
#elseif os(Android)
23+
import Android
24+
import unistd
2225
#elseif canImport(Glibc)
2326
import Glibc
2427
#elseif os(Windows)

Sources/FoundationEssentials/Data/Data.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@
1717
@usableFromInline let memset = ucrt.memset
1818
@usableFromInline let memcpy = ucrt.memcpy
1919
@usableFromInline let memcmp = ucrt.memcmp
20-
#endif
21-
#if canImport(Glibc)
20+
#elseif os(Android)
21+
import Bionic
22+
@usableFromInline let calloc = Bionic.calloc
23+
@usableFromInline let malloc = Bionic.malloc
24+
@usableFromInline let free = Bionic.free
25+
@usableFromInline let memset = Bionic.memset
26+
@usableFromInline let memcpy = Bionic.memcpy
27+
@usableFromInline let memcmp = Bionic.memcmp
28+
#elseif canImport(Glibc)
2229
@usableFromInline let calloc = Glibc.calloc
2330
@usableFromInline let malloc = Glibc.malloc
2431
@usableFromInline let free = Glibc.free

Sources/FoundationEssentials/Date.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Bionic
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif canImport(WinSDK)

Sources/FoundationEssentials/Error/CocoaError+FilePath.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal import _ForSwiftFoundation
1515

1616
#if canImport(Darwin)
1717
import Darwin
18+
#elseif os(Android)
19+
import Bionic
1820
#elseif canImport(Glibc)
1921
import Glibc
2022
#elseif os(Windows)

Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
// Import for POSIXErrorCode
13-
#if canImport(Glibc)
13+
#if os(Android)
14+
import Android
15+
#elseif canImport(Glibc)
1416
@preconcurrency import Glibc
1517
#elseif canImport(Darwin)
1618
@preconcurrency import Darwin

Sources/FoundationEssentials/FileManager/FileManager+Basics.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Android
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif os(Windows)

Sources/FoundationEssentials/FileManager/FileManager+Directories.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ internal import os
1818

1919
#if canImport(Darwin)
2020
import Darwin
21+
#elseif os(Android)
22+
import Android
23+
import unistd
2124
#elseif canImport(Glibc)
2225
import Glibc
2326
#elseif os(Windows)

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ internal import DarwinPrivate.sys.content_protection
1717

1818
#if canImport(Darwin)
1919
import Darwin
20+
#elseif os(Android)
21+
import Android
22+
import posix_filesystem
2023
#elseif canImport(Glibc)
2124
import Glibc
2225
internal import _FoundationCShims

Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Android
17+
import unistd
1518
#elseif canImport(Glibc)
1619
import Glibc
1720
#elseif os(Windows)

Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ internal import QuarantinePrivate
2323

2424
#if canImport(Darwin)
2525
import Darwin
26+
#elseif os(Android)
27+
import Android
2628
#elseif canImport(Glibc)
2729
import Glibc
2830
internal import _FoundationCShims

Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ struct _Win32DirectoryContentsSequence: Sequence {
109109

110110
#if canImport(Darwin)
111111
import Darwin
112+
#elseif os(Android)
113+
import Android
114+
import posix_filesystem.dirent
112115
#elseif canImport(Glibc)
113116
import Glibc
114117
internal import _FoundationCShims
@@ -315,7 +318,7 @@ extension Sequence<_FTSSequence.Element> {
315318
struct _POSIXDirectoryContentsSequence: Sequence {
316319
#if canImport(Darwin)
317320
typealias DirectoryEntryPtr = UnsafeMutablePointer<DIR>
318-
#elseif canImport(Glibc)
321+
#elseif os(Android) || canImport(Glibc)
319322
typealias DirectoryEntryPtr = OpaquePointer
320323
#endif
321324

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Android
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif os(Windows)

Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Android
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif os(Windows)

Sources/FoundationEssentials/LockedState.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal import os
1515
#if FOUNDATION_FRAMEWORK && canImport(C.os.lock)
1616
internal import C.os.lock
1717
#endif
18+
#elseif os(Android)
19+
import Bionic
1820
#elseif canImport(Glibc)
1921
import Glibc
2022
#elseif canImport(WinSDK)
@@ -27,7 +29,7 @@ package struct LockedState<State> {
2729
private struct _Lock {
2830
#if canImport(os)
2931
typealias Primitive = os_unfair_lock
30-
#elseif canImport(Glibc)
32+
#elseif os(Android) || canImport(Glibc)
3133
typealias Primitive = pthread_mutex_t
3234
#elseif canImport(WinSDK)
3335
typealias Primitive = SRWLOCK
@@ -39,15 +41,15 @@ package struct LockedState<State> {
3941
fileprivate static func initialize(_ platformLock: PlatformLock) {
4042
#if canImport(os)
4143
platformLock.initialize(to: os_unfair_lock())
42-
#elseif canImport(Glibc)
44+
#elseif os(Android) || canImport(Glibc)
4345
pthread_mutex_init(platformLock, nil)
4446
#elseif canImport(WinSDK)
4547
InitializeSRWLock(platformLock)
4648
#endif
4749
}
4850

4951
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
50-
#if canImport(Glibc)
52+
#if os(Android) || canImport(Glibc)
5153
pthread_mutex_destroy(platformLock)
5254
#endif
5355
platformLock.deinitialize(count: 1)
@@ -56,7 +58,7 @@ package struct LockedState<State> {
5658
static fileprivate func lock(_ platformLock: PlatformLock) {
5759
#if canImport(os)
5860
os_unfair_lock_lock(platformLock)
59-
#elseif canImport(Glibc)
61+
#elseif os(Android) || canImport(Glibc)
6062
pthread_mutex_lock(platformLock)
6163
#elseif canImport(WinSDK)
6264
AcquireSRWLockExclusive(platformLock)
@@ -66,7 +68,7 @@ package struct LockedState<State> {
6668
static fileprivate func unlock(_ platformLock: PlatformLock) {
6769
#if canImport(os)
6870
os_unfair_lock_unlock(platformLock)
69-
#elseif canImport(Glibc)
71+
#elseif os(Android) || canImport(Glibc)
7072
pthread_mutex_unlock(platformLock)
7173
#elseif canImport(WinSDK)
7274
ReleaseSRWLockExclusive(platformLock)

Sources/FoundationEssentials/Platform.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ fileprivate let _pageSize: Int = {
2828
#elseif os(WASI)
2929
// WebAssembly defines a fixed page size
3030
fileprivate let _pageSize: Int = 65_536
31+
#elseif os(Android)
32+
import Bionic
33+
import unistd
34+
fileprivate let _pageSize: Int = Int(getpagesize())
3135
#elseif canImport(Glibc)
3236
import Glibc
3337
fileprivate let _pageSize: Int = Int(getpagesize())
@@ -167,7 +171,12 @@ extension Platform {
167171
}
168172
}
169173
#else
174+
// FIXME: bionic implements this as `return 0;` and does not expose the
175+
// function via headers. We should be able to shim this and use the call
176+
// if it is available.
177+
#if !os(Android)
170178
guard issetugid() == 0 else { return nil }
179+
#endif
171180
if let value = getenv(name) {
172181
return String(cString: value)
173182
} else {

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal import _FoundationCShims
1414

1515
#if canImport(Darwin)
1616
import Darwin
17+
#elseif os(Android)
18+
import Bionic
19+
import unistd
1720
#elseif canImport(Glibc)
1821
import Glibc
1922
#elseif os(Windows)
@@ -158,7 +161,7 @@ final class _ProcessInfo: Sendable {
158161
}
159162

160163
var userName: String {
161-
#if canImport(Darwin) || canImport(Glibc)
164+
#if canImport(Darwin) || os(Android) || canImport(Glibc)
162165
// Darwin and Linux
163166
let (euid, _) = Platform.getUGIDs()
164167
if let upwd = getpwuid(euid),
@@ -193,7 +196,7 @@ final class _ProcessInfo: Sendable {
193196
}
194197

195198
var fullUserName: String {
196-
#if canImport(Darwin) || canImport(Glibc)
199+
#if canImport(Darwin) || os(Android) || canImport(Glibc)
197200
let (euid, _) = Platform.getUGIDs()
198201
if let upwd = getpwuid(euid),
199202
let fullname = upwd.pointee.pw_gecos {

Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import Bionic
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#endif

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
internal import os
15+
#elseif os(Android)
16+
import Android
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif os(Windows)

Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if canImport(Darwin)
1414
import Darwin
15+
#elseif os(Android)
16+
import unistd
1517
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif canImport(ucrt)

Sources/FoundationEssentials/_ThreadLocal.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#if canImport(Darwin)
1313
import Darwin
14+
#elseif os(Android)
15+
import Bionic
1416
#elseif canImport(Glibc)
1517
import Glibc
1618
#elseif canImport(WinSDK)
@@ -22,7 +24,7 @@ internal import threads
2224
#endif
2325

2426
struct _ThreadLocal {
25-
#if canImport(Darwin) || canImport(Glibc)
27+
#if canImport(Darwin) || os(Android) || canImport(Glibc)
2628
fileprivate typealias PlatformKey = pthread_key_t
2729
#elseif USE_TSS
2830
fileprivate typealias PlatformKey = tss_t
@@ -34,7 +36,7 @@ struct _ThreadLocal {
3436
fileprivate let key: PlatformKey
3537

3638
init() {
37-
#if canImport(Darwin) || canImport(Glibc)
39+
#if canImport(Darwin) || os(Android) || canImport(Glibc)
3840
var key = PlatformKey()
3941
pthread_key_create(&key, nil)
4042
self.key = key
@@ -50,7 +52,7 @@ struct _ThreadLocal {
5052

5153
private static subscript(_ key: PlatformKey) -> UnsafeMutableRawPointer? {
5254
get {
53-
#if canImport(Darwin) || canImport(Glibc)
55+
#if canImport(Darwin) || os(Android) || canImport(Glibc)
5456
pthread_getspecific(key)
5557
#elseif USE_TSS
5658
tss_get(key)
@@ -60,7 +62,7 @@ struct _ThreadLocal {
6062
}
6163

6264
set {
63-
#if canImport(Darwin) || canImport(Glibc)
65+
#if canImport(Darwin) || os(Android) || canImport(Glibc)
6466
pthread_setspecific(key, newValue)
6567
#elseif USE_TSS
6668
tss_set(key, newValue)

0 commit comments

Comments
 (0)