Skip to content

WASILibc module for main stream #1832

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

Merged
merged 1 commit into from
Sep 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,41 @@ getGlibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
return None;
}

static Optional<StringRef>
getWasiLibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
SmallVectorImpl<char> &buffer) {
StringRef platform = swift::getPlatformNameForTriple(triple);
StringRef arch = swift::getMajorArchitectureName(triple);

if (!Opts.SDKPath.empty()) {
buffer.clear();
buffer.append(Opts.SDKPath.begin(), Opts.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<std::string> &invocationArgStrs,
Expand Down Expand Up @@ -604,6 +639,10 @@ importer::getNormalInvocationArguments(

if (triple.isOSWASI()) {
invocationArgStrs.insert(invocationArgStrs.end(), {"-D_WASI_EMULATED_MMAN"});
SmallString<128> buffer;
if (auto path = getWasiLibcModuleMapPath(searchPathOpts, triple, buffer)) {
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
}
}

if (triple.isOSWindows()) {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES}
SWIFT_MODULE_DEPENDS_OPENBSD Glibc SwiftPrivateThreadExtras
SWIFT_MODULE_DEPENDS_CYGWIN Glibc SwiftPrivateThreadExtras
SWIFT_MODULE_DEPENDS_HAIKU Glibc SwiftPrivateThreadExtras
SWIFT_MODULE_DEPENDS_WASI Glibc
SWIFT_MODULE_DEPENDS_WASI WASILibc
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SwiftPrivateThreadExtras
SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
INSTALL_IN_COMPONENT stdlib-experimental
Expand Down
2 changes: 2 additions & 0 deletions stdlib/private/StdlibUnittest/RaceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import SwiftPrivateThreadExtras
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
import WinSDK
Expand Down
2 changes: 2 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibCoreExtras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import SwiftPrivateLibcExtras
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
#endif
Expand Down
2 changes: 2 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import Foundation
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
import WinSDK
Expand Down
2 changes: 2 additions & 0 deletions stdlib/private/StdlibUnittest/SymbolLookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
import WinSDK
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 Glibc
SWIFT_MODULE_DEPENDS_WASI WASILibc
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
INSTALL_IN_COMPONENT stdlib-experimental
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")
2 changes: 2 additions & 0 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import SwiftPrivate
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
import WinSDK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import SwiftPrivate
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
#endif
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 Glibc
SWIFT_MODULE_DEPENDS_WASI WASILibc
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
TARGET_SDKS ALL_APPLE_PLATFORMS CYGWIN FREEBSD OPENBSD HAIKU LINUX WINDOWS ANDROID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
import WinSDK
Expand Down
2 changes: 2 additions & 0 deletions stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
import WinSDK
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Differentiation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 Glibc
SWIFT_MODULE_DEPENDS_WASI WASILibc
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT

SWIFT_COMPILE_FLAGS
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/Differentiation/TgmathDerivatives.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import Swift

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin.C.tgmath
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import MSVCRT
#else
Expand Down
57 changes: 54 additions & 3 deletions stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,62 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU WASI
TARGET_SDKS ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU
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")
handle_gyb_source_single(wasi_libc_modulemap_target
SOURCE "wasi.modulemap.gyb"
OUTPUT "${wasi_libc_modulemap_out}"
FLAGS "-DGLIBC_INCLUDE_PATH=${SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY}"
"-DGLIBC_ARCH_INCLUDE_PATH=${SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY}")
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")

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} ${copy_wasi_libc_modulemap_static})

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)

add_custom_target(wasi_libc_modulemap
DEPENDS ${wasi_libc_modulemap_target} ${copy_wasi_libc_modulemap_static})
set_property(TARGET wasi_libc_modulemap PROPERTY FOLDER "Miscellaneous")
add_dependencies(sdk-overlay wasi_libc_modulemap)
endif()

add_swift_target_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
msvcrt.swift
${swift_platform_sources}
Expand All @@ -71,8 +123,7 @@ foreach(sdk ${SWIFT_SDKS})
NOT "${sdk}" STREQUAL "OPENBSD" AND
NOT "${sdk}" STREQUAL "ANDROID" AND
NOT "${sdk}" STREQUAL "CYGWIN" AND
NOT "${sdk}" STREQUAL "HAIKU" AND
NOT "${sdk}" STREQUAL "WASI")
NOT "${sdk}" STREQUAL "HAIKU")
continue()
endif()

Expand Down
69 changes: 69 additions & 0 deletions stdlib/public/Platform/WASI.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===----------------------------------------------------------------------===//
//
// 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 <math.h>
@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 <float.h>
@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)

// WebAssembly's error.h uses a macro that Swift can't import.
public let EINTR:Int32 = 27
public let EINVAL:Int32 = 28
Loading