Skip to content

Commit 3fd25e7

Browse files
committed
Changes required for Android port
1 parent a0f8df7 commit 3fd25e7

File tree

15 files changed

+57
-214
lines changed

15 files changed

+57
-214
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
#endif
3333

34+
#if defined(__ANDROID__)
35+
#include <linux/prctl.h>
36+
#endif
37+
3438
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS
3539
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
3640
#else
@@ -1319,6 +1323,8 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
13191323
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
13201324
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13211325
pthread_setname_np(name);
1326+
#elif defined(__ANDROID__)
1327+
prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0);
13221328
#elif DEPLOYMENT_TARGET_LINUX
13231329
pthread_setname_np(pthread_self(), name);
13241330
#endif
@@ -1327,6 +1333,8 @@ CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
13271333
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
13281334
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13291335
return pthread_getname_np(pthread_self(), buf, length);
1336+
#elif defined(__ANDROID__)
1337+
prctl(PR_GET_NAME, (unsigned long) buf, 0, 0, 0);
13301338
#elif DEPLOYMENT_TARGET_LINUX
13311339
return pthread_getname_np(pthread_self(), buf, length);
13321340
#endif

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
#include <CoreFoundation/ForFoundationOnly.h>
2828
#include <fts.h>
2929
#include <pthread.h>
30-
#include <execinfo.h>
3130

32-
#if __has_include(<malloc/malloc.h>)
33-
#include <malloc/malloc.h>
31+
#if __has_include(<execinfo.h>)
32+
#include <execinfo.h>
3433
#endif
3534

3635
#if __has_include(<malloc/malloc.h>)

CoreFoundation/URL.subproj/CFURL.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
#include <unistd.h>
2626
#include <sys/stat.h>
2727
#include <sys/types.h>
28-
#if __has_include(<sys/syslog.h>)
29-
#include <sys/syslog.h>
30-
#else
28+
#if DEPLOYMENT_TARGET_ANDROID
3129
#include <syslog.h>
30+
#else
31+
#include <sys/syslog.h>
3232
#endif
3333
#include <CoreFoundation/CFURLPriv.h>
3434
#endif

Foundation/Bundle.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ open class Bundle: NSObject {
5353
self.init(path: url.path)
5454
}
5555

56-
public init(for aClass: AnyClass) { NSUnimplemented() }
57-
56+
#if os(Android)
57+
public convenience init(for aClass: AnyClass) {
58+
self.init(path: Bundle.main.bundlePath)!
59+
}
60+
#else
61+
public init(for aClass: AnyClass) {
62+
NSUnimplemented()
63+
}
64+
#endif
65+
5866
public init?(identifier: String) {
5967
super.init()
6068

Foundation/FileManager.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ open class FileManager : NSObject {
337337

338338
return result
339339
}
340-
340+
341+
#if !os(Android)
341342
/* attributesOfFileSystemForPath:error: returns an NSDictionary of key/value pairs containing the attributes of the filesystem containing the provided path. If this method returns 'nil', an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.
342343

343344
This method replaces fileSystemAttributesAtPath:.
@@ -372,7 +373,8 @@ open class FileManager : NSObject {
372373

373374
return result
374375
}
375-
376+
#endif
377+
376378
/* createSymbolicLinkAtPath:withDestination:error: returns YES if the symbolic link that point at 'destPath' was able to be created at the location specified by 'path'. If this method returns NO, the link was unable to be created and an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.
377379

378380
This method replaces createSymbolicLinkAtPath:pathContent:

Foundation/Host.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ open class Host: NSObject {
6565
}
6666

6767
internal func _resolveCurrent() {
68+
#if !os(Android)
6869
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
6970
if getifaddrs(&ifaddr) != 0 {
7071
return
@@ -88,6 +89,7 @@ open class Host: NSObject {
8889
}
8990
ifa = ifaValue.ifa_next
9091
}
92+
#endif
9193
}
9294

9395
internal func _resolve() {
@@ -138,7 +140,7 @@ open class Host: NSObject {
138140
}
139141
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
140142
let lookupInfo = { (content: inout [String], flags: Int32) in
141-
if getnameinfo(info.ai_addr, sa_len, host, socklen_t(NI_MAXHOST), nil, 0, flags) == 0 {
143+
if getnameinfo(info.ai_addr, sa_len, host, numericCast(NI_MAXHOST), nil, 0, flags) == 0 {
142144
content.append(String(cString: host))
143145
}
144146
}

Foundation/NSLog.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public func NSLogv(_ format: String, _ args: CVaListPointer) {
3333
CFLog1(kCFLogLevelWarning, message._cfObject)
3434
#else
3535
CFLog1(Int32(kCFLogLevelWarning), message._cfObject)
36+
#if os(Android)
37+
print(message)
38+
#endif
3639
#endif
3740
}
3841

Foundation/Thread.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ open class Thread : NSObject {
250250
_cancelled = true
251251
}
252252

253-
253+
#if !os(Android)
254254
private class func backtraceAddresses<T>(_ body: (UnsafeMutablePointer<UnsafeMutableRawPointer?>, Int) -> [T]) -> [T] {
255255
// Same as swift/stdlib/public/runtime/Errors.cpp backtrace
256256
let maxSupportedStackDepth = 128;
@@ -284,6 +284,15 @@ open class Thread : NSObject {
284284
return symbols
285285
})
286286
}
287+
#else
288+
open class var callStackReturnAddresses: [NSNumber] {
289+
NSUnimplemented()
290+
}
291+
292+
open class var callStackSymbols: [String] {
293+
NSUnimplemented()
294+
}
295+
#endif
287296
}
288297

289298
extension NSNotification.Name {

Foundation/URLSession/http/EasyHandle.swift

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ internal final class _EasyHandle {
5757
fileprivate var pauseState: _PauseState = []
5858
internal var timeoutTimer: _TimeoutSource!
5959
internal lazy var errorBuffer = [UInt8](repeating: 0, count: Int(CFURLSessionEasyErrorSize))
60-
#if os(Android)
61-
static fileprivate var _CAInfoFile: UnsafeMutablePointer<Int8>?
62-
#endif
6360

6461
init(delegate: _EasyHandleDelegate) {
6562
self.delegate = delegate
@@ -107,7 +104,7 @@ internal protocol _EasyHandleDelegate: class {
107104
/// - returns: the number of bytes written to the `data` buffer, or `nil` to stop the current transfer immediately.
108105
func fill(writeBuffer buffer: UnsafeMutableBufferPointer<Int8>) -> _EasyHandle._WriteBufferResult
109106
/// The transfer for this handle completed.
110-
/// - parameter errorCode: An NSURLError code, or `nil` if no error occured.
107+
/// - parameter error: An NSError, or `nil` if no error occured.
111108
func transferCompleted(withError error: NSError?)
112109
/// Seek the input stream to the given position
113110
func seekInputStream(to position: UInt64) throws
@@ -172,20 +169,20 @@ extension _EasyHandle {
172169
let protocols = (CFURLSessionProtocolHTTP | CFURLSessionProtocolHTTPS)
173170
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionPROTOCOLS, protocols).asError()
174171
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionREDIR_PROTOCOLS, protocols).asError()
175-
#if os(Android)
176-
// See https://curl.haxx.se/docs/sslcerts.html
177-
// For SSL to work you need "cacert.pem" to be accessable
178-
// at the path pointed to by the URLSessionCAInfo env var.
179-
// Downloadable here: https://curl.haxx.se/ca/cacert.pem
180-
if let caInfo = _EasyHandle._CAInfoFile {
181-
if String(cString: caInfo) == "UNSAFE_SSL_NOVERIFY" {
182-
try! CFURLSession_easy_setopt_int(rawHandle, CFURLSessionOptionSSL_VERIFYPEER, 0).asError()
183-
}
184-
else {
185-
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, caInfo).asError()
186-
}
172+
#if os(Android)
173+
// See https://curl.haxx.se/docs/sslcerts.html
174+
// For SSL on Android you need a "cacert.pem" to be
175+
// accessible at the path pointed to by this env var.
176+
// Downloadable here: https://curl.haxx.se/ca/cacert.pem
177+
if let caInfo = getenv("URLSessionCertificateAuthorityInfoFile") {
178+
if String(cString: caInfo) == "INSECURE_SSL_NO_VERIFY" {
179+
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionSSL_VERIFYPEER, 0).asError()
180+
}
181+
else {
182+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, caInfo).asError()
187183
}
188-
#endif
184+
}
185+
#endif
189186
//TODO: Added in libcurl 7.45.0
190187
//TODO: Set default protocol for schemeless URLs
191188
//CURLOPT_DEFAULT_PROTOCOL available only in libcurl 7.45.0
@@ -632,19 +629,6 @@ extension _EasyHandle._CurlStringList {
632629
}
633630
}
634631

635-
#if os(Android)
636-
extension URLSession {
637-
638-
public static func setCAInfoFile(_ _CAInfoFile: String) {
639-
free(_EasyHandle._CAInfoFile)
640-
_CAInfoFile.withCString {
641-
_EasyHandle._CAInfoFile = strdup($0)
642-
}
643-
}
644-
645-
}
646-
#endif
647-
648632
extension CFURLSessionEasyCode : Equatable {
649633
public static func ==(lhs: CFURLSessionEasyCode, rhs: CFURLSessionEasyCode) -> Bool {
650634
return lhs.value == rhs.value

android/README.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

android/builder.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

android/install.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

android/package.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

android/prepare.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)