Skip to content

Rewrite of PR #1103 + addition Android specific patches #1113

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

#endif

#if defined(__ANDROID__)
#include <linux/prctl.h>
#endif

#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
#else
Expand Down Expand Up @@ -1319,6 +1323,8 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
pthread_setname_np(name);
#elif defined(__ANDROID__)
prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0);
#elif DEPLOYMENT_TARGET_LINUX
pthread_setname_np(pthread_self(), name);
#endif
Expand All @@ -1327,6 +1333,8 @@ CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
return pthread_getname_np(pthread_self(), buf, length);
#elif defined(__ANDROID__)
prctl(PR_GET_NAME, (unsigned long) buf, 0, 0, 0);
#elif DEPLOYMENT_TARGET_LINUX
return pthread_getname_np(pthread_self(), buf, length);
#endif
Expand Down
5 changes: 2 additions & 3 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
#include <CoreFoundation/ForFoundationOnly.h>
#include <fts.h>
#include <pthread.h>
#include <execinfo.h>

#if __has_include(<malloc/malloc.h>)
#include <malloc/malloc.h>
#if __has_include(<execinfo.h>)
#include <execinfo.h>
#endif

#if __has_include(<malloc/malloc.h>)
Expand Down
6 changes: 3 additions & 3 deletions CoreFoundation/URL.subproj/CFURL.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#if __has_include(<sys/syslog.h>)
#include <sys/syslog.h>
#else
#if DEPLOYMENT_TARGET_ANDROID
#include <syslog.h>
#else
#include <sys/syslog.h>
#endif
#include <CoreFoundation/CFURLPriv.h>
#endif
Expand Down
12 changes: 10 additions & 2 deletions Foundation/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ open class Bundle: NSObject {
self.init(path: url.path)
}

public init(for aClass: AnyClass) { NSUnimplemented() }

#if os(Android)
public convenience init(for aClass: AnyClass) {
self.init(path: Bundle.main.bundlePath)!
}
#else
public init(for aClass: AnyClass) {
NSUnimplemented()
}
#endif

public init?(identifier: String) {
super.init()

Expand Down
6 changes: 4 additions & 2 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ open class FileManager : NSObject {

return result
}


#if !os(Android)
/* 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.

This method replaces fileSystemAttributesAtPath:.
Expand Down Expand Up @@ -372,7 +373,8 @@ open class FileManager : NSObject {

return result
}

#endif

/* 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.

This method replaces createSymbolicLinkAtPath:pathContent:
Expand Down
4 changes: 3 additions & 1 deletion Foundation/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ open class Host: NSObject {
}

internal func _resolveCurrent() {
#if !os(Android)
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) != 0 {
return
Expand All @@ -88,6 +89,7 @@ open class Host: NSObject {
}
ifa = ifaValue.ifa_next
}
#endif
}

internal func _resolve() {
Expand Down Expand Up @@ -138,7 +140,7 @@ open class Host: NSObject {
}
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
let lookupInfo = { (content: inout [String], flags: Int32) in
if getnameinfo(info.ai_addr, sa_len, host, socklen_t(NI_MAXHOST), nil, 0, flags) == 0 {
if getnameinfo(info.ai_addr, sa_len, host, numericCast(NI_MAXHOST), nil, 0, flags) == 0 {
content.append(String(cString: host))
}
}
Expand Down
3 changes: 3 additions & 0 deletions Foundation/NSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public func NSLogv(_ format: String, _ args: CVaListPointer) {
CFLog1(kCFLogLevelWarning, message._cfObject)
#else
CFLog1(Int32(kCFLogLevelWarning), message._cfObject)
#if os(Android)
print(message)
#endif
#endif
}

Expand Down
11 changes: 10 additions & 1 deletion Foundation/Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ open class Thread : NSObject {
_cancelled = true
}


#if !os(Android)
private class func backtraceAddresses<T>(_ body: (UnsafeMutablePointer<UnsafeMutableRawPointer?>, Int) -> [T]) -> [T] {
// Same as swift/stdlib/public/runtime/Errors.cpp backtrace
let maxSupportedStackDepth = 128;
Expand Down Expand Up @@ -284,6 +284,15 @@ open class Thread : NSObject {
return symbols
})
}
#else
open class var callStackReturnAddresses: [NSNumber] {
NSUnimplemented()
}

open class var callStackSymbols: [String] {
NSUnimplemented()
}
#endif
}

extension NSNotification.Name {
Expand Down
44 changes: 14 additions & 30 deletions Foundation/URLSession/http/EasyHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ internal final class _EasyHandle {
fileprivate var pauseState: _PauseState = []
internal var timeoutTimer: _TimeoutSource!
internal lazy var errorBuffer = [UInt8](repeating: 0, count: Int(CFURLSessionEasyErrorSize))
#if os(Android)
static fileprivate var _CAInfoFile: UnsafeMutablePointer<Int8>?
#endif

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

#if os(Android)
extension URLSession {

public static func setCAInfoFile(_ _CAInfoFile: String) {
free(_EasyHandle._CAInfoFile)
_CAInfoFile.withCString {
_EasyHandle._CAInfoFile = strdup($0)
}
}

}
#endif

extension CFURLSessionEasyCode : Equatable {
public static func ==(lhs: CFURLSessionEasyCode, rhs: CFURLSessionEasyCode) -> Bool {
return lhs.value == rhs.value
Expand Down
8 changes: 4 additions & 4 deletions TestFoundation/TestNSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: UInt.min).stringValue, "0")
XCTAssertEqual(NSNumber(value: UInt.min + 1).stringValue, "1")
XCTAssertEqual(NSNumber(value: UInt.max).stringValue, "4294967295")
XCTAssertEqual(NSNumber(value: UInt.max - 1).stringValue, "4294967294")
XCTAssertEqual(NSNumber(value: UInt.max - 1 as UInt).stringValue, "4294967294")
} else if UInt.max == UInt64.max {
XCTAssertEqual(NSNumber(value: UInt.min).stringValue, "0")
XCTAssertEqual(NSNumber(value: UInt.min + 1).stringValue, "1")
Expand All @@ -1097,12 +1097,12 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: UInt32.min).stringValue, "0")
XCTAssertEqual(NSNumber(value: UInt32.min + 1).stringValue, "1")
XCTAssertEqual(NSNumber(value: UInt32.max).stringValue, "4294967295")
XCTAssertEqual(NSNumber(value: UInt32.max - 1).stringValue, "4294967294")
XCTAssertEqual(NSNumber(value: UInt32.max - 1 as UInt32).stringValue, "4294967294")

XCTAssertEqual(NSNumber(value: UInt64.min).stringValue, "0")
XCTAssertEqual(NSNumber(value: UInt64.min + 1).stringValue, "1")
XCTAssertEqual(NSNumber(value: UInt64.max).stringValue, "18446744073709551615")
XCTAssertEqual(NSNumber(value: UInt64.max - 1).stringValue, "18446744073709551614")
XCTAssertEqual(NSNumber(value: UInt64.max - 1 as UInt64).stringValue, "18446744073709551614")

if Int.max == Int32.max {
XCTAssertEqual(NSNumber(value: Int.min).stringValue, "-2147483648")
Expand Down Expand Up @@ -1134,7 +1134,7 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: Int64.min).stringValue, "-9223372036854775808")
XCTAssertEqual(NSNumber(value: Int64.min + 1).stringValue, "-9223372036854775807")
XCTAssertEqual(NSNumber(value: Int64.max).stringValue, "9223372036854775807")
XCTAssertEqual(NSNumber(value: Int64.max - 1).stringValue, "9223372036854775806")
XCTAssertEqual(NSNumber(value: Int64.max - 1 as Int64).stringValue, "9223372036854775806")
}

func test_Equals() {
Expand Down
51 changes: 0 additions & 51 deletions android/README.md

This file was deleted.

21 changes: 0 additions & 21 deletions android/builder.sh

This file was deleted.

21 changes: 0 additions & 21 deletions android/install.sh

This file was deleted.

22 changes: 0 additions & 22 deletions android/package.sh

This file was deleted.

17 changes: 0 additions & 17 deletions android/prepare.sh

This file was deleted.

Loading