Skip to content

Commit

Permalink
Add swift-foundation patch to fix android build
Browse files Browse the repository at this point in the history
  • Loading branch information
andriydruk committed Mar 1, 2025
1 parent 090f9e9 commit 2688489
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions patches/swift-foundation/0001-fix-the-android-build.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
diff --git a/Sources/FoundationEssentials/Platform.swift b/Sources/FoundationEssentials/Platform.swift
index 6eb3acc..72dbf88 100644
--- a/Sources/FoundationEssentials/Platform.swift
+++ b/Sources/FoundationEssentials/Platform.swift
@@ -29,8 +29,7 @@ fileprivate let _pageSize: Int = {
// WebAssembly defines a fixed page size
fileprivate let _pageSize: Int = 65_536
#elseif os(Android)
-import Bionic
-import unistd
+import Android
fileprivate let _pageSize: Int = Int(getpagesize())
#elseif canImport(Glibc)
import Glibc
@@ -142,7 +141,7 @@ extension Platform {
typealias Operation<Input, Output> = (Input, UnsafeMutablePointer<Output>, UnsafeMutablePointer<CChar>, Int, UnsafeMutablePointer<UnsafeMutablePointer<Output>?>) -> Int32
#endif

- private static func withUserGroupBuffer<Input, Output, R>(_ input: Input, _ output: Output, sizeProperty: Int32, operation: Operation<Input, Output>, block: (Output) throws -> R) rethrows -> R? {
+ private static func withUserGroupBuffer<Input, Output, R>(_ input: Input, _ output: Output, sizeProperty: Int32, operation: Operation<Input, Output>, block: (Output) throws -> R?) rethrows -> R? {
var bufferLen = sysconf(sizeProperty)
if bufferLen == -1 {
bufferLen = 4096 // Generous default size estimate
@@ -172,31 +171,51 @@ extension Platform {

static func name(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
- String(cString: $0.pw_name)
+ // Android's pw_name `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let pw_name: UnsafeMutablePointer<CChar>? = $0.pw_name
+ return pw_name.flatMap { String(cString: $0) }
}
}

static func fullName(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
- String(cString: $0.pw_gecos)
+#if os(Android) && _pointerBitWidth(_32)
+ // pw_gecos isn't available on 32-bit Android.
+ let pw_gecos: UnsafeMutablePointer<CChar>? = nil
+#else
+ // Android's pw_gecos `char *`` is nullable, so always coerce to a nullable pointer
+ // in order to be compatible with Android.
+ let pw_gecos: UnsafeMutablePointer<CChar>? = $0.pw_gecos
+#endif
+ return pw_gecos.flatMap { String(cString: $0) }
}
}

static func name(forGID gid: gid_t) -> String? {
withUserGroupBuffer(gid, group(), sizeProperty: Int32(_SC_GETGR_R_SIZE_MAX), operation: getgrgid_r) {
- String(cString: $0.gr_name)
+ // Android's gr_name `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let gr_name: UnsafeMutablePointer<CChar>? = $0.gr_name
+ return gr_name.flatMap { String(cString: $0) }
}
}

static func homeDirectory(forUserName userName: String) -> String? {
withUserGroupBuffer(userName, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwnam_r) {
- String(cString: $0.pw_dir)
+ // Android's pw_dir `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
+ return pw_dir.flatMap { String(cString: $0) }
}
}

static func homeDirectory(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
- String(cString: $0.pw_dir)
+ // Android's pw_dir `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
+ return pw_dir.flatMap { String(cString: $0) }
}
}
}

0 comments on commit 2688489

Please sign in to comment.