Skip to content

[Android] Use the Bionic module in more places #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/Calendar/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#if canImport(Darwin)
internal import os
#elseif os(Android)
import Android
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#if canImport(os)
internal import os
#elseif os(Android)
import Android
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/CodableUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/Data/Data+Reading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal import _FoundationCShims

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
#elseif canImport(Glibc)
import Glibc
Expand Down Expand Up @@ -377,7 +377,7 @@ internal func readBytesFromFile(path inPath: PathOrURL, reportProgress: Bool, ma
localProgress?.completedUnitCount = 1
} else if shouldMap {
#if !NO_FILESYSTEM
#if os(Android)
#if canImport(Android)
let bytes = mmap(nil, Int(fileSize), PROT_READ, MAP_PRIVATE, fd, 0)
if bytes == UnsafeMutableRawPointer(bitPattern: -1) {
throw CocoaError.errorWithFilePath(inPath, errno: errno, reading: true)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Data/Data+Writing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal import _FoundationCShims

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
import unistd
#elseif canImport(Glibc)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Data/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@usableFromInline let memset = ucrt.memset
@usableFromInline let memcpy = ucrt.memcpy
@usableFromInline let memcmp = ucrt.memcmp
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
@usableFromInline let calloc = Bionic.calloc
@usableFromInline let malloc = Bionic.malloc
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/Decimal/Decimal+Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
import Android
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal import _ForSwiftFoundation

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

// Import for POSIXErrorCode
#if os(Android)
#if canImport(Android)
@preconcurrency import Android
#elseif canImport(Glibc)
@preconcurrency import Glibc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
#elseif canImport(Glibc)
import Glibc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal import os

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
import unistd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this redundant since the prior Android overlay includes it?

#elseif canImport(Glibc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal import DarwinPrivate.sys.content_protection

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
import posix_filesystem
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#elseif canImport(Glibc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
import unistd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#elseif canImport(Glibc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal import QuarantinePrivate

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
#elseif canImport(Glibc)
import Glibc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct _Win32DirectoryContentsSequence: Sequence {

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
import posix_filesystem.dirent
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#elseif canImport(Glibc)
Expand Down Expand Up @@ -328,7 +328,7 @@ extension Sequence<_FTSSequence.Element> {
struct _POSIXDirectoryContentsSequence: Sequence {
#if canImport(Darwin)
typealias DirectoryEntryPtr = UnsafeMutablePointer<DIR>
#elseif os(Android) || canImport(Glibc) || canImport(Musl) || os(WASI)
#elseif canImport(Android) || canImport(Glibc) || canImport(Musl) || os(WASI)
typealias DirectoryEntryPtr = OpaquePointer
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import Android
#elseif canImport(Glibc)
import Glibc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
import Android
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
Expand Down
12 changes: 6 additions & 6 deletions Sources/FoundationEssentials/LockedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal import os
#if FOUNDATION_FRAMEWORK && canImport(C.os.lock)
internal import C.os.lock
#endif
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
Expand All @@ -31,7 +31,7 @@ package struct LockedState<State> {
private struct _Lock {
#if canImport(os)
typealias Primitive = os_unfair_lock
#elseif os(Android) || canImport(Glibc) || canImport(Musl)
#elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
typealias Primitive = pthread_mutex_t
#elseif canImport(WinSDK)
typealias Primitive = SRWLOCK
Expand All @@ -46,7 +46,7 @@ package struct LockedState<State> {
fileprivate static func initialize(_ platformLock: PlatformLock) {
#if canImport(os)
platformLock.initialize(to: os_unfair_lock())
#elseif os(Android) || canImport(Glibc)
#elseif canImport(Bionic) || canImport(Glibc)
pthread_mutex_init(platformLock, nil)
#elseif canImport(WinSDK)
InitializeSRWLock(platformLock)
Expand All @@ -56,7 +56,7 @@ package struct LockedState<State> {
}

fileprivate static func deinitialize(_ platformLock: PlatformLock) {
#if os(Android) || canImport(Glibc)
#if canImport(Bionic) || canImport(Glibc)
pthread_mutex_destroy(platformLock)
#endif
platformLock.deinitialize(count: 1)
Expand All @@ -65,7 +65,7 @@ package struct LockedState<State> {
static fileprivate func lock(_ platformLock: PlatformLock) {
#if canImport(os)
os_unfair_lock_lock(platformLock)
#elseif os(Android) || canImport(Glibc)
#elseif canImport(Bionic) || canImport(Glibc)
pthread_mutex_lock(platformLock)
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
Expand All @@ -77,7 +77,7 @@ package struct LockedState<State> {
static fileprivate func unlock(_ platformLock: PlatformLock) {
#if canImport(os)
os_unfair_lock_unlock(platformLock)
#elseif os(Android) || canImport(Glibc)
#elseif canImport(Bionic) || canImport(Glibc)
pthread_mutex_unlock(platformLock)
#elseif canImport(WinSDK)
ReleaseSRWLockExclusive(platformLock)
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fileprivate let _pageSize: Int = {
#elseif os(WASI)
// WebAssembly defines a fixed page size
fileprivate let _pageSize: Int = 65_536
#elseif os(Android)
#elseif canImport(Android)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that we check canImport(Android) but actually importing Bionic below? Do canImport(Android) and canImport(Bionic) always return the same value on Android?

I agree with you that using canImport() is better than os(), but it seems we are using canImport(Android) and canImport(Bionic) interchangeably in place of os(Android). Can we standardize that to one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that we check canImport(Android) but actually importing Bionic below?

Yes, as unistd comes from the Android overlay. I did not add those imports, so I merely used the most appropriate canImport() here, the one that includes them both, since Android is a superset of Bionic.

Do canImport(Android) and canImport(Bionic) always return the same value on Android?

Yes, they are interchangeable, as they are both defined in the same module map. However, import Bionic and import Android are not interchangeable, so I simply choose the canImport() to match the one we are actually importing.

we are using canImport(Android) and canImport(Bionic) interchangeably... Can we standardize that to one?

No need to, as they are interchangeable and one is simply chosen to match the non-interchangeable import used.

import Bionic
import unistd
fileprivate let _pageSize: Int = Int(getpagesize())
Expand Down Expand Up @@ -175,7 +175,7 @@ extension Platform {
// FIXME: bionic implements this as `return 0;` and does not expose the
// function via headers. We should be able to shim this and use the call
// if it is available.
#if !os(Android) && !os(WASI)
#if !canImport(Android) && !os(WASI)
guard issetugid() == 0 else { return nil }
#endif
if let value = getenv(name) {
Expand Down
10 changes: 5 additions & 5 deletions Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal import _FoundationCShims

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same explanation as above

import Bionic
import unistd
#elseif canImport(Glibc)
Expand Down Expand Up @@ -165,7 +165,7 @@ final class _ProcessInfo: Sendable {
}

var userName: String {
#if canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
#if canImport(Darwin) || canImport(Android) || canImport(Glibc) || canImport(Musl)
// Darwin and Linux
let (euid, _) = Platform.getUGIDs()
if let upwd = getpwuid(euid),
Expand Down Expand Up @@ -200,10 +200,10 @@ final class _ProcessInfo: Sendable {
}

var fullUserName: String {
#if os(Android) && (arch(i386) || arch(arm))
#if canImport(Android) && (arch(i386) || arch(arm))
// On LP32 Android, pw_gecos doesn't exist and is presumed to be NULL.
return ""
#elseif canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
#elseif canImport(Darwin) || canImport(Android) || canImport(Glibc) || canImport(Musl)
let (euid, _) = Platform.getUGIDs()
if let upwd = getpwuid(euid),
let fullname = upwd.pointee.pw_gecos {
Expand Down Expand Up @@ -368,7 +368,7 @@ extension _ProcessInfo {
}

var operatingSystemVersion: (major: Int, minor: Int, patch: Int) {
#if canImport(Darwin) || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
#if canImport(Darwin) || os(Linux) || os(FreeBSD) || os(OpenBSD) || canImport(Android)
var uts: utsname = utsname()
guard uname(&uts) == 0 else {
return (major: -1, minor: 0, patch: 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/String/String+Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
internal import os
#elseif os(Android)
#elseif canImport(Android)
import Android
#elseif canImport(Glibc)
import Glibc
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Android)
import unistd
#elseif canImport(Glibc)
import Glibc
Expand Down
10 changes: 5 additions & 5 deletions Sources/FoundationEssentials/_ThreadLocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#if canImport(Darwin)
import Darwin
#elseif os(Android)
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
Expand All @@ -26,7 +26,7 @@ internal import threads
#endif

struct _ThreadLocal {
#if canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
#if canImport(Darwin) || canImport(Bionic) || canImport(Glibc) || canImport(Musl)
fileprivate typealias PlatformKey = pthread_key_t
#elseif USE_TSS
fileprivate typealias PlatformKey = tss_t
Expand All @@ -40,7 +40,7 @@ struct _ThreadLocal {
fileprivate let key: PlatformKey

init() {
#if canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
#if canImport(Darwin) || canImport(Bionic) || canImport(Glibc) || canImport(Musl)
var key = PlatformKey()
pthread_key_create(&key, nil)
self.key = key
Expand All @@ -58,7 +58,7 @@ struct _ThreadLocal {

private static subscript(_ key: PlatformKey) -> UnsafeMutableRawPointer? {
get {
#if canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
#if canImport(Darwin) || canImport(Bionic) || canImport(Glibc) || canImport(Musl)
pthread_getspecific(key)
#elseif USE_TSS
tss_get(key)
Expand All @@ -70,7 +70,7 @@ struct _ThreadLocal {
}

set {
#if canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
#if canImport(Darwin) || canImport(Bionic) || canImport(Glibc) || canImport(Musl)
pthread_setspecific(key, newValue)
#elseif USE_TSS
tss_set(key, newValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import FoundationEssentials
#endif

#if os(Android)
#if canImport(Android)
import Android
#elseif canImport(Glibc)
import Glibc
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationInternationalization/Date+ICU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import FoundationEssentials

internal import _FoundationICU

#if os(Android)
import Android
#if canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
Expand Down
Loading