diff --git a/.github/scripts/build-matrix.rb b/.github/scripts/build-matrix.rb index 63c0bdb5..7f2aa2e0 100644 --- a/.github/scripts/build-matrix.rb +++ b/.github/scripts/build-matrix.rb @@ -6,7 +6,10 @@ "build_os": "ubuntu-18.04", "agent_query": "ubuntu-20.04", "target": "ubuntu18.04_x86_64", - "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-18.04", + "containers": { + "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-18.04", + "release/5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-18.04@sha256:c996b1fe2babba4e468a6422d9fbe1a9e080e4745c5cb933e4eeaed0a5a95840", + }, "run_stdlib_test": true, "run_full_test": false, "run_e2e_test": true, @@ -18,7 +21,10 @@ "build_os": "ubuntu-20.04", "agent_query": "ubuntu-20.04", "target": "ubuntu20.04_x86_64", - "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", + "containers": { + "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", + "release/5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:61d40c32e63abbebb35fa682d78991e0cb9b186f280ba42adec89443905af2f4", + }, "run_stdlib_test": true, "run_full_test": false, "run_e2e_test": true, @@ -30,7 +36,10 @@ "build_os": "ubuntu-22.04", "agent_query": "ubuntu-22.04", "target": "ubuntu22.04_x86_64", - "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-22.04", + "containers": { + "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-22.04", + "release/5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-22.04@sha256:a55c1ab110d3ef8b1605234245803b5917484990d0b3a00077f9957ae386da97", + }, "run_stdlib_test": true, "run_full_test": false, "run_e2e_test": true, @@ -42,7 +51,10 @@ "build_os": "amazon-linux-2", "agent_query": "ubuntu-22.04", "target": "amazonlinux2_x86_64", - "container": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2", + "containers": { + "main": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2", + "release/5.9": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2@sha256:5aa7643df1a0d2ea43d86e1dfa603848760deb6efba50349bd00fee9395d0ded", + }, "run_stdlib_test": false, "run_full_test": false, "run_e2e_test": false, @@ -105,7 +117,10 @@ def main "build_os": "ubuntu-20.04", "agent_query": label, "target": "ubuntu20.04_aarch64", - "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", + "containers": { + "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", + "release/5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:61d40c32e63abbebb35fa682d78991e0cb9b186f280ba42adec89443905af2f4", + }, "run_stdlib_test": false, "run_full_test": false, "run_e2e_test": false, @@ -133,7 +148,10 @@ def main raise "Unknown scheme: #{scheme}" end matrix_entries.map do |entry| - entry.merge("scheme": scheme, toolchain_channel: toolchain_channel) + container = if containers = entry[:containers] + containers[scheme.to_sym] || containers[:main] + end + entry.merge("scheme": scheme, toolchain_channel: toolchain_channel, container: container) end end diff --git a/.github/workflows/build-toolchain.yml b/.github/workflows/build-toolchain.yml index 485aa96b..91c014ef 100644 --- a/.github/workflows/build-toolchain.yml +++ b/.github/workflows/build-toolchain.yml @@ -65,7 +65,7 @@ jobs: name: Build matrix needs: [start-runner] runs-on: ubuntu-latest - if: ${{ always() }} + if: ${{ !cancelled() }} outputs: entries: ${{ steps.generate.outputs.entries }} steps: @@ -99,7 +99,7 @@ jobs: # https://github.com/actions/cache/blob/main/tips-and-workarounds.md#cache-segment-restore-timeout SEGMENT_DOWNLOAD_TIMEOUT_MINS: 30 needs: [build-matrix] - if: ${{ always() }} + if: ${{ !cancelled() }} strategy: fail-fast: false matrix: @@ -113,8 +113,6 @@ jobs: if: ${{ matrix.free_disk_space }} run: | df -h - sudo swapoff -a - sudo rm -f /swapfile sudo rm -rf /opt/hostedtoolcache sudo rm -rf /usr/share/dotnet sudo apt clean @@ -188,6 +186,7 @@ jobs: --cap-add=SYS_PTRACE \ --security-opt seccomp=unconfined \ -dit \ + -e CI=$CI \ -w /home/build-user/ \ -v ${{ github.workspace }}:/source \ -v oss-swift-package:/home/build-user \ @@ -196,7 +195,7 @@ jobs: docker cp swiftwasm-ci-buildbot:/home/build-user/swift-wasm-${{ matrix.toolchain_channel }}-SNAPSHOT-${{ matrix.target }}.tar.gz . - name: Extract build-cache from Docker container - if: ${{ matrix.container != null && always() }} + if: ${{ matrix.container != null && !cancelled() }} run: | rm -rf ${{ github.workspace }}/build-cache docker cp swiftwasm-ci-buildbot:/home/build-user/build-cache ${{ github.workspace }}/build-cache @@ -209,7 +208,7 @@ jobs: run: ./swiftwasm-build/tools/build/ci.sh ${{ matrix.scheme }} - uses: actions/cache/save@v3 - if: ${{ always() }} + if: ${{ !cancelled() }} with: path: build-cache key: ${{ steps.cache_key.outputs.SCCACHE_KEY }} @@ -223,7 +222,8 @@ jobs: - name: Run stdlib tests for wasi-wasm32 if: ${{ matrix.run_stdlib_test && matrix.container != null }} run: | - docker exec swiftwasm-ci-buildbot /bin/bash -lc "cd build/WebAssembly/swift-stdlib-wasi-wasm32 && ninja check-swift-wasi-wasm32-custom" + docker exec swiftwasm-ci-buildbot /bin/bash -lc \ + "ninja -C build/WebAssembly/swift-stdlib-wasi-wasm32 check-swift-wasi-wasm32-custom && rm -rf build" - name: Run stdlib tests for wasi-wasm32 if: ${{ matrix.run_stdlib_test && matrix.container == null }} run: | @@ -248,6 +248,13 @@ jobs: name: ${{ matrix.target }}-test-results path: ./swift-test-results.tar.gz + - name: Cleanup docker volume + if: ${{ matrix.container != null }} + run: | + docker stop swiftwasm-ci-buildbot + docker rm swiftwasm-ci-buildbot + docker volume rm --force oss-swift-package + # Run e2e test - name: Prepare E2E test run: | @@ -262,6 +269,7 @@ jobs: $TOOLCHAIN/usr/bin/swiftc \ -target wasm32-unknown-wasi \ -sdk $TOOLCHAIN/usr/share/wasi-sysroot \ + -resource-dir $TOOLCHAIN/usr/lib/swift_static \ hello.swift -o hello.wasm && \ echo "Successfully linked hello.wasm" - name: Upload hello.wasm diff --git a/schemes/main/manifest.json b/schemes/main/manifest.json index dbf01109..d5cb1624 100644 --- a/schemes/main/manifest.json +++ b/schemes/main/manifest.json @@ -1,8 +1,11 @@ { - "base-tag": "swift-DEVELOPMENT-SNAPSHOT-2023-08-03-a", + "base-tag": "swift-DEVELOPMENT-SNAPSHOT-2023-10-27-a", "repos": { "swift-corelibs-foundation": "660624d0220c0efbef054f4919f9f01fd3866be1", "swift-corelibs-xctest": "77bc9f5386ee8a2a4e8da5ac30e846b451d101b6" }, + "build-compiler": false, + "icu4c": "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.7.0/icu4c-wasi.tar.xz", + "wasi-sysroot": "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-19/wasi-sysroot-19.0.tar.gz", "swift-org-download-channel": "development" } diff --git a/schemes/main/swift-corelibs-foundation/0001-wasm-import-emulated-wasi-libc-module-for-mman-apis.patch b/schemes/main/swift-corelibs-foundation/0001-wasm-import-emulated-wasi-libc-module-for-mman-apis.patch new file mode 100644 index 00000000..d74de1ac --- /dev/null +++ b/schemes/main/swift-corelibs-foundation/0001-wasm-import-emulated-wasi-libc-module-for-mman-apis.patch @@ -0,0 +1,37 @@ +From 2be8e97d311ee0a7530303d62bcd51e7cba6df59 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sun, 29 Oct 2023 00:39:00 +0000 +Subject: [PATCH 1/2] [wasm] import emulated wasi-libc module for mman apis + +--- + Sources/Foundation/Data.swift | 1 + + Sources/Foundation/FileHandle.swift | 1 + + 2 files changed, 2 insertions(+) + +diff --git a/Sources/Foundation/Data.swift b/Sources/Foundation/Data.swift +index f6b9a06b..f396129a 100644 +--- a/Sources/Foundation/Data.swift ++++ b/Sources/Foundation/Data.swift +@@ -29,6 +29,7 @@ + @usableFromInline let memcpy = Glibc.memcpy + @usableFromInline let memcmp = Glibc.memcmp + #elseif canImport(WASILibc) ++@_implementationOnly import wasi_emulated_mman + @usableFromInline let calloc = WASILibc.calloc + @usableFromInline let malloc = WASILibc.malloc + @usableFromInline let free = WASILibc.free +diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift +index 5e28678c..670d684f 100644 +--- a/Sources/Foundation/FileHandle.swift ++++ b/Sources/Foundation/FileHandle.swift +@@ -26,6 +26,7 @@ fileprivate let _write = Glibc.write(_:_:_:) + fileprivate let _close = Glibc.close(_:) + #elseif canImport(WASILibc) + import WASILibc ++@_implementationOnly import wasi_emulated_mman + fileprivate let _read = WASILibc.read(_:_:_:) + fileprivate let _write = WASILibc.write(_:_:_:) + fileprivate let _close = WASILibc.close(_:) +-- +2.40.0 + diff --git a/schemes/main/swift-corelibs-foundation/0002-wasm-Get-rid-of-pthread-stub-hack.patch b/schemes/main/swift-corelibs-foundation/0002-wasm-Get-rid-of-pthread-stub-hack.patch new file mode 100644 index 00000000..4d65eaec --- /dev/null +++ b/schemes/main/swift-corelibs-foundation/0002-wasm-Get-rid-of-pthread-stub-hack.patch @@ -0,0 +1,384 @@ +From ed38c661b8892ac83e40c799f0ea873731275086 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sun, 29 Oct 2023 00:39:31 +0000 +Subject: [PATCH 2/2] [wasm] Get rid of pthread stub hack + +--- + CoreFoundation/Base.subproj/CFInternal.h | 31 ++++++++++++- + CoreFoundation/Base.subproj/CFPlatform.c | 3 ++ + CoreFoundation/Base.subproj/CFPriv.h | 2 +- + CoreFoundation/Base.subproj/CFRuntime.c | 4 +- + CoreFoundation/Base.subproj/CFUtilities.c | 8 +++- + .../Base.subproj/CoreFoundation_Prefix.h | 6 +++ + .../Base.subproj/ForSwiftFoundationOnly.h | 8 +++- + Sources/Foundation/NSLock.swift | 45 +++++++++++++++++++ + 8 files changed, 101 insertions(+), 6 deletions(-) + +diff --git a/CoreFoundation/Base.subproj/CFInternal.h b/CoreFoundation/Base.subproj/CFInternal.h +index 21f9efec..d6763e6c 100644 +--- a/CoreFoundation/Base.subproj/CFInternal.h ++++ b/CoreFoundation/Base.subproj/CFInternal.h +@@ -657,6 +657,35 @@ CF_INLINE int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) { + LeaveCriticalSection(mutex); + return 0; + } ++#elif TARGET_OS_WASI ++typedef struct {} _CFMutex; ++#define _CF_MUTEX_STATIC_INITIALIZER {} ++CF_INLINE int _CFMutexCreate(_CFMutex *lock) { ++ return 0; ++} ++CF_INLINE int _CFMutexDestroy(_CFMutex *lock) { ++ return 0; ++} ++CF_INLINE int _CFMutexLock(_CFMutex *lock) { ++ return 0; ++} ++CF_INLINE int _CFMutexUnlock(_CFMutex *lock) { ++ return 0; ++} ++ ++typedef struct {} _CFRecursiveMutex; ++CF_INLINE int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) { ++ return 0; ++} ++CF_INLINE int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) { ++ return 0; ++} ++CF_INLINE int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) { ++ return 0; ++} ++CF_INLINE int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) { ++ return 0; ++} + #else + #error "do not know how to define mutex and recursive mutex for this OS" + #endif +@@ -680,7 +709,7 @@ typedef uint32_t os_unfair_lock_options_t; + static void os_unfair_lock_lock(os_unfair_lock_t lock) { pthread_mutex_lock(lock); } + static void os_unfair_lock_lock_with_options(os_unfair_lock_t lock, os_unfair_lock_options_t options) { pthread_mutex_lock(lock); } + static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(lock); } +-#elif defined(_WIN32) ++#elif defined(_WIN32) || TARGET_OS_WASI + #define OS_UNFAIR_LOCK_INIT CFLockInit + #define os_unfair_lock CFLock_t + #define os_unfair_lock_lock __CFLock +diff --git a/CoreFoundation/Base.subproj/CFPlatform.c b/CoreFoundation/Base.subproj/CFPlatform.c +index 99a05609..6bf8b5a3 100644 +--- a/CoreFoundation/Base.subproj/CFPlatform.c ++++ b/CoreFoundation/Base.subproj/CFPlatform.c +@@ -1626,6 +1626,8 @@ CF_PRIVATE int asprintf(char **ret, const char *format, ...) { + extern void *swift_retain(void *); + extern void swift_release(void *); + ++#if !TARGET_OS_WASI ++ + #if TARGET_OS_WIN32 + typedef struct _CFThreadSpecificData { + CFTypeRef value; +@@ -1804,6 +1806,7 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) { + #endif + return -1; + } ++#endif // !TARGET_OS_WASI + + CF_EXPORT char **_CFEnviron(void) { + #if TARGET_OS_MAC +diff --git a/CoreFoundation/Base.subproj/CFPriv.h b/CoreFoundation/Base.subproj/CFPriv.h +index 692a106a..639de79c 100644 +--- a/CoreFoundation/Base.subproj/CFPriv.h ++++ b/CoreFoundation/Base.subproj/CFPriv.h +@@ -598,7 +598,7 @@ CF_EXPORT CFMessagePortRef _CFMessagePortCreateLocalEx(CFAllocatorRef allocator, + #if __has_include() + #include + #endif +-#if _POSIX_THREADS ++#if _POSIX_THREADS && __has_include() + #include + #endif + #include +diff --git a/CoreFoundation/Base.subproj/CFRuntime.c b/CoreFoundation/Base.subproj/CFRuntime.c +index a2ae585b..ab602608 100644 +--- a/CoreFoundation/Base.subproj/CFRuntime.c ++++ b/CoreFoundation/Base.subproj/CFRuntime.c +@@ -1198,9 +1198,11 @@ void __CFInitialize(void) { + DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), + GetCurrentProcess(), &_CFMainPThread, 0, FALSE, + DUPLICATE_SAME_ACCESS); +-#else ++#elif _POSIX_THREADS + // move this next line up into the #if above after Foundation gets off this symbol. Also: Stop using _CFMainPThread + _CFMainPThread = pthread_self(); ++#elif TARGET_OS_WASI ++ _CFMainPThread = NULL; + #endif + + #if TARGET_OS_WIN32 +diff --git a/CoreFoundation/Base.subproj/CFUtilities.c b/CoreFoundation/Base.subproj/CFUtilities.c +index 7c3fc9f8..fc07b8a7 100644 +--- a/CoreFoundation/Base.subproj/CFUtilities.c ++++ b/CoreFoundation/Base.subproj/CFUtilities.c +@@ -927,8 +927,12 @@ static void _populateBanner(char **banner, char **time, char **thread, int *bann + bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%lx] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), GetCurrentThreadId()); + asprintf(thread, "%lx", GetCurrentThreadId()); + #elif TARGET_OS_WASI +- bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)pthread_self()); +- asprintf(thread, "%lx", pthread_self()); ++ _CFThreadRef tid = 0; ++# if _POSIX_THREADS ++ tid = pthread_self(); ++# endif ++ bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)tid); ++ asprintf(thread, "%lx", tid); + #else + 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()); + asprintf(thread, "%lx", pthread_self()); +diff --git a/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h b/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h +index bf6f203b..2d0d4655 100644 +--- a/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h ++++ b/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h +@@ -138,6 +138,12 @@ typedef char * Class; + + #endif + ++#if __has_include() ++# include ++# if TARGET_OS_WASI && _POSIX_THREADS && !__has_include() ++# undef _POSIX_THREADS ++# endif ++#endif + + /* This macro creates some helper functions which are useful in dealing with libdispatch: + * __ PREFIX Queue -- manages and returns a singleton serial queue +diff --git a/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h b/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h +index af3fc843..04ea85b7 100644 +--- a/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h ++++ b/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h +@@ -42,7 +42,7 @@ + #if __has_include() + #include + #endif +-#if _POSIX_THREADS ++#if _POSIX_THREADS && __has_include() + #include + #endif + #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +@@ -414,6 +414,10 @@ typedef unsigned long _CFThreadSpecificKey; + typedef pthread_t _CFThreadRef; + typedef pthread_attr_t _CFThreadAttributes; + typedef pthread_key_t _CFThreadSpecificKey; ++#elif TARGET_OS_WASI ++typedef void *_CFThreadRef; ++typedef void *_CFThreadAttributes; ++typedef void *_CFThreadSpecificKey; + #endif + + CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void); +@@ -425,6 +429,7 @@ CF_EXPORT CFHashCode __CFHashDouble(double d); + CF_CROSS_PLATFORM_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex)); + #endif + ++#if !TARGET_OS_WASI + CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key); + CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value); + CF_EXPORT _CFThreadSpecificKey _CFThreadSpecificKeyCreate(void); +@@ -433,6 +438,7 @@ CF_EXPORT _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_N + + CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name); + CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length); ++#endif + + CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar); + CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet); +diff --git a/Sources/Foundation/NSLock.swift b/Sources/Foundation/NSLock.swift +index 59d334f1..4e8b6e25 100644 +--- a/Sources/Foundation/NSLock.swift ++++ b/Sources/Foundation/NSLock.swift +@@ -48,6 +48,8 @@ open class NSLock: NSObject, NSLocking { + InitializeSRWLock(mutex) + InitializeConditionVariable(timeoutCond) + InitializeSRWLock(timeoutMutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_init(mutex, nil) + #if os(macOS) || os(iOS) +@@ -60,6 +62,8 @@ open class NSLock: NSObject, NSLocking { + deinit { + #if os(Windows) + // SRWLocks do not need to be explicitly destroyed ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_destroy(mutex) + #endif +@@ -73,6 +77,8 @@ open class NSLock: NSObject, NSLocking { + open func lock() { + #if os(Windows) + AcquireSRWLockExclusive(mutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_lock(mutex) + #endif +@@ -84,6 +90,8 @@ open class NSLock: NSObject, NSLocking { + AcquireSRWLockExclusive(timeoutMutex) + WakeAllConditionVariable(timeoutCond) + ReleaseSRWLockExclusive(timeoutMutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_unlock(mutex) + #if os(macOS) || os(iOS) +@@ -98,6 +106,9 @@ open class NSLock: NSObject, NSLocking { + open func `try`() -> Bool { + #if os(Windows) + return TryAcquireSRWLockExclusive(mutex) != 0 ++#elseif os(WASI) ++ // noop on no thread platforms ++ return true + #else + return pthread_mutex_trylock(mutex) == 0 + #endif +@@ -108,6 +119,9 @@ open class NSLock: NSObject, NSLocking { + if TryAcquireSRWLockExclusive(mutex) != 0 { + return true + } ++#elseif os(WASI) ++ // noop on no thread platforms ++ return true + #else + if pthread_mutex_trylock(mutex) == 0 { + return true +@@ -247,6 +261,8 @@ open class NSRecursiveLock: NSObject, NSLocking { + InitializeCriticalSection(mutex) + InitializeConditionVariable(timeoutCond) + InitializeSRWLock(timeoutMutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + #if CYGWIN || os(OpenBSD) + var attrib : pthread_mutexattr_t? = nil +@@ -273,6 +289,8 @@ open class NSRecursiveLock: NSObject, NSLocking { + deinit { + #if os(Windows) + DeleteCriticalSection(mutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_destroy(mutex) + #endif +@@ -286,6 +304,8 @@ open class NSRecursiveLock: NSObject, NSLocking { + open func lock() { + #if os(Windows) + EnterCriticalSection(mutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_lock(mutex) + #endif +@@ -297,6 +317,8 @@ open class NSRecursiveLock: NSObject, NSLocking { + AcquireSRWLockExclusive(timeoutMutex) + WakeAllConditionVariable(timeoutCond) + ReleaseSRWLockExclusive(timeoutMutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_unlock(mutex) + #if os(macOS) || os(iOS) +@@ -311,6 +333,9 @@ open class NSRecursiveLock: NSObject, NSLocking { + open func `try`() -> Bool { + #if os(Windows) + return TryEnterCriticalSection(mutex) ++#elseif os(WASI) ++ // noop on no thread platforms ++ return true + #else + return pthread_mutex_trylock(mutex) == 0 + #endif +@@ -321,6 +346,9 @@ open class NSRecursiveLock: NSObject, NSLocking { + if TryEnterCriticalSection(mutex) { + return true + } ++#elseif os(WASI) ++ // noop on no thread platforms ++ return true + #else + if pthread_mutex_trylock(mutex) == 0 { + return true +@@ -352,6 +380,8 @@ open class NSCondition: NSObject, NSLocking { + #if os(Windows) + InitializeSRWLock(mutex) + InitializeConditionVariable(cond) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_init(mutex, nil) + pthread_cond_init(cond, nil) +@@ -361,6 +391,8 @@ open class NSCondition: NSObject, NSLocking { + deinit { + #if os(Windows) + // SRWLock do not need to be explicitly destroyed ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_destroy(mutex) + pthread_cond_destroy(cond) +@@ -374,6 +406,8 @@ open class NSCondition: NSObject, NSLocking { + open func lock() { + #if os(Windows) + AcquireSRWLockExclusive(mutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_lock(mutex) + #endif +@@ -382,6 +416,8 @@ open class NSCondition: NSObject, NSLocking { + open func unlock() { + #if os(Windows) + ReleaseSRWLockExclusive(mutex) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_mutex_unlock(mutex) + #endif +@@ -390,6 +426,8 @@ open class NSCondition: NSObject, NSLocking { + open func wait() { + #if os(Windows) + SleepConditionVariableSRW(cond, mutex, WinSDK.INFINITE, 0) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_cond_wait(cond, mutex) + #endif +@@ -398,6 +436,9 @@ open class NSCondition: NSObject, NSLocking { + open func wait(until limit: Date) -> Bool { + #if os(Windows) + return SleepConditionVariableSRW(cond, mutex, timeoutFrom(date: limit), 0) ++#elseif os(WASI) ++ // noop on no thread platforms ++ return true + #else + guard var timeout = timeSpecFrom(date: limit) else { + return false +@@ -409,6 +450,8 @@ open class NSCondition: NSObject, NSLocking { + open func signal() { + #if os(Windows) + WakeConditionVariable(cond) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_cond_signal(cond) + #endif +@@ -417,6 +460,8 @@ open class NSCondition: NSObject, NSLocking { + open func broadcast() { + #if os(Windows) + WakeAllConditionVariable(cond) ++#elseif os(WASI) ++ // noop on no thread platforms + #else + pthread_cond_broadcast(cond) + #endif +-- +2.40.0 + diff --git a/schemes/main/swift/0001-Fix-calling-convention-mismatch-for-debugger-utility.patch b/schemes/main/swift/0001-Fix-calling-convention-mismatch-for-debugger-utility.patch new file mode 100644 index 00000000..4670c818 --- /dev/null +++ b/schemes/main/swift/0001-Fix-calling-convention-mismatch-for-debugger-utility.patch @@ -0,0 +1,109 @@ +From 47a67685996d90bf0a296f1cae32db9b411991f1 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Tue, 24 Oct 2023 01:23:21 +0000 +Subject: [PATCH] Fix calling convention mismatch for debugger utility + functions + +This is the 3rd attempt to fix the mismatch, where the actual definition +(e.g. `swift_retainCount`) are defined with C calling-convention and the +callers wrongly expect Swift calling-convention. + +The 1st fix broke ABI compatibility by introducing new symbol references +from app-side without any availability checks. +The 2nd fix broke lldb's retain counting feature due to new x-ref to +Clang module in serialized function body by `@_alwaysEmitIntoClient`. + +This attemps to avoid introducing serialized x-ref to Clang module by +using new `@extern(c)` attribute. + +Co-authored-by: Karoy Lorentey +--- + stdlib/public/core/CMakeLists.txt | 1 + + stdlib/public/core/DebuggerSupport.swift | 40 ++++++++++++++++--- + .../stability-stdlib-abi-without-asserts.test | 9 +++++ + 3 files changed, 44 insertions(+), 6 deletions(-) + +diff --git a/stdlib/public/core/CMakeLists.txt b/stdlib/public/core/CMakeLists.txt +index 8bbfa84c21e..bff58501b00 100644 +--- a/stdlib/public/core/CMakeLists.txt ++++ b/stdlib/public/core/CMakeLists.txt +@@ -300,6 +300,7 @@ list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-enable-experimental-concis + + list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros") + list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "FreestandingMacros") ++list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Extern") + + set(swift_core_incorporate_object_libraries) + list(APPEND swift_core_incorporate_object_libraries swiftRuntime) +diff --git a/stdlib/public/core/DebuggerSupport.swift b/stdlib/public/core/DebuggerSupport.swift +index ef9eafeab81..9a0857ff45b 100644 +--- a/stdlib/public/core/DebuggerSupport.swift ++++ b/stdlib/public/core/DebuggerSupport.swift +@@ -268,10 +268,38 @@ public func _stringForPrintObject(_ value: Any) -> String { + + public func _debuggerTestingCheckExpect(_: String, _: String) { } + ++@_alwaysEmitIntoClient @_transparent ++internal func _withHeapObject( ++ of object: AnyObject, ++ _ body: (UnsafeMutableRawPointer) -> R ++) -> R { ++ defer { _fixLifetime(object) } ++ let unmanaged = Unmanaged.passUnretained(object) ++ return body(unmanaged.toOpaque()) ++} ++ ++@extern(c, "swift_retainCount") @usableFromInline ++internal func _swift_retainCount(_: UnsafeMutableRawPointer) -> Int ++@extern(c, "swift_unownedRetainCount") @usableFromInline ++internal func _swift_unownedRetainCount(_: UnsafeMutableRawPointer) -> Int ++@extern(c, "swift_weakRetainCount") @usableFromInline ++internal func _swift_weakRetainCount(_: UnsafeMutableRawPointer) -> Int ++ + // Utilities to get refcount(s) of class objects. +-@_silgen_name("swift_retainCount") +-public func _getRetainCount(_ Value: AnyObject) -> UInt +-@_silgen_name("swift_unownedRetainCount") +-public func _getUnownedRetainCount(_ Value: AnyObject) -> UInt +-@_silgen_name("swift_weakRetainCount") +-public func _getWeakRetainCount(_ Value: AnyObject) -> UInt ++@backDeployed(before: SwiftStdlib 9999) ++public func _getRetainCount(_ object: AnyObject) -> UInt { ++ let count = _withHeapObject(of: object) { _swift_retainCount($0) } ++ return UInt(bitPattern: count) ++} ++ ++@backDeployed(before: SwiftStdlib 9999) ++public func _getUnownedRetainCount(_ object: AnyObject) -> UInt { ++ let count = _withHeapObject(of: object) { _swift_unownedRetainCount($0) } ++ return UInt(bitPattern: count) ++} ++ ++@backDeployed(before: SwiftStdlib 9999) ++public func _getWeakRetainCount(_ object: AnyObject) -> UInt { ++ let count = _withHeapObject(of: object) { _swift_weakRetainCount($0) } ++ return UInt(bitPattern: count) ++} +diff --git a/test/api-digester/stability-stdlib-abi-without-asserts.test b/test/api-digester/stability-stdlib-abi-without-asserts.test +index 25c6ae709cb..c8dada1b02b 100644 +--- a/test/api-digester/stability-stdlib-abi-without-asserts.test ++++ b/test/api-digester/stability-stdlib-abi-without-asserts.test +@@ -51,6 +51,15 @@ Func _prespecialize() is a new API without @available attribute + Func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(_:_:_:_:_:_:) is a new API without @available attribute + Func _diagnoseUnavailableCodeReached() is a new API without @available attribute + ++// These functions are not actually added to the ABI, but they had been a part of ++// the ABI exposed by the runtime library, so this is not breakage. ++// They are now referenced by @extern(c) declarations in the standard library, but ++// api-digester cannot match them with the baseline symbols in the baseline runtime ++// library, which were not exposed by the baseline stdlib module. ++Func _swift_retainCount(_:) is a new API without @available attribute ++Func _swift_unownedRetainCount(_:) is a new API without @available attribute ++Func _swift_weakRetainCount(_:) is a new API without @available attribute ++ + Func Collection.removingSubranges(_:) has been removed + Func Collection.subranges(of:) has been removed + Func Collection.subranges(where:) has been removed +-- +2.40.0 + diff --git a/schemes/main/swift/0001-wasm-build-Add-WASILibc-module-and-its-build-support.patch b/schemes/main/swift/0001-wasm-build-Add-WASILibc-module-and-its-build-support.patch deleted file mode 100644 index 5bca9bda..00000000 --- a/schemes/main/swift/0001-wasm-build-Add-WASILibc-module-and-its-build-support.patch +++ /dev/null @@ -1,438 +0,0 @@ -From 9b77b175da79caf00b80dc15310f8bcd46a485c1 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Wed, 7 Jun 2023 17:58:49 +0000 -Subject: [PATCH] [wasm][build] Add WASILibc module and its build support - -[wasi-libc](https://github.com/WebAssembly/wasi-libc) is a libc -implementation widely used in WASI toolchains. This patch adds a new -Swift and C module for it to import and use it in Swift code. This -module is intentionally separate from the existing `Glibc` module -because wasi-libc is actually based on musl libc. ---- - stdlib/public/Platform/CMakeLists.txt | 51 ++++-- - stdlib/public/Platform/SwiftWASILibc.h.gyb | 126 ++++++++++++++ - stdlib/public/Platform/WASILibc.swift.gyb | 161 ++++++++++++++++++ - .../public/Platform/wasi-libc.modulemap.gyb | 18 ++ - 4 files changed, 343 insertions(+), 13 deletions(-) - create mode 100644 stdlib/public/Platform/SwiftWASILibc.h.gyb - create mode 100644 stdlib/public/Platform/WASILibc.swift.gyb - create mode 100644 stdlib/public/Platform/wasi-libc.modulemap.gyb - -diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt -index 18be6412cf3..e0ee5283fc5 100644 ---- a/stdlib/public/Platform/CMakeLists.txt -+++ b/stdlib/public/Platform/CMakeLists.txt -@@ -114,6 +114,23 @@ add_swift_target_library(swiftMusl ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OV - TARGET_SDKS MUSL - INSTALL_IN_COMPONENT sdk-overlay) - -+add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY -+ ${swift_platform_sources} -+ POSIXError.swift -+ -+ GYB_SOURCES -+ ${swift_platform_gyb_sources} -+ WASILibc.swift.gyb -+ -+ SWIFT_COMPILE_FLAGS -+ ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} -+ ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} -+ ${swift_platform_compile_flags} -+ LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" -+ TARGET_SDKS WASI -+ INSTALL_IN_COMPONENT sdk-overlay -+ DEPENDS glibc_modulemap) -+ - add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY - ucrt.swift - ${swift_platform_sources} -@@ -133,23 +150,31 @@ add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE - - set(glibc_modulemap_target_list) - foreach(sdk ${SWIFT_SDKS}) -- if(NOT "${sdk}" STREQUAL "LINUX" AND -- NOT "${sdk}" STREQUAL "FREEBSD" AND -- NOT "${sdk}" STREQUAL "OPENBSD" AND -- NOT "${sdk}" STREQUAL "ANDROID" AND -- NOT "${sdk}" STREQUAL "CYGWIN" AND -- NOT "${sdk}" STREQUAL "HAIKU") -+ if("${sdk}" STREQUAL "LINUX" OR -+ "${sdk}" STREQUAL "FREEBSD" OR -+ "${sdk}" STREQUAL "OPENBSD" OR -+ "${sdk}" STREQUAL "ANDROID" OR -+ "${sdk}" STREQUAL "CYGWIN" OR -+ "${sdk}" STREQUAL "HAIKU") -+ set(glibc_modulemap_source "glibc.modulemap.gyb") -+ set(glibc_header_source "SwiftGlibc.h.gyb") -+ elseif("${sdk}" STREQUAL "WASI") -+ set(glibc_modulemap_source "wasi-libc.modulemap.gyb") -+ set(glibc_header_source "SwiftWASILibc.h.gyb") -+ else() - continue() - endif() - -+ string(REGEX REPLACE "\\.gyb$" "" glibc_modulemap_outname "${glibc_modulemap_source}") -+ string(REGEX REPLACE "\\.gyb$" "" glibc_header_outname "${glibc_header_source}") -+ - foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) - set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}") - set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}") - set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}") - -- set(glibc_modulemap_source "glibc.modulemap.gyb") -- set(glibc_modulemap_out "${module_dir}/glibc.modulemap") -- set(glibc_modulemap_out_static "${module_dir_static}/glibc.modulemap") -+ set(glibc_modulemap_out "${module_dir}/${glibc_modulemap_outname}") -+ set(glibc_modulemap_out_static "${module_dir_static}/${glibc_modulemap_outname}") - - # Configure the module map based on the target. Each platform needs to - # reference different headers, based on what's available in their glibc. -@@ -163,10 +188,10 @@ foreach(sdk ${SWIFT_SDKS}) - - list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target}) - -- set(glibc_header_out "${module_dir}/SwiftGlibc.h") -- set(glibc_header_out_static "${module_dir_static}/SwiftGlibc.h") -+ set(glibc_header_out "${module_dir}/${glibc_header_outname}") -+ set(glibc_header_out_static "${module_dir_static}/${glibc_header_outname}") - handle_gyb_source_single(glibc_header_target -- SOURCE "SwiftGlibc.h.gyb" -+ SOURCE "${glibc_header_source}" - OUTPUT "${glibc_header_out}" - FLAGS "-DCMAKE_SDK=${sdk}") - list(APPEND glibc_modulemap_target_list ${glibc_header_target}) -@@ -197,7 +222,7 @@ foreach(sdk ${SWIFT_SDKS}) - # prefix. This is the one we'll install instead. - if(NOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${arch}_PATH}" STREQUAL "/" AND - NOT (sdk STREQUAL "ANDROID" AND NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")) -- set(glibc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/glibc.modulemap") -+ set(glibc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/${glibc_modulemap_outname}") - - handle_gyb_source_single(glibc_modulemap_native_target - SOURCE "${glibc_modulemap_source}" -diff --git a/stdlib/public/Platform/SwiftWASILibc.h.gyb b/stdlib/public/Platform/SwiftWASILibc.h.gyb -new file mode 100644 -index 00000000000..c051627f2b7 ---- /dev/null -+++ b/stdlib/public/Platform/SwiftWASILibc.h.gyb -@@ -0,0 +1,126 @@ -+%{ -+headers = [ -+ 'stdc-predef.h', -+ 'features.h', -+ -+ # C standard library -+ 'complex.h', -+ 'ctype.h', -+ 'errno.h', -+ 'fenv.h', -+ 'float.h', -+ 'inttypes.h', -+ 'iso646.h', -+ 'libutil.h', -+ 'limits.h', -+ 'locale.h', -+ 'math.h', -+ 'pty.h', -+ # setjmp/longjmp is not available -+ # 'setjmp.h', -+ 'signal.h', -+ 'stdarg.h', -+ 'stdbool.h', -+ 'stddef.h', -+ 'stdint.h', -+ 'stdio.h', -+ 'stdlib.h', -+ 'string.h', -+ 'tgmath.h', -+ 'time.h', -+ 'util.h', -+ 'utmp.h', -+ -+ # POSIX -+ 'aio.h', -+ 'arpa/inet.h', -+ 'bsd/ifaddrs.h', -+ 'bsd/pty.h', -+ 'cpio.h', -+ 'dirent.h', -+ 'dlfcn.h', -+ 'fcntl.h', -+ 'fmtmsg.h', -+ 'fnmatch.h', -+ 'ftw.h', -+ 'glob.h', -+ 'grp.h', -+ 'iconv.h', -+ 'ifaddrs.h', -+ 'langinfo.h', -+ 'libgen.h', -+ 'link.h', -+ 'monetary.h', -+ 'net/if.h', -+ 'netdb.h', -+ 'netinet/in.h', -+ 'netinet/tcp.h', -+ 'nl_types.h', -+ 'poll.h', -+ 'pthread.h', -+ 'pwd.h', -+ 'regex.h', -+ 'sched.h', -+ 'search.h', -+ 'semaphore.h', -+ 'spawn.h', -+ 'strings.h', -+ 'sys/event.h', -+ 'sys/file.h', -+ 'sys/inotify.h', -+ 'sys/ioctl.h', -+ 'sys/ipc.h', -+ 'sys/mman.h', -+ 'sys/mount.h', -+ 'sys/msg.h', -+ 'sys/resource.h', -+ 'sys/select.h', -+ 'sys/sem.h', -+ 'sys/sendfile.h', -+ 'sys/shm.h', -+ 'sys/socket.h', -+ 'sys/stat.h', -+ 'sys/statvfs.h', -+ 'sys/time.h', -+ 'sys/times.h', -+ 'sys/types.h', -+ 'sys/uio.h', -+ 'sys/un.h', -+ 'sys/user.h', -+ 'sys/utsname.h', -+ 'sys/wait.h', -+ 'sysexits.h', -+ 'syslog.h', -+ 'tar.h', -+ 'termios.h', -+ 'ulimit.h', -+ 'unistd.h', -+ 'utime.h', -+ 'utmpx.h', -+ 'wait.h', -+ 'wordexp.h', -+ -+ # WASI specific -+ 'wasi/api.h', -+] -+}% -+ -+// FIXME?(katei): -+// Those emulations are now enabled by default to have compatibility -+// with other platforms as much as possible without any extra modification. -+// But we should consider moving them into their own submodules. -+#ifndef _WASI_EMULATED_MMAN -+# define _WASI_EMULATED_MMAN -+#endif -+#ifndef _WASI_EMULATED_SIGNAL -+# define _WASI_EMULATED_SIGNAL -+#endif -+#ifndef _WASI_EMULATED_PROCESS_CLOCKS -+# define _WASI_EMULATED_PROCESS_CLOCKS -+#endif -+ -+% for header in headers: -+#if __has_include(<${header}>) -+#include <${header}> -+#endif -+% end -diff --git a/stdlib/public/Platform/WASILibc.swift.gyb b/stdlib/public/Platform/WASILibc.swift.gyb -new file mode 100644 -index 00000000000..bd6e03ed3c4 ---- /dev/null -+++ b/stdlib/public/Platform/WASILibc.swift.gyb -@@ -0,0 +1,161 @@ -+//===----------------------------------------------------------------------===// -+// -+// This source file is part of the Swift.org open source project -+// -+// Copyright (c) 2020 Apple Inc. and the Swift project authors -+// Licensed under Apache License v2.0 with Runtime Library Exception -+// -+// See https://swift.org/LICENSE.txt for license information -+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -+// -+//===----------------------------------------------------------------------===// -+ -+@_exported import SwiftWASILibc // Clang module -+ -+// Constants defined by -+@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.") -+public let M_PI = Double.pi -+ -+@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.") -+public let M_PI_2 = Double.pi / 2 -+ -+@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.") -+public let M_PI_4 = Double.pi / 4 -+ -+@available(swift, deprecated: 3.0, message: "Please use '2.squareRoot()'.") -+public let M_SQRT2 = 2.squareRoot() -+ -+@available(swift, deprecated: 3.0, message: "Please use '0.5.squareRoot()'.") -+public let M_SQRT1_2 = 0.5.squareRoot() -+ -+// Constants defined by -+@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.") -+public let FLT_RADIX = Double.radix -+ -+%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL')]: -+// Where does the 1 come from? C counts the usually-implicit leading -+// significand bit, but Swift does not. Neither is really right or wrong. -+@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.") -+public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1 -+ -+// Where does the 1 come from? C models floating-point numbers as having a -+// significand in [0.5, 1), but Swift (following IEEE 754) considers the -+// significand to be in [1, 2). This rationale applies to ${prefix}_MIN_EXP -+// as well. -+@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.") -+public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1 -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.") -+public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1 -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.") -+public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.") -+public let ${prefix}_EPSILON = ${type}.ulpOfOne -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.") -+public let ${prefix}_MIN = ${type}.leastNormalMagnitude -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.") -+public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude -+ -+%end -+ -+public let MAP_FAILED: UnsafeMutableRawPointer! = UnsafeMutableRawPointer(bitPattern: -1) -+ -+// TODO: wasi-libc's error.h defines these macros as function-like macros, which -+// Swift can't import for now. -+%{ -+posix_error_codes = [ -+ "E2BIG", -+ "EACCES", -+ "EADDRINUSE", -+ "EADDRNOTAVAIL", -+ "EAFNOSUPPORT", -+ "EAGAIN", -+ "EALREADY", -+ "EBADF", -+ "EBADMSG", -+ "EBUSY", -+ "ECANCELED", -+ "ECHILD", -+ "ECONNABORTED", -+ "ECONNREFUSED", -+ "ECONNRESET", -+ "EDEADLK", -+ "EDESTADDRREQ", -+ "EDOM", -+ "EDQUOT", -+ "EEXIST", -+ "EFAULT", -+ "EFBIG", -+ "EHOSTUNREACH", -+ "EIDRM", -+ "EILSEQ", -+ "EINPROGRESS", -+ "EINTR", -+ "EINVAL", -+ "EIO", -+ "EISCONN", -+ "EISDIR", -+ "ELOOP", -+ "EMFILE", -+ "EMLINK", -+ "EMSGSIZE", -+ "EMULTIHOP", -+ "ENAMETOOLONG", -+ "ENETDOWN", -+ "ENETRESET", -+ "ENETUNREACH", -+ "ENFILE", -+ "ENOBUFS", -+ "ENODEV", -+ "ENOENT", -+ "ENOEXEC", -+ "ENOLCK", -+ "ENOLINK", -+ "ENOMEM", -+ "ENOMSG", -+ "ENOPROTOOPT", -+ "ENOSPC", -+ "ENOSYS", -+ "ENOTCONN", -+ "ENOTDIR", -+ "ENOTEMPTY", -+ "ENOTRECOVERABLE", -+ "ENOTSOCK", -+ "ENOTSUP", -+ "ENOTTY", -+ "ENXIO", -+ "EOVERFLOW", -+ "EOWNERDEAD", -+ "EPERM", -+ "EPIPE", -+ "EPROTO", -+ "EPROTONOSUPPORT", -+ "EPROTOTYPE", -+ "ERANGE", -+ "EROFS", -+ "ESPIPE", -+ "ESRCH", -+ "ESTALE", -+ "ETIMEDOUT", -+ "ETXTBSY", -+ "EXDEV", -+ "ENOTCAPABLE", -+] -+}% -+ -+%for ecode in posix_error_codes: -+ -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.${ecode}'.") -+public let ${ecode} = POSIXErrorCode.${ecode}.rawValue -+ -+%end -+ -+// TODO: wasi-libc's _seek.h defines these macros as function-like macros, which -+// Swift can't import for now. -+public let SEEK_SET: Int32 = 0 -+public let SEEK_CUR: Int32 = 1 -+public let SEEK_END: Int32 = 2 -diff --git a/stdlib/public/Platform/wasi-libc.modulemap.gyb b/stdlib/public/Platform/wasi-libc.modulemap.gyb -new file mode 100644 -index 00000000000..59be5d56499 ---- /dev/null -+++ b/stdlib/public/Platform/wasi-libc.modulemap.gyb -@@ -0,0 +1,18 @@ -+//===--- wasi-libc.modulemap.gyb ------------------------------------------===// -+// -+// This source file is part of the Swift.org open source project -+// -+// Copyright (c) 2020 Apple Inc. and the Swift project authors -+// Licensed under Apache License v2.0 with Runtime Library Exception -+// -+// See https://swift.org/LICENSE.txt for license information -+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -+// -+//===----------------------------------------------------------------------===// -+ -+module SwiftWASILibc [system] { -+ // C standard library -+ header "SwiftWASILibc.h" -+ -+ export * -+} --- -2.40.0 - diff --git a/schemes/main/swift/0002-ClangImporter-Support-wasi-libc.modulemap-import-wit.patch b/schemes/main/swift/0002-ClangImporter-Support-wasi-libc.modulemap-import-wit.patch deleted file mode 100644 index a13dabe6..00000000 --- a/schemes/main/swift/0002-ClangImporter-Support-wasi-libc.modulemap-import-wit.patch +++ /dev/null @@ -1,91 +0,0 @@ -From a11041f37625d9b81b6ff03b8b523967b9bc55ed Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Sat, 10 Jun 2023 14:51:49 +0000 -Subject: [PATCH] [ClangImporter] Support wasi-libc.modulemap import with VFS - ---- - lib/ClangImporter/ClangIncludePaths.cpp | 41 +++++++++++++++++++------ - 1 file changed, 31 insertions(+), 10 deletions(-) - -diff --git a/lib/ClangImporter/ClangIncludePaths.cpp b/lib/ClangImporter/ClangIncludePaths.cpp -index 697c53b91c1..8faf184a472 100644 ---- a/lib/ClangImporter/ClangIncludePaths.cpp -+++ b/lib/ClangImporter/ClangIncludePaths.cpp -@@ -101,6 +101,13 @@ static llvm::Optional getGlibcModuleMapPath( - /*isArchSpecific*/ true, vfs); - } - -+static llvm::Optional getWASILibcModuleMapPath( -+ SearchPathOptions &Opts, const llvm::Triple &triple, -+ const llvm::IntrusiveRefCntPtr &vfs) { -+ return getActualModuleMapPath("wasi-libc.modulemap", Opts, triple, -+ /*isArchSpecific*/ true, vfs); -+} -+ - static llvm::Optional getLibStdCxxModuleMapPath( - SearchPathOptions &opts, const llvm::Triple &triple, - const llvm::IntrusiveRefCntPtr &vfs) { -@@ -195,11 +202,32 @@ static bool shouldInjectGlibcModulemap(const llvm::Triple &triple) { - triple.isAndroid(); - } - -+static bool shouldInjectWASILibcModulemap(const llvm::Triple &triple) { -+ return triple.isOSWASI(); -+} -+ - static SmallVector, 2> getGlibcFileMapping( - ASTContext &ctx, - const llvm::IntrusiveRefCntPtr &vfs) { - const llvm::Triple &triple = ctx.LangOpts.Target; -- if (!shouldInjectGlibcModulemap(triple)) -+ -+ std::string auxiliaryHeaderName; -+ llvm::Optional maybeActualModuleMapPath; -+ if (shouldInjectGlibcModulemap(triple)) { -+ auxiliaryHeaderName = "SwiftGlibc.h"; -+ maybeActualModuleMapPath = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs); -+ } else if (shouldInjectWASILibcModulemap(triple)) { -+ auxiliaryHeaderName = "SwiftWASILibc.h"; -+ maybeActualModuleMapPath = getWASILibcModuleMapPath(ctx.SearchPathOpts, triple, vfs); -+ } else { -+ return {}; -+ } -+ -+ Path actualModuleMapPath; -+ if (auto path = maybeActualModuleMapPath) -+ actualModuleMapPath = path.value(); -+ else -+ // FIXME: Emit a warning of some kind. - return {}; - - // Extract the Glibc path from Clang driver. -@@ -225,24 +253,17 @@ static SmallVector, 2> getGlibcFileMapping( - return {}; - } - -- Path actualModuleMapPath; -- if (auto path = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs)) -- actualModuleMapPath = path.value(); -- else -- // FIXME: Emit a warning of some kind. -- return {}; -- - // TODO: remove the SwiftGlibc.h header and reference all Glibc headers - // directly from the modulemap. - Path actualHeaderPath = actualModuleMapPath; - llvm::sys::path::remove_filename(actualHeaderPath); -- llvm::sys::path::append(actualHeaderPath, "SwiftGlibc.h"); -+ llvm::sys::path::append(actualHeaderPath, auxiliaryHeaderName); - - Path injectedModuleMapPath(glibcDir); - llvm::sys::path::append(injectedModuleMapPath, "module.modulemap"); - - Path injectedHeaderPath(glibcDir); -- llvm::sys::path::append(injectedHeaderPath, "SwiftGlibc.h"); -+ llvm::sys::path::append(injectedHeaderPath, auxiliaryHeaderName); - - return { - {std::string(injectedModuleMapPath), std::string(actualModuleMapPath)}, --- -2.40.0 - diff --git a/schemes/main/swift/0003-build-Replace-glibc-specific-naming-in-libc-like-mod.patch b/schemes/main/swift/0003-build-Replace-glibc-specific-naming-in-libc-like-mod.patch deleted file mode 100644 index 0cfdc2d4..00000000 --- a/schemes/main/swift/0003-build-Replace-glibc-specific-naming-in-libc-like-mod.patch +++ /dev/null @@ -1,195 +0,0 @@ -From 8d736a6ed61eee209eb1416b73087cbaf833dcda Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Sat, 10 Jun 2023 12:57:10 +0000 -Subject: [PATCH] [build] Replace glibc specific naming in libc-like modulemap - generation loop - ---- - stdlib/public/Platform/CMakeLists.txt | 90 +++++++++++++-------------- - 1 file changed, 45 insertions(+), 45 deletions(-) - -diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt -index e0ee5283fc5..a3f090e28ea 100644 ---- a/stdlib/public/Platform/CMakeLists.txt -+++ b/stdlib/public/Platform/CMakeLists.txt -@@ -96,7 +96,7 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O - LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" - TARGET_SDKS "${swiftGlibc_target_sdks}" - INSTALL_IN_COMPONENT sdk-overlay -- DEPENDS glibc_modulemap) -+ DEPENDS libc_modulemap) - - add_swift_target_library(swiftMusl ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY - ${swift_platform_sources} -@@ -129,7 +129,7 @@ add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SD - LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" - TARGET_SDKS WASI - INSTALL_IN_COMPONENT sdk-overlay -- DEPENDS glibc_modulemap) -+ DEPENDS libc_modulemap) - - add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY - ucrt.swift -@@ -148,7 +148,7 @@ add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE - TARGET_SDKS WINDOWS - INSTALL_IN_COMPONENT sdk-overlay) - --set(glibc_modulemap_target_list) -+set(libc_modulemap_target_list) - foreach(sdk ${SWIFT_SDKS}) - if("${sdk}" STREQUAL "LINUX" OR - "${sdk}" STREQUAL "FREEBSD" OR -@@ -156,65 +156,65 @@ foreach(sdk ${SWIFT_SDKS}) - "${sdk}" STREQUAL "ANDROID" OR - "${sdk}" STREQUAL "CYGWIN" OR - "${sdk}" STREQUAL "HAIKU") -- set(glibc_modulemap_source "glibc.modulemap.gyb") -- set(glibc_header_source "SwiftGlibc.h.gyb") -+ set(libc_modulemap_source "glibc.modulemap.gyb") -+ set(libc_header_source "SwiftGlibc.h.gyb") - elseif("${sdk}" STREQUAL "WASI") -- set(glibc_modulemap_source "wasi-libc.modulemap.gyb") -- set(glibc_header_source "SwiftWASILibc.h.gyb") -+ set(libc_modulemap_source "wasi-libc.modulemap.gyb") -+ set(libc_header_source "SwiftWASILibc.h.gyb") - else() - continue() - endif() - -- string(REGEX REPLACE "\\.gyb$" "" glibc_modulemap_outname "${glibc_modulemap_source}") -- string(REGEX REPLACE "\\.gyb$" "" glibc_header_outname "${glibc_header_source}") -+ string(REGEX REPLACE "\\.gyb$" "" libc_modulemap_outname "${libc_modulemap_source}") -+ string(REGEX REPLACE "\\.gyb$" "" libc_header_outname "${libc_header_source}") - - foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) - set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}") - set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}") - set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}") - -- set(glibc_modulemap_out "${module_dir}/${glibc_modulemap_outname}") -- set(glibc_modulemap_out_static "${module_dir_static}/${glibc_modulemap_outname}") -+ set(libc_modulemap_out "${module_dir}/${libc_modulemap_outname}") -+ set(libc_modulemap_out_static "${module_dir_static}/${libc_modulemap_outname}") - - # Configure the module map based on the target. Each platform needs to -- # reference different headers, based on what's available in their glibc. -- # This is the 'glibc.modulemap' in the 'resource-dir', so -+ # reference different headers, based on what's available in their libc. -+ # This is the .modulemap in the 'resource-dir', so - # it's the one we'll look at during the build process. -- handle_gyb_source_single(glibc_modulemap_target -- SOURCE "${glibc_modulemap_source}" -- OUTPUT "${glibc_modulemap_out}" -+ handle_gyb_source_single(libc_modulemap_target -+ SOURCE "${libc_modulemap_source}" -+ OUTPUT "${libc_modulemap_out}" - FLAGS - "-DCMAKE_SDK=${sdk}") - -- list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target}) -+ list(APPEND libc_modulemap_target_list ${libc_modulemap_target}) - -- set(glibc_header_out "${module_dir}/${glibc_header_outname}") -- set(glibc_header_out_static "${module_dir_static}/${glibc_header_outname}") -- handle_gyb_source_single(glibc_header_target -- SOURCE "${glibc_header_source}" -- OUTPUT "${glibc_header_out}" -+ set(libc_header_out "${module_dir}/${libc_header_outname}") -+ set(libc_header_out_static "${module_dir_static}/${libc_header_outname}") -+ handle_gyb_source_single(libc_header_target -+ SOURCE "${libc_header_source}" -+ OUTPUT "${libc_header_out}" - FLAGS "-DCMAKE_SDK=${sdk}") -- list(APPEND glibc_modulemap_target_list ${glibc_header_target}) -+ list(APPEND libc_modulemap_target_list ${libc_header_target}) - - if(SWIFT_BUILD_STATIC_STDLIB) - add_custom_command_target( -- copy_glibc_modulemap_header_static -+ copy_libc_modulemap_header_static - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} - COMMAND - "${CMAKE_COMMAND}" "-E" "copy" -- ${glibc_modulemap_out} ${glibc_modulemap_out_static} -+ ${libc_modulemap_out} ${libc_modulemap_out_static} - COMMAND - "${CMAKE_COMMAND}" "-E" "copy" -- ${glibc_header_out} ${glibc_header_out_static} -- OUTPUT ${glibc_modulemap_out_static} ${glibc_header_out_static} -+ ${libc_header_out} ${libc_header_out_static} -+ OUTPUT ${libc_modulemap_out_static} ${libc_header_out_static} - DEPENDS -- "${glibc_modulemap_target}" -- "${glibc_header_target}" -- COMMENT "Copying Glibc modulemap and header to static resources") -+ "${libc_modulemap_target}" -+ "${libc_header_target}" -+ COMMENT "Copying libc modulemap and header to static resources") - -- list(APPEND glibc_modulemap_target_list -- ${copy_glibc_modulemap_header_static}) -+ list(APPEND libc_modulemap_target_list -+ ${copy_libc_modulemap_header_static}) - endif() - - # If this SDK is a target for a non-native host, except if it's for Android -@@ -222,15 +222,15 @@ foreach(sdk ${SWIFT_SDKS}) - # prefix. This is the one we'll install instead. - if(NOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${arch}_PATH}" STREQUAL "/" AND - NOT (sdk STREQUAL "ANDROID" AND NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")) -- set(glibc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/${glibc_modulemap_outname}") -+ set(libc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/${libc_modulemap_outname}") - -- handle_gyb_source_single(glibc_modulemap_native_target -- SOURCE "${glibc_modulemap_source}" -- OUTPUT "${glibc_sysroot_relative_modulemap_out}" -+ handle_gyb_source_single(libc_modulemap_native_target -+ SOURCE "${libc_modulemap_source}" -+ OUTPUT "${libc_sysroot_relative_modulemap_out}" - FLAGS "-DCMAKE_SDK=${sdk}") - -- list(APPEND glibc_modulemap_target_list ${glibc_modulemap_native_target}) -- set(glibc_modulemap_out ${glibc_sysroot_relative_modulemap_out}) -+ list(APPEND libc_modulemap_target_list ${libc_modulemap_native_target}) -+ set(libc_modulemap_out ${libc_sysroot_relative_modulemap_out}) - endif() - - # FIXME: When SDK is a cross-compile target (SDK != Host), the generated -@@ -238,26 +238,26 @@ foreach(sdk ${SWIFT_SDKS}) - # It is not relocatable to the target platform itself. - # This affects any cross-compiled targets that use glibc.modulemap. - -- swift_install_in_component(FILES "${glibc_modulemap_out}" -+ swift_install_in_component(FILES "${libc_modulemap_out}" - DESTINATION "lib/swift/${arch_subdir}" - COMPONENT sdk-overlay) -- swift_install_in_component(FILES "${glibc_header_out}" -+ swift_install_in_component(FILES "${libc_header_out}" - DESTINATION "lib/swift/${arch_subdir}" - COMPONENT sdk-overlay) - - if(SWIFT_BUILD_STATIC_STDLIB) -- swift_install_in_component(FILES "${glibc_modulemap_out}" -+ swift_install_in_component(FILES "${libc_modulemap_out}" - DESTINATION "lib/swift_static/${arch_subdir}" - COMPONENT sdk-overlay) -- swift_install_in_component(FILES "${glibc_header_out}" -+ swift_install_in_component(FILES "${libc_header_out}" - DESTINATION "lib/swift_static/${arch_subdir}" - COMPONENT sdk-overlay) - endif() - endforeach() - endforeach() --add_custom_target(glibc_modulemap DEPENDS ${glibc_modulemap_target_list}) --set_property(TARGET glibc_modulemap PROPERTY FOLDER "Miscellaneous") --add_dependencies(sdk-overlay glibc_modulemap) -+add_custom_target(libc_modulemap DEPENDS ${libc_modulemap_target_list}) -+set_property(TARGET libc_modulemap PROPERTY FOLDER "Miscellaneous") -+add_dependencies(sdk-overlay libc_modulemap) - - if(WINDOWS IN_LIST SWIFT_SDKS) - swift_install_in_component(FILES --- -2.40.0 - diff --git a/schemes/main/swift/0004-wasm-Driver-HACK-Add-wasi-sysroot-layout-and-auto-de.patch b/schemes/main/swift/0004-wasm-Driver-HACK-Add-wasi-sysroot-layout-and-auto-de.patch deleted file mode 100644 index fdf3f48f..00000000 --- a/schemes/main/swift/0004-wasm-Driver-HACK-Add-wasi-sysroot-layout-and-auto-de.patch +++ /dev/null @@ -1,53 +0,0 @@ -From befdc1040d40bf33aa74bcdfc95a76401c41dcc8 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 07:11:04 +0000 -Subject: [PATCH] [wasm][Driver] HACK: Add wasi-sysroot layout and - auto-detection support - -Currenty, SwiftWasm toolchain contains wasi-sysroot under -`./usr/share/wasi-sysroot` directory in the toolchain package, but we -don't have a good consensus on the location of wasi-sysroot yet. So this -patch is a temporary solution just to avoid the need to specify `-sdk` -option manually. That being said, SwiftPM already passes `-sdk` option -to the driver, so the option is only needed when the driver is invoked -directly. ---- - lib/Driver/Driver.cpp | 7 +++++++ - lib/Driver/ToolChains.cpp | 3 ++- - 2 files changed, 9 insertions(+), 1 deletion(-) - -diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp -index 04f9684eb85..56e0d8cec43 100644 ---- a/lib/Driver/Driver.cpp -+++ b/lib/Driver/Driver.cpp -@@ -1794,6 +1794,13 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args, - }); - } - } -+ } if (OI.SDKPath.empty() && TC.getTriple().isOSWASI()) { -+ llvm::SmallString<128> SDKPath; -+ llvm::sys::path::append(SDKPath, getSwiftProgramPath()); -+ llvm::sys::path::remove_filename(SDKPath); // 'swift' -+ llvm::sys::path::remove_filename(SDKPath); // 'bin' -+ llvm::sys::path::append(SDKPath, "share", "wasi-sysroot"); -+ OI.SDKPath = SDKPath.str().str(); - } - - if (!OI.SDKPath.empty()) { -diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp -index eb1c8a0ade5..a1aecdc6d84 100644 ---- a/lib/Driver/ToolChains.cpp -+++ b/lib/Driver/ToolChains.cpp -@@ -1472,7 +1472,8 @@ void ToolChain::getResourceDirPath(SmallVectorImpl &resourceDirPath, - if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) { - StringRef value = A->getValue(); - resourceDirPath.append(value.begin(), value.end()); -- } else if (!getTriple().isOSDarwin() && args.hasArg(options::OPT_sdk)) { -+ } else if (!getTriple().isOSDarwin() && !getTriple().isOSWASI() && args.hasArg(options::OPT_sdk)) { -+ // for WASI, sdk option points to wasi-sysroot which doesn't have Swift toolchain - StringRef value = args.getLastArg(options::OPT_sdk)->getValue(); - resourceDirPath.append(value.begin(), value.end()); - llvm::sys::path::append(resourceDirPath, "usr"); --- -2.40.0 - diff --git a/schemes/main/swift/0005-wasm-Driver-HACK-Add-use-static-resource-dir-by-defa.patch b/schemes/main/swift/0005-wasm-Driver-HACK-Add-use-static-resource-dir-by-defa.patch deleted file mode 100644 index 3cc05cdb..00000000 --- a/schemes/main/swift/0005-wasm-Driver-HACK-Add-use-static-resource-dir-by-defa.patch +++ /dev/null @@ -1,76 +0,0 @@ -From af29fc6c8897c036d61d94e86dade390245a5a80 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 07:17:38 +0000 -Subject: [PATCH] [wasm][Driver] HACK: Add `-use-static-resource-dir` by - default for wasm target - -Currently, wasm target only supports static linking mode, so we add -`-use-static-resource-dir` by default for wasm target. This patch is not -an essential change because we can also add `-use-static-resource-dir` -in the driver invocation. But it is a convenient change for users. ---- - lib/Driver/Driver.cpp | 3 ++- - lib/Driver/ToolChains.cpp | 19 +++++++++++++++++-- - 2 files changed, 19 insertions(+), 3 deletions(-) - -diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp -index 56e0d8cec43..20c8571ff07 100644 ---- a/lib/Driver/Driver.cpp -+++ b/lib/Driver/Driver.cpp -@@ -2402,7 +2402,8 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) { - if (Args.hasFlag(options::OPT_static_executable, - options::OPT_no_static_executable, false) || - Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib, -- false)) { -+ false) || -+ TC.getTriple().isOSBinFormatWasm()) { - commandLine.push_back("-use-static-resource-dir"); - } - -diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp -index a1aecdc6d84..8e27d7d3410 100644 ---- a/lib/Driver/ToolChains.cpp -+++ b/lib/Driver/ToolChains.cpp -@@ -660,7 +660,8 @@ ToolChain::constructInvocation(const CompileJobAction &job, - if (context.Args.hasFlag(options::OPT_static_executable, - options::OPT_no_static_executable, false) || - context.Args.hasFlag(options::OPT_static_stdlib, -- options::OPT_no_static_stdlib, false)) { -+ options::OPT_no_static_stdlib, false) || -+ getTriple().isOSBinFormatWasm()) { - Arguments.push_back("-use-static-resource-dir"); - } - -@@ -1188,7 +1189,13 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job, - context.Args.AddLastArg(Arguments, options::OPT_import_objc_header); - - context.Args.AddLastArg(Arguments, options::OPT_disable_incremental_imports); -- -+ if (context.Args.hasFlag(options::OPT_static_executable, -+ options::OPT_no_static_executable, false) || -+ context.Args.hasFlag(options::OPT_static_stdlib, -+ options::OPT_no_static_stdlib, false) || -+ getTriple().isOSBinFormatWasm()) { -+ Arguments.push_back("-use-static-resource-dir"); -+ } - Arguments.push_back("-module-name"); - Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName)); - -@@ -1388,6 +1395,14 @@ ToolChain::constructInvocation(const GeneratePCHJobAction &job, - addInputsOfType(Arguments, context.InputActions, file_types::TY_ClangHeader); - context.Args.AddLastArg(Arguments, options::OPT_index_store_path); - -+ if (context.Args.hasFlag(options::OPT_static_executable, -+ options::OPT_no_static_executable, false) || -+ context.Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib, -+ false) || -+ getTriple().isOSBinFormatWasm()) { -+ Arguments.push_back("-use-static-resource-dir"); -+ } -+ - if (job.isPersistentPCH()) { - Arguments.push_back("-emit-pch"); - Arguments.push_back("-pch-output-dir"); --- -2.40.0 - diff --git a/schemes/main/swift/0006-wasm-IRGen-HACK-Disable-stack-protection.patch b/schemes/main/swift/0006-wasm-IRGen-HACK-Disable-stack-protection.patch deleted file mode 100644 index 7d018530..00000000 --- a/schemes/main/swift/0006-wasm-IRGen-HACK-Disable-stack-protection.patch +++ /dev/null @@ -1,29 +0,0 @@ -From 108aa743a52681aca1e2c914840ee10d6f8d189b Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 07:21:31 +0000 -Subject: [PATCH] [wasm][IRGen] HACK: Disable stack protection - -This is a temporary workaround until we can update wasi-sdk to version -17 or later, which includes stack protection support in wasi-libc. -https://github.com/WebAssembly/wasi-libc/commit/8b7148f69ae241a2749b3defe4606da8143b72e0 ---- - lib/IRGen/GenDecl.cpp | 3 ++- - 1 file changed, 2 insertions(+), 1 deletion(-) - -diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp -index 8062188cf4c..11edf34c0c1 100644 ---- a/lib/IRGen/GenDecl.cpp -+++ b/lib/IRGen/GenDecl.cpp -@@ -3358,7 +3358,8 @@ llvm::CallBase *swift::irgen::emitCXXConstructorCall( - - StackProtectorMode IRGenModule::shouldEmitStackProtector(SILFunction *f) { - const SILOptions &opts = IRGen.SIL.getOptions(); -- return (opts.EnableStackProtection && f->needsStackProtection()) ? -+ // FIXME(katei): stack protection support will be added by https://github.com/WebAssembly/wasi-libc/pull/351 -+ return (opts.EnableStackProtection && f->needsStackProtection() && Triple.getObjectFormat() != llvm::Triple::Wasm) ? - StackProtectorMode::StackProtector : StackProtectorMode::NoStackProtector; - } - --- -2.40.0 - diff --git a/schemes/main/swift/0007-wasm-SIL-IRGen-HACK-Always-forward-extra-argument-to.patch b/schemes/main/swift/0007-wasm-SIL-IRGen-HACK-Always-forward-extra-argument-to.patch deleted file mode 100644 index 6178fbf6..00000000 --- a/schemes/main/swift/0007-wasm-SIL-IRGen-HACK-Always-forward-extra-argument-to.patch +++ /dev/null @@ -1,246 +0,0 @@ -From 4f84234b8ca23a37b32ef5666489e6cbe400c7d6 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 07:25:35 +0000 -Subject: [PATCH] [wasm][SIL/IRGen] HACK: Always forward extra argument to - match callee and caller signature - -This is a legacy solution for the KeyPath calling convention problem -described in https://forums.swift.org/t/wasm-support/16087/21. This -patch will be replaced by https://github.com/apple/swift/pull/66273 ---- - lib/IRGen/GenKeyPath.cpp | 7 +++- - lib/SIL/Verifier/SILVerifier.cpp | 10 +++-- - lib/SILGen/SILGenExpr.cpp | 39 +++++++++++++++++-- - .../SILCombiner/SILCombinerApplyVisitors.cpp | 14 +++++++ - lib/SILOptimizer/Utils/KeyPathProjector.cpp | 17 +++++++- - 5 files changed, 77 insertions(+), 10 deletions(-) - -diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp -index d23a53e822c..dfd37ec2830 100644 ---- a/lib/IRGen/GenKeyPath.cpp -+++ b/lib/IRGen/GenKeyPath.cpp -@@ -224,7 +224,8 @@ getAccessorForComputedComponent(IRGenModule &IGM, - componentArgsBuf = params.claimNext(); - // Pass the argument pointer down to the underlying function, if it - // wants it. -- if (hasSubscriptIndices) { -+ // Always forward extra argument to match callee and caller signature on WebAssembly -+ if (hasSubscriptIndices || IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { - forwardedArgs.add(componentArgsBuf); - } - break; -@@ -250,6 +251,10 @@ getAccessorForComputedComponent(IRGenModule &IGM, - forwardingSubs, - &ignoreWitnessMetadata, - forwardedArgs); -+ } else if (IGM.Triple.isOSBinFormatWasm()) { -+ // wasm: Add null swift.type pointer to match signature even when there is -+ // no generic environment. -+ forwardedArgs.add(llvm::ConstantPointerNull::get(IGM.TypeMetadataPtrTy)); - } - auto fnPtr = - FunctionPointer::forDirect(IGM, accessorFn, /*secondaryValue*/ nullptr, -diff --git a/lib/SIL/Verifier/SILVerifier.cpp b/lib/SIL/Verifier/SILVerifier.cpp -index 0bf3941d6a3..9ef2761cfed 100644 ---- a/lib/SIL/Verifier/SILVerifier.cpp -+++ b/lib/SIL/Verifier/SILVerifier.cpp -@@ -304,8 +304,9 @@ void verifyKeyPathComponent(SILModule &M, - SILFunctionTypeRepresentation::Thin, - "getter should be a thin function"); - -- require(substGetterType->getNumParameters() == 1 + hasIndices, -- "getter should have one parameter"); -+ // FIXME(katei): Disabled for now. Will be replaced by keypath cc -+ // require(substGetterType->getNumParameters() == 1 + hasIndices, -+ // "getter should have one parameter"); - auto baseParam = substGetterType->getParameters()[0]; - require(baseParam.getConvention() == normalArgConvention, - "getter base parameter should have normal arg convention"); -@@ -356,8 +357,9 @@ void verifyKeyPathComponent(SILModule &M, - SILFunctionTypeRepresentation::Thin, - "setter should be a thin function"); - -- require(substSetterType->getNumParameters() == 2 + hasIndices, -- "setter should have two parameters"); -+ // FIXME(katei): Disabled for now. Will be replaced by keypath cc -+ // require(substSetterType->getNumParameters() == 2 + hasIndices, -+ // "setter should have two parameters"); - - auto newValueParam = substSetterType->getParameters()[0]; - // TODO: This should probably be unconditionally +1 when we -diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp -index d09f4974b7b..71d63a34f3d 100644 ---- a/lib/SILGen/SILGenExpr.cpp -+++ b/lib/SILGen/SILGenExpr.cpp -@@ -2987,6 +2987,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - } - } - -+ auto Target = SGM.getASTContext().LangOpts.Target; - auto genericSig = - genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() - : nullptr; -@@ -2995,6 +2996,14 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - genericEnv = nullptr; - } - -+ // Add empty generic type parameter to match function signature on WebAssembly -+ if (!genericSig && Target.isOSBinFormatWasm()) { -+ auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext()); -+ auto sig = GenericSignature::get(param, { }); -+ genericSig = CanGenericSignature(sig); -+ genericEnv = sig.getGenericEnvironment(); -+ } -+ - // Build the signature of the thunk as expected by the keypath runtime. - auto signature = [&]() { - CanType loweredBaseTy, loweredPropTy; -@@ -3010,7 +3019,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - SmallVector params; - params.push_back({loweredBaseTy, paramConvention}); - auto &C = SGM.getASTContext(); -- if (!indexes.empty()) -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm()) - params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(), - ParameterConvention::Direct_Unowned}); - -@@ -3070,7 +3080,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - } - auto baseArg = entry->createFunctionArgument(baseArgTy); - SILValue indexPtrArg; -- if (!indexes.empty()) { -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || Target.isOSBinFormatWasm()) { - auto indexArgTy = - subSGF.silConv.getSILType(signature->getParameters()[1], signature, - subSGF.F.getTypeExpansionContext()); -@@ -3160,6 +3171,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - } - } - -+ auto Target = SGM.getASTContext().LangOpts.Target; - auto genericSig = - genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() - : nullptr; -@@ -3168,6 +3180,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - genericEnv = nullptr; - } - -+ // Add empty generic type parameter to match function signature on WebAssembly -+ if (!genericSig && Target.isOSBinFormatWasm()) { -+ auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext()); -+ auto sig = GenericSignature::get(param, { }); -+ genericSig = CanGenericSignature(sig); -+ genericEnv = sig.getGenericEnvironment(); -+ } -+ - // Build the signature of the thunk as expected by the keypath runtime. - auto signature = [&]() { - CanType loweredBaseTy, loweredPropTy; -@@ -3193,7 +3213,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - ? ParameterConvention::Indirect_Inout - : paramConvention}); - // indexes -- if (!indexes.empty()) -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm()) - params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(), - ParameterConvention::Direct_Unowned}); - -@@ -3249,7 +3270,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - auto baseArg = entry->createFunctionArgument(baseArgTy); - SILValue indexPtrArg; - -- if (!indexes.empty()) { -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || Target.isOSBinFormatWasm()) { - auto indexArgTy = - subSGF.silConv.getSILType(signature->getParameters()[2], signature, - subSGF.getTypeExpansionContext()); -@@ -3342,6 +3364,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM, - return; - } - -+ auto Target = SGM.getASTContext().LangOpts.Target; - auto genericSig = - genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() - : nullptr; -@@ -3351,6 +3374,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM, - genericEnv = nullptr; - } - -+ // Add empty generic type parameter to match function signature on WebAssembly -+ if (!genericSig && Target.isOSBinFormatWasm()) { -+ auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext()); -+ auto sig = GenericSignature::get(param, { }); -+ genericSig = CanGenericSignature(sig); -+ genericEnv = sig.getGenericEnvironment(); -+ } -+ - auto &C = SGM.getASTContext(); - auto unsafeRawPointerTy = C.getUnsafeRawPointerType()->getCanonicalType(); - auto boolTy = C.getBoolType()->getCanonicalType(); -diff --git a/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp b/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp -index 6bf48c3d7f1..9b89e9d78e4 100644 ---- a/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp -+++ b/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp -@@ -505,6 +505,13 @@ bool SILCombiner::tryOptimizeKeypathKVCString(ApplyInst *AI, - } - - bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) { -+ // FIXME(katei): Disable for WebAssembly for now because -+ // KeyPath cc is unstable and KeyPathProjector hask violates -+ // some assert assumptions -+ SILModule &M = AI->getModule(); -+ if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) -+ return false; -+ - if (SILFunction *callee = AI->getReferencedFunctionOrNull()) { - return tryOptimizeKeypathApplication(AI, callee); - } -@@ -550,6 +557,13 @@ bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) { - /// %addr = struct_element_addr/ref_element_addr %root_object - /// // use %inout_addr - bool SILCombiner::tryOptimizeInoutKeypath(BeginApplyInst *AI) { -+ // FIXME(katei): Disable for WebAssembly for now because -+ // KeyPath cc is unstable and KeyPathProjector hask violates -+ // some assert assumptions -+ SILModule &M = AI->getModule(); -+ if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) -+ return false; -+ - // Disable in OSSA because KeyPathProjector is not fully ported - if (AI->getFunction()->hasOwnership()) - return false; -diff --git a/lib/SILOptimizer/Utils/KeyPathProjector.cpp b/lib/SILOptimizer/Utils/KeyPathProjector.cpp -index 073ba49a563..6950ab4ae10 100644 ---- a/lib/SILOptimizer/Utils/KeyPathProjector.cpp -+++ b/lib/SILOptimizer/Utils/KeyPathProjector.cpp -@@ -233,7 +233,22 @@ public: - assert(getter->getConventions().getNumSILArguments()); - - auto ref = builder.createFunctionRef(loc, getter); -- builder.createApply(loc, ref, subs, {addr, parentValue}); -+ -+ std::vector args{addr, parentValue}; -+ // FIXME(wasm): For wasm, KeyPath getter always take indices parameter -+ // to match callee and caller signature. So need to pass stub pointer. -+ // See also: getOrCreateKeyPathSetter and getOrCreateKeyPathGetter -+ if (builder.getASTContext().LangOpts.Target.isOSBinFormatWasm()) { -+ auto IntTy = SILType::getBuiltinIntegerType(32, builder.getASTContext()); -+ auto UnsafeRawPointerTy = SILType::getRawPointerType(builder.getASTContext()); -+ auto zeroVal = SILValue(builder.createIntegerLiteral(loc, IntTy, 0)); -+ auto stackBuffer = SILValue(builder.createAllocStack(loc, IntTy)); -+ builder.createStore(loc, zeroVal, stackBuffer, StoreOwnershipQualifier::Unqualified); -+ auto nonePointer = builder.createUncheckedAddrCast(loc, stackBuffer, UnsafeRawPointerTy); -+ args.push_back(SILValue(nonePointer)); -+ } -+ -+ builder.createApply(loc, ref, subs, args); - - // If we were previously accessing a class member, we're done now. - insertEndAccess(beginAccess, builder); --- -2.40.0 - diff --git a/schemes/main/swift/0008-wasm-cmake-Add-WASI-specific-stdlib-dependencies.patch b/schemes/main/swift/0008-wasm-cmake-Add-WASI-specific-stdlib-dependencies.patch deleted file mode 100644 index 2a28d867..00000000 --- a/schemes/main/swift/0008-wasm-cmake-Add-WASI-specific-stdlib-dependencies.patch +++ /dev/null @@ -1,79 +0,0 @@ -From 42890ba393098a717e9ebd9dfda42045e954dbcf Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 07:50:16 +0000 -Subject: [PATCH] [wasm][cmake] Add WASI specific stdlib dependencies - -Currently WASI target doesn't support SwiftPrivateThreadExtras, so we -need to remove it from the common dependencies. Also WASI target has its -own libc module WASILibc, so we need to add it as well as other libc modules. ---- - stdlib/private/StdlibUnittest/CMakeLists.txt | 1 + - stdlib/private/SwiftPrivate/CMakeLists.txt | 1 + - stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt | 1 + - stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt | 1 + - stdlib/public/Differentiation/CMakeLists.txt | 1 + - 5 files changed, 5 insertions(+) - -diff --git a/stdlib/private/StdlibUnittest/CMakeLists.txt b/stdlib/private/StdlibUnittest/CMakeLists.txt -index a8a3f17b60b..574b4aeea36 100644 ---- a/stdlib/private/StdlibUnittest/CMakeLists.txt -+++ b/stdlib/private/StdlibUnittest/CMakeLists.txt -@@ -61,6 +61,7 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - C_COMPILE_FLAGS -I${SWIFT_SOURCE_DIR}/stdlib/include - SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} -diff --git a/stdlib/private/SwiftPrivate/CMakeLists.txt b/stdlib/private/SwiftPrivate/CMakeLists.txt -index a1fc412a156..f1e7d461d0e 100644 ---- a/stdlib/private/SwiftPrivate/CMakeLists.txt -+++ b/stdlib/private/SwiftPrivate/CMakeLists.txt -@@ -26,6 +26,7 @@ add_swift_target_library(swiftSwiftPrivate ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - SWIFT_COMPILE_FLAGS ${swift_swiftprivate_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt -index 9095a700838..b8058031de8 100644 ---- a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt -+++ b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt -@@ -36,6 +36,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - INCORPORATE_OBJECT_LIBRARIES ${swift_private_libc_extras_incorporate_object_libraries} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt -index 35fcf24881c..52d2e28b9f5 100644 ---- a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt -+++ b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt -@@ -17,6 +17,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/public/Differentiation/CMakeLists.txt b/stdlib/public/Differentiation/CMakeLists.txt -index aa895c02bc8..30ad34900c7 100644 ---- a/stdlib/public/Differentiation/CMakeLists.txt -+++ b/stdlib/public/Differentiation/CMakeLists.txt -@@ -40,6 +40,7 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT - - C_COMPILE_FLAGS --- -2.40.0 - diff --git a/schemes/main/swift/0009-test-Fix-calling-convention-mismatch-for-swift_deman.patch b/schemes/main/swift/0009-test-Fix-calling-convention-mismatch-for-swift_deman.patch deleted file mode 100644 index 29e4eb51..00000000 --- a/schemes/main/swift/0009-test-Fix-calling-convention-mismatch-for-swift_deman.patch +++ /dev/null @@ -1,30 +0,0 @@ -From 55babf002797f1a0ba10a04dacda9e7214d45e2e Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 08:17:12 +0000 -Subject: [PATCH] [test] Fix calling convention mismatch for swift_demangle - -`test/stdlib/Runtime.swift.gyb` calls `swift_demangle` with the Swift -calling convention, but the functions is defined with the C calling -convention. But this does not cause a problem other than testing, and -ideal fix would be to put `@_cdecl` on the function declared in -Swift-side to choose the correct calling convention. `@cdecl` without -body is not supported yet but discussed in https://forums.swift.org/t/formalizing-cdecl/40677 ---- - stdlib/public/runtime/Demangle.cpp | 1 + - 1 file changed, 1 insertion(+) - -diff --git a/stdlib/public/runtime/Demangle.cpp b/stdlib/public/runtime/Demangle.cpp -index 7b593612949..0f614cbb889 100644 ---- a/stdlib/public/runtime/Demangle.cpp -+++ b/stdlib/public/runtime/Demangle.cpp -@@ -910,6 +910,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type, - /// \returns the demangled name. Returns nullptr if the input String is not a - /// Swift mangled name. - SWIFT_RUNTIME_EXPORT -+SWIFT_CC(swift) - char *swift_demangle(const char *mangledName, - size_t mangledNameLength, - char *outputBuffer, --- -2.40.0 - diff --git a/schemes/main/swift/0010-stdlib-Fix-calling-convention-mismatch-for-swift_COW.patch b/schemes/main/swift/0010-stdlib-Fix-calling-convention-mismatch-for-swift_COW.patch deleted file mode 100644 index 2ec325ad..00000000 --- a/schemes/main/swift/0010-stdlib-Fix-calling-convention-mismatch-for-swift_COW.patch +++ /dev/null @@ -1,32 +0,0 @@ -From 4e62fc36885b5ea6f1567c75c48e755aa269c49c Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 08:26:32 +0000 -Subject: [PATCH] [stdlib] Fix calling convention mismatch for - swift_COWChecksEnabled - -`ContiguousArrayBuffer.swift` calls `swift_COWChecksEnabled` with the -Swift calling convention, but the functions is defined with the C calling -convention. This mismatch actually happens only when building with -`enable-array-cow-checks` (which is not enabled by default). -Also ideal fix would be to put `@_cdecl` on the function declared in -Swift-side to choose the correct calling convention. `@cdecl` without -body is not supported yet but discussed in https://forums.swift.org/t/formalizing-cdecl/40677 ---- - stdlib/public/runtime/EnvironmentVariables.cpp | 1 + - 1 file changed, 1 insertion(+) - -diff --git a/stdlib/public/runtime/EnvironmentVariables.cpp b/stdlib/public/runtime/EnvironmentVariables.cpp -index 264e44a4db6..3a04eb9c928 100644 ---- a/stdlib/public/runtime/EnvironmentVariables.cpp -+++ b/stdlib/public/runtime/EnvironmentVariables.cpp -@@ -253,6 +253,7 @@ void swift::runtime::environment::initialize(void *context) { - } - #endif - -+SWIFT_CC(swift) - SWIFT_RUNTIME_EXPORT - bool swift_COWChecksEnabled() { - return runtime::environment::SWIFT_DEBUG_ENABLE_COW_CHECKS(); --- -2.40.0 - diff --git a/schemes/main/swift/0011-stdlib-Fix-calling-convention-mismatch-for-runtime-f.patch b/schemes/main/swift/0011-stdlib-Fix-calling-convention-mismatch-for-runtime-f.patch deleted file mode 100644 index 77d11090..00000000 --- a/schemes/main/swift/0011-stdlib-Fix-calling-convention-mismatch-for-runtime-f.patch +++ /dev/null @@ -1,161 +0,0 @@ -From 0fbd2f2c680ec6e61f33fc2dcc59475f089c5f2b Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 08:44:04 +0000 -Subject: [PATCH] [stdlib] Fix calling convention mismatch for runtime function - tracking - -Runtime function tracking APIs are defined with the C calling convention -but they are called from Swift through `@_silgen_name`, so they are -called with the Swift calling convention. This patch fixes the mismatch -by adding `SWIFT_CC(swift)` to the definitions, but ideally we should be -able to call these APIs with the C calling convention with `@_cdecl`. -`@cdecl` without body is not supported yet but discussed in -https://forums.swift.org/t/formalizing-cdecl/40677 ---- - .../runtime/RuntimeInvocationsTracking.cpp | 16 +++++++++------- - .../runtime/RuntimeInvocationsTracking.h | 18 +++++++++--------- - 2 files changed, 18 insertions(+), 16 deletions(-) - -diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp -index bf5812b4ba1..51a9ab16b11 100644 ---- a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp -+++ b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp -@@ -130,7 +130,7 @@ static std::uint16_t RuntimeFunctionCountersOffsets[] = { - /// Public APIs - - /// Get the runtime object state associated with an object. --void _swift_getObjectRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_getObjectRuntimeFunctionCounters( - HeapObject *object, RuntimeFunctionCountersState *result) { - auto &theSentinel = RuntimeObjectStateCache.get(); - Mutex::ScopedLock lock(theSentinel.Lock); -@@ -139,7 +139,7 @@ void _swift_getObjectRuntimeFunctionCounters( - - /// Set the runtime object state associated with an object from a provided - /// state. --void _swift_setObjectRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_setObjectRuntimeFunctionCounters( - HeapObject *object, RuntimeFunctionCountersState *state) { - auto &theSentinel = RuntimeObjectStateCache.get(); - Mutex::ScopedLock lock(theSentinel.Lock); -@@ -148,14 +148,14 @@ void _swift_setObjectRuntimeFunctionCounters( - - /// Get the global runtime state containing the total numbers of invocations for - /// each runtime function of interest. --void _swift_getGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_getGlobalRuntimeFunctionCounters( - RuntimeFunctionCountersState *result) { - LazyMutex::ScopedLock lock(RuntimeGlobalFunctionCountersState.Lock); - *result = RuntimeGlobalFunctionCountersState.State; - } - - /// Set the global runtime state of function pointers from a provided state. --void _swift_setGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_setGlobalRuntimeFunctionCounters( - RuntimeFunctionCountersState *state) { - LazyMutex::ScopedLock lock(RuntimeGlobalFunctionCountersState.Lock); - RuntimeGlobalFunctionCountersState.State = *state; -@@ -164,18 +164,20 @@ void _swift_setGlobalRuntimeFunctionCounters( - /// Return the names of the runtime functions being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeObjectState structure. All these strings are null terminated. --const char **_swift_getRuntimeFunctionNames() { -+SWIFT_CC(swift) const char **_swift_getRuntimeFunctionNames() { - return RuntimeFunctionNames; - } - - /// Return the offsets of the runtime function counters being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeObjectState structure. -+SWIFT_CC(swift) - const std::uint16_t *_swift_getRuntimeFunctionCountersOffsets() { - return RuntimeFunctionCountersOffsets; - } - - /// Return the number of runtime functions being tracked. -+SWIFT_CC(swift) - std::uint64_t _swift_getNumRuntimeFunctionCounters() { - return ID_LastRuntimeFunctionName; - } -@@ -204,7 +206,7 @@ void _swift_dumpObjectsRuntimeFunctionPointers() { - - /// Set mode for global runtime function counters. - /// Return the old value of this flag. --int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { -+SWIFT_CC(swift) int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { - int oldMode = UpdateGlobalRuntimeFunctionCounters; - UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0; - return oldMode; -@@ -212,7 +214,7 @@ int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { - - /// Set mode for per object runtime function counters. - /// Return the old value of this flag. --int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { -+SWIFT_CC(swift) int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { - int oldMode = UpdatePerObjectRuntimeFunctionCounters; - UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0; - return oldMode; -diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.h b/stdlib/public/runtime/RuntimeInvocationsTracking.h -index 74d60917656..3005df2b79c 100644 ---- a/stdlib/public/runtime/RuntimeInvocationsTracking.h -+++ b/stdlib/public/runtime/RuntimeInvocationsTracking.h -@@ -61,47 +61,47 @@ using RuntimeFunctionCountersUpdateHandler = - - /// Get the runtime object state associated with an object and store it - /// into the result. --SWIFT_RUNTIME_EXPORT void -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void - _swift_getObjectRuntimeFunctionCounters(HeapObject *object, - RuntimeFunctionCountersState *result); - - /// Get the global runtime state containing the total numbers of invocations for - /// each runtime function of interest and store it into the result. --SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( - swift::RuntimeFunctionCountersState *result); - - /// Return the names of the runtime functions being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeObjectState structure. --SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); - - /// Return the offsets of the runtime function counters being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeFunctionCountersState structure. --SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); - - /// Return the number of runtime functions being tracked. --SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); - - /// Dump all per-object runtime function pointers. - SWIFT_RUNTIME_EXPORT void _swift_dumpObjectsRuntimeFunctionPointers(); - - /// Set mode for global runtime function counters. - /// Return the old value of this flag. --SWIFT_RUNTIME_EXPORT int -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int - _swift_setPerObjectRuntimeFunctionCountersMode(int mode); - - /// Set mode for per object runtime function counters. - /// Return the old value of this flag. --SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); - - /// Set the global runtime state of function pointers from a provided state. --SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( - swift::RuntimeFunctionCountersState *state); - - /// Set the runtime object state associated with an object from a provided - /// state. --SWIFT_RUNTIME_EXPORT void -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void - _swift_setObjectRuntimeFunctionCounters(HeapObject *object, - RuntimeFunctionCountersState *state); - --- -2.40.0 - diff --git a/schemes/main/swift/0012-wasm-stdlib-HACK-Workaround-for-the-bad-stacksave-co.patch b/schemes/main/swift/0012-wasm-stdlib-HACK-Workaround-for-the-bad-stacksave-co.patch deleted file mode 100644 index 2f232284..00000000 --- a/schemes/main/swift/0012-wasm-stdlib-HACK-Workaround-for-the-bad-stacksave-co.patch +++ /dev/null @@ -1,32 +0,0 @@ -From 23bcb6fe2c629e82d9fc4f66b25fe99fc8851207 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Wed, 19 Apr 2023 13:14:46 +0000 -Subject: [PATCH] [wasm][stdlib] HACK: Workaround for the bad stacksave codegen - in LLVM - -The LLVM backend generates bad code for some code using `llvm.stacksave` -intrinsics. This patch works around the issue by avoiding such code -generation. See https://github.com/llvm/llvm-project/issues/62235 ---- - stdlib/public/core/ValidUTF8Buffer.swift | 5 ++++- - 1 file changed, 4 insertions(+), 1 deletion(-) - -diff --git a/stdlib/public/core/ValidUTF8Buffer.swift b/stdlib/public/core/ValidUTF8Buffer.swift -index 38799a2a86a..7dd7ca38bee 100644 ---- a/stdlib/public/core/ValidUTF8Buffer.swift -+++ b/stdlib/public/core/ValidUTF8Buffer.swift -@@ -127,7 +127,10 @@ extension _ValidUTF8Buffer: RandomAccessCollection { - @inlinable - @inline(__always) - public func distance(from i: Index, to j: Index) -> Int { -- _debugPrecondition(_isValid(i)) -+ // FIXME(katei): The precondition check is commented out because it -+ // causes bad codegen for `stacksave` on WebAssembly. -+ // See https://github.com/llvm/llvm-project/issues/62235 -+ // _debugPrecondition(_isValid(i)) - _debugPrecondition(_isValid(j)) - return ( - i._biasedBits.leadingZeroBitCount - j._biasedBits.leadingZeroBitCount --- -2.40.0 - diff --git a/schemes/main/swift/0013-wasm-test-HACK-Disable-os_stdlib-tests-due-to-no-dyn.patch b/schemes/main/swift/0013-wasm-test-HACK-Disable-os_stdlib-tests-due-to-no-dyn.patch deleted file mode 100644 index 16a9d5c0..00000000 --- a/schemes/main/swift/0013-wasm-test-HACK-Disable-os_stdlib-tests-due-to-no-dyn.patch +++ /dev/null @@ -1,26 +0,0 @@ -From be417141009e42c3191a715465c4f60d48a18860 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 09:27:38 +0000 -Subject: [PATCH] [wasm][test] HACK: Disable os_stdlib tests due to no dynamic - loading - ---- - test/lit.cfg | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/test/lit.cfg b/test/lit.cfg -index acc64536990..90a25334f7d 100644 ---- a/test/lit.cfg -+++ b/test/lit.cfg -@@ -2281,7 +2281,7 @@ if run_vendor != 'apple': - - if 'remote_run_host' in lit_config.params: - configure_remote_run() --elif not kIsWindows: -+elif not kIsWindows and not run_os == 'wasi': - if 'use_os_stdlib' in lit_config.params: - config.available_features.add('use_os_stdlib') - --- -2.40.0 - diff --git a/schemes/main/swift/0014-wasm-test-HACK-Disable-PrintStruct.swift-test-for-un.patch b/schemes/main/swift/0014-wasm-test-HACK-Disable-PrintStruct.swift-test-for-un.patch deleted file mode 100644 index 3ad9a93e..00000000 --- a/schemes/main/swift/0014-wasm-test-HACK-Disable-PrintStruct.swift-test-for-un.patch +++ /dev/null @@ -1,29 +0,0 @@ -From 838b7c756c3d2e1329528cc9b6fc4a8be2effb4e Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 09:40:00 +0000 -Subject: [PATCH] [wasm][test] HACK: Disable PrintStruct.swift test for unclear - reason - ---- - test/stdlib/PrintStruct.swift | 5 ++++- - 1 file changed, 4 insertions(+), 1 deletion(-) - -diff --git a/test/stdlib/PrintStruct.swift b/test/stdlib/PrintStruct.swift -index 9ecac235539..b616e61f517 100644 ---- a/test/stdlib/PrintStruct.swift -+++ b/test/stdlib/PrintStruct.swift -@@ -4,7 +4,10 @@ - // RUN: %target-codesign %t/main - // RUN: %target-run %t/main - // REQUIRES: executable_test --// REQUIRES: reflection -+// -+// FIXME Disable this case because this failed on only CI. -+// Failed with 'Caught exception of type "CallIndirectOOB"' -+// UNSUPPORTED: CPU=wasm32 - - import StdlibUnittest - import PrintTestTypes --- -2.40.0 - diff --git a/schemes/main/swift/0015-wasm-stdlib-HACK-Add-pthread-stub-to-avoid-linking-e.patch b/schemes/main/swift/0015-wasm-stdlib-HACK-Add-pthread-stub-to-avoid-linking-e.patch deleted file mode 100644 index 302bb663..00000000 --- a/schemes/main/swift/0015-wasm-stdlib-HACK-Add-pthread-stub-to-avoid-linking-e.patch +++ /dev/null @@ -1,215 +0,0 @@ -From da933f595b3699de354421242bb3c15671786299 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 09:45:10 +0000 -Subject: [PATCH] [wasm][stdlib] HACK: Add pthread stub to avoid linking error - -This is a hack to avoid missing pthread symbols error when linking. But -we now have `SWIFT_STDLIB_SINGLE_THREADED_RUNTIME` mode so the hack should -not be necessary anymore. ---- - stdlib/public/CMakeLists.txt | 4 + - .../Resources/wasi/static-executable-args.lnk | 1 + - stdlib/public/WASI/CMakeLists.txt | 4 + - stdlib/public/WASI/Pthread.cpp | 153 ++++++++++++++++++ - 4 files changed, 162 insertions(+) - create mode 100644 stdlib/public/WASI/CMakeLists.txt - create mode 100644 stdlib/public/WASI/Pthread.cpp - -diff --git a/stdlib/public/CMakeLists.txt b/stdlib/public/CMakeLists.txt -index a080c874711..6c6883a6475 100644 ---- a/stdlib/public/CMakeLists.txt -+++ b/stdlib/public/CMakeLists.txt -@@ -142,6 +142,10 @@ if(SWIFT_BUILD_STDLIB) - add_subdirectory(core) - add_subdirectory(SwiftOnoneSupport) - -+ if(WASI IN_LIST SWIFT_SDKS) -+ add_subdirectory(WASI) -+ endif() -+ - if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING) - add_subdirectory(Differentiation) - endif() -diff --git a/stdlib/public/Resources/wasi/static-executable-args.lnk b/stdlib/public/Resources/wasi/static-executable-args.lnk -index 1500f94f891..a18d5fc3668 100644 ---- a/stdlib/public/Resources/wasi/static-executable-args.lnk -+++ b/stdlib/public/Resources/wasi/static-executable-args.lnk -@@ -1,5 +1,6 @@ - -static - -lswiftSwiftOnoneSupport -+-lswiftWasiPthread - -ldl - -lstdc++ - -lm -diff --git a/stdlib/public/WASI/CMakeLists.txt b/stdlib/public/WASI/CMakeLists.txt -new file mode 100644 -index 00000000000..0d088443ad7 ---- /dev/null -+++ b/stdlib/public/WASI/CMakeLists.txt -@@ -0,0 +1,4 @@ -+add_swift_target_library(swiftWasiPthread STATIC IS_STDLIB -+ Pthread.cpp -+ TARGET_SDKS WASI -+ INSTALL_IN_COMPONENT stdlib) -diff --git a/stdlib/public/WASI/Pthread.cpp b/stdlib/public/WASI/Pthread.cpp -new file mode 100644 -index 00000000000..5a64d9bdb4c ---- /dev/null -+++ b/stdlib/public/WASI/Pthread.cpp -@@ -0,0 +1,153 @@ -+// SPDX-License-Identifier: 0BSD -+// prototypes taken from opengroup -+#include -+#include -+#include -+#include -+ -+#define STUB() do {fprintf(stderr, "FakePthread: unsupported %s\n", __func__);abort();}while(0) -+ -+// mutexes: just no-ops -+ -+int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { -+ return 0; -+} -+ -+int pthread_mutex_destroy(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+int pthread_mutexattr_init(pthread_mutexattr_t *attr) { -+ return 0; -+} -+ -+int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { -+ return 0; -+} -+ -+int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) { -+ return 0; -+} -+ -+int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { -+ return 0; -+} -+ -+int pthread_mutex_lock(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+int pthread_mutex_trylock(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+int pthread_mutex_unlock(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+// pthread_cond: STUB -+ -+int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { -+ return 0; -+} -+ -+int pthread_cond_destroy(pthread_cond_t *cond) { -+ return 0; -+} -+ -+int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { -+ STUB(); -+} -+ -+int pthread_cond_timedwait(pthread_cond_t *cond, -+ pthread_mutex_t *mutex, const struct timespec *abstime) { -+ STUB(); -+} -+ -+int pthread_cond_broadcast(pthread_cond_t *cond) { -+ return 0; -+} -+ -+int pthread_cond_signal(pthread_cond_t *cond) { -+ return 0; -+} -+ -+// tls -+ -+int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)) { -+ STUB(); -+} -+ -+void *pthread_getspecific(pthread_key_t key) { -+ STUB(); -+} -+ -+int pthread_setspecific(pthread_key_t key, const void *value) { -+ STUB(); -+} -+ -+// threads -+ -+pthread_t pthread_self() { -+ return (pthread_t)1234; -+} -+ -+#undef pthread_equal -+ -+int pthread_equal(pthread_t t1, pthread_t t2) { -+ return t1 == t2; -+} -+ -+int pthread_join(pthread_t thread, void **value_ptr) { -+ STUB(); -+} -+ -+int pthread_detach(pthread_t thread) { -+ STUB(); -+} -+ -+int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { -+ return 0; -+} -+ -+// once -+ -+int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { -+ STUB(); -+} -+ -+// rwlock -+ -+int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { -+ return 0; -+} -+ -+int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+// named semaphores -+ -+sem_t *sem_open(const char *name, int oflag, ...) { -+ STUB(); -+} --- -2.40.0 - diff --git a/schemes/main/swift/0016-wasm-build-HACK-Add-SwiftWasm-compiler-vendor-suppor.patch b/schemes/main/swift/0016-wasm-build-HACK-Add-SwiftWasm-compiler-vendor-suppor.patch deleted file mode 100644 index 5b7e373b..00000000 --- a/schemes/main/swift/0016-wasm-build-HACK-Add-SwiftWasm-compiler-vendor-suppor.patch +++ /dev/null @@ -1,78 +0,0 @@ -From 9fda3abde7289e2a5dd3801443ac90c0f8d95aa6 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 09:51:29 +0000 -Subject: [PATCH] [wasm][build] HACK: Add SwiftWasm compiler vendor support - -This patch should not be upstreamed. Maybe we can use -`--compiler-vendor=none` to avoid hardcoding specific downstream names. ---- - .../build_swift/build_swift/driver_arguments.py | 2 +- - .../swift_build_support/products/llvm.py | 7 +++++++ - .../swift_build_support/products/swift.py | 16 ++++++++++++---- - 3 files changed, 20 insertions(+), 5 deletions(-) - -diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py -index 28bc9a9c3b5..e0546d03625 100644 ---- a/utils/build_swift/build_swift/driver_arguments.py -+++ b/utils/build_swift/build_swift/driver_arguments.py -@@ -459,7 +459,7 @@ def create_argument_parser(): - help='if set, provide a fixed path for the Swift backtracer') - - option('--compiler-vendor', store, -- choices=['none', 'apple'], -+ choices=['none', 'apple', 'swiftwasm'], - default=defaults.COMPILER_VENDOR, - help='Compiler vendor name') - option('--clang-compiler-version', store, -diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py -index c0011248316..f38040ae21a 100644 ---- a/utils/swift_build_support/swift_build_support/products/llvm.py -+++ b/utils/swift_build_support/swift_build_support/products/llvm.py -@@ -64,6 +64,13 @@ class LLVM(cmake_product.CMakeProduct): - if self.args.compiler_vendor == "none": - return [] - -+ if self.args.compiler_vendor == "swiftwasm": -+ return [ -+ ('CLANG_VENDOR', 'SwiftWasm'), -+ ('CLANG_VENDOR_UTI', 'org.swiftwasm.compilers.llvm.clang'), -+ ('PACKAGE_VERSION', str(self.args.clang_user_visible_version)) -+ ] -+ - if self.args.compiler_vendor != "apple": - raise RuntimeError("Unknown compiler vendor?!") - -diff --git a/utils/swift_build_support/swift_build_support/products/swift.py b/utils/swift_build_support/swift_build_support/products/swift.py -index de6843b06a1..2e8d1caf41f 100644 ---- a/utils/swift_build_support/swift_build_support/products/swift.py -+++ b/utils/swift_build_support/swift_build_support/products/swift.py -@@ -104,14 +104,22 @@ class Swift(product.Product): - if self.args.compiler_vendor == "none": - return [] - -- if self.args.compiler_vendor != "apple": -- raise RuntimeError("Unknown compiler vendor?! Was build-script \ --updated without updating swift.py?") -- - swift_compiler_version = "" - if self.args.swift_compiler_version is not None: - swift_compiler_version = self.args.swift_compiler_version - -+ if self.args.compiler_vendor == "swiftwasm": -+ return [ -+ ('SWIFT_VENDOR', 'SwiftWasm'), -+ ('SWIFT_VENDOR_UTI', 'org.swiftwasm.compilers.llvm.swift'), -+ ('SWIFT_VERSION', str(self.args.swift_user_visible_version)), -+ ('SWIFT_COMPILER_VERSION', str(swift_compiler_version)), -+ ] -+ -+ if self.args.compiler_vendor != "apple": -+ raise RuntimeError("Unknown compiler vendor?! Was build-script \ -+updated without updating swift.py?") -+ - return [ - ('SWIFT_VENDOR', 'Apple'), - ('SWIFT_VENDOR_UTI', 'com.apple.compilers.llvm.swift'), --- -2.40.0 - diff --git a/schemes/main/swift/0017-installer-Do-not-follow-an-existing-symbolic-link-wh.patch b/schemes/main/swift/0017-installer-Do-not-follow-an-existing-symbolic-link-wh.patch deleted file mode 100644 index c90aece3..00000000 --- a/schemes/main/swift/0017-installer-Do-not-follow-an-existing-symbolic-link-wh.patch +++ /dev/null @@ -1,41 +0,0 @@ -From 6b97b2d581ab626fa95faab5b67ec204a46ea4cb Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 24 Nov 2020 13:16:58 +0900 -Subject: [PATCH] [installer] Do not follow an existing symbolic link when - overwriting -MIME-Version: 1.0 -Content-Type: text/plain; charset=UTF-8 -Content-Transfer-Encoding: 8bit - -Without `-h` option, `ln` command makes a symbolic link under the existing -link destination like `Toolchains/swift-DEVELOPMENT-SNAPSHOT-XX.xctoolchain/`. - -``` -$ ln -fs swift-DEVELOPMENT-SNAPSHOT-XX.xctoolchain swift-latest.xctoolchain -$ ln -fs swift-DEVELOPMENT-SNAPSHOT-YY.xctoolchain swift-latest.xctoolchain -$ tree -|-- swift-DEVELOPMENT-SNAPSHOT-XX.xctoolchain -|   |-- swift-DEVELOPMENT-SNAPSHOT-YY.xctoolchain -> swift-DEVELOPMENT-SNAPSHOT-YY.xctoolchain -|-- swift-DEVELOPMENT-SNAPSHOT-YY.xctoolchain -|-- swift-latest.xctoolchain -> swift-DEVELOPMENT-SNAPSHOT-XX.xctoolchain -``` - -So we should not follow the existing symbolic link even if it already -exists. ---- - utils/darwin-installer-scripts/postinstall | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/utils/darwin-installer-scripts/postinstall b/utils/darwin-installer-scripts/postinstall -index 8f00806645e..e9750f04e95 100755 ---- a/utils/darwin-installer-scripts/postinstall -+++ b/utils/darwin-installer-scripts/postinstall -@@ -13,4 +13,4 @@ - - INSTALLED_TOOLCHAIN=$2 - --ln -fs "${INSTALLED_TOOLCHAIN}" "${INSTALLED_TOOLCHAIN%/*}/swift-latest.xctoolchain" -+ln -fhs "${INSTALLED_TOOLCHAIN}" "${INSTALLED_TOOLCHAIN%/*}/swift-latest.xctoolchain" --- -2.40.0 - diff --git a/schemes/main/wasi-sysroot/disable_part_modules_for_cxx_interop.patch b/schemes/main/wasi-sysroot/disable_part_modules_for_cxx_interop.patch deleted file mode 100644 index 4995a603..00000000 --- a/schemes/main/wasi-sysroot/disable_part_modules_for_cxx_interop.patch +++ /dev/null @@ -1,75 +0,0 @@ -Author: Yuta Saito -Date: Thu Dec 29 10:50:33 2022 +0000 - - Without this patch, Cxx.swiftmodule build fails on macOS host environments - -diff --git a/include/c++/v1/module.modulemap b/include/c++/v1/module.modulemap -index b5781b0..af10440 100644 ---- a/include/c++/v1/module.modulemap -+++ b/include/c++/v1/module.modulemap -@@ -44,10 +44,12 @@ module std [system] { - header "math.h" - export * - } -+ /* XXX(swiftwasm): setjmp.h is unavailable in wasi-libc - module setjmp_h { - header "setjmp.h" - export * - } -+ */ - // FIXME: is missing. - // provided by C library. - // provided by compiler. -@@ -153,10 +155,12 @@ module std [system] { - header "cmath" - export * - } -+ /* XXX(swiftwasm): setjmp.h is unavailable in wasi-libc - module csetjmp { - header "csetjmp" - export * - } -+ */ - module csignal { - header "csignal" - export * -@@ -384,10 +388,12 @@ module std [system] { - header "execution" - export * - } -+ /* XXX(swiftwasm): wasi-sdk configures with LIBCXX_ENABLE_FILESYSTEM=OFF - module filesystem { - header "filesystem" - export * - } -+ */ - module format { - header "format" - export * -@@ -645,11 +651,13 @@ module std [system] { - header "scoped_allocator" - export * - } -+ /* XXX(swiftwasm): semaphore is unavailable due to lack of thread - module semaphore { - requires cplusplus14 - header "semaphore" - export * - } -+ */ - module set { - header "set" - export initializer_list -@@ -816,10 +824,12 @@ module std [system] { - header "experimental/deque" - export * - } -+ /* XXX(swiftwasm): wasi-sdk configures with LIBCXX_ENABLE_FILESYSTEM=OFF - module filesystem { - header "experimental/filesystem" - export * - } -+ */ - module forward_list { - header "experimental/forward_list" - export * diff --git a/schemes/release-5.8/manifest.json b/schemes/release-5.8/manifest.json deleted file mode 100644 index cb0f607d..00000000 --- a/schemes/release-5.8/manifest.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "update-checkout-scheme": "release/5.8", - "base-tag": "swift-5.8.1-RELEASE", - "repos": { - "swift-corelibs-foundation": "660624d0220c0efbef054f4919f9f01fd3866be1", - "swift-corelibs-xctest": "77bc9f5386ee8a2a4e8da5ac30e846b451d101b6" - }, - "swift-org-download-channel": "swift-5.8.1-release" -} diff --git a/schemes/release-5.8/swift/all.patch b/schemes/release-5.8/swift/all.patch deleted file mode 100644 index 33699450..00000000 --- a/schemes/release-5.8/swift/all.patch +++ /dev/null @@ -1,4848 +0,0 @@ -From 24181c4eaeb2c27f9d61c5fa6954d0cac1458c98 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 20 Jun 2023 09:06:08 +0000 -Subject: [PATCH] [wasm] All SwiftWasm patches - -Co-authored-by: Max Desiatov -Co-authored-by: Zhuowei Zhang ---- - .github/FUNDING.yml | 12 + - .github/PULL_REQUEST_TEMPLATE.md | 17 -- - .github/pull.yml | 52 ++++ - .github/scripts/build-matrix.rb | 112 ++++++++ - .github/workflows/build-toolchain.yml | 256 +++++++++++++++++ - .github/workflows/manual-distribution.yml | 82 ++++++ - .github/workflows/nightly-distribution.yml | 64 +++++ - CMakeLists.txt | 5 + - README.md | 20 ++ - cmake/caches/Runtime-WASI-wasm32.cmake | 9 + - cmake/modules/AddSwift.cmake | 2 + - include/swift/IRGen/Linking.h | 9 + - include/swift/Runtime/HeapObject.h | 3 + - lib/ClangImporter/ClangImporter.cpp | 47 ++++ - lib/ClangImporter/ImportType.cpp | 2 +- - lib/ClangImporter/MappedTypes.def | 1 + - lib/Driver/CMakeLists.txt | 1 + - lib/Driver/Driver.cpp | 14 +- - lib/Driver/ToolChains.cpp | 22 +- - lib/Driver/WebAssemblyToolChains.cpp | 4 + - lib/DriverTool/autolink_extract_main.cpp | 30 -- - lib/FrontendTool/TBD.cpp | 9 + - lib/IRGen/GenDecl.cpp | 9 +- - lib/IRGen/GenKeyPath.cpp | 10 +- - lib/IRGen/GenMeta.cpp | 2 +- - lib/IRGen/IRGen.cpp | 4 +- - lib/IRGen/IRGenFunction.cpp | 1 + - lib/IRGen/MetadataRequest.cpp | 6 +- - lib/SIL/Verifier/SILVerifier.cpp | 10 +- - lib/SILGen/SILGenBuiltin.cpp | 29 +- - lib/SILGen/SILGenExpr.cpp | 39 ++- - .../SILCombiner/SILCombinerApplyVisitors.cpp | 14 + - lib/SILOptimizer/Utils/KeyPathProjector.cpp | 17 +- - stdlib/cmake/modules/AddSwiftStdlib.cmake | 8 +- - stdlib/cmake/modules/SwiftSource.cmake | 8 + - stdlib/private/CMakeLists.txt | 9 +- - stdlib/private/StdlibUnittest/CMakeLists.txt | 25 +- - .../private/StdlibUnittest/InterceptTraps.cpp | 6 +- - stdlib/private/StdlibUnittest/RaceTest.swift | 2 + - .../StdlibUnittest/StdlibUnittest.swift | 20 +- - stdlib/private/SwiftPrivate/CMakeLists.txt | 1 + - stdlib/private/SwiftPrivate/IO.swift | 10 +- - .../SwiftPrivateLibcExtras/CMakeLists.txt | 1 + - .../SwiftPrivateThreadExtras/CMakeLists.txt | 1 + - .../SwiftPrivateThreadExtras.swift | 2 +- - stdlib/public/CMakeLists.txt | 4 + - stdlib/public/Concurrency/AsyncStream.cpp | 6 +- - stdlib/public/Concurrency/TaskGroup.cpp | 4 + - stdlib/public/Differentiation/CMakeLists.txt | 1 + - stdlib/public/Platform/CMakeLists.txt | 85 ++++++ - stdlib/public/Platform/SwiftWASILibc.h.gyb | 112 ++++++++ - stdlib/public/Platform/WASI.swift.gyb | 98 +++++++ - stdlib/public/Platform/wasi.modulemap.gyb | 26 ++ - .../SwiftShims/swift/shims/HeapObject.h | 8 + - stdlib/public/WASI/CMakeLists.txt | 4 + - stdlib/public/WASI/Pthread.cpp | 153 +++++++++++ - stdlib/public/runtime/CMakeLists.txt | 124 ++++++++- - stdlib/public/runtime/Demangle.cpp | 1 + - .../public/runtime/EnvironmentVariables.cpp | 1 + - stdlib/public/runtime/Exclusivity.cpp | 4 +- - stdlib/public/runtime/Heap.cpp | 5 +- - stdlib/public/runtime/HeapObject.cpp | 7 + - stdlib/public/runtime/Numeric.cpp | 2 + - .../runtime/RuntimeInvocationsTracking.cpp | 16 +- - .../runtime/RuntimeInvocationsTracking.h | 18 +- - stdlib/public/runtime/SwiftRT-WASM.cpp | 79 ++++++ - stdlib/public/stubs/Random.cpp | 1 + - .../BytecodeLayouts.cpp | 10 +- - .../BytecodeLayouts.h | 3 + - test/AutolinkExtract/empty.swift | 1 + - test/AutolinkExtract/empty_archive.swift | 1 + - test/AutolinkExtract/import.swift | 3 + - test/AutolinkExtract/import_archive.swift | 3 + - test/CMakeLists.txt | 13 +- - .../Runtime/checked_continuation.swift | 4 +- - .../Runtime/continuation_validation.swift | 1 + - test/DebugInfo/modulecache.swift | 2 +- - test/Driver/profiling.swift | 1 + - test/lit.cfg | 43 ++- - test/stdlib/Error.swift | 2 + - test/stdlib/FloatingPointIR.swift | 3 + - test/stdlib/PrintStruct.swift | 5 +- - test/stdlib/StringAPI.swift | 2 + - test/stdlib/StringAPICString.swift | 17 ++ - test/stdlib/TemporaryAllocation.swift | 4 + - test/stdlib/UnsafePointer.swift.gyb | 2 + - test/stdlib/UnsafeRawPointer.swift | 4 + - utils/build-script | 1 - - utils/build-script-impl | 21 +- - .../build_swift/driver_arguments.py | 2 +- - utils/darwin-installer-scripts/postinstall | 2 +- - .../host_specific_configuration.py | 6 + - .../swift_build_support/products/llvm.py | 7 + - .../swift_build_support/products/swift.py | 16 +- - .../update-checkout-config.json | 96 ++++++- - utils/webassembly/build-foundation.sh | 33 +++ - utils/webassembly/build-presets.ini | 72 +++++ - utils/webassembly/build-toolchain.sh | 258 ++++++++++++++++++ - utils/webassembly/build-xctest.sh | 25 ++ - utils/webassembly/ci.sh | 15 + - utils/webassembly/compiler-rt-cache.cmake | 13 + - .../distribute-latest-toolchain.sh | 235 ++++++++++++++++ - utils/webassembly/install-build-sdk.sh | 47 ++++ - ...disable_part_modules_for_cxx_interop.patch | 75 +++++ - utils/webassembly/static-executable-args.lnk | 12 + - utils/webassembly/toolchain-wasi.cmake | 26 ++ - utils/webassembly/wasm-run.py | 54 ++++ - 107 files changed, 2751 insertions(+), 155 deletions(-) - create mode 100644 .github/FUNDING.yml - delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md - create mode 100644 .github/pull.yml - create mode 100644 .github/scripts/build-matrix.rb - create mode 100644 .github/workflows/build-toolchain.yml - create mode 100644 .github/workflows/manual-distribution.yml - create mode 100644 .github/workflows/nightly-distribution.yml - create mode 100644 stdlib/public/Platform/SwiftWASILibc.h.gyb - create mode 100644 stdlib/public/Platform/WASI.swift.gyb - create mode 100644 stdlib/public/Platform/wasi.modulemap.gyb - create mode 100644 stdlib/public/WASI/CMakeLists.txt - create mode 100644 stdlib/public/WASI/Pthread.cpp - create mode 100644 stdlib/public/runtime/SwiftRT-WASM.cpp - create mode 100755 utils/webassembly/build-foundation.sh - create mode 100644 utils/webassembly/build-presets.ini - create mode 100755 utils/webassembly/build-toolchain.sh - create mode 100755 utils/webassembly/build-xctest.sh - create mode 100755 utils/webassembly/ci.sh - create mode 100644 utils/webassembly/compiler-rt-cache.cmake - create mode 100755 utils/webassembly/distribute-latest-toolchain.sh - create mode 100755 utils/webassembly/install-build-sdk.sh - create mode 100644 utils/webassembly/patches/wasi-sysroot/disable_part_modules_for_cxx_interop.patch - create mode 100644 utils/webassembly/static-executable-args.lnk - create mode 100644 utils/webassembly/toolchain-wasi.cmake - create mode 100755 utils/webassembly/wasm-run.py - -diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml -new file mode 100644 -index 00000000000..3afb2b0bea4 ---- /dev/null -+++ b/.github/FUNDING.yml -@@ -0,0 +1,12 @@ -+# These are supported funding model platforms -+ -+github: [swiftwasm, kateinoigakukun, MaxDesiatov] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] -+patreon: # Replace with a single Patreon username -+open_collective: # Replace with a single Open Collective username -+ko_fi: # Replace with a single Ko-fi username -+tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -+community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -+liberapay: # Replace with a single Liberapay username -+issuehunt: # Replace with a single IssueHunt username -+otechie: # Replace with a single Otechie username -+custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] -diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md -deleted file mode 100644 -index d5084b4b821..00000000000 ---- a/.github/PULL_REQUEST_TEMPLATE.md -+++ /dev/null -@@ -1,17 +0,0 @@ -- --Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate. -- -- --Resolves #NNNNN, fix apple/llvm-project#MMMMM. -- -- -diff --git a/.github/pull.yml b/.github/pull.yml -new file mode 100644 -index 00000000000..0f80214603a ---- /dev/null -+++ b/.github/pull.yml -@@ -0,0 +1,52 @@ -+version: "1" -+rules: -+ - base: swiftwasm -+ upstream: main -+ mergeMethod: merge -+ - base: main -+ upstream: apple:main -+ mergeMethod: hardreset -+ -+ - base: release/5.3 -+ upstream: apple:release/5.3 -+ mergeMethod: hardreset -+ - base: swiftwasm-release/5.3 -+ upstream: release/5.3 -+ mergeMethod: merge -+ -+ - base: release/5.4 -+ upstream: apple:release/5.4 -+ mergeMethod: hardreset -+ - base: swiftwasm-release/5.4 -+ upstream: release/5.4 -+ mergeMethod: merge -+ -+ - base: release/5.5 -+ upstream: apple:release/5.5 -+ mergeMethod: hardreset -+ - base: swiftwasm-release/5.5 -+ upstream: release/5.5 -+ mergeMethod: merge -+ -+ - base: release/5.6 -+ upstream: apple:release/5.6 -+ mergeMethod: hardreset -+ - base: swiftwasm-release/5.6 -+ upstream: release/5.6 -+ mergeMethod: merge -+ -+ - base: release/5.7 -+ upstream: apple:release/5.7 -+ mergeMethod: hardreset -+ - base: swiftwasm-release/5.7 -+ upstream: release/5.7 -+ mergeMethod: merge -+ -+ - base: release/5.8 -+ upstream: apple:release/5.8 -+ mergeMethod: hardreset -+ - base: swiftwasm-release/5.8 -+ upstream: release/5.8 -+ mergeMethod: merge -+ -+label: ":arrow_heading_down: Upstream Tracking" -diff --git a/.github/scripts/build-matrix.rb b/.github/scripts/build-matrix.rb -new file mode 100644 -index 00000000000..93667018083 ---- /dev/null -+++ b/.github/scripts/build-matrix.rb -@@ -0,0 +1,112 @@ -+require "json" -+require "optparse" -+ -+BASE_MATRIX_ENTRIES = [ -+ { -+ "build_os": "ubuntu-18.04", -+ "agent_query": "ubuntu-20.04", -+ "target": "ubuntu18.04_x86_64", -+# "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-18.04", -+ "container": "ghcr.io/swiftwasm/swift-ci@sha256:e38db2e47e9b4a9207276fd3061748a719901884a6fa44562abb47d196534864", -+ "run_stdlib_test": true, -+ "run_full_test": false, -+ "run_e2e_test": true, -+ "build_hello_wasm": true, -+ "clean_build_dir": false, -+ "free_disk_space": true -+ }, -+ { -+ "build_os": "ubuntu-20.04", -+ "agent_query": "ubuntu-20.04", -+ "target": "ubuntu20.04_x86_64", -+# "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", -+ "container": "ghcr.io/swiftwasm/swift-ci@sha256:5e135cc954d23467cbd3bc54583d1bd870d7e268b339ef8638f9a38b74cb8503", -+ "run_stdlib_test": true, -+ "run_full_test": false, -+ "run_e2e_test": true, -+ "build_hello_wasm": true, -+ "clean_build_dir": false, -+ "free_disk_space": true -+ }, -+ { -+ "build_os": "ubuntu-22.04", -+ "agent_query": "ubuntu-22.04", -+ "target": "ubuntu22.04_x86_64", -+# "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-22.04", -+ "container": "ghcr.io/swiftwasm/swift-ci@sha256:8b667f18c8cfc34b9823bfe797fb4a279611f9a61732d5e7f6e5304c8c43326e", -+ "run_stdlib_test": true, -+ "run_full_test": false, -+ "run_e2e_test": true, -+ "build_hello_wasm": true, -+ "clean_build_dir": false, -+ "free_disk_space": true -+ }, -+ { -+ "build_os": "amazon-linux-2", -+ "agent_query": "ubuntu-22.04", -+ "target": "amazonlinux2_x86_64", -+# "container": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2", -+ "container": "ghcr.io/swiftwasm/swift-ci@sha256:4bf1e80ea3757a6cb0c3ebf7ed9423971c5ecb9ce8162a46487a1cb0398b4e69", -+ "run_stdlib_test": false, -+ "run_full_test": false, -+ "run_e2e_test": false, -+ "build_hello_wasm": true, -+ "clean_build_dir": false, -+ "free_disk_space": true -+ }, -+ { -+ "build_os": "macos-11", -+ "agent_query": "macos-11", -+ "target": "macos_x86_64", -+ "run_stdlib_test": false, -+ "run_full_test": false, -+ "run_e2e_test": false, -+ "build_hello_wasm": false, -+ "clean_build_dir": false -+ }, -+ { -+ "build_os": "macos-11", -+ "agent_query": ["self-hosted", "macOS", "ARM64"], -+ "target": "macos_arm64", -+ "run_stdlib_test": false, -+ "run_full_test": false, -+ "run_e2e_test": false, -+ "build_hello_wasm": true, -+ "clean_build_dir": true -+ } -+] -+ -+def main -+ options = {} -+ OptionParser.new do |opts| -+ opts.banner = "Usage: build-matrix.rb [options]" -+ opts.on("--runner [JSON FILE]", "Path to runner data file") do |v| -+ options[:runner] = v -+ end -+ end.parse! -+ -+ matrix_entries = BASE_MATRIX_ENTRIES.dup -+ if options[:runner] -+ runner = JSON.parse(File.read(options[:runner])) -+ if label = runner["outputs"]["ubuntu20_04_aarch64-label"] -+ matrix_entries << { -+ "build_os": "ubuntu-20.04", -+ "agent_query": label, -+ "target": "ubuntu20.04_aarch64", -+# "container": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", -+ "container": "ghcr.io/swiftwasm/swift-ci@sha256:5e135cc954d23467cbd3bc54583d1bd870d7e268b339ef8638f9a38b74cb8503", -+ "run_stdlib_test": false, -+ "run_full_test": false, -+ "run_e2e_test": false, -+ "build_hello_wasm": true, -+ "clean_build_dir": false -+ } -+ end -+ end -+ -+ print JSON.generate(matrix_entries) -+end -+ -+if $0 == __FILE__ -+ main -+end -diff --git a/.github/workflows/build-toolchain.yml b/.github/workflows/build-toolchain.yml -new file mode 100644 -index 00000000000..a10cb287a81 ---- /dev/null -+++ b/.github/workflows/build-toolchain.yml -@@ -0,0 +1,256 @@ -+name: Build Toolchain -+ -+on: -+ push: -+ branches: -+ - swiftwasm -+ - swiftwasm-release/** -+ pull_request: -+ branches: -+ - swiftwasm -+ - swiftwasm-release/** -+ types: [opened, synchronize, reopened] -+jobs: -+ start-runner: -+ name: Start self-hosted EC2 runner -+ runs-on: ubuntu-latest -+ # Run only on main branches to avoid triggers by non-collaborator -+ if: ${{ github.event_name == 'push' || contains(github.event.pull_request.labels.*.name, 'check-self-hosted-ci') }} -+ outputs: -+ ubuntu20_04_aarch64-label: ${{ steps.start-ubuntu20_04_aarch64-runner.outputs.label }} -+ ubuntu20_04_aarch64-ec2-instance-id: ${{ steps.start-ubuntu20_04_aarch64-runner.outputs.ec2-instance-id }} -+ steps: -+ - name: Configure AWS credentials -+ uses: aws-actions/configure-aws-credentials@v1 -+ with: -+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} -+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} -+ aws-region: ${{ secrets.AWS_REGION }} -+ - name: Start EC2 runner (ubuntu20_04_aarch64) -+ id: start-ubuntu20_04_aarch64-runner -+ uses: machulav/ec2-github-runner@v2 -+ with: -+ mode: start -+ github-token: ${{ secrets.SWIFTWASM_BUILDBOT_TOKEN }} -+ ec2-image-id: ami-03803f6b50b56647a # swiftwasm-ci/ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-20221201 -+ ec2-instance-type: c6g.xlarge -+ subnet-id: subnet-327f4a13 -+ security-group-id: sg-0429f5ec2bee9dc0c -+ stop-runner: -+ name: Stop self-hosted EC2 runner -+ needs: [start-runner, build-toolchain] -+ runs-on: ubuntu-latest -+ # Required to stop the runner even if the error happened in the previous jobs -+ if: ${{ always() && needs.start-runner.result == 'success' }} -+ # The runner can be already stopped when re-running a part of failed jobs -+ continue-on-error: true -+ steps: -+ - name: Configure AWS credentials -+ uses: aws-actions/configure-aws-credentials@v1 -+ with: -+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} -+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} -+ aws-region: ${{ secrets.AWS_REGION }} -+ - name: Stop EC2 runner (ubuntu20_04_aarch64) -+ uses: machulav/ec2-github-runner@v2 -+ with: -+ mode: stop -+ github-token: ${{ secrets.SWIFTWASM_BUILDBOT_TOKEN }} -+ label: ${{ needs.start-runner.outputs.ubuntu20_04_aarch64-label }} -+ ec2-instance-id: ${{ needs.start-runner.outputs.ubuntu20_04_aarch64-ec2-instance-id }} -+ -+ build-matrix: -+ name: Build matrix -+ needs: [start-runner] -+ runs-on: ubuntu-latest -+ if: ${{ always() }} -+ outputs: -+ entries: ${{ steps.generate.outputs.entries }} -+ steps: -+ - uses: actions/checkout@v3 -+ - name: Generate entries -+ id: generate -+ run: | -+ START_RUNNER_JSON="${{ runner.temp }}/start-runner.json" -+ cat < "$START_RUNNER_JSON" -+ ${{ toJson(needs.start-runner) }} -+ EOF -+ echo "entries=$(ruby .github/scripts/build-matrix.rb --runner $START_RUNNER_JSON)" >> $GITHUB_OUTPUT -+ -+ build-toolchain: -+ env: -+ TOOLCHAIN_CHANNEL: 5.8 -+ DEVELOPER_DIR: /Applications/Xcode_13.2.1.app/Contents/Developer/ -+ # https://github.com/actions/cache/blob/main/tips-and-workarounds.md#cache-segment-restore-timeout -+ SEGMENT_DOWNLOAD_TIMEOUT_MINS: 30 -+ needs: [build-matrix] -+ if: ${{ always() }} -+ strategy: -+ fail-fast: false -+ matrix: -+ include: ${{ fromJSON(needs.build-matrix.outputs.entries) }} -+ -+ name: Target ${{ matrix.target }} -+ timeout-minutes: 0 -+ runs-on: ${{ matrix.agent_query }} -+ steps: -+ - name: Free disk space -+ if: ${{ matrix.free_disk_space }} -+ run: | -+ df -h -+ sudo rm -rf /opt/hostedtoolcache -+ sudo rm -rf /usr/share/dotnet -+ sudo apt clean -+ -+ if docker info > /dev/null 2>&1; then -+ docker rmi $(docker image ls -aq) -+ fi -+ df -h -+ -+ - uses: actions/checkout@v3 -+ with: -+ path: swift -+ fetch-depth: 0 -+ -+ - name: Prepare sccache timestamp -+ id: cache_key -+ run: | -+ export SCCACHE_KEY_PREFIX="${{ matrix.target }}-${{ env.TOOLCHAIN_CHANNEL }}-sccache-v14-" -+ echo "SCCACHE_KEY_PREFIX=$SCCACHE_KEY_PREFIX" >> $GITHUB_OUTPUT -+ echo "SCCACHE_KEY=$SCCACHE_KEY_PREFIX$(date +'%Y-%m-%d-%I-%M-%S')" >> $GITHUB_OUTPUT -+ -+ - name: Check Xcode version -+ if: ${{ startsWith(matrix.build_os, 'macos-') }} -+ run: | -+ xcodebuild -version -+ -+ - uses: actions/cache/restore@v3 -+ id: build-cache -+ with: -+ path: build-cache -+ key: ${{ steps.cache_key.outputs.SCCACHE_KEY }} -+ restore-keys: | -+ ${{ steps.cache_key.outputs.SCCACHE_KEY_PREFIX }} -+ -+ - name: Clean build directory -+ if: ${{ matrix.clean_build_dir }} -+ run: | -+ rm -rf ${{ github.workspace }}/target-build \ -+ ${{ github.workspace }}/host-build \ -+ ${{ github.workspace }}/build-sdk \ -+ ${{ github.workspace }}/host-toolchain-sdk \ -+ ${{ github.workspace }}/dist-toolchain-sdk -+ -+ - run: chmod -R 777 ./build-cache -+ if: ${{ steps.build-cache.outputs.cache-hit }} -+ - name: Install Homebrew dependencies -+ if: ${{ startsWith(matrix.build_os, 'macos-') }} -+ run: brew bundle --file swift/Brewfile && brew install wasmer -+ - name: Build ${{ matrix.target }} installable archive on Docker container -+ if: ${{ matrix.container != null }} -+ timeout-minutes: 300 # Exit before 6 hours limit to allow cache upload -+ run: | -+ docker volume create oss-swift-package -+ docker run --name swiftwasm-ci-buildbot \ -+ --security-opt=no-new-privileges \ -+ --cap-add=SYS_PTRACE \ -+ --security-opt seccomp=unconfined \ -+ -dit \ -+ -e TOOLCHAIN_CHANNEL=${{ env.TOOLCHAIN_CHANNEL }} \ -+ -w /home/build-user/ \ -+ -v ${{ github.workspace }}:/source \ -+ -v oss-swift-package:/home/build-user \ -+ ${{ matrix.container }} -+ docker exec swiftwasm-ci-buildbot /bin/bash -lc 'env; cp -r /source/* /home/build-user/; ./swift/utils/webassembly/ci.sh' -+ docker cp swiftwasm-ci-buildbot:/home/build-user/swift-wasm-${{ env.TOOLCHAIN_CHANNEL }}-SNAPSHOT-${{ matrix.target }}.tar.gz . -+ -+ - name: Extract build-cache from Docker container -+ if: ${{ matrix.container != null && always() }} -+ run: | -+ rm -rf ${{ github.workspace }}/build-cache -+ docker cp swiftwasm-ci-buildbot:/home/build-user/build-cache ${{ github.workspace }}/build-cache -+ -+ - name: Build ${{ matrix.target }} installable archive -+ if: ${{ matrix.container == null }} -+ timeout-minutes: 300 # Exit before 6 hours limit to allow cache upload -+ env: -+ SKIP_XCODE_VERSION_CHECK: 1 -+ run: | -+ ./swift/utils/webassembly/ci.sh -+ -+ - uses: actions/cache/save@v3 -+ if: ${{ always() }} -+ with: -+ path: build-cache -+ key: ${{ steps.cache_key.outputs.SCCACHE_KEY }} -+ -+ - name: Upload ${{ matrix.target }} installable archive -+ uses: actions/upload-artifact@v3 -+ with: -+ name: ${{ matrix.target }}-installable -+ path: swift-wasm-${{ env.TOOLCHAIN_CHANNEL }}-SNAPSHOT-${{ matrix.target }}.tar.gz -+ -+ - name: Add wasmer in PATH for testing -+ run: echo "$HOME/.wasmer/bin" >> $GITHUB_PATH -+ -+ - name: Run stdlib tests for wasi-wasm32 -+ if: ${{ matrix.run_stdlib_test && matrix.container != null }} -+ run: | -+ docker exec swiftwasm-ci-buildbot /bin/bash -lc "cd target-build/swift-stdlib-wasi-wasm32 && ninja check-swift-wasi-wasm32-custom" -+ - name: Run stdlib tests for wasi-wasm32 -+ if: ${{ matrix.run_stdlib_test && matrix.container == null }} -+ run: | -+ TARGET_STDLIB_BUILD_DIR=${{ github.workspace }}/target-build/swift-stdlib-wasi-wasm32 -+ ninja check-swift-wasi-wasm32-custom -C "$TARGET_STDLIB_BUILD_DIR" -+ -+ - name: Run all tests for wasi-wasm32 -+ if: ${{ matrix.run_full_test }} -+ run: | -+ TARGET_STDLIB_BUILD_DIR=${{ github.workspace }}/target-build/swift-stdlib-wasi-wasm32 -+ # Run all tests but ignore failure temporarily -+ ninja check-swift-wasi-wasm32 -C "$TARGET_STDLIB_BUILD_DIR" || true -+ -+ - name: Pack test results -+ if: ${{ matrix.run_full_test }} -+ run: | -+ tar cJf ./swift-test-results.tar.gz target-build/swift-stdlib-wasi-wasm32/swift-test-results -+ - name: Upload test results -+ uses: actions/upload-artifact@v3 -+ if: ${{ matrix.run_full_test }} -+ with: -+ name: ${{ matrix.target }}-test-results -+ path: ./swift-test-results.tar.gz -+ -+ # Run e2e test -+ - name: Prepare E2E test -+ run: | -+ INSTALL_DIR=$(mktemp -d) -+ tar xf swift-wasm-$TOOLCHAIN_CHANNEL-SNAPSHOT-${{ matrix.target }}.tar.gz -C "$INSTALL_DIR" -+ echo "TOOLCHAIN=$(find "$INSTALL_DIR" -name "swift-wasm-$TOOLCHAIN_CHANNEL-*" -type d | head -n1)" >> $GITHUB_ENV -+ - name: Build hello.wasm -+ shell: bash -+ if: ${{ matrix.build_hello_wasm }} -+ run: | -+ echo 'print("Hello, world!")' > hello.swift -+ $TOOLCHAIN/usr/bin/swiftc \ -+ -target wasm32-unknown-wasi \ -+ -sdk $TOOLCHAIN/usr/share/wasi-sysroot \ -+ hello.swift -o hello.wasm && \ -+ echo "Successfully linked hello.wasm" -+ - name: Upload hello.wasm -+ if: ${{ matrix.build_hello_wasm }} -+ uses: actions/upload-artifact@v3 -+ with: -+ name: ${{ matrix.target }}-hello.wasm -+ path: hello.wasm -+ - name: Checkout integration-tests -+ if: ${{ matrix.run_e2e_test }} -+ uses: actions/checkout@v3 -+ with: -+ repository: swiftwasm/integration-tests -+ path: integration-tests -+ - name: Run integration tests -+ if: ${{ matrix.run_e2e_test }} -+ run: | -+ $TOOLCHAIN/usr/bin/swift run # Use TOOLCHAIN env value -+ working-directory: ${{ github.workspace }}/integration-tests -diff --git a/.github/workflows/manual-distribution.yml b/.github/workflows/manual-distribution.yml -new file mode 100644 -index 00000000000..52e39483a4d ---- /dev/null -+++ b/.github/workflows/manual-distribution.yml -@@ -0,0 +1,82 @@ -+name: Manual distribution -+on: -+ workflow_dispatch: -+ inputs: -+ channel: -+ description: 'Release channel' -+ required: true -+ toolchain_name: -+ description: 'Toolchain name' -+ required: true -+ branch: -+ description: 'Branch name used to fetch built artifacts. If empty, use default branch based on channel' -+ display_name: -+ description: 'Display name for Darwin toolchain' -+ display_name_short: -+ description: 'Short display name for Darwin toolchain' -+jobs: -+ manual-distribution: -+ runs-on: macos-latest -+ steps: -+ - uses: actions/checkout@v2 -+ - name: Setup keychain -+ run: | -+ security create-keychain -p "$KEYCHAIN_PASSWORD" swiftwasm-ci -+ KEYCHAIN=$HOME/Library/Keychains/swiftwasm-ci-db -+ security unlock-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN -+ security set-keychain-settings -lut 7200 $KEYCHAIN -+ security default-keychain -s $KEYCHAIN -+ echo "$DARWIN_TOOLCHAIN_APPLICATION_CERT_BASE64" | base64 --decode -o developerID_application.p12 -+ security import developerID_application.p12 -P "$DARWIN_TOOLCHAIN_APPLICATION_CERT_PASSWORD" -T /usr/bin/pkgbuild -T /usr/bin/codesign -+ echo "$DARWIN_TOOLCHAIN_INSTALLER_CERT_BASE64" | base64 --decode -o developerID_installer.p12 -+ security import developerID_installer.p12 -P "$DARWIN_TOOLCHAIN_INSTALLER_CERT_PASSWORD" -T /usr/bin/pkgbuild -T /usr/bin/codesign -+ security set-key-partition-list -S "apple-tool:,apple:" -s -k "$KEYCHAIN_PASSWORD" $KEYCHAIN -+ env: -+ KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT_BASE64: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT_BASE64 }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT_PASSWORD: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT_PASSWORD }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT_BASE64: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT_BASE64 }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT_PASSWORD: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT_PASSWORD }} -+ - name: Run ./utils/webassembly/distribute-latest-toolchain.sh -+ run: | -+ BRANCH="${{ github.event.inputs.branch }}" -+ CHANNEL="${{ github.event.inputs.channel }}" -+ -+ case "$CHANNEL" in -+ "DEVELOPMENT") -+ export TARGETS_TO_DIST="amazonlinux2_x86_64 ubuntu18.04_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 macos_x86_64 macos_arm64" -+ ./utils/webassembly/distribute-latest-toolchain.sh ${BRANCH:-swiftwasm} DEVELOPMENT "${{ github.event.inputs.toolchain_name }}" -+ ;; -+ "5.3") -+ export TARGETS_TO_DIST="ubuntu18.04_x86_64 ubuntu20.04_x86_64 macos_x86_64" -+ ./utils/webassembly/distribute-latest-toolchain.sh ${BRANCH:-swiftwasm-release/5.3} 5.3 "${{ github.event.inputs.toolchain_name }}" -+ ;; -+ "5.4") -+ export TARGETS_TO_DIST="ubuntu18.04_x86_64 ubuntu20.04_x86_64 macos_x86_64 macos_arm64" -+ ./utils/webassembly/distribute-latest-toolchain.sh ${BRANCH:-swiftwasm-release/5.4} 5.4 "${{ github.event.inputs.toolchain_name }}" -+ ;; -+ "5.5") -+ export TARGETS_TO_DIST="ubuntu18.04_x86_64 ubuntu20.04_x86_64 macos_x86_64 macos_arm64" -+ ./utils/webassembly/distribute-latest-toolchain.sh ${BRANCH:-swiftwasm-release/5.5} 5.5 "${{ github.event.inputs.toolchain_name }}" -+ ;; -+ "5.6") -+ export TARGETS_TO_DIST="amazonlinux2_x86_64 ubuntu18.04_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 macos_x86_64 macos_arm64" -+ ./utils/webassembly/distribute-latest-toolchain.sh ${BRANCH:-swiftwasm-release/5.6} 5.6 "${{ github.event.inputs.toolchain_name }}" -+ ;; -+ *) -+ export TARGETS_TO_DIST="amazonlinux2_x86_64 ubuntu18.04_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 macos_x86_64 macos_arm64" -+ ./utils/webassembly/distribute-latest-toolchain.sh ${BRANCH:-swiftwasm-release/$CHANNEL} "$CHANNEL" "${{ github.event.inputs.toolchain_name }}" -+ ;; -+ esac -+ -+ env: -+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT }} -+ DARWIN_TOOLCHAIN_NOTARIZE_EMAIL: ${{ secrets.DARWIN_TOOLCHAIN_NOTARIZE_EMAIL }} -+ DARWIN_TOOLCHAIN_NOTARIZE_PASSWORD: ${{ secrets.DARWIN_TOOLCHAIN_NOTARIZE_PASSWORD }} -+ DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID: ${{ secrets.DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID }} -+ DARWIN_TOOLCHAIN_DISPLAY_NAME: ${{ github.event.inputs.display_name }} -+ DARWIN_TOOLCHAIN_DISPLAY_NAME_SHORT: ${{ github.event.inputs.display_name_short }} -diff --git a/.github/workflows/nightly-distribution.yml b/.github/workflows/nightly-distribution.yml -new file mode 100644 -index 00000000000..ea6744d3702 ---- /dev/null -+++ b/.github/workflows/nightly-distribution.yml -@@ -0,0 +1,64 @@ -+name: Nightly distribution -+on: -+ schedule: -+ - cron: '0 0 * * *' -+jobs: -+ nightly-distribution: -+ runs-on: macos-latest -+ strategy: -+ matrix: -+ include: -+ - branch: "swiftwasm" -+ channel: "DEVELOPMENT" -+# targets: "amazonlinux2_x86_64 ubuntu18.04_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 ubuntu22.04_x86_64 macos_x86_64 macos_arm64" -+ targets: "ubuntu18.04_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 ubuntu22.04_x86_64 macos_x86_64 macos_arm64" -+ - branch: "swiftwasm-release/5.3" -+ channel: "5.3" -+ targets: "ubuntu18.04_x86_64 ubuntu20.04_x86_64 macos_x86_64" -+ - branch: "swiftwasm-release/5.4" -+ channel: "5.4" -+ targets: "ubuntu18.04_x86_64 ubuntu20.04_x86_64 macos_x86_64 macos_arm64" -+ - branch: "swiftwasm-release/5.5" -+ channel: "5.5" -+ targets: "ubuntu18.04_x86_64 ubuntu20.04_x86_64 macos_x86_64 macos_arm64" -+ - branch: "swiftwasm-release/5.6" -+ channel: "5.6" -+ targets: "amazonlinux2_x86_64 ubuntu18.04_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 macos_x86_64 macos_arm64" -+ - branch: "swiftwasm-release/5.7" -+ channel: "5.7" -+ targets: "amazonlinux2_x86_64 ubuntu20.04_x86_64 ubuntu20.04_aarch64 ubuntu22.04_x86_64 macos_x86_64 macos_arm64" -+ steps: -+ - uses: actions/checkout@v1 -+ - name: Setup keychain -+ run: | -+ security create-keychain -p "$KEYCHAIN_PASSWORD" swiftwasm-ci -+ KEYCHAIN=$HOME/Library/Keychains/swiftwasm-ci-db -+ security unlock-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN -+ security set-keychain-settings -lut 7200 $KEYCHAIN -+ security default-keychain -s $KEYCHAIN -+ -+ echo "$DARWIN_TOOLCHAIN_APPLICATION_CERT_BASE64" | base64 --decode -o developerID_application.p12 -+ security import developerID_application.p12 -P "$DARWIN_TOOLCHAIN_APPLICATION_CERT_PASSWORD" -T /usr/bin/pkgbuild -T /usr/bin/codesign -+ echo "$DARWIN_TOOLCHAIN_INSTALLER_CERT_BASE64" | base64 --decode -o developerID_installer.p12 -+ security import developerID_installer.p12 -P "$DARWIN_TOOLCHAIN_INSTALLER_CERT_PASSWORD" -T /usr/bin/pkgbuild -T /usr/bin/codesign -+ -+ security set-key-partition-list -S "apple-tool:,apple:" -s -k "$KEYCHAIN_PASSWORD" $KEYCHAIN -+ env: -+ KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT_BASE64: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT_BASE64 }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT_PASSWORD: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT_PASSWORD }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT_BASE64: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT_BASE64 }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT_PASSWORD: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT_PASSWORD }} -+ -+ - run: ./utils/webassembly/distribute-latest-toolchain.sh ${{ matrix.branch }} ${{ matrix.channel }} -+ env: -+ TARGETS_TO_DIST: ${{ matrix.targets }} -+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -+ BUILDBOT_GITHUB_TOKEN: ${{ secrets.SWIFTWASM_BUILDBOT_TOKEN }} -+ DARWIN_TOOLCHAIN_APPLICATION_CERT: ${{ secrets.DARWIN_TOOLCHAIN_APPLICATION_CERT }} -+ DARWIN_TOOLCHAIN_INSTALLER_CERT: ${{ secrets.DARWIN_TOOLCHAIN_INSTALLER_CERT }} -+ DARWIN_TOOLCHAIN_NOTARIZE_EMAIL: ${{ secrets.DARWIN_TOOLCHAIN_NOTARIZE_EMAIL }} -+ DARWIN_TOOLCHAIN_NOTARIZE_PASSWORD: ${{ secrets.DARWIN_TOOLCHAIN_NOTARIZE_PASSWORD }} -+ DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID: ${{ secrets.DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID }} -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 8f8b08c2335..26601e0171d 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -515,6 +515,11 @@ option(SWIFT_STDLIB_ENABLE_OBJC_INTEROP - "Should stdlib be built with Obj-C interop." - "${SWIFT_STDLIB_ENABLE_OBJC_INTEROP_default}") - -+# FIXME(wasm) Reflection tests are temporalily disabled due to lack of linker features -+option(SWIFTWASM_DISABLE_REFLECTION_TEST -+ "Disable building swift-reflection-test for WebAssembly build" -+ FALSE) -+ - # - # User-configurable experimental options. Do not use in production builds. - # -diff --git a/README.md b/README.md -index 43501cc0ad2..92f4b79a4b5 100644 ---- a/README.md -+++ b/README.md -@@ -1,3 +1,23 @@ -+# SwiftWasm -+ -+## Compile your Swift code to WebAssembly -+ -+[![CI status](https://github.com/swiftwasm/swift/workflows/Build%20Toolchain/badge.svg?branch=swiftwasm)](https://github.com/swiftwasm/swift/actions?query=workflow%3ABuild) [![Discord](https://img.shields.io/discord/780838335798706197?label=Discord)](https://discord.gg/ashJW8T8yp) -+ -+This is the main repository for SwiftWasm toolchain and SDK. Please refer to [the -+SwiftWasm book](https://swiftwasm.github.io/swiftwasm-book/) to get started, and to -+the [`awesome-swiftwasm`](https://github.com/swiftwasm/awesome-swiftwasm) list for -+more links in the SwiftWasm ecosystem. -+ -+If you'd like to participate in the growing [SwiftWasm](https://swiftwasm.org) community, you're -+very welcome to join [our Discord server](https://discord.gg/ashJW8T8yp), or the `#webassembly` -+channel in [the SwiftPM Slack](https://swift-package-manager.herokuapp.com/). -+ -+What follows below is `README.md` of the upstream Swift project included verbatim. If you'd like -+to track the status of our builds, please refer to our [GitHub Actions page](https://github.com/swiftwasm/swift/actions). -+ -+
-+
- - - Swift logo -diff --git a/cmake/caches/Runtime-WASI-wasm32.cmake b/cmake/caches/Runtime-WASI-wasm32.cmake -index d96dc885022..a7be4f3d6b0 100644 ---- a/cmake/caches/Runtime-WASI-wasm32.cmake -+++ b/cmake/caches/Runtime-WASI-wasm32.cmake -@@ -18,8 +18,17 @@ set(SWIFTWASM_DISABLE_REFLECTION_TEST YES CACHE BOOL "") - set(SWIFT_BUILD_STATIC_STDLIB YES CACHE BOOL "") - set(SWIFT_BUILD_DYNAMIC_STDLIB NO CACHE BOOL "") - set(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY YES CACHE BOOL "") -+# TODO(katei): This should get turned off, as this is not an ABI stable platform. -+# But current CMake build system doesn't support SWIFT_STDLIB_STABLE_ABI=NO -+set(SWIFT_STDLIB_STABLE_ABI YES CACHE BOOL "") -+set(SWIFT_STDLIB_ENABLE_THINCMO YES CACHE BOOL "") - - # build with the host compiler - set(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER YES CACHE BOOL "") - - set(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY TRUE CACHE BOOL "") -+set(SWIFT_THREADING_PACKAGE TRUE CACHE STRING "none") -+ -+set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING FALSE CACHE BOOL "") -+set(SWIFT_STDLIB_HAS_DLADDR FALSE CACHE BOOL "") -+set(SWIFT_STDLIB_COMPACT_ABSOLUTE_FUNCTION_POINTER TRUE CACHE BOOL "") -diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake -index 58c7eb1bd4f..719cdcb0e6d 100644 ---- a/cmake/modules/AddSwift.cmake -+++ b/cmake/modules/AddSwift.cmake -@@ -392,6 +392,8 @@ function(_add_host_variant_link_flags target) - cxx_link_libraries) - target_link_libraries(${target} PRIVATE - ${cxx_link_libraries}) -+ elseif("${LFLAGS_SDK}" STREQUAL "WASI") -+ list(APPEND result "-Wl,wasi-emulated-mman") - else() - # If lto is enabled, we need to add the object path flag so that the LTO code - # generator leaves the intermediate object file in a place where it will not -diff --git a/include/swift/IRGen/Linking.h b/include/swift/IRGen/Linking.h -index 81000774e96..612c2d43252 100644 ---- a/include/swift/IRGen/Linking.h -+++ b/include/swift/IRGen/Linking.h -@@ -1616,6 +1616,15 @@ public: - // TODO: BFD and gold do not handle COMDATs properly - if (Triple.isOSBinFormatELF()) - return; -+ // WebAssembly: hack: comdat + custom section = explosion -+ // the comdat code assumes section name would be unique for each comdat -+ // this doesn't happen for metadata. -+ // And avoid to create GOT because wasm32 doesn't support -+ // dynamic linking yet -+ if (Triple.isOSBinFormatWasm()) { -+ GV->setDSOLocal(true); -+ return; -+ } - - // COMDATs cannot be applied to declarations. If we have a definition, - // apply the COMDAT. -diff --git a/include/swift/Runtime/HeapObject.h b/include/swift/Runtime/HeapObject.h -index 62c178285d2..2fedb199ef1 100644 ---- a/include/swift/Runtime/HeapObject.h -+++ b/include/swift/Runtime/HeapObject.h -@@ -194,10 +194,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n); - - // Refcounting observation hooks for memory tools. Don't use these. - SWIFT_RUNTIME_EXPORT -+SWIFT_CC(swift) - size_t swift_retainCount(HeapObject *object); - SWIFT_RUNTIME_EXPORT -+SWIFT_CC(swift) - size_t swift_unownedRetainCount(HeapObject *object); - SWIFT_RUNTIME_EXPORT -+SWIFT_CC(swift) - size_t swift_weakRetainCount(HeapObject *object); - - /// Is this pointer a non-null unique reference to an object? -diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp -index 7905131d05b..4b5001e3acb 100644 ---- a/lib/ClangImporter/ClangImporter.cpp -+++ b/lib/ClangImporter/ClangImporter.cpp -@@ -450,6 +450,42 @@ static inline bool isPCHFilenameExtension(StringRef path) { - .endswith(file_types::getExtension(file_types::TY_PCH)); - } - -+static Optional -+getWasiLibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple, -+ SmallVectorImpl &buffer) { -+ StringRef platform = swift::getPlatformNameForTriple(triple); -+ StringRef arch = swift::getMajorArchitectureName(triple); -+ StringRef SDKPath = Opts.getSDKPath(); -+ -+ if (!SDKPath.empty()) { -+ buffer.clear(); -+ buffer.append(SDKPath.begin(), SDKPath.end()); -+ llvm::sys::path::append(buffer, "usr", "lib", "swift"); -+ llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap"); -+ -+ // Only specify the module map if that file actually exists. It may not; -+ // for example in the case that `swiftc -target x86_64-unknown-linux-gnu -+ // -emit-ir` is invoked using a Swift compiler not built for Linux targets. -+ if (llvm::sys::fs::exists(buffer)) -+ return StringRef(buffer.data(), buffer.size()); -+ } -+ -+ if (!Opts.RuntimeResourcePath.empty()) { -+ buffer.clear(); -+ buffer.append(Opts.RuntimeResourcePath.begin(), -+ Opts.RuntimeResourcePath.end()); -+ llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap"); -+ -+ // Only specify the module map if that file actually exists. It may not; -+ // for example in the case that `swiftc -target x86_64-unknown-linux-gnu -+ // -emit-ir` is invoked using a Swift compiler not built for Linux targets. -+ if (llvm::sys::fs::exists(buffer)) -+ return StringRef(buffer.data(), buffer.size()); -+ } -+ -+ return None; -+} -+ - void - importer::getNormalInvocationArguments( - std::vector &invocationArgStrs, -@@ -630,6 +666,17 @@ importer::getNormalInvocationArguments( - }); - } - -+ if (triple.isOSWASI()) { -+ invocationArgStrs.insert(invocationArgStrs.end(), -+ {"-D_WASI_EMULATED_MMAN", -+ "-D_WASI_EMULATED_SIGNAL", -+ "-D_WASI_EMULATED_PROCESS_CLOCKS"}); -+ SmallString<128> buffer; -+ if (auto path = getWasiLibcModuleMapPath(searchPathOpts, triple, buffer)) { -+ invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str()); -+ } -+ } -+ - if (triple.isOSWindows()) { - switch (triple.getArch()) { - default: llvm_unreachable("unsupported Windows architecture"); -diff --git a/lib/ClangImporter/ImportType.cpp b/lib/ClangImporter/ImportType.cpp -index 376993b9c6b..4829f9bf2e3 100644 ---- a/lib/ClangImporter/ImportType.cpp -+++ b/lib/ClangImporter/ImportType.cpp -@@ -842,7 +842,7 @@ namespace { - } - - static const llvm::StringLiteral vaListNames[] = { -- "va_list", "__gnuc_va_list", "__va_list" -+ "va_list", "__gnuc_va_list", "__isoc_va_list", "__va_list" - }; - - ImportHint hint = ImportHint::None; -diff --git a/lib/ClangImporter/MappedTypes.def b/lib/ClangImporter/MappedTypes.def -index cb5694f5161..43079a03f32 100644 ---- a/lib/ClangImporter/MappedTypes.def -+++ b/lib/ClangImporter/MappedTypes.def -@@ -128,6 +128,7 @@ MAP_STDLIB_TYPE("u_int64_t", UnsignedInt, 64, "UInt64", false, DoNothing) - // There's an explicit workaround in ImportType.cpp's VisitDecayedType for that. - MAP_STDLIB_TYPE("va_list", VaList, 0, "CVaListPointer", false, DoNothing) - MAP_STDLIB_TYPE("__gnuc_va_list", VaList, 0, "CVaListPointer", false, DoNothing) -+MAP_STDLIB_TYPE("__isoc_va_list", VaList, 0, "CVaListPointer", false, DoNothing) - MAP_STDLIB_TYPE("__va_list", VaList, 0, "CVaListPointer", false, DoNothing) - - // libkern/OSTypes.h types. -diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt -index 679e4854ff7..389534745fc 100644 ---- a/lib/Driver/CMakeLists.txt -+++ b/lib/Driver/CMakeLists.txt -@@ -14,6 +14,7 @@ set(swiftDriver_sources - UnixToolChains.cpp - WebAssemblyToolChains.cpp - WindowsToolChains.cpp -+ WebAssemblyToolChains.cpp - ) - - set(swiftDriver_targetDefines) -diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp -index 4b3e384315f..66d4956da38 100644 ---- a/lib/Driver/Driver.cpp -+++ b/lib/Driver/Driver.cpp -@@ -367,7 +367,7 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) { - case llvm::Triple::Haiku: - return std::make_unique(*this, target); - case llvm::Triple::WASI: -- return std::make_unique(*this, target); -+ return std::make_unique(*this, target); - default: - Diags.diagnose(SourceLoc(), diag::error_unknown_target, - ArgList.getLastArg(options::OPT_target)->getValue()); -@@ -1787,6 +1787,13 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args, - }); - } - } -+ } if (OI.SDKPath.empty() && TC.getTriple().isOSWASI()) { -+ llvm::SmallString<128> SDKPath; -+ llvm::sys::path::append(SDKPath, getSwiftProgramPath()); -+ llvm::sys::path::remove_filename(SDKPath); // 'swift' -+ llvm::sys::path::remove_filename(SDKPath); // 'bin' -+ llvm::sys::path::append(SDKPath, "share", "wasi-sysroot"); -+ OI.SDKPath = SDKPath.str().str(); - } - - if (!OI.SDKPath.empty()) { -@@ -2047,6 +2054,7 @@ void Driver::buildActions(SmallVectorImpl &TopLevelActions, - } - case file_types::TY_AutolinkFile: - case file_types::TY_Object: -+ case file_types::TY_LLVM_BC: - case file_types::TY_TBD: - // Object inputs are only okay if linking. - if (OI.shouldLink()) { -@@ -2060,7 +2068,6 @@ void Driver::buildActions(SmallVectorImpl &TopLevelActions, - case file_types::TY_Dependencies: - case file_types::TY_Assembly: - case file_types::TY_LLVM_IR: -- case file_types::TY_LLVM_BC: - case file_types::TY_SerializedDiagnostics: - case file_types::TY_ClangHeader: - case file_types::TY_ClangModuleFile: -@@ -2384,7 +2391,8 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) { - if (Args.hasFlag(options::OPT_static_executable, - options::OPT_no_static_executable, false) || - Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib, -- false)) { -+ false) || -+ TC.getTriple().isOSBinFormatWasm()) { - commandLine.push_back("-use-static-resource-dir"); - } - -diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp -index c0ee9217e86..4ba2bce12ac 100644 ---- a/lib/Driver/ToolChains.cpp -+++ b/lib/Driver/ToolChains.cpp -@@ -603,7 +603,8 @@ ToolChain::constructInvocation(const CompileJobAction &job, - if (context.Args.hasFlag(options::OPT_static_executable, - options::OPT_no_static_executable, false) || - context.Args.hasFlag(options::OPT_static_stdlib, -- options::OPT_no_static_stdlib, false)) { -+ options::OPT_no_static_stdlib, false) || -+ getTriple().isOSBinFormatWasm()) { - Arguments.push_back("-use-static-resource-dir"); - } - -@@ -1125,7 +1126,13 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job, - context.Args.AddLastArg(Arguments, options::OPT_import_objc_header); - - context.Args.AddLastArg(Arguments, options::OPT_disable_incremental_imports); -- -+ if (context.Args.hasFlag(options::OPT_static_executable, -+ options::OPT_no_static_executable, false) || -+ context.Args.hasFlag(options::OPT_static_stdlib, -+ options::OPT_no_static_stdlib, false) || -+ getTriple().isOSBinFormatWasm()) { -+ Arguments.push_back("-use-static-resource-dir"); -+ } - Arguments.push_back("-module-name"); - Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName)); - -@@ -1325,6 +1332,14 @@ ToolChain::constructInvocation(const GeneratePCHJobAction &job, - addInputsOfType(Arguments, context.InputActions, file_types::TY_ClangHeader); - context.Args.AddLastArg(Arguments, options::OPT_index_store_path); - -+ if (context.Args.hasFlag(options::OPT_static_executable, -+ options::OPT_no_static_executable, false) || -+ context.Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib, -+ false) || -+ getTriple().isOSBinFormatWasm()) { -+ Arguments.push_back("-use-static-resource-dir"); -+ } -+ - if (job.isPersistentPCH()) { - Arguments.push_back("-emit-pch"); - Arguments.push_back("-pch-output-dir"); -@@ -1409,7 +1424,8 @@ void ToolChain::getResourceDirPath(SmallVectorImpl &resourceDirPath, - if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) { - StringRef value = A->getValue(); - resourceDirPath.append(value.begin(), value.end()); -- } else if (!getTriple().isOSDarwin() && args.hasArg(options::OPT_sdk)) { -+ } else if (!getTriple().isOSDarwin() && !getTriple().isOSWASI() && args.hasArg(options::OPT_sdk)) { -+ // for WASI, sdk option points to wasi-sysroot which doesn't have Swift toolchain - StringRef value = args.getLastArg(options::OPT_sdk)->getValue(); - resourceDirPath.append(value.begin(), value.end()); - llvm::sys::path::append(resourceDirPath, "usr"); -diff --git a/lib/Driver/WebAssemblyToolChains.cpp b/lib/Driver/WebAssemblyToolChains.cpp -index 52c9a2cb06d..61a296b1e8d 100644 ---- a/lib/Driver/WebAssemblyToolChains.cpp -+++ b/lib/Driver/WebAssemblyToolChains.cpp -@@ -158,6 +158,10 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job, - llvm::report_fatal_error(linkFile + " not found"); - } - -+ // Explicitly pass the target to the linker -+ Arguments.push_back( -+ context.Args.MakeArgString("--target=" + getTriple().str())); -+ - // Delegate to Clang for sanitizers. It will figure out the correct linker - // options. - if (job.getKind() == LinkKind::Executable && context.OI.SelectedSanitizers) { -diff --git a/lib/DriverTool/autolink_extract_main.cpp b/lib/DriverTool/autolink_extract_main.cpp -index 3d6b7fdb8a2..3f6f78774fa 100644 ---- a/lib/DriverTool/autolink_extract_main.cpp -+++ b/lib/DriverTool/autolink_extract_main.cpp -@@ -153,36 +153,6 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, - return false; - } - --/// Look inside the object file 'WasmObjectFile' and append any linker flags --/// found in its ".swift1_autolink_entries" section to 'LinkerFlags'. Return --/// 'true' if there was an error, and 'false' otherwise. --static bool --extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, -- std::vector &LinkerFlags, -- std::unordered_map &SwiftRuntimeLibraries, -- CompilerInstance &Instance) { -- // Search for the data segment we hold autolink entries in -- for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments()) { -- if (Segment.Data.Name == ".swift1_autolink_entries") { -- -- StringRef SegmentData = llvm::toStringRef(Segment.Data.Content); -- // entries are null-terminated, so extract them and push them into -- // the set. -- llvm::SmallVector SplitFlags; -- SegmentData.split(SplitFlags, llvm::StringRef("\0", 1), -1, -- /*KeepEmpty=*/false); -- for (const auto &Flag : SplitFlags) { -- auto RuntimeLibEntry = SwiftRuntimeLibraries.find(Flag.str()); -- if (RuntimeLibEntry == SwiftRuntimeLibraries.end()) -- LinkerFlags.emplace_back(Flag.str()); -- else -- RuntimeLibEntry->second = true; -- } -- } -- } -- return false; --} -- - /// Look inside the binary 'Bin' and append any linker flags found in its - /// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive, - /// recursively look inside all children within the archive. Return 'true' if -diff --git a/lib/FrontendTool/TBD.cpp b/lib/FrontendTool/TBD.cpp -index b490687c4bb..35f7ae06e07 100644 ---- a/lib/FrontendTool/TBD.cpp -+++ b/lib/FrontendTool/TBD.cpp -@@ -75,6 +75,15 @@ static bool isSymbolIgnored(const StringRef& name, - } - } - -+ if (llvm::Triple(IRModule.getTargetTriple()).isWasm()) { -+ // `__main_void`, which is called by `_start` in crt1.o, is artificially -+ // aliased in IR module when `main` doesn't take any params. -+ // This alias will be hidden after https://reviews.llvm.org/D75277 -+ if (name == "__main_void") { -+ return true; -+ } -+ } -+ - return false; - } - -diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp -index 4e5e9e46404..a860cbc148b 100644 ---- a/lib/IRGen/GenDecl.cpp -+++ b/lib/IRGen/GenDecl.cpp -@@ -3257,7 +3257,8 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded( - - StackProtectorMode IRGenModule::shouldEmitStackProtector(SILFunction *f) { - const SILOptions &opts = IRGen.SIL.getOptions(); -- return (opts.EnableStackProtection && f->needsStackProtection()) ? -+ // FIXME(katei): stack protection support will be added by https://github.com/WebAssembly/wasi-libc/pull/351 -+ return (opts.EnableStackProtection && f->needsStackProtection() && Triple.getObjectFormat() != llvm::Triple::Wasm) ? - StackProtectorMode::StackProtector : StackProtectorMode::NoStackProtector; - } - -@@ -3730,6 +3731,12 @@ IRGenModule::getAddrOfLLVMVariableOrGOTEquivalent(LinkEntity entity) { - cast(entry), entity); - return {gotEquivalent, ConstantReference::Indirect}; - }; -+ -+ // Avoid to create GOT because wasm32 doesn't support -+ // dynamic linking yet -+ if (TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { -+ return direct(); -+ } - - // Dynamically replaceable function keys are stored in the GlobalVars - // table, but they don't have an associated Decl, so they require -diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp -index b3569a855f1..b6006c6fb87 100644 ---- a/lib/IRGen/GenKeyPath.cpp -+++ b/lib/IRGen/GenKeyPath.cpp -@@ -119,7 +119,6 @@ getAccessorForComputedComponent(IRGenModule &IGM, - accessor = component.getSubscriptIndexHash(); - break; - } -- - // If the accessor is not generic, and locally available, we can use it as is. - // If it's only externally available, we need a local thunk to relative- - // reference. -@@ -225,7 +224,8 @@ getAccessorForComputedComponent(IRGenModule &IGM, - componentArgsBuf = params.claimNext(); - // Pass the argument pointer down to the underlying function, if it - // wants it. -- if (hasSubscriptIndices) { -+ // Always forward extra argument to match callee and caller signature on WebAssembly -+ if (hasSubscriptIndices || IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { - forwardedArgs.add(componentArgsBuf); - } - break; -@@ -251,6 +251,10 @@ getAccessorForComputedComponent(IRGenModule &IGM, - forwardingSubs, - &ignoreWitnessMetadata, - forwardedArgs); -+ } else if (IGM.Triple.isOSBinFormatWasm()) { -+ // wasm: Add null swift.type pointer to match signature even when there is -+ // no generic environment. -+ forwardedArgs.add(llvm::ConstantPointerNull::get(IGM.TypeMetadataPtrTy)); - } - auto fnPtr = - FunctionPointer::forDirect(IGM, accessorFn, /*secondaryValue*/ nullptr, -@@ -382,6 +386,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM, - /*vararg*/ false); - auto destroyFn = llvm::Function::Create(destroyType, - llvm::GlobalValue::PrivateLinkage, "keypath_destroy", IGM.getModule()); -+ destroyFn->setCallingConv(IGM.SwiftCC); - destroy = destroyFn; - destroyFn->setAttributes(IGM.constructInitialAttributes()); - destroyFn->setCallingConv(IGM.SwiftCC); -@@ -433,6 +438,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM, - /*vararg*/ false); - auto copyFn = llvm::Function::Create(copyType, - llvm::GlobalValue::PrivateLinkage, "keypath_copy", IGM.getModule()); -+ copyFn->setCallingConv(IGM.SwiftCC); - copy = copyFn; - copyFn->setAttributes(IGM.constructInitialAttributes()); - copyFn->setCallingConv(IGM.SwiftCC); -diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp -index f58b197b218..755ae1ed93a 100644 ---- a/lib/IRGen/GenMeta.cpp -+++ b/lib/IRGen/GenMeta.cpp -@@ -6019,7 +6019,7 @@ GenericRequirementsMetadata irgen::addGenericRequirements( - unsigned tag = unsigned(descriptorRef.isIndirect()); - if (protocol->isObjC()) - tag |= 0x02; -- -+ - B.addTaggedRelativeOffset(IGM.RelativeAddressTy, - descriptorRef.getValue(), - tag); -diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp -index 3da81576d67..2fca32ed692 100644 ---- a/lib/IRGen/IRGen.cpp -+++ b/lib/IRGen/IRGen.cpp -@@ -183,8 +183,10 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) { - - // WebAssembly doesn't support atomics yet, see - // https://github.com/apple/swift/issues/54533 for more details. -- if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm()) -+ if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm()) { -+ TargetOpts.DebuggerTuning = llvm::DebuggerKind::Default; - TargetOpts.ThreadModel = llvm::ThreadModel::Single; -+ } - - if (Opts.EnableGlobalISel) { - TargetOpts.EnableGlobalISel = true; -diff --git a/lib/IRGen/IRGenFunction.cpp b/lib/IRGen/IRGenFunction.cpp -index a8f52446d15..624595d8af1 100644 ---- a/lib/IRGen/IRGenFunction.cpp -+++ b/lib/IRGen/IRGenFunction.cpp -@@ -320,6 +320,7 @@ IRGenFunction::emitLoadOfRelativePointer(Address addr, bool isFar, - if (!isFar) { - value = Builder.CreateSExt(value, IGM.IntPtrTy); - } -+ - auto *addrInt = Builder.CreatePtrToInt(addr.getAddress(), IGM.IntPtrTy); - auto *uncastPointerInt = Builder.CreateAdd(addrInt, value); - auto *uncastPointer = Builder.CreateIntToPtr(uncastPointerInt, IGM.Int8PtrTy); -diff --git a/lib/IRGen/MetadataRequest.cpp b/lib/IRGen/MetadataRequest.cpp -index 74a126596ae..18f9ef8995f 100644 ---- a/lib/IRGen/MetadataRequest.cpp -+++ b/lib/IRGen/MetadataRequest.cpp -@@ -2990,13 +2990,15 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, - IGM.Int32Ty); - stringAddrOffset = subIGF.Builder.CreateSExtOrBitCast(stringAddrOffset, - IGM.SizeTy); -+ -+ llvm::Value *stringAddr; - auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy); - if (IGM.getModule()->getDataLayout().isBigEndian()) { - stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase, - llvm::ConstantInt::get(IGM.SizeTy, 4)); - } -- auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, -- stringAddrOffset); -+ stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, -+ stringAddrOffset); - stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); - - llvm::CallInst *call; -diff --git a/lib/SIL/Verifier/SILVerifier.cpp b/lib/SIL/Verifier/SILVerifier.cpp -index 55fca29e8bc..29eec3a55bf 100644 ---- a/lib/SIL/Verifier/SILVerifier.cpp -+++ b/lib/SIL/Verifier/SILVerifier.cpp -@@ -292,8 +292,9 @@ void verifyKeyPathComponent(SILModule &M, - SILFunctionTypeRepresentation::Thin, - "getter should be a thin function"); - -- require(substGetterType->getNumParameters() == 1 + hasIndices, -- "getter should have one parameter"); -+ // FIXME(katei): Disabled for now. Will be replaced by keypath cc -+ // require(substGetterType->getNumParameters() == 1 + hasIndices, -+ // "getter should have one parameter"); - auto baseParam = substGetterType->getParameters()[0]; - require(baseParam.getConvention() == normalArgConvention, - "getter base parameter should have normal arg convention"); -@@ -344,8 +345,9 @@ void verifyKeyPathComponent(SILModule &M, - SILFunctionTypeRepresentation::Thin, - "setter should be a thin function"); - -- require(substSetterType->getNumParameters() == 2 + hasIndices, -- "setter should have two parameters"); -+ // FIXME(katei): Disabled for now. Will be replaced by keypath cc -+ // require(substSetterType->getNumParameters() == 2 + hasIndices, -+ // "setter should have two parameters"); - - auto newValueParam = substSetterType->getParameters()[0]; - // TODO: This should probably be unconditionally +1 when we -diff --git a/lib/SILGen/SILGenBuiltin.cpp b/lib/SILGen/SILGenBuiltin.cpp -index 249a8a2e379..5ca4b031469 100644 ---- a/lib/SILGen/SILGenBuiltin.cpp -+++ b/lib/SILGen/SILGenBuiltin.cpp -@@ -1605,8 +1605,33 @@ static ManagedValue emitBuiltinCreateAsyncTaskInGroup( - SGF.B.createMetatype(loc, SGF.getLoweredType(futureResultType))); - }).borrow(SGF, loc).forward(SGF); - -- auto function = emitFunctionArgumentForAsyncTaskEntryPoint(SGF, loc, args[2], -- futureResultType); -+ // FIXME(katei): This is a hack to fix the ABI signature mismatch issue -+ // https://github.com/apple/swift/issues/63060 -+ // ==================== BEGIN HACK ==================== -+ // Ensure that the closure has the appropriate type. -+ auto extInfo = -+ ASTExtInfoBuilder() -+ .withAsync() -+ .withThrows() -+ .withRepresentation(GenericFunctionType::Representation::Swift) -+ .build(); -+ auto genericSig = subs.getGenericSignature().getCanonicalSignature(); -+ auto genericResult = -+ GenericTypeParamType::get(/*isParameterPack*/ false, -+ /*depth*/ 0, /*index*/ 0, SGF.getASTContext()); -+ // () async throws -> Void -+ CanType functionTy = -+ GenericFunctionType::get(genericSig, {}, genericResult, extInfo) -+ ->getCanonicalType(); -+ AbstractionPattern origParam(genericSig, functionTy); -+ CanType substParamType = functionTy.subst(subs)->getCanonicalType(); -+ auto reabstractedFun = -+ SGF.emitSubstToOrigValue(loc, args[2], origParam, substParamType); -+ // ==================== END HACK ====================== -+ -+ auto function = emitFunctionArgumentForAsyncTaskEntryPoint( -+ SGF, loc, reabstractedFun, futureResultType); -+ - auto apply = SGF.B.createBuiltin( - loc, - ctx.getIdentifier( -diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp -index c93158b6ede..7f2b3914095 100644 ---- a/lib/SILGen/SILGenExpr.cpp -+++ b/lib/SILGen/SILGenExpr.cpp -@@ -2881,6 +2881,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - } - } - -+ auto Target = SGM.getASTContext().LangOpts.Target; - auto genericSig = - genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() - : nullptr; -@@ -2889,6 +2890,14 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - genericEnv = nullptr; - } - -+ // Add empty generic type parameter to match function signature on WebAssembly -+ if (!genericSig && Target.isOSBinFormatWasm()) { -+ auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext()); -+ auto sig = GenericSignature::get(param, { }); -+ genericSig = CanGenericSignature(sig); -+ genericEnv = sig.getGenericEnvironment(); -+ } -+ - // Build the signature of the thunk as expected by the keypath runtime. - auto signature = [&]() { - CanType loweredBaseTy, loweredPropTy; -@@ -2904,7 +2913,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - SmallVector params; - params.push_back({loweredBaseTy, paramConvention}); - auto &C = SGM.getASTContext(); -- if (!indexes.empty()) -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm()) - params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(), - ParameterConvention::Direct_Unowned}); - -@@ -2966,7 +2976,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, - } - auto baseArg = entry->createFunctionArgument(baseArgTy); - SILValue indexPtrArg; -- if (!indexes.empty()) { -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || Target.isOSBinFormatWasm()) { - auto indexArgTy = signature->getParameters()[1].getSILStorageType( - SGM.M, signature, subSGF.F.getTypeExpansionContext()); - indexPtrArg = entry->createFunctionArgument(indexArgTy); -@@ -3055,6 +3066,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - } - } - -+ auto Target = SGM.getASTContext().LangOpts.Target; - auto genericSig = - genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() - : nullptr; -@@ -3063,6 +3075,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - genericEnv = nullptr; - } - -+ // Add empty generic type parameter to match function signature on WebAssembly -+ if (!genericSig && Target.isOSBinFormatWasm()) { -+ auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext()); -+ auto sig = GenericSignature::get(param, { }); -+ genericSig = CanGenericSignature(sig); -+ genericEnv = sig.getGenericEnvironment(); -+ } -+ - // Build the signature of the thunk as expected by the keypath runtime. - auto signature = [&]() { - CanType loweredBaseTy, loweredPropTy; -@@ -3088,7 +3108,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - ? ParameterConvention::Indirect_Inout - : paramConvention}); - // indexes -- if (!indexes.empty()) -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm()) - params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(), - ParameterConvention::Direct_Unowned}); - -@@ -3144,7 +3165,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, - auto baseArg = entry->createFunctionArgument(baseArgTy); - SILValue indexPtrArg; - -- if (!indexes.empty()) { -+ // Always take indexes parameter to match callee and caller signature on WebAssembly -+ if (!indexes.empty() || Target.isOSBinFormatWasm()) { - auto indexArgTy = signature->getParameters()[2].getSILStorageType( - SGM.M, signature, subSGF.getTypeExpansionContext()); - indexPtrArg = entry->createFunctionArgument(indexArgTy); -@@ -3226,6 +3248,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM, - return; - } - -+ auto Target = SGM.getASTContext().LangOpts.Target; - auto genericSig = - genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() - : nullptr; -@@ -3235,6 +3258,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM, - genericEnv = nullptr; - } - -+ // Add empty generic type parameter to match function signature on WebAssembly -+ if (!genericSig && Target.isOSBinFormatWasm()) { -+ auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext()); -+ auto sig = GenericSignature::get(param, { }); -+ genericSig = CanGenericSignature(sig); -+ genericEnv = sig.getGenericEnvironment(); -+ } -+ - auto &C = SGM.getASTContext(); - auto unsafeRawPointerTy = C.getUnsafeRawPointerType()->getCanonicalType(); - auto boolTy = C.getBoolType()->getCanonicalType(); -diff --git a/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp b/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp -index 1f989e3ae2a..da2b006c0ca 100644 ---- a/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp -+++ b/lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp -@@ -504,6 +504,13 @@ bool SILCombiner::tryOptimizeKeypathKVCString(ApplyInst *AI, - } - - bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) { -+ // FIXME(katei): Disable for WebAssembly for now because -+ // KeyPath cc is unstable and KeyPathProjector hask violates -+ // some assert assumptions -+ SILModule &M = AI->getModule(); -+ if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) -+ return false; -+ - if (SILFunction *callee = AI->getReferencedFunctionOrNull()) { - return tryOptimizeKeypathApplication(AI, callee); - } -@@ -549,6 +556,13 @@ bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) { - /// %addr = struct_element_addr/ref_element_addr %root_object - /// // use %inout_addr - bool SILCombiner::tryOptimizeInoutKeypath(BeginApplyInst *AI) { -+ // FIXME(katei): Disable for WebAssembly for now because -+ // KeyPath cc is unstable and KeyPathProjector hask violates -+ // some assert assumptions -+ SILModule &M = AI->getModule(); -+ if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) -+ return false; -+ - // Disable in OSSA because KeyPathProjector is not fully ported - if (AI->getFunction()->hasOwnership()) - return false; -diff --git a/lib/SILOptimizer/Utils/KeyPathProjector.cpp b/lib/SILOptimizer/Utils/KeyPathProjector.cpp -index 6051ae07d3e..f66ed98396c 100644 ---- a/lib/SILOptimizer/Utils/KeyPathProjector.cpp -+++ b/lib/SILOptimizer/Utils/KeyPathProjector.cpp -@@ -233,7 +233,22 @@ public: - assert(getter->getConventions().getNumSILArguments()); - - auto ref = builder.createFunctionRef(loc, getter); -- builder.createApply(loc, ref, subs, {addr, parentValue}); -+ -+ std::vector args{addr, parentValue}; -+ // FIXME(wasm): For wasm, KeyPath getter always take indices parameter -+ // to match callee and caller signature. So need to pass stub pointer. -+ // See also: getOrCreateKeyPathSetter and getOrCreateKeyPathGetter -+ if (builder.getASTContext().LangOpts.Target.isOSBinFormatWasm()) { -+ auto IntTy = SILType::getBuiltinIntegerType(32, builder.getASTContext()); -+ auto UnsafeRawPointerTy = SILType::getRawPointerType(builder.getASTContext()); -+ auto zeroVal = SILValue(builder.createIntegerLiteral(loc, IntTy, 0)); -+ auto stackBuffer = SILValue(builder.createAllocStack(loc, IntTy)); -+ builder.createStore(loc, zeroVal, stackBuffer, StoreOwnershipQualifier::Unqualified); -+ auto nonePointer = builder.createUncheckedAddrCast(loc, stackBuffer, UnsafeRawPointerTy); -+ args.push_back(SILValue(nonePointer)); -+ } -+ -+ builder.createApply(loc, ref, subs, args); - - // If we were previously accessing a class member, we're done now. - insertEndAccess(beginAccess, builder); -diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake -index 25a79d145fb..5d11a171543 100644 ---- a/stdlib/cmake/modules/AddSwiftStdlib.cmake -+++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake -@@ -286,6 +286,10 @@ function(_add_target_variant_c_compile_flags) - list(APPEND result -funwind-tables) - endif() - -+ if("${CFLAGS_SDK}" STREQUAL "WASI") -+ list(APPEND result "-D_WASI_EMULATED_MMAN" "-D_WASI_EMULATED_SIGNAL" "-D_WASI_EMULATED_PROCESS_CLOCKS") -+ endif() -+ - if("${CFLAGS_SDK}" STREQUAL "LINUX") - if(${CFLAGS_ARCH} STREQUAL x86_64) - # this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic -@@ -293,10 +297,6 @@ function(_add_target_variant_c_compile_flags) - endif() - endif() - -- if("${CFLAGS_SDK}" STREQUAL "WASI") -- list(APPEND result "-D_WASI_EMULATED_MMAN") -- endif() -- - if(NOT SWIFT_STDLIB_ENABLE_OBJC_INTEROP) - list(APPEND result "-DSWIFT_OBJC_INTEROP=0") - endif() -diff --git a/stdlib/cmake/modules/SwiftSource.cmake b/stdlib/cmake/modules/SwiftSource.cmake -index d5d57dfd18a..a916bf5c880 100644 ---- a/stdlib/cmake/modules/SwiftSource.cmake -+++ b/stdlib/cmake/modules/SwiftSource.cmake -@@ -243,6 +243,10 @@ function(_add_target_variant_swift_compile_flags - "-target" "${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}") - endif() - -+ if("${sdk}" STREQUAL "WASI") -+ list(APPEND result "-Xcc" "-D_WASI_EMULATED_MMAN" "-Xcc" "-D_WASI_EMULATED_SIGNAL" "-Xcc" "-D_WASI_EMULATED_PROCESS_CLOCKS") -+ endif() -+ - if(NOT BUILD_STANDALONE) - list(APPEND result "-resource-dir" "${SWIFTLIB_DIR}") - endif() -@@ -318,6 +322,10 @@ function(_add_target_variant_swift_compile_flags - list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN") - endif() - -+ if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME) -+ list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME") -+ endif() -+ - if(SWIFT_STDLIB_HAS_ENVIRON) - list(APPEND result "-D" "SWIFT_STDLIB_HAS_ENVIRON") - list(APPEND result "-Xcc" "-DSWIFT_STDLIB_HAS_ENVIRON") -diff --git a/stdlib/private/CMakeLists.txt b/stdlib/private/CMakeLists.txt -index 05ef455b755..3fb32fdca5d 100644 ---- a/stdlib/private/CMakeLists.txt -+++ b/stdlib/private/CMakeLists.txt -@@ -23,12 +23,13 @@ endif() - if(SWIFT_BUILD_SDK_OVERLAY) - add_subdirectory(OSLog) - -- if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") -+ list_intersect("${SWIFT_APPLE_PLATFORMS}" "${SWIFT_SDKS}" building_darwin_sdks) -+ if(building_darwin_sdks) - add_subdirectory(StdlibUnittestFoundationExtras) - endif() -- # Currently SwiftReflectionTest cannot be built on Windows, due to -- # dependencies on POSIX symbols -- if (SWIFT_INCLUDE_TESTS AND (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")) -+ # Currently SwiftReflectionTest cannot be built on Windows, due to -+ # dependencies on POSIX symbols -+ if (SWIFT_INCLUDE_TESTS AND (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") AND NOT SWIFTWASM_DISABLE_REFLECTION_TEST) - add_subdirectory(SwiftReflectionTest) - endif() - endif() -diff --git a/stdlib/private/StdlibUnittest/CMakeLists.txt b/stdlib/private/StdlibUnittest/CMakeLists.txt -index c816afeb3fe..a444130438c 100644 ---- a/stdlib/private/StdlibUnittest/CMakeLists.txt -+++ b/stdlib/private/StdlibUnittest/CMakeLists.txt -@@ -49,18 +49,19 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} - TestHelpers.swift - TypeIndexed.swift - -- SWIFT_MODULE_DEPENDS SwiftPrivate SwiftPrivateThreadExtras SwiftPrivateLibcExtras ${swift_stdlib_unittest_modules} -- SWIFT_MODULE_DEPENDS_IOS Darwin -- SWIFT_MODULE_DEPENDS_OSX Darwin -- SWIFT_MODULE_DEPENDS_TVOS Darwin -- SWIFT_MODULE_DEPENDS_WATCHOS Darwin -- SWIFT_MODULE_DEPENDS_FREESTANDING "${SWIFT_FREESTANDING_TEST_DEPENDENCIES}" -- SWIFT_MODULE_DEPENDS_LINUX Glibc -- SWIFT_MODULE_DEPENDS_FREEBSD Glibc -- SWIFT_MODULE_DEPENDS_OPENBSD Glibc -- SWIFT_MODULE_DEPENDS_CYGWIN Glibc -- SWIFT_MODULE_DEPENDS_HAIKU Glibc -- SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK -+ SWIFT_MODULE_DEPENDS SwiftPrivate SwiftPrivateLibcExtras ${swift_stdlib_unittest_modules} -+ SWIFT_MODULE_DEPENDS_IOS Darwin SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_OSX Darwin SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_TVOS Darwin SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_WATCHOS Darwin SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_FREESTANDING "${SWIFT_FREESTANDING_TEST_DEPENDENCIES}" SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_LINUX Glibc SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_FREEBSD Glibc SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_OPENBSD Glibc SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_CYGWIN Glibc SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_HAIKU Glibc SwiftPrivateThreadExtras -+ SWIFT_MODULE_DEPENDS_WASI WASILibc -+ SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK SwiftPrivateThreadExtras - C_COMPILE_FLAGS -I${SWIFT_SOURCE_DIR}/stdlib/include - SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/private/StdlibUnittest/InterceptTraps.cpp b/stdlib/private/StdlibUnittest/InterceptTraps.cpp -index 80c3fa59795..cf81e872556 100644 ---- a/stdlib/private/StdlibUnittest/InterceptTraps.cpp -+++ b/stdlib/private/StdlibUnittest/InterceptTraps.cpp -@@ -50,8 +50,6 @@ static void CrashCatcher(int Sig) { - _exit(0); - } - --#endif // __wasi__ -- - #if defined(_WIN32) - static LONG WINAPI - VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) { -@@ -92,6 +90,8 @@ void installTrapInterceptor() { - signal(SIGBUS, CrashCatcher); - signal(SIGSYS, CrashCatcher); - #endif --} - - #endif // !defined(__wasi__) -+} -+ -+#endif // !defined(__wasi__) -\ No newline at end of file -diff --git a/stdlib/private/StdlibUnittest/RaceTest.swift b/stdlib/private/StdlibUnittest/RaceTest.swift -index 008f17a5200..368f49d7000 100644 ---- a/stdlib/private/StdlibUnittest/RaceTest.swift -+++ b/stdlib/private/StdlibUnittest/RaceTest.swift -@@ -38,7 +38,9 @@ - - import SwiftPrivate - import SwiftPrivateLibcExtras -+#if !os(WASI) - import SwiftPrivateThreadExtras -+#endif - #if canImport(Darwin) - import Darwin - #elseif canImport(Glibc) -diff --git a/stdlib/private/StdlibUnittest/StdlibUnittest.swift b/stdlib/private/StdlibUnittest/StdlibUnittest.swift -index d6e3f90bae2..f3ac00c80e6 100644 ---- a/stdlib/private/StdlibUnittest/StdlibUnittest.swift -+++ b/stdlib/private/StdlibUnittest/StdlibUnittest.swift -@@ -12,7 +12,9 @@ - - - import SwiftPrivate -+#if !os(WASI) - import SwiftPrivateThreadExtras -+#endif - import SwiftPrivateLibcExtras - - #if canImport(Darwin) -@@ -22,6 +24,8 @@ import Foundation - import Darwin - #elseif canImport(Glibc) - import Glibc -+#elseif os(WASI) -+import WASILibc - #elseif os(Windows) - import CRT - import WinSDK -@@ -35,6 +39,12 @@ import ObjectiveC - import _Concurrency - #endif - -+#if os(WASI) -+let platformSupportSpawnChild = false -+#else -+let platformSupportSpawnChild = true -+#endif -+ - extension String { - /// Returns the lines in `self`. - public var _lines : [String] { -@@ -855,8 +865,10 @@ var _testSuiteNameToIndex: [String : Int] = [:] - let _stdlibUnittestStreamPrefix = "__STDLIB_UNITTEST__" - let _crashedPrefix = "CRASHED:" - -+#if !os(WASI) - @_silgen_name("installTrapInterceptor") - func _installTrapInterceptor() -+#endif - - #if _runtime(_ObjC) - @objc protocol _StdlibUnittestNSException { -@@ -867,7 +879,9 @@ func _installTrapInterceptor() - // Avoid serializing references to objc_setUncaughtExceptionHandler in SIL. - @inline(never) - func _childProcess() { -+#if !os(WASI) - _installTrapInterceptor() -+#endif - - #if _runtime(_ObjC) - objc_setUncaughtExceptionHandler { -@@ -923,7 +937,9 @@ func _childProcess() { - @available(SwiftStdlib 5.1, *) - @inline(never) - func _childProcessAsync() async { -+#if !os(WASI) - _installTrapInterceptor() -+#endif - - #if _runtime(_ObjC) - objc_setUncaughtExceptionHandler { -@@ -1724,7 +1740,7 @@ public func runAllTests() { - if _isChildProcess { - _childProcess() - } else { -- var runTestsInProcess: Bool = false -+ var runTestsInProcess: Bool = !platformSupportSpawnChild - var filter: String? - var args = [String]() - var i = 0 -@@ -1794,7 +1810,7 @@ public func runAllTestsAsync() async { - if _isChildProcess { - await _childProcessAsync() - } else { -- var runTestsInProcess: Bool = false -+ var runTestsInProcess: Bool = !platformSupportSpawnChild - var filter: String? - var args = [String]() - var i = 0 -diff --git a/stdlib/private/SwiftPrivate/CMakeLists.txt b/stdlib/private/SwiftPrivate/CMakeLists.txt -index 66613d9789c..c1a3f46067c 100644 ---- a/stdlib/private/SwiftPrivate/CMakeLists.txt -+++ b/stdlib/private/SwiftPrivate/CMakeLists.txt -@@ -25,6 +25,7 @@ add_swift_target_library(swiftSwiftPrivate ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - SWIFT_COMPILE_FLAGS ${swift_swiftprivate_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/private/SwiftPrivate/IO.swift b/stdlib/private/SwiftPrivate/IO.swift -index ba5d26515c3..dac3f39dc11 100644 ---- a/stdlib/private/SwiftPrivate/IO.swift -+++ b/stdlib/private/SwiftPrivate/IO.swift -@@ -16,12 +16,16 @@ import SwiftShims - #if canImport(Darwin) - import Darwin - let (platform_read, platform_write, platform_close) = (read, write, close) --#elseif canImport(Glibc) --import Glibc --let (platform_read, platform_write, platform_close) = (read, write, close) - #elseif os(Windows) - import CRT - import WinSDK -+#else -+#if os(WASI) -+import WASILibc -+#elseif canImport(Glibc) -+import Glibc -+#endif -+let (platform_read, platform_write, platform_close) = (read, write, close) - #endif - - #if os(Windows) -diff --git a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt -index ffbea96aa26..09dcb2a7ebb 100644 ---- a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt -+++ b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt -@@ -35,6 +35,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - INCORPORATE_OBJECT_LIBRARIES ${swift_private_libc_extras_incorporate_object_libraries} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt -index 062f51052c4..0a6e3e665a1 100644 ---- a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt -+++ b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt -@@ -16,6 +16,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK - SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT stdlib-experimental -diff --git a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift -index f854aed44aa..1fafe326465 100644 ---- a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift -+++ b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift -@@ -135,7 +135,7 @@ public func _stdlib_thread_join( - } - return (CInt(result), value) - #elseif os(WASI) -- // WASI environment has a only single thread -+ // WASI environment is single-threaded - return (0, nil) - #else - var threadResultRawPtr: UnsafeMutableRawPointer? -diff --git a/stdlib/public/CMakeLists.txt b/stdlib/public/CMakeLists.txt -index c301b1bf9ef..8cf96c4e55e 100644 ---- a/stdlib/public/CMakeLists.txt -+++ b/stdlib/public/CMakeLists.txt -@@ -129,6 +129,10 @@ if(SWIFT_BUILD_STDLIB) - add_subdirectory(core) - add_subdirectory(SwiftOnoneSupport) - -+ if(WASI IN_LIST SWIFT_SDKS) -+ add_subdirectory(WASI) -+ endif() -+ - if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING) - add_subdirectory(Differentiation) - endif() -diff --git a/stdlib/public/Concurrency/AsyncStream.cpp b/stdlib/public/Concurrency/AsyncStream.cpp -index 389ebd3d738..ac2aae25cd3 100644 ---- a/stdlib/public/Concurrency/AsyncStream.cpp -+++ b/stdlib/public/Concurrency/AsyncStream.cpp -@@ -12,22 +12,26 @@ - - #include - -+#include "swift/Runtime/Config.h" - #include "swift/Threading/Mutex.h" - - namespace swift { - // return the size in words for the given mutex primitive --extern "C" -+extern "C" SWIFT_CC(swift) - size_t _swift_async_stream_lock_size() { - size_t words = sizeof(Mutex) / sizeof(void *); - if (words < 1) { return 1; } - return words; - } - -+SWIFT_CC(swift) - extern "C" void _swift_async_stream_lock_init(Mutex &lock) { - new (&lock) Mutex(); - } - -+SWIFT_CC(swift) - extern "C" void _swift_async_stream_lock_lock(Mutex &lock) { lock.lock(); } - -+SWIFT_CC(swift) - extern "C" void _swift_async_stream_lock_unlock(Mutex &lock) { lock.unlock(); } - } -diff --git a/stdlib/public/Concurrency/TaskGroup.cpp b/stdlib/public/Concurrency/TaskGroup.cpp -index f121b24371b..1c88d9030d7 100644 ---- a/stdlib/public/Concurrency/TaskGroup.cpp -+++ b/stdlib/public/Concurrency/TaskGroup.cpp -@@ -53,6 +53,10 @@ - #include - #endif - -+#if defined(__wasi__) -+#include -+#endif -+ - #include - #if SWIFT_CONCURRENCY_ENABLE_DISPATCH - #include -diff --git a/stdlib/public/Differentiation/CMakeLists.txt b/stdlib/public/Differentiation/CMakeLists.txt -index b4bf0973912..9c1ab6ebddb 100644 ---- a/stdlib/public/Differentiation/CMakeLists.txt -+++ b/stdlib/public/Differentiation/CMakeLists.txt -@@ -40,6 +40,7 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE - SWIFT_MODULE_DEPENDS_OPENBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc -+ SWIFT_MODULE_DEPENDS_WASI WASILibc - SWIFT_MODULE_DEPENDS_WINDOWS CRT - - C_COMPILE_FLAGS -diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt -index 9063931bd20..532e3cda2fc 100644 ---- a/stdlib/public/Platform/CMakeLists.txt -+++ b/stdlib/public/Platform/CMakeLists.txt -@@ -97,6 +97,91 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O - INSTALL_IN_COMPONENT sdk-overlay - DEPENDS glibc_modulemap) - -+if(WASI IN_LIST SWIFT_SDKS) -+ set(arch_subdir "${SWIFT_SDK_WASI_LIB_SUBDIR}/wasm32") -+ set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}") -+ set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}") -+ set(wasi_libc_modulemap_out "${module_dir}/wasi.modulemap") -+ set(wasi_libc_modulemap_out_static "${module_dir_static}/wasi.modulemap") -+ set(wasi_libc_modulemap_target_list) -+ handle_gyb_source_single(wasi_libc_modulemap_target -+ SOURCE "wasi.modulemap.gyb" -+ OUTPUT "${wasi_libc_modulemap_out}" -+ FLAGS "-DLIBC_INCLUDE_PATH=${SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY}" -+ "-DLIBC_ARCH_INCLUDE_PATH=${SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY}") -+ list(APPEND wasi_libc_modulemap_target_list ${wasi_libc_modulemap_target}) -+ -+ add_custom_command_target( -+ copy_wasi_libc_modulemap_static -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "copy" ${wasi_libc_modulemap_out} ${wasi_libc_modulemap_out_static} -+ OUTPUT ${wasi_libc_modulemap_out_static} -+ DEPENDS -+ "${wasi_libc_modulemap_target}" -+ COMMENT "Copying WASILlibc modulemap to static resources") -+ -+ list(APPEND wasi_libc_modulemap_target_list ${copy_wasi_libc_modulemap_static}) -+ -+ set(wasi_libc_header_out "${module_dir}/SwiftWASILibc.h") -+ set(wasi_libc_header_out_static "${module_dir_static}/SwiftWASILibc.h") -+ handle_gyb_source_single(wasi_libc_header_target -+ SOURCE "SwiftWASILibc.h.gyb" -+ OUTPUT "${wasi_libc_header_out}" -+ FLAGS "-DCMAKE_SDK=${sdk}") -+ list(APPEND wasi_libc_modulemap_target_list ${wasi_libc_header_target}) -+ -+ add_custom_command_target( -+ copy_wasi_libc_header_static -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "copy" ${wasi_libc_header_out} ${wasi_libc_header_out_static} -+ OUTPUT ${wasi_libc_header_out_static} -+ DEPENDS -+ "${wasi_libc_header_target}" -+ COMMENT "Copying WASILlibc header to static resources") -+ list(APPEND wasi_libc_modulemap_target_list ${copy_wasi_libc_header_static}) -+ -+ add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY -+ ${swift_platform_sources} -+ POSIXError.swift -+ -+ GYB_SOURCES -+ ${swift_platform_gyb_sources} -+ WASI.swift.gyb -+ -+ SWIFT_COMPILE_FLAGS -+ ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} -+ ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} -+ LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" -+ TARGET_SDKS WASI -+ INSTALL_IN_COMPONENT sdk-overlay -+ DEPENDS ${wasi_libc_modulemap_target_list}) -+ -+ swift_install_in_component(FILES "${wasi_libc_modulemap_out}" -+ DESTINATION "lib/swift/${arch_subdir}" -+ COMPONENT sdk-overlay) -+ -+ swift_install_in_component(FILES "${wasi_libc_modulemap_out}" -+ DESTINATION "lib/swift_static/${arch_subdir}" -+ COMPONENT sdk-overlay) -+ -+ swift_install_in_component(FILES "${wasi_libc_header_out}" -+ DESTINATION "lib/swift/${arch_subdir}" -+ COMPONENT sdk-overlay) -+ -+ swift_install_in_component(FILES "${wasi_libc_header_out}" -+ DESTINATION "lib/swift_static/${arch_subdir}" -+ COMPONENT sdk-overlay) -+ -+ add_custom_target(wasi_libc_modulemap -+ DEPENDS ${wasi_libc_modulemap_target_list}) -+ set_property(TARGET wasi_libc_modulemap PROPERTY FOLDER "Miscellaneous") -+ add_dependencies(sdk-overlay wasi_libc_modulemap) -+endif() -+ - add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY - ucrt.swift - ${swift_platform_sources} -diff --git a/stdlib/public/Platform/SwiftWASILibc.h.gyb b/stdlib/public/Platform/SwiftWASILibc.h.gyb -new file mode 100644 -index 00000000000..86fc25b24c8 ---- /dev/null -+++ b/stdlib/public/Platform/SwiftWASILibc.h.gyb -@@ -0,0 +1,112 @@ -+%{ -+headers = [ -+ 'stdc-predef.h', -+ 'features.h', -+ -+ # C standard library -+ 'complex.h', -+ 'ctype.h', -+ 'errno.h', -+ 'fenv.h', -+ 'float.h', -+ 'inttypes.h', -+ 'iso646.h', -+ 'libutil.h', -+ 'limits.h', -+ 'locale.h', -+ 'math.h', -+ 'pty.h', -+ # setjmp/longjmp is not available -+ # 'setjmp.h', -+ 'signal.h', -+ 'stdarg.h', -+ 'stdbool.h', -+ 'stddef.h', -+ 'stdint.h', -+ 'stdio.h', -+ 'stdlib.h', -+ 'string.h', -+ 'tgmath.h', -+ 'time.h', -+ 'util.h', -+ 'utmp.h', -+ -+ # POSIX -+ 'aio.h', -+ 'arpa/inet.h', -+ 'bsd/ifaddrs.h', -+ 'bsd/pty.h', -+ 'cpio.h', -+ 'dirent.h', -+ 'dlfcn.h', -+ 'fcntl.h', -+ 'fmtmsg.h', -+ 'fnmatch.h', -+ 'ftw.h', -+ 'glob.h', -+ 'grp.h', -+ 'iconv.h', -+ 'ifaddrs.h', -+ 'langinfo.h', -+ 'libgen.h', -+ 'link.h', -+ 'monetary.h', -+ 'net/if.h', -+ 'netdb.h', -+ 'netinet/in.h', -+ 'netinet/tcp.h', -+ 'nl_types.h', -+ 'poll.h', -+ 'pthread.h', -+ 'pwd.h', -+ 'regex.h', -+ 'sched.h', -+ 'search.h', -+ 'semaphore.h', -+ 'spawn.h', -+ 'strings.h', -+ 'sys/event.h', -+ 'sys/file.h', -+ 'sys/inotify.h', -+ 'sys/ioctl.h', -+ 'sys/ipc.h', -+ 'sys/mman.h', -+ 'sys/mount.h', -+ 'sys/msg.h', -+ 'sys/resource.h', -+ 'sys/select.h', -+ 'sys/sem.h', -+ 'sys/sendfile.h', -+ 'sys/shm.h', -+ 'sys/socket.h', -+ 'sys/stat.h', -+ 'sys/statvfs.h', -+ 'sys/time.h', -+ 'sys/times.h', -+ 'sys/types.h', -+ 'sys/uio.h', -+ 'sys/un.h', -+ 'sys/user.h', -+ 'sys/utsname.h', -+ 'sys/wait.h', -+ 'sysexits.h', -+ 'syslog.h', -+ 'tar.h', -+ 'termios.h', -+ 'ulimit.h', -+ 'unistd.h', -+ 'utime.h', -+ 'utmpx.h', -+ 'wait.h', -+ 'wordexp.h', -+ -+ # WASI specific -+ 'wasi/api.h', -+] -+}% -+ -+% for header in headers: -+#if __has_include(<${header}>) -+#include <${header}> -+#endif -+% end -diff --git a/stdlib/public/Platform/WASI.swift.gyb b/stdlib/public/Platform/WASI.swift.gyb -new file mode 100644 -index 00000000000..a4379816ad6 ---- /dev/null -+++ b/stdlib/public/Platform/WASI.swift.gyb -@@ -0,0 +1,98 @@ -+//===----------------------------------------------------------------------===// -+// -+// This source file is part of the Swift.org open source project -+// -+// Copyright (c) 2020 Apple Inc. and the Swift project authors -+// Licensed under Apache License v2.0 with Runtime Library Exception -+// -+// See https://swift.org/LICENSE.txt for license information -+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -+// -+//===----------------------------------------------------------------------===// -+ -+@_exported import SwiftWASILibc // Clang module -+ -+// Constants defined by -+@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.") -+public let M_PI = Double.pi -+ -+@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.") -+public let M_PI_2 = Double.pi / 2 -+ -+@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.") -+public let M_PI_4 = Double.pi / 4 -+ -+@available(swift, deprecated: 3.0, message: "Please use '2.squareRoot()'.") -+public let M_SQRT2 = 2.squareRoot() -+ -+@available(swift, deprecated: 3.0, message: "Please use '0.5.squareRoot()'.") -+public let M_SQRT1_2 = 0.5.squareRoot() -+ -+// Constants defined by -+@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.") -+public let FLT_RADIX = Double.radix -+ -+%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL')]: -+// Where does the 1 come from? C counts the usually-implicit leading -+// significand bit, but Swift does not. Neither is really right or wrong. -+@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.") -+public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1 -+ -+// Where does the 1 come from? C models floating-point numbers as having a -+// significand in [0.5, 1), but Swift (following IEEE 754) considers the -+// significand to be in [1, 2). This rationale applies to ${prefix}_MIN_EXP -+// as well. -+@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.") -+public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1 -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.") -+public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1 -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.") -+public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.") -+public let ${prefix}_EPSILON = ${type}.ulpOfOne -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.") -+public let ${prefix}_MIN = ${type}.leastNormalMagnitude -+ -+@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.") -+public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude -+ -+%end -+ -+public let MAP_FAILED: UnsafeMutableRawPointer! = UnsafeMutableRawPointer(bitPattern: -1) -+ -+// wasi-libc's error.h uses a macro that Swift can't import. -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EACCES'.") -+public let EACCES: Int32 = 2 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EDQUOT'.") -+public let EDQUOT: Int32 = 19 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EEXIST'.") -+public let EEXIST: Int32 = 20 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EFBIG'.") -+public let EFBIG: Int32 = 22 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EINTR'.") -+public let EINTR: Int32 = 27 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EINVAL'.") -+public let EINVAL: Int32 = 28 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.ENAMETOOLONG'.") -+public let ENAMETOOLONG: Int32 = 37 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.ENOENT'.") -+public let ENOENT: Int32 = 44 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.ENOSPC'.") -+public let ENOSPC: Int32 = 51 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EPERM'.") -+public let EPERM: Int32 = 63 -+@available(*, deprecated, message: "Please use 'POSIXErrorCode.EROFS'.") -+public let EROFS: Int32 = 69 -+ -+ -+// wasi-libc's _seek.h uses a macro that Swift can't import. -+// https://developer.apple.com/documentation/swift/using-imported-c-macros-in-swift -+// https://github.com/apple/swift/blob/c55b9cc87925c5d63a21d383ad23dff056d36607/lib/ClangImporter/ImportName.cpp#L2115-L2117 -+// https://github.com/WebAssembly/wasi-libc/pull/148 -+public let SEEK_SET: Int32 = 0 -+public let SEEK_CUR: Int32 = 1 -+public let SEEK_END: Int32 = 2 -diff --git a/stdlib/public/Platform/wasi.modulemap.gyb b/stdlib/public/Platform/wasi.modulemap.gyb -new file mode 100644 -index 00000000000..c51c81f61c3 ---- /dev/null -+++ b/stdlib/public/Platform/wasi.modulemap.gyb -@@ -0,0 +1,26 @@ -+//===--- wasi.modulemap.gyb ----------------------------------------------===// -+// -+// This source file is part of the Swift.org open source project -+// -+// Copyright (c) 2020 Apple Inc. and the Swift project authors -+// Licensed under Apache License v2.0 with Runtime Library Exception -+// -+// See https://swift.org/LICENSE.txt for license information -+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -+// -+//===----------------------------------------------------------------------===// -+ -+/// This is a semi-complete modulemap that maps WASI headers in a roughly -+/// similar way to the Darwin SDK modulemap. We do not take care to list every -+/// single header which may be included by a particular submodule, so there can -+/// still be issues if imported into the same context as one in which someone -+/// included those headers directly. -+/// -+/// It's not named just WASI so that it doesn't conflict in the event of a -+/// future official WASI modulemap. -+module SwiftWASILibc [system] { -+ // C standard library -+ header "SwiftWASILibc.h" -+ -+ export * -+} -diff --git a/stdlib/public/SwiftShims/swift/shims/HeapObject.h b/stdlib/public/SwiftShims/swift/shims/HeapObject.h -index 6c0a0fe9558..56440cdb5e8 100644 ---- a/stdlib/public/SwiftShims/swift/shims/HeapObject.h -+++ b/stdlib/public/SwiftShims/swift/shims/HeapObject.h -@@ -81,17 +81,25 @@ struct HeapObject { - #ifdef __cplusplus - extern "C" { - #endif -+#if __has_attribute(swiftcall) -+#define SWIFT_CC_swift __attribute__((swiftcall)) -+#else -+#define SWIFT_CC_swift -+#endif - - SWIFT_RUNTIME_STDLIB_API - void _swift_instantiateInertHeapObject(void *address, - const HeapMetadata *metadata); - -+SWIFT_CC_swift - SWIFT_RUNTIME_STDLIB_API - __swift_size_t swift_retainCount(HeapObject *obj); - -+SWIFT_CC_swift - SWIFT_RUNTIME_STDLIB_API - __swift_size_t swift_unownedRetainCount(HeapObject *obj); - -+SWIFT_CC_swift - SWIFT_RUNTIME_STDLIB_API - __swift_size_t swift_weakRetainCount(HeapObject *obj); - -diff --git a/stdlib/public/WASI/CMakeLists.txt b/stdlib/public/WASI/CMakeLists.txt -new file mode 100644 -index 00000000000..0d088443ad7 ---- /dev/null -+++ b/stdlib/public/WASI/CMakeLists.txt -@@ -0,0 +1,4 @@ -+add_swift_target_library(swiftWasiPthread STATIC IS_STDLIB -+ Pthread.cpp -+ TARGET_SDKS WASI -+ INSTALL_IN_COMPONENT stdlib) -diff --git a/stdlib/public/WASI/Pthread.cpp b/stdlib/public/WASI/Pthread.cpp -new file mode 100644 -index 00000000000..5a64d9bdb4c ---- /dev/null -+++ b/stdlib/public/WASI/Pthread.cpp -@@ -0,0 +1,153 @@ -+// SPDX-License-Identifier: 0BSD -+// prototypes taken from opengroup -+#include -+#include -+#include -+#include -+ -+#define STUB() do {fprintf(stderr, "FakePthread: unsupported %s\n", __func__);abort();}while(0) -+ -+// mutexes: just no-ops -+ -+int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { -+ return 0; -+} -+ -+int pthread_mutex_destroy(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+int pthread_mutexattr_init(pthread_mutexattr_t *attr) { -+ return 0; -+} -+ -+int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { -+ return 0; -+} -+ -+int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) { -+ return 0; -+} -+ -+int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { -+ return 0; -+} -+ -+int pthread_mutex_lock(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+int pthread_mutex_trylock(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+int pthread_mutex_unlock(pthread_mutex_t *mutex) { -+ return 0; -+} -+ -+// pthread_cond: STUB -+ -+int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { -+ return 0; -+} -+ -+int pthread_cond_destroy(pthread_cond_t *cond) { -+ return 0; -+} -+ -+int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { -+ STUB(); -+} -+ -+int pthread_cond_timedwait(pthread_cond_t *cond, -+ pthread_mutex_t *mutex, const struct timespec *abstime) { -+ STUB(); -+} -+ -+int pthread_cond_broadcast(pthread_cond_t *cond) { -+ return 0; -+} -+ -+int pthread_cond_signal(pthread_cond_t *cond) { -+ return 0; -+} -+ -+// tls -+ -+int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)) { -+ STUB(); -+} -+ -+void *pthread_getspecific(pthread_key_t key) { -+ STUB(); -+} -+ -+int pthread_setspecific(pthread_key_t key, const void *value) { -+ STUB(); -+} -+ -+// threads -+ -+pthread_t pthread_self() { -+ return (pthread_t)1234; -+} -+ -+#undef pthread_equal -+ -+int pthread_equal(pthread_t t1, pthread_t t2) { -+ return t1 == t2; -+} -+ -+int pthread_join(pthread_t thread, void **value_ptr) { -+ STUB(); -+} -+ -+int pthread_detach(pthread_t thread) { -+ STUB(); -+} -+ -+int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { -+ return 0; -+} -+ -+// once -+ -+int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { -+ STUB(); -+} -+ -+// rwlock -+ -+int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { -+ return 0; -+} -+ -+int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { -+ return 0; -+} -+ -+// named semaphores -+ -+sem_t *sem_open(const char *name, int oflag, ...) { -+ STUB(); -+} -diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt -index e46c37d626b..be7ebffe760 100644 ---- a/stdlib/public/runtime/CMakeLists.txt -+++ b/stdlib/public/runtime/CMakeLists.txt -@@ -75,6 +75,7 @@ set(swift_runtime_sources - set(LLVM_OPTIONAL_SOURCES - SwiftRT-COFF.cpp - SwiftRT-ELF.cpp -+ SwiftRT-WASM.cpp - ${swift_runtime_sources} - ${swift_runtime_objc_sources} - ${swift_runtime_leaks_sources}) -@@ -83,8 +84,7 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags}) - list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS) - list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/stdlib/include/llvm/Support -I${SWIFT_SOURCE_DIR}/include) - --set(sdk "${SWIFT_HOST_VARIANT_SDK}") --if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") -+macro(add_image_inspection_shared sdk primary_arch inspection_file linkfile_src) - set(static_binary_lnk_file_list) - string(TOLOWER "${sdk}" lowercase_sdk) - -@@ -93,19 +93,57 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") - add_custom_command_target(swift_static_binary_${sdk}_args - COMMAND - "${CMAKE_COMMAND}" -E copy -- "${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk" -+ "${linkfile_src}" - "${SWIFTSTATICLIB_DIR}/${linkfile}" - OUTPUT - "${SWIFTSTATICLIB_DIR}/${linkfile}" - DEPENDS -- "${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk") -+ "${linkfile_src}") - - list(APPEND static_binary_lnk_file_list ${swift_static_binary_${sdk}_args}) - swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}" - DESTINATION "lib/swift_static/${lowercase_sdk}" - COMPONENT stdlib) -- add_dependencies(stdlib ${static_binary_lnk_file_list}) -+endmacro() -+ -+set(is_image_inspection_required) -+foreach(sdk IN LISTS SWIFT_SDKS) -+ set(image_inspection_shared_sdk) -+ set(primary_arch) -+ set(image_inspection_shared_file) -+ set(linkfile_src) -+ -+ if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF") -+ list(APPEND ELFISH_SDKS ${sdk}) -+ set(linkfile_src "${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk") -+ elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") -+ set(linkfile_src "${SWIFT_SOURCE_DIR}/utils/webassembly/static-executable-args.lnk") -+ endif() -+ -+ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") -+ set(image_inspection_shared_sdk "${sdk}") -+ set(image_inspection_shared_file ImageInspectionELF.cpp) -+ elseif(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "WASI") -+ set(image_inspection_shared_sdk "${sdk}") -+ set(image_inspection_shared_file ImageInspectionWasm.cpp) -+ # Set default arch -+ set(primary_arch "wasm32") -+ endif() -+ -+ if("${sdk}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") -+ set(primary_arch ${SWIFT_PRIMARY_VARIANT_ARCH}) -+ endif() -+ -+ if(NOT "${image_inspection_shared_sdk}" STREQUAL "" AND NOT "${primary_arch}" STREQUAL "") -+ set(is_image_inspection_required TRUE) -+ add_image_inspection_shared(${image_inspection_shared_sdk} ${primary_arch} ${image_inspection_shared_file} ${linkfile_src}) -+ endif() -+endforeach() -+ -+if(is_image_inspection_required) - add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list}) -+ add_dependencies(stdlib static_binary_magic) -+ add_dependencies(stdlib ${static_binary_lnk_file_list}) - endif() - - add_swift_target_library(swiftRuntime OBJECT_LIBRARY -@@ -120,11 +158,14 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY - - set(ELFISH_SDKS) - set(COFF_SDKS) -+set(WASM_SDKS) - foreach(sdk ${SWIFT_SDKS}) - if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF") - list(APPEND ELFISH_SDKS ${sdk}) - elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF") - list(APPEND COFF_SDKS ${sdk}) -+ elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") -+ list(APPEND WASM_SDKS ${sdk}) - endif() - endforeach() - -@@ -140,6 +181,16 @@ add_swift_target_library(swiftImageRegistrationObjectELF - TARGET_SDKS ${ELFISH_SDKS} - SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT none) -+ -+add_swift_target_library(swiftImageRegistrationObjectWASM -+ OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE -+ SwiftRT-WASM.cpp -+ C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS} -+ LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS} -+ TARGET_SDKS ${WASM_SDKS} -+ SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} -+ INSTALL_IN_COMPONENT none) -+ - # FIXME(compnerd) this should be compiled twice, once for static and once for - # shared. The static version should be used for building the standard library. - add_swift_target_library(swiftImageRegistrationObjectCOFF -@@ -157,7 +208,8 @@ foreach(sdk ${SWIFT_SDKS}) - set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - - if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR -- "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF") -+ "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF" OR -+ "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") - # TODO(compnerd) switch to the generator expression when cmake is upgraded - # to a version which supports it. - # set(swiftrtObject "$") -@@ -207,10 +259,31 @@ foreach(sdk ${SWIFT_SDKS}) - add_dependencies(stdlib swift-stdlib-${arch_suffix} swiftImageRegistration-${arch_suffix}) - endif() - -+ string(TOLOWER "${sdk}" lowercase_sdk) - # Generate the static-stdlib-args.lnk file used by -static-stdlib option for - # 'GenericUnix' (eg linux) - if(${SWIFT_SDK_${sdk}_OBJECT_FORMAT} STREQUAL ELF) - string(TOLOWER "${sdk}" lowercase_sdk) -+ if(SWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_STATICLIB) -+ set(libicu_i18n_a -licui18nswift) -+ set(libicu_uc_a -licuucswift) -+ set(libicu_data_a -licudataswift) -+ else() -+ find_package(ICU REQUIRED COMPONENTS uc i18n) -+ get_filename_component(ICU_UC_LIBDIR "${ICU_UC_LIBRARIES}" DIRECTORY) -+ get_filename_component(ICU_I18N_LIBDIR "${ICU_I18N_LIBRARIES}" DIRECTORY) -+ -+ set(libicu_i18n_a -licui18n) -+ set(libicu_uc_a -licuuc) -+ set(libicu_data_a) -+ if(EXISTS ${ICU_I18N_LIBDIR}/libicui18n.a AND -+ EXISTS ${ICU_UC_LIBDIR}/libicuuc.a AND -+ EXISTS ${ICU_UC_LIBDIR}/libicudata.a) -+ set(libicu_i18n_a ${ICU_I18N_LIBDIR}/libicui18n.a) -+ set(libicu_uc_a ${ICU_UC_LIBDIR}/libicuuc.a) -+ set(libicu_data_a ${ICU_UC_LIBDIR}/libicudata.a) -+ endif() -+ endif() - set(libpthread -lpthread) - set(concurrency_libs) - set(android_libraries) -@@ -237,6 +310,45 @@ ${concurrency_libs} - swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}" - DESTINATION "lib/swift_static/${lowercase_sdk}" - COMPONENT stdlib) -+ elseif("${sdk}" STREQUAL "WASI") -+ set(linkfile "${lowercase_sdk}/static-stdlib-args.lnk") -+ file(WRITE "${SWIFTSTATICLIB_DIR}/${linkfile}" " -+-ldl -+-latomic -+-lswiftWasiPthread -+-lswiftCore -+-licuuc -+-licudata -+-lstdc++ -+-lm -+-Xlinker --exclude-libs -+-Xlinker ALL") -+ -+ swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}" -+ DESTINATION "lib/swift_static/${lowercase_sdk}" -+ COMPONENT stdlib) -+ -+ set(swift_icu_libs_wasi_list) -+ set(icu_modules UC I18N DATA) -+ foreach(module IN LISTS icu_modules) -+ set(module_lib "${SWIFT_WASI_wasm32_ICU_${module}}") -+ get_filename_component(module_lib_name ${module_lib} NAME) -+ add_custom_command_target(swift_icu_${module}_${sdk} -+ COMMAND -+ "${CMAKE_COMMAND}" -E copy -+ "${module_lib}" -+ "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${module_lib_name}" -+ OUTPUT -+ "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${module_lib_name}" -+ DEPENDS -+ "${module_lib}") -+ list(APPEND swift_icu_libs_wasi_list ${swift_icu_${module}_${sdk}}) -+ swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${module_lib_name}" -+ DESTINATION "lib/swift_static/${lowercase_sdk}" -+ COMPONENT stdlib) -+ endforeach() -+ add_custom_target(swift_icu_libs_wasi ALL DEPENDS ${swift_icu_libs_wasi_list}) -+ add_dependencies(stdlib swift_icu_libs_wasi) - endif() - endforeach() - endforeach() -diff --git a/stdlib/public/runtime/Demangle.cpp b/stdlib/public/runtime/Demangle.cpp -index d6125d9a3b3..765e93ada51 100644 ---- a/stdlib/public/runtime/Demangle.cpp -+++ b/stdlib/public/runtime/Demangle.cpp -@@ -718,6 +718,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type, - /// \returns the demangled name. Returns nullptr if the input String is not a - /// Swift mangled name. - SWIFT_RUNTIME_EXPORT -+SWIFT_CC(swift) - char *swift_demangle(const char *mangledName, - size_t mangledNameLength, - char *outputBuffer, -diff --git a/stdlib/public/runtime/EnvironmentVariables.cpp b/stdlib/public/runtime/EnvironmentVariables.cpp -index 5aa91f23e49..c534901b1b1 100644 ---- a/stdlib/public/runtime/EnvironmentVariables.cpp -+++ b/stdlib/public/runtime/EnvironmentVariables.cpp -@@ -237,6 +237,7 @@ void swift::runtime::environment::initialize(void *context) { - } - #endif - -+SWIFT_CC(swift) - SWIFT_RUNTIME_EXPORT - bool swift_COWChecksEnabled() { - return runtime::environment::SWIFT_DEBUG_ENABLE_COW_CHECKS(); -diff --git a/stdlib/public/runtime/Exclusivity.cpp b/stdlib/public/runtime/Exclusivity.cpp -index e0cb4902c49..9a2c96f0dcc 100644 ---- a/stdlib/public/runtime/Exclusivity.cpp -+++ b/stdlib/public/runtime/Exclusivity.cpp -@@ -37,7 +37,9 @@ - #include - - // Pick a return-address strategy --#if __GNUC__ -+#if defined(__wasm__) -+#define get_return_address() ((void*) 0) -+#elif __GNUC__ - #define get_return_address() __builtin_return_address(0) - #elif _MSC_VER - #include -diff --git a/stdlib/public/runtime/Heap.cpp b/stdlib/public/runtime/Heap.cpp -index 8c761c9d59f..a44b6b30195 100644 ---- a/stdlib/public/runtime/Heap.cpp -+++ b/stdlib/public/runtime/Heap.cpp -@@ -32,10 +32,11 @@ using namespace swift; - /// On Apple platforms, \c malloc() is always 16-byte aligned. - static constexpr size_t MALLOC_ALIGN_MASK = 15; - --#elif defined(__linux__) || defined(_WIN32) -+#elif defined(__linux__) || defined(_WIN32) || defined(__wasi__) - /// On Linux and Windows, \c malloc() returns 16-byte aligned pointers on 64-bit - /// and 8-byte aligned pointers on 32-bit. --#if defined(__LP64) || defined(_WIN64) -+/// On wasi-libc, pointers are 16-byte aligned even though 32-bit for SIMD access. -+#if defined(__LP64) || defined(_WIN64) || defined(__wasi__) - static constexpr size_t MALLOC_ALIGN_MASK = 15; - #else - static constexpr size_t MALLOC_ALIGN_MASK = 7; -diff --git a/stdlib/public/runtime/HeapObject.cpp b/stdlib/public/runtime/HeapObject.cpp -index 4421ae69eed..50d9f4512ec 100644 ---- a/stdlib/public/runtime/HeapObject.cpp -+++ b/stdlib/public/runtime/HeapObject.cpp -@@ -257,6 +257,7 @@ public: - - static SimpleGlobalCache Boxes; - -+SWIFT_CC(swift) - BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type, - size_t alignMask) { - auto *inlineBuffer = reinterpret_cast(buffer); -@@ -282,6 +283,7 @@ BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type, - } - } - -+SWIFT_CC(swift) - BoxPair swift::swift_allocBox(const Metadata *type) { - // Get the heap metadata for the box. - auto metadata = &Boxes.getOrInsert(type).first->Data; -@@ -452,16 +454,19 @@ void swift::swift_nonatomic_release_n(HeapObject *object, uint32_t n) { - object->refCounts.decrementAndMaybeDeinitNonAtomic(n); - } - -+SWIFT_CC(swift) - size_t swift::swift_retainCount(HeapObject *object) { - if (isValidPointerForNativeRetain(object)) - return object->refCounts.getCount(); - return 0; - } - -+SWIFT_CC(swift) - size_t swift::swift_unownedRetainCount(HeapObject *object) { - return object->refCounts.getUnownedCount(); - } - -+SWIFT_CC(swift) - size_t swift::swift_weakRetainCount(HeapObject *object) { - return object->refCounts.getWeakCount(); - } -@@ -966,6 +971,7 @@ WeakReference *swift::swift_weakTakeAssign(WeakReference *dest, - /// Returns true if the "immutable" flag is set on \p object. - /// - /// Used for runtime consistency checking of COW buffers. -+SWIFT_CC(swift) - SWIFT_RUNTIME_EXPORT - bool _swift_isImmutableCOWBuffer(HeapObject *object) { - return object->refCounts.isImmutableCOWBuffer(); -@@ -975,6 +981,7 @@ bool _swift_isImmutableCOWBuffer(HeapObject *object) { - /// value of the flag. - /// - /// Used for runtime consistency checking of COW buffers. -+SWIFT_CC(swift) - SWIFT_RUNTIME_EXPORT - bool _swift_setImmutableCOWBuffer(HeapObject *object, bool immutable) { - return object->refCounts.setIsImmutableCOWBuffer(immutable); -diff --git a/stdlib/public/runtime/Numeric.cpp b/stdlib/public/runtime/Numeric.cpp -index ac3933e6b62..40421d0336e 100644 ---- a/stdlib/public/runtime/Numeric.cpp -+++ b/stdlib/public/runtime/Numeric.cpp -@@ -50,10 +50,12 @@ static T convert(IntegerLiteral value) { - return result; - } - -+SWIFT_CC(swift) - float swift::swift_intToFloat32(IntegerLiteral value) { - return convert(value); - } - -+SWIFT_CC(swift) - double swift::swift_intToFloat64(IntegerLiteral value) { - return convert(value); - } -diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp -index bf5812b4ba1..51a9ab16b11 100644 ---- a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp -+++ b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp -@@ -130,7 +130,7 @@ static std::uint16_t RuntimeFunctionCountersOffsets[] = { - /// Public APIs - - /// Get the runtime object state associated with an object. --void _swift_getObjectRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_getObjectRuntimeFunctionCounters( - HeapObject *object, RuntimeFunctionCountersState *result) { - auto &theSentinel = RuntimeObjectStateCache.get(); - Mutex::ScopedLock lock(theSentinel.Lock); -@@ -139,7 +139,7 @@ void _swift_getObjectRuntimeFunctionCounters( - - /// Set the runtime object state associated with an object from a provided - /// state. --void _swift_setObjectRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_setObjectRuntimeFunctionCounters( - HeapObject *object, RuntimeFunctionCountersState *state) { - auto &theSentinel = RuntimeObjectStateCache.get(); - Mutex::ScopedLock lock(theSentinel.Lock); -@@ -148,14 +148,14 @@ void _swift_setObjectRuntimeFunctionCounters( - - /// Get the global runtime state containing the total numbers of invocations for - /// each runtime function of interest. --void _swift_getGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_getGlobalRuntimeFunctionCounters( - RuntimeFunctionCountersState *result) { - LazyMutex::ScopedLock lock(RuntimeGlobalFunctionCountersState.Lock); - *result = RuntimeGlobalFunctionCountersState.State; - } - - /// Set the global runtime state of function pointers from a provided state. --void _swift_setGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) void _swift_setGlobalRuntimeFunctionCounters( - RuntimeFunctionCountersState *state) { - LazyMutex::ScopedLock lock(RuntimeGlobalFunctionCountersState.Lock); - RuntimeGlobalFunctionCountersState.State = *state; -@@ -164,18 +164,20 @@ void _swift_setGlobalRuntimeFunctionCounters( - /// Return the names of the runtime functions being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeObjectState structure. All these strings are null terminated. --const char **_swift_getRuntimeFunctionNames() { -+SWIFT_CC(swift) const char **_swift_getRuntimeFunctionNames() { - return RuntimeFunctionNames; - } - - /// Return the offsets of the runtime function counters being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeObjectState structure. -+SWIFT_CC(swift) - const std::uint16_t *_swift_getRuntimeFunctionCountersOffsets() { - return RuntimeFunctionCountersOffsets; - } - - /// Return the number of runtime functions being tracked. -+SWIFT_CC(swift) - std::uint64_t _swift_getNumRuntimeFunctionCounters() { - return ID_LastRuntimeFunctionName; - } -@@ -204,7 +206,7 @@ void _swift_dumpObjectsRuntimeFunctionPointers() { - - /// Set mode for global runtime function counters. - /// Return the old value of this flag. --int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { -+SWIFT_CC(swift) int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { - int oldMode = UpdateGlobalRuntimeFunctionCounters; - UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0; - return oldMode; -@@ -212,7 +214,7 @@ int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { - - /// Set mode for per object runtime function counters. - /// Return the old value of this flag. --int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { -+SWIFT_CC(swift) int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { - int oldMode = UpdatePerObjectRuntimeFunctionCounters; - UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0; - return oldMode; -diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.h b/stdlib/public/runtime/RuntimeInvocationsTracking.h -index 74d60917656..3005df2b79c 100644 ---- a/stdlib/public/runtime/RuntimeInvocationsTracking.h -+++ b/stdlib/public/runtime/RuntimeInvocationsTracking.h -@@ -61,47 +61,47 @@ using RuntimeFunctionCountersUpdateHandler = - - /// Get the runtime object state associated with an object and store it - /// into the result. --SWIFT_RUNTIME_EXPORT void -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void - _swift_getObjectRuntimeFunctionCounters(HeapObject *object, - RuntimeFunctionCountersState *result); - - /// Get the global runtime state containing the total numbers of invocations for - /// each runtime function of interest and store it into the result. --SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( - swift::RuntimeFunctionCountersState *result); - - /// Return the names of the runtime functions being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeObjectState structure. --SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); - - /// Return the offsets of the runtime function counters being tracked. - /// Their order is the same as the order of the counters in the - /// RuntimeFunctionCountersState structure. --SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); - - /// Return the number of runtime functions being tracked. --SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); - - /// Dump all per-object runtime function pointers. - SWIFT_RUNTIME_EXPORT void _swift_dumpObjectsRuntimeFunctionPointers(); - - /// Set mode for global runtime function counters. - /// Return the old value of this flag. --SWIFT_RUNTIME_EXPORT int -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int - _swift_setPerObjectRuntimeFunctionCountersMode(int mode); - - /// Set mode for per object runtime function counters. - /// Return the old value of this flag. --SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); - - /// Set the global runtime state of function pointers from a provided state. --SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( - swift::RuntimeFunctionCountersState *state); - - /// Set the runtime object state associated with an object from a provided - /// state. --SWIFT_RUNTIME_EXPORT void -+SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void - _swift_setObjectRuntimeFunctionCounters(HeapObject *object, - RuntimeFunctionCountersState *state); - -diff --git a/stdlib/public/runtime/SwiftRT-WASM.cpp b/stdlib/public/runtime/SwiftRT-WASM.cpp -new file mode 100644 -index 00000000000..55dbe7aa851 ---- /dev/null -+++ b/stdlib/public/runtime/SwiftRT-WASM.cpp -@@ -0,0 +1,79 @@ -+//===--- SwiftRT-WASM.cpp --------------------------------------------------===// -+// -+// This source file is part of the Swift.org open source project -+// -+// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -+// Licensed under Apache License v2.0 with Runtime Library Exception -+// -+// See https://swift.org/LICENSE.txt for license information -+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -+// -+//===----------------------------------------------------------------------===// -+ -+#include "ImageInspectionCommon.h" -+#include "swift/shims/MetadataSections.h" -+ -+#include -+#include -+ -+// Link start/stop symbols weakly to link them if they aren't synthesized by the linker. -+#define DECLARE_SWIFT_SECTION(name) \ -+ __attribute__((__visibility__("hidden"),__aligned__(1),weak)) extern const char __start_##name; \ -+ __attribute__((__visibility__("hidden"),__aligned__(1),weak)) extern const char __stop_##name; -+ -+extern "C" { -+DECLARE_SWIFT_SECTION(swift5_protocols) -+DECLARE_SWIFT_SECTION(swift5_protocol_conformances) -+DECLARE_SWIFT_SECTION(swift5_type_metadata) -+ -+DECLARE_SWIFT_SECTION(swift5_typeref) -+DECLARE_SWIFT_SECTION(swift5_reflstr) -+DECLARE_SWIFT_SECTION(swift5_fieldmd) -+DECLARE_SWIFT_SECTION(swift5_assocty) -+DECLARE_SWIFT_SECTION(swift5_replace) -+DECLARE_SWIFT_SECTION(swift5_replac2) -+DECLARE_SWIFT_SECTION(swift5_builtin) -+DECLARE_SWIFT_SECTION(swift5_capture) -+DECLARE_SWIFT_SECTION(swift5_mpenum) -+DECLARE_SWIFT_SECTION(swift5_accessible_functions) -+} -+ -+#undef DECLARE_SWIFT_SECTION -+ -+namespace { -+static swift::MetadataSections sections{}; -+} -+ -+__attribute__((__constructor__)) -+static void swift_image_constructor() { -+#define SWIFT_SECTION_RANGE(name) \ -+ { reinterpret_cast(&__start_##name), \ -+ static_cast(&__stop_##name - &__start_##name) } -+ -+ new (§ions) swift::MetadataSections { -+ swift::CurrentSectionMetadataVersion, -+ { 0 }, -+ -+ nullptr, -+ nullptr, -+ -+ SWIFT_SECTION_RANGE(swift5_protocols), -+ SWIFT_SECTION_RANGE(swift5_protocol_conformances), -+ SWIFT_SECTION_RANGE(swift5_type_metadata), -+ -+ SWIFT_SECTION_RANGE(swift5_typeref), -+ SWIFT_SECTION_RANGE(swift5_reflstr), -+ SWIFT_SECTION_RANGE(swift5_fieldmd), -+ SWIFT_SECTION_RANGE(swift5_assocty), -+ SWIFT_SECTION_RANGE(swift5_replace), -+ SWIFT_SECTION_RANGE(swift5_replac2), -+ SWIFT_SECTION_RANGE(swift5_builtin), -+ SWIFT_SECTION_RANGE(swift5_capture), -+ SWIFT_SECTION_RANGE(swift5_mpenum), -+ SWIFT_SECTION_RANGE(swift5_accessible_functions), -+ }; -+ -+#undef SWIFT_SECTION_RANGE -+ -+ swift_addNewDSOImage(§ions); -+} -diff --git a/stdlib/public/stubs/Random.cpp b/stdlib/public/stubs/Random.cpp -index 460e12e8656..c694f0508d4 100644 ---- a/stdlib/public/stubs/Random.cpp -+++ b/stdlib/public/stubs/Random.cpp -@@ -45,6 +45,7 @@ - #endif - - #include -+#include - - #include "swift/shims/Random.h" - #include "swift/Runtime/Debug.h" -diff --git a/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.cpp b/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.cpp -index 5946e57596c..c854aeea431 100644 ---- a/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.cpp -+++ b/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.cpp -@@ -75,6 +75,14 @@ size_t BitVector::count() const { - return total; - } - -+void BitVector::addPointer(uintptr_t byte) { -+#if __POINTER_WIDTH__ == 64 -+ add((uint64_t)byte); -+#else -+ add((uint32_t)byte); -+#endif -+} -+ - void BitVector::add(uint64_t byte) { - for (size_t i = 0; i < 64; i++) - data.push_back(byte >> (63 - i) & 0x1); -@@ -391,7 +399,7 @@ BitVector MultiPayloadEnum::spareBits() const { - - static BitVector pointerSpareBitMask() { - BitVector bv; -- bv.add(heap_object_abi::SwiftSpareBitsMask); -+ bv.addPointer(heap_object_abi::SwiftSpareBitsMask); - return bv; - } - -diff --git a/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.h b/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.h -index 328081a0c06..3c3d98ef664 100644 ---- a/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.h -+++ b/stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.h -@@ -159,6 +159,9 @@ struct BitVector { - /// Append on a 64bit value - void add(uint64_t values); - -+ /// Append on a pointer-size value -+ void addPointer(uintptr_t values); -+ - /// Append on a vector of bytes - void add(std::vector values); - -diff --git a/test/AutolinkExtract/empty.swift b/test/AutolinkExtract/empty.swift -index b9ae0068c07..c5b61292709 100644 ---- a/test/AutolinkExtract/empty.swift -+++ b/test/AutolinkExtract/empty.swift -@@ -5,3 +5,4 @@ - - // CHECK-elf: -lswiftCore - // CHECK-coff: -lswiftCore -+// CHECK-wasm: -lswiftCore -diff --git a/test/AutolinkExtract/empty_archive.swift b/test/AutolinkExtract/empty_archive.swift -index ee66d676261..a94f40ad68c 100644 ---- a/test/AutolinkExtract/empty_archive.swift -+++ b/test/AutolinkExtract/empty_archive.swift -@@ -7,3 +7,4 @@ - - // CHECK-elf: -lswiftCore - // CHECK-coff: -lswiftCore -+// CHECK-wasm: -lswiftCore -diff --git a/test/AutolinkExtract/import.swift b/test/AutolinkExtract/import.swift -index 2b95be66fe0..5fe5a77ff4b 100644 ---- a/test/AutolinkExtract/import.swift -+++ b/test/AutolinkExtract/import.swift -@@ -18,4 +18,7 @@ - // CHECK-coff-DAG: -lswiftCore - // CHECK-coff-DAG: -lempty - -+// CHECK-wasm-DAG: -lswiftCore -+// CHECK-wasm-DAG: -lempty -+ - import empty -diff --git a/test/AutolinkExtract/import_archive.swift b/test/AutolinkExtract/import_archive.swift -index 5aa0ee41d74..1bd6f5323a8 100644 ---- a/test/AutolinkExtract/import_archive.swift -+++ b/test/AutolinkExtract/import_archive.swift -@@ -12,4 +12,7 @@ - // CHECK-coff-DAG: -lswiftCore - // CHECK-coff-DAG: -lempty - -+// CHECK-wasm-DAG: -lswiftCore -+// CHECK-wasm-DAG: -lempty -+ - import empty -diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt -index 03175207e5c..e2316453a94 100644 ---- a/test/CMakeLists.txt -+++ b/test/CMakeLists.txt -@@ -114,7 +114,8 @@ function(get_test_dependencies SDK result_var_name) - ("${SDK}" STREQUAL "OPENBSD") OR - ("${SDK}" STREQUAL "ANDROID") OR - ("${SDK}" STREQUAL "WINDOWS") OR -- ("${SDK}" STREQUAL "HAIKU")) -+ ("${SDK}" STREQUAL "HAIKU") OR -+ ("${SDK}" STREQUAL "WASI")) - # No extra dependencies. - else() - message(FATAL_ERROR "Unknown SDK: ${SDK}") -@@ -154,8 +155,14 @@ if(NOT SWIFT_INCLUDE_TOOLS) - ) - endif() - if(SWIFT_BUILD_STDLIB) -- list(APPEND SWIFT_LIT_ARGS -- "--param" "test_resource_dir=${SWIFTLIB_DIR}") -+ # if building only static stdlib, prefer static lib. -+ if(NOT SWIFT_BUILD_DYNAMIC_STDLIB AND SWIFT_BUILD_STATIC_STDLIB) -+ list(APPEND SWIFT_LIT_ARGS -+ "--param" "test_resource_dir=${SWIFTSTATICLIB_DIR}") -+ else() -+ list(APPEND SWIFT_LIT_ARGS -+ "--param" "test_resource_dir=${SWIFTLIB_DIR}") -+ endif() - endif() - endif() - -diff --git a/test/Concurrency/Runtime/checked_continuation.swift b/test/Concurrency/Runtime/checked_continuation.swift -index 9f9f1d81469..9869d120905 100644 ---- a/test/Concurrency/Runtime/checked_continuation.swift -+++ b/test/Concurrency/Runtime/checked_continuation.swift -@@ -16,6 +16,8 @@ struct TestError: Error {} - var tests = TestSuite("CheckedContinuation") - - if #available(SwiftStdlib 5.1, *) { -+// Disable crash tests -+#if !os(WASI) - tests.test("trap on double resume non-throwing continuation") { - expectCrashLater() - -@@ -42,7 +44,7 @@ struct TestError: Error {} - } - await task.get() - } -- -+#endif - tests.test("test withCheckedThrowingContinuation") { - let task2 = detach { - do { -diff --git a/test/Concurrency/Runtime/continuation_validation.swift b/test/Concurrency/Runtime/continuation_validation.swift -index 941fdd559eb..78c86e94647 100644 ---- a/test/Concurrency/Runtime/continuation_validation.swift -+++ b/test/Concurrency/Runtime/continuation_validation.swift -@@ -12,6 +12,7 @@ - // UNSUPPORTED: back_deploy_concurrency - // UNSUPPORTED: use_os_stdlib - // UNSUPPORTED: freestanding -+// UNSUPPORTED: OS=wasi - - import StdlibUnittest - -diff --git a/test/DebugInfo/modulecache.swift b/test/DebugInfo/modulecache.swift -index 1dceafbf15d..22d126eca7c 100644 ---- a/test/DebugInfo/modulecache.swift -+++ b/test/DebugInfo/modulecache.swift -@@ -16,7 +16,7 @@ import ClangModule - // RUN: %empty-directory(%t) - // RUN: %target-swift-frontend %s -c -g -o %t.o -module-cache-path %t -I %S/Inputs - // RUN: llvm-readobj -h %t/*/ClangModule-*.pcm | %FileCheck %s --// CHECK: Format: {{(Mach-O|ELF|elf64|COFF|elf32-littlearm)}} -+// CHECK: Format: {{(Mach-O|ELF|elf64|COFF|elf32-littlearm|WASM)}} - - // 3. Test that swift-ide-check will not share swiftc's module cache. - -diff --git a/test/Driver/profiling.swift b/test/Driver/profiling.swift -index f96cea46e33..0140cb13891 100644 ---- a/test/Driver/profiling.swift -+++ b/test/Driver/profiling.swift -@@ -6,6 +6,7 @@ - // RUN: %swiftc_driver -sdk "" -driver-print-jobs -profile-generate -target x86_64-apple-ios7.1-simulator -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOSSIM %s - // RUN: %swiftc_driver -sdk "" -driver-print-jobs -profile-generate -target arm64-apple-ios7.1 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOS %s - -+// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target wasm32-unknown-wasi %s | %FileCheck -check-prefix CHECK -check-prefix WASI %s - // RUN: %swiftc_driver -sdk "" -driver-print-jobs -profile-generate -target x86_64-apple-tvos9.0-simulator -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS %s - // RUN: %swiftc_driver -sdk "" -driver-print-jobs -profile-generate -target arm64-apple-tvos9.0 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS %s - -diff --git a/test/lit.cfg b/test/lit.cfg -index 259c7772be1..c6f22a9388e 100644 ---- a/test/lit.cfg -+++ b/test/lit.cfg -@@ -341,7 +341,6 @@ config.swift_demangle_yamldump = inferSwiftBinary('swift-demangle-yamldump') - config.swift_demangle = inferSwiftBinary('swift-demangle') - config.benchmark_o = inferSwiftBinary('Benchmark_O') - config.benchmark_driver = inferSwiftBinary('Benchmark_Driver') --config.wasmer = inferSwiftBinary('wasmer') - config.wasm_ld = inferSwiftBinary('wasm-ld') - - config.swift_utils = make_path(config.swift_src_root, 'utils') -@@ -1679,6 +1678,32 @@ elif run_os == 'wasi': - config.target_sdk_name = "wasi" - config.target_runtime = "native" - -+ # Exclude test cases that use objc-interop because clang doesn't support it -+ # with WebAssembly binary file yet. -+ def use_objc_interop(path): -+ try: -+ with open(path) as f: -+ return '-enable-objc-interop' in f.read() -+ except: -+ return False -+ def lldb_related_test(path): -+ try: -+ with open(path) as f: -+ return 'lldb-moduleimport-test' in f.read() -+ except: -+ return False -+ -+ def disabled_filenames(path): -+ matches = [] -+ for root, dirnames, filenames in os.walk(path): -+ for filename in filenames: -+ filepath = os.path.join(root, filename) -+ if use_objc_interop(filepath) or lldb_related_test(filepath): -+ matches.append(os.path.basename(filepath)) -+ return matches -+ -+ config.excludes += disabled_filenames(config.test_source_root) -+ - config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") - - config.target_build_swift = ' '.join([ -@@ -1686,12 +1711,12 @@ elif run_os == 'wasi': - '-target', config.variant_triple, - '-Xcc', '--sysroot=%s' % config.variant_sdk, - '-Xclang-linker', '--sysroot=%s' % config.variant_sdk, -- '-toolchain-stdlib-rpath', config.resource_dir_opt, -+ '-toolchain-stdlib-rpath', config.resource_dir_opt, - mcp_opt, config.swift_test_options, - config.swift_driver_test_options, swift_execution_tests_extra_flags]) - config.target_codesign = "echo" - config.target_build_swift_dylib = ( -- "%s -parse-as-library -emit-library -o '\\1'" -+ "%s -parse-as-library -emit-library -static -o '\\1'" - % (config.target_build_swift)) - config.target_add_rpath = '' - config.target_swift_frontend = ' '.join([ -@@ -1702,16 +1727,24 @@ elif run_os == 'wasi': - config.swift_test_options, config.swift_frontend_test_options]) - subst_target_swift_frontend_mock_sdk = config.target_swift_frontend - subst_target_swift_frontend_mock_sdk_after = "" -- config.target_run = '%s run --backend cranelift --' % config.wasmer -+ config.target_run = os.path.join(config.swift_utils, 'webassembly', 'wasm-run.py') -+ config.target_env_prefix = 'WASM_RUN_CHILD_' -+ - if 'interpret' in lit_config.params: - use_interpreter_for_simple_runs() - config.target_sil_opt = ( - '%s -target %s %s %s %s' % - (config.sil_opt, config.variant_triple, config.resource_dir_opt, mcp_opt, config.sil_test_options)) -+ subst_target_sil_opt_mock_sdk = config.target_sil_opt -+ subst_target_sil_opt_mock_sdk_after = "" - config.target_swift_symbolgraph_extract = ' '.join([ - config.swift_symbolgraph_extract, - '-target', config.variant_triple, - mcp_opt]) -+ config.target_swift_api_extract = ' '.join([ -+ config.swift_api_extract, -+ '-target', config.variant_triple, -+ '-sdk', shell_quote(config.variant_sdk)]) - config.target_swift_ide_test = ( - '%s -target %s %s %s %s' % - (config.swift_ide_test, config.variant_triple, config.resource_dir_opt, -@@ -2119,7 +2152,7 @@ if run_vendor != 'apple': - - if 'remote_run_host' in lit_config.params: - configure_remote_run() --elif not kIsWindows: -+elif not kIsWindows and not run_os == 'wasi': - if 'use_os_stdlib' in lit_config.params: - config.available_features.add('use_os_stdlib') - -diff --git a/test/stdlib/Error.swift b/test/stdlib/Error.swift -index 542b15f4f23..77e259621d1 100644 ---- a/test/stdlib/Error.swift -+++ b/test/stdlib/Error.swift -@@ -207,6 +207,7 @@ ErrorTests.test("test dealloc empty error box") { - } - } - -+#if !os(WASI) - var errors: [Error] = [] - - @inline(never) -@@ -249,6 +250,7 @@ ErrorTests.test("willThrow") { - expectEqual(2, errors.count) - expectEqual(SillyError.self, type(of: errors.last!)) - } -+#endif - - runAllTests() - -diff --git a/test/stdlib/FloatingPointIR.swift b/test/stdlib/FloatingPointIR.swift -index a00469c855c..00410eee5a4 100644 ---- a/test/stdlib/FloatingPointIR.swift -+++ b/test/stdlib/FloatingPointIR.swift -@@ -56,3 +56,6 @@ func testConstantFoldFloatLiterals() { - - // s390x: call swiftcc void @"$s15FloatingPointIR13acceptFloat32yySfF{{.*}}"(float 1.000000e+00) - // s390x: call swiftcc void @"$s15FloatingPointIR13acceptFloat64yySdF{{.*}}"(double 1.000000e+00) -+ -+// wasm32: call swiftcc void @"$s15FloatingPointIR13acceptFloat32yySfF{{.*}}"(float 1.000000e+00) -+// wasm32: call swiftcc void @"$s15FloatingPointIR13acceptFloat64yySdF{{.*}}"(double 1.000000e+00) -diff --git a/test/stdlib/PrintStruct.swift b/test/stdlib/PrintStruct.swift -index 9ecac235539..b616e61f517 100644 ---- a/test/stdlib/PrintStruct.swift -+++ b/test/stdlib/PrintStruct.swift -@@ -4,7 +4,10 @@ - // RUN: %target-codesign %t/main - // RUN: %target-run %t/main - // REQUIRES: executable_test --// REQUIRES: reflection -+// -+// FIXME Disable this case because this failed on only CI. -+// Failed with 'Caught exception of type "CallIndirectOOB"' -+// UNSUPPORTED: CPU=wasm32 - - import StdlibUnittest - import PrintTestTypes -diff --git a/test/stdlib/StringAPI.swift b/test/stdlib/StringAPI.swift -index 46f6326b9cb..3aed6b2949e 100644 ---- a/test/stdlib/StringAPI.swift -+++ b/test/stdlib/StringAPI.swift -@@ -342,6 +342,7 @@ StringTests.test("SameTypeComparisons") { - expectFalse(xs != xs) - } - -+#if !os(WASI) - StringTests.test("CompareStringsWithUnpairedSurrogates") - .xfail( - .always(" Strings referring to underlying " + -@@ -357,6 +358,7 @@ StringTests.test("CompareStringsWithUnpairedSurrogates") - ] - ) - } -+#endif - - StringTests.test("[String].joined() -> String") { - let s = ["hello", "world"].joined() -diff --git a/test/stdlib/StringAPICString.swift b/test/stdlib/StringAPICString.swift -index fa8f005ab37..5612d9b2a2e 100644 ---- a/test/stdlib/StringAPICString.swift -+++ b/test/stdlib/StringAPICString.swift -@@ -7,6 +7,12 @@ - - import StdlibUnittest - -+#if arch(wasm32) -+let enableCrashTests = false -+#else -+let enableCrashTests = true -+#endif -+ - var CStringTests = TestSuite("CStringTests") - - func getNullUTF8() -> UnsafeMutablePointer? { -@@ -227,6 +233,7 @@ CStringTests.test("Substring.withCString") { - - CStringTests.test("String.cString.with.Array.UInt8.input") { - guard #available(SwiftStdlib 5.7, *) else { return } -+ - do { - let (u8p, dealloc) = getASCIIUTF8() - defer { dealloc() } -@@ -239,6 +246,7 @@ CStringTests.test("String.cString.with.Array.UInt8.input") { - } - } - } -+ guard enableCrashTests else { return } - // no need to test every case; that is covered in other tests - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) -@@ -263,6 +271,7 @@ CStringTests.test("String.cString.with.Array.CChar.input") { - } - } - } -+ guard enableCrashTests else { return } - // no need to test every case; that is covered in other tests - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) -@@ -292,6 +301,7 @@ CStringTests.test("String.cString.with.inout.UInt8.conversion") { - var str = String(cString: &c) - expectTrue(str.isEmpty) - c = 100 -+ guard enableCrashTests else { return } - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) - // withMessage: "input of String.init(cString:) must be null-terminated" -@@ -306,6 +316,7 @@ CStringTests.test("String.cString.with.inout.CChar.conversion") { - var str = String(cString: &c) - expectTrue(str.isEmpty) - c = 100 -+ guard enableCrashTests else { return } - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) - // withMessage: "input of String.init(cString:) must be null-terminated" -@@ -330,6 +341,7 @@ CStringTests.test("String.validatingUTF8.with.Array.input") { - } - } - } -+ guard enableCrashTests else { return } - // no need to test every case; that is covered in other tests - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) -@@ -362,6 +374,7 @@ CStringTests.test("String.validatingUTF8.with.inout.conversion") { - expectNotNil(str) - expectEqual(str?.isEmpty, true) - c = 100 -+ guard enableCrashTests else { return } - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) - // withMessage: "input of String.init(validatingUTF8:) must be null-terminated" -@@ -387,6 +400,7 @@ CStringTests.test("String.decodeCString.with.Array.input") { - } - } - } -+ guard enableCrashTests else { return } - // no need to test every case; that is covered in other tests - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) -@@ -426,6 +440,7 @@ CStringTests.test("String.decodeCString.with.inout.conversion") { - expectEqual(result?.result.isEmpty, true) - expectEqual(result?.repairsMade, false) - c = 100 -+ guard enableCrashTests else { return } - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) - // withMessage: "input of decodeCString(_:as:repairingInvalidCodeUnits:) must be null-terminated" -@@ -449,6 +464,7 @@ CStringTests.test("String.init.decodingCString.with.Array.input") { - } - } - } -+ guard enableCrashTests else { return } - // no need to test every case; that is covered in other tests - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) -@@ -478,6 +494,7 @@ CStringTests.test("String.init.decodingCString.with.inout.conversion") { - var str = String(decodingCString: &c, as: Unicode.UTF8.self) - expectEqual(str.isEmpty, true) - c = 100 -+ guard enableCrashTests else { return } - expectCrashLater( - // Workaround for https://github.com/apple/swift/issues/58362 (rdar://91365967) - // withMessage: "input of String.init(decodingCString:as:) must be null-terminated" -diff --git a/test/stdlib/TemporaryAllocation.swift b/test/stdlib/TemporaryAllocation.swift -index 0477492634e..ad66fa4fbd6 100644 ---- a/test/stdlib/TemporaryAllocation.swift -+++ b/test/stdlib/TemporaryAllocation.swift -@@ -55,6 +55,7 @@ TemporaryAllocationTestSuite.test("untypedEmptyAllocationIsStackAllocated") { - } - } - -+#if !os(WASI) - TemporaryAllocationTestSuite.test("crashOnNegativeByteCount") { - expectCrash { - let byteCount = Int.random(in: -2 ..< -1) -@@ -68,6 +69,7 @@ TemporaryAllocationTestSuite.test("crashOnNegativeAlignment") { - withUnsafeTemporaryAllocation(byteCount: 16, alignment: alignment) { _ in } - } - } -+#endif - - TemporaryAllocationTestSuite.test("untypedAllocationIsAligned") { - withUnsafeTemporaryAllocation(byteCount: 1, alignment: 8) { buffer in -@@ -106,12 +108,14 @@ TemporaryAllocationTestSuite.test("voidAllocationIsStackAllocated") { - } - } - -+#if !os(WASI) - TemporaryAllocationTestSuite.test("crashOnNegativeValueCount") { - expectCrash { - let capacity = Int.random(in: -2 ..< -1) - withUnsafeTemporaryAllocation(of: Int.self, capacity: capacity) { _ in } - } - } -+#endif - - TemporaryAllocationTestSuite.test("typedAllocationIsAligned") { - withUnsafeTemporaryAllocation(of: Int.self, capacity: 1) { buffer in -diff --git a/test/stdlib/UnsafePointer.swift.gyb b/test/stdlib/UnsafePointer.swift.gyb -index e0ea6199ef3..f8f8a1c33fb 100644 ---- a/test/stdlib/UnsafePointer.swift.gyb -+++ b/test/stdlib/UnsafePointer.swift.gyb -@@ -537,6 +537,7 @@ ${SelfName}TestSuite.test("pointer(to:)") { - % end - } - -+#if !os(WASI) - ${SelfName}TestSuite.test("pointer(to:).overflow") { - struct Example { - var a = false -@@ -558,6 +559,7 @@ ${SelfName}TestSuite.test("pointer(to:).overflow") { - let doublePointer = p.pointer(to: \.d) - expectNotNil(doublePointer) - } -+#endif - - % end - -diff --git a/test/stdlib/UnsafeRawPointer.swift b/test/stdlib/UnsafeRawPointer.swift -index d450335fa4b..5223bc373b3 100644 ---- a/test/stdlib/UnsafeRawPointer.swift -+++ b/test/stdlib/UnsafeRawPointer.swift -@@ -114,6 +114,7 @@ UnsafeMutableRawPointerExtraTestSuite.test("load.unaligned") - expectEqual(result, 0xffff_0000) - } - -+#if !os(WASI) - UnsafeMutableRawPointerExtraTestSuite.test("load.invalid") - .skip(.custom({ !_isDebugAssertConfiguration() }, - reason: "This tests a debug precondition..")) -@@ -137,6 +138,7 @@ UnsafeMutableRawPointerExtraTestSuite.test("load.invalid.mutable") - } - expectUnreachable() - } -+#endif - - UnsafeMutableRawPointerExtraTestSuite.test("store.unaligned") - .skip(.custom({ -@@ -167,6 +169,7 @@ UnsafeMutableRawPointerExtraTestSuite.test("store.unaligned") - 0) - } - -+#if !os(WASI) - UnsafeMutableRawPointerExtraTestSuite.test("store.invalid") - .skip(.custom({ !_isDebugAssertConfiguration() }, - reason: "This tests a debug precondition..")) -@@ -186,6 +189,7 @@ UnsafeMutableRawPointerExtraTestSuite.test("store.invalid") - p1.storeBytes(of: m, as: Missile.self) - expectUnreachable() - } -+#endif - - UnsafeMutableRawPointerExtraTestSuite.test("copyMemory") { - let sizeInBytes = 4 * MemoryLayout.stride -diff --git a/utils/build-script b/utils/build-script -index dcbc3915489..737a820ce64 100755 ---- a/utils/build-script -+++ b/utils/build-script -@@ -266,7 +266,6 @@ def validate_arguments(toolchain, args): - "--legacy-impl is incompatible with building packages needing " - "a toolchain (%s)" % ", ".join(targets_needing_toolchain)) - -- - def default_stdlib_deployment_targets(args): - """ - Return targets for the Swift stdlib, based on the build machine. -diff --git a/utils/build-script-impl b/utils/build-script-impl -index d8a7b6d5951..de8882ddb65 100755 ---- a/utils/build-script-impl -+++ b/utils/build-script-impl -@@ -116,12 +116,16 @@ KNOWN_SETTINGS=( - darwin-toolchain-require-use-os-runtime "0" "When setting up a plist for a toolchain, require the users of the toolchain to link against the OS instead of the packaged toolchain runtime. 0 for false, 1 for true" - darwin-xcrun-toolchain "default" "the name of the toolchain to use on Darwin" - -+ ## WebAssembly/WASI Options -+ wasi-sysroot "" "An absolute path to the wasi-sysroot that will be used as a libc implementation for Wasm builds" -+ - ## Build Types for Components - swift-stdlib-build-type "Debug" "the CMake build variant for Swift" - - ## Skip Build ... - skip-build "" "set to configure as usual while skipping the build step" - skip-build-android "" "set to skip building Swift stdlibs for Android" -+ skip-build-wasm "" "set to skip building Swift stdlibs for WebAssembly" - skip-build-benchmarks "" "set to skip building Swift Benchmark Suite" - skip-build-clang-tools-extra "" "set to skip building clang-tools-extra as part of llvm" - skip-build-compiler-rt "" "set to skip building Compiler-RT" -@@ -478,7 +482,8 @@ function verify_host_is_supported() { - | watchos-arm64_32 \ - | android-armv7 \ - | android-aarch64 \ -- | android-x86_64) -+ | android-x86_64 \ -+ | wasi-wasm32) - ;; - *) - echo "Unknown host tools target: ${host}" -@@ -1346,6 +1351,9 @@ function common_cross_c_flags() { - watchos-*) - echo -n " -arch ${arch} -target ${arch}-apple-watchos${DARWIN_DEPLOYMENT_VERSION_WATCHOS}" - ;; -+ wasi-wasm32) -+ echo -n " -arch wasm32" -+ ;; - esac - - local build_type=$2 -@@ -1846,6 +1854,13 @@ for host in "${ALL_HOSTS[@]}"; do - ) - fi - -+ if [[ ! "${SKIP_BUILD_WASM}" ]]; then -+ cmake_options=( -+ "${cmake_options[@]}" -+ -DSWIFT_WASI_SYSROOT_PATH:STRING="${WASI_SYSROOT}" -+ ) -+ fi -+ - if [[ $(is_cross_tools_host ${host}) && "${host}" == "openbsd-"* && -n "${OPENBSD_USE_TOOLCHAIN_FILE}" ]]; then - cmake_options=( - "${cmake_options[@]}" -@@ -2418,7 +2433,7 @@ for host in "${ALL_HOSTS[@]}"; do - XCTEST_BUILD_ARGS="--release" - fi - -- call "${XCTEST_SOURCE_DIR}"/build_script.py \ -+ call python3 "${XCTEST_SOURCE_DIR}"/build_script.py \ - --swiftc="${SWIFTC_BIN}" \ - --build-dir="${XCTEST_BUILD_DIR}" \ - --foundation-build-dir="${FOUNDATION_BUILD_DIR}" \ -@@ -2964,7 +2979,7 @@ for host in "${ALL_HOSTS[@]}"; do - echo "--- Running tests for ${product} ---" - FOUNDATION_BUILD_DIR=$(build_directory ${host} foundation) - XCTEST_BUILD_DIR=$(build_directory ${host} xctest) -- call "${XCTEST_SOURCE_DIR}"/build_script.py test \ -+ call python3 "${XCTEST_SOURCE_DIR}"/build_script.py test \ - --swiftc="${SWIFTC_BIN}" \ - --lit="${LLVM_SOURCE_DIR}/utils/lit/lit.py" \ - --foundation-build-dir="${FOUNDATION_BUILD_DIR}" \ -diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py -index 828085e9437..35d980d5a23 100644 ---- a/utils/build_swift/build_swift/driver_arguments.py -+++ b/utils/build_swift/build_swift/driver_arguments.py -@@ -450,7 +450,7 @@ def create_argument_parser(): - 'fuzzing swiftc') - - option('--compiler-vendor', store, -- choices=['none', 'apple'], -+ choices=['none', 'apple', 'swiftwasm'], - default=defaults.COMPILER_VENDOR, - help='Compiler vendor name') - option('--clang-compiler-version', store, -diff --git a/utils/darwin-installer-scripts/postinstall b/utils/darwin-installer-scripts/postinstall -index 8f00806645e..e9750f04e95 100755 ---- a/utils/darwin-installer-scripts/postinstall -+++ b/utils/darwin-installer-scripts/postinstall -@@ -13,4 +13,4 @@ - - INSTALLED_TOOLCHAIN=$2 - --ln -fs "${INSTALLED_TOOLCHAIN}" "${INSTALLED_TOOLCHAIN%/*}/swift-latest.xctoolchain" -+ln -fhs "${INSTALLED_TOOLCHAIN}" "${INSTALLED_TOOLCHAIN%/*}/swift-latest.xctoolchain" -diff --git a/utils/swift_build_support/swift_build_support/host_specific_configuration.py b/utils/swift_build_support/swift_build_support/host_specific_configuration.py -index c0a2e55bfcd..609f82fad49 100644 ---- a/utils/swift_build_support/swift_build_support/host_specific_configuration.py -+++ b/utils/swift_build_support/swift_build_support/host_specific_configuration.py -@@ -255,6 +255,8 @@ class HostSpecificConfiguration(object): - StdlibDeploymentTarget.AppleWatchSimulator) - if not stage_dependent_args.build_android: - platforms_to_skip_build.add(StdlibDeploymentTarget.Android) -+ if not args.build_wasm: -+ platforms_to_skip_build.add(StdlibDeploymentTarget.WASI) - return platforms_to_skip_build - - def __platforms_to_skip_test(self, args, stage_dependent_args): -@@ -296,6 +298,8 @@ class HostSpecificConfiguration(object): - StdlibDeploymentTarget.AppleWatchSimulator) - if not stage_dependent_args.test_android: - platforms_to_skip_test.add(StdlibDeploymentTarget.Android) -+ if not args.test_wasm: -+ platforms_to_skip_test.add(StdlibDeploymentTarget.WASI) - - return platforms_to_skip_test - -@@ -336,4 +340,6 @@ class HostSpecificConfiguration(object): - if not stage_dependent_args.test_watchos_host and \ - not args.only_non_executable_test: - platforms_to_skip_test_host.add(StdlibDeploymentTarget.AppleWatch) -+ if not args.test_wasm_host: -+ platforms_to_skip_test_host.add(StdlibDeploymentTarget.WASI) - return platforms_to_skip_test_host -diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py -index 6cbbe7b56cc..b8bafad5051 100644 ---- a/utils/swift_build_support/swift_build_support/products/llvm.py -+++ b/utils/swift_build_support/swift_build_support/products/llvm.py -@@ -64,6 +64,13 @@ class LLVM(cmake_product.CMakeProduct): - if self.args.compiler_vendor == "none": - return [] - -+ if self.args.compiler_vendor == "swiftwasm": -+ return [ -+ ('CLANG_VENDOR', 'SwiftWasm'), -+ ('CLANG_VENDOR_UTI', 'org.swiftwasm.compilers.llvm.clang'), -+ ('PACKAGE_VERSION', str(self.args.clang_user_visible_version)) -+ ] -+ - if self.args.compiler_vendor != "apple": - raise RuntimeError("Unknown compiler vendor?!") - -diff --git a/utils/swift_build_support/swift_build_support/products/swift.py b/utils/swift_build_support/swift_build_support/products/swift.py -index 16e3ff6b55c..9663ce42ebb 100644 ---- a/utils/swift_build_support/swift_build_support/products/swift.py -+++ b/utils/swift_build_support/swift_build_support/products/swift.py -@@ -94,14 +94,22 @@ class Swift(product.Product): - if self.args.compiler_vendor == "none": - return [] - -- if self.args.compiler_vendor != "apple": -- raise RuntimeError("Unknown compiler vendor?! Was build-script \ --updated without updating swift.py?") -- - swift_compiler_version = "" - if self.args.swift_compiler_version is not None: - swift_compiler_version = self.args.swift_compiler_version - -+ if self.args.compiler_vendor == "swiftwasm": -+ return [ -+ ('SWIFT_VENDOR', 'SwiftWasm'), -+ ('SWIFT_VENDOR_UTI', 'org.swiftwasm.compilers.llvm.swift'), -+ ('SWIFT_VERSION', str(self.args.swift_user_visible_version)), -+ ('SWIFT_COMPILER_VERSION', str(swift_compiler_version)), -+ ] -+ -+ if self.args.compiler_vendor != "apple": -+ raise RuntimeError("Unknown compiler vendor?! Was build-script \ -+updated without updating swift.py?") -+ - return [ - ('SWIFT_VENDOR', 'Apple'), - ('SWIFT_VENDOR_UTI', 'com.apple.compilers.llvm.swift'), -diff --git a/utils/update_checkout/update-checkout-config.json b/utils/update_checkout/update-checkout-config.json -index bc437bbff77..bb794b1eb6b 100644 ---- a/utils/update_checkout/update-checkout-config.json -+++ b/utils/update_checkout/update-checkout-config.json -@@ -3,7 +3,7 @@ - "https-clone-pattern": "https://github.com/%s.git", - "repos" : { - "swift": { -- "remote": { "id": "apple/swift" } }, -+ "remote": { "id": "swiftwasm/swift" } }, - "cmark": { - "remote": { "id": "apple/swift-cmark" } }, - "llbuild": { -@@ -31,9 +31,9 @@ - "swift-stress-tester": { - "remote": { "id": "apple/swift-stress-tester" } }, - "swift-corelibs-xctest": { -- "remote": { "id": "apple/swift-corelibs-xctest" } }, -+ "remote": { "id": "swiftwasm/swift-corelibs-xctest" } }, - "swift-corelibs-foundation": { -- "remote": { "id": "apple/swift-corelibs-foundation" } }, -+ "remote": { "id": "swiftwasm/swift-corelibs-foundation" } }, - "swift-corelibs-libdispatch": { - "remote": { "id": "apple/swift-corelibs-libdispatch" } }, - "swift-integration-tests": { -@@ -43,8 +43,7 @@ - "ninja": { - "remote": { "id": "ninja-build/ninja" } }, - "icu": { -- "remote": { "id": "unicode-org/icu" }, -- "platforms": [ "Linux" ] -+ "remote": { "id": "unicode-org/icu" } - }, - "yams": { - "remote": { "id": "jpsim/Yams" } -@@ -84,8 +83,93 @@ - "llvm-project": { - "remote": { "id": "apple/llvm-project" } } - }, -- "default-branch-scheme": "main", -+ "default-branch-scheme": "wasm", - "branch-schemes": { -+ "wasm": { -+ "aliases": ["wasm", "swiftwasm"], -+ "repos": { -+ "llvm-project": "stable/20220421", -+ "swift-llvm-bindings": "stable/20220421", -+ "swift": "swiftwasm", -+ "cmark": "gfm", -+ "llbuild": "main", -+ "swift-tools-support-core": "main", -+ "swiftpm": "main", -+ "swift-argument-parser": "1.0.3", -+ "swift-atomics": "1.0.2", -+ "swift-collections": "1.0.1", -+ "swift-crypto": "2.2.3", -+ "swift-driver": "main", -+ "swift-numerics": "1.0.1", -+ "swift-syntax": "main", -+ "swift-system": "1.1.1", -+ "swift-stress-tester": "main", -+ "swift-corelibs-xctest": "swiftwasm", -+ "swift-corelibs-foundation": "swiftwasm", -+ "swift-corelibs-libdispatch": "main", -+ "swift-integration-tests": "main", -+ "swift-xcode-playground-support": "main", -+ "ninja": "release", -+ "icu": "release-65-1", -+ "yams": "5.0.1", -+ "cmake": "v3.19.6", -+ "indexstore-db": "main", -+ "sourcekit-lsp": "main", -+ "swift-format": "main", -+ "swift-installer-scripts": "main", -+ "swift-docc": "main", -+ "swift-lmdb": "main", -+ "swift-docc-render-artifact": "main", -+ "swift-docc-symbolkit": "main", -+ "swift-markdown": "main", -+ "swift-cmark-gfm": "gfm", -+ "swift-nio": "2.31.2", -+ "swift-nio-ssl": "2.15.0", -+ "swift-experimental-string-processing": "swift/main" -+ } -+ }, -+ "wasm/5.8": { -+ "aliases": ["wasm/5.8", "swiftwasm/5.8"], -+ "repos": { -+ "llvm-project": "swift/release/5.8", -+ "swift-llvm-bindings": "swift/release/5.8", -+ "swift": "swiftwasm-release/5.8", -+ "cmark": "release/5.8", -+ "llbuild": "release/5.8", -+ "swift-tools-support-core": "release/5.8", -+ "swiftpm": "release/5.8", -+ "swift-argument-parser": "1.0.3", -+ "swift-atomics": "1.0.2", -+ "swift-collections": "1.0.1", -+ "swift-crypto": "2.2.3", -+ "swift-driver": "release/5.8", -+ "swift-numerics": "1.0.1", -+ "swift-syntax": "release/5.8", -+ "swift-system": "1.1.1", -+ "swift-stress-tester": "release/5.8", -+ "swift-corelibs-xctest": "swiftwasm-release/5.8", -+ "swift-corelibs-foundation": "swiftwasm-release/5.8", -+ "swift-corelibs-libdispatch": "release/5.8", -+ "swift-integration-tests": "release/5.8", -+ "swift-xcode-playground-support": "release/5.8", -+ "ninja": "release", -+ "icu": "release-65-1", -+ "yams": "5.0.1", -+ "cmake": "v3.19.6", -+ "indexstore-db": "release/5.8", -+ "sourcekit-lsp": "release/5.8", -+ "swift-format": "release/5.8", -+ "swift-installer-scripts": "release/5.8", -+ "swift-docc": "release/5.8", -+ "swift-lmdb": "release/5.8", -+ "swift-docc-render-artifact": "release/5.8", -+ "swift-docc-symbolkit": "release/5.8", -+ "swift-markdown": "release/5.8", -+ "swift-nio": "2.31.2", -+ "swift-nio-ssl": "2.15.0", -+ "swift-experimental-string-processing": "swift/release/5.8" -+ } -+ }, - "main": { - "aliases": ["swift/main", "main", "stable/20220421"], - "repos": { -diff --git a/utils/webassembly/build-foundation.sh b/utils/webassembly/build-foundation.sh -new file mode 100755 -index 00000000000..d30c045a5fb ---- /dev/null -+++ b/utils/webassembly/build-foundation.sh -@@ -0,0 +1,33 @@ -+#!/bin/bash -+set -ex -+DESTINATION_TOOLCHAIN=$1 -+WASI_SYSROOT_PATH=$2 -+SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" -+BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" -+LIBXML2_PATH="$BUILD_SDK_PATH/libxml2" -+ -+FOUNDATION_BUILD="$SOURCE_PATH/target-build/foundation-wasi-wasm32" -+ -+mkdir -p $FOUNDATION_BUILD -+cd $FOUNDATION_BUILD -+ -+cmake -G Ninja \ -+ -DCMAKE_BUILD_TYPE="Release" \ -+ -DCMAKE_SYSROOT="$WASI_SYSROOT_PATH" \ -+ -DCMAKE_Swift_COMPILER="$DESTINATION_TOOLCHAIN/usr/bin/swiftc" \ -+ -DCMAKE_STAGING_PREFIX="$DESTINATION_TOOLCHAIN/usr" \ -+ -DCMAKE_TOOLCHAIN_FILE="$SOURCE_PATH/swift/utils/webassembly/toolchain-wasi.cmake" \ -+ -DLLVM_BIN="$DESTINATION_TOOLCHAIN/usr/bin" \ -+ -DICU_ROOT="$BUILD_SDK_PATH/icu" \ -+ -DLIBXML2_INCLUDE_DIR="$LIBXML2_PATH/include/libxml2" \ -+ -DLIBXML2_LIBRARY="$LIBXML2_PATH/lib" \ -+ -DBUILD_SHARED_LIBS=OFF \ -+ -DBUILD_NETWORKING=OFF \ -+ -DBUILD_TOOLS=OFF \ -+ -DHAS_LIBDISPATCH_API=OFF \ -+ -DCMAKE_Swift_COMPILER_FORCED=ON \ -+ -DCMAKE_Swift_FLAGS="-sdk $WASI_SYSROOT_PATH" \ -+ "${SOURCE_PATH}/swift-corelibs-foundation" -+ -+ninja -+ninja install -diff --git a/utils/webassembly/build-presets.ini b/utils/webassembly/build-presets.ini -new file mode 100644 -index 00000000000..87aa443e766 ---- /dev/null -+++ b/utils/webassembly/build-presets.ini -@@ -0,0 +1,72 @@ -+#===----------------------------------------------------------------------===# -+# Preset for the WebAssembly toolchain and SDK -+#===----------------------------------------------------------------------===# -+ -+[preset: webassembly] -+skip-build-benchmarks -+llvm-targets-to-build=X86;AArch64;WebAssembly -+swift-darwin-supported-archs=%(HOST_ARCHITECTURE)s -+compiler-vendor=swiftwasm -+enable-experimental-concurrency=1 -+enable-experimental-differentiable-programming=1 -+enable-experimental-distributed=1 -+ -+[preset: webassembly-install] -+ -+release -+no-assertions -+install-swift -+install-destdir=%(INSTALL_DESTDIR)s -+swift-install-components=autolink-driver;compiler;clang-resource-dir-symlink;stdlib;sdk-overlay;editor-integration;tools;testsuite-tools;toolchain-tools;license;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers -+llvm-install-components=llvm-cov;llvm-profdata;IndexStore;clang;clang-resource-headers;compiler-rt;clangd;dsymutil;lld;llvm-ar;llvm-ranlib -+install-prefix=/usr -+ -+ -+[preset: webassembly-host] -+ -+mixin-preset=webassembly -+sccache -+swift-compiler-version=5.8.0 -+extra-cmake-options= -+ -DSWIFT_BUILD_SOURCEKIT=FALSE -+ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE -+ -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE -+ -+[preset: webassembly-host-install] -+mixin-preset= -+ webassembly-host -+ webassembly-install -+ -+llbuild -+swiftpm -+indexstore-db -+sourcekit-lsp -+libcxx -+build-lld -+ -+install-llvm -+install-swift -+install-llbuild -+install-swiftpm -+install-sourcekit-lsp -+install-libcxx -+install-swiftsyntax -+ -+[preset: webassembly-linux-host-install] -+ -+mixin-preset=webassembly-host-install -+ -+libdispatch -+libicu -+foundation -+xctest -+ -+install-libicu -+install-foundation -+install-libdispatch -+install-xctest -+ -+skip-test-libicu -+skip-test-foundation -+skip-test-libdispatch -+skip-test-xctest -diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh -new file mode 100755 -index 00000000000..5a372a5a2c1 ---- /dev/null -+++ b/utils/webassembly/build-toolchain.sh -@@ -0,0 +1,258 @@ -+#!/bin/bash -+ -+set -ex -+SOURCE_PATH="$(cd "$(dirname "$0")/../../.." && pwd)" -+UTILS_PATH="$(cd "$(dirname "$0")" && pwd)" -+ -+BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" -+WASI_SYSROOT_PATH="$BUILD_SDK_PATH/wasi-sysroot" -+ -+case $(uname -s) in -+ Darwin) -+ OS_SUFFIX=macos_$(uname -m) -+ HOST_PRESET=webassembly-host-install -+ HOST_SUFFIX=macosx-$(uname -m) -+ ;; -+ Linux) -+ if [ "$(grep RELEASE /etc/lsb-release)" == "DISTRIB_RELEASE=18.04" ]; then -+ OS_SUFFIX=ubuntu18.04_$(uname -m) -+ elif [ "$(grep RELEASE /etc/lsb-release)" == "DISTRIB_RELEASE=20.04" ]; then -+ OS_SUFFIX=ubuntu20.04_$(uname -m) -+ elif [ "$(grep RELEASE /etc/lsb-release)" == "DISTRIB_RELEASE=22.04" ]; then -+ OS_SUFFIX=ubuntu22.04_$(uname -m) -+ elif [[ "$(grep PRETTY_NAME /etc/os-release)" == 'PRETTY_NAME="Amazon Linux 2"' ]]; then -+ OS_SUFFIX=amazonlinux2_$(uname -m) -+ else -+ echo "Unknown Ubuntu version" -+ exit 1 -+ fi -+ HOST_PRESET=webassembly-linux-host-install -+ HOST_SUFFIX=linux-$(uname -m) -+ ;; -+ *) -+ echo "Unrecognised platform $(uname -s)" -+ exit 1 -+ ;; -+esac -+ -+BUILD_HOST_TOOLCHAIN=1 -+TOOLCHAIN_CHANNEL=${TOOLCHAIN_CHANNEL:-DEVELOPMENT} -+ -+while [ $# -ne 0 ]; do -+ case "$1" in -+ --skip-build-host-toolchain) -+ BUILD_HOST_TOOLCHAIN=0 -+ ;; -+ *) -+ echo "Unrecognised argument \"$1\"" -+ exit 1 -+ ;; -+ esac -+ shift -+done -+ -+YEAR=$(date +"%Y") -+MONTH=$(date +"%m") -+DAY=$(date +"%d") -+TOOLCHAIN_NAME="swift-wasm-${TOOLCHAIN_CHANNEL}-SNAPSHOT-${YEAR}-${MONTH}-${DAY}-a" -+ -+PACKAGE_ARTIFACT="$SOURCE_PATH/swift-wasm-${TOOLCHAIN_CHANNEL}-SNAPSHOT-${OS_SUFFIX}.tar.gz" -+ -+HOST_TOOLCHAIN_DESTDIR=$SOURCE_PATH/host-toolchain-sdk -+DIST_TOOLCHAIN_DESTDIR=$SOURCE_PATH/dist-toolchain-sdk -+DIST_TOOLCHAIN_SDK=$DIST_TOOLCHAIN_DESTDIR/$TOOLCHAIN_NAME -+ -+ -+HOST_BUILD_ROOT=$SOURCE_PATH/host-build -+TARGET_BUILD_ROOT=$SOURCE_PATH/target-build -+HOST_BUILD_DIR=$HOST_BUILD_ROOT/Ninja-Release -+ -+build_host_toolchain() { -+ # Build the host toolchain and SDK first. -+ env SWIFT_BUILD_ROOT="$HOST_BUILD_ROOT" \ -+ "$SOURCE_PATH/swift/utils/build-script" \ -+ --preset-file="$UTILS_PATH/build-presets.ini" \ -+ --preset=$HOST_PRESET \ -+ --build-dir="$HOST_BUILD_DIR" \ -+ HOST_ARCHITECTURE="$(uname -m)" \ -+ INSTALL_DESTDIR="$HOST_TOOLCHAIN_DESTDIR" -+} -+ -+build_target_toolchain() { -+ -+ local COMPILER_RT_BUILD_DIR="$TARGET_BUILD_ROOT/compiler-rt-wasi-wasm32" -+ cmake -B "$COMPILER_RT_BUILD_DIR" \ -+ -D CMAKE_TOOLCHAIN_FILE="$SOURCE_PATH/swift/utils/webassembly/compiler-rt-cache.cmake" \ -+ -D CMAKE_BUILD_TYPE=Release \ -+ -D CMAKE_C_COMPILER="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/clang" \ -+ -D CMAKE_CXX_COMPILER="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/clang++" \ -+ -D CMAKE_RANLIB="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/llvm-ranlib" \ -+ -D CMAKE_AR="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/llvm-ar" \ -+ -D CMAKE_C_COMPILER_LAUNCHER="$(which sccache)" \ -+ -D CMAKE_CXX_COMPILER_LAUNCHER="$(which sccache)" \ -+ -D CMAKE_INSTALL_PREFIX="$DIST_TOOLCHAIN_SDK/usr/lib/clang/13.0.0/" \ -+ -D CMAKE_SYSROOT="${WASI_SYSROOT_PATH}" \ -+ -G Ninja \ -+ -S "$SOURCE_PATH/llvm-project/compiler-rt" -+ -+ ninja install -C "$COMPILER_RT_BUILD_DIR" -+ -+ # Only configure LLVM to use CMake functionalities in LLVM -+ local LLVM_TARGET_BUILD_DIR="$TARGET_BUILD_ROOT/llvm-wasi-wasm32" -+ cmake -B "$LLVM_TARGET_BUILD_DIR" \ -+ -D CMAKE_BUILD_TYPE=Release \ -+ -D LLVM_ENABLE_ZLIB=NO \ -+ -D LLVM_ENABLE_LIBXML2=NO \ -+ -G Ninja \ -+ -S "$SOURCE_PATH/llvm-project/llvm" -+ -+ local SWIFT_STDLIB_BUILD_DIR="$TARGET_BUILD_ROOT/swift-stdlib-wasi-wasm32" -+ -+ # FIXME(katei): Platform/WASI is not recognized as a platform in LLVM, so it reports -+ # "Unable to determine platform" while handling LLVM options. -+ # Set WASI as a UNIX platform to spoof LLVM -+ # FIXME(katei): host-build clang's libcxx is capable with LLVM, but it somehow -+ # fails libcxx version check. So activate LLVM_COMPILER_CHECKED to spoof the checker -+ # SWIFT_DRIVER_TEST_OPTIONS is used to specify clang resource dir for wasm32-unknown-wasi -+ # because it's not built beside clang -+ cmake -B "$SWIFT_STDLIB_BUILD_DIR" \ -+ -C "$SOURCE_PATH/swift/cmake/caches/Runtime-WASI-wasm32.cmake" \ -+ -D CMAKE_TOOLCHAIN_FILE="$SOURCE_PATH/swift/utils/webassembly/toolchain-wasi.cmake" \ -+ -D CMAKE_BUILD_TYPE=Release \ -+ -D CMAKE_C_COMPILER_LAUNCHER="$(which sccache)" \ -+ -D CMAKE_CXX_COMPILER_LAUNCHER="$(which sccache)" \ -+ -D CMAKE_INSTALL_PREFIX="$DIST_TOOLCHAIN_SDK/usr" \ -+ -D LLVM_BIN="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin" \ -+ -D LLVM_DIR="$LLVM_TARGET_BUILD_DIR/lib/cmake/llvm/" \ -+ -D LLVM_COMPILER_CHECKED=YES \ -+ -D UNIX=1 \ -+ -D SWIFT_NATIVE_SWIFT_TOOLS_PATH="$HOST_BUILD_DIR/swift-$HOST_SUFFIX/bin" \ -+ -D SWIFT_NATIVE_CLANG_TOOLS_PATH="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin" \ -+ -D SWIFT_NATIVE_LLVM_TOOLS_PATH="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin" \ -+ -D SWIFT_LIT_TEST_PATHS="$SWIFT_STDLIB_BUILD_DIR/test-wasi-wasm32/stdlib;$SWIFT_STDLIB_BUILD_DIR/test-wasi-wasm32/Concurrency/Runtime" \ -+ -D SWIFT_DRIVER_TEST_OPTIONS=" -Xclang-linker -resource-dir -Xclang-linker $COMPILER_RT_BUILD_DIR" \ -+ -D SWIFT_WASI_SYSROOT_PATH="$WASI_SYSROOT_PATH" \ -+ -D SWIFT_WASI_wasm32_ICU_UC_INCLUDE="$BUILD_SDK_PATH/icu/include" \ -+ -D SWIFT_WASI_wasm32_ICU_UC="$BUILD_SDK_PATH/icu/lib/libicuuc.a" \ -+ -D SWIFT_WASI_wasm32_ICU_I18N_INCLUDE="$BUILD_SDK_PATH/icu/include" \ -+ -D SWIFT_WASI_wasm32_ICU_I18N="$BUILD_SDK_PATH/icu/lib/libicui18n.a" \ -+ -D SWIFT_WASI_wasm32_ICU_DATA="$BUILD_SDK_PATH/icu/lib/libicudata.a" \ -+ -D SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING=YES \ -+ -D SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED=YES \ -+ -D SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING=YES \ -+ -D SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE="$SOURCE_PATH/swift-syntax" \ -+ -D SWIFT_PATH_TO_STRING_PROCESSING_SOURCE="$SOURCE_PATH/swift-experimental-string-processing" \ -+ -G Ninja \ -+ -S "$SOURCE_PATH/swift" -+ -+ # FIXME(katei): 'sdk-overlay' is explicitly used to build libcxxshim.modulemap -+ # which is used only in tests, so 'ninja install' doesn't build it -+ # the header and modulemap custom targets should be added as dependency of install -+ ninja sdk-overlay install -C "$SWIFT_STDLIB_BUILD_DIR" -+ -+ # Link compiler-rt libs to stdlib build dir -+ mkdir -p "$SWIFT_STDLIB_BUILD_DIR/lib/clang/10.0.0/" -+ ln -fs "$COMPILER_RT_BUILD_DIR/lib" "$SWIFT_STDLIB_BUILD_DIR/lib/clang/10.0.0/lib" -+ -+ # Remove host CoreFoundation module directory to avoid module conflict -+ # while building Foundation -+ rm -rf "$DIST_TOOLCHAIN_SDK/usr/lib/swift_static/CoreFoundation" -+ "$UTILS_PATH/build-foundation.sh" "$DIST_TOOLCHAIN_SDK" "$WASI_SYSROOT_PATH" -+ "$UTILS_PATH/build-xctest.sh" "$DIST_TOOLCHAIN_SDK" "$WASI_SYSROOT_PATH" -+ -+} -+ -+embed_wasi_sysroot() { -+ # Merge wasi-sdk and the toolchain -+ cp -r "$WASI_SYSROOT_PATH" "$DIST_TOOLCHAIN_SDK/usr/share" -+ -+ # Replace absolute sysroot path with relative path -+ sed -i.bak -e "s@\".*/include@\"../../../../share/wasi-sysroot/include@g" "$DIST_TOOLCHAIN_SDK/usr/lib/swift/wasi/wasm32/wasi.modulemap" -+ rm "$DIST_TOOLCHAIN_SDK/usr/lib/swift/wasi/wasm32/wasi.modulemap.bak" -+ sed -i.bak -e "s@\".*/include@\"../../../../share/wasi-sysroot/include@g" "$DIST_TOOLCHAIN_SDK/usr/lib/swift_static/wasi/wasm32/wasi.modulemap" -+ rm "$DIST_TOOLCHAIN_SDK/usr/lib/swift_static/wasi/wasm32/wasi.modulemap.bak" -+} -+ -+swift_version() { -+ cat "$SOURCE_PATH/swift/CMakeLists.txt" | grep 'set(SWIFT_VERSION ' | sed -E 's/set\(SWIFT_VERSION "(.+)"\)/\1/' -+} -+ -+create_darwin_info_plist() { -+ echo "-- Create Info.plist --" -+ PLISTBUDDY_BIN="/usr/libexec/PlistBuddy" -+ -+ DARWIN_TOOLCHAIN_VERSION="$(swift_version).${YEAR}${MONTH}${DAY}" -+ BUNDLE_PREFIX="org.swiftwasm" -+ DARWIN_TOOLCHAIN_BUNDLE_IDENTIFIER="${BUNDLE_PREFIX}.${YEAR}${MONTH}${DAY}" -+ DARWIN_TOOLCHAIN_DISPLAY_NAME_SHORT="Swift for WebAssembly Snapshot" -+ DARWIN_TOOLCHAIN_DISPLAY_NAME="${DARWIN_TOOLCHAIN_DISPLAY_NAME_SHORT} ${YEAR}-${MONTH}-${DAY}" -+ DARWIN_TOOLCHAIN_ALIAS="swiftwasm" -+ -+ DARWIN_TOOLCHAIN_INFO_PLIST="${DIST_TOOLCHAIN_SDK}/Info.plist" -+ DARWIN_TOOLCHAIN_REPORT_URL="https://github.com/swiftwasm/swift/issues" -+ COMPATIBILITY_VERSION=2 -+ COMPATIBILITY_VERSION_DISPLAY_STRING="Xcode 8.0" -+ DARWIN_TOOLCHAIN_CREATED_DATE="$(date -u +'%a %b %d %T GMT %Y')" -+ SWIFT_USE_DEVELOPMENT_TOOLCHAIN_RUNTIME="YES" -+ -+ rm -f "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ -+ ${PLISTBUDDY_BIN} -c "Add DisplayName string '${DARWIN_TOOLCHAIN_DISPLAY_NAME}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add ShortDisplayName string '${DARWIN_TOOLCHAIN_DISPLAY_NAME_SHORT}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add CreatedDate date '${DARWIN_TOOLCHAIN_CREATED_DATE}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add CompatibilityVersion integer ${COMPATIBILITY_VERSION}" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add CompatibilityVersionDisplayString string ${COMPATIBILITY_VERSION_DISPLAY_STRING}" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add Version string '${DARWIN_TOOLCHAIN_VERSION}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add CFBundleIdentifier string '${DARWIN_TOOLCHAIN_BUNDLE_IDENTIFIER}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add ReportProblemURL string '${DARWIN_TOOLCHAIN_REPORT_URL}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add Aliases array" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add Aliases:0 string '${DARWIN_TOOLCHAIN_ALIAS}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings dict" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:ENABLE_BITCODE string 'NO'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_DISABLE_REQUIRED_ARCLITE string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_LINK_OBJC_RUNTIME string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_DEVELOPMENT_TOOLCHAIN string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_USE_DEVELOPMENT_TOOLCHAIN_RUNTIME string '${SWIFT_USE_DEVELOPMENT_TOOLCHAIN_RUNTIME}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+ -+ chmod a+r "${DARWIN_TOOLCHAIN_INFO_PLIST}" -+} -+ -+show_sccache_stats() { -+ # If sccache is installed in PATH -+ if command -v sccache &> /dev/null; then -+ sccache --show-stats -+ else -+ echo "sccache is not installed in PATH" -+ fi -+} -+ -+if [ ${BUILD_HOST_TOOLCHAIN} -eq 1 ]; then -+ build_host_toolchain -+ echo "==================================" -+ echo "Host toolchain built successfully!" -+ echo "==================================" -+ echo "" -+ echo "sccache stats:" -+ show_sccache_stats -+ rm -rf "$DIST_TOOLCHAIN_DESTDIR" -+ mkdir -p "$DIST_TOOLCHAIN_SDK" -+ rsync -a "$HOST_TOOLCHAIN_DESTDIR/" "$DIST_TOOLCHAIN_SDK" -+fi -+ -+build_target_toolchain -+echo "====================================" -+echo "Target toolchain built successfully!" -+echo "====================================" -+echo "" -+echo "sccache stats:" -+show_sccache_stats -+ -+embed_wasi_sysroot -+ -+if [[ "$(uname)" == "Darwin" ]]; then -+ create_darwin_info_plist -+fi -+ -+cd "$DIST_TOOLCHAIN_DESTDIR" -+tar cfz "$PACKAGE_ARTIFACT" "$TOOLCHAIN_NAME" -+echo "Toolchain archive created successfully!" -diff --git a/utils/webassembly/build-xctest.sh b/utils/webassembly/build-xctest.sh -new file mode 100755 -index 00000000000..e098fe46297 ---- /dev/null -+++ b/utils/webassembly/build-xctest.sh -@@ -0,0 +1,25 @@ -+#!/bin/bash -+set -ex -+DESTINATION_TOOLCHAIN=$1 -+WASI_SYSROOT_PATH=$2 -+SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" -+ -+BUILD_DIR="$SOURCE_PATH/target-build/xctest-wasi-wasm32" -+ -+mkdir -p $BUILD_DIR -+cd $BUILD_DIR -+ -+cmake -G Ninja \ -+ -DCMAKE_BUILD_TYPE="Release" \ -+ -DCMAKE_SYSROOT="$WASI_SYSROOT_PATH" \ -+ -DCMAKE_Swift_COMPILER="$DESTINATION_TOOLCHAIN/usr/bin/swiftc" \ -+ -DCMAKE_STAGING_PREFIX="$DESTINATION_TOOLCHAIN/usr" \ -+ -DCMAKE_TOOLCHAIN_FILE="$SOURCE_PATH/swift/utils/webassembly/toolchain-wasi.cmake" \ -+ -DLLVM_BIN="$DESTINATION_TOOLCHAIN/usr/bin" \ -+ -DBUILD_SHARED_LIBS=OFF \ -+ -DCMAKE_Swift_COMPILER_FORCED=ON \ -+ -DSWIFT_FOUNDATION_PATH=$DESTINATION_TOOLCHAIN/usr/lib/swift_static/wasi/wasm32 \ -+ "${SOURCE_PATH}/swift-corelibs-xctest" -+ -+ninja -v -+ninja -v install -diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh -new file mode 100755 -index 00000000000..c60938c6341 ---- /dev/null -+++ b/utils/webassembly/ci.sh -@@ -0,0 +1,15 @@ -+#!/bin/bash -+ -+set -ex -+ -+SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" -+SWIFT_PATH=$SOURCE_PATH/swift -+UTILS_PATH=$SWIFT_PATH/utils/webassembly -+ -+$SWIFT_PATH/utils/update-checkout --clone --scheme wasm/5.8 --skip-repository swift -+$UTILS_PATH/install-build-sdk.sh -+ -+export SCCACHE_CACHE_SIZE="50G" -+export SCCACHE_DIR="$SOURCE_PATH/build-cache" -+ -+$UTILS_PATH/build-toolchain.sh -diff --git a/utils/webassembly/compiler-rt-cache.cmake b/utils/webassembly/compiler-rt-cache.cmake -new file mode 100644 -index 00000000000..6e11b01d09a ---- /dev/null -+++ b/utils/webassembly/compiler-rt-cache.cmake -@@ -0,0 +1,13 @@ -+set(COMPILER_RT_DEFAULT_TARGET_ARCH wasm32 CACHE STRING "" FORCE) -+set(COMPILER_RT_DEFAULT_TARGET_ONLY TRUE CACHE BOOL "" FORCE) -+set(CMAKE_C_COMPILER_TARGET wasm32-wasi CACHE STRING "" FORCE) -+set(CMAKE_C_COMPILER_FORCED TRUE CACHE BOOL "") -+set(CMAKE_CXX_COMPILER_FORCED TRUE CACHE BOOL "") -+set(CMAKE_SYSTEM_NAME Wasm) -+set(CMAKE_SIZEOF_VOID_P 4 CACHE STRING "") -+set(COMPILER_RT_BAREMETAL_BUILD ON CACHE BOOL "") -+set(COMPILER_RT_BUILD_XRAY OFF CACHE BOOL "") -+set(COMPILER_RT_INCLUDE_TESTS OFF CACHE BOOL "") -+set(COMPILER_RT_HAS_FPIC_FLAG OFF CACHE BOOL "") -+set(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN OFF CACHE BOOL "") -+set(COMPILER_RT_OS_DIR wasi CACHE STRING "") -diff --git a/utils/webassembly/distribute-latest-toolchain.sh b/utils/webassembly/distribute-latest-toolchain.sh -new file mode 100755 -index 00000000000..233f281141d ---- /dev/null -+++ b/utils/webassembly/distribute-latest-toolchain.sh -@@ -0,0 +1,235 @@ -+#!/bin/bash -+ -+set -xe -+repository='swiftwasm/swift' -+workflow_name='build-toolchain.yml' -+branch=$1 -+channel=$2 -+swift_source_dir="$(cd "$(dirname $0)/../.." && pwd)" -+ -+TARGETS_TO_DIST=${TARGETS_TO_DIST:?"Please set TARGETS_TO_DIST"} -+targets=($TARGETS_TO_DIST) -+DARWIN_TOOLCHAIN_APPLICATION_CERT=${DARWIN_TOOLCHAIN_APPLICATION_CERT:?"Please set DARWIN_TOOLCHAIN_APPLICATION_CERT"} -+DARWIN_TOOLCHAIN_INSTALLER_CERT=${DARWIN_TOOLCHAIN_INSTALLER_CERT:?"Please set DARWIN_TOOLCHAIN_APPLICATION_CERT"} -+DARWIN_TOOLCHAIN_NOTARIZE_EMAIL=${DARWIN_TOOLCHAIN_NOTARIZE_EMAIL:?"Please set DARWIN_TOOLCHAIN_NOTARIZE_EMAIL"} -+DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID=${DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID:?"Please set DARWIN_TOOLCHAIN_NOTARIZE_TEAM_ID"} -+ -+gh_api=https://api.github.com -+ -+github() { -+ curl --header "authorization: Bearer $GITHUB_TOKEN" "$@" -+} -+ -+# workaround: if status=success option is given, the API doesn't return the latest runs list, so remove it temporarily. -+latest_run=$(github "${gh_api}/repos/${repository}/actions/workflows/${workflow_name}/runs?branch=${branch}" \ -+ | jq ".workflow_runs | map(select(.head_branch == \"$branch\")) | map(select(.conclusion == \"success\")) | sort_by(.run_number) | last") -+ -+if [ -z "$latest_run" ] || [ "$latest_run" == "null" ]; then -+ echo "No successful runs available" -+ exit 0 -+fi -+ -+artifacts_url=$(echo $latest_run | jq .artifacts_url --raw-output) -+head_sha=$(echo $latest_run | jq .head_sha --raw-output) -+ -+get_artifact_url() { -+ local name=$1 -+ github $artifacts_url --fail | jq ".artifacts[] | select((.name == \"$name\") and (.expired | not)) | .archive_download_url" | sed 's/\"//g' -+} -+ -+download_artifact() { -+ local name=$1 -+ local artifact_url="$(get_artifact_url $name)" -+ -+ if [ -z "$artifact_url" ] || [ "$artifact_url" == "null" ]; then -+ echo "No successfully built artifacts available for $name" -+ exit 0 -+ fi -+ -+ github -L "$artifact_url" --fail -o "$name.zip" -+} -+ -+is_released() { -+ local name=$1 -+ local code=$(github "$gh_api/repos/$repository/releases/tags/$name" -o /dev/null -w '%{http_code}') -+ test $code = 200 -+} -+ -+create_tag() { -+ local name=$1 -+ local sha=$2 -+ local body=$(cat < -+Date: Thu Dec 29 10:50:33 2022 +0000 -+ -+ Without this patch, Cxx.swiftmodule build fails on macOS host environments -+ -+diff --git a/include/c++/v1/module.modulemap b/include/c++/v1/module.modulemap -+index b5781b0..af10440 100644 -+--- a/include/c++/v1/module.modulemap -++++ b/include/c++/v1/module.modulemap -+@@ -44,10 +44,12 @@ module std [system] { -+ header "math.h" -+ export * -+ } -++ /* XXX(swiftwasm): setjmp.h is unavailable in wasi-libc -+ module setjmp_h { -+ header "setjmp.h" -+ export * -+ } -++ */ -+ // FIXME: is missing. -+ // provided by C library. -+ // provided by compiler. -+@@ -153,10 +155,12 @@ module std [system] { -+ header "cmath" -+ export * -+ } -++ /* XXX(swiftwasm): setjmp.h is unavailable in wasi-libc -+ module csetjmp { -+ header "csetjmp" -+ export * -+ } -++ */ -+ module csignal { -+ header "csignal" -+ export * -+@@ -384,10 +388,12 @@ module std [system] { -+ header "execution" -+ export * -+ } -++ /* XXX(swiftwasm): wasi-sdk configures with LIBCXX_ENABLE_FILESYSTEM=OFF -+ module filesystem { -+ header "filesystem" -+ export * -+ } -++ */ -+ module format { -+ header "format" -+ export * -+@@ -645,11 +651,13 @@ module std [system] { -+ header "scoped_allocator" -+ export * -+ } -++ /* XXX(swiftwasm): semaphore is unavailable due to lack of thread -+ module semaphore { -+ requires cplusplus14 -+ header "semaphore" -+ export * -+ } -++ */ -+ module set { -+ header "set" -+ export initializer_list -+@@ -816,10 +824,12 @@ module std [system] { -+ header "experimental/deque" -+ export * -+ } -++ /* XXX(swiftwasm): wasi-sdk configures with LIBCXX_ENABLE_FILESYSTEM=OFF -+ module filesystem { -+ header "experimental/filesystem" -+ export * -+ } -++ */ -+ module forward_list { -+ header "experimental/forward_list" -+ export * -diff --git a/utils/webassembly/static-executable-args.lnk b/utils/webassembly/static-executable-args.lnk -new file mode 100644 -index 00000000000..a18d5fc3668 ---- /dev/null -+++ b/utils/webassembly/static-executable-args.lnk -@@ -0,0 +1,12 @@ -+-static -+-lswiftSwiftOnoneSupport -+-lswiftWasiPthread -+-ldl -+-lstdc++ -+-lm -+-lwasi-emulated-mman -+-lwasi-emulated-signal -+-lwasi-emulated-process-clocks -+-Xlinker --error-limit=0 -+-Xlinker --no-gc-sections -+-Xlinker --threads=1 -diff --git a/utils/webassembly/toolchain-wasi.cmake b/utils/webassembly/toolchain-wasi.cmake -new file mode 100644 -index 00000000000..aee54bab179 ---- /dev/null -+++ b/utils/webassembly/toolchain-wasi.cmake -@@ -0,0 +1,26 @@ -+set(CMAKE_SYSTEM_NAME WASI) -+set(CMAKE_SYSTEM_VERSION 1) -+set(CMAKE_SYSTEM_PROCESSOR wasm32) -+set(triple wasm32-unknown-wasi) -+ -+set(CMAKE_C_COMPILER "${LLVM_BIN}/clang") -+set(CMAKE_CXX_COMPILER "${LLVM_BIN}/clang++") -+set(CMAKE_AR "${LLVM_BIN}/llvm-ar" CACHE STRING "LLVM Archiver for wasm32") -+set(CMAKE_RANLIB "${LLVM_BIN}/llvm-ranlib" CACHE STRING "LLVM Ranlib for wasm32") -+set(CMAKE_C_COMPILER_TARGET ${triple} CACHE STRING "") -+set(CMAKE_CXX_COMPILER_TARGET ${triple} CACHE STRING "") -+set(CMAKE_Swift_COMPILER_TARGET ${triple} CACHE STRING "") -+set(CMAKE_ASM_COMPILER_TARGET ${triple} CACHE STRING "") -+ -+set(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-threads" CACHE STRING "Single thread options") -+ -+# Don't look in the sysroot for executables to run during the build -+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) -+# Only look in the sysroot (not in the host paths) for the rest -+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER) -+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER) -+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER) -+ -+# Some other hacks -+set(CMAKE_C_COMPILER_WORKS ON) -+set(CMAKE_CXX_COMPILER_WORKS ON) -diff --git a/utils/webassembly/wasm-run.py b/utils/webassembly/wasm-run.py -new file mode 100755 -index 00000000000..e59f95ebf3c ---- /dev/null -+++ b/utils/webassembly/wasm-run.py -@@ -0,0 +1,54 @@ -+#!/usr/bin/env python3 -+ -+import argparse -+import os -+import subprocess -+import sys -+ -+ -+def collect_wasm_env(local_env=os.environ, prefix='WASM_RUN_CHILD_'): -+ return dict((key[len(prefix):], value) -+ for key, value in local_env.items() if key.startswith(prefix)) -+ -+ -+class WasmtimeRunner(object): -+ def __init__(self): -+ pass -+ -+ def run(self, args): -+ command = self.invocation(args) -+ if args.verbose: -+ print(' '.join(command), file=sys.stderr) -+ -+ if not args.dry_run: -+ subprocess.check_call(command) -+ -+ def invocation(self, args): -+ command = ["wasmtime", "run"] -+ envs = collect_wasm_env() -+ for key in envs: -+ command.append("--env") -+ command.append(f"{key}={envs[key]}") -+ command.append("--") -+ command.extend(args.command) -+ return command -+ -+ -+def main(): -+ parser = argparse.ArgumentParser() -+ -+ parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', -+ help='print commands as they are run') -+ parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run', -+ help="print the commands that would have been run, but" -+ " don't actually run them") -+ parser.add_argument('command', nargs=argparse.REMAINDER, -+ help='the command to run', metavar='command...') -+ -+ args = parser.parse_args() -+ runner = WasmtimeRunner() -+ runner.run(args) -+ -+ -+if __name__ == "__main__": -+ main() --- -2.40.0 - diff --git a/schemes/release-5.8/wasi-sysroot/disable_part_modules_for_cxx_interop.patch b/schemes/release-5.8/wasi-sysroot/disable_part_modules_for_cxx_interop.patch deleted file mode 100644 index 4995a603..00000000 --- a/schemes/release-5.8/wasi-sysroot/disable_part_modules_for_cxx_interop.patch +++ /dev/null @@ -1,75 +0,0 @@ -Author: Yuta Saito -Date: Thu Dec 29 10:50:33 2022 +0000 - - Without this patch, Cxx.swiftmodule build fails on macOS host environments - -diff --git a/include/c++/v1/module.modulemap b/include/c++/v1/module.modulemap -index b5781b0..af10440 100644 ---- a/include/c++/v1/module.modulemap -+++ b/include/c++/v1/module.modulemap -@@ -44,10 +44,12 @@ module std [system] { - header "math.h" - export * - } -+ /* XXX(swiftwasm): setjmp.h is unavailable in wasi-libc - module setjmp_h { - header "setjmp.h" - export * - } -+ */ - // FIXME: is missing. - // provided by C library. - // provided by compiler. -@@ -153,10 +155,12 @@ module std [system] { - header "cmath" - export * - } -+ /* XXX(swiftwasm): setjmp.h is unavailable in wasi-libc - module csetjmp { - header "csetjmp" - export * - } -+ */ - module csignal { - header "csignal" - export * -@@ -384,10 +388,12 @@ module std [system] { - header "execution" - export * - } -+ /* XXX(swiftwasm): wasi-sdk configures with LIBCXX_ENABLE_FILESYSTEM=OFF - module filesystem { - header "filesystem" - export * - } -+ */ - module format { - header "format" - export * -@@ -645,11 +651,13 @@ module std [system] { - header "scoped_allocator" - export * - } -+ /* XXX(swiftwasm): semaphore is unavailable due to lack of thread - module semaphore { - requires cplusplus14 - header "semaphore" - export * - } -+ */ - module set { - header "set" - export initializer_list -@@ -816,10 +824,12 @@ module std [system] { - header "experimental/deque" - export * - } -+ /* XXX(swiftwasm): wasi-sdk configures with LIBCXX_ENABLE_FILESYSTEM=OFF - module filesystem { - header "experimental/filesystem" - export * - } -+ */ - module forward_list { - header "experimental/forward_list" - export * diff --git a/schemes/release-5.9/manifest.json b/schemes/release-5.9/manifest.json index f3f9c25e..f97fa332 100644 --- a/schemes/release-5.9/manifest.json +++ b/schemes/release-5.9/manifest.json @@ -1,9 +1,11 @@ { "update-checkout-scheme": "release/5.9", - "base-tag": "swift-5.9-DEVELOPMENT-SNAPSHOT-2023-08-04-a", + "base-tag": "swift-5.9.1-RELEASE", "repos": { "swift-corelibs-foundation": "660624d0220c0efbef054f4919f9f01fd3866be1", "swift-corelibs-xctest": "77bc9f5386ee8a2a4e8da5ac30e846b451d101b6" }, - "swift-org-download-channel": "swift-5.9-branch" + "icu4c": "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.5.0/icu4c-wasi.tar.xz", + "wasi-sysroot": "https://github.com/swiftwasm/wasi-sdk-build/releases/download/wasi-sdk-14%2Bswiftwasm-2022-03-13/wasi-sysroot.tar.gz", + "swift-org-download-channel": "swift-5.9.1-release" } diff --git a/schemes/release-5.9/swift/0001-wasm-Add-metadata-registration-for-WebAssembly.patch b/schemes/release-5.9/swift/0001-wasm-Add-metadata-registration-for-WebAssembly.patch index c703e272..96955d49 100644 --- a/schemes/release-5.9/swift/0001-wasm-Add-metadata-registration-for-WebAssembly.patch +++ b/schemes/release-5.9/swift/0001-wasm-Add-metadata-registration-for-WebAssembly.patch @@ -1,4 +1,4 @@ -From 7ba73fc0c8360ff659eaf1bb140cf10303a0c779 Mon Sep 17 00:00:00 2001 +From a910b7224bc35c478d0b1575cb03ab165a1d83cf Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 11 Jun 2023 11:44:24 +0000 Subject: [PATCH] [wasm] Add metadata registration for WebAssembly @@ -9,13 +9,13 @@ adds build support for it. stdlib/public/runtime/CMakeLists.txt | 26 ++++++++++++++++--- .../{SwiftRT-ELF.cpp => SwiftRT-ELF-WASM.cpp} | 15 +++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) - rename stdlib/public/runtime/{SwiftRT-ELF.cpp => SwiftRT-ELF-WASM.cpp} (82%) + rename stdlib/public/runtime/{SwiftRT-ELF.cpp => SwiftRT-ELF-WASM.cpp} (85%) diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt -index 3771d972d54..38de01cd2a8 100644 +index f80eb0cd7a2..33dfd594b7b 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt -@@ -88,7 +88,7 @@ set(swift_runtime_backtracing_sources +@@ -93,7 +93,7 @@ set(swift_runtime_backtracing_sources # Acknowledge that the following sources are known. set(LLVM_OPTIONAL_SOURCES SwiftRT-COFF.cpp @@ -24,7 +24,7 @@ index 3771d972d54..38de01cd2a8 100644 ${swift_runtime_sources} ${swift_runtime_objc_sources} ${swift_runtime_leaks_sources} -@@ -139,11 +139,14 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY +@@ -147,11 +147,14 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY set(ELFISH_SDKS) set(COFF_SDKS) @@ -39,16 +39,16 @@ index 3771d972d54..38de01cd2a8 100644 endif() endforeach() -@@ -152,7 +155,7 @@ endforeach() +@@ -160,7 +163,7 @@ endforeach() # with LTO, force swift runtime to compile without LTO for Linux. add_swift_target_library(swiftImageRegistrationObjectELF OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE - SwiftRT-ELF.cpp + SwiftRT-ELF-WASM.cpp - C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS} - C_COMPILE_FLAGS_LINUX -fno-lto - LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS} -@@ -170,17 +173,32 @@ add_swift_target_library(swiftImageRegistrationObjectCOFF + C_COMPILE_FLAGS + ${SWIFT_RUNTIME_CORE_CXX_FLAGS} + ${swift_enable_backtracing} +@@ -182,17 +185,32 @@ add_swift_target_library(swiftImageRegistrationObjectCOFF SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT none) @@ -84,10 +84,10 @@ index 3771d972d54..38de01cd2a8 100644 if(sdk STREQUAL WINDOWS) set(extension .obj) diff --git a/stdlib/public/runtime/SwiftRT-ELF.cpp b/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp -similarity index 82% +similarity index 85% rename from stdlib/public/runtime/SwiftRT-ELF.cpp rename to stdlib/public/runtime/SwiftRT-ELF-WASM.cpp -index 1b9bf3b9aa7..e68103a6a67 100644 +index 64d62f3de09..4ee737c8352 100644 --- a/stdlib/public/runtime/SwiftRT-ELF.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp @@ -1,4 +1,4 @@ @@ -96,7 +96,7 @@ index 1b9bf3b9aa7..e68103a6a67 100644 // // This source file is part of the Swift.org open source project // -@@ -16,14 +16,25 @@ +@@ -17,7 +17,13 @@ #include #include @@ -108,6 +108,9 @@ index 1b9bf3b9aa7..e68103a6a67 100644 +static constexpr const void *__dso_handle = nullptr; +#endif + #if SWIFT_ENABLE_BACKTRACING + // Drag in a symbol from the backtracer, to force the static linker to include +@@ -29,9 +35,14 @@ static const void *__backtraceRef __attribute__((used)) // Create empty sections to ensure that the start/stop symbols are synthesized // by the linker. Otherwise, we may end up with undefined symbol references as // the linker table section was never constructed. @@ -119,7 +122,7 @@ index 1b9bf3b9aa7..e68103a6a67 100644 #define DECLARE_SWIFT_SECTION(name) \ - __asm__("\t.section " #name ",\"a\"\n"); \ -+ DECLARE_EMPTY_METADATA_SECTION(name) \ ++ DECLARE_EMPTY_METADATA_SECTION(name) \ __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name; diff --git a/schemes/release-5.9/swift/0058-lit-Repair-tests-for-stdlib-only-build.patch b/schemes/release-5.9/swift/0058-lit-Repair-tests-for-stdlib-only-build.patch new file mode 100644 index 00000000..f30d2597 --- /dev/null +++ b/schemes/release-5.9/swift/0058-lit-Repair-tests-for-stdlib-only-build.patch @@ -0,0 +1,41 @@ +From b42bf448f82ff197b742f7d85a4e15dba72037e7 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sun, 29 Oct 2023 23:27:31 +0000 +Subject: [PATCH] [lit] Repair tests for stdlib-only build + +`config.host_triple` can be None when no host tools are built + +This is based on 21d57e887ad1f2d319b8e89af90d2732d50ecf55 in main branch +--- + test/lit.cfg | 8 +++++--- + 1 file changed, 5 insertions(+), 3 deletions(-) + +diff --git a/test/lit.cfg b/test/lit.cfg +index 8a955f0da88..c989777dfd5 100644 +--- a/test/lit.cfg ++++ b/test/lit.cfg +@@ -566,8 +566,10 @@ else: + ) + ) + # Parse the host triple. +- (host_cpu, host_vendor, host_os, host_vers) = re.match('([^-]+)-([^-]+)-([^0-9-]+)(.*)', config.host_triple).groups() +- toolchain_lib_dir = make_path(config.swift_lib_dir, 'swift', host_os) ++ toolchain_lib_dir = None ++ if config.host_triple: ++ (host_cpu, host_vendor, host_os, host_vers) = re.match('([^-]+)-([^-]+)-([^0-9-]+)(.*)', config.host_triple).groups() ++ toolchain_lib_dir = make_path(config.swift_lib_dir, 'swift', host_os) + config.host_build_swift = ( + "%s -target %s -g %s -I %s -L %s %s" % ( + config.swiftc_driver, +@@ -575,7 +577,7 @@ else: + '' if kIsWindows else '-no-toolchain-stdlib-rpath', + config.swift_build_lib_dir, + config.swift_build_lib_dir, +- '' if kIsWindows else '-Xlinker -rpath -Xlinker {}'.format(toolchain_lib_dir) ++ '' if kIsWindows or toolchain_lib_dir is None else '-Xlinker -rpath -Xlinker {}'.format(toolchain_lib_dir) + ) + ) + +-- +2.40.0 + diff --git a/tools/build/build-foundation.sh b/tools/build/build-foundation.sh index 5674dc6f..9bbb7349 100755 --- a/tools/build/build-foundation.sh +++ b/tools/build/build-foundation.sh @@ -1,7 +1,11 @@ #!/bin/bash set -ex DESTINATION_TOOLCHAIN=$1 -WASI_SYSROOT_PATH=$2 +LLVM_BIN_DIR=$2 +CLANG_BIN_DIR=$3 +SWIFT_BIN_DIR=$4 +WASI_SYSROOT_PATH=$5 + SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" @@ -15,10 +19,11 @@ cd $FOUNDATION_BUILD cmake -G Ninja \ -DCMAKE_BUILD_TYPE="Release" \ -DCMAKE_SYSROOT="$WASI_SYSROOT_PATH" \ - -DCMAKE_Swift_COMPILER="$DESTINATION_TOOLCHAIN/usr/bin/swiftc" \ + -DCMAKE_Swift_COMPILER="$SWIFT_BIN_DIR/swiftc" \ -DCMAKE_STAGING_PREFIX="$DESTINATION_TOOLCHAIN/usr" \ -DCMAKE_TOOLCHAIN_FILE="$TOOLS_BUILD_PATH/toolchain-wasi.cmake" \ - -DLLVM_BIN="$DESTINATION_TOOLCHAIN/usr/bin" \ + -DLLVM_BIN="$LLVM_BIN_DIR" \ + -DCLANG_BIN="$CLANG_BIN_DIR" \ -DICU_ROOT="$BUILD_SDK_PATH/icu" \ -DLIBXML2_INCLUDE_DIR="$LIBXML2_PATH/include/libxml2" \ -DLIBXML2_LIBRARY="$LIBXML2_PATH/lib" \ @@ -27,7 +32,7 @@ cmake -G Ninja \ -DBUILD_TOOLS=OFF \ -DHAS_LIBDISPATCH_API=OFF \ -DCMAKE_Swift_COMPILER_FORCED=ON \ - -DCMAKE_Swift_FLAGS="-sdk $WASI_SYSROOT_PATH" \ + -DCMAKE_Swift_FLAGS="-sdk $WASI_SYSROOT_PATH -resource-dir $DESTINATION_TOOLCHAIN/usr/lib/swift_static" \ "${SOURCE_PATH}/swift-corelibs-foundation" ninja diff --git a/tools/build/build-host-toolchain.sh b/tools/build/build-host-toolchain.sh new file mode 100755 index 00000000..8c498506 --- /dev/null +++ b/tools/build/build-host-toolchain.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +set -ex +SOURCE_PATH="$(cd "$(dirname "$0")/../../.." && pwd)" +TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" + +PACKAGING_DIR="$SOURCE_PATH/build/Packaging" +HOST_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/host-toolchain + +case $(uname -s) in + Darwin) + HOST_BUILD_PRESET=wasm_buildbot_osx_package + ;; + Linux) + HOST_BUILD_PRESET=wasm_buildbot_linux + ;; + *) + echo "Unrecognised platform $(uname -s)" + exit 1 + ;; +esac + +build_host_toolchain() { + # Build the host toolchain and SDK first. + "$SOURCE_PATH/swift/utils/build-script" \ + --preset-file="$TOOLS_BUILD_PATH/build-presets.ini" \ + --preset=$HOST_BUILD_PRESET \ + HOST_ARCHITECTURE="$(uname -m)" \ + INSTALL_DESTDIR="$HOST_TOOLCHAIN_DESTDIR" +} + +show_sccache_stats() { + # If sccache is installed in PATH + if command -v sccache &> /dev/null; then + sccache --show-stats + else + echo "sccache is not installed in PATH" + fi +} + +build_host_toolchain +echo "==================================" +echo "Host toolchain built successfully!" +echo "==================================" +echo "" +echo "sccache stats:" +show_sccache_stats diff --git a/tools/build/build-llvm-tools b/tools/build/build-llvm-tools new file mode 100755 index 00000000..ee0bd3e3 --- /dev/null +++ b/tools/build/build-llvm-tools @@ -0,0 +1,172 @@ +#!/usr/bin/env python3 +# +# Build missing LLVM tools required to build Swift SDK for WebAssembly. +# + +import argparse +import pathlib +import subprocess +import shutil +from typing import List, Optional + +from build_support.actions import REPO_ROOT +from build_support.llvm_tools import WASM_SPECIFIC_TOOLS + + +def compute_missing_tools(toolchain_path: pathlib.Path): + + missing_tools = [] + for tool in WASM_SPECIFIC_TOOLS: + if not (toolchain_path / 'usr' / 'bin' / tool).exists(): + missing_tools.append(tool) + + return missing_tools + + +def compute_build_products(toolchain_path: pathlib.Path): + build_products = [] + missing_tools = compute_missing_tools(toolchain_path) + + if 'llvm-ranlib' in missing_tools and \ + (toolchain_path / 'usr' / 'bin' / 'llvm-ar').exists(): + build_products.append( + SymlinkTool(tool_name='llvm-ar', toolchain_path=toolchain_path)) + build_products.append( + SymlinkTool(tool_name='llvm-ranlib', target='llvm-ar')) + missing_tools.remove('llvm-ranlib') + + if len(missing_tools) > 0: + build_products.append(BuildLLVMTools(toolchain_path, missing_tools)) + + # Symlink tools for existing tools + for tool in WASM_SPECIFIC_TOOLS: + if tool in missing_tools: + continue + + build_products.append(SymlinkTool( + tool_name=tool, toolchain_path=toolchain_path)) + + return build_products + + +class BuildLLVMTools: + + def __init__(self, toolchain_path: pathlib.Path, tools: List[str]): + self.toolchain_path = toolchain_path + self.tools = tools + + def configure( + self, build_dir: pathlib.Path, llvm_project_dir: pathlib.Path): + cmake_options = [] + if 'wasm-ld' in self.tools: + cmake_options.append('-DLLVM_ENABLE_PROJECTS=lld') + clang_bin_dir = self.toolchain_path / 'usr' / 'bin' + + sccache_path = shutil.which('sccache') + if sccache_path: + for launcher_cmake_option in [ + 'CMAKE_C_COMPILER_LAUNCHER', + 'CMAKE_CXX_COMPILER_LAUNCHER' + ]: + cmake_options.append( + f'-D{launcher_cmake_option}={sccache_path}') + + cmd = [ + 'cmake', + '-G', 'Ninja', + '-DCMAKE_BUILD_TYPE=Release', + '-DCMAKE_C_COMPILER=' + str(clang_bin_dir / 'clang'), + '-DCMAKE_CXX_COMPILER=' + str(clang_bin_dir / 'clang++'), + '-DLLVM_TARGETS_TO_BUILD=WebAssembly', + '-DCMAKE_INSTALL_PREFIX=' + str(self.toolchain_path), + '-DLLVM_TOOLCHAIN_TOOLS=' + ';'.join(self.tools), + ] + + cmd += cmake_options + cmd.append(str(llvm_project_dir / 'llvm')) + print(f'Running "{" ".join(cmd)}"') + + subprocess.check_call(cmd, cwd=build_dir) + + def build(self, build_dir: pathlib.Path): + print(f'Building LLVM tools in {build_dir}') + + cmake_targets = [] + for tool in self.tools: + if tool == 'wasm-ld': + cmake_targets.append('lld') + else: + cmake_targets.append(tool) + cmd = ['ninja'] + cmake_targets + print(f'Running "{" ".join(cmd)}"') + subprocess.check_call(cmd, cwd=build_dir) + # Symlink wasm-ld to lld + wasm_ld_path = build_dir / 'bin' / 'wasm-ld' + if 'wasm-ld' in self.tools and not wasm_ld_path.exists(): + wasm_ld_path.symlink_to(build_dir / 'bin' / 'lld') + + def __str__(self): + return 'Build LLVM tools (' + ', '.join(self.tools) + ')' + + +class SymlinkTool: + + def __init__( + self, tool_name: str, target: Optional[str] = None, + toolchain_path: pathlib.Path = None): + self.toolchain_path = toolchain_path + self.tool_name = tool_name + self.target = target + + def configure( + self, build_dir: pathlib.Path, llvm_project_dir: pathlib.Path): + pass + + def build(self, build_dir: pathlib.Path): + bin_dir = build_dir / 'bin' + bin_dir.mkdir(parents=True, exist_ok=True) + target_name = self.target if self.target else self.tool_name + if self.toolchain_path: + target = self.toolchain_path / 'usr' / 'bin' / target_name + else: + target = target_name + tool = bin_dir / self.tool_name + if not tool.exists(): + tool.symlink_to(target) + + def __str__(self): + if self.target: + return f'Symlink {self.tool_name} to {self.target}' + return 'Symlink ' + self.tool_name + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--toolchain', required=True) + args = parser.parse_args() + + toolchain_path = pathlib.Path(args.toolchain) + missing_tools = compute_missing_tools(toolchain_path) + + if len(missing_tools) == 0: + print('All required tools are available') + else: + print('Missing tools:') + for tool in missing_tools: + print(f' * {tool}') + + repo_root = pathlib.Path(REPO_ROOT) + build_dir = repo_root / '..' / 'build' / 'llvm-tools' + build_dir.mkdir(parents=True, exist_ok=True) + + llvm_project_dir = repo_root / '..' / 'llvm-project' + + products = compute_build_products(toolchain_path) + for product in products: + print(product) + product.configure(build_dir, llvm_project_dir) + product.build(build_dir) + + +if __name__ == '__main__': + main() diff --git a/tools/build/build-presets.ini b/tools/build/build-presets.ini index c8c0ecb7..9ff68130 100644 --- a/tools/build/build-presets.ini +++ b/tools/build/build-presets.ini @@ -10,6 +10,7 @@ compiler-vendor=swiftwasm enable-experimental-concurrency=1 enable-experimental-differentiable-programming=1 enable-experimental-distributed=1 +build-subdir=WebAssemblyCompiler [preset: webassembly-install] @@ -41,3 +42,11 @@ build-lld install-llvm install-swift install-swiftsyntax + +[preset: wasm_buildbot_osx_package] +mixin-preset=webassembly-host-install + +[preset: wasm_buildbot_linux] +mixin-preset=webassembly-host-install +libdispatch +install-libdispatch diff --git a/tools/build/build-target-toolchain.sh b/tools/build/build-target-toolchain.sh new file mode 100755 index 00000000..94515bd9 --- /dev/null +++ b/tools/build/build-target-toolchain.sh @@ -0,0 +1,199 @@ +#!/bin/bash +# +# Build the Swift standard library. + +set -euo pipefail +set -x + +print_help() { + echo "Usage: $0 [options]" + echo "" + echo "Options:" + echo " --help Display this help message." + echo " --llvm-bin Path to LLVM bin directory." + echo " --swift-bin Path to Swift bin directory." +} + +SOURCE_PATH="$(cd "$(dirname "$0")/../../.." && pwd)" +TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" +TARGET_BUILD_ROOT=$SOURCE_PATH/build/WebAssembly +PACKAGING_DIR="$SOURCE_PATH/build/Packaging" +TARGET_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/target-toolchain +BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" +WASI_SYSROOT_PATH="$BUILD_SDK_PATH/wasi-sysroot" + +build_target_toolchain() { + + local LLVM_BIN_DIR="$1" + local CLANG_BIN_DIR="$2" + local SWIFT_BIN_DIR="$3" + + local COMPILER_RT_BUILD_DIR="$TARGET_BUILD_ROOT/compiler-rt-wasi-wasm32" + local CLANG_VERSION + CLANG_VERSION="$(basename "$($CLANG_BIN_DIR/clang -print-resource-dir)")" + + cmake -B "$COMPILER_RT_BUILD_DIR" \ + -D CMAKE_TOOLCHAIN_FILE="$TOOLS_BUILD_PATH/compiler-rt-cache.cmake" \ + -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_C_COMPILER="$CLANG_BIN_DIR/clang" \ + -D CMAKE_CXX_COMPILER="$CLANG_BIN_DIR/clang++" \ + -D CMAKE_RANLIB="$LLVM_BIN_DIR/llvm-ranlib" \ + -D CMAKE_AR="$LLVM_BIN_DIR/llvm-ar" \ + -D CMAKE_C_COMPILER_LAUNCHER="$(which sccache)" \ + -D CMAKE_CXX_COMPILER_LAUNCHER="$(which sccache)" \ + -D CMAKE_INSTALL_PREFIX="$TARGET_TOOLCHAIN_DESTDIR/usr/lib/clang/$CLANG_VERSION/" \ + -D CMAKE_SYSROOT="${WASI_SYSROOT_PATH}" \ + -G Ninja \ + -S "$SOURCE_PATH/llvm-project/compiler-rt" + + ninja install -C "$COMPILER_RT_BUILD_DIR" + + local LLVM_TARGET_BUILD_DIR + + if [ -f "$SOURCE_PATH/build/llvm-tools/CMakeCache.txt" ]; then + LLVM_TARGET_BUILD_DIR="$SOURCE_PATH/build/llvm-tools" + else + LLVM_TARGET_BUILD_DIR="$TARGET_BUILD_ROOT/llvm-wasi-wasm32" + if [ ! -f "$LLVM_TARGET_BUILD_DIR/CMakeCache.txt" ]; then + # Only configure LLVM to use CMake functionalities in LLVM + cmake -B "$LLVM_TARGET_BUILD_DIR" \ + -D CMAKE_BUILD_TYPE=Release \ + -D LLVM_ENABLE_ZLIB=NO \ + -D LLVM_ENABLE_LIBXML2=NO \ + -D CMAKE_C_COMPILER="$CLANG_BIN_DIR/clang" \ + -D CMAKE_CXX_COMPILER="$CLANG_BIN_DIR/clang++" \ + -G Ninja \ + -S "$SOURCE_PATH/llvm-project/llvm" + fi + fi + + local SWIFT_STDLIB_BUILD_DIR="$TARGET_BUILD_ROOT/swift-stdlib-wasi-wasm32" + + # FIXME(katei): Platform/WASI is not recognized as a platform in LLVM, so it reports + # "Unable to determine platform" while handling LLVM options. + # Set WASI as a UNIX platform to spoof LLVM + # FIXME(katei): host-build clang's libcxx is capable with LLVM, but it somehow + # fails libcxx version check. So activate LLVM_COMPILER_CHECKED to spoof the checker + # SWIFT_DRIVER_TEST_OPTIONS is used to specify clang resource dir for wasm32-unknown-wasi + # because it's not built beside clang + # TODO(katei): Move SWIFT_STDLIB_HAS_ASL and SWIFT_RUNTIME_CRASH_REPORTER_CLIENT + # to cmake/caches/Runtime-WASI-wasm32.cmake + cmake -B "$SWIFT_STDLIB_BUILD_DIR" \ + -C "$SOURCE_PATH/swift/cmake/caches/Runtime-WASI-wasm32.cmake" \ + -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_C_COMPILER_LAUNCHER="$(which sccache)" \ + -D CMAKE_CXX_COMPILER_LAUNCHER="$(which sccache)" \ + -D CMAKE_C_COMPILER="$CLANG_BIN_DIR/clang" \ + -D CMAKE_CXX_COMPILER="$CLANG_BIN_DIR/clang++" \ + -D CMAKE_RANLIB="$LLVM_BIN_DIR/llvm-ranlib" \ + -D CMAKE_AR="$LLVM_BIN_DIR/llvm-ar" \ + -D CMAKE_INSTALL_PREFIX="$TARGET_TOOLCHAIN_DESTDIR/usr" \ + -D LLVM_DIR="$LLVM_TARGET_BUILD_DIR/lib/cmake/llvm/" \ + -D LLVM_COMPILER_CHECKED=YES \ + -D UNIX=1 \ + -D SWIFT_NATIVE_SWIFT_TOOLS_PATH="$SWIFT_BIN_DIR" \ + -D SWIFT_NATIVE_CLANG_TOOLS_PATH="$CLANG_BIN_DIR" \ + -D SWIFT_NATIVE_LLVM_TOOLS_PATH="$LLVM_BIN_DIR" \ + -D SWIFT_LIT_TEST_PATHS="$SWIFT_STDLIB_BUILD_DIR/test-wasi-wasm32/stdlib;$SWIFT_STDLIB_BUILD_DIR/test-wasi-wasm32/Concurrency/Runtime" \ + -D SWIFT_DRIVER_TEST_OPTIONS=" -Xclang-linker -resource-dir -Xclang-linker $COMPILER_RT_BUILD_DIR" \ + -D SWIFT_WASI_SYSROOT_PATH="$WASI_SYSROOT_PATH" \ + -D SWIFT_WASI_wasm32_ICU_UC_INCLUDE="$BUILD_SDK_PATH/icu/include" \ + -D SWIFT_WASI_wasm32_ICU_UC="$BUILD_SDK_PATH/icu/lib/libicuuc.a" \ + -D SWIFT_WASI_wasm32_ICU_I18N_INCLUDE="$BUILD_SDK_PATH/icu/include" \ + -D SWIFT_WASI_wasm32_ICU_I18N="$BUILD_SDK_PATH/icu/lib/libicui18n.a" \ + -D SWIFT_WASI_wasm32_ICU_DATA="$BUILD_SDK_PATH/icu/lib/libicudata.a" \ + -D SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING=YES \ + -D SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED=YES \ + -D SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING=YES \ + -D SWIFT_ENABLE_EXPERIMENTAL_REFLECTION=YES \ + -D SWIFT_PRIMARY_VARIANT_SDK=WASI \ + -D SWIFT_PRIMARY_VARIANT_ARCH=wasm32 \ + -D SWIFT_SDKS:STRING=WASI \ + -D SWIFT_STDLIB_HAS_ASL=NO \ + -D SWIFT_STDLIB_TRACING=NO \ + -D SWIFT_STDLIB_CONCURRENCY_TRACING=NO \ + -D SWIFT_RUNTIME_CRASH_REPORTER_CLIENT=NO \ + -D SWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS=NO \ + -D SWIFT_BUILD_DYNAMIC_SDK_OVERLAY=NO \ + -D SWIFT_BUILD_STATIC_SDK_OVERLAY=NO \ + -D SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE="$SOURCE_PATH/swift-syntax" \ + -D SWIFT_PATH_TO_STRING_PROCESSING_SOURCE="$SOURCE_PATH/swift-experimental-string-processing" \ + -G Ninja \ + -S "$SOURCE_PATH/swift" + + # FIXME(katei): 'sdk-overlay' is explicitly used to build libcxxshim.modulemap + # which is used only in tests, so 'ninja install' doesn't build it + # the header and modulemap custom targets should be added as dependency of install + ninja sdk-overlay install -C "$SWIFT_STDLIB_BUILD_DIR" + + # Link compiler-rt libs to stdlib build dir + mkdir -p "$SWIFT_STDLIB_BUILD_DIR/lib/clang/10.0.0/" + ln -fs "$COMPILER_RT_BUILD_DIR/lib" "$SWIFT_STDLIB_BUILD_DIR/lib/clang/10.0.0/lib" + + # Remove host CoreFoundation module directory to avoid module conflict + # while building Foundation + rm -rf "$TARGET_TOOLCHAIN_DESTDIR/usr/lib/swift_static/CoreFoundation" + + local CORELIBS_ARGS=( + "$TARGET_TOOLCHAIN_DESTDIR" + "$LLVM_BIN_DIR" + "$CLANG_BIN_DIR" + "$SWIFT_BIN_DIR" + "$WASI_SYSROOT_PATH" + ) + "$TOOLS_BUILD_PATH/build-foundation.sh" "${CORELIBS_ARGS[@]}" + "$TOOLS_BUILD_PATH/build-xctest.sh" "${CORELIBS_ARGS[@]}" + +} + +main() { + local OPTIONS_LLVM_BIN="" + local OPTIONS_CLANG_BIN="" + local OPTIONS_SWIFT_BIN="" + + while [[ $# -gt 0 ]]; do + case "$1" in + --llvm-bin) + OPTIONS_LLVM_BIN="$2" + shift 2 + ;; + --clang-bin) + OPTIONS_CLANG_BIN="$2" + shift 2 + ;; + --swift-bin) + OPTIONS_SWIFT_BIN="$2" + shift 2 + ;; + --help) + print_help + exit 0 + ;; + *) + echo "Unknown option: $1" + print_help + exit 1 + ;; + esac + done + + if [[ -z "$OPTIONS_LLVM_BIN" ]]; then + echo "Missing --llvm-bin option" + print_help + exit 1 + fi + + if [[ -z "$OPTIONS_SWIFT_BIN" ]]; then + echo "Missing --swift-bin option" + print_help + exit 1 + fi + + if [[ -z "$OPTIONS_CLANG_BIN" ]]; then + OPTIONS_CLANG_BIN="$OPTIONS_LLVM_BIN" + fi + + build_target_toolchain "$OPTIONS_LLVM_BIN" "$OPTIONS_CLANG_BIN" "$OPTIONS_SWIFT_BIN" +} + +main "$@" diff --git a/tools/build/build-toolchain.sh b/tools/build/build-toolchain.sh index ca411101..a99ac427 100755 --- a/tools/build/build-toolchain.sh +++ b/tools/build/build-toolchain.sh @@ -1,170 +1,61 @@ #!/bin/bash -set -ex -SOURCE_PATH="$(cd "$(dirname "$0")/../../.." && pwd)" -TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" - -BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" -WASI_SYSROOT_PATH="$BUILD_SDK_PATH/wasi-sysroot" - -case $(uname -s) in - Darwin) - HOST_SUFFIX=macosx-$(uname -m) - ;; - Linux) - HOST_SUFFIX=linux-$(uname -m) - ;; - *) - echo "Unrecognised platform $(uname -s)" - exit 1 - ;; -esac - -OPTIONS_BUILD_HOST_TOOLCHAIN=1 - -while [ $# -ne 0 ]; do - case "$1" in - --skip-build-host-toolchain) - OPTIONS_BUILD_HOST_TOOLCHAIN=0 - ;; - *) - echo "Unrecognised argument \"$1\"" - exit 1 - ;; - esac - shift -done - -PACKAGING_DIR="$SOURCE_PATH/build/Packaging" -HOST_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/host-toolchain -TARGET_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/target-toolchain - -HOST_BUILD_ROOT=$SOURCE_PATH/build/WebAssemblyCompiler -TARGET_BUILD_ROOT=$SOURCE_PATH/build/WebAssembly -HOST_BUILD_DIR=$HOST_BUILD_ROOT/Ninja-ReleaseAssert - -build_host_toolchain() { - # Build the host toolchain and SDK first. - env SWIFT_BUILD_ROOT="$HOST_BUILD_ROOT" \ - "$SOURCE_PATH/swift/utils/build-script" \ - --preset-file="$TOOLS_BUILD_PATH/build-presets.ini" \ - --preset=webassembly-host-install \ - --build-dir="$HOST_BUILD_DIR" \ - HOST_ARCHITECTURE="$(uname -m)" \ - INSTALL_DESTDIR="$HOST_TOOLCHAIN_DESTDIR" -} +set -eux -o pipefail -build_target_toolchain() { +REPO_PATH="$(cd "$(dirname $0)/../.." && pwd)" +SOURCE_PATH="$REPO_PATH/.." +SCHEME="${1:?"scheme is not specified"}" - local COMPILER_RT_BUILD_DIR="$TARGET_BUILD_ROOT/compiler-rt-wasi-wasm32" - cmake -B "$COMPILER_RT_BUILD_DIR" \ - -D CMAKE_TOOLCHAIN_FILE="$TOOLS_BUILD_PATH/compiler-rt-cache.cmake" \ - -D CMAKE_BUILD_TYPE=Release \ - -D CMAKE_C_COMPILER="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/clang" \ - -D CMAKE_CXX_COMPILER="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/clang++" \ - -D CMAKE_RANLIB="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/llvm-ranlib" \ - -D CMAKE_AR="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin/llvm-ar" \ - -D CMAKE_C_COMPILER_LAUNCHER="$(which sccache)" \ - -D CMAKE_CXX_COMPILER_LAUNCHER="$(which sccache)" \ - -D CMAKE_INSTALL_PREFIX="$TARGET_TOOLCHAIN_DESTDIR/usr/lib/clang/13.0.0/" \ - -D CMAKE_SYSROOT="${WASI_SYSROOT_PATH}" \ - -G Ninja \ - -S "$SOURCE_PATH/llvm-project/compiler-rt" - - ninja install -C "$COMPILER_RT_BUILD_DIR" - - # Only configure LLVM to use CMake functionalities in LLVM - local LLVM_TARGET_BUILD_DIR="$TARGET_BUILD_ROOT/llvm-wasi-wasm32" - cmake -B "$LLVM_TARGET_BUILD_DIR" \ - -D CMAKE_BUILD_TYPE=Release \ - -D LLVM_ENABLE_ZLIB=NO \ - -D LLVM_ENABLE_LIBXML2=NO \ - -G Ninja \ - -S "$SOURCE_PATH/llvm-project/llvm" +TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" - local SWIFT_STDLIB_BUILD_DIR="$TARGET_BUILD_ROOT/swift-stdlib-wasi-wasm32" +BUILD_DIR="$SOURCE_PATH/build" +PACKAGING_DIR="$BUILD_DIR/Packaging" - # FIXME(katei): Platform/WASI is not recognized as a platform in LLVM, so it reports - # "Unable to determine platform" while handling LLVM options. - # Set WASI as a UNIX platform to spoof LLVM - # FIXME(katei): host-build clang's libcxx is capable with LLVM, but it somehow - # fails libcxx version check. So activate LLVM_COMPILER_CHECKED to spoof the checker - # SWIFT_DRIVER_TEST_OPTIONS is used to specify clang resource dir for wasm32-unknown-wasi - # because it's not built beside clang - cmake -B "$SWIFT_STDLIB_BUILD_DIR" \ - -C "$SOURCE_PATH/swift/cmake/caches/Runtime-WASI-wasm32.cmake" \ - -D CMAKE_TOOLCHAIN_FILE="$TOOLS_BUILD_PATH/toolchain-wasi.cmake" \ - -D CMAKE_BUILD_TYPE=Release \ - -D CMAKE_C_COMPILER_LAUNCHER="$(which sccache)" \ - -D CMAKE_CXX_COMPILER_LAUNCHER="$(which sccache)" \ - -D CMAKE_INSTALL_PREFIX="$TARGET_TOOLCHAIN_DESTDIR/usr" \ - -D LLVM_BIN="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin" \ - -D LLVM_DIR="$LLVM_TARGET_BUILD_DIR/lib/cmake/llvm/" \ - -D LLVM_COMPILER_CHECKED=YES \ - -D UNIX=1 \ - -D SWIFT_NATIVE_SWIFT_TOOLS_PATH="$HOST_BUILD_DIR/swift-$HOST_SUFFIX/bin" \ - -D SWIFT_NATIVE_CLANG_TOOLS_PATH="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin" \ - -D SWIFT_NATIVE_LLVM_TOOLS_PATH="$HOST_BUILD_DIR/llvm-$HOST_SUFFIX/bin" \ - -D SWIFT_LIT_TEST_PATHS="$SWIFT_STDLIB_BUILD_DIR/test-wasi-wasm32/stdlib;$SWIFT_STDLIB_BUILD_DIR/test-wasi-wasm32/Concurrency/Runtime" \ - -D SWIFT_DRIVER_TEST_OPTIONS=" -Xclang-linker -resource-dir -Xclang-linker $COMPILER_RT_BUILD_DIR" \ - -D SWIFT_WASI_SYSROOT_PATH="$WASI_SYSROOT_PATH" \ - -D SWIFT_WASI_wasm32_ICU_UC_INCLUDE="$BUILD_SDK_PATH/icu/include" \ - -D SWIFT_WASI_wasm32_ICU_UC="$BUILD_SDK_PATH/icu/lib/libicuuc.a" \ - -D SWIFT_WASI_wasm32_ICU_I18N_INCLUDE="$BUILD_SDK_PATH/icu/include" \ - -D SWIFT_WASI_wasm32_ICU_I18N="$BUILD_SDK_PATH/icu/lib/libicui18n.a" \ - -D SWIFT_WASI_wasm32_ICU_DATA="$BUILD_SDK_PATH/icu/lib/libicudata.a" \ - -D SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING=YES \ - -D SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED=YES \ - -D SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING=YES \ - -D SWIFT_ENABLE_EXPERIMENTAL_REFLECTION=YES \ - -D SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE="$SOURCE_PATH/swift-syntax" \ - -D SWIFT_PATH_TO_STRING_PROCESSING_SOURCE="$SOURCE_PATH/swift-experimental-string-processing" \ - -G Ninja \ - -S "$SOURCE_PATH/swift" +need_build_cross_compiler() { + # If the scheme does not have any compiler patches, + # we can skip building compiler and use the prebuilt one. + # Return 0 if we need to build compiler, 1 otherwise. - # FIXME(katei): 'sdk-overlay' is explicitly used to build libcxxshim.modulemap - # which is used only in tests, so 'ninja install' doesn't build it - # the header and modulemap custom targets should be added as dependency of install - ninja sdk-overlay install -C "$SWIFT_STDLIB_BUILD_DIR" + local patch_dir="$REPO_PATH/schemes/$SCHEME/swift" - # Link compiler-rt libs to stdlib build dir - mkdir -p "$SWIFT_STDLIB_BUILD_DIR/lib/clang/10.0.0/" - ln -fs "$COMPILER_RT_BUILD_DIR/lib" "$SWIFT_STDLIB_BUILD_DIR/lib/clang/10.0.0/lib" + if [ ! -d "$patch_dir" ]; then + return 1 + fi - # Remove host CoreFoundation module directory to avoid module conflict - # while building Foundation - rm -rf "$TARGET_TOOLCHAIN_DESTDIR/usr/lib/swift_static/CoreFoundation" - "$TOOLS_BUILD_PATH/build-foundation.sh" "$TARGET_TOOLCHAIN_DESTDIR" "$WASI_SYSROOT_PATH" - "$TOOLS_BUILD_PATH/build-xctest.sh" "$TARGET_TOOLCHAIN_DESTDIR" "$WASI_SYSROOT_PATH" + if [ -z "$(ls -A "$patch_dir")" ]; then + return 1 + fi + python3 -c 'import sys, json; exit(0 if json.load(sys.stdin).get("build-compiler", True) else 1)' < $REPO_PATH/schemes/$SCHEME/manifest.json } -show_sccache_stats() { - # If sccache is installed in PATH - if command -v sccache &> /dev/null; then - sccache --show-stats - else - echo "sccache is not installed in PATH" - fi -} -if [ ${OPTIONS_BUILD_HOST_TOOLCHAIN} -eq 1 ]; then - build_host_toolchain - echo "==================================" - echo "Host toolchain built successfully!" - echo "==================================" - echo "" - echo "sccache stats:" - show_sccache_stats +if need_build_cross_compiler; then + echo "Building cross compiler..." + "$TOOLS_BUILD_PATH/build-host-toolchain.sh" + CROSS_COMPILER_DESTDIR=$PACKAGING_DIR/host-toolchain + + # package-toolchain will merge the base toolchain and + # target toolchain into one toolchain. We need to overwrite + # compilers in the base toolchain with the just built ones + # in the host toolchain. So we copy the host toolchain to + # target toolchain first. + TARGET_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/target-toolchain rm -rf "$TARGET_TOOLCHAIN_DESTDIR" mkdir -p "$TARGET_TOOLCHAIN_DESTDIR" - rsync -a "$HOST_TOOLCHAIN_DESTDIR/" "$TARGET_TOOLCHAIN_DESTDIR" + rsync -a "$CROSS_COMPILER_DESTDIR/" "$TARGET_TOOLCHAIN_DESTDIR" +else + echo "Using prebuilt cross compiler..." + "$TOOLS_BUILD_PATH/install-base-toolchain" --scheme "$SCHEME" + CROSS_COMPILER_DESTDIR=$PACKAGING_DIR/base-snapshot + # FIXME: Remove this once we fixed new swift-driver to use llvm-ar + # for WebAssembly target. + rm -f "$CROSS_COMPILER_DESTDIR/usr/bin/swift-driver" fi +"$TOOLS_BUILD_PATH/build-llvm-tools" --toolchain "$CROSS_COMPILER_DESTDIR" + +"$TOOLS_BUILD_PATH/build-target-toolchain.sh" \ + --llvm-bin "$BUILD_DIR/llvm-tools/bin" \ + --clang-bin "$CROSS_COMPILER_DESTDIR/usr/bin" \ + --swift-bin "$CROSS_COMPILER_DESTDIR/usr/bin" -build_target_toolchain -echo "====================================" -echo "Target toolchain built successfully!" -echo "====================================" -echo "" -echo "sccache stats:" -show_sccache_stats diff --git a/tools/build/build-xctest.sh b/tools/build/build-xctest.sh index de89cc7e..aa34e84f 100755 --- a/tools/build/build-xctest.sh +++ b/tools/build/build-xctest.sh @@ -1,7 +1,11 @@ #!/bin/bash set -ex DESTINATION_TOOLCHAIN=$1 -WASI_SYSROOT_PATH=$2 +LLVM_BIN_DIR=$2 +CLANG_BIN_DIR=$3 +SWIFT_BIN_DIR=$4 +WASI_SYSROOT_PATH=$5 + SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" @@ -13,11 +17,13 @@ cd $BUILD_DIR cmake -G Ninja \ -DCMAKE_BUILD_TYPE="Release" \ -DCMAKE_SYSROOT="$WASI_SYSROOT_PATH" \ - -DCMAKE_Swift_COMPILER="$DESTINATION_TOOLCHAIN/usr/bin/swiftc" \ + -DCMAKE_Swift_COMPILER="$SWIFT_BIN_DIR/swiftc" \ -DCMAKE_TOOLCHAIN_FILE="$TOOLS_BUILD_PATH/toolchain-wasi.cmake" \ - -DLLVM_BIN="$DESTINATION_TOOLCHAIN/usr/bin" \ + -DLLVM_BIN="$LLVM_BIN_DIR" \ + -DCLANG_BIN="$CLANG_BIN_DIR" \ -DBUILD_SHARED_LIBS=OFF \ -DCMAKE_Swift_COMPILER_FORCED=ON \ + -DCMAKE_Swift_FLAGS="-resource-dir $DESTINATION_TOOLCHAIN/usr/lib/swift_static" \ -DSWIFT_FOUNDATION_PATH=$DESTINATION_TOOLCHAIN/usr/lib/swift_static/wasi/wasm32 \ "${SOURCE_PATH}/swift-corelibs-xctest" diff --git a/tools/build/build_support/actions.py b/tools/build/build_support/actions.py index fa1a9ba7..f49d3e19 100644 --- a/tools/build/build_support/actions.py +++ b/tools/build/build_support/actions.py @@ -47,29 +47,39 @@ def run(self): self.system(*args) class ApplyPatchesAction(Action): + + def __init__(self, options, repo: str): + super().__init__(options) + self.repo = repo + self.repo_dir = f'../{repo}' + def run(self): - patches_dir = os.path.join('schemes', self.options.scheme, 'swift') + patches_dir = os.path.join('schemes', self.options.scheme, self.repo) + if not os.path.exists(patches_dir): + print("=====> No patches for scheme {} for repo {}".format(self.options.scheme, self.repo)) + return + patches = [os.path.join(patches_dir, path) for path in os.listdir(patches_dir)] patches.sort() print('=====> Applying {} patches for scheme {}'.format(len(patches), self.options.scheme)) # If the repository is not clean, abort - status = subprocess.run(['git', '-C', '../swift', 'status', '--porcelain']).returncode + status = subprocess.run(['git', '-C', self.repo_dir, 'status', '--porcelain']).returncode if status != 0: raise Exception('Repository is not clean. Please commit or stash your changes.') staging_branch = self.compute_unique_branch_name('swiftwasm-staging/{}'.format(self.options.tag)) - self.system('git', '-C', '../swift', 'switch', '-c', staging_branch) + self.system('git', '-C', self.repo_dir, 'switch', '-c', staging_branch) for patch in patches: repo_root_dirname = pathlib.Path(".").resolve().name relative_path = os.path.join("..", repo_root_dirname, patch) - self.system('git', '-C', '../swift', 'am', '--keep-non-patch', str(relative_path)) + self.system('git', '-C', self.repo_dir, 'am', '--keep-non-patch', str(relative_path)) def compute_unique_branch_name(self, basename): name = basename suffix = 0 while True: - result = subprocess.run(['git', '-C', '../swift', 'branch', '--list', name], stdout=subprocess.PIPE) + result = subprocess.run(['git', '-C', self.repo_dir, 'branch', '--list', name], stdout=subprocess.PIPE) if len(result.stdout) == 0: return name suffix += 1 @@ -94,27 +104,34 @@ def run(self): extension = platform_info.package_extension tarball_path = os.path.join('..', 'build', 'Packaging', f'base-snapshot.{extension}') + base_snapshot_dir = os.path.join('..', 'build', 'Packaging', 'base-snapshot') + + if os.path.exists(os.path.join(base_snapshot_dir, 'usr')): + print(f"=====> Base snapshot '{base_snapshot_dir}' already exists") + return + if not os.path.exists(tarball_path): print(f"=====> Downloading base snapshot {tarball_name}") os.makedirs(os.path.dirname(tarball_path), exist_ok=True) - self.system('curl', '-L', '-o', tarball_path, snapshot_url) - - base_snapshot_dir = os.path.join('..', 'build', 'Packaging', 'base-snapshot') - - if not os.path.exists(os.path.join(base_snapshot_dir, 'usr')): - print(f"=====> Unpacking base snapshot {tarball_name}") - os.makedirs(base_snapshot_dir, exist_ok=True) - if extension == "tar.gz": - self.system('tar', '-C', base_snapshot_dir, '--strip-components', '1', '-xzf', tarball_path) - elif extension == "pkg": - import tempfile - with tempfile.TemporaryDirectory() as tmpdir: - self.system('xar', '-xf', tarball_path, '-C', tmpdir) - old_cwd = os.getcwd() - os.chdir(base_snapshot_dir) - pkg_name = tarball_name.replace(".pkg", "-package.pkg") - self.system('cpio', '-i', '-I', os.path.join(tmpdir, pkg_name, 'Payload')) - os.chdir(old_cwd) + self.system('curl', '--fail', '-L', '-o', tarball_path, snapshot_url) + + print(f"=====> Unpacking base snapshot {tarball_name}") + os.makedirs(base_snapshot_dir, exist_ok=True) + if extension == "tar.gz": + self.system('tar', '-C', base_snapshot_dir, '--strip-components', '1', '-xzf', tarball_path) + elif extension == "pkg": + import tempfile + with tempfile.TemporaryDirectory() as tmpdir: + self.system('xar', '-xf', tarball_path, '-C', tmpdir) + old_cwd = os.getcwd() + os.chdir(base_snapshot_dir) + pkg_name = tarball_name.replace(".pkg", "-package.pkg") + self.system('cpio', '-i', '-I', os.path.join(tmpdir, pkg_name, 'Payload')) + os.chdir(old_cwd) + + if self.options.optimize_disk_footprint: + print(f"=====> Cleaning up base snapshot tarball '{tarball_path}'") + os.remove(tarball_path) class ActionRunner: def __init__(self, actions): @@ -133,6 +150,9 @@ def derive_options_from_args(argv, parser: argparse.ArgumentParser): parser.add_argument('--dry-run', help='Prints the commands that would be executed', action='store_true') parser.add_argument('-v', '--verbose', help='Prints the commands that are executed', action='store_true') parser.add_argument('--skip-history', help='Skip histories when obtaining sources', action='store_true') + parser.add_argument('--optimize-disk-footprint', + help='Minimize disk footprint', + action='store_true', default=os.environ.get('CI', None) is not None) options = parser.parse_args(argv) diff --git a/tools/build/build_support/llvm_tools.py b/tools/build/build_support/llvm_tools.py new file mode 100644 index 00000000..eb47da43 --- /dev/null +++ b/tools/build/build_support/llvm_tools.py @@ -0,0 +1,11 @@ +WASM_SPECIFIC_TOOLS_TO_INSTALL = [ + 'wasm-ld', + 'llvm-ar', + 'llvm-ranlib', +] + +WASM_SPECIFIC_TOOLS = WASM_SPECIFIC_TOOLS_TO_INSTALL + [ + # These tools are used by Swift test suite + 'llvm-nm', + 'FileCheck', +] \ No newline at end of file diff --git a/tools/build/ci.sh b/tools/build/ci.sh index 03239655..47297475 100755 --- a/tools/build/ci.sh +++ b/tools/build/ci.sh @@ -3,11 +3,11 @@ set -ex SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" -TOOLS_BUILD_PATH="$(cd "$(dirname $0)" && pwd)" +TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" SCHEME="${1:?"scheme is not specified"}" export SCCACHE_CACHE_SIZE="50G" export SCCACHE_DIR="$SOURCE_PATH/build-cache" -$TOOLS_BUILD_PATH/build-toolchain.sh +$TOOLS_BUILD_PATH/build-toolchain.sh "$SCHEME" $TOOLS_BUILD_PATH/package-toolchain --scheme "$SCHEME" --daily-snapshot diff --git a/tools/build/install-base-toolchain b/tools/build/install-base-toolchain new file mode 100755 index 00000000..d41ad7f7 --- /dev/null +++ b/tools/build/install-base-toolchain @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +import sys +import argparse +from build_support.actions import ActionRunner, DownloadBaseSnapshotAction, derive_options_from_args + + +def main(): + parser = argparse.ArgumentParser(description='A script to install base toolchain') + options = derive_options_from_args(sys.argv[1:], parser) + actions = [DownloadBaseSnapshotAction(options)] + ActionRunner(actions).run() + + +if __name__ == '__main__': + main() diff --git a/tools/build/install-build-sdk.sh b/tools/build/install-build-sdk.sh index 87e9882f..976e2910 100755 --- a/tools/build/install-build-sdk.sh +++ b/tools/build/install-build-sdk.sh @@ -6,7 +6,7 @@ SOURCE_PATH="$( cd "$(dirname "$0")/../../../" && pwd )" TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" SCHEME="${1:?"scheme is not specified"}" -PATCHES="$(cd "$(dirname "$0")/../../schemes/$SCHEME" && pwd)" +SCHEME_DIR="$(cd "$(dirname "$0")/../../schemes/$SCHEME" && pwd)" install_libxml2() { LIBXML2_URL="https://github.com/swiftwasm/libxml2-wasm/releases/download/1.0.0/libxml2-wasm32-unknown-wasi.tar.gz" @@ -16,19 +16,29 @@ install_libxml2() { } install_icu() { - ICU_URL="https://github.com/swiftwasm/icu4c-wasi/releases/download/0.5.0/icu4c-wasi.tar.xz" + local ICU_URL + ICU_URL="$(python3 -c 'import sys, json; print(json.load(sys.stdin)["icu4c"])' < "$SCHEME_DIR/manifest.json")" curl -L "$ICU_URL" | tar Jx rm -rf "$BUILD_SDK_PATH/icu" - mv icu_out "$BUILD_SDK_PATH/icu" + if [ -d "icu_out" ]; then + # Just for backward compatibility + mv icu_out "$BUILD_SDK_PATH/icu" + else + mv icu "$BUILD_SDK_PATH/icu" + fi } install_wasi-sysroot() { - WASI_SYSROOT_URL="https://github.com/swiftwasm/wasi-sdk-build/releases/download/wasi-sdk-14%2Bswiftwasm-2022-03-13/wasi-sysroot.tar.gz" + + local WASI_SYSROOT_URL + WASI_SYSROOT_URL="$(python3 -c 'import sys, json; print(json.load(sys.stdin)["wasi-sysroot"])' < "$SCHEME_DIR/manifest.json")" curl -L "$WASI_SYSROOT_URL" | tar xz mv "wasi-sysroot" "$BUILD_SDK_PATH/wasi-sysroot" - patch -p1 -d "$BUILD_SDK_PATH/wasi-sysroot" < "$PATCHES/wasi-sysroot"/*.patch + if [ -d "$SCHEME_DIR/wasi-sysroot" ]; then + patch -p1 -d "$BUILD_SDK_PATH/wasi-sysroot" < "$SCHEME_DIR/wasi-sysroot"/*.patch + fi } workdir=$(mktemp -d) diff --git a/tools/build/package-toolchain b/tools/build/package-toolchain index 67350aa0..14c1d712 100755 --- a/tools/build/package-toolchain +++ b/tools/build/package-toolchain @@ -5,6 +5,7 @@ import os import argparse from build_support.actions import Action, ActionRunner, DownloadBaseSnapshotAction, derive_options_from_args from build_support.platform import PlatformInfo +from build_support.llvm_tools import WASM_SPECIFIC_TOOLS_TO_INSTALL class SnapshotInfo: @@ -29,6 +30,7 @@ class PackageAction(Action): base_toolchain_path = os.path.join(packaging_dir, 'base-snapshot') target_toolchain_path = os.path.join(packaging_dir, 'target-toolchain') dist_toolchain_path = os.path.join(packaging_dir, 'dist-toolchain', self.snapshot_info.toolchain_name) + llvm_tools_path = os.path.join('..', 'build', 'llvm-tools') build_sdk_path = os.path.join('..', 'build-sdk') shutil.rmtree(os.path.dirname(dist_toolchain_path), ignore_errors=True) @@ -40,6 +42,8 @@ class PackageAction(Action): self.copy_icu_libs(build_sdk_path, dist_toolchain_path) + self.install_extra_llvm_tools(llvm_tools_path, dist_toolchain_path) + # FIXME: We now support only legacy driver because the new swift-driver doesn't have hacks for default # -sdk and forcing -use-static-resource-dir (-static-executable). # e.g. @@ -72,6 +76,30 @@ class PackageAction(Action): dest_path = os.path.join(dest_dir, lib) shutil.copy(os.path.join(icu_lib_dir, lib), dest_path) + def install_extra_llvm_tools(self, llvm_tools_path, dist_toolchain_path): + import shutil + print(f"=====> Installing extra LLVM tools") + llvm_tools_bin_dir = os.path.join(llvm_tools_path, 'bin') + install_bin_dir = os.path.join(dist_toolchain_path, 'usr', 'bin') + for tool_name in WASM_SPECIFIC_TOOLS_TO_INSTALL: + # Skip installing if the tool already exists + if os.path.exists(os.path.join(install_bin_dir, tool_name)): + continue + + tool_path = os.path.join(llvm_tools_bin_dir, tool_name) + # Copy the tool while preserving the symlink + print(f"Copying {tool_path} to {install_bin_dir}") + shutil.copy(tool_path, install_bin_dir, follow_symlinks=False) + + # If it's a symlink, copy the direct symlink target too + if os.path.islink(tool_path): + target = os.readlink(tool_path) + if os.path.exists(os.path.join(install_bin_dir, target)): + continue + target_path = os.path.join(llvm_tools_bin_dir, target) + print(f"Copying {target_path} to {install_bin_dir} (target of {tool_path})") + shutil.copy(target_path, install_bin_dir, follow_symlinks=False) + class DarwinInfoPlistAction(Action): diff --git a/tools/build/toolchain-wasi.cmake b/tools/build/toolchain-wasi.cmake index aee54bab..2836e6b5 100644 --- a/tools/build/toolchain-wasi.cmake +++ b/tools/build/toolchain-wasi.cmake @@ -3,8 +3,8 @@ set(CMAKE_SYSTEM_VERSION 1) set(CMAKE_SYSTEM_PROCESSOR wasm32) set(triple wasm32-unknown-wasi) -set(CMAKE_C_COMPILER "${LLVM_BIN}/clang") -set(CMAKE_CXX_COMPILER "${LLVM_BIN}/clang++") +set(CMAKE_C_COMPILER "${CLANG_BIN}/clang") +set(CMAKE_CXX_COMPILER "${CLANG_BIN}/clang++") set(CMAKE_AR "${LLVM_BIN}/llvm-ar" CACHE STRING "LLVM Archiver for wasm32") set(CMAKE_RANLIB "${LLVM_BIN}/llvm-ranlib" CACHE STRING "LLVM Ranlib for wasm32") set(CMAKE_C_COMPILER_TARGET ${triple} CACHE STRING "") diff --git a/tools/git-swift-workspace b/tools/git-swift-workspace index e74c4abd..47fb2ac9 100755 --- a/tools/git-swift-workspace +++ b/tools/git-swift-workspace @@ -15,7 +15,7 @@ from build.build_support.actions import * def main(): parser = argparse.ArgumentParser(description='A script to create a workspace for a Swift project applying patches') options = derive_options_from_args(sys.argv[1:], parser) - actions = [CloneAction(options), UpdateCheckoutAction(options), ApplyPatchesAction(options), CheckoutCorelibsAction(options)] + actions = [CloneAction(options), UpdateCheckoutAction(options), ApplyPatchesAction(options, 'swift'), CheckoutCorelibsAction(options), ApplyPatchesAction(options, 'swift-corelibs-foundation')] ActionRunner(actions).run()