Skip to content

Commit 9ac5acf

Browse files
Merge pull request #1773 from swiftwasm/wasi-module-5.3
Add WASI module to replace Glibc on WASI platform
2 parents 50925ce + a1c8f08 commit 9ac5acf

33 files changed

+556
-90
lines changed

.github/workflows/main.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
sudo apt-get purge libgcc-9-dev gcc-9 libstdc++-9-dev
2323
sudo swapoff -a
2424
sudo rm -f /swapfile
25+
sudo rm -rf /opt/hostedtoolcache
26+
sudo rm -rf /usr/share/dotnet
2527
sudo apt clean
2628
docker rmi $(docker image ls -aq)
2729
df -h
@@ -93,7 +95,7 @@ jobs:
9395
name: packaging-scripts
9496
path: utils/webassembly
9597
- name: Pack test results
96-
run: tar cJf swift-test-results.tar.gz ../build/*/swift-macosx-x86_64/swift-test-results
98+
run: tar cJf swift-test-results.tar.gz ../target-build/*/swift-macosx-x86_64/swift-test-results
9799
- name: Upload test results
98100
uses: actions/upload-artifact@v1
99101
with:

lib/ClangImporter/ClangImporter.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,41 @@ getGlibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
458458
return None;
459459
}
460460

461+
static Optional<StringRef>
462+
getWasiLibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
463+
SmallVectorImpl<char> &buffer) {
464+
StringRef platform = swift::getPlatformNameForTriple(triple);
465+
StringRef arch = swift::getMajorArchitectureName(triple);
466+
467+
if (!Opts.SDKPath.empty()) {
468+
buffer.clear();
469+
buffer.append(Opts.SDKPath.begin(), Opts.SDKPath.end());
470+
llvm::sys::path::append(buffer, "usr", "lib", "swift");
471+
llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap");
472+
473+
// Only specify the module map if that file actually exists. It may not;
474+
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
475+
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
476+
if (llvm::sys::fs::exists(buffer))
477+
return StringRef(buffer.data(), buffer.size());
478+
}
479+
480+
if (!Opts.RuntimeResourcePath.empty()) {
481+
buffer.clear();
482+
buffer.append(Opts.RuntimeResourcePath.begin(),
483+
Opts.RuntimeResourcePath.end());
484+
llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap");
485+
486+
// Only specify the module map if that file actually exists. It may not;
487+
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
488+
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
489+
if (llvm::sys::fs::exists(buffer))
490+
return StringRef(buffer.data(), buffer.size());
491+
}
492+
493+
return None;
494+
}
495+
461496
static void
462497
getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
463498
ASTContext &ctx,
@@ -601,6 +636,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
601636

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

606645
if (triple.isOSWindows()) {

stdlib/private/StdlibUnittest/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES}
4141
SWIFT_MODULE_DEPENDS_OPENBSD Glibc SwiftPrivateThreadExtras
4242
SWIFT_MODULE_DEPENDS_CYGWIN Glibc SwiftPrivateThreadExtras
4343
SWIFT_MODULE_DEPENDS_HAIKU Glibc SwiftPrivateThreadExtras
44-
SWIFT_MODULE_DEPENDS_WASI Glibc
44+
SWIFT_MODULE_DEPENDS_WASI WASILibc
4545
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SwiftPrivateThreadExtras
4646
SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
4747
INSTALL_IN_COMPONENT stdlib-experimental

stdlib/private/StdlibUnittest/RaceTest.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ import SwiftPrivateThreadExtras
4343
#endif
4444
#if os(macOS) || os(iOS)
4545
import Darwin
46-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
46+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
4747
import Glibc
48+
#elseif os(WASI)
49+
import WASILibc
4850
#elseif os(Windows)
4951
import MSVCRT
5052
import WinSDK

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import SwiftPrivate
1414
import SwiftPrivateLibcExtras
1515
#if os(macOS) || os(iOS)
1616
import Darwin
17-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
17+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
1818
import Glibc
19+
#elseif os(WASI)
20+
import WASILibc
1921
#elseif os(Windows)
2022
import MSVCRT
2123
#endif

stdlib/private/StdlibUnittest/StdlibUnittest.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import SwiftPrivateLibcExtras
2020
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
2121
import Foundation
2222
import Darwin
23-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
23+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
2424
import Glibc
25+
#elseif os(WASI)
26+
import WASILibc
2527
#elseif os(Windows)
2628
import MSVCRT
2729
import WinSDK

stdlib/private/StdlibUnittest/SymbolLookup.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1414
import Darwin
15-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
15+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
1616
import Glibc
17+
#elseif os(WASI)
18+
import WASILibc
1719
#elseif os(Windows)
1820
import MSVCRT
1921
import WinSDK

stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL
1818
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
1919
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
2020
SWIFT_MODULE_DEPENDS_HAIKU Glibc
21-
SWIFT_MODULE_DEPENDS_WASI Glibc
21+
SWIFT_MODULE_DEPENDS_WASI WASILibc
2222
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
2323
INSTALL_IN_COMPONENT stdlib-experimental
2424
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import SwiftPrivate
1414
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1515
import Darwin
16-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
16+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
1717
import Glibc
18+
#elseif os(WASI)
19+
import WASILibc
1820
#elseif os(Windows)
1921
import MSVCRT
2022
import WinSDK

stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import SwiftPrivate
1414
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1515
import Darwin
16-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
16+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
1717
import Glibc
18+
#elseif os(WASI)
19+
import WASILibc
1820
#elseif os(Windows)
1921
import MSVCRT
2022
#endif

stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU
1515
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
1616
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
1717
SWIFT_MODULE_DEPENDS_HAIKU Glibc
18-
SWIFT_MODULE_DEPENDS_WASI Glibc
18+
SWIFT_MODULE_DEPENDS_WASI WASILibc
1919
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
2020
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
2121
TARGET_SDKS ALL_APPLE_PLATFORMS CYGWIN FREEBSD OPENBSD HAIKU LINUX WINDOWS ANDROID

stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1919
import Darwin
20-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
20+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
2121
import Glibc
22+
#elseif os(WASI)
23+
import WASILibc
2224
#elseif os(Windows)
2325
import MSVCRT
2426
import WinSDK

stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1414
import Darwin
15-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
15+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
1616
import Glibc
17+
#elseif os(WASI)
18+
import WASILibc
1719
#elseif os(Windows)
1820
import MSVCRT
1921
import WinSDK

stdlib/public/Differentiation/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE
3131
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
3232
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
3333
SWIFT_MODULE_DEPENDS_HAIKU Glibc
34-
SWIFT_MODULE_DEPENDS_WASI Glibc
34+
SWIFT_MODULE_DEPENDS_WASI WASILibc
3535
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT
3636

3737
SWIFT_COMPILE_FLAGS

stdlib/public/Differentiation/TgmathDerivatives.swift.gyb

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import Swift
1616

1717
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1818
import Darwin.C.tgmath
19-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI)
19+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
2020
import Glibc
21+
#elseif os(WASI)
22+
import WASILibc
2123
#elseif os(Windows)
2224
import MSVCRT
2325
#else

stdlib/public/Platform/CMakeLists.txt

+54-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,62 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O
4444
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
4545
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
4646
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
47-
TARGET_SDKS ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU WASI
47+
TARGET_SDKS ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU
4848
INSTALL_IN_COMPONENT sdk-overlay
4949
DEPENDS glibc_modulemap)
5050

51+
if(WASI IN_LIST SWIFT_SDKS)
52+
set(arch_subdir "${SWIFT_SDK_WASI_LIB_SUBDIR}/wasm32")
53+
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
54+
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
55+
set(wasi_libc_modulemap_out "${module_dir}/wasi.modulemap")
56+
set(wasi_libc_modulemap_out_static "${module_dir_static}/wasi.modulemap")
57+
handle_gyb_source_single(wasi_libc_modulemap_target
58+
SOURCE "wasi.modulemap.gyb"
59+
OUTPUT "${wasi_libc_modulemap_out}"
60+
FLAGS "-DGLIBC_INCLUDE_PATH=${SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY}"
61+
"-DGLIBC_ARCH_INCLUDE_PATH=${SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY}")
62+
add_custom_command_target(
63+
copy_wasi_libc_modulemap_static
64+
COMMAND
65+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
66+
COMMAND
67+
"${CMAKE_COMMAND}" "-E" "copy" ${wasi_libc_modulemap_out} ${wasi_libc_modulemap_out_static}
68+
OUTPUT ${wasi_libc_modulemap_out_static}
69+
DEPENDS
70+
"${wasi_libc_modulemap_target}"
71+
COMMENT "Copying WASILlibc modulemap to static resources")
72+
73+
add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
74+
${swift_platform_sources}
75+
POSIXError.swift
76+
77+
GYB_SOURCES
78+
${swift_platform_gyb_sources}
79+
WASI.swift.gyb
80+
81+
SWIFT_COMPILE_FLAGS
82+
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
83+
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
84+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
85+
TARGET_SDKS WASI
86+
INSTALL_IN_COMPONENT sdk-overlay
87+
DEPENDS ${wasi_libc_modulemap_target} ${copy_wasi_libc_modulemap_static})
88+
89+
swift_install_in_component(FILES "${wasi_libc_modulemap_out}"
90+
DESTINATION "lib/swift/${arch_subdir}"
91+
COMPONENT sdk-overlay)
92+
93+
swift_install_in_component(FILES "${wasi_libc_modulemap_out}"
94+
DESTINATION "lib/swift_static/${arch_subdir}"
95+
COMPONENT sdk-overlay)
96+
97+
add_custom_target(wasi_libc_modulemap
98+
DEPENDS ${wasi_libc_modulemap_target} ${copy_wasi_libc_modulemap_static})
99+
set_property(TARGET wasi_libc_modulemap PROPERTY FOLDER "Miscellaneous")
100+
add_dependencies(sdk-overlay wasi_libc_modulemap)
101+
endif()
102+
51103
add_swift_target_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
52104
msvcrt.swift
53105
${swift_platform_sources}
@@ -71,8 +123,7 @@ foreach(sdk ${SWIFT_SDKS})
71123
NOT "${sdk}" STREQUAL "OPENBSD" AND
72124
NOT "${sdk}" STREQUAL "ANDROID" AND
73125
NOT "${sdk}" STREQUAL "CYGWIN" AND
74-
NOT "${sdk}" STREQUAL "HAIKU" AND
75-
NOT "${sdk}" STREQUAL "WASI")
126+
NOT "${sdk}" STREQUAL "HAIKU")
76127
continue()
77128
endif()
78129

stdlib/public/Platform/WASI.swift.gyb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import SwiftWASILibc // Clang module
14+
15+
// Constants defined by <math.h>
16+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.")
17+
public let M_PI = Double.pi
18+
19+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.")
20+
public let M_PI_2 = Double.pi / 2
21+
22+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.")
23+
public let M_PI_4 = Double.pi / 4
24+
25+
@available(swift, deprecated: 3.0, message: "Please use '2.squareRoot()'.")
26+
public let M_SQRT2 = 2.squareRoot()
27+
28+
@available(swift, deprecated: 3.0, message: "Please use '0.5.squareRoot()'.")
29+
public let M_SQRT1_2 = 0.5.squareRoot()
30+
31+
// Constants defined by <float.h>
32+
@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.")
33+
public let FLT_RADIX = Double.radix
34+
35+
%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL')]:
36+
// Where does the 1 come from? C counts the usually-implicit leading
37+
// significand bit, but Swift does not. Neither is really right or wrong.
38+
@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.")
39+
public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1
40+
41+
// Where does the 1 come from? C models floating-point numbers as having a
42+
// significand in [0.5, 1), but Swift (following IEEE 754) considers the
43+
// significand to be in [1, 2). This rationale applies to ${prefix}_MIN_EXP
44+
// as well.
45+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.")
46+
public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1
47+
48+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.")
49+
public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1
50+
51+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
52+
public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude
53+
54+
@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.")
55+
public let ${prefix}_EPSILON = ${type}.ulpOfOne
56+
57+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.")
58+
public let ${prefix}_MIN = ${type}.leastNormalMagnitude
59+
60+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
61+
public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude
62+
63+
%end
64+
65+
public let MAP_FAILED: UnsafeMutableRawPointer! = UnsafeMutableRawPointer(bitPattern: -1)
66+
67+
// WebAssembly's error.h uses a macro that Swift can't import.
68+
public let EINTR:Int32 = 27
69+
public let EINVAL:Int32 = 28

0 commit comments

Comments
 (0)