@@ -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+
461496static void
462497getNormalInvocationArguments (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 ()) {
0 commit comments