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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 39 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

0 commit comments

Comments
 (0)