forked from finagolfin/swift-android-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swift-android.patch
406 lines (376 loc) · 17.8 KB
/
swift-android.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
diff --git a/swift/utils/build-script-impl b/swift/utils/build-script-impl
index 16e05052609..114e768bdbb 100755
--- a/swift/utils/build-script-impl
+++ b/swift/utils/build-script-impl
@@ -2412,7 +2412,7 @@ for host in "${ALL_HOSTS[@]}"; do
;;
foundation_macros)
- if [[ ${host} == "macosx"* ]]; then
+ if [[ ${host} == "macosx"* ]] || [[ "${host}" == "android-"* ]]; then
echo "Skipping Foundation Macros on OS X -- Foundation is part of the OS on this platform"
continue
fi
@@ -2601,6 +2601,7 @@ for host in "${ALL_HOSTS[@]}"; do
if [[ "${host}" == "android-"* ]]; then
cmake_options+=(
-DCMAKE_HAVE_LIBC_PTHREAD=True
+ -DSwiftFoundation_MODULE_TRIPLE:STRING="${ANDROID_ARCH}-unknown-linux-android"
)
fi
;;
@@ -3113,7 +3113,7 @@ for host in "${ALL_HOSTS[@]}"; do
;;
foundation|foundation_static|foundation_macros)
# FIXME: Foundation doesn't build from the script on OS X
- if [[ ${host} == "macosx"* ]]; then
+ if [[ ${host} == "macosx"* ]] || [[ "${host}" == "android-"* && "${product}" == "foundation_macros" ]]; then
echo "Skipping Foundation on OS X -- Foundation does not build for this platform"
continue
fi
diff --git a/swift-corelibs-foundation/CMakeLists.txt b/swift-corelibs-foundation/CMakeLists.txt
index 7f290d16..95366592 100644
--- a/swift-corelibs-foundation/CMakeLists.txt
+++ b/swift-corelibs-foundation/CMakeLists.txt
@@ -51,6 +51,7 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
endif()
endif()
+set(CMAKE_SHARED_LINKER_FLAGS "")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
diff --git a/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt b/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt
index 016bf294..5c42986a 100644
--- a/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt
+++ b/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt
@@ -162,6 +162,10 @@ if(NOT BUILD_SHARED_LIBS)
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend _FoundationICU>")
target_compile_options(Foundation PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend swiftSynchronization>")
+ if(${CMAKE_SYSTEM_NAME} STREQUAL Android)
+ target_compile_options(Foundation PRIVATE
+ "SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend android-spawn>")
+ endif()
endif()
set_target_properties(Foundation PROPERTIES
@@ -174,6 +174,12 @@
target_link_libraries(Foundation PUBLIC
swiftDispatch)
endif()
+ if(${CMAKE_SYSTEM_NAME} STREQUAL Android)
+ target_link_libraries(Foundation PRIVATE android-spawn)
+ list(GET CMAKE_FIND_ROOT_PATH 0 SPAWN_DIR)
+ target_include_directories(Foundation PUBLIC ${SPAWN_DIR}/usr/include)
+ target_link_directories(Foundation PUBLIC ${SPAWN_DIR}/usr/lib)
+ endif()
if(LINKER_SUPPORTS_BUILD_ID)
target_link_options(Foundation PRIVATE "LINKER:--build-id=sha1")
diff --git a/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h b/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h
index a2ba56cd..91a312dc 100644
--- a/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h
+++ b/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h
@@ -69,6 +69,13 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#include <termios.h>
+#include <linux/fcntl.h>
+#ifdef __swift__
+// The linux/stat header is private in the Android modulemap.
+#pragma clang module import posix_filesystem.linux_stat
+#else
+#include <linux/stat.h>
+#endif
#elif TARGET_OS_WASI
#include <fcntl.h>
#include <sys/stat.h>
diff --git a/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift b/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift
index ffe3a6c6..c59977f8 100644
--- a/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift
@@ -7,6 +7,10 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
+#if canImport(Android)
+import Android
+#endif
+
@frozen
public struct CGFloat: Sendable {
#if arch(i386) || arch(arm) || arch(wasm32)
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift b/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
index b07c49ac..b540e284 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
@@ -34,6 +34,11 @@ import WASILibc
fileprivate let _read = WASILibc.read(_:_:_:)
fileprivate let _write = WASILibc.write(_:_:_:)
fileprivate let _close = WASILibc.close(_:)
+#elseif canImport(Android)
+import Android
+fileprivate let _read = Android.read(_:_:_:)
+fileprivate let _write = Android.write(_:_:_:)
+fileprivate let _close = Android.close(_:)
#endif
#if canImport(WinSDK)
@@ -321,10 +326,15 @@ open class FileHandle : NSObject, @unchecked Sendable {
if options.contains(.alwaysMapped) {
// Filesizes are often 64bit even on 32bit systems
let mapSize = min(length, Int(clamping: statbuf.st_size))
+ #if canImport(Android)
+ // Bionic mmap() now returns _Nonnull, so the force unwrap below isn't needed.
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
+ #else
+ let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)!
+ #endif
// Swift does not currently expose MAP_FAILURE
if data != UnsafeMutableRawPointer(bitPattern: -1) {
- return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
+ return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
munmap(buffer, length)
}
}
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift b/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
index e89b3bf6..a82fe1c2 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
@@ -7,6 +7,10 @@
//
#if !os(Windows)
+#if canImport(Android)
+import Android
+#endif
+
#if os(Android) && (arch(i386) || arch(arm)) // struct stat.st_mode is UInt32
internal func &(left: UInt32, right: mode_t) -> mode_t {
return mode_t(left) & right
@@ -398,13 +402,13 @@ extension FileManager {
_current = fts_read(stream)
while let current = _current {
- let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
+ let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
switch Int32(current.pointee.fts_info) {
case FTS_D:
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
if skipDescendants {
- fts_set(_stream, _current, FTS_SKIP)
+ fts_set(stream, current, FTS_SKIP)
}
if showFile {
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -578,7 +582,7 @@ extension FileManager {
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
- if rename(newItemFS, originalFS) == 0 {
+ if rename(newItemFS!, originalFS!) == 0 {
return nil
} else {
return errno
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileManager.swift b/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
index a5d75820..a19464d7 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
@@ -21,6 +21,8 @@ import WinSDK
#if os(WASI)
import WASILibc
+#elseif canImport(Bionic)
+import Bionic
#endif
#if os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/Host.swift b/swift-corelibs-foundation/Sources/Foundation/Host.swift
index 6c4f5291..fb130631 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Host.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Host.swift
@@ -12,8 +12,9 @@
import WinSDK
#endif
-#if os(Android)
- // Android Glibc differs a little with respect to the Linux Glibc.
+#if canImport(Android)
+ import Android
+ // Android Bionic differs a little with respect to the Linux Glibc.
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
// convert to UInt32.
@@ -24,8 +25,8 @@ import WinSDK
}
// getnameinfo uses size_t for its 4th and 6th arguments.
- private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
- return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
+ private func getnameinfo(_ addr: UnsafePointer<sockaddr>, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
+ return Android.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
}
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSData.swift b/swift-corelibs-foundation/Sources/Foundation/NSData.swift
index ae54f971..65eb0d93 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSData.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSData.swift
@@ -11,6 +11,9 @@
#if !os(WASI)
import Dispatch
#endif
+#if canImport(Android)
+import Android
+#endif
extension NSData {
public typealias ReadingOptions = Data.ReadingOptions
@@ -469,6 +472,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
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)
#elseif canImport(WASILibc)
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)
+#elseif canImport(Android)
+ 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)
#endif
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
throw _NSErrorWithErrno(errno, reading: false, path: path)
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSError.swift b/swift-corelibs-foundation/Sources/Foundation/NSError.swift
index 6f21d3a0..dcd6f3f1 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSError.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSError.swift
@@ -16,6 +16,8 @@ import Darwin
import Glibc
#elseif canImport(CRT)
import CRT
+#elseif canImport(Android)
+import Android
#endif
@_implementationOnly import CoreFoundation
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSLock.swift b/swift-corelibs-foundation/Sources/Foundation/NSLock.swift
index fe1d08b7..9d0fadc9 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSLock.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSLock.swift
@@ -11,6 +11,8 @@
#if canImport(Glibc)
import Glibc
+#elseif canImport(Bionic)
+import Bionic
#endif
#if os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift b/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift
index a1809026..5424f5bb 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift
@@ -10,6 +10,9 @@
#if os(macOS) || os(iOS)
fileprivate let _NSPageSize = Int(vm_page_size)
#elseif os(Linux) || os(Android) || os(OpenBSD)
+#if canImport(Android)
+import Android
+#endif
fileprivate let _NSPageSize = Int(getpagesize())
#elseif os(Windows)
import WinSDK
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift b/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift
index 03176c17..1509c31d 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift
@@ -19,6 +19,8 @@ internal import Synchronization
@_exported import Glibc
#elseif canImport(Musl)
@_exported import Musl
+#elseif canImport(Bionic)
+@_exported import Bionic
#elseif os(WASI)
@_exported import WASILibc
#elseif os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSURL.swift b/swift-corelibs-foundation/Sources/Foundation/NSURL.swift
index 6af73f16..9e6f20d0 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSURL.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSURL.swift
@@ -22,6 +22,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
+#elseif canImport(Bionic)
+import Bionic
#endif
// NOTE: this represents PLATFORM_PATH_STYLE
diff --git a/swift-corelibs-foundation/Sources/Foundation/Port.swift b/swift-corelibs-foundation/Sources/Foundation/Port.swift
index c4ed8282..e71f591b 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Port.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Port.swift
@@ -107,7 +107,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
#endif
-#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
+#if canImport(Glibc) && !os(OpenBSD)
import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
@@ -119,14 +119,19 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
#endif
-#if canImport(Glibc) && os(Android) || os(OpenBSD)
+#if canImport(Glibc) && os(OpenBSD)
import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
fileprivate let INADDR_ANY: in_addr_t = 0
-#if os(OpenBSD)
fileprivate let INADDR_LOOPBACK = 0x7f000001
#endif
+
+#if canImport(Android)
+import Android
+fileprivate let FOUNDATION_SOCK_STREAM = Int32(Android.SOCK_STREAM)
+fileprivate let FOUNDATION_IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
+fileprivate let INADDR_ANY: in_addr_t = 0
#endif
diff --git a/swift-corelibs-foundation/Sources/Foundation/Process.swift b/swift-corelibs-foundation/Sources/Foundation/Process.swift
index ee35c23a..5e8cfbaa 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Process.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Process.swift
@@ -18,6 +18,8 @@ import struct WinSDK.HANDLE
#if canImport(Darwin)
import Darwin
+#elseif canImport(Android)
+import Android
#endif
internal import Synchronization
diff --git a/swift-corelibs-foundation/Sources/Foundation/Thread.swift b/swift-corelibs-foundation/Sources/Foundation/Thread.swift
index 5e79579c..0985a482 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Thread.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Thread.swift
@@ -17,6 +17,8 @@ import WinSDK
import Glibc
#elseif canImport(Musl)
import Musl
+#elseif canImport(Bionic)
+import Bionic
#endif
// WORKAROUND_SR9811
diff --git a/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift b/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift
index e0d1cbbd..237c1daf 100644
--- a/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift
+++ b/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift
@@ -15,6 +15,8 @@ import Foundation
#if os(Windows)
import WinSDK
+#elseif canImport(Android)
+import Android
#endif
public struct HTTPCookiePropertyKey : RawRepresentable, Equatable, Hashable, Sendable {
diff --git a/swift-corelibs-foundation/Sources/plutil/main.swift b/swift-corelibs-foundation/Sources/plutil/main.swift
index 29316d16..29584596 100644
--- a/swift-corelibs-foundation/Sources/plutil/main.swift
+++ b/swift-corelibs-foundation/Sources/plutil/main.swift
@@ -15,6 +15,9 @@ import Glibc
#elseif canImport(Musl)
import Foundation
import Musl
+#elseif canImport(Bionic)
+import Foundation
+import Bionic
#elseif canImport(CRT)
import Foundation
import CRT
diff --git a/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift b/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift
index d515a63f..fb037e24 100644
--- a/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift
+++ b/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift
@@ -19,6 +19,8 @@ import FoundationNetworking
#endif
#if os(Windows)
import WinSDK
+#elseif canImport(Android)
+import Android
#endif
enum HelperCheckStatus : Int32 {