Skip to content

Commit 16f83dd

Browse files
johnno1962parkera
authored andcommitted
Port of Foundation to Android (#622)
* Port of Foundation to Android * Port of Foundation to Android * Missing -lcurl for Linux builds of Foundation * README and script tidy-up * Response to comments * NSGeonetry, Operation queues fixed * More reponses to comments * NSLog now working * Update README * Update README * CGFloat.swift problems resolved, NSGeometry, NSScanner.swift updated * Glibc is already an @_exported import in NSSwiftRuntime.swift * Glibc is already an @_exported import in NSSwiftRuntime.swift * #import <stdarg.h> problem went away with rebuild * Update scripts * move to SEEK_* macros * Better logging and restructuring * CONST_STRING_DECL() problem resolved * Jira for CFStringGetCString() crash with zero length strings * Only use heap if required in logging * Reinstate fix for SR-2666 * Reinstate fix for SR-2666 * Ensure NSUnimplemented() functions logged * one true brace style * Final tweaks?
1 parent c2ecbeb commit 16f83dd

28 files changed

+302
-22
lines changed

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ CF_PRIVATE Boolean __CFProcessIsRestricted();
339339
#define STACK_BUFFER_DECL(T, N, C) T N[C]
340340
#endif
341341

342+
#ifdef __ANDROID__
343+
// Avoids crashes on Android
344+
// https://bugs.swift.org/browse/SR-2587
345+
// https://bugs.swift.org/browse/SR-2588
346+
// Seemed to be a linker/relocation? problem.
347+
// CFStrings using CONST_STRING_DECL() were not working
348+
// Applies reference to _NSCFConstantString's isa here
349+
// rather than using a linker option to create an alias.
350+
#define __CFConstantStringClassReference _TMC10Foundation19_NSCFConstantString
351+
#endif
342352

343353
CF_EXPORT void * __CFConstantStringClassReferencePtr;
344354
#if defined(__CONSTANT_CFSTRINGS__)

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ const char *_CFProcessPath(void) {
157157

158158
#if DEPLOYMENT_TARGET_LINUX
159159
#include <unistd.h>
160+
#if __has_include(<syscall.h>)
160161
#include <syscall.h>
162+
#else
163+
#include <sys/syscall.h>
164+
#endif
161165

162166
Boolean _CFIsMainThread(void) {
163167
return syscall(SYS_gettid) == getpid();

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#if DEPLOYMENT_TARGET_WINDOWS
2626
#include <process.h>
2727
#endif
28+
#ifdef __ANDROID__
29+
#include <android/log.h>
30+
#endif
2831
#include <math.h>
2932
#include <string.h>
3033
#include <stdio.h>
@@ -764,7 +767,45 @@ void CFLog(CFLogLevel lev, CFStringRef format, ...) {
764767
#if DEPLOYMENT_RUNTIME_SWIFT
765768
// Temporary as Swift cannot import varag C functions
766769
void CFLog1(CFLogLevel lev, CFStringRef message) {
770+
#ifdef __ANDROID__
771+
android_LogPriority priority = ANDROID_LOG_UNKNOWN;
772+
switch (lev) {
773+
case kCFLogLevelEmergency: priority = ANDROID_LOG_FATAL; break;
774+
case kCFLogLevelAlert: priority = ANDROID_LOG_ERROR; break;
775+
case kCFLogLevelCritical: priority = ANDROID_LOG_ERROR; break;
776+
case kCFLogLevelError: priority = ANDROID_LOG_ERROR; break;
777+
case kCFLogLevelWarning: priority = ANDROID_LOG_WARN; break;
778+
case kCFLogLevelNotice: priority = ANDROID_LOG_WARN; break;
779+
case kCFLogLevelInfo: priority = ANDROID_LOG_INFO; break;
780+
case kCFLogLevelDebug: priority = ANDROID_LOG_DEBUG; break;
781+
}
782+
783+
if (message == NULL) message = CFSTR("NULL");
784+
785+
char stack_buffer[1024] = { 0 };
786+
char *buffer = &stack_buffer[0];
787+
CFStringEncoding encoding = kCFStringEncodingUTF8;
788+
CFIndex maxLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(message), encoding) + 1;
789+
790+
if (maxLength > sizeof(stack_buffer) / sizeof(stack_buffer[0])) {
791+
buffer = calloc(sizeof(char), maxLength);
792+
}
793+
794+
if (maxLength == 1) {
795+
// was crashing with zero length strings
796+
// https://bugs.swift.org/browse/SR-2666
797+
strcpy(buffer, " "); // log empty string
798+
}
799+
else
800+
CFStringGetCString(message, buffer, maxLength, encoding);
801+
802+
const char *tag = "Swift"; // process name not available from NDK
803+
__android_log_print(priority, tag, "%s", buffer);
804+
805+
if (buffer != &stack_buffer[0]) free(buffer);
806+
#else
767807
CFLog(lev, CFSTR("%@"), message);
808+
#endif
768809
}
769810
#endif
770811

@@ -1265,7 +1306,7 @@ CFDictionaryRef __CFGetEnvironment() {
12651306
extern char **environ;
12661307
char **envp = environ;
12671308
#elif DEPLOYMENT_TARGET_LINUX
1268-
#ifndef environ
1309+
#if !defined(environ) && !defined(__ANDROID__)
12691310
#define environ __environ
12701311
#endif
12711312
char **envp = environ;

CoreFoundation/Base.subproj/CoreFoundation_Prefix.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ typedef int boolean_t;
178178

179179
#include <pthread.h>
180180

181+
#ifdef __ANDROID__
182+
typedef unsigned long fd_mask;
183+
#endif
184+
185+
#ifndef __ANDROID__
181186
CF_INLINE size_t
182187
strlcpy(char * dst, const char * src, size_t maxlen) {
183188
const size_t srclen = strlen(src);
@@ -203,6 +208,7 @@ strlcat(char * dst, const char * src, size_t maxlen) {
203208
}
204209
return dstlen + srclen;
205210
}
211+
#endif
206212

207213
#define issetugid() 0
208214

CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include <string.h>
4040
#include <time.h>
4141

42+
#if __has_include(<netdb.h>)
43+
#include <netdb.h> // for NSHost.swift
44+
#endif
45+
4246
#if defined(__STDC_VERSION__) && (199901L <= __STDC_VERSION__)
4347

4448
#include <inttypes.h>

CoreFoundation/NumberDate.subproj/CFTimeZone.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
3333
#include <dirent.h>
3434
#include <unistd.h>
35+
#if __has_include(<sys/fcntl.h>)
3536
#include <sys/fcntl.h>
37+
#else
38+
#include <fcntl.h>
39+
#endif
3640
#endif
3741
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
3842
#include <tzfile.h>

CoreFoundation/PlugIn.subproj/CFBundle_InfoPlist.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_FREEBSD
2727
#include <dirent.h>
28+
#if __has_include(<sys/sysctl.h>)
2829
#include <sys/sysctl.h>
30+
#endif
2931
#include <sys/mman.h>
3032
#endif
3133

CoreFoundation/PlugIn.subproj/CFBundle_Resources.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
3535
#include <unistd.h>
36+
#if __has_include(<sys/sysctl.h>)
3637
#include <sys/sysctl.h>
38+
#endif
3739
#include <sys/stat.h>
3840
#include <dirent.h>
3941
#endif

CoreFoundation/URL.subproj/CFURL.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
#include <unistd.h>
3131
#include <sys/stat.h>
3232
#include <sys/types.h>
33+
#if __has_include(<sys/syslog.h>)
3334
#include <sys/syslog.h>
35+
#else
36+
#include <syslog.h>
37+
#endif
3438
#include <CoreFoundation/CFURLPriv.h>
3539
#endif
3640

Foundation/NSData.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
423423
repeat {
424424
#if os(OSX) || os(iOS)
425425
bytesWritten = Darwin.write(fd, buf.advanced(by: length - bytesRemaining), bytesRemaining)
426-
#elseif os(Linux)
426+
#elseif os(Linux) || os(Android)
427427
bytesWritten = Glibc.write(fd, buf.advanced(by: length - bytesRemaining), bytesRemaining)
428428
#endif
429429
} while (bytesWritten < 0 && errno == EINTR)
@@ -444,7 +444,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
444444
// Preserve permissions.
445445
var info = stat()
446446
if lstat(path, &info) == 0 {
447-
mode = info.st_mode
447+
mode = mode_t(info.st_mode)
448448
} else if errno != ENOENT && errno != ENAMETOOLONG {
449449
throw _NSErrorWithErrno(errno, reading: false, path: path)
450450
}

0 commit comments

Comments
 (0)