Skip to content

Commit 7d93003

Browse files
authored
Add support for WASI in CFUtilities.c (swiftlang#3031)
Processes, file access, platform info, and bundles aren't available on WASI, so these have to be disabled when compiling for that platform.
1 parent 347a439 commit 7d93003

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

CoreFoundation/Base.subproj/CFUtilities.c

+21-9
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
#include <CoreFoundation/CFPriv.h>
1919
#include "CFInternal.h"
2020
#include "CFLocaleInternal.h"
21+
#if !TARGET_OS_WASI
2122
#include "CFBundle_Internal.h"
23+
#endif
2224
#include <CoreFoundation/CFPriv.h>
2325
#if TARGET_OS_MAC || TARGET_OS_WIN32
2426
#include <CoreFoundation/CFBundle.h>
2527
#endif
2628
#include <CoreFoundation/CFURLAccess.h>
29+
#if !TARGET_OS_WASI
2730
#include <CoreFoundation/CFPropertyList.h>
31+
#endif
2832
#if TARGET_OS_WIN32
2933
#include <process.h>
3034
#endif
@@ -65,7 +69,7 @@
6569
#include <os/lock.h>
6670
#endif
6771

68-
#if TARGET_OS_LINUX || TARGET_OS_BSD
72+
#if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
6973
#include <string.h>
7074
#include <sys/mman.h>
7175
#endif
@@ -354,6 +358,7 @@ static CFDictionaryRef _CFCopyVersionDictionary(CFStringRef path) {
354358
return (CFDictionaryRef)plist;
355359
}
356360

361+
#if !TARGET_OS_WASI
357362
CFStringRef _CFCopySystemVersionDictionaryValue(CFStringRef key) {
358363
CFStringRef versionString;
359364
CFDictionaryRef dict = _CFCopyServerVersionDictionary();
@@ -462,15 +467,13 @@ CONST_STRING_DECL(_kCFSystemVersionProductUserVisibleVersionKey, "ProductUserVis
462467
CONST_STRING_DECL(_kCFSystemVersionBuildVersionKey, "ProductBuildVersion")
463468
CONST_STRING_DECL(_kCFSystemVersionProductVersionStringKey, "Version")
464469
CONST_STRING_DECL(_kCFSystemVersionBuildStringKey, "Build")
470+
#endif
465471

466472

467473
CF_EXPORT Boolean _CFExecutableLinkedOnOrAfter(CFSystemVersion version) {
468474
return true;
469475
}
470476

471-
472-
473-
474477
#if TARGET_OS_OSX
475478
CF_PRIVATE void *__CFLookupCarbonCoreFunction(const char *name) {
476479
static void *image = NULL;
@@ -637,7 +640,8 @@ CF_INLINE BOOL _CFCanChangeEUIDs(void) {
637640
return true;
638641
#endif
639642
}
640-
643+
644+
#if !TARGET_OS_WASI
641645
typedef struct _ugids {
642646
uid_t _euid;
643647
uid_t _egid;
@@ -690,6 +694,7 @@ CF_EXPORT uid_t _CFGetEGID(void) {
690694
__CFGetUGIDs(NULL, &egid);
691695
return egid;
692696
}
697+
#endif // !TARGET_OS_WASI
693698

694699
const char *_CFPrintForDebugger(const void *obj) {
695700
static char *result = NULL;
@@ -900,6 +905,9 @@ static void _populateBanner(char **banner, char **time, char **thread, int *bann
900905
#elif TARGET_OS_WIN32
901906
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%lx] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), GetCurrentThreadId());
902907
asprintf(thread, "%lx", GetCurrentThreadId());
908+
#elif TARGET_OS_WASI
909+
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)pthread_self());
910+
asprintf(thread, "%lx", pthread_self());
903911
#else
904912
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%x] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), (unsigned int)pthread_self());
905913
asprintf(thread, "%lx", pthread_self());
@@ -1080,8 +1088,12 @@ CF_PRIVATE void _CFLogSimple(int32_t lev, char *format, ...) {
10801088

10811089
void CFLog(int32_t lev, CFStringRef format, ...) {
10821090
va_list args;
1083-
va_start(args, format);
1091+
va_start(args, format);
1092+
#if TARGET_OS_WASI
1093+
_CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, NULL);
1094+
#else
10841095
_CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, __builtin_return_address(0));
1096+
#endif
10851097
va_end(args);
10861098
}
10871099

@@ -1333,7 +1345,7 @@ CF_PRIVATE Boolean _CFReadMappedFromFile(CFStringRef path, Boolean map, Boolean
13331345
if (0LL == statBuf.st_size) {
13341346
bytes = malloc(8); // don't return constant string -- it's freed!
13351347
length = 0;
1336-
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
1348+
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
13371349
} else if (map) {
13381350
if((void *)-1 == (bytes = mmap(0, (size_t)statBuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))) {
13391351
int32_t savederrno = errno;
@@ -1628,7 +1640,7 @@ CF_EXPORT Boolean _CFExtensionIsValidToAppend(CFStringRef extension) {
16281640
}
16291641

16301642

1631-
#if DEPLOYMENT_RUNTIME_SWIFT
1643+
#if DEPLOYMENT_RUNTIME_SWIFT && !TARGET_OS_WASI
16321644

16331645
CFDictionaryRef __CFGetEnvironment() {
16341646
static dispatch_once_t once = 0L;
@@ -1689,4 +1701,4 @@ int32_t __CFGetPid() {
16891701
return getpid();
16901702
}
16911703

1692-
#endif
1704+
#endif // DEPLOYMENT_RUNTIME_SWIFT && !TARGET_OS_WASI

0 commit comments

Comments
 (0)