@@ -461,6 +461,41 @@ getGlibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
461461 return None;
462462}
463463
464+ static Optional<StringRef>
465+ getWasiLibcModuleMapPath (SearchPathOptions& Opts, llvm::Triple triple,
466+ SmallVectorImpl<char > &buffer) {
467+ StringRef platform = swift::getPlatformNameForTriple (triple);
468+ StringRef arch = swift::getMajorArchitectureName (triple);
469+
470+ if (!Opts.SDKPath .empty ()) {
471+ buffer.clear ();
472+ buffer.append (Opts.SDKPath .begin (), Opts.SDKPath .end ());
473+ llvm::sys::path::append (buffer, " usr" , " lib" , " swift" );
474+ llvm::sys::path::append (buffer, platform, arch, " wasi.modulemap" );
475+
476+ // Only specify the module map if that file actually exists. It may not;
477+ // for example in the case that `swiftc -target x86_64-unknown-linux-gnu
478+ // -emit-ir` is invoked using a Swift compiler not built for Linux targets.
479+ if (llvm::sys::fs::exists (buffer))
480+ return StringRef (buffer.data (), buffer.size ());
481+ }
482+
483+ if (!Opts.RuntimeResourcePath .empty ()) {
484+ buffer.clear ();
485+ buffer.append (Opts.RuntimeResourcePath .begin (),
486+ Opts.RuntimeResourcePath .end ());
487+ llvm::sys::path::append (buffer, platform, arch, " wasi.modulemap" );
488+
489+ // Only specify the module map if that file actually exists. It may not;
490+ // for example in the case that `swiftc -target x86_64-unknown-linux-gnu
491+ // -emit-ir` is invoked using a Swift compiler not built for Linux targets.
492+ if (llvm::sys::fs::exists (buffer))
493+ return StringRef (buffer.data (), buffer.size ());
494+ }
495+
496+ return None;
497+ }
498+
464499void
465500importer::getNormalInvocationArguments (
466501 std::vector<std::string> &invocationArgStrs,
@@ -604,6 +639,10 @@ importer::getNormalInvocationArguments(
604639
605640 if (triple.isOSWASI ()) {
606641 invocationArgStrs.insert (invocationArgStrs.end (), {" -D_WASI_EMULATED_MMAN" });
642+ SmallString<128 > buffer;
643+ if (auto path = getWasiLibcModuleMapPath (searchPathOpts, triple, buffer)) {
644+ invocationArgStrs.push_back ((Twine (" -fmodule-map-file=" ) + *path).str ());
645+ }
607646 }
608647
609648 if (triple.isOSWindows ()) {
0 commit comments