Skip to content

Commit e254279

Browse files
committed
Changes required for swift-4.1-branch port to Android
1 parent 6c54c84 commit e254279

File tree

16 files changed

+58
-32
lines changed

16 files changed

+58
-32
lines changed

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ extern void __CFGenericValidateType_(CFTypeRef cf, CFTypeID type, const char *fu
216216
#define __CFBitfield64GetValue(V, N1, N2) (((V) & __CFBitfield64Mask(N1, N2)) >> (N2))
217217
#define __CFBitfield64SetValue(V, N1, N2, X) ((V) = ((V) & ~__CFBitfield64Mask(N1, N2)) | ((((uint64_t)X) << (N2)) & __CFBitfield64Mask(N1, N2)))
218218

219-
#if __LP64__
219+
#if __LP64__ || DEPLOYMENT_TARGET_ANDROID
220220
typedef uint64_t __CFInfoType;
221221
#define __CFInfoMask(N1, N2) __CFBitfield64Mask(N1, N2)
222222
#else

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ CF_EXPORT CFStringRef CFCopyFullUserName(void) {
297297
uid_t euid;
298298
__CFGetUGIDs(&euid, NULL);
299299
struct passwd *upwd = getpwuid(euid ? euid : getuid());
300+
#if DEPLOYMENT_TARGET_ANDROID
301+
#define pw_gecos pw_name
302+
#endif
300303
if (upwd && upwd->pw_gecos) {
301304
result = CFStringCreateWithCString(kCFAllocatorSystemDefault, upwd->pw_gecos, kCFPlatformInterfaceStringEncoding);
302305
}

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ void CFLog1(CFLogLevel lev, CFStringRef message) {
10001000
CFStringGetCString(message, buffer, maxLength, encoding);
10011001

10021002
__android_log_print(priority, tag, "%s", buffer);
1003+
fprintf(stderr, "%s\n", buffer);
10031004

10041005
if (buffer != &stack_buffer[0]) free(buffer);
10051006
#else

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((n
403403
static inline int _direntNameLength(struct dirent *entry) {
404404
#ifdef _D_EXACT_NAMLEN // defined on Linux
405405
return _D_EXACT_NAMLEN(entry);
406+
#elif DEPLOYMENT_TARGET_ANDROID
407+
return strlen(entry->d_name);
406408
#else
407409
return entry->d_namlen;
408410
#endif

CoreFoundation/NumberDate.subproj/CFTimeZone.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,14 @@ static CFTimeZoneRef __CFTimeZoneCreateSystem(void) {
813813
CFRelease(name);
814814
if (result) return result;
815815
}
816+
#if DEPLOYMENT_TARGET_ANDROID
817+
// Timezone database by name not available on Android.
818+
// Approximate with gmtoff - could be general default.
819+
struct tm info;
820+
time_t now = time(NULL);
821+
if (NULL != localtime_r(&now, &info))
822+
return CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorSystemDefault, info.tm_gmtoff);
823+
#endif
816824
return CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorSystemDefault, 0.0);
817825
}
818826

CoreFoundation/PlugIn.subproj/CFBundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ static CFBundleRef _CFBundleCreate(CFAllocatorRef allocator, CFURLRef bundleURL,
714714

715715
bundle->_url = newURL;
716716

717-
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
717+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS && !DEPLOYMENT_TARGET_ANDROID
718718
bundle->_isFHSInstalledBundle = _CFBundleURLIsForFHSInstalledBundle(newURL);
719719
#endif
720720

CoreFoundation/PlugIn.subproj/CFBundle_Main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,20 @@ static CFBundleRef _CFBundleGetMainBundleAlreadyLocked(void) {
7979
CFStringRef str = NULL;
8080
CFURLRef executableURL = NULL, bundleURL = NULL;
8181
_initedMainBundle = true;
82+
#if DEPLOYMENT_TARGET_ANDROID
83+
const char *bundlePath = getenv("CFFIXED_USER_HOME") ?: getenv("TMPDIR") ?: "/data/local/tmp";
84+
CFStringRef bundleRef = CFStringCreateWithFileSystemRepresentation(kCFAllocatorNull, bundlePath);
85+
bundleURL = CFRetain(CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, bundleRef,
86+
PLATFORM_PATH_STYLE, false));
87+
CFRelease(bundleRef);
88+
#else
8289
processPath = _CFProcessPath();
8390
if (processPath) {
8491
str = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, processPath);
8592
if (!executableURL) executableURL = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str, PLATFORM_PATH_STYLE, false);
8693
}
8794
if (executableURL) bundleURL = _CFBundleCopyBundleURLForExecutableURL(executableURL);
95+
#endif
8896
if (bundleURL) {
8997
// make sure that main bundle has executable path
9098
//??? what if we are not the main executable in the bundle?

CoreFoundation/Preferences.subproj/CFPreferences.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,11 @@ static CFURLRef _CFPreferencesURLForStandardDomainWithSafetyLevel(CFStringRef do
440440
CFURLRef theURL = NULL;
441441
CFAllocatorRef prefAlloc = __CFPreferencesAllocator();
442442
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_WINDOWS
443+
#if DEPLOYMENT_TARGET_ANDROID
444+
CFURLRef prefDir = CFBundleCopyBundleURL(CFBundleGetMainBundle());
445+
#else
443446
CFURLRef prefDir = _preferencesDirectoryForUserHostSafetyLevel(userName, hostName, safeLevel);
447+
#endif
444448
CFStringRef appName;
445449
CFStringRef fileName;
446450
Boolean mustFreeAppName = false;

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-
#elif __has_include(<syslog.h>)
28+
#if __has_include(<syslog.h>)
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,13 @@ open class FileManager : NSObject {
375375

376376
This method replaces fileSystemAttributesAtPath:.
377377
*/
378+
#if os(Android)
379+
@available(*, unavailable, message: "Unsuppported on this platform")
380+
open func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
381+
NSUnsupported()
382+
}
383+
#else
378384
open func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
379-
#if os(Android)
380-
NSUnimplemented()
381-
#else
382385
// statvfs(2) doesn't support 64bit inode on Darwin (apfs), fallback to statfs(2)
383386
#if os(macOS) || os(iOS)
384387
var s = statfs()
@@ -407,8 +410,8 @@ open class FileManager : NSObject {
407410
result[.systemFreeNodes] = NSNumber(value: UInt64(s.f_ffree))
408411

409412
return result
410-
#endif
411413
}
414+
#endif
412415

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

Foundation/NSCFString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ internal final class _NSCFConstantString : _NSCFString {
7171
// FIXME: Split expression as a work-around for slow type
7272
// checking (tracked by SR-5322).
7373
let offTemp1 = MemoryLayout<OpaquePointer>.size + MemoryLayout<uintptr_t>.size
74-
let offTemp2 = MemoryLayout<_CFInfo>.size
75-
return offTemp1 + offTemp2 + MemoryLayout<UnsafePointer<UInt8>>.size
74+
let offset = offTemp1 + MemoryLayout<_CFInfo>.size
75+
return offset + MemoryLayout<UnsafePointer<UInt8>>.size
7676
}
7777

7878
private var _lenPtr : UnsafeMutableRawPointer {

Foundation/NSTimeZone.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
179179
extension NSTimeZone {
180180

181181
open class var system: TimeZone {
182-
#if os(Android)
183-
var now = time(nil), info = tm()
184-
if localtime_r(&now, &info) != nil {
185-
// NOTE: this is not a real time zone but a fixed offset from GMT.
186-
// It will be incorrect outside the current daylight saving period.
187-
return TimeZone(reference: NSTimeZone(forSecondsFromGMT: info.tm_gmtoff))
188-
}
189-
#endif
190182
return CFTimeZoneCopySystem()._swiftObject
191183
}
192184

TestFoundation/TestFileManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ class TestFileManager : XCTestCase {
275275
}
276276

277277
func test_fileSystemAttributes() {
278+
#if !os(Android)
278279
let fm = FileManager.default
279280
let path = NSTemporaryDirectory()
280281

@@ -306,6 +307,7 @@ class TestFileManager : XCTestCase {
306307
} catch let err {
307308
XCTFail("\(err)")
308309
}
310+
#endif
309311
}
310312

311313
func test_setFileAttributes() {

TestFoundation/TestNSAttributedString.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,6 @@ class TestNSAttributedString : XCTestCase {
192192
}
193193

194194
func test_enumerateAttributes() {
195-
#if os(Android)
196-
// Invalid dictionary returned by CFAttributedStringGetAttributesAndLongestEffectiveRange
197-
XCTFail("Intermittent failures on Android")
198-
#else
199195
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
200196

201197
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
@@ -254,7 +250,6 @@ class TestNSAttributedString : XCTestCase {
254250
}
255251
XCTAssertEqual(rangeDescriptionString, "(0,10)")
256252
XCTAssertEqual(attrsDescriptionString, "[attribute.placeholder.key1:attribute.placeholder.value1]")
257-
#endif
258253
}
259254

260255
func test_copy() {

TestFoundation/TestNSNumber.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,33 +1087,33 @@ class TestNSNumber : XCTestCase {
10871087
XCTAssertEqual(NSNumber(value: UInt.min).stringValue, "0")
10881088
XCTAssertEqual(NSNumber(value: UInt.min + 1).stringValue, "1")
10891089
XCTAssertEqual(NSNumber(value: UInt.max).stringValue, "4294967295")
1090-
XCTAssertEqual(NSNumber(value: UInt.max - 1).stringValue, "4294967294")
1090+
XCTAssertEqual(NSNumber(value: UInt.max - 1 as UInt).stringValue, "4294967294")
10911091
} else if UInt.max == UInt64.max {
10921092
XCTAssertEqual(NSNumber(value: UInt.min).stringValue, "0")
10931093
XCTAssertEqual(NSNumber(value: UInt.min + 1).stringValue, "1")
10941094
XCTAssertEqual(NSNumber(value: UInt.max).stringValue, "18446744073709551615")
1095-
XCTAssertEqual(NSNumber(value: UInt.max - 1).stringValue, "18446744073709551614")
1095+
XCTAssertEqual(NSNumber(value: UInt.max - 1 as UInt).stringValue, "18446744073709551614")
10961096
}
10971097

10981098
XCTAssertEqual(NSNumber(value: UInt8.min).stringValue, "0")
10991099
XCTAssertEqual(NSNumber(value: UInt8.min + 1).stringValue, "1")
11001100
XCTAssertEqual(NSNumber(value: UInt8.max).stringValue, "255")
1101-
XCTAssertEqual(NSNumber(value: UInt8.max - 1).stringValue, "254")
1101+
XCTAssertEqual(NSNumber(value: UInt8.max - 1 as UInt8).stringValue, "254")
11021102

11031103
XCTAssertEqual(NSNumber(value: UInt16.min).stringValue, "0")
11041104
XCTAssertEqual(NSNumber(value: UInt16.min + 1).stringValue, "1")
11051105
XCTAssertEqual(NSNumber(value: UInt16.max).stringValue, "65535")
1106-
XCTAssertEqual(NSNumber(value: UInt16.max - 1).stringValue, "65534")
1106+
XCTAssertEqual(NSNumber(value: UInt16.max - 1 as UInt16).stringValue, "65534")
11071107

11081108
XCTAssertEqual(NSNumber(value: UInt32.min).stringValue, "0")
11091109
XCTAssertEqual(NSNumber(value: UInt32.min + 1).stringValue, "1")
11101110
XCTAssertEqual(NSNumber(value: UInt32.max).stringValue, "4294967295")
1111-
XCTAssertEqual(NSNumber(value: UInt32.max - 1).stringValue, "4294967294")
1111+
XCTAssertEqual(NSNumber(value: UInt32.max - 1 as UInt32).stringValue, "4294967294")
11121112

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

11181118
if Int.max == Int32.max {
11191119
XCTAssertEqual(NSNumber(value: Int.min).stringValue, "-2147483648")

0 commit comments

Comments
 (0)