Skip to content

Commit 075707c

Browse files
authored
Merge branch 'swiftwasm' into main
2 parents 1bc603e + 6856f76 commit 075707c

File tree

92 files changed

+870
-179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+870
-179
lines changed

.github/pull.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: "1"
2+
rules:
3+
- base: swiftwasm
4+
upstream: main
5+
mergeMethod: merge
6+
- base: main
7+
upstream: apple:main
8+
mergeMethod: hardreset
9+
10+
- base: release/5.3
11+
upstream: apple:release/5.3
12+
mergeMethod: hardreset
13+
- base: swiftwasm-release/5.3
14+
upstream: release/5.3
15+
mergeMethod: merge
16+
17+
- base: release/5.4
18+
upstream: apple:release/5.4
19+
mergeMethod: hardreset
20+
- base: swiftwasm-release/5.4
21+
upstream: release/5.4
22+
mergeMethod: merge
23+
24+
- base: release/5.5
25+
upstream: apple:release/5.5
26+
mergeMethod: hardreset
27+
- base: swiftwasm-release/5.5
28+
upstream: release/5.5
29+
mergeMethod: merge
30+
31+
label: ":arrow_heading_down: Upstream Tracking"

CMakeLists.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ set(CF_DEPLOYMENT_SWIFT YES CACHE BOOL "Build for Swift" FORCE)
5757

5858
set(SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
5959
set(BUILD_SHARED_LIBS NO)
60+
6061
add_subdirectory(CoreFoundation EXCLUDE_FROM_ALL)
6162
set(BUILD_SHARED_LIBS ${SAVED_BUILD_SHARED_LIBS})
6263

@@ -75,6 +76,7 @@ add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/uuid-headers/uuid/uuid.h
7576
add_custom_target(uuid-headers
7677
DEPENDS ${CMAKE_BINARY_DIR}/uuid-headers/uuid/uuid.h)
7778
add_dependencies(CoreFoundation uuid-headers)
79+
7880
target_include_directories(CoreFoundation PRIVATE
7981
${CMAKE_BINARY_DIR}/uuid-headers
8082
${CMAKE_CURRENT_BINARY_DIR}/CoreFoundation.framework/Headers)
@@ -116,10 +118,21 @@ install(DIRECTORY
116118
DESTINATION
117119
${swift_lib_dir}/CoreFoundation
118120
FILES_MATCHING PATTERN "*.h")
119-
install(FILES
120-
CoreFoundation/Base.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>module.map
121-
DESTINATION
122-
${swift_lib_dir}/CoreFoundation)
121+
122+
# TODO(katei): Remove wasm32-unknown-wasi specific modulemap which is required to link icui18n
123+
# This workaround can be removed after https://github.com/apple/swift/pull/35936
124+
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
125+
install(FILES
126+
CoreFoundation/Base.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>wasm32-unknown-wasi.modulemap
127+
DESTINATION
128+
${swift_lib_dir}/CoreFoundation
129+
RENAME module.map)
130+
else()
131+
install(FILES
132+
CoreFoundation/Base.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>module.map
133+
DESTINATION
134+
${swift_lib_dir}/CoreFoundation)
135+
endif()
123136
install(DIRECTORY
124137
${CMAKE_CURRENT_BINARY_DIR}/CFURLSessionInterface.framework/Headers/
125138
DESTINATION

CoreFoundation/Base.subproj/CFBase.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
#define __has_extension(x) 0
6060
#endif
6161

62+
#if __has_attribute(swiftcall)
63+
#define CF_CC_swift __attribute__((swiftcall))
64+
#else
65+
#define CF_CC_swift
66+
#endif
67+
6268
#if defined(__GNUC__) || TARGET_OS_WIN32
6369
#include <stdint.h>
6470
#include <stdbool.h>

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838
#include <unistd.h>
3939
#include <dirent.h>
4040
#include <sys/types.h>
41+
42+
#if !TARGET_OS_WASI
4143
#include <pwd.h>
44+
#else
45+
#define WASI_D_NAME_SIZE 256
46+
#endif
47+
4248
#include <fcntl.h>
4349

4450
#define statinfo stat
@@ -341,7 +347,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
341347
FindClose(handle);
342348
pathBuf[pathLength] = '\0';
343349

344-
#elif TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
350+
#elif TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
345351
uint8_t extBuff[CFMaxPathSize];
346352
int extBuffInteriorDotCount = 0; //people insist on using extensions like ".trace.plist", so we need to know how many dots back to look :(
347353

@@ -437,7 +443,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
437443
dirURL = CFURLCreateFromFileSystemRepresentation(alloc, (uint8_t *)dirPath, pathLength, true);
438444
releaseBase = true;
439445
}
440-
#if !defined(__OpenBSD__)
446+
#if !defined(__OpenBSD__) && !TARGET_OS_WASI
441447
if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK || dp->d_type == DT_WHT) {
442448
#else
443449
if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK) {
@@ -454,13 +460,13 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
454460
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
455461
}
456462
}
457-
#if TARGET_OS_LINUX
463+
#if TARGET_OS_LINUX || TARGET_OS_WASI
458464
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, namelen, isDir, dirURL);
459465
#else
460466
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, dp->d_namlen, isDir, dirURL);
461467
#endif
462468
} else {
463-
#if TARGET_OS_LINUX
469+
#if TARGET_OS_LINUX || TARGET_OS_WASI
464470
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc, (uint8_t *)dp->d_name, namelen, false, dirURL);
465471
#else
466472
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc, (uint8_t *)dp->d_name, dp->d_namlen, false, dirURL);
@@ -547,7 +553,7 @@ CF_PRIVATE SInt32 _CFGetPathProperties(CFAllocatorRef alloc, char *path, Boolean
547553

548554
if (modTime != NULL) {
549555
if (fileExists) {
550-
#if TARGET_OS_WIN32 || TARGET_OS_LINUX
556+
#if TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
551557
struct timespec ts = {statBuf.st_mtime, 0};
552558
#else
553559
struct timespec ts = statBuf.st_mtimespec;
@@ -1092,12 +1098,16 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
10921098
struct dirent *dent;
10931099
if ((dirp = opendir(directoryPathBuf))) {
10941100
while ((dent = readdir(dirp))) {
1095-
#if TARGET_OS_LINUX
1101+
#if TARGET_OS_LINUX || TARGET_OS_WASI
10961102
CFIndex nameLen = strlen(dent->d_name);
10971103
if (dent->d_type == DT_UNKNOWN) {
10981104
// on some old file systems readdir may always fill d_type as DT_UNKNOWN (0), double check with stat
10991105
struct stat statBuf;
1106+
#if TARGET_OS_WASI
1107+
char pathToStat[WASI_D_NAME_SIZE];
1108+
#else
11001109
char pathToStat[sizeof(dent->d_name)];
1110+
#endif
11011111
strncpy(pathToStat, directoryPathBuf, sizeof(pathToStat));
11021112
strlcat(pathToStat, "/", sizeof(pathToStat));
11031113
strlcat(pathToStat, dent->d_name, sizeof(pathToStat));
@@ -1123,7 +1133,12 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
11231133

11241134
// This buffer has to be 1 bigger than the size of the one in the dirent so we can hold the extra '/' if it's required
11251135
// Be sure to initialize the first character to null, so that strlcat below works correctly
1126-
char fullPathToFile[sizeof(dent->d_name) + 1];
1136+
#if TARGET_OS_WASI
1137+
size_t d_name_size = WASI_D_NAME_SIZE;
1138+
#else
1139+
size_t d_name_size = sizeof(dent->d_name);
1140+
#endif
1141+
char fullPathToFile[d_name_size + 1];
11271142
fullPathToFile[0] = 0;
11281143
CFIndex startAt = 0;
11291144

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ CF_EXTERN_C_BEGIN
102102
#include <CoreFoundation/CFRuntime.h>
103103
#include <limits.h>
104104
#include <stdatomic.h>
105+
106+
#if __BLOCKS__
105107
#include <Block.h>
108+
#endif
106109

107110
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
108111

@@ -317,7 +320,7 @@ static inline void __CFRuntimeSetValue(CFTypeRef cf, uint8_t n1, uint8_t n2, uin
317320
__CFInfoType info = atomic_load(&(((CFRuntimeBase *)cf)->_cfinfoa));
318321
__CFInfoType newInfo;
319322
__CFInfoType mask = __CFInfoMask(n1, n2);
320-
323+
321324
#if !TARGET_OS_WASI
322325
do {
323326
#endif
@@ -677,7 +680,7 @@ static int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
677680
#error "do not know how to define mutex and recursive mutex for this OS"
678681
#endif
679682

680-
#if !__HAS_DISPATCH__
683+
#if !__HAS_DISPATCH__ && __BLOCKS__
681684

682685
typedef volatile long dispatch_once_t;
683686
CF_PRIVATE void _CF_dispatch_once(dispatch_once_t *, void (^)(void));

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ const char *_CFProcessPath(void) {
247247
}
248248
#endif // TARGET_OS_WASI
249249

250+
#endif // TARGET_OS_WASI
251+
250252
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
251253
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
252254
return pthread_main_np() == 1;
@@ -583,6 +585,7 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
583585
#error Dont know how to compute users home directories on this platform
584586
#endif
585587
}
588+
#endif
586589

587590

588591
#undef CFMaxHostNameLength
@@ -1389,7 +1392,7 @@ void OSMemoryBarrier() {
13891392
#pragma mark -
13901393
#pragma mark Dispatch Replacements
13911394

1392-
#if !__HAS_DISPATCH__
1395+
#if !__HAS_DISPATCH__ && __BLOCKS__
13931396

13941397
#include <semaphore.h>
13951398

@@ -1539,7 +1542,8 @@ CF_PRIVATE int asprintf(char **ret, const char *format, ...) {
15391542
#if DEPLOYMENT_RUNTIME_SWIFT
15401543
#include <fcntl.h>
15411544

1542-
extern void swift_retain(void *);
1545+
CF_CC_swift
1546+
extern void *swift_retain(void *);
15431547
extern void swift_release(void *);
15441548

15451549
#if TARGET_OS_WIN32
@@ -1723,6 +1727,9 @@ CF_EXPORT char **_CFEnviron(void) {
17231727
return *_NSGetEnviron();
17241728
#elif TARGET_OS_WIN32
17251729
return _environ;
1730+
#elif TARGET_OS_WASI
1731+
extern char **environ;
1732+
return environ;
17261733
#else
17271734
#if TARGET_OS_BSD || TARGET_OS_WASI
17281735
extern char **environ;

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ __kCFRetainEvent = 28,
5151
__kCFReleaseEvent = 29
5252
};
5353

54-
#if TARGET_OS_WIN32 || TARGET_OS_LINUX
54+
#if TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
5555
#include <malloc.h>
5656
#elif TARGET_OS_BSD
5757
#include <stdlib.h> // malloc()
@@ -224,18 +224,18 @@ CFRuntimeClass const * __CFRuntimeClassTable[__CFRuntimeClassTableSize * 2] __at
224224
[_kCFRuntimeIDCFXMLNode] = &__CFXMLNodeClass,
225225
#endif // TARGET_OS_OSX
226226

227+
#if !TARGET_OS_WASI
227228
[_kCFRuntimeIDCFBundle] = &__CFBundleClass,
228229
[_kCFRuntimeIDCFPFactory] = &__CFPFactoryClass,
229230
[_kCFRuntimeIDCFPlugInInstance] = &__CFPlugInInstanceClass,
230-
231231
[_kCFRuntimeIDCFPreferencesDomain] = &__CFPreferencesDomainClass,
232+
#endif
232233

233234
#if TARGET_OS_MAC
234235
[_kCFRuntimeIDCFMachPort] = &__CFMachPortClass,
235236
#endif
236237

237-
238-
238+
#if !TARGET_OS_WASI
239239
[_kCFRuntimeIDCFRunLoopMode] = &__CFRunLoopModeClass,
240240
[_kCFRuntimeIDCFRunLoop] = &__CFRunLoopClass,
241241
[_kCFRuntimeIDCFRunLoopSource] = &__CFRunLoopSourceClass,
@@ -244,6 +244,8 @@ CFRuntimeClass const * __CFRuntimeClassTable[__CFRuntimeClassTableSize * 2] __at
244244
[_kCFRuntimeIDCFSocket] = &__CFSocketClass,
245245
[_kCFRuntimeIDCFReadStream] = &__CFReadStreamClass,
246246
[_kCFRuntimeIDCFWriteStream] = &__CFWriteStreamClass,
247+
#endif
248+
247249
[_kCFRuntimeIDCFAttributedString] = &__CFAttributedStringClass,
248250
[_kCFRuntimeIDCFRunArray] = &__CFRunArrayClass,
249251
[_kCFRuntimeIDCFCharacterSet] = &__CFCharacterSetClass,
@@ -1117,7 +1119,7 @@ _CFThreadRef _CF_pthread_main_thread_np(void) {
11171119

11181120

11191121

1120-
#if TARGET_OS_LINUX || TARGET_OS_BSD
1122+
#if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
11211123
static void __CFInitialize(void) __attribute__ ((constructor));
11221124
static
11231125
#endif
@@ -1254,7 +1256,9 @@ void __CFInitialize(void) {
12541256
#endif
12551257
}
12561258

1259+
#if !TARGET_OS_WASI
12571260
_CFProcessPath(); // cache this early
1261+
#endif
12581262

12591263
__CFOAInitialize();
12601264

@@ -1358,7 +1362,8 @@ int DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID pReserved ) {
13581362
#endif
13591363

13601364
#if DEPLOYMENT_RUNTIME_SWIFT
1361-
extern void swift_retain(void *);
1365+
CF_CC_swift
1366+
extern void *swift_retain(void *);
13621367
#endif
13631368

13641369
// For "tryR==true", a return of NULL means "failed".
@@ -1725,6 +1730,18 @@ struct _NSCFXMLBridgeUntyped __NSCFXMLBridgeUntyped = {
17251730
&kCFErrorLocalizedDescriptionKey,
17261731
};
17271732

1733+
// This function is also provided in libdispatch.
1734+
#if !__HAS_DISPATCH__
1735+
// For CF functions with 'Get' semantics, the compiler currently assumes that the result is autoreleased and must be retained. It does so on all platforms by emitting a call to objc_retainAutoreleasedReturnValue. On Darwin, this is implemented by the ObjC runtime. On Linux, there is no runtime, and therefore we have to stub it out here ourselves. The compiler will eventually call swift_release to balance the retain below. This is a workaround until the compiler no longer emits this callout on Linux.
1736+
void * objc_retainAutoreleasedReturnValue(void *obj) {
1737+
if (obj) {
1738+
swift_retain(obj);
1739+
return obj;
1740+
}
1741+
else return NULL;
1742+
}
1743+
#endif
1744+
17281745
// Call out to the CF-level finalizer, because the object is going to go away.
17291746
CF_CROSS_PLATFORM_EXPORT void _CFDeinit(CFTypeRef cf) {
17301747
__CFInfoType info = atomic_load(&(((CFRuntimeBase *)cf)->_cfinfoa));

CoreFoundation/Base.subproj/CFSortFunctions.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ enum {
6666
typedef CFIndex VALUE_TYPE;
6767
typedef CFIndex INDEX_TYPE;
6868
typedef CFComparisonResult CMP_RESULT_TYPE;
69+
70+
#if __BLOCKS__
6971
typedef CMP_RESULT_TYPE (^COMPARATOR_BLOCK)(VALUE_TYPE, VALUE_TYPE);
7072

7173
/*
@@ -420,5 +422,4 @@ void CFMergeSortArray(void *list, CFIndex count, CFIndex elementSize, CFComparat
420422
if (locals != store) free(store);
421423
if (locali != indexes) free(indexes);
422424
}
423-
424-
425+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// workaround: wasm32-unknown-wasi needs to link icui18n because it's not linked by default
2+
// on the platform.
3+
module CoreFoundation [extern_c] [system] {
4+
umbrella header "CoreFoundation.h"
5+
explicit module CFPlugInCOM { header "CFPlugInCOM.h" }
6+
7+
link "icui18n"
8+
}

0 commit comments

Comments
 (0)