Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WebAssembly] Add ifdefs for the WASI target #29530

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/Basic/Lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <memory>
#ifdef __APPLE__
#include <dispatch/dispatch.h>
#elif defined(__wasi__)
// No pthread on wasi, see https://bugs.swift.org/browse/SR-12097 for more details.
#else
#include <mutex>
#endif
Expand Down
6 changes: 2 additions & 4 deletions include/swift/SwiftRemoteMirror/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
#endif

#if defined(swiftRemoteMirror_EXPORTS)
# if defined(__ELF__)
# if defined(__ELF__) || defined(__WASM__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("protected")))
# elif defined(__MACH__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
Expand All @@ -30,9 +30,7 @@ extern "C" {
# endif
# endif
#else
# if defined(__ELF__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
# elif defined(__MACH__)
# if defined(__ELF__) || defined(__MACH__) || defined(__WASM__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
# else
# if defined(_WINDLL)
Expand Down
6 changes: 6 additions & 0 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
TargetOpts.FunctionSections = Opts.FunctionSections;

auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());

// WebAssembly doesn't support atomics yet, see https://bugs.swift.org/browse/SR-12097
// for more details.
if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm())
TargetOpts.ThreadModel = llvm::ThreadModel::Single;

clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts();
return std::make_tuple(TargetOpts, ClangOpts.CPU, ClangOpts.Features, ClangOpts.Triple);
}
Expand Down
10 changes: 9 additions & 1 deletion stdlib/private/StdlibUnittest/InterceptTraps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

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

#endif // __wasi__

#if defined(_WIN32)
static LONG WINAPI
VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) {
Expand All @@ -63,13 +67,16 @@ VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) {

return EXCEPTION_CONTINUE_SEARCH;
}
#endif
#endif // _WIN32

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

// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
#if !defined(__wasi__)

#if defined(_WIN32)
_set_abort_behavior(0, _WRITE_ABORT_MSG);
#endif
Expand All @@ -87,3 +94,4 @@ void installTrapInterceptor() {
#endif
}

#endif // !defined(__wasi__)
6 changes: 4 additions & 2 deletions stdlib/private/StdlibUnittest/RaceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import SwiftPrivateLibcExtras
import SwiftPrivateThreadExtras
#if os(macOS) || os(iOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
import Glibc
#elseif os(Windows)
import MSVCRT
Expand Down Expand Up @@ -562,7 +562,9 @@ class _InterruptibleSleep {
return
}

var timeout = timeval(tv_sec: duration, tv_usec: 0)
// WebAssembly/WASI on wasm32 is the only 32-bit platform with Int64 time_t,
// needs an explicit conversion to `time_t` because of this.
var timeout = timeval(tv_sec: time_t(duration), tv_usec: 0)
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code would still compile on other platforms if you the time_t(duration) variant everywhere (no conditional compilation).


var readFDs = _stdlib_fd_set()
var writeFDs = _stdlib_fd_set()
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/StdlibCoreExtras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftPrivate
import SwiftPrivateLibcExtras
#if os(macOS) || os(iOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
import Glibc
#elseif os(Windows)
import MSVCRT
Expand Down
10 changes: 9 additions & 1 deletion stdlib/private/StdlibUnittest/StdlibUnittest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SwiftPrivateLibcExtras
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Foundation
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
import Glibc
#elseif os(Windows)
import MSVCRT
Expand Down Expand Up @@ -748,6 +748,9 @@ extension ProcessTerminationStatus {
case .signal(let signal):
#if os(Windows)
return CInt(signal) == SIGILL
#elseif os(WASI)
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
return false
#else
return CInt(signal) == SIGILL || CInt(signal) == SIGTRAP
#endif
Expand Down Expand Up @@ -1746,6 +1749,7 @@ public enum OSVersion : CustomStringConvertible {
case windowsCygnus
case windows
case haiku
case wasi

public var description: String {
switch self {
Expand Down Expand Up @@ -1777,6 +1781,8 @@ public enum OSVersion : CustomStringConvertible {
return "Windows"
case .haiku:
return "Haiku"
case .wasi:
return "WASI"
}
}
}
Expand Down Expand Up @@ -1821,6 +1827,8 @@ func _getOSVersion() -> OSVersion {
return .windows
#elseif os(Haiku)
return .haiku
#elseif os(WASI)
return .wasi
#else
let productVersion = _getSystemVersionPlistProperty("ProductVersion")!
let (major, minor, bugFix) = _parseDottedVersionTriple(productVersion)
Expand Down
6 changes: 5 additions & 1 deletion stdlib/private/StdlibUnittest/SymbolLookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
import Glibc
#elseif os(Windows)
import MSVCRT
Expand All @@ -35,6 +35,8 @@
#endif
#elseif os(Windows)
let hStdlibCore: HMODULE = GetModuleHandleA("swiftCore.dll")!
#elseif os(WASI)
// WASI doesn't support dynamic linking yet.
#else
#error("Unsupported platform")
#endif
Expand All @@ -43,6 +45,8 @@ public func pointerToSwiftCoreSymbol(name: String) -> UnsafeMutableRawPointer? {
#if os(Windows)
return unsafeBitCast(GetProcAddress(hStdlibCore, name),
to: UnsafeMutableRawPointer?.self)
#elseif os(WASI)
fatalError("\(#function) is not supported on WebAssembly/WASI")
#else
return dlsym(RTLD_DEFAULT, name)
#endif
Expand Down
6 changes: 3 additions & 3 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//

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

#include "swift/Runtime/Config.h"

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

#endif // !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CGYWIN__))
#endif // !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CGYWIN__)) && !defined(__wasi__)

19 changes: 18 additions & 1 deletion stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import SwiftPrivate
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
import Glibc
#elseif os(Windows)
import MSVCRT
import WinSDK
#endif

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

public enum ProcessTerminationStatus : CustomStringConvertible {
case exit(Int)
Expand All @@ -44,7 +47,12 @@ public enum ProcessTerminationStatus : CustomStringConvertible {
case .exit(let status):
return "Exit(\(status))"
case .signal(let signal):
#if os(WASI)
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
fatalError("Signals are not supported on WebAssembly/WASI")
#else
return "Signal(\(_signalToString(signal)))"
#endif
}
}
}
Expand Down Expand Up @@ -141,6 +149,15 @@ public func waitProcess(_ process: HANDLE) -> ProcessTerminationStatus {
}
return .exit(Int(status))
}
#elseif os(WASI)
// WASI doesn't support child processes
public func spawnChild(_ args: [String])
-> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) {
fatalError("\(#function) is not supported on WebAssembly/WASI")
}
public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus {
fatalError("\(#function) is not supported on WebAssembly/WASI")
}
#else
// posix_spawn is not available on Android.
// posix_spawn is not available on Haiku.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import SwiftPrivate
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
import Glibc
#elseif os(Windows)
import MSVCRT
#endif

public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
#if os(Android) || os(Haiku) || os(Windows)
#if os(Android) || os(Haiku) || os(Windows) || os(WASI)
preconditionFailure("mkstemps doesn't work on your platform")
#else
var utf8CStr = template.utf8CString
Expand Down Expand Up @@ -125,6 +125,8 @@ public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) {
let ret = fds.withUnsafeMutableBufferPointer { unsafeFds -> CInt in
#if os(Windows)
return _pipe(unsafeFds.baseAddress, 0, 0)
#elseif os(WASI)
preconditionFailure("No pipes available on WebAssembly/WASI")
#else
return pipe(unsafeFds.baseAddress)
#endif
Expand Down
1 change: 1 addition & 0 deletions stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU
SWIFT_MODULE_DEPENDS_HAIKU Glibc
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
TARGET_SDKS ALL_APPLE_PLATFORMS CYGWIN FREEBSD HAIKU LINUX WINDOWS
INSTALL_IN_COMPONENT stdlib-experimental
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")

4 changes: 3 additions & 1 deletion stdlib/public/Platform/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ public var SIG_IGN: _crt_signal_t {
public var SIG_ERR: _crt_signal_t {
return unsafeBitCast(-1, to: _crt_signal_t.self)
}
#elseif os(WASI)
// No signals support on WASI yet, see https://github.com/WebAssembly/WASI/issues/166.
#else
internal var _ignore = _UnsupportedPlatformError()
#endif
Expand All @@ -380,7 +382,7 @@ public var SEM_FAILED: UnsafeMutablePointer<sem_t>? {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
// The value is ABI. Value verified to be correct for OS X, iOS, watchOS, tvOS.
return UnsafeMutablePointer<sem_t>(bitPattern: -1)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does sem_open() exist and work on WASI? If not, ifdef out the whole variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does exist and whole semaphore.h is available too.

// The value is ABI. Value verified to be correct on Glibc.
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
#else
Expand Down