Skip to content

Commit 3566e6e

Browse files
authored
Merge pull request #29530 from swiftwasm/swiftwasm-ifdefs
[WebAssembly] Add ifdefs for the WASI target
2 parents 75713b8 + 6729790 commit 3566e6e

29 files changed

+139
-53
lines changed

include/swift/Basic/Lazy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <memory>
1717
#ifdef __APPLE__
1818
#include <dispatch/dispatch.h>
19+
#elif defined(__wasi__)
20+
// No pthread on wasi, see https://bugs.swift.org/browse/SR-12097 for more details.
1921
#else
2022
#include <mutex>
2123
#endif

include/swift/SwiftRemoteMirror/Platform.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern "C" {
1818
#endif
1919

2020
#if defined(swiftRemoteMirror_EXPORTS)
21-
# if defined(__ELF__)
21+
# if defined(__ELF__) || defined(__WASM__)
2222
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("protected")))
2323
# elif defined(__MACH__)
2424
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
@@ -30,9 +30,7 @@ extern "C" {
3030
# endif
3131
# endif
3232
#else
33-
# if defined(__ELF__)
34-
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
35-
# elif defined(__MACH__)
33+
# if defined(__ELF__) || defined(__MACH__) || defined(__WASM__)
3634
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
3735
# else
3836
# if defined(_WINDLL)

lib/IRGen/IRGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
166166
TargetOpts.FunctionSections = Opts.FunctionSections;
167167

168168
auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
169+
170+
// WebAssembly doesn't support atomics yet, see https://bugs.swift.org/browse/SR-12097
171+
// for more details.
172+
if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm())
173+
TargetOpts.ThreadModel = llvm::ThreadModel::Single;
174+
169175
clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts();
170176
return std::make_tuple(TargetOpts, ClangOpts.CPU, ClangOpts.Features, ClangOpts.Triple);
171177
}

stdlib/private/StdlibUnittest/InterceptTraps.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
14+
#if !defined(__wasi__)
1315
#include <stdio.h>
1416
#include <signal.h>
1517
#include <string.h>
@@ -48,6 +50,8 @@ static void CrashCatcher(int Sig) {
4850
_exit(0);
4951
}
5052

53+
#endif // __wasi__
54+
5155
#if defined(_WIN32)
5256
static LONG WINAPI
5357
VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) {
@@ -63,13 +67,16 @@ VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) {
6367

6468
return EXCEPTION_CONTINUE_SEARCH;
6569
}
66-
#endif
70+
#endif // _WIN32
6771

6872
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
6973
void installTrapInterceptor() {
7074
// Disable buffering on stdout so that everything is printed before crashing.
7175
setbuf(stdout, 0);
7276

77+
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
78+
#if !defined(__wasi__)
79+
7380
#if defined(_WIN32)
7481
_set_abort_behavior(0, _WRITE_ABORT_MSG);
7582
#endif
@@ -87,3 +94,4 @@ void installTrapInterceptor() {
8794
#endif
8895
}
8996

97+
#endif // !defined(__wasi__)

stdlib/private/StdlibUnittest/RaceTest.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import SwiftPrivateLibcExtras
4141
import SwiftPrivateThreadExtras
4242
#if os(macOS) || os(iOS)
4343
import Darwin
44-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
44+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
4545
import Glibc
4646
#elseif os(Windows)
4747
import MSVCRT
@@ -562,7 +562,9 @@ class _InterruptibleSleep {
562562
return
563563
}
564564

565-
var timeout = timeval(tv_sec: duration, tv_usec: 0)
565+
// WebAssembly/WASI on wasm32 is the only 32-bit platform with Int64 time_t,
566+
// needs an explicit conversion to `time_t` because of this.
567+
var timeout = timeval(tv_sec: time_t(duration), tv_usec: 0)
566568

567569
var readFDs = _stdlib_fd_set()
568570
var writeFDs = _stdlib_fd_set()

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftPrivate
1414
import SwiftPrivateLibcExtras
1515
#if os(macOS) || os(iOS)
1616
import Darwin
17-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
17+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
1818
import Glibc
1919
#elseif os(Windows)
2020
import MSVCRT

stdlib/private/StdlibUnittest/StdlibUnittest.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SwiftPrivateLibcExtras
1818
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1919
import Foundation
2020
import Darwin
21-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
21+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
2222
import Glibc
2323
#elseif os(Windows)
2424
import MSVCRT
@@ -748,6 +748,9 @@ extension ProcessTerminationStatus {
748748
case .signal(let signal):
749749
#if os(Windows)
750750
return CInt(signal) == SIGILL
751+
#elseif os(WASI)
752+
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
753+
return false
751754
#else
752755
return CInt(signal) == SIGILL || CInt(signal) == SIGTRAP
753756
#endif
@@ -1746,6 +1749,7 @@ public enum OSVersion : CustomStringConvertible {
17461749
case windowsCygnus
17471750
case windows
17481751
case haiku
1752+
case wasi
17491753

17501754
public var description: String {
17511755
switch self {
@@ -1777,6 +1781,8 @@ public enum OSVersion : CustomStringConvertible {
17771781
return "Windows"
17781782
case .haiku:
17791783
return "Haiku"
1784+
case .wasi:
1785+
return "WASI"
17801786
}
17811787
}
17821788
}
@@ -1821,6 +1827,8 @@ func _getOSVersion() -> OSVersion {
18211827
return .windows
18221828
#elseif os(Haiku)
18231829
return .haiku
1830+
#elseif os(WASI)
1831+
return .wasi
18241832
#else
18251833
let productVersion = _getSystemVersionPlistProperty("ProductVersion")!
18261834
let (major, minor, bugFix) = _parseDottedVersionTriple(productVersion)

stdlib/private/StdlibUnittest/SymbolLookup.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1414
import Darwin
15-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
15+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
1616
import Glibc
1717
#elseif os(Windows)
1818
import MSVCRT
@@ -35,6 +35,8 @@
3535
#endif
3636
#elseif os(Windows)
3737
let hStdlibCore: HMODULE = GetModuleHandleA("swiftCore.dll")!
38+
#elseif os(WASI)
39+
// WASI doesn't support dynamic linking yet.
3840
#else
3941
#error("Unsupported platform")
4042
#endif
@@ -43,6 +45,8 @@ public func pointerToSwiftCoreSymbol(name: String) -> UnsafeMutableRawPointer? {
4345
#if os(Windows)
4446
return unsafeBitCast(GetProcAddress(hStdlibCore, name),
4547
to: UnsafeMutableRawPointer?.self)
48+
#elseif os(WASI)
49+
fatalError("\(#function) is not supported on WebAssembly/WASI")
4650
#else
4751
return dlsym(RTLD_DEFAULT, name)
4852
#endif

stdlib/private/SwiftPrivateLibcExtras/Subprocess.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// posix_spawn is not available on Android or Windows (MSVC).
14-
#if !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CYGWIN__))
13+
// posix_spawn is not available on Android, HAIKU, WASI or Windows (MSVC).
14+
#if !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CYGWIN__)) && !defined(__wasi__)
1515

1616
#include "swift/Runtime/Config.h"
1717

@@ -54,5 +54,5 @@ int _stdlib_posix_spawn(pid_t *__restrict pid, const char * __restrict path,
5454
return posix_spawn(pid, path, file_actions, attrp, argv, envp);
5555
}
5656

57-
#endif // !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CGYWIN__))
57+
#endif // !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CGYWIN__)) && !defined(__wasi__)
5858

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
import SwiftPrivate
1414
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1515
import Darwin
16-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
16+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
1717
import Glibc
1818
#elseif os(Windows)
1919
import MSVCRT
2020
import WinSDK
2121
#endif
2222

23+
#if !os(WASI)
24+
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
2325
internal func _signalToString(_ signal: Int) -> String {
2426
switch CInt(signal) {
2527
case SIGILL: return "SIGILL"
@@ -34,6 +36,7 @@ internal func _signalToString(_ signal: Int) -> String {
3436
default: return "SIG???? (\(signal))"
3537
}
3638
}
39+
#endif
3740

3841
public enum ProcessTerminationStatus : CustomStringConvertible {
3942
case exit(Int)
@@ -44,7 +47,12 @@ public enum ProcessTerminationStatus : CustomStringConvertible {
4447
case .exit(let status):
4548
return "Exit(\(status))"
4649
case .signal(let signal):
50+
#if os(WASI)
51+
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
52+
fatalError("Signals are not supported on WebAssembly/WASI")
53+
#else
4754
return "Signal(\(_signalToString(signal)))"
55+
#endif
4856
}
4957
}
5058
}
@@ -141,6 +149,15 @@ public func waitProcess(_ process: HANDLE) -> ProcessTerminationStatus {
141149
}
142150
return .exit(Int(status))
143151
}
152+
#elseif os(WASI)
153+
// WASI doesn't support child processes
154+
public func spawnChild(_ args: [String])
155+
-> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) {
156+
fatalError("\(#function) is not supported on WebAssembly/WASI")
157+
}
158+
public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus {
159+
fatalError("\(#function) is not supported on WebAssembly/WASI")
160+
}
144161
#else
145162
// posix_spawn is not available on Android.
146163
// posix_spawn is not available on Haiku.

stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import SwiftPrivate
1414
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1515
import Darwin
16-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
16+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
1717
import Glibc
1818
#elseif os(Windows)
1919
import MSVCRT
2020
#endif
2121

2222
public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
23-
#if os(Android) || os(Haiku) || os(Windows)
23+
#if os(Android) || os(Haiku) || os(Windows) || os(WASI)
2424
preconditionFailure("mkstemps doesn't work on your platform")
2525
#else
2626
var utf8CStr = template.utf8CString
@@ -125,6 +125,8 @@ public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) {
125125
let ret = fds.withUnsafeMutableBufferPointer { unsafeFds -> CInt in
126126
#if os(Windows)
127127
return _pipe(unsafeFds.baseAddress, 0, 0)
128+
#elseif os(WASI)
129+
preconditionFailure("No pipes available on WebAssembly/WASI")
128130
#else
129131
return pipe(unsafeFds.baseAddress)
130132
#endif

stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU
1616
SWIFT_MODULE_DEPENDS_HAIKU Glibc
1717
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
1818
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
19+
TARGET_SDKS ALL_APPLE_PLATFORMS CYGWIN FREEBSD HAIKU LINUX WINDOWS
1920
INSTALL_IN_COMPONENT stdlib-experimental
2021
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")
2122

stdlib/public/Platform/Platform.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ public var SIG_IGN: _crt_signal_t {
366366
public var SIG_ERR: _crt_signal_t {
367367
return unsafeBitCast(-1, to: _crt_signal_t.self)
368368
}
369+
#elseif os(WASI)
370+
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
369371
#else
370372
internal var _ignore = _UnsupportedPlatformError()
371373
#endif
@@ -380,7 +382,7 @@ public var SEM_FAILED: UnsafeMutablePointer<sem_t>? {
380382
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
381383
// The value is ABI. Value verified to be correct for OS X, iOS, watchOS, tvOS.
382384
return UnsafeMutablePointer<sem_t>(bitPattern: -1)
383-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
385+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
384386
// The value is ABI. Value verified to be correct on Glibc.
385387
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
386388
#else

0 commit comments

Comments
 (0)